From d9b8c539de926fe6ec1f3d2be2059b33176f8f32 Mon Sep 17 00:00:00 2001 From: echobt Date: Wed, 18 Feb 2026 22:15:35 +0000 Subject: [PATCH] feat: WASM-only architecture with sudo challenge management and P2P distribution Remove all legacy Docker/container code (challenge-orchestrator, secure-container-runtime) and clean up container references from wasm-runtime-interface, challenge-registry, and SDK. Add sudo actions for challenge management (AddChallenge, RemoveChallenge, EditChallenge, StopNetwork) with hotkey-based consensus verification. Store per-challenge emission weights in blockchain state with proper versioning (V7). Implement WASM module P2P distribution via DataRequest/DataResponse messages with SHA-256 peer verification. New validators automatically download missing modules on startup. Key changes: - Delete crates/challenge-orchestrator and crates/secure-container-runtime - Remove Docker-in-Docker from docker-compose.yml (privileged, docker.sock) - Add challenge weight system integrated into emission calculations - Add state mutation methods for sudo-initiated challenge operations - Add P2P message types for WASM module distribution - Increment state version to V7 with migration from V6 - Update documentation to reflect WASM-only architecture - Add comprehensive test suite for sudo actions --- .cargo/config.toml | 40 + .dockerignore | 27 + .env.example | 8 + .githooks/install.sh | 33 +- .githooks/pre-commit | 33 +- .githooks/pre-push | 115 +- .github/ci-trigger | 1 - .github/workflows/ci.yml | 267 +- .github/workflows/release.yml | 67 +- .gitignore | 31 +- .release-please-manifest.json | 2 +- AGENTS.md | 307 +- CHANGELOG.md | 630 +- Cargo.lock | 9962 +++++++++++++++-- Cargo.toml | 107 +- Dockerfile | 154 + README.md | 344 +- assets/banner.jpg | Bin 203957 -> 135143 bytes bins/mock-subtensor/Cargo.toml | 39 + bins/mock-subtensor/Dockerfile | 71 + bins/mock-subtensor/src/chain.rs | 309 + bins/mock-subtensor/src/jsonrpc.rs | 968 ++ bins/mock-subtensor/src/main.rs | 194 + bins/mock-subtensor/src/state.rs | 600 + bins/mock-subtensor/src/websocket.rs | 456 + bins/platform-cli/Cargo.toml | 23 + bins/platform-cli/src/main.rs | 694 ++ bins/utils/Cargo.toml | 8 + bins/utils/src/main.rs | 13 + bins/validator-node/Cargo.toml | 48 + bins/validator-node/src/challenge_storage.rs | 55 + bins/validator-node/src/main.rs | 1569 +++ bins/validator-node/src/wasm_executor.rs | 853 ++ challenges/.gitkeep | 1 + challenges/README.md | 76 + cli/Cargo.toml | 24 - cli/src/app.rs | 295 - cli/src/main.rs | 105 - cli/src/rpc.rs | 264 - cli/src/ui.rs | 423 - crates/bittensor-integration/Cargo.toml | 40 + .../examples/check_metagraph.rs | 72 + .../bittensor-integration/src/block_sync.rs | 602 + .../src/challenge_weight_collector.rs | 846 ++ crates/bittensor-integration/src/client.rs | 238 + crates/bittensor-integration/src/config.rs | 75 + crates/bittensor-integration/src/lib.rs | 50 + crates/bittensor-integration/src/mock.rs | 573 + crates/bittensor-integration/src/tests.rs | 411 + .../src/validator_sync.rs | 460 + crates/bittensor-integration/src/weights.rs | 1432 +++ crates/challenge-registry/Cargo.toml | 43 + crates/challenge-registry/src/discovery.rs | 387 + crates/challenge-registry/src/error.rs | 248 + crates/challenge-registry/src/health.rs | 437 + crates/challenge-registry/src/lib.rs | 41 + crates/challenge-registry/src/lifecycle.rs | 432 + crates/challenge-registry/src/migration.rs | 518 + crates/challenge-registry/src/registry.rs | 616 + crates/challenge-registry/src/state.rs | 316 + crates/challenge-registry/src/version.rs | 408 + crates/challenge-sdk-wasm/Cargo.toml | 17 + crates/challenge-sdk-wasm/src/alloc_impl.rs | 71 + .../challenge-sdk-wasm/src/host_functions.rs | 308 + crates/challenge-sdk-wasm/src/lib.rs | 267 + crates/challenge-sdk-wasm/src/llm_types.rs | 30 + crates/challenge-sdk-wasm/src/types.rs | 170 + crates/challenge-sdk/Cargo.toml | 51 + crates/challenge-sdk/src/data.rs | 618 + crates/challenge-sdk/src/database.rs | 579 + crates/challenge-sdk/src/decentralized.rs | 537 + crates/challenge-sdk/src/error.rs | 138 + crates/challenge-sdk/src/lib.rs | 151 + crates/challenge-sdk/src/p2p_client.rs | 1028 ++ crates/challenge-sdk/src/routes.rs | 897 ++ crates/challenge-sdk/src/server.rs | 1930 ++++ crates/challenge-sdk/src/submission_types.rs | 535 + crates/challenge-sdk/src/test_challenge.rs | 262 + crates/challenge-sdk/src/types.rs | 494 + crates/challenge-sdk/src/weight_types.rs | 172 + crates/challenge-sdk/src/weights.rs | 216 + crates/core/Cargo.toml | 26 + crates/core/src/challenge.rs | 580 + crates/core/src/checkpoint.rs | 738 ++ crates/core/src/constants.rs | 123 + crates/core/src/crypto.rs | 442 + crates/core/src/error.rs | 116 + crates/core/src/lib.rs | 32 + crates/core/src/message.rs | 1368 +++ crates/core/src/restoration.rs | 618 + crates/core/src/schema_guard.rs | 523 + crates/core/src/state.rs | 676 ++ crates/core/src/state_versioning.rs | 653 ++ crates/core/src/types.rs | 522 + crates/distributed-storage/Cargo.toml | 38 + .../src/challenge_store.rs | 1247 +++ crates/distributed-storage/src/dht.rs | 981 ++ crates/distributed-storage/src/error.rs | 281 + crates/distributed-storage/src/lib.rs | 373 + crates/distributed-storage/src/local.rs | 1582 +++ crates/distributed-storage/src/query.rs | 619 + crates/distributed-storage/src/replication.rs | 595 + .../src/state_consensus.rs | 1559 +++ crates/distributed-storage/src/store.rs | 564 + crates/distributed-storage/src/submission.rs | 688 ++ .../src/validated_storage.rs | 1105 ++ crates/distributed-storage/src/weights.rs | 690 ++ crates/epoch/Cargo.toml | 25 + crates/epoch/src/aggregator.rs | 568 + crates/epoch/src/commit_reveal.rs | 816 ++ crates/epoch/src/lib.rs | 198 + crates/epoch/src/manager.rs | 483 + crates/epoch/src/mechanism_weights.rs | 1137 ++ crates/p2p-consensus/Cargo.toml | 37 + crates/p2p-consensus/src/config.rs | 163 + crates/p2p-consensus/src/consensus.rs | 1259 +++ crates/p2p-consensus/src/lib.rs | 97 + crates/p2p-consensus/src/messages.rs | 867 ++ crates/p2p-consensus/src/network.rs | 1433 +++ crates/p2p-consensus/src/state.rs | 1375 +++ crates/p2p-consensus/src/validator.rs | 622 + crates/rpc-server/Cargo.toml | 40 + crates/rpc-server/src/auth.rs | 175 + crates/rpc-server/src/handlers.rs | 1199 ++ crates/rpc-server/src/health.rs | 380 + crates/rpc-server/src/jsonrpc.rs | 2614 +++++ crates/rpc-server/src/lib.rs | 35 + crates/rpc-server/src/server.rs | 811 ++ crates/rpc-server/src/types.rs | 517 + crates/storage/Cargo.toml | 32 + crates/storage/src/blockchain.rs | 1255 +++ crates/storage/src/distributed.rs | 2031 ++++ crates/storage/src/dynamic.rs | 1288 +++ crates/storage/src/lib.rs | 517 + crates/storage/src/metadata.rs | 985 ++ crates/storage/src/migration.rs | 1653 +++ crates/storage/src/optimized.rs | 718 ++ crates/storage/src/types.rs | 632 ++ crates/subnet-manager/Cargo.toml | 33 + crates/subnet-manager/src/commands.rs | 1922 ++++ crates/subnet-manager/src/config.rs | 481 + crates/subnet-manager/src/health.rs | 1081 ++ crates/subnet-manager/src/lib.rs | 23 + crates/subnet-manager/src/recovery.rs | 1269 +++ crates/subnet-manager/src/snapshot.rs | 841 ++ crates/subnet-manager/src/update.rs | 1361 +++ crates/wasm-runtime-interface/Cargo.toml | 19 + crates/wasm-runtime-interface/src/bridge.rs | 206 + .../wasm-runtime-interface/src/consensus.rs | 438 + .../wasm-runtime-interface/src/container.rs | 197 + crates/wasm-runtime-interface/src/data.rs | 455 + crates/wasm-runtime-interface/src/exec.rs | 620 + crates/wasm-runtime-interface/src/lib.rs | 800 ++ crates/wasm-runtime-interface/src/llm.rs | 480 + crates/wasm-runtime-interface/src/network.rs | 1369 +++ crates/wasm-runtime-interface/src/runtime.rs | 679 ++ crates/wasm-runtime-interface/src/sandbox.rs | 762 ++ crates/wasm-runtime-interface/src/storage.rs | 875 ++ crates/wasm-runtime-interface/src/terminal.rs | 762 ++ crates/wasm-runtime-interface/src/time.rs | 221 + docker-compose.yml | 88 + docker/Dockerfile.challenge | 92 + docker/Dockerfile.validator | 87 + docker/docker-compose.yml | 70 + docker/entrypoint.sh | 45 + docs/architecture.md | 410 +- docs/challenge-integration.md | 232 + docs/challenges.md | 67 + docs/miner/how-to-mine.md | 312 - docs/miner/submission.md | 188 - docs/operations/validator.md | 173 + docs/security.md | 76 + docs/validator.md | 150 + docs/validator/setup.md | 356 - docs/validator_wasm_audit.md | 42 + pr_diff_full.txt | 1191 ++ release-please-config.json | 7 +- rust-toolchain-nightly.toml | 6 + rust-toolchain.toml | 3 + scripts/build-wasm.sh | 109 + scripts/install-docker.sh | 183 + scripts/setup-hooks.sh | 9 + scripts/test-all.sh | 73 + scripts/test-comprehensive.sh | 195 + scripts/test-harness.sh | 201 + scripts/test-single-validator.sh | 22 + scripts/verify-nightly-config.sh | 224 + tests/Cargo.toml | 62 + tests/bittensor_tests.rs | 730 ++ tests/blockchain_state_tests.rs | 615 + tests/checkpoint_tests.rs | 524 + tests/e2e_tests.rs | 266 + tests/epoch_tests.rs | 938 ++ tests/error_cases.rs | 410 + tests/rpc_server_tests.rs | 413 + tests/storage_tests.rs | 414 + tests/sudo_action_tests.rs | 588 + wasm/Cargo.toml | 13 - wasm/src/agent_storage.rs | 88 - wasm/src/ast_validation.rs | 134 - wasm/src/dataset.rs | 115 - wasm/src/lib.rs | 454 - wasm/src/llm_review.rs | 356 - wasm/src/routes.rs | 530 - wasm/src/scoring.rs | 183 - wasm/src/submission.rs | 102 - wasm/src/tasks.rs | 53 - wasm/src/timeout_handler.rs | 102 - wasm/src/types.rs | 331 - 209 files changed, 96838 insertions(+), 6721 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .dockerignore create mode 100644 .env.example mode change 100755 => 100644 .githooks/install.sh delete mode 100644 .github/ci-trigger create mode 100644 Dockerfile create mode 100644 bins/mock-subtensor/Cargo.toml create mode 100644 bins/mock-subtensor/Dockerfile create mode 100644 bins/mock-subtensor/src/chain.rs create mode 100644 bins/mock-subtensor/src/jsonrpc.rs create mode 100644 bins/mock-subtensor/src/main.rs create mode 100644 bins/mock-subtensor/src/state.rs create mode 100644 bins/mock-subtensor/src/websocket.rs create mode 100644 bins/platform-cli/Cargo.toml create mode 100644 bins/platform-cli/src/main.rs create mode 100644 bins/utils/Cargo.toml create mode 100644 bins/utils/src/main.rs create mode 100644 bins/validator-node/Cargo.toml create mode 100644 bins/validator-node/src/challenge_storage.rs create mode 100644 bins/validator-node/src/main.rs create mode 100644 bins/validator-node/src/wasm_executor.rs create mode 100644 challenges/.gitkeep create mode 100644 challenges/README.md delete mode 100644 cli/Cargo.toml delete mode 100644 cli/src/app.rs delete mode 100644 cli/src/main.rs delete mode 100644 cli/src/rpc.rs delete mode 100644 cli/src/ui.rs create mode 100644 crates/bittensor-integration/Cargo.toml create mode 100644 crates/bittensor-integration/examples/check_metagraph.rs create mode 100644 crates/bittensor-integration/src/block_sync.rs create mode 100644 crates/bittensor-integration/src/challenge_weight_collector.rs create mode 100644 crates/bittensor-integration/src/client.rs create mode 100644 crates/bittensor-integration/src/config.rs create mode 100644 crates/bittensor-integration/src/lib.rs create mode 100644 crates/bittensor-integration/src/mock.rs create mode 100644 crates/bittensor-integration/src/tests.rs create mode 100644 crates/bittensor-integration/src/validator_sync.rs create mode 100644 crates/bittensor-integration/src/weights.rs create mode 100644 crates/challenge-registry/Cargo.toml create mode 100644 crates/challenge-registry/src/discovery.rs create mode 100644 crates/challenge-registry/src/error.rs create mode 100644 crates/challenge-registry/src/health.rs create mode 100644 crates/challenge-registry/src/lib.rs create mode 100644 crates/challenge-registry/src/lifecycle.rs create mode 100644 crates/challenge-registry/src/migration.rs create mode 100644 crates/challenge-registry/src/registry.rs create mode 100644 crates/challenge-registry/src/state.rs create mode 100644 crates/challenge-registry/src/version.rs create mode 100644 crates/challenge-sdk-wasm/Cargo.toml create mode 100644 crates/challenge-sdk-wasm/src/alloc_impl.rs create mode 100644 crates/challenge-sdk-wasm/src/host_functions.rs create mode 100644 crates/challenge-sdk-wasm/src/lib.rs create mode 100644 crates/challenge-sdk-wasm/src/llm_types.rs create mode 100644 crates/challenge-sdk-wasm/src/types.rs create mode 100644 crates/challenge-sdk/Cargo.toml create mode 100644 crates/challenge-sdk/src/data.rs create mode 100644 crates/challenge-sdk/src/database.rs create mode 100644 crates/challenge-sdk/src/decentralized.rs create mode 100644 crates/challenge-sdk/src/error.rs create mode 100644 crates/challenge-sdk/src/lib.rs create mode 100644 crates/challenge-sdk/src/p2p_client.rs create mode 100644 crates/challenge-sdk/src/routes.rs create mode 100644 crates/challenge-sdk/src/server.rs create mode 100644 crates/challenge-sdk/src/submission_types.rs create mode 100644 crates/challenge-sdk/src/test_challenge.rs create mode 100644 crates/challenge-sdk/src/types.rs create mode 100644 crates/challenge-sdk/src/weight_types.rs create mode 100644 crates/challenge-sdk/src/weights.rs create mode 100644 crates/core/Cargo.toml create mode 100644 crates/core/src/challenge.rs create mode 100644 crates/core/src/checkpoint.rs create mode 100644 crates/core/src/constants.rs create mode 100644 crates/core/src/crypto.rs create mode 100644 crates/core/src/error.rs create mode 100644 crates/core/src/lib.rs create mode 100644 crates/core/src/message.rs create mode 100644 crates/core/src/restoration.rs create mode 100644 crates/core/src/schema_guard.rs create mode 100644 crates/core/src/state.rs create mode 100644 crates/core/src/state_versioning.rs create mode 100644 crates/core/src/types.rs create mode 100644 crates/distributed-storage/Cargo.toml create mode 100644 crates/distributed-storage/src/challenge_store.rs create mode 100644 crates/distributed-storage/src/dht.rs create mode 100644 crates/distributed-storage/src/error.rs create mode 100644 crates/distributed-storage/src/lib.rs create mode 100644 crates/distributed-storage/src/local.rs create mode 100644 crates/distributed-storage/src/query.rs create mode 100644 crates/distributed-storage/src/replication.rs create mode 100644 crates/distributed-storage/src/state_consensus.rs create mode 100644 crates/distributed-storage/src/store.rs create mode 100644 crates/distributed-storage/src/submission.rs create mode 100644 crates/distributed-storage/src/validated_storage.rs create mode 100644 crates/distributed-storage/src/weights.rs create mode 100644 crates/epoch/Cargo.toml create mode 100644 crates/epoch/src/aggregator.rs create mode 100644 crates/epoch/src/commit_reveal.rs create mode 100644 crates/epoch/src/lib.rs create mode 100644 crates/epoch/src/manager.rs create mode 100644 crates/epoch/src/mechanism_weights.rs create mode 100644 crates/p2p-consensus/Cargo.toml create mode 100644 crates/p2p-consensus/src/config.rs create mode 100644 crates/p2p-consensus/src/consensus.rs create mode 100644 crates/p2p-consensus/src/lib.rs create mode 100644 crates/p2p-consensus/src/messages.rs create mode 100644 crates/p2p-consensus/src/network.rs create mode 100644 crates/p2p-consensus/src/state.rs create mode 100644 crates/p2p-consensus/src/validator.rs create mode 100644 crates/rpc-server/Cargo.toml create mode 100644 crates/rpc-server/src/auth.rs create mode 100644 crates/rpc-server/src/handlers.rs create mode 100644 crates/rpc-server/src/health.rs create mode 100644 crates/rpc-server/src/jsonrpc.rs create mode 100644 crates/rpc-server/src/lib.rs create mode 100644 crates/rpc-server/src/server.rs create mode 100644 crates/rpc-server/src/types.rs create mode 100644 crates/storage/Cargo.toml create mode 100644 crates/storage/src/blockchain.rs create mode 100644 crates/storage/src/distributed.rs create mode 100644 crates/storage/src/dynamic.rs create mode 100644 crates/storage/src/lib.rs create mode 100644 crates/storage/src/metadata.rs create mode 100644 crates/storage/src/migration.rs create mode 100644 crates/storage/src/optimized.rs create mode 100644 crates/storage/src/types.rs create mode 100644 crates/subnet-manager/Cargo.toml create mode 100644 crates/subnet-manager/src/commands.rs create mode 100644 crates/subnet-manager/src/config.rs create mode 100644 crates/subnet-manager/src/health.rs create mode 100644 crates/subnet-manager/src/lib.rs create mode 100644 crates/subnet-manager/src/recovery.rs create mode 100644 crates/subnet-manager/src/snapshot.rs create mode 100644 crates/subnet-manager/src/update.rs create mode 100644 crates/wasm-runtime-interface/Cargo.toml create mode 100644 crates/wasm-runtime-interface/src/bridge.rs create mode 100644 crates/wasm-runtime-interface/src/consensus.rs create mode 100644 crates/wasm-runtime-interface/src/container.rs create mode 100644 crates/wasm-runtime-interface/src/data.rs create mode 100644 crates/wasm-runtime-interface/src/exec.rs create mode 100644 crates/wasm-runtime-interface/src/lib.rs create mode 100644 crates/wasm-runtime-interface/src/llm.rs create mode 100644 crates/wasm-runtime-interface/src/network.rs create mode 100644 crates/wasm-runtime-interface/src/runtime.rs create mode 100644 crates/wasm-runtime-interface/src/sandbox.rs create mode 100644 crates/wasm-runtime-interface/src/storage.rs create mode 100644 crates/wasm-runtime-interface/src/terminal.rs create mode 100644 crates/wasm-runtime-interface/src/time.rs create mode 100644 docker-compose.yml create mode 100644 docker/Dockerfile.challenge create mode 100644 docker/Dockerfile.validator create mode 100644 docker/docker-compose.yml create mode 100644 docker/entrypoint.sh create mode 100644 docs/challenge-integration.md create mode 100644 docs/challenges.md delete mode 100644 docs/miner/how-to-mine.md delete mode 100644 docs/miner/submission.md create mode 100644 docs/operations/validator.md create mode 100644 docs/security.md create mode 100644 docs/validator.md delete mode 100644 docs/validator/setup.md create mode 100644 docs/validator_wasm_audit.md create mode 100644 pr_diff_full.txt create mode 100644 rust-toolchain-nightly.toml create mode 100644 rust-toolchain.toml create mode 100755 scripts/build-wasm.sh create mode 100755 scripts/install-docker.sh create mode 100755 scripts/setup-hooks.sh create mode 100755 scripts/test-all.sh create mode 100755 scripts/test-comprehensive.sh create mode 100644 scripts/test-harness.sh create mode 100755 scripts/test-single-validator.sh create mode 100755 scripts/verify-nightly-config.sh create mode 100644 tests/Cargo.toml create mode 100644 tests/bittensor_tests.rs create mode 100644 tests/blockchain_state_tests.rs create mode 100644 tests/checkpoint_tests.rs create mode 100644 tests/e2e_tests.rs create mode 100644 tests/epoch_tests.rs create mode 100644 tests/error_cases.rs create mode 100644 tests/rpc_server_tests.rs create mode 100644 tests/storage_tests.rs create mode 100644 tests/sudo_action_tests.rs delete mode 100644 wasm/Cargo.toml delete mode 100644 wasm/src/agent_storage.rs delete mode 100644 wasm/src/ast_validation.rs delete mode 100644 wasm/src/dataset.rs delete mode 100644 wasm/src/lib.rs delete mode 100644 wasm/src/llm_review.rs delete mode 100644 wasm/src/routes.rs delete mode 100644 wasm/src/scoring.rs delete mode 100644 wasm/src/submission.rs delete mode 100644 wasm/src/tasks.rs delete mode 100644 wasm/src/timeout_handler.rs delete mode 100644 wasm/src/types.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..517524194 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,40 @@ +[build] +# Use all available CPU cores for compilation. +# Override with CARGO_BUILD_JOBS if needed. +# Omit explicit jobs to allow Cargo defaults. + +[env] +# Optional flags gated by environment variables (opt-in). +# PLATFORM_NIGHTLY_RUSTFLAGS: nightly-only parallel rustc flags (2023-11-09 blog). +# Set by rust-toolchain-nightly.toml or scripts; leave empty on stable. +# Example: PLATFORM_NIGHTLY_RUSTFLAGS="-Z threads=0" +# PLATFORM_FAST_LINKER_RUSTFLAGS: fast linker defaults for Linux (mold or lld). +# Example: PLATFORM_FAST_LINKER_RUSTFLAGS="-C link-arg=-fuse-ld=mold" +# Example: PLATFORM_FAST_LINKER_RUSTFLAGS="-C link-arg=-fuse-ld=lld" +# PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN: fast linker defaults for macOS (lld or zld). +# Example: PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="-C link-arg=-fuse-ld=lld" +# Example: PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="-C link-arg=-fuse-ld=zld" +# PLATFORM_LINKER_RUSTFLAGS: explicit fast linker flags (appended after defaults). +# Example: PLATFORM_LINKER_RUSTFLAGS="-C link-arg=-fuse-ld=mold" +# Example: PLATFORM_LINKER_RUSTFLAGS="-C link-arg=-fuse-ld=lld" +# PLATFORM_LINKER_RUSTFLAGS_DARWIN: explicit fast linker flags for macOS (appended after defaults). +# Example: PLATFORM_LINKER_RUSTFLAGS_DARWIN="-C link-arg=-fuse-ld=lld" +# Example: PLATFORM_LINKER_RUSTFLAGS_DARWIN="-C link-arg=-fuse-ld=zld" +# PLATFORM_DISABLE_NIGHTLY: set to 1 to disable nightly flags. +# PLATFORM_RUST_NIGHTLY: set to 1 to force nightly flags. +PLATFORM_DISABLE_NIGHTLY = { value = "${PLATFORM_DISABLE_NIGHTLY}", force = false } +PLATFORM_RUST_NIGHTLY = { value = "${PLATFORM_RUST_NIGHTLY}", force = false } +PLATFORM_NIGHTLY_RUSTFLAGS = { value = "${PLATFORM_NIGHTLY_RUSTFLAGS}", force = false } +# Fast-linker defaults are opt-in: set PLATFORM_FAST_LINKER_RUSTFLAGS +# or PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN to enable on a system +# with the desired linker installed. Set PLATFORM_DISABLE_FAST_LINKER=1 +# to ignore fast-linker settings (scripts clear the env values). +PLATFORM_DISABLE_FAST_LINKER = { value = "${PLATFORM_DISABLE_FAST_LINKER}", force = false } +PLATFORM_FAST_LINKER_RUSTFLAGS = { value = "${PLATFORM_FAST_LINKER_RUSTFLAGS}", force = false } +PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN = { value = "${PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN}", force = false } +PLATFORM_LINKER_RUSTFLAGS = { value = "${PLATFORM_LINKER_RUSTFLAGS}", force = false } +PLATFORM_LINKER_RUSTFLAGS_DARWIN = { value = "${PLATFORM_LINKER_RUSTFLAGS_DARWIN}", force = false } +RUSTFLAGS = { value = "${RUSTFLAGS} ${PLATFORM_NIGHTLY_RUSTFLAGS} ${PLATFORM_FAST_LINKER_RUSTFLAGS} ${PLATFORM_LINKER_RUSTFLAGS}", force = true } + +[target.'cfg(target_os = "macos")'.env] +RUSTFLAGS = { value = "${RUSTFLAGS} ${PLATFORM_NIGHTLY_RUSTFLAGS} ${PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN} ${PLATFORM_LINKER_RUSTFLAGS_DARWIN}", force = true } \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..bcee71e07 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,27 @@ +# Build artifacts +target/ +*.rlib +*.rmeta + +# Git +.git/ + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# OS +.DS_Store +Thumbs.db + +# Local config +.env +.env.local +*.log + +# Large data directories +data/ +*.db +*.sqlite diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..c791761eb --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# Platform Chain Validator Configuration +# Copy this file to .env and fill in your values + +# REQUIRED: Your validator secret key (hex encoded 32 bytes or BIP39 mnemonic) +VALIDATOR_SECRET_KEY=your_secret_key_here + +# Optional: Slack webhook for Watchtower notifications +# SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxx/xxx/xxx diff --git a/.githooks/install.sh b/.githooks/install.sh old mode 100755 new mode 100644 index d3cef508e..573298371 --- a/.githooks/install.sh +++ b/.githooks/install.sh @@ -1,23 +1,28 @@ #!/bin/bash -# Install git hooks for term-challenge +# Install git hooks for this repository +# Run this after cloning: ./githooks/install.sh + +set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -REPO_DIR="$(dirname "$SCRIPT_DIR")" +REPO_ROOT="$(dirname "$SCRIPT_DIR")" +HOOKS_DIR="$REPO_ROOT/.git/hooks" -echo "Installing git hooks for term-challenge..." +echo "Installing git hooks..." -# Configure git to use our hooks directory -git -C "$REPO_DIR" config core.hooksPath .githooks +# Copy hooks +cp "$SCRIPT_DIR/pre-commit" "$HOOKS_DIR/pre-commit" +cp "$SCRIPT_DIR/pre-push" "$HOOKS_DIR/pre-push" -# Make hooks executable -chmod +x "$SCRIPT_DIR/pre-push" +# Make executable +chmod +x "$HOOKS_DIR/pre-commit" +chmod +x "$HOOKS_DIR/pre-push" -echo "โœ… Git hooks installed!" +echo "Git hooks installed successfully!" echo "" -echo "The following checks will run before each push:" -echo " 1. cargo fmt --check" -echo " 2. cargo check" -echo " 3. cargo clippy" -echo " 4. cargo test" +echo "Hooks enabled:" +echo " - pre-commit: Auto-format code" +echo " - pre-push: Run all CI checks (format, clippy, tests)" echo "" -echo "To bypass hooks (not recommended): git push --no-verify" +echo "To skip hooks temporarily (not recommended):" +echo " git push --no-verify" diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 25eebf385..16799457f 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,24 +1,25 @@ #!/bin/bash -set -e - -# Skip hooks if SKIP_GIT_HOOKS=1 -if [ "${SKIP_GIT_HOOKS:-0}" = "1" ]; then - echo "Skipping pre-commit hooks (SKIP_GIT_HOOKS=1)" - exit 0 -fi +# Pre-commit hook: Format code and check for issues -# Source cargo environment -if [ -f "$HOME/.cargo/env" ]; then - source "$HOME/.cargo/env" -fi +set -e echo "Running pre-commit checks..." # Format code -echo "Formatting code..." -cargo fmt --all - -# Add formatted files back to staging -git add -u +echo "Checking formatting..." +if ! cargo fmt --all -- --check 2>/dev/null; then + echo "Formatting code..." + cargo fmt --all + + # Check if there were changes + if ! git diff --quiet; then + echo "" + echo "Code was auto-formatted. Please review and re-add the changes:" + git diff --stat + echo "" + echo "Run: git add -u && git commit" + exit 1 + fi +fi echo "Pre-commit checks passed!" diff --git a/.githooks/pre-push b/.githooks/pre-push index bbb0e09e7..f5c8a716f 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,73 +1,82 @@ #!/bin/bash -set -e - -# Skip hooks if SKIP_GIT_HOOKS=1 -if [ "${SKIP_GIT_HOOKS:-0}" = "1" ]; then - echo "Skipping pre-push hooks (SKIP_GIT_HOOKS=1)" - exit 0 -fi - -# Source cargo environment -[ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env" +# Pre-push hook: ALL CI checks MUST pass before pushing +# This ensures code quality and prevents broken builds -echo "๐Ÿ” Running pre-push checks..." -echo "" +set -e -# Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color -check_failed() { - echo -e "${RED}โŒ $1 FAILED${NC}" - echo "" - echo "Push aborted. Fix the issues and try again." - exit 1 -} - -check_passed() { - echo -e "${GREEN}โœ“ $1 passed${NC}" -} +echo "=============================================" +echo "Running pre-push CI checks (ALL MANDATORY)..." +echo "=============================================" # 1. Format check -echo "๐Ÿ“ Checking code formatting..." -if ! cargo fmt --check 2>/dev/null; then - echo -e "${YELLOW}โš ๏ธ Code not formatted. Running cargo fmt...${NC}" - cargo fmt - echo -e "${YELLOW}Code has been formatted. Please review and commit the changes.${NC}" - check_failed "Format" +echo -e "\n${YELLOW}[1/4] Checking formatting...${NC}" +if ! cargo fmt --all -- --check; then + echo -e "${RED}ERROR: Code is not formatted.${NC}" + echo "Run 'cargo fmt' before pushing." + exit 1 fi -check_passed "Format" +echo -e "${GREEN}โœ“ Formatting OK${NC}" -# 2. Build check -echo "" -echo "๐Ÿ”จ Checking compilation..." -if ! cargo check --all-targets 2>/dev/null; then - check_failed "Compilation" +# 2. Cargo check (compilation) +echo -e "\n${YELLOW}[2/4] Running cargo check...${NC}" +if ! cargo check --workspace; then + echo -e "${RED}ERROR: Compilation failed.${NC}" + echo "Fix compilation errors before pushing." + exit 1 fi -check_passed "Compilation" +echo -e "${GREEN}โœ“ Compilation OK${NC}" -# 3. Clippy -echo "" -echo "๐Ÿ“Ž Running clippy..." -if ! cargo clippy --all-targets --workspace -- -W clippy::all -D warnings \ +# 3. Clippy (linting) - MANDATORY +# Check main code (not tests) with strict warnings +echo -e "\n${YELLOW}[3/4] Running clippy...${NC}" +if ! cargo clippy --workspace -- \ + -D warnings \ -A clippy::too_many_arguments \ - -A clippy::type_complexity \ -A clippy::large_enum_variant \ - -A clippy::should_implement_trait 2>/dev/null; then - check_failed "Clippy" + -A clippy::type_complexity \ + -A clippy::await_holding_lock \ + -A clippy::collapsible_match \ + -A clippy::collapsible_if \ + -A clippy::needless_borrows_for_generic_args \ + -A clippy::to_string_in_format_args \ + -A clippy::manual_map \ + -A clippy::map_flatten \ + -A clippy::useless_format \ + -A clippy::redundant_closure \ + -A deprecated \ + -A dead_code \ + -A clippy::for_kv_map \ + -A clippy::to_string_trait_impl \ + -A clippy::if_same_then_else \ + -A unused_variables \ + -A unused_imports \ + -A clippy::useless_conversion \ + -A for_loops_over_fallibles \ + -A clippy::manual_filter_map \ + -A clippy::collapsible_str_replace \ + -A clippy::manual_is_multiple_of \ + -A clippy::map_entry \ + -A clippy::manual_flatten; then + echo -e "${RED}ERROR: Clippy found issues.${NC}" + echo "Fix clippy warnings before pushing." + exit 1 fi -check_passed "Clippy" +echo -e "${GREEN}โœ“ Clippy OK${NC}" -# 4. Tests -echo "" -echo "๐Ÿงช Running tests..." -if ! cargo test --workspace -- --skip live --skip integration 2>/dev/null; then - check_failed "Tests" +# 4. Tests - MANDATORY +echo -e "\n${YELLOW}[4/4] Running tests...${NC}" +if ! cargo test --workspace; then + echo -e "${RED}ERROR: Tests failed.${NC}" + echo "Fix failing tests before pushing." + exit 1 fi -check_passed "Tests" +echo -e "${GREEN}โœ“ Tests OK${NC}" -echo "" -echo -e "${GREEN}โœ… All pre-push checks passed!${NC}" -echo "" +echo -e "\n${GREEN}=============================================" +echo "All CI checks passed! Pushing..." +echo -e "=============================================${NC}" diff --git a/.github/ci-trigger b/.github/ci-trigger deleted file mode 100644 index 7a2827343..000000000 --- a/.github/ci-trigger +++ /dev/null @@ -1 +0,0 @@ -# CI Restart - Tue Jan 6 14:08:24 UTC 2026 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f819ef8e3..4359018aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,10 +2,16 @@ name: CI on: push: - branches: [main, master] - tags: ['v*'] + branches: [main, master, "worker/**"] + tags: ["v*"] pull_request: branches: [main, master] + workflow_dispatch: + inputs: + run_nightly: + description: "Run optional nightly build/tests" + type: boolean + default: false env: CARGO_TERM_COLOR: always @@ -13,45 +19,44 @@ env: RUST_BACKTRACE: short CARGO_NET_RETRY: 10 RUSTUP_MAX_RETRIES: 10 - -permissions: - contents: read + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: - # All Rust jobs run in parallel, sharing cache from previous runs build: name: Build runs-on: blacksmith-32vcpu-ubuntu-2404 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep + - name: Verify nightly config (stable dry-run) + run: scripts/verify-nightly-config.sh - uses: Swatinem/rust-cache@v2 with: - shared-key: "term-ci" + shared-key: "platform-ci" - run: cargo build --release clippy: name: Clippy - runs-on: blacksmith-32vcpu-ubuntu-2404 + runs-on: blacksmith-2vcpu-ubuntu-2404 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable with: components: clippy + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep - uses: Swatinem/rust-cache@v2 with: - shared-key: "term-ci" + shared-key: "platform-ci" save-if: false - - run: | - cargo clippy --all-targets --workspace -- -W clippy::all \ - -A clippy::too_many_arguments \ - -A clippy::type_complexity \ - -A clippy::large_enum_variant \ - -A clippy::should_implement_trait + - run: cargo clippy --all-targets --workspace -- -W clippy::all test: name: Test @@ -59,65 +64,239 @@ jobs: steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep + - uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest - uses: Swatinem/rust-cache@v2 with: - shared-key: "term-ci" + shared-key: "platform-ci" save-if: false - name: Run tests - run: cargo test --workspace + run: cargo nextest run --workspace -E 'not (test(/live/) | test(/integration/) | test(/e2e/))' - wasm: - name: WASM Build + integration: + name: Integration runs-on: blacksmith-32vcpu-ubuntu-2404 needs: [build, clippy, test] steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep + - uses: taiki-e/install-action@v2 with: - targets: wasm32-unknown-unknown + tool: cargo-nextest - uses: Swatinem/rust-cache@v2 with: - shared-key: "term-ci" + shared-key: "platform-ci" save-if: false + - name: Ensure Docker available for integration tests + run: scripts/install-docker.sh + - name: Run integration tests + run: cargo nextest run --workspace -E 'test(/integration/)' - - name: Build WASM module - run: | - cargo build --release --target wasm32-unknown-unknown \ - -p term-challenge-wasm --no-default-features + e2e: + name: E2E + runs-on: blacksmith-32vcpu-ubuntu-2404 + needs: [build, clippy, test] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep + - uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest + - uses: Swatinem/rust-cache@v2 + with: + shared-key: "platform-ci" + save-if: false + - name: Ensure Docker available for e2e tests + run: scripts/install-docker.sh + - name: Run e2e tests + run: cargo nextest run --workspace -E 'test(/e2e/)' - - name: Verify WASM artifact + coverage: + name: Coverage + runs-on: blacksmith-32vcpu-ubuntu-2404 + if: github.ref == 'refs/heads/main' + concurrency: + group: coverage-deploy + cancel-in-progress: false + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools-preview + - uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest,cargo-llvm-cov + - uses: Swatinem/rust-cache@v2 + with: + shared-key: "platform-ci" + save-if: false + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash curl jq + - name: Run tests with coverage run: | - WASM_PATH="target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm" - if [ ! -f "$WASM_PATH" ]; then - echo "ERROR: WASM artifact not found at $WASM_PATH" - exit 1 - fi - SIZE=$(du -h "$WASM_PATH" | cut -f1) - echo "WASM artifact: $WASM_PATH ($SIZE)" - - - name: Upload WASM artifact + cargo llvm-cov nextest --workspace --json --output-path coverage.json -E 'not (test(/live/) | test(/integration/) | test(/e2e/))' + cargo llvm-cov report --html --output-dir coverage-html + - name: Upload coverage HTML report uses: actions/upload-artifact@v4 with: - name: term-challenge-wasm - path: target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm + name: coverage-html + path: coverage-html/ + - name: Publish coverage HTML to GitHub Pages + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./coverage-html + destination_dir: coverage-html + keep_files: true + - name: Generate and deploy coverage badge + run: | + COVERAGE=$(jq '.data[0].totals.lines.percent // 0 | round' coverage.json) + echo "Coverage: $COVERAGE%" + mkdir -p badges + if (( COVERAGE >= 80 )); then COLOR="brightgreen" + elif (( COVERAGE >= 60 )); then COLOR="green" + elif (( COVERAGE >= 40 )); then COLOR="yellow" + else COLOR="red"; fi + curl -s "https://img.shields.io/badge/coverage-${COVERAGE}%25-${COLOR}" > badges/coverage.svg + - name: Deploy coverage badge + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./badges + destination_dir: badges + keep_files: true + + docker: + name: Docker + runs-on: blacksmith-32vcpu-ubuntu-2404 + needs: [build, clippy, test] + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + - name: Ensure Docker available + run: scripts/install-docker.sh + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep + - uses: useblacksmith/setup-docker-builder@v1 + - uses: docker/login-action@v3 + if: github.event_name != 'pull_request' + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=sha,prefix= + type=raw,value=latest,enable={{is_default_branch}} + - uses: useblacksmith/build-push-action@v2 + with: + context: . + build-args: | + RUSTUP_TOOLCHAIN=stable + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} - # Release only on tags, after all checks pass release: name: Release runs-on: blacksmith-32vcpu-ubuntu-2404 - needs: [build, clippy, test, wasm] + needs: [build, clippy, test, docker] if: startsWith(github.ref, 'refs/tags/v') permissions: contents: write steps: - uses: actions/checkout@v4 - - uses: actions/download-artifact@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 with: - name: term-challenge-wasm - path: release/ + shared-key: "platform-ci" + save-if: false + - run: cargo build --release - run: | - tar -czvf term-challenge-wasm-${{ github.ref_name }}.tar.gz -C release . + mkdir -p release + cp target/release/validator-node release/ + cp target/release/csudo release/ 2>/dev/null || true + tar -czvf platform-${{ github.ref_name }}-linux-x86_64.tar.gz -C release . - uses: softprops/action-gh-release@v2 with: - files: term-challenge-wasm-${{ github.ref_name }}.tar.gz + files: platform-${{ github.ref_name }}-linux-x86_64.tar.gz generate_release_notes: true + + nightly: + name: Nightly Build & Test + runs-on: blacksmith-32vcpu-ubuntu-2404 + if: github.event_name == 'workflow_dispatch' && github.event.inputs.run_nightly == 'true' + env: + RUSTUP_TOOLCHAIN: nightly + PLATFORM_RUST_NIGHTLY: "1" + PLATFORM_NIGHTLY_RUSTFLAGS: "-Z threads=0" + PLATFORM_FAST_LINKER_RUSTFLAGS: "-C link-arg=-fuse-ld=mold" + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly + - name: Install nightly dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep mold + - uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest + - name: Verify nightly config (nightly dry-run) + run: scripts/verify-nightly-config.sh + - uses: Swatinem/rust-cache@v2 + with: + shared-key: "platform-ci-nightly" + save-if: false + - name: Nightly build + run: cargo build --release + - name: Nightly tests + run: cargo nextest run --workspace -E 'not (test(/live/) | test(/integration/) | test(/e2e/))' + + docker-nightly: + name: Docker Nightly Build + runs-on: blacksmith-32vcpu-ubuntu-2404 + if: github.event_name == 'workflow_dispatch' && github.event.inputs.run_nightly == 'true' + needs: [nightly] + env: + RUSTUP_TOOLCHAIN: nightly + PLATFORM_RUST_NIGHTLY: "1" + PLATFORM_NIGHTLY_RUSTFLAGS: "-Z threads=0" + PLATFORM_FAST_LINKER_RUSTFLAGS: "-C link-arg=-fuse-ld=mold" + steps: + - uses: actions/checkout@v4 + - name: Ensure Docker available + run: scripts/install-docker.sh + - uses: useblacksmith/setup-docker-builder@v1 + - uses: useblacksmith/build-push-action@v2 + with: + context: . + push: false + build-args: | + RUSTUP_TOOLCHAIN=nightly + PLATFORM_NIGHTLY_RUSTFLAGS=-Z threads=0 + PLATFORM_FAST_LINKER_RUSTFLAGS=-C link-arg=-fuse-ld=mold + - uses: dtolnay/rust-toolchain@nightly + - name: Install nightly dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep mold + - uses: taiki-e/install-action@v2 + with: + tool: cargo-nextest + - name: Nightly build + run: cargo build --release + - name: Nightly tests + run: cargo nextest run --workspace -E 'not (test(/live/) | test(/integration/) | test(/e2e/))' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3facff1b8..0cbea2da3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,7 +2,7 @@ name: Release on: push: - branches: [main] + branches: [main, "worker/**"] permissions: contents: write @@ -11,7 +11,7 @@ permissions: jobs: release-please: name: Release Please - runs-on: blacksmith-32vcpu-ubuntu-2404 + runs-on: blacksmith-2vcpu-ubuntu-2404 outputs: release_created: ${{ steps.release.outputs.release_created }} tag_name: ${{ steps.release.outputs.tag_name }} @@ -33,55 +33,58 @@ jobs: - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@v2 with: - shared-key: "term-ci" - + shared-key: "platform-ci" + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y bash ripgrep - name: Build release binaries run: cargo build --release - name: Package binaries run: | mkdir -p release - cp target/release/term release/ 2>/dev/null || true - tar -czvf term-challenge-${{ needs.release-please.outputs.version }}-linux-x86_64.tar.gz -C release . + cp target/release/validator-node release/ + cp target/release/csudo release/ 2>/dev/null || true + tar -czvf platform-${{ needs.release-please.outputs.version }}-linux-x86_64.tar.gz -C release . - name: Upload release artifacts uses: softprops/action-gh-release@v2 with: tag_name: ${{ needs.release-please.outputs.tag_name }} files: | - term-challenge-${{ needs.release-please.outputs.version }}-linux-x86_64.tar.gz + platform-${{ needs.release-please.outputs.version }}-linux-x86_64.tar.gz - wasm-release: - name: WASM Release + docker-release: + name: Docker Release runs-on: blacksmith-32vcpu-ubuntu-2404 needs: release-please if: ${{ needs.release-please.outputs.release_created }} + permissions: + contents: read + packages: write steps: - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - name: Ensure Docker available + run: scripts/install-docker.sh + - uses: useblacksmith/setup-docker-builder@v1 + - uses: docker/login-action@v3 with: - targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2 - with: - shared-key: "term-ci" - - - name: Build WASM module - run: | - cargo build --release --target wasm32-unknown-unknown \ - -p term-challenge-wasm --no-default-features + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Prepare WASM artifact - run: | - WASM_PATH="target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm" - cp "$WASM_PATH" term_challenge_wasm.wasm - if command -v sha256sum &> /dev/null; then - sha256sum term_challenge_wasm.wasm > term_challenge_wasm.wasm.sha256 - fi + - id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}},value=${{ needs.release-please.outputs.version }} + type=semver,pattern={{major}}.{{minor}},value=${{ needs.release-please.outputs.version }} + type=semver,pattern={{major}},value=${{ needs.release-please.outputs.version }} + type=raw,value=latest - - name: Upload WASM to release - uses: softprops/action-gh-release@v2 + - uses: useblacksmith/build-push-action@v2 with: - tag_name: ${{ needs.release-please.outputs.tag_name }} - files: | - term_challenge_wasm.wasm - term_challenge_wasm.wasm.sha256 + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.gitignore b/.gitignore index 15e504f6a..f5292c097 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Build artifacts /target/ -**/target/ **/*.rs.bk # IDE @@ -14,31 +13,15 @@ .DS_Store Thumbs.db -# Environment -.env -.env.local -*.env - # Logs *.log -logs/ -# Test artifacts -*.profraw -coverage/ - -# Benchmark results -benchmark_results/ +# Environment +.env +.env.local -# Python -__pycache__/ -*.py[cod] -*.pyo -.pytest_cache/ -*.egg-info/ -dist/ -build/ +# Debug +*.pdb -# Node.js -node_modules/ -package-lock.json +# Data directories (runtime data) +/data/ diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 045149954..af55ef03a 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.2.3" + ".": "0.2.1" } diff --git a/AGENTS.md b/AGENTS.md index 6ad39e7dd..a48a5e74d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,177 +1,204 @@ -# AGENTS.md โ€” Term Challenge +# Agent Development Guide -## Project Purpose +This document explains how agents (miners) interact with the Platform network. -Term Challenge is a WASM evaluation module for AI agents on the Bittensor network via platform-v2. Miners submit Python agent packages (as zip files) that solve SWE-bench tasks. The WASM module runs inside platform-v2 validators to validate submissions, evaluate task results, and compute scores. A companion native CLI (`term-cli`) provides a TUI for monitoring leaderboards, evaluation progress, and network health. +--- -## Architecture Overview +## Important: Challenge-Specific Logic +**Platform is a fully decentralized P2P network for distributed evaluation.** It does not contain challenge-specific agent logic. + +Each challenge defines: +- Task definitions and evaluation criteria +- Submission formats and requirements +- Scoring algorithms + +Challenge crates are maintained in their own repositories and import `platform-challenge-sdk-wasm` as a git dependency. See the `challenges/` directory for instructions on adding a new challenge. + +--- + +## What is Platform? + +Platform is a **fully decentralized P2P infrastructure** that: + +1. **Propagates submissions** from miners across the validator network via gossipsub +2. **Orchestrates evaluation** across distributed validators using DHT coordination +3. **Aggregates scores** using stake-weighted consensus (P2P) +4. **Submits weights** to Bittensor at epoch boundaries + +```mermaid +flowchart LR + Miners[Miners] -->|Submissions| P2P[(libp2p Mesh)] + P2P --> Validators[Validator Nodes] + Validators --> Runtime[WASM Challenge Runtime] + Validators -->|Weights| Bittensor[Bittensor Chain] ``` -term-challenge/ -โ”œโ”€โ”€ Cargo.toml # workspace with members = ["wasm", "cli"] -โ”œโ”€โ”€ wasm/ -โ”‚ โ”œโ”€โ”€ Cargo.toml # cdylib, depends on platform-challenge-sdk-wasm -โ”‚ โ””โ”€โ”€ src/ -โ”‚ โ”œโ”€โ”€ lib.rs # Challenge impl + register_challenge! -โ”‚ โ”œโ”€โ”€ types.rs # Submission, TaskDefinition, AgentLogs, etc. -โ”‚ โ”œโ”€โ”€ scoring.rs # Aggregate scoring, decay, weight calculation -โ”‚ โ”œโ”€โ”€ tasks.rs # Active dataset storage (SWE-bench tasks) -โ”‚ โ”œโ”€โ”€ dataset.rs # Dataset selection, consensus, and random index generation -โ”‚ โ”œโ”€โ”€ routes.rs # Challenge route definitions and handlers for RPC -โ”‚ โ”œโ”€โ”€ agent_storage.rs # Agent code, log, and evaluation status storage -โ”‚ โ”œโ”€โ”€ ast_validation.rs # Python AST whitelist validation (imports, builtins, patterns) -โ”‚ โ”œโ”€โ”€ llm_review.rs # LLM-based code review, reviewer selection, aggregation -โ”‚ โ”œโ”€โ”€ submission.rs # Named submission registry and version tracking -โ”‚ โ””โ”€โ”€ timeout_handler.rs # Review assignment timeout tracking and replacement -โ”œโ”€โ”€ cli/ -โ”‚ โ”œโ”€โ”€ Cargo.toml # native binary, ratatui TUI -โ”‚ โ””โ”€โ”€ src/ -โ”‚ โ”œโ”€โ”€ main.rs # Entry point, event loop -โ”‚ โ”œโ”€โ”€ app.rs # Application state -โ”‚ โ”œโ”€โ”€ ui.rs # Ratatui UI rendering -โ”‚ โ””โ”€โ”€ rpc.rs # JSON-RPC 2.0 client -โ”œโ”€โ”€ docs/ -โ”‚ โ”œโ”€โ”€ architecture.md -โ”‚ โ”œโ”€โ”€ miner/ -โ”‚ โ”‚ โ”œโ”€โ”€ how-to-mine.md -โ”‚ โ”‚ โ””โ”€โ”€ submission.md -โ”‚ โ””โ”€โ”€ validator/ -โ”‚ โ””โ”€โ”€ setup.md -โ”œโ”€โ”€ .github/ -โ”‚ โ””โ”€โ”€ workflows/ -โ”‚ โ”œโ”€โ”€ ci.yml # Build, clippy, test, WASM build, release on tags -โ”‚ โ””โ”€โ”€ release.yml # release-please + artifact publishing -โ”œโ”€โ”€ AGENTS.md -โ”œโ”€โ”€ README.md -โ”œโ”€โ”€ LICENSE -โ”œโ”€โ”€ CHANGELOG.md -โ””โ”€โ”€ .githooks/ + +--- + +## Agent Lifecycle + +### 1. Development + +Develop your agent following the challenge-specific requirements. Challenge crates implement the `Challenge` trait from `platform-challenge-sdk-wasm`: + +```rust +// Example: my-challenge/src/lib.rs +use platform_challenge_sdk_wasm::{Challenge, EvaluationInput, EvaluationOutput}; + +pub struct MyChallenge; + +impl Challenge for MyChallenge { + fn name(&self) -> &'static str { "my-challenge" } + fn version(&self) -> &'static str { "0.1.0" } + fn evaluate(&self, input: EvaluationInput) -> EvaluationOutput { /* ... */ } + fn validate(&self, input: EvaluationInput) -> bool { /* ... */ } +} + +platform_challenge_sdk_wasm::register_challenge!(MyChallenge, MyChallenge::new()); ``` -### Data Flow +**Check the challenge documentation** for the correct submission format and evaluation criteria. -1. **Miner** submits a zip package with agent code and task results -2. **RPC** receives submission, verifies signature, relays to validators -3. **Validators** run WASM `validate()` โ€” checks signature, epoch rate limit, Basilica metadata, package size -4. **50% validator approval** โ†’ submission stored in blockchain -5. **Validators** run WASM `evaluate()`: - a. **AST validation** โ€” checks Python code against import whitelist, forbidden builtins, and dangerous patterns - b. **LLM review** โ€” optional LLM-based security review via `host_http_post()` (if enabled) - c. **Task scoring** โ€” scores task results, optionally applies LLM judge per task - d. **Aggregate & decay** โ€” computes pass rate, applies epoch-based decay -6. **Agent code & logs** stored on-chain for auditability (code โ‰ค 1MB, logs โ‰ค 256KB) -7. **Log consensus** โ€” validators propose logs, >50% hash agreement required -8. **Consensus** aggregates scores, applies decay, submits weights to Bittensor +### 2. Submission -### Key Concepts +Submit your agent's output to the P2P network. The submission is: +- Broadcast to validators via libp2p gossipsub +- Validated by the challenge WASM module +- Distributed across the validator network for evaluation -- **WASM-only**: All challenge logic runs as a `wasm32-unknown-unknown` module loaded by platform-v2 -- **Host functions**: WASM interacts with the outside world via `host_http_post()`, `host_storage_get()`, `host_storage_set()`, `host_consensus_get_epoch()`, `host_consensus_get_submission_count()`, `host_random_seed()`, `host_get_timestamp()` -- **SWE-bench datasets**: Tasks are selected from HuggingFace CortexLM/swe-bench via P2P consensus -- **Epoch rate limiting**: 1 submission per 3 epochs per miner -- **Top agent decay**: 60-epoch grace period, then exponential decay with 20-epoch half-life +### 3. Evaluation -## Agent Code Storage +Validators independently evaluate your submission: +- Each validator runs the challenge-specific WASM module in a sandboxed runtime +- Your submission executes deterministically +- Scores are computed based on challenge criteria -Agent submissions are stored on-chain for auditability and retrieval. The `agent_storage` module manages three storage categories: +### 4. Scoring -| Storage Key Format | Content | Max Size | -|---|---|---| -| `agent_code::` | Raw zip package bytes | 1 MB (1,048,576 bytes) | -| `agent_hash::` | Hash of the agent package | โ€” | -| `agent_logs::` | Serialized `AgentLogs` struct | 256 KB (262,144 bytes) | +Validators aggregate scores across the P2P network: +- Stake-weighted averaging via DHT coordination +- Outlier detection (removes anomalous validators) +- Consensus achieved through gossipsub protocol -- **Package size limit**: Submissions with `package_zip` exceeding 1 MB are rejected at the storage layer. -- **Log size limit**: Serialized logs exceeding 256 KB are rejected. Individual task output previews are truncated to 4 KB (4,096 bytes) before storage. -- **Key format**: Keys are constructed as `:` using little-endian encoding for the epoch. +### 5. Rewards -## CLI +At each epoch boundary (tempo synced from Bittensor), weights are submitted to the chain: +- Higher scores = higher weights = more TAO rewards +- Weights are normalized by sum (each weight divided by total) -The `term-cli` crate is a **native binary** (NOT `no_std`) that provides a terminal user interface for monitoring the term-challenge network. +--- -### Design +## P2P Network -- **Framework**: Built with [ratatui](https://ratatui.rs/) for TUI rendering -- **Transport**: Connects to validators via JSON-RPC 2.0 over HTTP -- **Target**: Standard `x86_64` / `aarch64` native targets (not WASM) +### How It Works -### Available Tabs +Platform uses libp2p for fully decentralized communication: -| Tab | Description | -|---|---| -| Leaderboard | Current scores, ranks, and miner hotkeys | -| Evaluation | Live evaluation progress for pending submissions | -| Submission | Recent submission history and status | -| Network | Validator count, epoch info, system health | +- **Gossipsub**: Submissions and scores are broadcast across the validator network +- **DHT (Kademlia)**: Peer discovery and coordination without central servers +- **Direct Connections**: Validators communicate directly with each other -### Keyboard Shortcuts +### Authentication -| Key | Action | -|---|---| -| `Tab` / `Shift+Tab` | Switch between tabs | -| `โ†‘` / `โ†“` | Navigate rows | -| `r` | Refresh data | -| `q` | Quit | +All P2P messages are signed with the validator's Bittensor hotkey: +- Uses `sr25519` signature scheme (Substrate/Bittensor compatible, via `sp_core`) +- Includes timestamp to prevent replay attacks +- Validators verify signatures before processing -### RPC Methods Used +### Submitting via P2P -- `epoch_current` โ€” Current epoch number, phase, and block height -- `system_health` โ€” Node health status -- `validator_count` โ€” Number of active validators -- `challenge_list` โ€” Auto-detect challenge ID when only one exists -- `challenge_call` with paths: - - `/leaderboard` โ€” Leaderboard data - - `/stats` โ€” Total submissions and active miners - - `/decay` โ€” Top agent decay status - - `/agent/:hotkey/journey` โ€” Evaluation status journey - - `/agent/:hotkey/logs` โ€” Evaluation logs for a miner -- `evaluation_getProgress` โ€” Evaluation progress for a submission +Miners connect to the validator mesh to submit agent outputs. Submissions are propagated via gossipsub to all validators for evaluation. -## Build Commands +--- -```bash -# Build CLI (native) -cargo build --release -p term-cli +## Common Questions -# Build WASM module -cargo build --release --target wasm32-unknown-unknown -p term-challenge-wasm +### Where do I find the challenge SDK? -# Check (no target needed for workspace check) -cargo check -p term-challenge-wasm -``` +The WASM challenge SDK is at `crates/challenge-sdk-wasm/`. The server-side SDK is at `crates/challenge-sdk/`. Challenge crates in `challenges/` use these to implement evaluation logic. + +### Why did my submission fail? + +Check the challenge module for: +- Required submission format and fields +- Resource limits (memory, CPU, time) +- Validation rules in the `validate()` method -## Git Hooks +### How are scores calculated? -Git hooks live in `.githooks/` and are activated with `git config core.hooksPath .githooks`. +Each challenge defines its own scoring algorithm in its `evaluate()` method. Validators coordinate score aggregation via P2P consensus. -| Hook | What it does | -|------|-------------| -| `pre-commit` | Runs `cargo fmt --all`, stages formatted files. Skippable with `SKIP_GIT_HOOKS=1`. | -| `pre-push` | Full quality gate: format check โ†’ `cargo check` โ†’ `cargo clippy` โ†’ `cargo test`. Skippable with `SKIP_GIT_HOOKS=1` or `git push --no-verify`. | +### Can I test locally? -## CRITICAL RULES +Build and test challenge WASM modules locally: -1. **No `std` in WASM code.** The module compiles with `#![no_std]`. Use `alloc::` equivalents. -2. **Cryptographic signatures use sr25519.** SS58 prefix 42. Do NOT switch schemes. -3. **Conventional commits required.** The project uses `release-please`. -4. **No `.unwrap()` or `.expect()` in library paths.** Use pattern matching or `unwrap_or_default()`. -5. **Host functions are the ONLY external interface.** No direct HTTP, no filesystem, no std::net. -6. **Do NOT add `#[allow(dead_code)]` broadly.** Fix unused code or remove it. +```bash +# Build a challenge WASM artifact (example) +cargo build --release --target wasm32-unknown-unknown -p my-challenge + +# Run workspace tests +cargo test +``` -> **Note:** The `cli/` crate is exempt from the `no_std` rule (rule 1) and the host-functions-only rule (rule 5) since it is a native binary that runs outside the WASM sandbox. Rules 2, 3, 4, and 6 still apply to CLI code. +### What's the evaluation timeout? -## DO / DO NOT +Defined by each challenge and the WASM runtime policy. Check the challenge and runtime configuration for specific limits. -### DO -- Use `alloc::string::String`, `alloc::vec::Vec`, `alloc::collections::BTreeMap` (WASM code) -- Use `serde` with `default-features = false, features = ["derive", "alloc"]` (WASM code) -- Use `bincode` with `default-features = false` for serialization (WASM code) -- Use host functions for all I/O: `host_storage_get/set`, `host_http_post`, `host_consensus_get_epoch`, `host_consensus_get_submission_count`, `host_random_seed`, `host_get_timestamp` (WASM code) -- Keep the `register_challenge!` macro ABI contract intact -- Use standard `std` library features in the `cli/` crate (it is a native binary) +--- + +## Architecture Summary + +```mermaid +flowchart TB + Platform[Platform Repository] --> SDK[challenge-sdk] + Platform --> SDKW[challenge-sdk-wasm] + Platform --> Validator[validator-node] + Platform --> Runtime[wasm-runtime-interface] + Platform --> P2P[p2p-consensus] +``` -### DO NOT -- Do NOT use `std::`, `println!`, `std::collections::HashMap` in WASM code -- Do NOT add heavy dependencies โ€” the WASM module must stay minimal -- Do NOT break the WASM ABI (evaluate, validate, get_name, get_version, get_tasks, configure, alloc, get_routes, handle_route) -- Do NOT store sensitive data in plain text in blockchain storage +**Workspace crates** (from `Cargo.toml`): +- `crates/core` โ€” shared types, crypto (`sr25519`), constants +- `crates/storage` โ€” local storage layer +- `crates/distributed-storage` โ€” DHT-backed distributed storage +- `crates/challenge-sdk` โ€” server-side challenge trait +- `crates/challenge-sdk-wasm` โ€” WASM challenge trait (`no_std`) +- `crates/challenge-registry` โ€” challenge metadata registry +- `crates/epoch` โ€” epoch management synced with Bittensor tempo +- `crates/bittensor-integration` โ€” Bittensor chain interaction +- `crates/subnet-manager` โ€” subnet management +- `crates/rpc-server` โ€” RPC server for validator API +- `crates/p2p-consensus` โ€” libp2p gossipsub + DHT consensus +- `crates/wasm-runtime-interface` โ€” WASM runtime host interface +- `bins/validator-node` โ€” main validator binary +- `bins/platform-cli` โ€” CLI for downloading and managing challenge CLIs +- `bins/utils` โ€” CLI utilities +- `bins/mock-subtensor` โ€” mock Bittensor node for testing +- `tests` โ€” integration tests + +**Note:** Platform is fully decentralizedโ€”there is no central server. All validators communicate directly via libp2p (gossipsub + DHT). + +--- + +## Getting Started + +1. **Choose a challenge** you want to participate in +2. **Read the challenge documentation** for your chosen challenge +3. **Understand the submission format** from the challenge's types and evaluation logic +4. **Submit** through the P2P network +5. **Monitor** your submission status and scores + +--- + +## Links + +- [Bittensor Docs](https://docs.bittensor.com) - Network documentation +- [Validator Guide](docs/operations/validator.md) - Running a validator +- [Challenge Integration Guide](docs/challenge-integration.md) - Adding new challenges +- [Architecture](docs/architecture.md) - System architecture + +Platform is fully decentralizedโ€”validators communicate directly via P2P without any central server. +See the main README for deployment instructions. + +For challenge-specific questions, refer to the appropriate challenge crate or repository. diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fc6d8bff..8bc575edf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,431 +1,301 @@ # Changelog -## [0.2.3](https://github.com/PlatformNetwork/term-challenge/compare/v0.2.2...v0.2.3) (2026-01-18) +## [0.2.1](https://github.com/PlatformNetwork/platform/compare/v0.2.0...v0.2.1) (2026-01-09) ### Features -* add API module structure ([f767bf6](https://github.com/PlatformNetwork/term-challenge/commit/f767bf6f6240c67d70a0af12a56d39f01d0661d2)) -* add cache, chain, validation, container, agent, and evaluation modules ([ffa9e5f](https://github.com/PlatformNetwork/term-challenge/commit/ffa9e5f02040783b40c4bdc81090a22e81f58017)) -* add client and weights modules ([8f044de](https://github.com/PlatformNetwork/term-challenge/commit/8f044de96f379aaaef5d1a1d1f92a9d576d82d73)) -* add core types and crypto modules ([25db2c4](https://github.com/PlatformNetwork/term-challenge/commit/25db2c4bd18ab92ded297a8320933ad30b414cc5)) -* add lib_new.rs and STRUCTURE.md documentation ([7deb466](https://github.com/PlatformNetwork/term-challenge/commit/7deb466490401d9107dc0d622630d3f077bbd24b)) -* Add OpenAI Responses API support (GPT-4.1+/GPT-5.x) and use real provider costs ([2738dd4](https://github.com/PlatformNetwork/term-challenge/commit/2738dd460a499fe88d85b48604b2ec4b720dc73d)) -* Add OpenRouter prompt caching support with usage tracking ([f8924d2](https://github.com/PlatformNetwork/term-challenge/commit/f8924d2f7c811227ee81afb1be721d7c353db19b)) -* add storage module structure ([08097ac](https://github.com/PlatformNetwork/term-challenge/commit/08097ac0c0a0aed749aed5d511310f62b50bb99a)) -* add tool_calls/function calling support through platform bridge ([0133db9](https://github.com/PlatformNetwork/term-challenge/commit/0133db9566cf1e6c5cb16e300da0557fb35a5acf)) -* add worker, task, admin, and server modules ([98779c2](https://github.com/PlatformNetwork/term-challenge/commit/98779c2d56efc51bb2958d87c62f12868a7adbc5)) -* Add ZIP package support to submit wizard ([52e6e14](https://github.com/PlatformNetwork/term-challenge/commit/52e6e14aa8d301d3c551247a7da9008e8fc28222)) -* Add ZIP package support to submit wizard for Bridge API ([493c40a](https://github.com/PlatformNetwork/term-challenge/commit/493c40a6e6ea65a420d143e6ad270f6d561cbd2b)) -* create directory structure and util module ([ec597d9](https://github.com/PlatformNetwork/term-challenge/commit/ec597d93f9af18f4e327f716002ceb6e19314b5a)) -* enforce minimum 10000 TAO stake for validator assignment ([320585d](https://github.com/PlatformNetwork/term-challenge/commit/320585d2ce47c6ecd6d75558003dd305d6997a9f)) -* extract pg_storage.rs and api.rs into submodules ([66e6724](https://github.com/PlatformNetwork/term-challenge/commit/66e67247324268393c01e9bca87abd22b784f578)) -* Make temperature parameter optional ([70513ba](https://github.com/PlatformNetwork/term-challenge/commit/70513baeccd5d95f24a36b9c06b322cb154320d7)) -* **sdk:** add raw_chat() method for full control over LLM request body ([ea96ff6](https://github.com/PlatformNetwork/term-challenge/commit/ea96ff6f229c95262ac2d8061a33704a42b134e1)) -* **sdk:** preserve raw_arguments on JSON parse failure ([8e7fe10](https://github.com/PlatformNetwork/term-challenge/commit/8e7fe103a1ab36428011d465122388df6a086030)) -* Support max_completion_tokens parameter for o-series models ([e51b6e0](https://github.com/PlatformNetwork/term-challenge/commit/e51b6e065959edae29eed0d96375bd941104ec42)) -* **validator:** add timeout retry with local and server-side reassignment ([375575b](https://github.com/PlatformNetwork/term-challenge/commit/375575bb4e1188ec98256d0dd527e77a166b77d9)) +* add container broker to platform-server ([4772327](https://github.com/PlatformNetwork/platform/commit/47723278a21f2a4b5dfbceb56d1966d67b142c70)) +* add tempo fetching from Bittensor chain ([e455e29](https://github.com/PlatformNetwork/platform/commit/e455e29a6266a2a8adf626c086b41c96863a1605)) +* add WsContainerClient for WebSocket broker access ([58c8618](https://github.com/PlatformNetwork/platform/commit/58c861855ed2c8114c9e69e06097aba8f7dae0ff)) +* auto-generate random BROKER_JWT_SECRET if not provided ([d7be22b](https://github.com/PlatformNetwork/platform/commit/d7be22bfe9d5cf19aaffef496fe5fc3e424fd9dc)) +* auto-pull images in broker when creating containers ([f0ef63d](https://github.com/PlatformNetwork/platform/commit/f0ef63df4ac3c0815da932d1002ed8e1501c86f0)) +* **broker:** add image build support via Build command ([6da49e2](https://github.com/PlatformNetwork/platform/commit/6da49e2550c12565b2fe347252dcd240f0043145)) +* CI publish HTML test coverage report to GitHub Pages ([299d25a](https://github.com/PlatformNetwork/platform/commit/299d25ab7851831e3e33fa5eb0bdb17a8d6a905e)) +* CI publish HTML test coverage report to GitHub Pages ([1155c72](https://github.com/PlatformNetwork/platform/commit/1155c721eba7a8820315ae6afad666eb89046558)) +* Docker-in-Docker support with named volumes and CopyFrom/CopyTo protocol ([e56a639](https://github.com/PlatformNetwork/platform/commit/e56a639a181ee24f56f6ab4fdfece577593e935c)) +* enable debug logging for challenges and broker ([1db95e6](https://github.com/PlatformNetwork/platform/commit/1db95e6c7aeccfa3e21100fdd7011f6fb083cf1d)) +* Improve auto-updater coverage and fix CI coverage command ([d9183f2](https://github.com/PlatformNetwork/platform/commit/d9183f2bff80e627f5199dce076d4fd1a1bde8ef)) +* Improve auto-updater coverage and fix CI coverage command ([19a6e0d](https://github.com/PlatformNetwork/platform/commit/19a6e0d00aaaf9234066e16f70b6db2f9fbe1ace)) +* remove image whitelist by default, allow all public images ([0d30bfa](https://github.com/PlatformNetwork/platform/commit/0d30bfada92015a5e8f2ac75863a2d092fd5c7d3)) +* validator metrics reporting (CPU/RAM) with 5s interval and in-memory cache ([f06e67d](https://github.com/PlatformNetwork/platform/commit/f06e67da58de13887591c1c4223ec9a7f71ba5b6)) ### Bug Fixes -* add 15 min timeout to LLM HTTP clients and handle empty responses ([7b3a11f](https://github.com/PlatformNetwork/term-challenge/commit/7b3a11f894d07bbf6501c13ccac6e0775d6f0b51)) -* always run tests even if agent times out ([11ab582](https://github.com/PlatformNetwork/term-challenge/commit/11ab582f13087347a2340be0d80ad617dda079e1)) -* clippy warnings ([ef98763](https://github.com/PlatformNetwork/term-challenge/commit/ef98763f3c71798f116b7e0bb6e9166e6d022c38)) -* detect active validators by started_at, not just completed_at ([f48a153](https://github.com/PlatformNetwork/term-challenge/commit/f48a153fe9d7204ea462fb63cafc176ee2699d71)) -* **expire:** calculate consensus with 2+ validators when window expires ([b147962](https://github.com/PlatformNetwork/term-challenge/commit/b1479625098534b5813f3e531d3f35f535fb4809)) -* implement missing FakeStorage trait methods for tests ([8385f10](https://github.com/PlatformNetwork/term-challenge/commit/8385f100ff125ffd72086364e2865d46d9487d06)) -* Remove agent wrapper to preserve 'from __future__' imports ([d088b44](https://github.com/PlatformNetwork/term-challenge/commit/d088b44f9cf49412d4ffef2df3fd8a7eeb671762)) -* Restore full Cargo.toml with all dependencies ([6133234](https://github.com/PlatformNetwork/term-challenge/commit/6133234389b2570acdd9e4bdf5237c2505034144)) -* **retry:** detect test execution failures and resource errors ([075b90a](https://github.com/PlatformNetwork/term-challenge/commit/075b90a29bd1677bdf5c45269248262bc220c4e2)) -* **stale:** only detect stale assignments for pending agents ([eb91952](https://github.com/PlatformNetwork/term-challenge/commit/eb919520cad11a45368159d2eebfe1fd912c6ae0)) -* **timeout:** apply 1.3x multiplier to agent timeout and fix retry detection ([5db6be0](https://github.com/PlatformNetwork/term-challenge/commit/5db6be06bb108f1c164305a953b26dd566f934c8)) -* **timeout:** websocket timeout 300s, case-insensitive retry detection, detailed messages ([1b33dc6](https://github.com/PlatformNetwork/term-challenge/commit/1b33dc6ad2691c7e84fc1fb6c0c6fea5fa202106)) -* Transform system messages for OpenRouter+Claude requests ([6ff4b4f](https://github.com/PlatformNetwork/term-challenge/commit/6ff4b4f5dc47e56979c26965995737b8a10e2803)) -* **validator:** add global timeout to force-kill hung tasks ([738214b](https://github.com/PlatformNetwork/term-challenge/commit/738214b907121fa7edc9c1b85f4fe994c61f578e)) -* **validator:** detect stuck validators and improve reassignment logic ([06622f5](https://github.com/PlatformNetwork/term-challenge/commit/06622f5434ce67b6c9089ba3a599431d5d482f8d)) -* **validator:** kill agent process before running tests on timeout/incomplete ([4322340](https://github.com/PlatformNetwork/term-challenge/commit/43223403a615d3b4132254a49ab31489994ec9ad)) -* **weights:** only allow completed agents to receive emissions ([8fa4b22](https://github.com/PlatformNetwork/term-challenge/commit/8fa4b22f8d69ebba8e6e3187a820d199e0bfc729)) +* add PLATFORM_PUBLIC_URL to validator docker-compose ([c70f755](https://github.com/PlatformNetwork/platform/commit/c70f755408005b00446dfeb934430bf02b51ca09)) +* **consensus:** improve test accuracy for zero-stake scenarios ([de9d492](https://github.com/PlatformNetwork/platform/commit/de9d4921e27025f4d20cb483bc0761dd6175eb5b)) +* default PLATFORM_PUBLIC_URL to chain.platform.network ([a980e47](https://github.com/PlatformNetwork/platform/commit/a980e47ec2ca46dd0296abb6f7ad4ca8c240e119)) +* export VALIDATOR_HOTKEY and VALIDATOR_SECRET_KEY as env vars ([eb3065c](https://github.com/PlatformNetwork/platform/commit/eb3065c73819384d7777a278c3ca240e20eeefb6)) +* handle auth message even when JWT validation is disabled ([19e9d1f](https://github.com/PlatformNetwork/platform/commit/19e9d1f6c8e0d202db8e7b4b3c58698981b90abd)) +* increase broker JWT TTL to 10 years to prevent ExpiredSignature errors ([94682da](https://github.com/PlatformNetwork/platform/commit/94682daed18fb6c0264edddb6f6caceeb47e54c0)) +* increase container limits and add startup cleanup ([9387864](https://github.com/PlatformNetwork/platform/commit/93878641b9dc6031acace439847d85defd95d793)) +* integer overflow in calculation ([138fbf9](https://github.com/PlatformNetwork/platform/commit/138fbf908d8ec619406c9f72b409ebd239154ed9)) +* integer overflow in calculation ([534c7f9](https://github.com/PlatformNetwork/platform/commit/534c7f995d6ce475fbbb766dc2232b947f77411b)) +* pass CHALLENGE_UUID env var for broker authentication ([d88bfe5](https://github.com/PlatformNetwork/platform/commit/d88bfe5e15ccd18f75dd81ed5ee9eb0a79e6b0de)) +* remove restrictive cap_drop from container broker, add user field ([372641c](https://github.com/PlatformNetwork/platform/commit/372641ca437daa1ee6b2d32b8b1b86cf3ae6a711)) +* reorder validator routes - static paths before :hotkey parameter ([6ffb7ca](https://github.com/PlatformNetwork/platform/commit/6ffb7cadc65f90411b09c9d2e44309f2dc666535)) +* update tests to use strict() policy for image whitelist tests ([fb63e63](https://github.com/PlatformNetwork/platform/commit/fb63e63871da1830229f8ade4dfc22ef5459acac)) +* use actual hostname as fallback for broker URL ([351b657](https://github.com/PlatformNetwork/platform/commit/351b6571036f0853ae40d522f07079ced43e0c79)) +* use challenge name (not UUID) for JWT token generation ([8ecab04](https://github.com/PlatformNetwork/platform/commit/8ecab04badd85532cebd9ef380b90323d97ddfdc)) ### Code Refactoring -* integrate new module structure into lib.rs and fix compilation ([59ac5d2](https://github.com/PlatformNetwork/term-challenge/commit/59ac5d21c0babeda4117213da335ee90bcb8f0fc)) -* remove automatic prompt caching from SDK, let users implement manually ([2b469ee](https://github.com/PlatformNetwork/term-challenge/commit/2b469eea7347eaa8d5dac43a0401abbe5ddca216)) +* **consensus:** address code review feedback ([10a5394](https://github.com/PlatformNetwork/platform/commit/10a53945b5cdf73dd27760ecce0be5fa6aeacaec)) -### Miscellaneous - -* addressed code review suggestions ([9fdbd2e](https://github.com/PlatformNetwork/term-challenge/commit/9fdbd2e127a344a5c12798c95d160580c5931a6a)) - - -### Tests - -* Update compiler tests for no-wrapper behavior ([2c8a87a](https://github.com/PlatformNetwork/term-challenge/commit/2c8a87ab244fcd9b9b8f3c87cb90ccc28455454d)) - -## [0.2.2](https://github.com/PlatformNetwork/term-challenge/compare/v0.2.1...v0.2.2) (2026-01-12) - - -### Features - -* add folder upload support to term wizard ([6e2ae37](https://github.com/PlatformNetwork/term-challenge/commit/6e2ae375cfe3a9b0ac578646950bd61c0cc5b7c2)) -* add forced_weights for manual weight overrides + sort leaderboard by success_rate ([5ecfe21](https://github.com/PlatformNetwork/term-challenge/commit/5ecfe21b29132f849701456bcc978cdeb4196c00)) -* add requirements.txt support for package compilation ([a1e655b](https://github.com/PlatformNetwork/term-challenge/commit/a1e655b1c492387704f5777d430b4824fd59fc2c)) - - -### Bug Fixes +### Documentation -* change eligibility from 8 tasks/validator to 8 tasks total ([1eb9812](https://github.com/PlatformNetwork/term-challenge/commit/1eb9812a3ea0a57d7a0912bba4c00769af4e7a09)) -* create pending_evaluations after compilation + exclude __evaluation_failure__ from task counts ([a8646c3](https://github.com/PlatformNetwork/term-challenge/commit/a8646c3edbcf23693b335323710782688dc97e56)) -* filter evaluation progress by validator_hotkey ([2b44209](https://github.com/PlatformNetwork/term-challenge/commit/2b44209bcaa7d489c016e740b742d1e94a08702a)) -* log task results immediately after each task completes ([5823384](https://github.com/PlatformNetwork/term-challenge/commit/58233844241a14c93184f24a17491a834e3f1ad0)) -* remove fallback mode - skip evaluation if no assigned tasks ([f8f7a86](https://github.com/PlatformNetwork/term-challenge/commit/f8f7a861f94b4c360c5567f4a5e6d4a72bc60f72)) +* update README to reflect centralized architecture ([4bc8313](https://github.com/PlatformNetwork/platform/commit/4bc83130134dd5162976cc61e00cb770c2d3b37e)) -### Performance Improvements +### Miscellaneous -* run tasks concurrently (2 per agent, 8 max global) ([86f7efc](https://github.com/PlatformNetwork/term-challenge/commit/86f7efccb7110614dc08889db66655db8a8c60af)) +* remove unused import ([13b55b1](https://github.com/PlatformNetwork/platform/commit/13b55b1c0ee8ed7d6d841dab181c722b7430a25b)) +* update doc string ([8245dab](https://github.com/PlatformNetwork/platform/commit/8245dab16cc5ad8c6f610f435036946229424db5)) +* version up serial_test ([926598c](https://github.com/PlatformNetwork/platform/commit/926598c04d95d5c6ae80c17363eeafc4cacc0cb2)) -### Code Refactoring - -* remove submit_result, auto-detect task completion in log_task ([1763ece](https://github.com/PlatformNetwork/term-challenge/commit/1763ece64cb238619e2a055cec2d5a01bed34ee8)) +### Tests +* **auto-updater:** add helper-backed coverage tests ([f572dd8](https://github.com/PlatformNetwork/platform/commit/f572dd8be5dcf59d62fa467725bf55e6380a4c27)) +* broaden platform-bittensor coverage ([add3be8](https://github.com/PlatformNetwork/platform/commit/add3be8ada943e9179a8bb7306afebeb9640a53c)) +* **challenge-orchestrator:** add env and helper coverage ([a97b84e](https://github.com/PlatformNetwork/platform/commit/a97b84eaa5f0ae651c5e2c3f5326d4db396f9d08)) +* **challenge-orchestrator:** broaden orchestrator, lifecycle, evaluator, and health coverage ([ed67e29](https://github.com/PlatformNetwork/platform/commit/ed67e297c08b8824b2eb9aabd709e99144cc101c)) +* **challenge-orchestrator:** expand config/health/evaluator coverage ([3c30e6b](https://github.com/PlatformNetwork/platform/commit/3c30e6b631682af637d7f9bdbf23d4c7f082acaa)) +* **challenge-sdk:** Add comprehensive test coverage ([168d03d](https://github.com/PlatformNetwork/platform/commit/168d03d3e9cc967ee503485f8ad33ca13622746d)) +* **challenge-sdk:** Add comprehensive test coverage for challenge SDK modules ([6dd0d55](https://github.com/PlatformNetwork/platform/commit/6dd0d5585cdeacda9f026bb66134e1bf4438003a)) +* **challenge-sdk:** Add database persistence test across reopens ([b7a7984](https://github.com/PlatformNetwork/platform/commit/b7a7984c35664378a35801b225531b373ea638d5)) +* **challenge-sdk:** enhance multi-agent testing in get_latest_results ([ddf89c0](https://github.com/PlatformNetwork/platform/commit/ddf89c0788f19197ba432470efa9ba543880f9fb)) +* **consensus:** add comprehensive test coverage for consensus module ([fe39408](https://github.com/PlatformNetwork/platform/commit/fe39408c27b02b2b984b8397a9e6abc20c58958c)) +* **consensus:** comprehensive coverage improvements for platform-consensus ([7d96ec8](https://github.com/PlatformNetwork/platform/commit/7d96ec8a6829c51ae91ec0b6b8e07ff1601374c4)) +* **distributed-db:** Achieve 97% test coverage across all modules ([982c67c](https://github.com/PlatformNetwork/platform/commit/982c67c4e7faa7c90c8539f4f3185b4544601ef1)) +* **distributed-db:** Achieve 97% test coverage across all modules ([1946fd6](https://github.com/PlatformNetwork/platform/commit/1946fd6e2c8c60949f8b4e6a78658e8b7f966db1)) +* **distributed-db:** consolidate test helpers and fix code review issues ([d2dade0](https://github.com/PlatformNetwork/platform/commit/d2dade00e83ebf661b340e7c3364ef17463a4e47)) +* **epoch:** add comprehensive unit tests achieving 95%+ coverage ([ceb4984](https://github.com/PlatformNetwork/platform/commit/ceb49845f4bfe5eedb1c6555331c48051fdf3359)) +* **epoch:** add comprehensive unit tests achieving 95%+ coverage ([4e96e50](https://github.com/PlatformNetwork/platform/commit/4e96e50c2f9c6ab3e8952961b3231ee0f56d6607)) +* expand bittensor helper coverage ([e6c4950](https://github.com/PlatformNetwork/platform/commit/e6c49505b169bce1d2c6dc1e48166272ec70b50d)) +* **platform-core:** Add comprehensive test coverage for core module ([446af5a](https://github.com/PlatformNetwork/platform/commit/446af5a7cfd01bf42ce1365ad1238a7c3a749a3c)) +* **platform-server:** add comprehensive unit tests ([8ebc662](https://github.com/PlatformNetwork/platform/commit/8ebc66281f7bc30ba74f2da9008efe04b143fccd)) +* **platform-server:** add comprehensive unit tests ([551a4f4](https://github.com/PlatformNetwork/platform/commit/551a4f4147789caaa0c361d0ec9ea395fc403666)) +* **platform-server:** improve test quality and reduce duplication ([5726879](https://github.com/PlatformNetwork/platform/commit/57268791d2fc4a4428c60cc942efd4fad7365e17)) +* **rpc:** add comprehensive test suite for platform-rpc module ([7cb4a33](https://github.com/PlatformNetwork/platform/commit/7cb4a33c4e08af0097f00b119605fb57f6932d70)) +* **rpc:** add comprehensive test suite for platform-rpc module ([55fc570](https://github.com/PlatformNetwork/platform/commit/55fc570c338885f1c81da349bf6ffb4b8b76dbec)) +* **secure-runtime:** achieve comprehensive test coverage for container runtime ([93459a8](https://github.com/PlatformNetwork/platform/commit/93459a8b2c18f00fd8ef1a502141c617c6100df1)) + +## [0.2.0](https://github.com/PlatformNetwork/platform/compare/v0.1.0...v0.2.0) (2026-01-04) -### Miscellaneous -* add migration for forced_weights table ([1f26565](https://github.com/PlatformNetwork/term-challenge/commit/1f265652c47cff7a22ba09e988647df2d5708d6d)) - -## [0.2.1](https://github.com/PlatformNetwork/term-challenge/compare/v0.2.0...v0.2.1) (2026-01-12) +### โš  BREAKING CHANGES +* Production validators now require the broker by default. ### Features -* add detailed agent status endpoint with all phases and timings ([f3dfa7c](https://github.com/PlatformNetwork/term-challenge/commit/f3dfa7cda776323dbf48f07ef648c988fe5f5103)) -* add GET /api/v1/agent/{hash}/code endpoint for public code visibility ([4c8e1ac](https://github.com/PlatformNetwork/term-challenge/commit/4c8e1ac443ea8f4d43c8e258d7249c321ae334a4)) -* Add real-time task streaming cache for live evaluation progress ([e61556c](https://github.com/PlatformNetwork/term-challenge/commit/e61556cf4601e6de99e4157acd3a730ecc5bb95e)) +* add --no-bootnode flag and improve bootnode reconnection ([c1045a6](https://github.com/PlatformNetwork/platform/commit/c1045a6cb9b8b7b067d51bf4b9fa4b8639ccbe56)) +* add bridge API to proxy submissions to term-challenge ([85c30da](https://github.com/PlatformNetwork/platform/commit/85c30da11153c0d31404802ac0dbd13482423fe3)) +* add challenge custom events for WebSocket broadcast ([39b0ccf](https://github.com/PlatformNetwork/platform/commit/39b0ccfd80035759690b2aa507f4a1f7da4a5d70)) +* add Custom P2P message type for challenge-specific protocols ([00ebeec](https://github.com/PlatformNetwork/platform/commit/00ebeec7ba46b9b53b1490a624836d5f6cca4b13)) +* add docker-compose for bootnode deployment ([d0f3f1e](https://github.com/PlatformNetwork/platform/commit/d0f3f1ea87ebacfd035098df4b02741b00fe6147)) +* Add dynamic route discovery via /.well-known/routes ([106681f](https://github.com/PlatformNetwork/platform/commit/106681fb60b88adced6adcf8c7cd2226288e3d78)) +* add infinite retry loop (30s) for platform-server connection in validator-node ([d8e28b6](https://github.com/PlatformNetwork/platform/commit/d8e28b651426006fc6f7427b79ccb685018f9d29)) +* add keypair to RpcHandler for webhook P2P broadcast ([0f4eb83](https://github.com/PlatformNetwork/platform/commit/0f4eb83ad7e9794d0d0b8e032d7b2f2aa1c83622)) +* add P2P message forwarding to challenge containers ([8abeda7](https://github.com/PlatformNetwork/platform/commit/8abeda761ce2c8efe6e831bfc0584bee65d75628)) +* add periodic cleanup of stale task containers ([4b26cf4](https://github.com/PlatformNetwork/platform/commit/4b26cf45d85ec6887731212d7f0d160086cca25e)) +* add periodic validator sync to challenge containers ([cff55d7](https://github.com/PlatformNetwork/platform/commit/cff55d7aa86e470d20836ee3ccae3e8f648c0bec)) +* add persistent data volume for challenge containers ([1b5463b](https://github.com/PlatformNetwork/platform/commit/1b5463b7fa5d377aaf45f188a0d9c2c2233424a4)) +* add platform-server (central API) with PostgreSQL, Data API, Claim/Lease ([6bd057d](https://github.com/PlatformNetwork/platform/commit/6bd057dde1c9ba94b6074b4f27ffbe3c4bee8bf6)) +* add secure-container-runtime crate with powerful helpers ([38862c4](https://github.com/PlatformNetwork/platform/commit/38862c4c9d986e541e2bd3316c90695890af95a9)) +* Add Sentry error monitoring (enabled by default) ([fdd0f65](https://github.com/PlatformNetwork/platform/commit/fdd0f650a056eb2463267fd4c591a024eda3868f)) +* add verbose logging option to docker-compose.server.yml ([57e17e6](https://github.com/PlatformNetwork/platform/commit/57e17e6e178435ea5c7e25070e9054ee5996916f)) +* add WebSocket event listener for distributed evaluation ([54057fd](https://github.com/PlatformNetwork/platform/commit/54057fde405c3bd575b2ae5637485b0e0af52482)) +* add WebSocket transport to secure-container-runtime ([004f60c](https://github.com/PlatformNetwork/platform/commit/004f60c5d57c197adde4a10e3ab175ed2138af5a)) +* authenticated WebSocket + validator whitelist endpoint ([f18b74b](https://github.com/PlatformNetwork/platform/commit/f18b74b5a7df94689ea19df0bae2b5a2e5be0993)) +* auto-convert hotkeys to UIDs in weight submission ([319f92d](https://github.com/PlatformNetwork/platform/commit/319f92d6b20bc2b2d873ed85a3451b1f59e3e881)) +* auto-create Docker network and connect validator ([e8de0ce](https://github.com/PlatformNetwork/platform/commit/e8de0ce876b6410e4b7ed1c68a3bc98ac591f0cb)) +* auto-pull docker images and add refresh challenges command ([3b9f9f2](https://github.com/PlatformNetwork/platform/commit/3b9f9f2570f20044ca429bc6f590448ad354a4cf)) +* Auto-retry bootstrap peer connection every 30 seconds ([326b8a6](https://github.com/PlatformNetwork/platform/commit/326b8a68d4465f807caa6593414f4892781ba17a)) +* auto-start challenge containers from platform-server registry ([4362a3c](https://github.com/PlatformNetwork/platform/commit/4362a3cabf1f2c8f55c8cd2993c8ebfdaa84d89c)) +* centralized LLM review with miner API key ([39e72c1](https://github.com/PlatformNetwork/platform/commit/39e72c1d217bbc67f7974d4cae58ebc60c3a282d)) +* **challenge-sdk:** add P2P chain storage module ([8bac6e9](https://github.com/PlatformNetwork/platform/commit/8bac6e932f1b6f3a15277b02d1087b32fd8972f7)) +* **consensus:** Add stake-based governance with bootstrap period ([7dc4e3f](https://github.com/PlatformNetwork/platform/commit/7dc4e3fc54df942fc89a937d5ccd3c2a2e61e7ff)) +* **csudo:** complete CLI refactoring with beautiful interactive mode ([60b115c](https://github.com/PlatformNetwork/platform/commit/60b115ca6c4818a74fac6474b4a504f5fe659097)) +* Derive libp2p peer ID from validator keypair for stable identity ([e64b1c6](https://github.com/PlatformNetwork/platform/commit/e64b1c63f27057d6e938b06ff92ec8b3581d5bdd)) +* DEVELOPMENT_MODE support for local Docker images ([71a8086](https://github.com/PlatformNetwork/platform/commit/71a80861bea969125584826b9830055147f5a68e)) +* emission-based weight distribution with UID 0 burn ([169ac1b](https://github.com/PlatformNetwork/platform/commit/169ac1b72787a6ab4bec524d9ce8dac0cc06e8fc)) +* generic bridge API for multi-challenge support ([686c44f](https://github.com/PlatformNetwork/platform/commit/686c44fdebc9d8ab65863204b5313007d8987ea4)) +* Hardcode default bootnode address ([9f270af](https://github.com/PlatformNetwork/platform/commit/9f270af793565ee0d275c161a236f64253279559)) +* immediate stake validation via identify protocol ([d38b39b](https://github.com/PlatformNetwork/platform/commit/d38b39b42de40323a4b87990fe1848f7eecb68e3)) +* implement sr25519 signature verification for agent submissions ([6fb3820](https://github.com/PlatformNetwork/platform/commit/6fb38206b4a6cbb9515f82e03d19d117a2dbc213)) +* integrate container broker into validator node ([1a0592f](https://github.com/PlatformNetwork/platform/commit/1a0592f3a81ac70551e4b99f98c5f0b48111ff19)) +* Major platform improvements and bittensor-rs Subtensor API integration ([db6cd10](https://github.com/PlatformNetwork/platform/commit/db6cd107725ad0b85a236d77c27db1080da10f8c)) +* make secure broker the default container backend ([e50e1df](https://github.com/PlatformNetwork/platform/commit/e50e1df3b9cfeedf27b97fd9277c54bf3011d99d)) +* **orchestrator:** add ContainerBackend abstraction for secure mode ([5e729c9](https://github.com/PlatformNetwork/platform/commit/5e729c9baa680d7d5e5d649aa682b4d301b8f793)) +* **orchestrator:** use Docker named volume for persistent challenge data ([c1db0ed](https://github.com/PlatformNetwork/platform/commit/c1db0edd3656329789e756e5b505b846965403ed)) +* pass BROADCAST_SECRET to challenge containers ([a5d7505](https://github.com/PlatformNetwork/platform/commit/a5d7505b0be9e2241d7bd67c9dd8de7e69eb7eb2)) +* pass VERBOSE env var to challenge containers for debug logging ([2c6cb55](https://github.com/PlatformNetwork/platform/commit/2c6cb550f32d548d1b7c2069dac79e607cb0b7d3)) +* **platform-server:** dynamic challenge orchestration ([55686f0](https://github.com/PlatformNetwork/platform/commit/55686f0e1f3af5edf0e2835955c582a6fbec5206)) +* real sr25519 signatures + binary evaluation in Docker ([353aff3](https://github.com/PlatformNetwork/platform/commit/353aff3b8180aca7acaf66631950efd7d46814e3)) +* **sdk:** add P2P messages for real-time progress sharing ([b524581](https://github.com/PlatformNetwork/platform/commit/b524581a779e69827a6e8dbe90d5e392cd30fec9)) +* **security:** add stake-weighted PBFT, merkle sync verification, and hotkey rate limiting ([27353a0](https://github.com/PlatformNetwork/platform/commit/27353a08c773b8cca6b83e16df4ca2c57b6db651)) +* **security:** implement authenticated P2P communication with challenge containers ([5e48ea2](https://github.com/PlatformNetwork/platform/commit/5e48ea2ad919daaab8798a6d73c8e5137070786f)) +* **sentry:** improve error tracking with hotkey context ([1039741](https://github.com/PlatformNetwork/platform/commit/103974185243bd78c07c87465827cc44b885fe59)) +* **server:** add submissions and evaluations API routes ([88e9d4a](https://github.com/PlatformNetwork/platform/commit/88e9d4a0e7d1856fc90dd7300d65808bb6053f46)) +* sr25519 crypto + Docker image whitelist security ([b723820](https://github.com/PlatformNetwork/platform/commit/b7238203f491179033e2e518ee57e273b0de6eee)) +* **state:** add versioned state serialization with automatic migration ([1fe787b](https://github.com/PlatformNetwork/platform/commit/1fe787b5328507f36e12d1f98e4e1d79ce829d6d)) +* **storage:** add anti-corruption protections for RocksDB ([f45d41c](https://github.com/PlatformNetwork/platform/commit/f45d41c7c9ce73be8db18d7533005ece9b7d1913)) +* sync metagraph at startup for validator stake lookup ([c155fe4](https://github.com/PlatformNetwork/platform/commit/c155fe49a04237aad1a7f4c76b202a84149aafc4)) +* unified platform binary with server/validator modes ([5036425](https://github.com/PlatformNetwork/platform/commit/5036425e4336105d61fb249fba9a8c42053db24f)) +* validate miner_hotkey is SS58 format in /submit endpoint ([972161d](https://github.com/PlatformNetwork/platform/commit/972161d2a5cc26715a82e7d8c46fe722894abe56)) +* **validator-node:** authenticate WebSocket connection with signature ([5934e7d](https://github.com/PlatformNetwork/platform/commit/5934e7d1bb1bcc5b5197dbcd217b0ada3d95409e)) +* **validator-node:** handle ChallengeStopped WebSocket event ([f3d52d0](https://github.com/PlatformNetwork/platform/commit/f3d52d0d10d9b79d8762bdc6f76384c3151e2e37)) +* **validator:** integrate with platform-server for centralized weights ([1c5b77a](https://github.com/PlatformNetwork/platform/commit/1c5b77ad6fb677355acd3d6f58e5b9a865effdcb)) +* **validator:** sync validators to challenge containers on startup ([94f72c5](https://github.com/PlatformNetwork/platform/commit/94f72c5124ab8d598b2571964336b2522bd34a49)) +* **weights:** Add CRv4 timelock encryption support ([475126d](https://github.com/PlatformNetwork/platform/commit/475126d9113f53316da35a2b7eadf5f007412724)) +* **weights:** add persistence for commit-reveal state ([53dd4dd](https://github.com/PlatformNetwork/platform/commit/53dd4dd482867049e13ec2c1fff0bf4fb836d2a9)) ### Bug Fixes -* cleanup_stale_claims type error (use make_interval with i32) ([91466cd](https://github.com/PlatformNetwork/term-challenge/commit/91466cd49e0a5b14f4decaaab81e78d262b887ce)) -* decay based on last task completion + disable_decay flag + heartbeat URL ([02cbadf](https://github.com/PlatformNetwork/term-challenge/commit/02cbadf577af5e3fa2df4d9d8a53d9c561d58b01)) -* filter out completed agents from validator jobs ([8a5a21e](https://github.com/PlatformNetwork/term-challenge/commit/8a5a21ed9af15e113285359332a34d75128177f8)) -* use CONTAINER_BROKER_WS_URL instead of BROKER_WSS_URL ([0db1eef](https://github.com/PlatformNetwork/term-challenge/commit/0db1eef7898297de95d5159aa81b41dd248f5a2b)) -* Validators now evaluate only their assigned tasks (10 each) ([ac8828a](https://github.com/PlatformNetwork/term-challenge/commit/ac8828a239bffb19d76a9118c095fe3409c86556)) - -## [0.2.0](https://github.com/PlatformNetwork/term-challenge/compare/v0.1.0...v0.2.0) (2026-01-12) - - -### โš  BREAKING CHANGES - -* **sdk:** SDK API completely redesigned - -### Features - -* 3-validator task distribution, cancel command, and improved error handling ([e18083b](https://github.com/PlatformNetwork/term-challenge/commit/e18083b7a555280cd6e8d0c2978c00c303651b48)) -* add assignment monitor for stale validator reassignment ([31fbb15](https://github.com/PlatformNetwork/term-challenge/commit/31fbb15e6fc6138d082d5b0be62ff4769844fd86)) -* add binary caching to validator worker ([bbf237e](https://github.com/PlatformNetwork/term-challenge/commit/bbf237ebd8d5b0fa3a4ede246cf19e96430c67ad)) -* add DirectDockerBackend and binary agent runner for local bench testing ([d84ed75](https://github.com/PlatformNetwork/term-challenge/commit/d84ed7586fe97158f6f6d94b293055e6f355463c)) -* add disable_decay and disable_public_code fields ([172223f](https://github.com/PlatformNetwork/term-challenge/commit/172223f5cf94289b98fd35845921fd171e4004eb)) -* add epoch calculation with custom start block ([ebe42fa](https://github.com/PlatformNetwork/term-challenge/commit/ebe42fad75bae76ea5982a820648c2fe0e91fdb9)) -* add multi-file package submission support ([d1d8cba](https://github.com/PlatformNetwork/term-challenge/commit/d1d8cba2b8b97c83e4e0b43322dfe8b47fa761f4)) -* add real-time task logging to platform server ([54b1b42](https://github.com/PlatformNetwork/term-challenge/commit/54b1b422f0c7fc746d6baddbad499fc1f4de36af)) -* add status, total_cost_usd and success_rate to leaderboard ([5716384](https://github.com/PlatformNetwork/term-challenge/commit/5716384cfcefca812c7ba76a4e1ef7212931f788)) -* add Terminus-2 agent adapted for Term SDK 2.0 ([e72c7eb](https://github.com/PlatformNetwork/term-challenge/commit/e72c7ebb147a5ebf91f917dbc4e2202a154274a5)) -* add time-based reward decay system ([20d978d](https://github.com/PlatformNetwork/term-challenge/commit/20d978d522eb9c52f1ea1942a12f2ac26297fa4a)) -* add verbose agent logging and evaluation resume support ([4415307](https://github.com/PlatformNetwork/term-challenge/commit/4415307a549464b8d0e3b957a984914c92a95505)) -* add verbose logging for container backend and compilation ([9886e1f](https://github.com/PlatformNetwork/term-challenge/commit/9886e1f5a86fd7ef1bea5e0e386b48cb5d48b143)) -* add weight and submitted_at to leaderboard responses ([d6d8e37](https://github.com/PlatformNetwork/term-challenge/commit/d6d8e37442ca30426d846e80a968369e44f9c347)) -* automatic cleanup of orphan Docker volumes ([cf148a3](https://github.com/PlatformNetwork/term-challenge/commit/cf148a3b2026d20b9a7b84bb0c75caeb3488b75c)) -* cleanup stale task containers at validator startup ([8da0f7b](https://github.com/PlatformNetwork/term-challenge/commit/8da0f7bd4fe38c4477ae24bebcbc1d183bcdec45)) -* distributed task evaluation and validator readiness system ([bdcf46d](https://github.com/PlatformNetwork/term-challenge/commit/bdcf46d911e65c45906073b8068603e3e9f923fb)) -* Docker-in-Docker fixes and glibc compatibility ([75a81c6](https://github.com/PlatformNetwork/term-challenge/commit/75a81c6c2944e9c11fd8ee9fa2301c407dd49107)) -* Implement StaticX for portable agent binaries ([90652ea](https://github.com/PlatformNetwork/term-challenge/commit/90652ead65478526df664f738f949d6bf77c9958)) -* improve LLM proxy cost tracking and add Grok provider support ([395fd9b](https://github.com/PlatformNetwork/term-challenge/commit/395fd9bfcfa2ee32a5108e90d5197e876ab5dc4b)) -* install full SDK with LLM support during compilation ([8674eac](https://github.com/PlatformNetwork/term-challenge/commit/8674eacc4d687d09d76a991dd20d37d31b616082)) -* LLM proxy with cost tracking, task observability APIs, streaming support ([2eb5fb0](https://github.com/PlatformNetwork/term-challenge/commit/2eb5fb0d506a0f4f95d92d267858bcc1778f05eb)) -* **maintenance:** add periodic maintenance task + require all validators for consensus ([b0e1713](https://github.com/PlatformNetwork/term-challenge/commit/b0e171329c1f081adf765106be9717bfad9abc5a)) -* migrate bench run to use binary agent system ([1915444](https://github.com/PlatformNetwork/term-challenge/commit/1915444513a3a2314fbcc18a12127488791e238d)) -* move validator and task assignment to compile_worker ([7958323](https://github.com/PlatformNetwork/term-challenge/commit/7958323f8344084680eaf5624a8bc335bd80c964)) -* replace epoch-based submission rate limit with time-based (3.6h cooldown) ([6216f33](https://github.com/PlatformNetwork/term-challenge/commit/6216f3300815c39fd6b3edcc97fa60b6b3363a23)) -* replace validator whitelist with stake-based auth via metagraph ([bfb91f0](https://github.com/PlatformNetwork/term-challenge/commit/bfb91f09d57e34d338c1dd6e21fb360fcadbe917)) -* **sdk:** SDK 2.0 with agent-controlled execution model ([41b86a4](https://github.com/PlatformNetwork/term-challenge/commit/41b86a474a8f3f8052901b380010567d79d4d65d)) -* use ContainerBackend for validator worker task execution ([31d7022](https://github.com/PlatformNetwork/term-challenge/commit/31d7022084ab9544f9b561bb5de9bb16f85c145c)) -* use secure broker for building compiler image ([be617a2](https://github.com/PlatformNetwork/term-challenge/commit/be617a205dc182038de301afdf16d006f81cf010)) -* winner-takes-all weight calculation with manual validation ([6915096](https://github.com/PlatformNetwork/term-challenge/commit/691509640d36d285390b78c54d1e39baaed6bb97)) +* add 30s delay before crash exit to allow Watchtower updates ([1635a16](https://github.com/PlatformNetwork/platform/commit/1635a168a5b0350fc6e35c59a1bb11301b8a8f2d)) +* add bootnode retry timer to validator event loop ([007b920](https://github.com/PlatformNetwork/platform/commit/007b9205d7cd513b7e293f6be7a908c88d3950c1)) +* add bridge routes to unified platform binary ([ce04921](https://github.com/PlatformNetwork/platform/commit/ce0492162aead5b1ed5b8831117e3cbccc8c92d3)) +* add cache bind mount for Docker-in-Docker path mapping ([107b064](https://github.com/PlatformNetwork/platform/commit/107b064abadc22eef1da572032914cdf866f3577)) +* add challenge to ChainState when receiving P2P Proposal ([0ab4c2e](https://github.com/PlatformNetwork/platform/commit/0ab4c2e525b0abfb60e35c3ce170a0e0c5aba3bf)) +* add DNS servers to docker-compose.server.yml ([ef47913](https://github.com/PlatformNetwork/platform/commit/ef479131e6bdfbf8dc4292acd293a02c60b47c22)) +* add eval logs bind mount for Docker-in-Docker ([1b29e3d](https://github.com/PlatformNetwork/platform/commit/1b29e3d012a2617ff34ee1dce78805dc9434942e)) +* add events/broadcast route to platform bin + secure with BROADCAST_SECRET ([ebb468f](https://github.com/PlatformNetwork/platform/commit/ebb468f1873c337f621ca89b6c21b1526cdadc0f)) +* add logging for P2P outbox message processing ([aef5359](https://github.com/PlatformNetwork/platform/commit/aef535966e99ddb85b439b17a57dd83b969c8f3b)) +* Add peer ID to bootstrap peer address ([2be994d](https://github.com/PlatformNetwork/platform/commit/2be994daae8662ea563ce2e8f0bbc3fd2cc734fe)) +* add whitelist route and metagraph sync to platform bin server mode ([c09cd79](https://github.com/PlatformNetwork/platform/commit/c09cd799c2252e91a803fde760ad7b0ae0dd38cc)) +* allow all ghcr.io/platformnetwork/ images in agent runner whitelist ([9ed64b3](https://github.com/PlatformNetwork/platform/commit/9ed64b309e1c504ec0dc6a77998f7b640bed75a8)) +* allow custom DATABASE_URL (Supabase, etc.) ([f543705](https://github.com/PlatformNetwork/platform/commit/f543705981c0c39851719efc70b675dd1477e4e0)) +* auto-detect validator network for challenge containers ([5bf156f](https://github.com/PlatformNetwork/platform/commit/5bf156f32addaaf133f6f6253a163c3efeda6193)) +* auto-recover from corrupted distributed-db and state ([332f75d](https://github.com/PlatformNetwork/platform/commit/332f75d2977ccdfb0e4ef58e41698129c69cc7b3)) +* **bittensor:** update to bittensor-rs f79759e with DRAND fix ([11cfe4a](https://github.com/PlatformNetwork/platform/commit/11cfe4a1831c4c30ce82126c3657f31f2205bca3)) +* broadcast ChallengeStarted event to validators ([9047616](https://github.com/PlatformNetwork/platform/commit/9047616cd000b48b4fa764eefaa8e2f7c789bbb6)) +* broadcast ChallengeStopped event to validators in platform bin server ([b99e43d](https://github.com/PlatformNetwork/platform/commit/b99e43d5c5217de5a5e3c229b6f3c90032847fda)) +* **challenge-sdk:** fix consensus test math ([8aa071f](https://github.com/PlatformNetwork/platform/commit/8aa071f4219028972cc7140e3157d76e901b8036)) +* change stale container cleanup to 2 hours ([8f4592d](https://github.com/PlatformNetwork/platform/commit/8f4592dda88c69c9b4d08f1c9521e974ed6f9bf5)) +* **ci:** move -E filter before -- in cargo llvm-cov nextest ([c91e830](https://github.com/PlatformNetwork/platform/commit/c91e8309c2efa47a44df9f2fed0c6ab48f57c902)) +* **ci:** properly use cargo cache across jobs ([14bc231](https://github.com/PlatformNetwork/platform/commit/14bc2315b2703a1727579309d602bb46a6de3d2b)) +* **ci:** separate cache keys for release and debug builds ([a2149e4](https://github.com/PlatformNetwork/platform/commit/a2149e4ff1c7cc39384ee58655060dcf5cbf4c09)) +* clippy warnings and add mandatory CI hooks ([9a9c395](https://github.com/PlatformNetwork/platform/commit/9a9c395da843e63d27f3aec6cd25367cede8b207)) +* **clippy:** resolve all clippy warnings for CI ([e8829bd](https://github.com/PlatformNetwork/platform/commit/e8829bd87dc830d6688e9bf1dacd68a185ffb114)) +* connect validator container to platform-network on startup ([41d9d8b](https://github.com/PlatformNetwork/platform/commit/41d9d8b2784743ce7a4ae2b5ed45d05fd01d6c20)) +* **consensus:** correct test expectations for 33% default threshold ([d621eb8](https://github.com/PlatformNetwork/platform/commit/d621eb8cd150745356997276604b46a228e29450)) +* **consensus:** improve P2P mesh stability and reduce quorum threshold ([34f65a0](https://github.com/PlatformNetwork/platform/commit/34f65a0bfd502f3807fc208bf52986311b8319eb)) +* correct netuid_index calculation and salt serialization ([c2ba34b](https://github.com/PlatformNetwork/platform/commit/c2ba34bee74d2a0be2120e44b95c2bff1fa536d0)) +* correct sled corruption recovery to remove actual db files ([00df049](https://github.com/PlatformNetwork/platform/commit/00df049bb7d86e06b7f57a8d6275ce87317d25e6)) +* **db:** strip trailing database name from URL ([19d4298](https://github.com/PlatformNetwork/platform/commit/19d4298f16f7737f41478e13f74f61c733a48bf0)) +* Derive peer ID from hotkey (public key) not seed ([39f578d](https://github.com/PlatformNetwork/platform/commit/39f578d5810b4365e99ec313a13f1ee9cb2b8a9b)) +* **docker:** add clang/libclang-dev for zstd-sys bindgen ([650d5f7](https://github.com/PlatformNetwork/platform/commit/650d5f71601248cf4f87251e4400eb4836c88434)) +* **docker:** default to validator-node for backward compatibility ([4d6f210](https://github.com/PlatformNetwork/platform/commit/4d6f210e492c5c688d78bb3f7107c338637d1e6e)) +* **docker:** use ubuntu:24.04 for glibc 2.39 compatibility ([af00fac](https://github.com/PlatformNetwork/platform/commit/af00facebfe1193671930615bb82c59245a2e6aa)) +* don't cleanup challenge containers with different suffixes ([f433704](https://github.com/PlatformNetwork/platform/commit/f433704f62ef1b878e35eee7b397ae1272b142af)) +* force exact network name 'platform-network' ([27071de](https://github.com/PlatformNetwork/platform/commit/27071de11cf53231348054df216001a64a3895d4)) +* **gossipsub:** resolve empty mesh issue by removing add_explicit_peer calls ([375ce2e](https://github.com/PlatformNetwork/platform/commit/375ce2eb3b2830a1f7d36f01ee5b0e47979fdebf)) +* implement targeted P2P send (broadcasts to all) ([121d5f0](https://github.com/PlatformNetwork/platform/commit/121d5f07c967f78e0c81dfb92809925e70311c9e)) +* include challenge_configs in chain_getState RPC response ([00690e7](https://github.com/PlatformNetwork/platform/commit/00690e76917baf1f0c507dd14513dba527cb63a7)) +* migrate remaining ed25519 to sr25519 + add mandatory CI hooks ([65bd1e8](https://github.com/PlatformNetwork/platform/commit/65bd1e866b0e0c9357ad819070bd170728d3802e)) +* **network:** add mesh repair mechanism for gossipsub ([bad9b34](https://github.com/PlatformNetwork/platform/commit/bad9b349cb385943880ec8492ade05b6c9701b9e)) +* **network:** add mesh repair mechanism for gossipsub ([b171ff4](https://github.com/PlatformNetwork/platform/commit/b171ff4b0849118ffff94e190649d189a1a42178)) +* **network:** handle gossipsub Subscribed event for proper mesh membership ([f8efbd6](https://github.com/PlatformNetwork/platform/commit/f8efbd6fa66469cce6e580eb2d2f26a526901971)) +* **network:** improve gossipsub mesh stability for bootnodes ([381ae26](https://github.com/PlatformNetwork/platform/commit/381ae26b1d45a6a37f901e839fe7c5159f98004b)) +* **orchestrator:** pass PORT=8080 env var to challenge containers ([ab4782e](https://github.com/PlatformNetwork/platform/commit/ab4782e2efbd912974971cff40401ff36d05da39)) +* Pass bootstrap-peer argument directly in docker-compose ([d3ac865](https://github.com/PlatformNetwork/platform/commit/d3ac865e198aeb7bd589082c9b43a9c8152da7e4)) +* pass real challenge config values in ChallengeStarted event ([06dc544](https://github.com/PlatformNetwork/platform/commit/06dc544eecfecd80d2e2287c9bee8491871cc136)) +* pass VALIDATOR_SECRET_KEY to challenge containers ([44e1dce](https://github.com/PlatformNetwork/platform/commit/44e1dce594eb6fdaab96096f84edb20e608e5cc8)) +* populate registered_hotkeys from metagraph sync ([aff29f3](https://github.com/PlatformNetwork/platform/commit/aff29f3a2be60ee8deab7425268f1a81e8ad81e5)) +* remove obsolete version from docker-compose.bootnode.yml ([3ba56f2](https://github.com/PlatformNetwork/platform/commit/3ba56f26af552bd5543919d1c0d4d930704b188f)) +* resolve CI lint and clippy warnings ([c470b4c](https://github.com/PlatformNetwork/platform/commit/c470b4c1b8d9cf263a0ed066ffa8b5d2ea4444a1)) +* reveal mechanism weights when internal epoch manager detects reveal phase ([3291318](https://github.com/PlatformNetwork/platform/commit/3291318451b35b949485caf7efcbbbd96202bd9f)) +* skip stake validation in no-bittensor mode for PeerIdentified event ([f6ff0a2](https://github.com/PlatformNetwork/platform/commit/f6ff0a28f31084913d27d9176690f4070e02048c)) +* trigger commit/reveal on Subtensor timing windows in tests ([fa85fea](https://github.com/PlatformNetwork/platform/commit/fa85fea5c9df466907af165630077602b0b4fbfa)) +* Update bittensor-rs to 3fc7ab5 and export sync_metagraph ([c328831](https://github.com/PlatformNetwork/platform/commit/c328831e3b091ecde107a140d8d1c562ab83d50f)) +* Update bittensor-rs to a1a4558 and fix clippy warnings ([b9bc10c](https://github.com/PlatformNetwork/platform/commit/b9bc10c7d0d99480dcc5c286eb7a477880ca4156)) +* Update bittensor-rs to db10a19 - fix commit-reveal detection ([80f10e5](https://github.com/PlatformNetwork/platform/commit/80f10e53ac7a78754a2e933d5e9123b8dd96b666)) +* update challenge endpoints for P2P and dev mode containers ([9ee7c7a](https://github.com/PlatformNetwork/platform/commit/9ee7c7a80444d370273d3f0f3eaf44bdaef07ddd)) +* update coverage badge generation to use peaceiris/actions-gh-pages ([347daba](https://github.com/PlatformNetwork/platform/commit/347daba15461fb59306b244e1a6296b7c2eed2d5)) +* update release-please config for workspace ([c6ff25c](https://github.com/PlatformNetwork/platform/commit/c6ff25c57f25fd3add9bf70ae000d1c80e4deb01)) +* use challenge name (not UUID) for CHALLENGE_ID env var ([b5bd205](https://github.com/PlatformNetwork/platform/commit/b5bd205a611242222428854bede60ce26264b157)) +* use container ID for challenge container naming suffix ([39e4724](https://github.com/PlatformNetwork/platform/commit/39e4724ff13ca2ae37fdf9c4f39fa61b341c89fb)) +* use container name for challenge endpoint instead of 127.0.0.1 ([f04d4fa](https://github.com/PlatformNetwork/platform/commit/f04d4fa86a0a603bc6834907b7c1642facfdf96b)) +* use correct salt type for reveal_weights ([41e0a05](https://github.com/PlatformNetwork/platform/commit/41e0a05fab3cf95e82e07b1d4fb68ce02f28669a)) +* use effective stake (alpha + root) for validator validation ([cb30a93](https://github.com/PlatformNetwork/platform/commit/cb30a935d547375634f36ba25396a901552550b2)) +* use orchestrator endpoint instead of hardcoded URL for challenge containers ([bd4c349](https://github.com/PlatformNetwork/platform/commit/bd4c349c4cdc402df1e9b1108b111cb371d66a4f)) +* use PLATFORM_PUBLIC_URL for validator challenge containers ([7ce3b24](https://github.com/PlatformNetwork/platform/commit/7ce3b24b890a1820be2c9aff6e87db56c72ff24b)) +* Use proper Substrate SR25519 derivation for peer ID ([95321e5](https://github.com/PlatformNetwork/platform/commit/95321e52ce4aeccdd828a243ec7408ab00b0c3c7)) +* use real container endpoints for P2P outbox polling ([ae9250d](https://github.com/PlatformNetwork/platform/commit/ae9250df751e745ffb7b3a632eaeadf91ce72eeb)) +* use simple release type for workspace ([60c3078](https://github.com/PlatformNetwork/platform/commit/60c3078cef809d2ea07e1b136e617917836dcd34)) +* use simple release type with manifest config ([ba79252](https://github.com/PlatformNetwork/platform/commit/ba79252ff3b4c847376215e94a17666cdba521f8)) +* use stake_weight for validator stake validation ([be15cdc](https://github.com/PlatformNetwork/platform/commit/be15cdc769547d030410afab71de21c542927304)) +* use stored endpoints for challenge container URLs ([2adda1a](https://github.com/PlatformNetwork/platform/commit/2adda1a3d308c614d6aa8043b90e2955a88fe77d)) +* use total_stake from runtime API for validator stake validation ([dcb389c](https://github.com/PlatformNetwork/platform/commit/dcb389c020a90441d38c603451c1217bd1a59b50)) +* use u16 for NetUidStorageIndex in hash computation ([c8a392e](https://github.com/PlatformNetwork/platform/commit/c8a392eb8ad15b8d80e674d6f0a93741e23d1ab3)) +* use u16 salt directly for reveal_mechanism_weights ([72f3806](https://github.com/PlatformNetwork/platform/commit/72f380625e9931c37670d9dfaf8f807f81b1dd51)) +* validator uses Subtensor.set_mechanism_weights() for CRv4 ([eda164e](https://github.com/PlatformNetwork/platform/commit/eda164e21eac7a1ae51294447937882c7ab9724c)) +* validator uses Subtensor.set_mechanism_weights() for CRv4 ([3e0b1e5](https://github.com/PlatformNetwork/platform/commit/3e0b1e5e1fc09974aa7df80ef6a3effc0c75cf7f)) +* **validator:** change default RPC port from 8545 to 8080 ([c610dde](https://github.com/PlatformNetwork/platform/commit/c610dde4001f874520b1695ade98e1602be57f3f)) +* **validator:** check sudo/owner FIRST before any stake validation ([81524af](https://github.com/PlatformNetwork/platform/commit/81524af60381ebf15849de08f5761535f954a85b)) +* **validator:** use correct container name with validator suffix for challenge endpoints ([c6137eb](https://github.com/PlatformNetwork/platform/commit/c6137ebc24c1f97e9853bcd3e99fb732a431042f)) +* warn when validator cannot join platform network ([3c1bdde](https://github.com/PlatformNetwork/platform/commit/3c1bdde332146b7ede99cdbd0240d891ba117b9c)) -### Bug Fixes +### Performance Improvements -* add --break-system-packages flag to pip install in compiler ([7dcbdec](https://github.com/PlatformNetwork/term-challenge/commit/7dcbdec071ffd116a7b7df711c48f889d5aa66e3)) -* add --break-system-packages to httpx pip install ([f228ba6](https://github.com/PlatformNetwork/term-challenge/commit/f228ba65fc489d870d24e6e9b522ebaf0d0a7228)) -* add FLOAT8 cast to RETURNING clause in update_submission_cost ([c514f2c](https://github.com/PlatformNetwork/term-challenge/commit/c514f2cf15b5494a3d5206f5a7184a03859c04bc)) -* add FLOAT8 casts for all REAL column reads in pg_storage ([8ec0efd](https://github.com/PlatformNetwork/term-challenge/commit/8ec0efdca638a29984fe0b8822964a2e6ad8824d)) -* add httpx to PyInstaller hidden imports ([b7d25a6](https://github.com/PlatformNetwork/term-challenge/commit/b7d25a6a1729abb80c438cb6aff4cb5b78ffe5e3)) -* add LLM_MODEL env var support and reduce log noise from /status requests ([f487693](https://github.com/PlatformNetwork/term-challenge/commit/f487693a853806005d67eb071793ccfee239fa3b)) -* add migration 009 for validator_assignment status column ([17886de](https://github.com/PlatformNetwork/term-challenge/commit/17886decbbda47264780c0be2f486a72e0772580)) -* add Pong variant to BrokerResponse for auth success parsing ([dad55b4](https://github.com/PlatformNetwork/term-challenge/commit/dad55b43c56e338b7a52351d547118317ecea4c4)) -* add validator_assignments table and use claude-haiku-4.5 for reviews ([97fdff7](https://github.com/PlatformNetwork/term-challenge/commit/97fdff7d36662da90daf36b445e14461a6b09854)) -* align default timeout with Harbor/terminal-bench (180s) ([2b41e9c](https://github.com/PlatformNetwork/term-challenge/commit/2b41e9ccebf67a5811050b1bbf7c4ec57c8c74d2)) -* align LLM proxy signature format with central server ([ca40138](https://github.com/PlatformNetwork/term-challenge/commit/ca401386bcf7108c760b6fd68a0a705fe5c87f20)) -* always build compiler image, never pull from Docker Hub ([337d345](https://github.com/PlatformNetwork/term-challenge/commit/337d3455ffeacc6ee08733f146879e44f7d0a750)) -* **broker:** add retry logic for WS connection failures ([1188c30](https://github.com/PlatformNetwork/term-challenge/commit/1188c3037589bc85ef29695262ad00040d5e5f8e)) -* build compiler image on demand if not found during compilation ([12de066](https://github.com/PlatformNetwork/term-challenge/commit/12de0663f55ab05087face7bab9b7cf5c422beaa)) -* calculate evaluation costs from llm_usage table ([e5ac0aa](https://github.com/PlatformNetwork/term-challenge/commit/e5ac0aa632a87d4c09629e269a911e3d7f3de4e3)) -* cast f64 to f32 for PostgreSQL REAL columns in cost updates ([08c3613](https://github.com/PlatformNetwork/term-challenge/commit/08c36131b9e11f7842b53f975185e13b5ac09035)) -* check if PyInstaller exists before installing ([78a648d](https://github.com/PlatformNetwork/term-challenge/commit/78a648deb53134ca8174dab34106b8e281a12501)) -* check multiple SDK paths for full SDK installation ([cd9ddb0](https://github.com/PlatformNetwork/term-challenge/commit/cd9ddb040f5bbae9aa79259e72b6c8659b2c3e94)) -* **ci:** separate coverage job to prevent cancellation ([7ba740d](https://github.com/PlatformNetwork/term-challenge/commit/7ba740d3578f4565c53985b749b48b7d5c6b39e9)) -* cleanup orphan compiler containers at startup and use UUID in names ([ec2c026](https://github.com/PlatformNetwork/term-challenge/commit/ec2c0260729ee404382cc850352a038ff783c7de)) -* copy docker directory into images for compiler image building ([ffb42fb](https://github.com/PlatformNetwork/term-challenge/commit/ffb42fb32c2c24be83c2432e0efeb732aa8c5ccc)) -* correct iteration increment in terminus_2 agent loop ([ddca36c](https://github.com/PlatformNetwork/term-challenge/commit/ddca36cff56f4863469af33f735106290f2dde1a)) -* correct signature message for my_jobs endpoint ([cd079d7](https://github.com/PlatformNetwork/term-challenge/commit/cd079d7fe4501a65799222fd7b9ec0b6daca7d5a)) -* decrypt API key before sending to OpenRouter ([4e78be0](https://github.com/PlatformNetwork/term-challenge/commit/4e78be088f043bfb470a53bc6d0a8385073239d1)) -* deduplicate agent logs by tracking last printed line ([6d6abcd](https://github.com/PlatformNetwork/term-challenge/commit/6d6abcdda4e9e68e14e5cb051c3a85b46a210d8f)) -* detect and abort stuck agents with consecutive empty responses ([848a3cc](https://github.com/PlatformNetwork/term-challenge/commit/848a3cc620c226fb243aedfde09daf8102ea6b5c)) -* ensure binutils is installed before PyInstaller ([af6a776](https://github.com/PlatformNetwork/term-challenge/commit/af6a776298e86c428c496a2b57f1a2ad5f25f159)) -* Harbor-compatible test verification and dynamic challenge_id ([319fdd6](https://github.com/PlatformNetwork/term-challenge/commit/319fdd6a37a19afa6a5a1f49df26afc43d5700be)) -* improve broker WS error message to include URL ([b8f7877](https://github.com/PlatformNetwork/term-challenge/commit/b8f7877929a75ff8e57c3e8f27ee883a5768db71)) -* improve Docker error logging for debugging task container failures ([1bffd2a](https://github.com/PlatformNetwork/term-challenge/commit/1bffd2abc2b981c2193143e7132484c1ccbdacf2)) -* include all migrations (006-009) in embedded migrations list ([83c4245](https://github.com/PlatformNetwork/term-challenge/commit/83c42459acec0b4f0a851e569ac6dfbb3515aa40)) -* increase limits and reduce validators ([dca4dd5](https://github.com/PlatformNetwork/term-challenge/commit/dca4dd58291463a5b4cc8be31780c4dab49c0cde)) -* **leaderboard:** show only fully evaluated submissions (status='completed') ([7b7ec1c](https://github.com/PlatformNetwork/term-challenge/commit/7b7ec1c8a305a19eb5909cb475652256643c7e46)) -* map cache directory paths for Docker-in-Docker mounts ([5c4979d](https://github.com/PlatformNetwork/term-challenge/commit/5c4979d4a210848ec73cca1277be5f7593f91394)) -* parse pending_jobs field correctly in validator_worker ([146860e](https://github.com/PlatformNetwork/term-challenge/commit/146860e614f22d2bb454778754c9f1ccfb7f4759)) -* pass LLM proxy env vars to agent binary process ([d630d36](https://github.com/PlatformNetwork/term-challenge/commit/d630d369c26d57c2abe89debf5840fd1635fd981)) -* preserve HTTP status codes in LLM proxy error handling ([f6aa7bb](https://github.com/PlatformNetwork/term-challenge/commit/f6aa7bbf569cefb87a40741e77ba1e6074519348)) -* prevent duplicate jobs and add container concurrency limit ([b3e0276](https://github.com/PlatformNetwork/term-challenge/commit/b3e02766e57909c62c4053c3b6df4eccfd68d5af)) -* PyInstaller extraction issues in task containers ([f73650a](https://github.com/PlatformNetwork/term-challenge/commit/f73650a4c3c7c5e6893ea7515734ce066e87877c)) -* re-declare TERM_REPO_PATH ARG in Dockerfile.server runtime stage ([5bad625](https://github.com/PlatformNetwork/term-challenge/commit/5bad6252fbd5f511d70157d9089cd631a4c5feb9)) -* remove global timeout from SDK - let agent builders define their own ([f0ee67f](https://github.com/PlatformNetwork/term-challenge/commit/f0ee67f58c596366f5efdc469045dbac14c8e614)) -* remove max_steps and timeout_secs from SDK - let agents manage their own limits ([108d262](https://github.com/PlatformNetwork/term-challenge/commit/108d2623a73ae17fa9f921ad030d3e50e3d1a337)) -* remove restrictive cap_drop, run containers as root ([8bc2f75](https://github.com/PlatformNetwork/term-challenge/commit/8bc2f7578427d882cb14125678991951e2430d6a)) -* Remove unnecessary borrow in clippy lint ([5277a64](https://github.com/PlatformNetwork/term-challenge/commit/5277a64299b02f30be7faf91414bc02a3b27ceb9)) -* run verification tests from /workspace directory ([5059f5a](https://github.com/PlatformNetwork/term-challenge/commit/5059f5ac184c54930e9dbe6308f187c7e792dfe1)) -* **sdk:** add remaining_steps and remaining_secs to AgentContext ([eb6fd06](https://github.com/PlatformNetwork/term-challenge/commit/eb6fd067079d395b6ec28512092af4845ed23369)) -* send all required fields to log_task API ([f23ec72](https://github.com/PlatformNetwork/term-challenge/commit/f23ec72aba9e98521f6b15e775da60711d620ccf)) -* set total_validators=2 when queueing submissions + reset window on requeue ([3b0d75f](https://github.com/PlatformNetwork/term-challenge/commit/3b0d75f796001b573cdab4490a7717843aa792d1)) -* stop agent loop on cost_limit_exceeded and empty responses ([f685359](https://github.com/PlatformNetwork/term-challenge/commit/f685359311cf2d24aae19eaad2c28eddb320e487)) -* support both 'done' and 'task_complete' in agent response ([9243cbd](https://github.com/PlatformNetwork/term-challenge/commit/9243cbdd88fc2bcf37714d2f09aceb2031d999fd)) -* update BrokerError to match platform's ContainerError enum format ([496a582](https://github.com/PlatformNetwork/term-challenge/commit/496a58218fb6b86102883fd8227546c55c64f709)) -* update secure-container-runtime to remove cap_drop restrictions ([a10b952](https://github.com/PlatformNetwork/term-challenge/commit/a10b9523289026d60db30f8260f49359177ecef5)) -* use /app as standard working directory (matching harbor) ([d58c349](https://github.com/PlatformNetwork/term-challenge/commit/d58c349b35ebf2da4c2db5e006b51443e26b6a34)) -* use /workspace as default working directory instead of /app ([546af74](https://github.com/PlatformNetwork/term-challenge/commit/546af7413c992d63e4749324568381f2591ec12c)) -* use bash instead of sh for Harbor test scripts ([0892f5d](https://github.com/PlatformNetwork/term-challenge/commit/0892f5db490df1b7135f86fb88adafcfdc45dc16)) -* use CHALLENGE_UUID for broker authentication ([2e429a7](https://github.com/PlatformNetwork/term-challenge/commit/2e429a72dc3f503069e0aafb7612774b9f139858)) -* use correct timeouts from task config ([6b1c812](https://github.com/PlatformNetwork/term-challenge/commit/6b1c8129e048fd718b3a0629c0558ea6224640be)) -* use exec_shell instead of exec to avoid double shell wrapping ([df0cd46](https://github.com/PlatformNetwork/term-challenge/commit/df0cd46846197b6583ee6885c69156dceb602678)) -* use fixed 30 task count and deterministic task selection ([c1210ac](https://github.com/PlatformNetwork/term-challenge/commit/c1210ac0a0316c2c074704eefe038bdcf69c5fc0)) -* use miner's API key directly for LLM security review ([36eff85](https://github.com/PlatformNetwork/term-challenge/commit/36eff853873a941bce24337e50d0ef85de214bef)) -* use python:3.11 full image for PyInstaller (includes binutils) ([a062d3e](https://github.com/PlatformNetwork/term-challenge/commit/a062d3e5e5711e6a5c1ce4b52761cc7b1006e6b4)) -* use simple release type with manifest config ([4876e3c](https://github.com/PlatformNetwork/term-challenge/commit/4876e3c4f00cf9d6a923d58f655fc34363e79f2f)) -* use snake_case serde rename for BrokerResponse to match platform protocol ([999f9ba](https://github.com/PlatformNetwork/term-challenge/commit/999f9bae391d447b3be846c29b74fcf75c3ae437)) +* **ci:** optimize caching for Rust builds and Docker images ([52fb82b](https://github.com/PlatformNetwork/platform/commit/52fb82bd74e064755d851ad4c7d76a63032226c2)) ### Code Refactoring -* remove direct Docker backend, use container names for HTTP communication ([79120ea](https://github.com/PlatformNetwork/term-challenge/commit/79120ea694e3d4b06f32d5b312d2a37310adcdb5)) -* remove local platform-repo copying, use git dependency from Cargo.toml ([e52d711](https://github.com/PlatformNetwork/term-challenge/commit/e52d711fb310028a426fd01bdb27f3b8990162c2)) -* standardize challenge ID to term-challenge, remove CHALLENGE_UUID ([635e53c](https://github.com/PlatformNetwork/term-challenge/commit/635e53c74b8f8276dc4e0c8d3603f7d3a617d717)) -* use secure-container-runtime types from platform ([c3bfc22](https://github.com/PlatformNetwork/term-challenge/commit/c3bfc22c366faed8a0de5e428569e26ddbe837d6)) +* clean dead code in evaluator, increase proxy timeout ([cbffb81](https://github.com/PlatformNetwork/platform/commit/cbffb8107f2deb4f1757a67c0e1e569e0f11b63e)) +* event-driven commit/reveal from Bittensor timing ([4d3f0ff](https://github.com/PlatformNetwork/platform/commit/4d3f0fff314163a2cf1150b81c8ce644c6ab22a9)) +* migrate from ed25519 to sr25519 for Substrate/Bittensor compatibility ([dbf722e](https://github.com/PlatformNetwork/platform/commit/dbf722e4906063b8ef0f9a4a39cadc52faeca89e)) +* remove anti-cheat weight manipulation for pure pass-through ([63ed510](https://github.com/PlatformNetwork/platform/commit/63ed5101e1f262a9b919f50642e4856603e329a6)) +* remove P2P networking, centralized architecture only ([66477bd](https://github.com/PlatformNetwork/platform/commit/66477bd62eb3163f27487bcd944bfa105647bf58)) +* remove P2P networking, centralized architecture only ([9ef71a6](https://github.com/PlatformNetwork/platform/commit/9ef71a66255882bac864455ac792b6d54c460425)) +* Remove redundant weights_v2.rs, use bittensor_rs::Subtensor directly ([5317b8d](https://github.com/PlatformNetwork/platform/commit/5317b8d518bc76b4fd8c3bec10f2b85e218d448d)) +* remove term-challenge specific code from validator-node ([a4713e9](https://github.com/PlatformNetwork/platform/commit/a4713e96986ad36b278657fb0855bd157f5439d9)) +* Replace WeightSubmitter with bittensor_rs::Subtensor ([1b55113](https://github.com/PlatformNetwork/platform/commit/1b5511344bf20d04424081a934b42ab5c5e8541b)) +* simplify WeightAssignment to hotkey + weight only ([56ccd21](https://github.com/PlatformNetwork/platform/commit/56ccd2118e107a5fe15796063116bb3dcfdf6d01)) ### Documentation -* remove remaining_steps/remaining_secs from documentation and examples ([40197be](https://github.com/PlatformNetwork/term-challenge/commit/40197be9f982adcbc6f50ce53db0fe69abe3cd44)) -* update README with missing features and architecture ([1ecd09f](https://github.com/PlatformNetwork/term-challenge/commit/1ecd09fcc27efaca28aefe13c203ef3e8a3b2152)) +* add badges and repobeats analytics to README ([5c2a3bb](https://github.com/PlatformNetwork/platform/commit/5c2a3bb0b6f51d925ebce2803ea0ede845341385)) +* add banner image to README ([2a89445](https://github.com/PlatformNetwork/platform/commit/2a89445086881d644737473ad4a22fc344f60972)) +* add hardware requirements section (4 vCPU, 16GB RAM minimum) ([e1ffadb](https://github.com/PlatformNetwork/platform/commit/e1ffadbc7cef3f868029c36bd5915cc5a2cab53a)) +* add network requirements (port 9000/tcp) ([a0fee78](https://github.com/PlatformNetwork/platform/commit/a0fee78d65581be7a4439af717694e6d098735f8)) +* comprehensive README with incentive mechanism and validator guide ([b85812f](https://github.com/PlatformNetwork/platform/commit/b85812f57ed668ac872cc26e9f7f1170bdd08010)) +* improve README formatting and clarify aggregation method ([31ab5bc](https://github.com/PlatformNetwork/platform/commit/31ab5bc7a0a7cb5ee81a64cceac0b0341006c8d4)) +* improve README formatting and clarify aggregation method ([ccc90f0](https://github.com/PlatformNetwork/platform/commit/ccc90f0ac319bc306522d05ae9f3f5c6a613b6fc)) +* move description before badges ([4c6c3fc](https://github.com/PlatformNetwork/platform/commit/4c6c3fc958c83af903d270c032327512ab24ea01)) +* move repobeats after badges, update rust to 1.90+ ([491f255](https://github.com/PlatformNetwork/platform/commit/491f2550043817853e0425a12a916863bc5ab378)) +* remove Validator Utility Maximization section from README ([fef431a](https://github.com/PlatformNetwork/platform/commit/fef431a1f1a3382b9eef9253c66896d7169bf31b)) +* simplify README - remove weight capping, commit-reveal, and agent terminology ([8897501](https://github.com/PlatformNetwork/platform/commit/88975016a62b03c4636003d40e6b09c703a9eeee)) +* update storage requirements (250GB min, 500GB recommended) ([43b6805](https://github.com/PlatformNetwork/platform/commit/43b6805b6ec74afce2ae99517193874f6d335c14)) +* **validator:** simplify guide - no GPU, no third-party APIs needed ([79a1e97](https://github.com/PlatformNetwork/platform/commit/79a1e97b2933db10fa1deaa24091a9cb1a1f1481)) ### Miscellaneous -* restart CI pipeline ([73a1a6e](https://github.com/PlatformNetwork/term-challenge/commit/73a1a6e1e00c70ed8ff7b3fb838797fdb865d8ab)) -* update platform dependency with auth fix ([7c70308](https://github.com/PlatformNetwork/term-challenge/commit/7c70308990074a9f412e516530dbdd7a4912423c)) -* update platform dependency with debug logging ([3750c3b](https://github.com/PlatformNetwork/term-challenge/commit/3750c3bc0f157e78372b9d7362511f3f0626aea1)) -* update secure-container-runtime dependency to latest build image support ([f020b6d](https://github.com/PlatformNetwork/term-challenge/commit/f020b6d443834b5904489c3ffa4b34045a7c9d0b)) -* update secure-container-runtime to latest with JWT fix ([8e8de66](https://github.com/PlatformNetwork/term-challenge/commit/8e8de663a2fe0f2e008873a01f364290f540b03b)) - - -### Tests - -* add SDK compilation integration tests ([18cbf2d](https://github.com/PlatformNetwork/term-challenge/commit/18cbf2d6018cd5fa38c50ced3c55b5702762c5b5)) -* add serialization test to verify broker request uses lowercase type ([8181359](https://github.com/PlatformNetwork/term-challenge/commit/8181359d66395c62ebf010077b97e1ab29cb58cc)) - -## 0.1.0 (2026-01-04) - - -### โš  BREAKING CHANGES +* add git hooks for format and CI checks ([b42dfc8](https://github.com/PlatformNetwork/platform/commit/b42dfc809efdd21fc04017210bc2a42c574f7557)) +* add MIT license file ([7fd5106](https://github.com/PlatformNetwork/platform/commit/7fd510649557a35dc63fb81aac9ece92f306c02d)) +* update bittensor-rs to 453ce56 (proper SCALE decoding) ([56f99f9](https://github.com/PlatformNetwork/platform/commit/56f99f96fc1f6c25617ab329b3ee97b822fcf5b4)) +* update Rust to 1.92 (latest stable) ([ca22249](https://github.com/PlatformNetwork/platform/commit/ca222496b24be834c94ecb25fd083840fa2e5eea)) -* Evaluation now uses separate containers: - - Agent container: base image (ghcr.io/platformnetwork/term-challenge) - with term_sdk installed, runs agent HTTP server - - Task container: task-specific image (e.g., alexgshaw/fix-git) - executes commands and runs tests -* **security:** Agents now run inside Docker containers, not on the host. -### Features - -* add 'term review' CLI command for local LLM agent validation ([cfdc7ed](https://github.com/PlatformNetwork/term-challenge/commit/cfdc7ed672d448c0f687293f6394a489523045ec)) -* Add /.well-known/routes endpoint for dynamic route discovery ([f4f8048](https://github.com/PlatformNetwork/term-challenge/commit/f4f80480cb1fadba1d376c4fbdbce16fd53390a6)) -* add agent evaluation queue system ([07ea520](https://github.com/PlatformNetwork/term-challenge/commit/07ea5201f0efdadf21c9af1b02f03e59a2390c00)) -* add always-on server mode with /get_weights endpoint ([bb29283](https://github.com/PlatformNetwork/term-challenge/commit/bb2928310e871b6b3d5f731c4b64abc4d090a021)) -* add beautiful TUI output with spinners and progress ([a88d5d4](https://github.com/PlatformNetwork/term-challenge/commit/a88d5d4aa3d119daa2d8ba12bb3a6bd8d074ec0e)) -* add blockchain-based agent evaluation system ([7fe204f](https://github.com/PlatformNetwork/term-challenge/commit/7fe204f5e44f57f915efc231ff6117ad07ea5c4e)) -* Add code visibility system ([4eb14e8](https://github.com/PlatformNetwork/term-challenge/commit/4eb14e8f7f93b1845898e75883be25bf8faa1a00)) -* add container backend abstraction with secure broker default ([a98e312](https://github.com/PlatformNetwork/term-challenge/commit/a98e3125748dd8308ff174a3a4546ef031bcd0d0)) -* add container cleanup for evaluation containers ([e0e90c9](https://github.com/PlatformNetwork/term-challenge/commit/e0e90c920c972790a44ee661af269243fe6e5b2e)) -* add conversation history to agent requests ([6f6b094](https://github.com/PlatformNetwork/term-challenge/commit/6f6b09457a9b4d5f04702d8d9b6ef3bdfd7e258c)) -* add detailed error logging for database operations ([7eb88ba](https://github.com/PlatformNetwork/term-challenge/commit/7eb88baef7a559341150ff10b72c72ea649e30b1)) -* add disk persistence for kv_store (evaluation state recovery) ([05a4eca](https://github.com/PlatformNetwork/term-challenge/commit/05a4ecac5205a44459f75f127ba9c9bc920fee1b)) -* add function calling examples for all SDKs (Python, TypeScript, Rust) ([3b9f7ff](https://github.com/PlatformNetwork/term-challenge/commit/3b9f7ff0b14572a4df4b1adea9f42725a66a8796)) -* add grok agent example and fix registry URL ([6979849](https://github.com/PlatformNetwork/term-challenge/commit/6979849df5658f3aa94cf997eeb1fdc81fc76e88)) -* add in-container agent execution with platform LLM bridge ([d6c4f0a](https://github.com/PlatformNetwork/term-challenge/commit/d6c4f0af7eeb22543ea776ab9acc4656fcec8c28)) -* add LLM proxy endpoint with validator auth ([0b3f647](https://github.com/PlatformNetwork/term-challenge/commit/0b3f647969d399e8edcbcdf1cee3b1883b7c0376)) -* add LLM-based agent code review system with sudo management ([8e9c832](https://github.com/PlatformNetwork/term-challenge/commit/8e9c832f460feba3036628e92dae77ad106dd599)) -* add logging system to all SDKs ([eda4209](https://github.com/PlatformNetwork/term-challenge/commit/eda4209bde3d0372a4ea4bdf8248006617184bc6)) -* Add manual review system for LLM-rejected agents ([fe2d517](https://github.com/PlatformNetwork/term-challenge/commit/fe2d517fb200a29eca60deb2874dd2e530e29c46)) -* add P2P bridge for platform validator integration ([64df472](https://github.com/PlatformNetwork/term-challenge/commit/64df472da258b219c4dcf831e18018ff2f6ebefb)) -* add P2P chain storage for agent submissions and evaluations ([4522d7d](https://github.com/PlatformNetwork/term-challenge/commit/4522d7d635efe63ac2857ff029147e9101d91860)) -* add ProposalManager for P2P agent proposal flow ([fe47817](https://github.com/PlatformNetwork/term-challenge/commit/fe4781764049d02f88a3c5f73c6c8b5ecc9d8b5d)) -* add public API endpoints for pending submissions and validator assignments ([89cb608](https://github.com/PlatformNetwork/term-challenge/commit/89cb608953a0abfeee159664b9247c2e5e1ae37a)) -* add retry loop for platform-server connection (30s interval, 5 attempts) ([fb23d26](https://github.com/PlatformNetwork/term-challenge/commit/fb23d267f9c55096cf64ea7577b580288e3af7dc)) -* Add Sentry error monitoring (enabled by default) ([5ed44bc](https://github.com/PlatformNetwork/term-challenge/commit/5ed44bc4668e63c16323588cf0959dc50f6d9518)) -* Add subnet owner control system with RPC and CLI ([bea654b](https://github.com/PlatformNetwork/term-challenge/commit/bea654b6f01950536a78b380be500a361bc06ace)) -* add term-sudo CLI + remove leaked API key ([eca7fd7](https://github.com/PlatformNetwork/term-challenge/commit/eca7fd713462a91f7c16179d11ea7500a1437c0c)) -* Add terminal harness for agent evaluation ([aece350](https://github.com/PlatformNetwork/term-challenge/commit/aece350585f3274c9fd08695efa52ff31b946263)) -* add validator worker for evaluation recovery and polling ([6c9af2d](https://github.com/PlatformNetwork/term-challenge/commit/6c9af2da0712daabdb5f410e53c93d9e6f59719e)) -* add verbose logging for LLM requests/responses and command execution ([956b7ad](https://github.com/PlatformNetwork/term-challenge/commit/956b7ad9ebc8ed932a222b08a15e15450f1060aa)) -* add WebSocket broker backend for container management ([1742947](https://github.com/PlatformNetwork/term-challenge/commit/17429470ba331923b7cde67f9fa418a0f5616f40)) -* async task logging system with real-time tracking and recovery ([ca3a09b](https://github.com/PlatformNetwork/term-challenge/commit/ca3a09bc61babb09c53deefd91b75a1302a4100c)) -* auto-evaluation after agent submission ([ba1f911](https://github.com/PlatformNetwork/term-challenge/commit/ba1f9110a75e78a6f8075ea37655a392d42dc01a)) -* broadcast new_submission event to validators via WebSocket ([e05646f](https://github.com/PlatformNetwork/term-challenge/commit/e05646f9fac414ef8c42c4ceb54a64870ad046ac)) -* **cli:** add agent name prompt in submit wizard ([937e3f1](https://github.com/PlatformNetwork/term-challenge/commit/937e3f1fddc2da9b444502c5afb3048f2a8c1159)) -* **cli:** add centralized TermClient for API calls ([0ef1dcd](https://github.com/PlatformNetwork/term-challenge/commit/0ef1dcda5d13c63523933f2b20a6d2055cca8dc4)) -* **cli:** default platform URL to https://chain.platform.network ([14211c6](https://github.com/PlatformNetwork/term-challenge/commit/14211c689f1651f141bf8720f08955f7af4fa8ab)) -* **cli:** merge bench agent/benchmark into single command with required --api-key ([fda4fa5](https://github.com/PlatformNetwork/term-challenge/commit/fda4fa5fb1bd0d7f312545810bfc522a476f3afb)) -* **cli:** require external agent for benchmark command ([5996645](https://github.com/PlatformNetwork/term-challenge/commit/59966453c60e33d5050899120ccd06eb2ea047f7)) -* complete SDK rewrite - Python, TypeScript, Rust ([bcdad0f](https://github.com/PlatformNetwork/term-challenge/commit/bcdad0f1981f414bec4e4f171eed8c8026ffae00)) -* concurrent task execution (30 tasks, 4 concurrent per agent) ([d14cc55](https://github.com/PlatformNetwork/term-challenge/commit/d14cc5510fe413f170f9d72b0f4dcfca1a39412c)) -* concurrent task execution with Ctrl+C cleanup ([4e17cf5](https://github.com/PlatformNetwork/term-challenge/commit/4e17cf570fa9b4b9819533089ccd670aa2dcc7fb)) -* **config:** change LLM model config to blacklist approach ([eca6e9f](https://github.com/PlatformNetwork/term-challenge/commit/eca6e9f49ffebbc2de2b6182d58627d2d6941449)) -* Docker-isolated compilation + binary_ready notification to validators ([ca5ecb7](https://github.com/PlatformNetwork/term-challenge/commit/ca5ecb727fa8f5262329b648c542a07ed4aa796c)) -* dynamic multi-model LLM support for all SDKs ([24b651a](https://github.com/PlatformNetwork/term-challenge/commit/24b651ac69459e7eca940cc84a270668136f90f3)) -* enhanced SDKs with function calling, text responses, flexible LLM ([249e659](https://github.com/PlatformNetwork/term-challenge/commit/249e659493e1590a27e6da6868a6547e27b6c02f)) -* **eval:** auto-download tasks from terminal-bench@2.0 registry ([37abfa3](https://github.com/PlatformNetwork/term-challenge/commit/37abfa35f6370dc39b29a65b944835cfede4f36e)) -* fetch whitelisted validators from platform-server ([e65d81e](https://github.com/PlatformNetwork/term-challenge/commit/e65d81e20704b678aff67600436ebc4190445c8c)) -* fix evaluation system and add real-time progress tracking ([30544ef](https://github.com/PlatformNetwork/term-challenge/commit/30544ef568ed648a95cdc5fc437ad286651f793f)) -* fully integrate ProposalManager into submission flow ([0576970](https://github.com/PlatformNetwork/term-challenge/commit/0576970ef3ad05a1a676bbdbe5d986bd506e6d5f)) -* get validator count from platform-server for distributed evaluation ([5204f53](https://github.com/PlatformNetwork/term-challenge/commit/5204f53a221b4b5049d76372c30bea6a2a61ac7c)) -* implement distributed evaluation system - ALL validators must evaluate ([1a7684c](https://github.com/PlatformNetwork/term-challenge/commit/1a7684c123fa309c339fcab5a18cb04824e7b0c6)) -* implement full evaluation flow with LLM review ([fdb56cf](https://github.com/PlatformNetwork/term-challenge/commit/fdb56cf1ebc9aca24f83325451a1a996f981bf66)) -* implement P2P progress sharing system ([f30978d](https://github.com/PlatformNetwork/term-challenge/commit/f30978dce1777f4c262c6ddd1643f36ab8e10b63)) -* implement real Docker evaluation with TaskRegistry ([922df5c](https://github.com/PlatformNetwork/term-challenge/commit/922df5c364be187d210f326fc652779170927e97)) -* improve benchmark output and increase default max_steps ([931ef3f](https://github.com/PlatformNetwork/term-challenge/commit/931ef3f100336909253aeb659dc5ba7a25cc588c)) -* increase default timeout to 300s and make configurable ([3bee189](https://github.com/PlatformNetwork/term-challenge/commit/3bee1899aff3e0719665f5a376f8cf64c2b87975)) -* migrate all CLI commands to use bridge routes ([5299263](https://github.com/PlatformNetwork/term-challenge/commit/529926399f33b2f918d88711a9e33ac726fea88e)) -* migrate persistence from JSON files to sled embedded database ([fda293d](https://github.com/PlatformNetwork/term-challenge/commit/fda293d16e12eb571eb6b5a4e376688526c0997e)) -* Migrate submissions API from platform-server to term-challenge ([f17e10c](https://github.com/PlatformNetwork/term-challenge/commit/f17e10c8642e1df241cb1cf51520029fb8674704)) -* multi-validator consensus and dev mode improvements ([2b741a6](https://github.com/PlatformNetwork/term-challenge/commit/2b741a6e06a7bd4a27572fee1ac4d08515451f9e)) -* non-interactive command execution via script ([b3948aa](https://github.com/PlatformNetwork/term-challenge/commit/b3948aa1323447c1f0f61119c3eeaf9b59c71aac)) -* **p2p:** enable secure submission with P2P commit-reveal protocol ([2afa9d1](https://github.com/PlatformNetwork/term-challenge/commit/2afa9d1b2b26d0d1c9b05406d4b66dbd6e9c3b5b)) -* production-ready agent naming, consensus, and scoring ([9e5eed6](https://github.com/PlatformNetwork/term-challenge/commit/9e5eed64f80aa2227180bababe827695c3433855)) -* production-ready task execution with real Terminal-Bench ([b4efd99](https://github.com/PlatformNetwork/term-challenge/commit/b4efd99016f93cb4faa65f619678cdaa48de8177)) -* PyInstaller binary compilation for agents ([c58a29b](https://github.com/PlatformNetwork/term-challenge/commit/c58a29bacead726b306ed8b3a66507ca8afd2366)) -* Python-only agent with HTTP server for persistence ([c7d387e](https://github.com/PlatformNetwork/term-challenge/commit/c7d387e5b8b2100f0eda172f80c43d3f5bdbbccd)) -* **rpc:** add sudo endpoints to manage model blacklist dynamically ([2c6d13d](https://github.com/PlatformNetwork/term-challenge/commit/2c6d13d67698f7f14d2e351bf6badde03e417d53)) -* **security:** execute agents inside non-privileged Docker containers ([87edb5d](https://github.com/PlatformNetwork/term-challenge/commit/87edb5d89243484971ea3a5eb220c47f27577c5a)) -* **security:** implement platform authentication for P2P endpoints ([13116de](https://github.com/PlatformNetwork/term-challenge/commit/13116debfda4965a2a5265e43c8a4c733b8ba731)) -* set validation_enabled=false by default ([aa0ed07](https://github.com/PlatformNetwork/term-challenge/commit/aa0ed07550b33a0ae07319b25721c739249f973f)) -* show pending agents in status command ([b873507](https://github.com/PlatformNetwork/term-challenge/commit/b873507a537bfaa7931ced08621910942b3b22f8)) -* simplify scoring to pass/fail only ([37cd137](https://github.com/PlatformNetwork/term-challenge/commit/37cd137b07dd9240b85941b2583f6f8c131355bb)) -* streaming support + OpenRouter/Chutes only ([3d31aeb](https://github.com/PlatformNetwork/term-challenge/commit/3d31aeb126a781f9b584654bf274821d9bfd8914)) -* structured JSON errors for LLM SDK ([d269fda](https://github.com/PlatformNetwork/term-challenge/commit/d269fda7cf76625493a8cd434813581f889f3dad)) -* sudo endpoints + LLM proxy via validator ([ba8a799](https://github.com/PlatformNetwork/term-challenge/commit/ba8a799d7907db1bb297bd88bb1d40287c9cd680)) -* task-level progress tracking per validator ([bc51be6](https://github.com/PlatformNetwork/term-challenge/commit/bc51be6fc684d32898ba5b911115cffa12495c6f)) -* update CLI to use bridge API for submissions ([f47c444](https://github.com/PlatformNetwork/term-challenge/commit/f47c444f8d7f9f07570dea43e8974144d91c8178)) -* update simple_agent.py to use SDK, add hello-world sample task ([b3650bf](https://github.com/PlatformNetwork/term-challenge/commit/b3650bf8933328de068b7b4d4b36e173eef04a3c)) -* validate miner_hotkey is SS58 format in /evaluate endpoint ([f56c6d6](https://github.com/PlatformNetwork/term-challenge/commit/f56c6d6d346886772cb4b3b0ca5ed6b694e2088f)) -* validator worker loads real tasks from terminal-bench@2.0 ([aeb1cdf](https://github.com/PlatformNetwork/term-challenge/commit/aeb1cdfac2c60330b14ba842aa68158dc28a511c)) - - -### Bug Fixes - -* add cache directory mapping for Docker-in-Docker ([c39d5b4](https://github.com/PlatformNetwork/term-challenge/commit/c39d5b409ac87dac1f0d2d535e4ca34912527d82)) -* add Docker-in-Docker path mapping for environment.rs ([e899e94](https://github.com/PlatformNetwork/term-challenge/commit/e899e9424f0c826ed1346d36fb2cb665c8039de3)) -* add migrations to Docker build context for include_str! ([f9c5413](https://github.com/PlatformNetwork/term-challenge/commit/f9c54133877bd1fb6d19eab24a7e27be8d4e8ea0)) -* add missing COPY bin and .dockerignore for Docker build ([87afef6](https://github.com/PlatformNetwork/term-challenge/commit/87afef63c0ba53da2028ef1fd2d47022f99ce547)) -* add multi-stage build for CI ([0f7acf2](https://github.com/PlatformNetwork/term-challenge/commit/0f7acf24566aa137582579e74b44ba77931d3377)) -* add retry and better error logging for agent communication ([9cc1064](https://github.com/PlatformNetwork/term-challenge/commit/9cc10644526cf35f16a8e653ab8a4bdf456ae3f1)) -* add scrolling support to wizard file selector ([08c5812](https://github.com/PlatformNetwork/term-challenge/commit/08c58129949c77f183c0457af6a769f914948c00)) -* add target dirs to gitignore, remove build artifacts ([81a2763](https://github.com/PlatformNetwork/term-challenge/commit/81a276325edde94b5b0589c6beac97d5f71f873f)) -* add term_sdk to allowed third-party modules whitelist ([57af0ec](https://github.com/PlatformNetwork/term-challenge/commit/57af0ecac0ae8eb94268cff14bdcfb50d8edb9c9)) -* always log agent stderr output ([9cfd726](https://github.com/PlatformNetwork/term-challenge/commit/9cfd7267f891e6b59d2b1441e7f52f8b145b40a5)) -* Always pull latest image from GHCR registry ([5812c96](https://github.com/PlatformNetwork/term-challenge/commit/5812c96bda156f0b072ec55fc20d59dc51491308)) -* **ci:** move -E filter before -- in cargo llvm-cov nextest ([ab54402](https://github.com/PlatformNetwork/term-challenge/commit/ab54402fbba80bf3a4d56063150a5a38c194650f)) -* cleaner command execution without temp script ([da7651d](https://github.com/PlatformNetwork/term-challenge/commit/da7651dc13bb44257bb765d97bd426f629d65463)) -* cleanup bench containers by name prefix instead of tracking ([9a2c9d0](https://github.com/PlatformNetwork/term-challenge/commit/9a2c9d08c0351a3897b2d7d9b7f276f619ee1350)) -* **clippy:** resolve all clippy warnings for CI ([f273d3a](https://github.com/PlatformNetwork/term-challenge/commit/f273d3a55c75b37384ec6052e8314c3a2fb7b269)) -* **cli:** read best_score from API leaderboard response ([0110c25](https://github.com/PlatformNetwork/term-challenge/commit/0110c25c2db8871ffc634dbdbe91fa2bff46a348)) -* **cli:** use correct challenge endpoint paths ([589914f](https://github.com/PlatformNetwork/term-challenge/commit/589914f8fcd131a292dfc49e4aa189782e01e8af)) -* correct model ID to z-ai/glm-4.5 for OpenRouter ([e976f61](https://github.com/PlatformNetwork/term-challenge/commit/e976f61f2fce1ef5d8b58cae1f9b95104e49dbae)) -* default to openrouter if llm_provider is empty ([5f78b3c](https://github.com/PlatformNetwork/term-challenge/commit/5f78b3cf28e44676728072521ed4f826f2dcfd18)) -* disable /evaluate in server mode, use /validators endpoint ([a4357f1](https://github.com/PlatformNetwork/term-challenge/commit/a4357f1a71b2b0e7351fdb7fdf29ab395334a7ee)) -* force kill on Ctrl+C - exit immediately without waiting ([d01958d](https://github.com/PlatformNetwork/term-challenge/commit/d01958d10246b91c7727aa6591387778727e4467)) -* improve Docker error logging with detailed context ([a7334db](https://github.com/PlatformNetwork/term-challenge/commit/a7334dba202bc9bc7063171a9261bdaed8be7581)) -* improve error logging for agent response parsing ([69754c6](https://github.com/PlatformNetwork/term-challenge/commit/69754c605d346ccd1f280117b73f70c98e6a95c5)) -* include Cargo.lock for Docker builds ([640d3ab](https://github.com/PlatformNetwork/term-challenge/commit/640d3ab69d4be972cf193e06a12f15bd4b5c3e38)) -* increase Docker health check start-period to 30s ([341bfb9](https://github.com/PlatformNetwork/term-challenge/commit/341bfb997da57dd1274f732b309645f5e5931f36)) -* infinite retry loop for platform-server, no fallback ([b520bee](https://github.com/PlatformNetwork/term-challenge/commit/b520bee2685df73ba006f8dc28e5ed10139f143c)) -* limit Docker hostname to 64 characters ([5764eba](https://github.com/PlatformNetwork/term-challenge/commit/5764eba48f826053f82a6436ad1b8b0c4c78f69b)) -* LLM rejection flags agent for manual review instead of blocking ([516cebe](https://github.com/PlatformNetwork/term-challenge/commit/516cebe37aeb99c0c820d906915bef1bff4d74bf)) -* **llm_review:** clarify that Response.cmd() is ALLOWED ([1668c6d](https://github.com/PlatformNetwork/term-challenge/commit/1668c6d31c324d7e7827b031d625d25e550c7efc)) -* make queue test tolerant of Docker permission errors in CI ([2d0210a](https://github.com/PlatformNetwork/term-challenge/commit/2d0210a6d48ec13a65848257863de08904fdf997)) -* make validator worker optional, support VALIDATOR_SECRET_KEY ([59c3288](https://github.com/PlatformNetwork/term-challenge/commit/59c32888e4f306fed9ec1713873e3e7aede26a2e)) -* P2P validators sync and consensus logic ([ec9552e](https://github.com/PlatformNetwork/term-challenge/commit/ec9552ea466b6dae631ea210e0a7b8924ee0b199)) -* parse docker_image from task.toml [environment] section ([0ece103](https://github.com/PlatformNetwork/term-challenge/commit/0ece103e34255631b39c0bb211df97d8177bfead)) -* pass command output to agent for next step ([aceb7a5](https://github.com/PlatformNetwork/term-challenge/commit/aceb7a5645e64bb60b38cc64d970d3f1e00edcc1)) -* reduce docker pull log spam ([1286d60](https://github.com/PlatformNetwork/term-challenge/commit/1286d60e2c6413f0119e2f1d4b59174ce407708e)) -* remove auth requirement from /p2p/outbox endpoint ([395dc5e](https://github.com/PlatformNetwork/term-challenge/commit/395dc5e06859690b191ec6f769e1c9c7ef550037)) -* remove cost tracking - only score matters ([db73687](https://github.com/PlatformNetwork/term-challenge/commit/db7368775be18f6d87da26aa3545f0d04ddd23af)) -* remove difficulty weighting - all tasks scored equally ([221bb36](https://github.com/PlatformNetwork/term-challenge/commit/221bb36a24eb8ab23a01b7eed369664b7cdf63a2)) -* remove unnecessary drop(task_registry.read()) ([4ad9f7a](https://github.com/PlatformNetwork/term-challenge/commit/4ad9f7a7dab8d3c4f75094ed138d9f9c9909c8b0)) -* remove unused mut in execute_step ([8048cea](https://github.com/PlatformNetwork/term-challenge/commit/8048cea1a1e66a17f3a2f7dd80f4e52b9fddd7f0)) -* replace placeholders with real implementations ([cbb9393](https://github.com/PlatformNetwork/term-challenge/commit/cbb9393e3acf9ffd264ef9f9594a96ebeda5f47c)) -* resolve clippy errors and string indexing issues ([753f65a](https://github.com/PlatformNetwork/term-challenge/commit/753f65ababfb7e4173c3803ec689e32840f3d7e5)) -* resolve clippy warnings and update tests for simplified distribution flow ([6b85ab3](https://github.com/PlatformNetwork/term-challenge/commit/6b85ab3377f42c7d4c143b77ee366ca9091bd31c)) -* resolve compilation errors and add pre-push hooks ([3bd7f92](https://github.com/PlatformNetwork/term-challenge/commit/3bd7f923516c0c52927eef555fa3e64137f8b25b)) -* SDK exports and comprehensive tests ([1b3661e](https://github.com/PlatformNetwork/term-challenge/commit/1b3661e91577a2a1cfbeb6c508b5477e3d789400)) -* SDK reads stdin line-by-line for persistent agent process ([ada6956](https://github.com/PlatformNetwork/term-challenge/commit/ada6956a7d64b4b1a4af1f14cb361b5f05bc9192)) -* **sdk:** add safe output access methods to prevent IndexError ([e6201cc](https://github.com/PlatformNetwork/term-challenge/commit/e6201cc1f3fd88a6a38e1f4bcfbb7c27b6714347)) -* **sdk:** align Rust Request API with Python/TypeScript ([29f3613](https://github.com/PlatformNetwork/term-challenge/commit/29f3613a2c631e05f59aa979f3582a1797ceee34)) -* **sdk:** handle None tool_calls from Chutes models ([d018d20](https://github.com/PlatformNetwork/term-challenge/commit/d018d20f9b040433758f4929461c22a908679aa3)) -* send BROADCAST_SECRET header for event broadcasts ([05d526c](https://github.com/PlatformNetwork/term-challenge/commit/05d526c7fdb98cd18d51300cdcc73498dd9198fa)) -* simplify TUI to single spinner during evaluation ([b86812e](https://github.com/PlatformNetwork/term-challenge/commit/b86812e7d257e098a16baec23aa141a71367c012)) -* support new SDK response format in bench harness ([bb8a1fd](https://github.com/PlatformNetwork/term-challenge/commit/bb8a1fd5c073e6762d552d5bd437da204bca0c89)) -* term-sudo uses bridge routes via chain.platform.network ([de42398](https://github.com/PlatformNetwork/term-challenge/commit/de423982bdb8f0f92524c4984c9b7c5af49b4aec)) -* update CLI to use correct signature format for agent submissions ([c31d816](https://github.com/PlatformNetwork/term-challenge/commit/c31d816a61eaa9aeeb8d7b7ea40bad7260ec381d)) -* update coverage badge generation to use peaceiris/actions-gh-pages ([41fd2d2](https://github.com/PlatformNetwork/term-challenge/commit/41fd2d25a43a0b15c76c9f920a4956547b4aeee3)) -* update license to MIT in Cargo.toml ([0185619](https://github.com/PlatformNetwork/term-challenge/commit/018561978c33ec8935c9d090230f6addda6fd8a2)) -* update Python examples to current SDK API ([54b8c29](https://github.com/PlatformNetwork/term-challenge/commit/54b8c298e3e6857233a07189f27e5e3461a4b56b)) -* use absolute paths for Docker bind mounts ([fc55b1b](https://github.com/PlatformNetwork/term-challenge/commit/fc55b1b75139e774a05ebc22dafc82f49df46b68)) -* use agent_binary column name, better error logging ([273f0ef](https://github.com/PlatformNetwork/term-challenge/commit/273f0ef07824d6d5645114b203a8aa37f6fa81ab)) -* use env var for API key in tests instead of hardcoded value ([703e8be](https://github.com/PlatformNetwork/term-challenge/commit/703e8bec62f30a2638152db4c31d097bf26b4dfb)) -* use full git clone when specific commit is needed ([97f9aa7](https://github.com/PlatformNetwork/term-challenge/commit/97f9aa774344393cb82e33e2b2836e641277f345)) -* use full OpenRouter model IDs in examples ([d7f5b07](https://github.com/PlatformNetwork/term-challenge/commit/d7f5b0791ebc0071ba6db35b3a3ad9445509dc9f)) -* use GHCR image for evaluator instead of term-challenge/base ([54ff7f5](https://github.com/PlatformNetwork/term-challenge/commit/54ff7f5a2236289a2254f1dc36ce30e104ab7e3a)) -* Use ghcr.io for AGENT_BASE_IMAGE in external_agent.rs ([a355724](https://github.com/PlatformNetwork/term-challenge/commit/a3557248ae846c7e44b9ae8f58d9f73613c42a39)) -* use latest Rust for edition2024 support ([062704c](https://github.com/PlatformNetwork/term-challenge/commit/062704c5fca7788456f2520ee29d3b2ea187ee94)) -* use Rust 1.83 for Cargo.lock v4 support ([241a383](https://github.com/PlatformNetwork/term-challenge/commit/241a38390f73ef0ccfa88065d2a0cc5b14ffa7a5)) -* use Rust 1.91.1-slim-bookworm for Docker build ([228e73f](https://github.com/PlatformNetwork/term-challenge/commit/228e73f556473d469101beeee9ee20e1df016fe1)) - - -### Performance Improvements - -* add Rust dependency caching to Dockerfiles ([5dc31b8](https://github.com/PlatformNetwork/term-challenge/commit/5dc31b883ec7b3b00aa4241953f9ffeb52f54484)) -* **ci:** optimize caching for Rust builds and Docker images ([ee383cd](https://github.com/PlatformNetwork/term-challenge/commit/ee383cd12a9a859899ca3a5dde5024585d55bf70)) -* parallel dataset download (8 concurrent tasks) ([475b7c9](https://github.com/PlatformNetwork/term-challenge/commit/475b7c9adadc52467deac5f5aafec8dc6325b74a)) - - -### Code Refactoring +### CI/CD -* use two-container architecture for evaluation ([d8ab393](https://github.com/PlatformNetwork/term-challenge/commit/d8ab3935b8f1fdc15f21168da4ff6f647bd2f974)) +* add automatic versioning with release-please ([80070f7](https://github.com/PlatformNetwork/platform/commit/80070f76d2a86eaee9169a3a7a4f3c5f91da66af)) +* add code coverage badge with cargo-llvm-cov ([7b22a73](https://github.com/PlatformNetwork/platform/commit/7b22a732383138574c41ee9e81ecd28d88e1e852)) +* docker push only after build/clippy/test pass ([616dfac](https://github.com/PlatformNetwork/platform/commit/616dfaca0bc4f733dbb80f7bff1836ffed536a20)) +* optimize CI with nextest, better caching, and parallel jobs ([6155af8](https://github.com/PlatformNetwork/platform/commit/6155af8845e4c0ccb6c945e8aff53909c591f8f5)) +* optimize with fully parallel jobs ([360c14c](https://github.com/PlatformNetwork/platform/commit/360c14c8496a419c1f0b616bbe982c9b2d0fa086)) +* optimize workflow with disk cleanup and rust-cache ([4bd7164](https://github.com/PlatformNetwork/platform/commit/4bd71647bd096e23eccc7bc99799e773894d7635)) +* restore parallel jobs with proper cache dependencies ([11d25b0](https://github.com/PlatformNetwork/platform/commit/11d25b0b2b2c9e4332fc17ef3151a4dc0a2c8c89)) +* round coverage to whole number ([35bc17e](https://github.com/PlatformNetwork/platform/commit/35bc17e2d31892da3fd9308a63fafc14ae0e16b6)) +* use platform-runner label ([7080d56](https://github.com/PlatformNetwork/platform/commit/7080d56e6550c5de4c1c32a643f429e513b31699)) +* use self-hosted runners ([63c1e86](https://github.com/PlatformNetwork/platform/commit/63c1e86d23c1596bff751801a34a8fad3dd9d71f)) diff --git a/Cargo.lock b/Cargo.lock index 71bc51972..13cc51c85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,99 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "addr2line" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97" +dependencies = [ + "gimli 0.27.3", +] + +[[package]] +name = "addr2line" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" +dependencies = [ + "gimli 0.32.3", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + +[[package]] +name = "ahash" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom 0.2.16", + "once_cell", + "version_check", +] + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.4" @@ -26,6 +119,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anstream" version = "0.6.21" @@ -78,1716 +180,9177 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.101" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea" - -[[package]] -name = "atomic-waker" -version = "1.1.2" +version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" [[package]] -name = "autocfg" -version = "1.5.0" +name = "ar_archive_writer" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +checksum = "f0c269894b6fe5e9d7ada0cf69b5bf847ff35bc25fc271f08e1d080fce80339a" +dependencies = [ + "object 0.32.2", +] [[package]] -name = "base64" -version = "0.22.1" +name = "arbitrary" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" [[package]] -name = "bincode" -version = "1.3.3" +name = "ark-bls12-377" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +checksum = "fb00293ba84f51ce3bd026bd0de55899c4e68f0a39a5728cebae3a73ffdc0a4f" dependencies = [ - "serde", + "ark-ec 0.4.2", + "ark-ff 0.4.2", + "ark-std 0.4.0", ] [[package]] -name = "bitflags" -version = "2.11.0" +name = "ark-bls12-381" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" +checksum = "c775f0d12169cba7aae4caeb547bb6a50781c7449a8aa53793827c9ec4abf488" +dependencies = [ + "ark-ec 0.4.2", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", +] [[package]] -name = "bumpalo" -version = "3.19.1" +name = "ark-bls12-381" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" +checksum = "3df4dcc01ff89867cd86b0da835f23c3f02738353aaee7dde7495af71363b8d5" +dependencies = [ + "ark-ec 0.5.0", + "ark-ff 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", +] [[package]] -name = "bytes" -version = "1.11.1" +name = "ark-ec" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" +checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba" +dependencies = [ + "ark-ff 0.4.2", + "ark-poly 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "derivative", + "hashbrown 0.13.2", + "itertools 0.10.5", + "num-traits", + "zeroize", +] [[package]] -name = "cassowary" -version = "0.3.0" +name = "ark-ec" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" +checksum = "43d68f2d516162846c1238e755a7c4d131b892b70cc70c471a8e3ca3ed818fce" +dependencies = [ + "ahash 0.8.12", + "ark-ff 0.5.0", + "ark-poly 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "educe", + "fnv", + "hashbrown 0.15.5", + "itertools 0.13.0", + "num-bigint", + "num-integer", + "num-traits", + "zeroize", +] [[package]] -name = "castaway" -version = "0.2.4" +name = "ark-ed-on-bls12-381-bandersnatch" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +checksum = "1786b2e3832f6f0f7c8d62d5d5a282f6952a1ab99981c54cd52b6ac1d8f02df5" dependencies = [ - "rustversion", + "ark-bls12-381 0.5.0", + "ark-ec 0.5.0", + "ark-ff 0.5.0", + "ark-std 0.5.0", ] [[package]] -name = "cc" -version = "1.2.56" +name = "ark-ff" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" dependencies = [ - "find-msvc-tools", - "shlex", + "ark-ff-asm 0.4.2", + "ark-ff-macros 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "derivative", + "digest 0.10.7", + "itertools 0.10.5", + "num-bigint", + "num-traits", + "paste", + "rustc_version", + "zeroize", ] [[package]] -name = "cfg-if" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" - -[[package]] -name = "cfg_aliases" -version = "0.2.1" +name = "ark-ff" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +checksum = "a177aba0ed1e0fbb62aa9f6d0502e9b46dad8c2eab04c14258a1212d2557ea70" +dependencies = [ + "ark-ff-asm 0.5.0", + "ark-ff-macros 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "arrayvec 0.7.6", + "digest 0.10.7", + "educe", + "itertools 0.13.0", + "num-bigint", + "num-traits", + "paste", + "zeroize", +] [[package]] -name = "chrono" -version = "0.4.43" +name = "ark-ff-asm" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" dependencies = [ - "iana-time-zone", - "js-sys", - "num-traits", - "serde", - "wasm-bindgen", - "windows-link", + "quote", + "syn 1.0.109", ] [[package]] -name = "clap" -version = "4.5.59" +name = "ark-ff-asm" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5caf74d17c3aec5495110c34cc3f78644bfa89af6c8993ed4de2790e49b6499" +checksum = "62945a2f7e6de02a31fe400aa489f0e0f5b2502e69f95f853adb82a96c7a6b60" dependencies = [ - "clap_builder", - "clap_derive", + "quote", + "syn 2.0.111", ] [[package]] -name = "clap_builder" -version = "4.5.59" +name = "ark-ff-macros" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "370daa45065b80218950227371916a1633217ae42b2715b2287b606dcd618e24" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] -name = "clap_derive" -version = "4.5.55" +name = "ark-ff-macros" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +checksum = "09be120733ee33f7693ceaa202ca41accd5653b779563608f1234f78ae07c4b3" dependencies = [ - "heck", + "num-bigint", + "num-traits", "proc-macro2", "quote", - "syn", + "syn 2.0.111", ] [[package]] -name = "clap_lex" -version = "1.0.0" +name = "ark-poly" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" +checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf" +dependencies = [ + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "derivative", + "hashbrown 0.13.2", +] [[package]] -name = "colorchoice" -version = "1.0.4" +name = "ark-poly" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +checksum = "579305839da207f02b89cd1679e50e67b4331e2f9294a57693e5051b7703fe27" +dependencies = [ + "ahash 0.8.12", + "ark-ff 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "educe", + "fnv", + "hashbrown 0.15.5", +] [[package]] -name = "compact_str" -version = "0.8.1" +name = "ark-serialize" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b79c4069c6cad78e2e0cdfcbd26275770669fb39fd308a752dc110e83b9af32" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" dependencies = [ - "castaway", - "cfg-if", - "itoa", - "rustversion", - "ryu", - "static_assertions", + "ark-serialize-derive 0.4.2", + "ark-std 0.4.0", + "digest 0.10.7", + "num-bigint", ] [[package]] -name = "core-foundation-sys" -version = "0.8.7" +name = "ark-serialize" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +checksum = "3f4d068aaf107ebcd7dfb52bc748f8030e0fc930ac8e360146ca54c1203088f7" +dependencies = [ + "ark-serialize-derive 0.5.0", + "ark-std 0.5.0", + "arrayvec 0.7.6", + "digest 0.10.7", + "num-bigint", +] [[package]] -name = "crossterm" -version = "0.28.1" +name = "ark-serialize-derive" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" +checksum = "ae3281bc6d0fd7e549af32b52511e1302185bd688fd3359fa36423346ff682ea" dependencies = [ - "bitflags", - "crossterm_winapi", - "mio", - "parking_lot", - "rustix", - "signal-hook", - "signal-hook-mio", - "winapi", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] -name = "crossterm_winapi" -version = "0.9.1" +name = "ark-serialize-derive" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +checksum = "213888f660fddcca0d257e88e54ac05bca01885f258ccdf695bafd77031bb69d" dependencies = [ - "winapi", + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "darling" -version = "0.23.0" +name = "ark-std" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" dependencies = [ - "darling_core", - "darling_macro", + "num-traits", + "rand 0.8.5", ] [[package]] -name = "darling_core" -version = "0.23.0" +name = "ark-std" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0" +checksum = "246a225cc6131e9ee4f24619af0f19d67761fff15d7ccc22e42b80846e69449a" dependencies = [ - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn", + "num-traits", + "rand 0.8.5", ] [[package]] -name = "darling_macro" -version = "0.23.0" +name = "ark-transcript" +version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d" +checksum = "47c1c928edb9d8ff24cb5dcb7651d3a98494fff3099eee95c2404cd813a9139f" dependencies = [ - "darling_core", - "quote", - "syn", + "ark-ff 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "digest 0.10.7", + "rand_core 0.6.4", + "sha3", ] [[package]] -name = "displaydoc" -version = "0.2.5" +name = "ark-vrf" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" -dependencies = [ - "proc-macro2", - "quote", - "syn", +checksum = "9501da18569b2afe0eb934fb7afd5a247d238b94116155af4dd068f319adfe6d" +dependencies = [ + "ark-bls12-381 0.5.0", + "ark-ec 0.5.0", + "ark-ed-on-bls12-381-bandersnatch", + "ark-ff 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "digest 0.10.7", + "rand_chacha 0.3.1", + "sha2 0.10.9", + "w3f-ring-proof", + "zeroize", ] [[package]] -name = "either" -version = "1.15.0" +name = "array-bytes" +version = "6.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +checksum = "5d5dde061bd34119e902bbb2d9b90c5692635cf59fb91d582c2b68043f1b8293" [[package]] -name = "equivalent" -version = "1.0.2" +name = "arrayref" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" [[package]] -name = "errno" -version = "0.3.14" +name = "arrayvec" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" dependencies = [ - "libc", - "windows-sys 0.61.2", + "nodrop", ] [[package]] -name = "find-msvc-tools" -version = "0.1.9" +name = "arrayvec" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] -name = "foldhash" -version = "0.1.5" +name = "asn1-rs" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +checksum = "5493c3bedbacf7fd7382c6346bbd66687d12bbaad3a89a2d2c303ee6cf20b048" +dependencies = [ + "asn1-rs-derive", + "asn1-rs-impl", + "displaydoc", + "nom 7.1.3", + "num-traits", + "rusticata-macros", + "thiserror 1.0.69", + "time", +] [[package]] -name = "form_urlencoded" -version = "1.2.2" +name = "asn1-rs-derive" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +checksum = "965c2d33e53cb6b267e148a4cb0760bc01f4904c1cd4bb4002a085bb016d1490" dependencies = [ - "percent-encoding", + "proc-macro2", + "quote", + "syn 2.0.111", + "synstructure", ] [[package]] -name = "futures-channel" -version = "0.3.32" +name = "asn1-rs-impl" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" dependencies = [ - "futures-core", + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "futures-core" -version = "0.3.32" +name = "async-channel" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] [[package]] -name = "futures-task" -version = "0.3.32" +name = "async-executor" +version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" +checksum = "497c00e0fd83a72a79a39fcbd8e3e2f055d6f6c7e025f3b3d91f4f8e76527fb8" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "pin-project-lite", + "slab", +] [[package]] -name = "futures-util" -version = "0.3.32" +name = "async-fs" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5" dependencies = [ - "futures-core", - "futures-task", - "pin-project-lite", - "slab", + "async-lock", + "blocking", + "futures-lite", ] [[package]] -name = "getrandom" -version = "0.2.17" +name = "async-io" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix 1.1.2", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-lock" +version = "3.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd03604047cee9b6ce9de9f70c6cd540a0520c813cbd49bae61f33ab80ed1dc" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-net" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7" +dependencies = [ + "async-io", + "blocking", + "futures-lite", +] + +[[package]] +name = "async-process" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75" +dependencies = [ + "async-channel", + "async-io", + "async-lock", + "async-signal", + "async-task", + "blocking", + "cfg-if", + "event-listener", + "futures-lite", + "rustix 1.1.2", +] + +[[package]] +name = "async-signal" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43c070bbf59cd3570b6b2dd54cd772527c7c3620fce8be898406dd3ed6adc64c" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "futures-core", + "futures-io", + "rustix 1.1.2", + "signal-hook-registry", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-stream" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "asynchronous-codec" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" +dependencies = [ + "bytes", + "futures-sink", + "futures-util", + "memchr", + "pin-project-lite", +] + +[[package]] +name = "atomic-take" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8ab6b55fe97976e46f91ddbed8d147d966475dc29b2032757ba47e02376fbc3" + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "attohttpc" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d9a9bf8b79a749ee0b911b91b671cc2b6c670bdbc7e3dfd537576ddc94bb2a2" +dependencies = [ + "http 0.2.12", + "log", + "url", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "axum" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" +dependencies = [ + "async-trait", + "axum-core", + "base64 0.22.1", + "bytes", + "futures-util", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.8.1", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sha1", + "sync_wrapper 1.0.2", + "tokio", + "tokio-tungstenite", + "tower 0.5.2", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 1.0.2", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "backtrace" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6" +dependencies = [ + "addr2line 0.25.1", "cfg-if", - "js-sys", "libc", - "wasi", - "wasm-bindgen", + "miniz_oxide", + "object 0.37.3", + "rustc-demangle", + "windows-link", +] + +[[package]] +name = "base-x" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" + +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + +[[package]] +name = "base256emoji" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e9430d9a245a77c92176e649af6e275f20839a48389859d1661e9a128d077c" +dependencies = [ + "const-str", + "match-lookup", +] + +[[package]] +name = "base58" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581" + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64ct" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e050f626429857a27ddccb31e0aca21356bfa709c04041aefddac081a8f068a" + +[[package]] +name = "binary-merkle-tree" +version = "16.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95c9f6900c9fd344d53fbdfb36e1343429079d73f4168c8ef48884bf15616dbd" +dependencies = [ + "hash-db", + "log", + "parity-scale-codec", +] + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bip39" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90dbd31c98227229239363921e60fcf5e558e43ec69094d46fc4996f08d1d5bc" +dependencies = [ + "bitcoin_hashes 0.14.1", +] + +[[package]] +name = "bitcoin-internals" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" + +[[package]] +name = "bitcoin_hashes" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" +dependencies = [ + "bitcoin-internals", + "hex-conservative 0.1.2", +] + +[[package]] +name = "bitcoin_hashes" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26ec84b80c482df901772e931a9a681e26a1b9ee2302edeff23cb30328745c8b" +dependencies = [ + "hex-conservative 0.2.2", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "bitmaps" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2" +dependencies = [ + "typenum", +] + +[[package]] +name = "bittensor-rs" +version = "0.1.0" +source = "git+https://github.com/CortexLM/bittensor-rs?rev=9cf5991#9cf59914468a30abb5d594381cc2c0944d4b0c61" +dependencies = [ + "anyhow", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "async-trait", + "chrono", + "futures", + "hex", + "num-traits", + "parity-scale-codec", + "rand 0.9.2", + "rand_chacha 0.3.1", + "regex", + "scale-decode", + "scale-encode", + "scale-info", + "serde", + "serde_json", + "sha2 0.10.9", + "sp-core 38.1.0", + "sp-runtime", + "subxt", + "thiserror 2.0.17", + "tle", + "tokio", + "tracing", + "w3f-bls 0.1.3", +] + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "blake2-rfc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +dependencies = [ + "arrayvec 0.4.12", + "constant_time_eq 0.1.5", +] + +[[package]] +name = "blake2b_simd" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06e903a20b159e944f91ec8499fe1e55651480c541ea0a584f5d967c49ad9d99" +dependencies = [ + "arrayref", + "arrayvec 0.7.6", + "constant_time_eq 0.3.1", +] + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blocking" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + +[[package]] +name = "bounded-collections" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ad8a0bed7827f0b07a5d23cec2e58cc02038a99e4ca81616cb2bb2025f804d" +dependencies = [ + "log", + "parity-scale-codec", + "scale-info", + "serde", +] + +[[package]] +name = "bounded-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee8eddd066a8825ec5570528e6880471210fd5d88cb6abbe1cfdd51ca249c33" +dependencies = [ + "jam-codec", + "log", + "parity-scale-codec", + "scale-info", + "serde", +] + +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +dependencies = [ + "allocator-api2", +] + +[[package]] +name = "byte-slice-cast" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7575182f7272186991736b70173b0ea045398f984bf5ebbb3804736ce1330c9d" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" +dependencies = [ + "serde", +] + +[[package]] +name = "cc" +version = "1.2.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", + "zeroize", +] + +[[package]] +name = "clap" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.17", +] + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "common-path" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2382f75942f4b3be3690fe4f86365e9c853c1587d6ee58212cebf6e2a9ccd101" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "const-str" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f421161cb492475f1661ddc9815a745a1c894592070661180fdec3d4872e9c3" + +[[package]] +name = "const_format" +version = "0.2.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7faa7469a93a566e9ccc1c73fe783b4a65c274c5ace346038dca9c39fe0030ad" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d57c2eccfb16dbac1f4e61e206105db5820c9d26c3c472bc17c774259ef7744" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "constant_time_eq" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6" + +[[package]] +name = "convert_case" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b49ba7ef1ad6107f8824dbe97de947cbaac53c44e7f9756a1fba0d37c1eec505" +dependencies = [ + "memchr", +] + +[[package]] +name = "cpp_demangle" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeaa953eaad386a53111e47172c2fedba671e5684c8dd601a5f474f4f118710f" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "cpp_demangle" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2bb79cb74d735044c972aae58ed0aaa9a837e85b01106a54c39e42e97f62253" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cranelift-assembler-x64" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0377b13bf002a0774fcccac4f1102a10f04893d24060cf4b7350c87e4cbb647c" +dependencies = [ + "cranelift-assembler-x64-meta", +] + +[[package]] +name = "cranelift-assembler-x64-meta" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfa027979140d023b25bf7509fb7ede3a54c3d3871fb5ead4673c4b633f671a2" +dependencies = [ + "cranelift-srcgen", +] + +[[package]] +name = "cranelift-bforest" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "618e4da87d9179a70b3c2f664451ca8898987aa6eb9f487d16988588b5d8cc40" +dependencies = [ + "cranelift-entity 0.128.3", +] + +[[package]] +name = "cranelift-bitset" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db53764b5dad233b37b8f5dc54d3caa9900c54579195e00f17ea21f03f71aaa7" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "cranelift-codegen" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae927f1d8c0abddaa863acd201471d56e7fc6c3925104f4861ed4dc3e28b421" +dependencies = [ + "bumpalo", + "cranelift-assembler-x64", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-control", + "cranelift-entity 0.128.3", + "cranelift-isle", + "gimli 0.32.3", + "hashbrown 0.15.5", + "log", + "pulley-interpreter", + "regalloc2", + "rustc-hash", + "serde", + "smallvec", + "target-lexicon 0.13.4", + "wasmtime-internal-math", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fcf1e3e6757834bd2584f4cbff023fcc198e9279dcb5d684b4bb27a9b19f54" +dependencies = [ + "cranelift-assembler-x64-meta", + "cranelift-codegen-shared", + "cranelift-srcgen", + "heck", + "pulley-interpreter", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "205dcb9e6ccf9d368b7466be675ff6ee54a63e36da6fe20e72d45169cf6fd254" + +[[package]] +name = "cranelift-control" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "108eca9fcfe86026054f931eceaf57b722c1b97464bf8265323a9b5877238817" +dependencies = [ + "arbitrary", +] + +[[package]] +name = "cranelift-entity" +version = "0.95.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40099d38061b37e505e63f89bab52199037a72b931ad4868d9089ff7268660b0" +dependencies = [ + "serde", +] + +[[package]] +name = "cranelift-entity" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d96496910065d3165f84ff8e1e393916f4c086f88ac8e1b407678bc78735aa" +dependencies = [ + "cranelift-bitset", + "serde", + "serde_derive", +] + +[[package]] +name = "cranelift-frontend" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e303983ad7e23c850f24d9c41fc3cb346e1b930f066d3966545e4c98dac5c9fb" +dependencies = [ + "cranelift-codegen", + "log", + "smallvec", + "target-lexicon 0.13.4", +] + +[[package]] +name = "cranelift-isle" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b0cf8d867d891245836cac7abafb0a5b0ea040a019d720702b3b8bcba40bfa" + +[[package]] +name = "cranelift-native" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e24b641e315443e27807b69c440fe766737d7e718c68beb665a2d69259c77bf3" +dependencies = [ + "cranelift-codegen", + "libc", + "target-lexicon 0.13.4", +] + +[[package]] +name = "cranelift-srcgen" +version = "0.128.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e378a54e7168a689486d67ee1f818b7e5356e54ae51a1d7a53f4f13f7f8b7a" + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "typenum", +] + +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + +[[package]] +name = "curve25519-dalek" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +dependencies = [ + "byteorder", + "digest 0.9.0", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "darling" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.111", +] + +[[package]] +name = "darling_macro" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "data-encoding" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476" + +[[package]] +name = "data-encoding-macro" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47ce6c96ea0102f01122a185683611bd5ac8d99e62bc59dd12e6bda344ee673d" +dependencies = [ + "data-encoding", + "data-encoding-macro-internal", +] + +[[package]] +name = "data-encoding-macro-internal" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d162beedaa69905488a8da94f5ac3edb4dd4788b732fadb7bd120b2625c1976" +dependencies = [ + "data-encoding", + "syn 2.0.111", +] + +[[package]] +name = "debugid" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef552e6f588e446098f6ba40d89ac146c8c7b64aade83c051ee00bb5d2bc18d" +dependencies = [ + "uuid", +] + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "der-parser" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cd0a5c643689626bec213c4d8bd4d96acc8ffdb4ad4bb6bc16abf27d5f4b553" +dependencies = [ + "asn1-rs", + "displaydoc", + "nom 7.1.3", + "num-bigint", + "num-traits", + "rusticata-macros", +] + +[[package]] +name = "deranged" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ececcb659e7ba858fb4f10388c250a7252eb0a27373f1a72b8748afdd248e587" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derive-syn-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "derive-where" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef941ded77d15ca19b40374869ac6000af1c9f2a4c0f3d4c70926287e6364a8f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl 1.0.0", +] + +[[package]] +name = "derive_more" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10b768e943bed7bf2cab53df09f4bc34bfd217cdb57d971e769874c9a6710618" +dependencies = [ + "derive_more-impl 2.1.0", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d286bfdaf75e988b4a78e013ecd79c581e06399ab53fbacd2d916c2f904f30b" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.111", + "unicode-xid", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "const-oid", + "crypto-common", + "subtle", +] + +[[package]] +name = "directories-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "docify" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a772b62b1837c8f060432ddcc10b17aae1453ef17617a99bc07789252d2a5896" +dependencies = [ + "docify_macros", +] + +[[package]] +name = "docify_macros" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60e6be249b0a462a14784a99b19bf35a667bb5e09de611738bb7362fa4c95ff7" +dependencies = [ + "common-path", + "derive-syn-parse", + "once_cell", + "proc-macro2", + "quote", + "regex", + "syn 2.0.111", + "termcolor", + "toml 0.8.23", + "walkdir", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "dtoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c3cf4824e2d5f025c7b531afcb2325364084a16806f6d47fbc1f5fbd9960590" + +[[package]] +name = "dyn-clonable" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a36efbb9bfd58e1723780aa04b61aba95ace6a05d9ffabfdb0b43672552f0805" +dependencies = [ + "dyn-clonable-impl", + "dyn-clone", +] + +[[package]] +name = "dyn-clonable-impl" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8671d54058979a37a26f3511fbf8d198ba1aa35ffb202c42587d918d77213a" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der", + "digest 0.10.7", + "elliptic-curve", + "rfc6979", + "serdect", + "signature", + "spki", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek 4.1.3", + "ed25519", + "serde", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "ed25519-zebra" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c24f403d068ad0b359e577a77f92392118be3f3c927538f2bb544a5ecd828c6" +dependencies = [ + "curve25519-dalek 3.2.0", + "hashbrown 0.12.3", + "hex", + "rand_core 0.6.4", + "sha2 0.9.9", + "zeroize", +] + +[[package]] +name = "ed25519-zebra" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0017d969298eec91e3db7a2985a8cab4df6341d86e6f3a6f5878b13fb7846bc9" +dependencies = [ + "curve25519-dalek 4.1.3", + "ed25519", + "hashbrown 0.15.5", + "pkcs8", + "rand_core 0.6.4", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "educe" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7bc049e1bd8cdeb31b68bbd586a9464ecf9f3944af3958a7a9d0f8b9799417" +dependencies = [ + "enum-ordinalize", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest 0.10.7", + "ff", + "generic-array", + "group", + "pkcs8", + "rand_core 0.6.4", + "sec1", + "serdect", + "subtle", + "zeroize", +] + +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "enum-as-inner" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "enum-ordinalize" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a1091a7bb1f8f2c4b28f1fe2cef4980ca2d410a3d727d67ecc3178c9b0800f0" +dependencies = [ + "enum-ordinalize-derive", +] + +[[package]] +name = "enum-ordinalize-derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ca9601fb2d62598ee17836250842873a413586e5d7ed88b356e38ddbb0ec631" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "environmental" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48c92028aaa870e83d51c64e5d4e0b6981b360c522198c23959f219a4e1b15b" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "event-listener" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "expander" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2c470c71d91ecbd179935b24170459e926382eaaa86b590b78814e180d8a8e2" +dependencies = [ + "blake2", + "file-guard", + "fs-err", + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "ff" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "file-guard" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ef72acf95ec3d7dbf61275be556299490a245f017cf084bd23b4f68cf9407c" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" + +[[package]] +name = "fixed-hash" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" +dependencies = [ + "byteorder", + "rand 0.8.5", + "rustc-hex", + "static_assertions", +] + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "frame-decode" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c470df86cf28818dd3cd2fc4667b80dbefe2236c722c3dc1d09e7c6c82d6dfcd" +dependencies = [ + "frame-metadata", + "parity-scale-codec", + "scale-decode", + "scale-encode", + "scale-info", + "scale-type-resolver", + "sp-crypto-hashing", + "thiserror 2.0.17", +] + +[[package]] +name = "frame-metadata" +version = "23.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba5be0edbdb824843a0f9c6f0906ecfc66c5316218d74457003218b24909ed0" +dependencies = [ + "cfg-if", + "parity-scale-codec", + "scale-info", + "serde", +] + +[[package]] +name = "fs-err" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88a41f105fe1d5b6b34b2055e3dc59bb79b46b48b2040b9e6c7b4b5de097aa41" +dependencies = [ + "autocfg", +] + +[[package]] +name = "fs2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-bounded" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91f328e7fb845fc832912fb6a34f40cf6d1888c92f974d1893a54e97b5ff542e" +dependencies = [ + "futures-timer", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", + "num_cpus", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "futures-rustls" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f2f12607f92c69b12ed746fabf9ca4f5c482cba46679c1a75b874ed7c26adb" +dependencies = [ + "futures-io", + "rustls", + "rustls-pki-types", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-ticker" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9763058047f713632a52e916cc7f6a4b3fc6e9fc1ff8c5b1dc49e5a89041682e" +dependencies = [ + "futures", + "futures-timer", + "instant", +] + +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + +[[package]] +name = "fxprof-processed-profile" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25234f20a3ec0a962a61770cfe39ecf03cb529a6e474ad8cff025ed497eda557" +dependencies = [ + "bitflags 2.10.0", + "debugid", + "rustc-hash", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", + "zeroize", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "getrandom_or_panic" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea1015b5a70616b688dc230cfe50c8af89d972cb132d5a622814d29773b10b9" +dependencies = [ + "rand 0.8.5", + "rand_core 0.6.4", +] + +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + +[[package]] +name = "gimli" +version = "0.27.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" +dependencies = [ + "fallible-iterator 0.2.0", + "indexmap 1.9.3", + "stable_deref_trait", +] + +[[package]] +name = "gimli" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" +dependencies = [ + "fallible-iterator 0.3.0", + "indexmap 2.12.1", + "stable_deref_trait", +] + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "h2" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap 2.12.1", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c0b69cfcb4e1b9f1bf2f53f95f766e4661169728ec61cd3fe5a0166f2d1386" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.4.0", + "indexmap 2.12.1", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "half" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403" + +[[package]] +name = "hash-db" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e7d7786361d7425ae2fe4f9e407eb0efaa0840f5212d109cc018c40c35c6ab4" + +[[package]] +name = "hash256-std-hasher" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c171d55b98633f4ed3860808f004099b36c1cc29c42cfc53aa8591b21efcf2" +dependencies = [ + "crunchy", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash 0.7.8", +] + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash 0.8.12", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash 0.8.12", + "allocator-api2", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", + "serde", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-conservative" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212ab92002354b4819390025006c897e8140934349e8635c9b077f47b4dcbd20" + +[[package]] +name = "hex-conservative" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda06d18ac606267c40c04e41b9947729bf8b9efe74bd4e82b61a5f26a510b9f" +dependencies = [ + "arrayvec 0.7.6", +] + +[[package]] +name = "hex_fmt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b07f60793ff0a4d9cef0f18e63b5357e06209987153a64648c972c1e5aff336f" + +[[package]] +name = "hickory-proto" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92652067c9ce6f66ce53cc38d1169daa36e6e7eb7dd3b63b5103bd9d97117248" +dependencies = [ + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna 1.1.0", + "ipnet", + "once_cell", + "rand 0.8.5", + "socket2 0.5.10", + "thiserror 1.0.69", + "tinyvec", + "tokio", + "tracing", + "url", +] + +[[package]] +name = "hickory-resolver" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbb117a1ca520e111743ab2f6688eddee69db4e0ea242545a604dce8a66fd22e" +dependencies = [ + "cfg-if", + "futures-util", + "hickory-proto", + "ipconfig", + "lru-cache", + "once_cell", + "parking_lot 0.12.5", + "rand 0.8.5", + "resolv-conf", + "smallvec", + "thiserror 1.0.69", + "tokio", + "tracing", +] + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac 0.12.1", +] + +[[package]] +name = "hmac" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126888268dcc288495a26bf004b38c5fdbb31682f992c84ceb046a1f0fe38840" +dependencies = [ + "crypto-mac", + "digest 0.9.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "hmac-drbg" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ea0a1394df5b6574da6e0c1ade9e78868c9fb0a4e5ef4428e32da4676b85b1" +dependencies = [ + "digest 0.9.0", + "generic-array", + "hmac 0.8.1", +] + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.4.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "0.14.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.27", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2 0.5.10", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2 0.4.12", + "http 1.4.0", + "http-body 1.0.1", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" +dependencies = [ + "http 1.4.0", + "hyper 1.8.1", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.32", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.8.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "727805d60e7938b76b826a6ef209eb70eaa1812794f9424d4a4e2d740662df5f" +dependencies = [ + "base64 0.22.1", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http 1.4.0", + "http-body 1.0.1", + "hyper 1.8.1", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2 0.6.1", + "system-configuration 0.6.1", + "tokio", + "tower-service", + "tracing", + "windows-registry", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core 0.62.2", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "if-addrs" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cabb0019d51a643781ff15c9c8a3e5dedc365c47211270f4e8f82812fedd8f0a" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "if-watch" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf9d64cfcf380606e64f9a0bcf493616b65331199f984151a6fa11a7b3cde38" +dependencies = [ + "async-io", + "core-foundation 0.9.4", + "fnv", + "futures", + "if-addrs", + "ipnet", + "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-proto", + "netlink-sys", + "rtnetlink", + "system-configuration 0.6.1", + "tokio", + "windows", +] + +[[package]] +name = "igd-next" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "064d90fec10d541084e7b39ead8875a5a80d9114a2b18791565253bae25f49e4" +dependencies = [ + "async-trait", + "attohttpc", + "bytes", + "futures", + "http 0.2.12", + "hyper 0.14.32", + "log", + "rand 0.8.5", + "tokio", + "url", + "xmltree", +] + +[[package]] +name = "im-rc" +version = "15.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe" +dependencies = [ + "bitmaps", + "rand_core 0.6.4", + "rand_xoshiro", + "sized-chunks", + "typenum", + "version_check", +] + +[[package]] +name = "impl-codec" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba6a270039626615617f3f36d15fc827041df3b78c439da2cadfa47455a77f2f" +dependencies = [ + "parity-scale-codec", +] + +[[package]] +name = "impl-codec" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d40b9d5e17727407e55028eafc22b2dc68781786e6d7eb8a21103f5058e3a14" +dependencies = [ + "parity-scale-codec", +] + +[[package]] +name = "impl-num-traits" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "803d15461ab0dcc56706adf266158acbc44ccf719bf7d0af30705f58b90a4b8c" +dependencies = [ + "integer-sqrt", + "num-traits", + "uint 0.10.0", +] + +[[package]] +name = "impl-serde" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc88fc67028ae3db0c853baa36269d398d5f45b6982f95549ff5def78c935cd" +dependencies = [ + "serde", +] + +[[package]] +name = "impl-serde" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a143eada6a1ec4aefa5049037a26a6d597bfd64f8c026d07b77133e02b7dd0b" +dependencies = [ + "serde", +] + +[[package]] +name = "impl-trait-for-tuples" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0eb5a3343abf848c0984fe4604b2b105da9539376e24fc0a3b0007411ae4fd9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ad4bb2b565bca0645f4d68c5c9af97fba094e9791da685bf83cb5f3ce74acf2" +dependencies = [ + "equivalent", + "hashbrown 0.16.1", + "serde", + "serde_core", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "integer-sqrt" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770" +dependencies = [ + "num-traits", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi 0.3.9", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "ipconfig" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" +dependencies = [ + "socket2 0.5.10", + "widestring", + "windows-sys 0.48.0", + "winreg", +] + +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "iri-string" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f867b9d1d896b67beb18518eda36fdb77a32ea590de864f1325b294a6d14397" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "ittapi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b996fe614c41395cdaedf3cf408a9534851090959d90d54a535f675550b64b1" +dependencies = [ + "anyhow", + "ittapi-sys", + "log", +] + +[[package]] +name = "ittapi-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52f5385394064fa2c886205dba02598013ce83d3e92d33dbdc0c52fe0e7bf4fc" +dependencies = [ + "cc", +] + +[[package]] +name = "jam-codec" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb948eace373d99de60501a02fb17125d30ac632570de20dccc74370cdd611b9" +dependencies = [ + "arrayvec 0.7.6", + "bitvec", + "byte-slice-cast", + "const_format", + "impl-trait-for-tuples", + "jam-codec-derive", + "rustversion", + "serde", +] + +[[package]] +name = "jam-codec-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "319af585c4c8a6b5552a52b7787a1ab3e4d59df7614190b1f85b9b842488789d" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys", + "log", + "thiserror 1.0.69", + "walkdir", + "windows-sys 0.45.0", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + +[[package]] +name = "jobserver" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33" +dependencies = [ + "getrandom 0.3.4", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "jsonrpsee" +version = "0.24.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e281ae70cc3b98dac15fced3366a880949e65fc66e345ce857a5682d152f3e62" +dependencies = [ + "jsonrpsee-client-transport", + "jsonrpsee-core", + "jsonrpsee-types", + "jsonrpsee-ws-client", +] + +[[package]] +name = "jsonrpsee-client-transport" +version = "0.24.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4280b709ac3bb5e16cf3bad5056a0ec8df55fa89edfe996361219aadc2c7ea" +dependencies = [ + "base64 0.22.1", + "futures-util", + "http 1.4.0", + "jsonrpsee-core", + "pin-project", + "rustls", + "rustls-pki-types", + "rustls-platform-verifier", + "soketto", + "thiserror 1.0.69", + "tokio", + "tokio-rustls", + "tokio-util", + "tracing", + "url", +] + +[[package]] +name = "jsonrpsee-core" +version = "0.24.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "348ee569eaed52926b5e740aae20863762b16596476e943c9e415a6479021622" +dependencies = [ + "async-trait", + "futures-timer", + "futures-util", + "jsonrpsee-types", + "pin-project", + "rustc-hash", + "serde", + "serde_json", + "thiserror 1.0.69", + "tokio", + "tokio-stream", + "tracing", +] + +[[package]] +name = "jsonrpsee-types" +version = "0.24.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0f05e0028e55b15dbd2107163b3c744cd3bb4474f193f95d9708acbf5677e44" +dependencies = [ + "http 1.4.0", + "serde", + "serde_json", + "thiserror 1.0.69", +] + +[[package]] +name = "jsonrpsee-ws-client" +version = "0.24.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78fc744f17e7926d57f478cf9ca6e1ee5d8332bf0514860b1a3cdf1742e614cc" +dependencies = [ + "http 1.4.0", + "jsonrpsee-client-transport", + "jsonrpsee-core", + "jsonrpsee-types", + "url", +] + +[[package]] +name = "k256" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6e3919bbaa2945715f0bb6d3934a173d1e9a59ac23767fbaaef277265a7411b" +dependencies = [ + "cfg-if", + "ecdsa", + "elliptic-curve", + "once_cell", + "serdect", + "sha2 0.10.9", +] + +[[package]] +name = "keccak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "keccak-hash" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e1b8590eb6148af2ea2d75f38e7d29f5ca970d5a4df456b3ef19b8b415d0264" +dependencies = [ + "primitive-types 0.13.1", + "tiny-keccak", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.178" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" + +[[package]] +name = "libm" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" + +[[package]] +name = "libp2p" +version = "0.54.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbe80f9c7e00526cd6b838075b9c171919404a4732cb2fa8ece0a093223bfc4" +dependencies = [ + "bytes", + "either", + "futures", + "futures-timer", + "getrandom 0.2.16", + "libp2p-allow-block-list", + "libp2p-connection-limits", + "libp2p-core", + "libp2p-dns", + "libp2p-gossipsub", + "libp2p-identify", + "libp2p-identity", + "libp2p-kad", + "libp2p-mdns", + "libp2p-metrics", + "libp2p-noise", + "libp2p-quic", + "libp2p-swarm", + "libp2p-tcp", + "libp2p-upnp", + "libp2p-yamux", + "multiaddr", + "pin-project", + "rw-stream-sink", + "thiserror 1.0.69", +] + +[[package]] +name = "libp2p-allow-block-list" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1027ccf8d70320ed77e984f273bc8ce952f623762cb9bf2d126df73caef8041" +dependencies = [ + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "void", +] + +[[package]] +name = "libp2p-connection-limits" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d003540ee8baef0d254f7b6bfd79bac3ddf774662ca0abf69186d517ef82ad8" +dependencies = [ + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "void", +] + +[[package]] +name = "libp2p-core" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a61f26c83ed111104cd820fe9bc3aaabbac5f1652a1d213ed6e900b7918a1298" +dependencies = [ + "either", + "fnv", + "futures", + "futures-timer", + "libp2p-identity", + "multiaddr", + "multihash", + "multistream-select", + "once_cell", + "parking_lot 0.12.5", + "pin-project", + "quick-protobuf", + "rand 0.8.5", + "rw-stream-sink", + "serde", + "smallvec", + "thiserror 1.0.69", + "tracing", + "unsigned-varint 0.8.0", + "void", + "web-time", +] + +[[package]] +name = "libp2p-dns" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97f37f30d5c7275db282ecd86e54f29dd2176bd3ac656f06abf43bedb21eb8bd" +dependencies = [ + "async-trait", + "futures", + "hickory-resolver", + "libp2p-core", + "libp2p-identity", + "parking_lot 0.12.5", + "smallvec", + "tracing", +] + +[[package]] +name = "libp2p-gossipsub" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4e830fdf24ac8c444c12415903174d506e1e077fbe3875c404a78c5935a8543" +dependencies = [ + "asynchronous-codec", + "base64 0.22.1", + "byteorder", + "bytes", + "either", + "fnv", + "futures", + "futures-ticker", + "getrandom 0.2.16", + "hex_fmt", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "prometheus-client", + "quick-protobuf", + "quick-protobuf-codec", + "rand 0.8.5", + "regex", + "serde", + "sha2 0.10.9", + "smallvec", + "tracing", + "void", + "web-time", +] + +[[package]] +name = "libp2p-identify" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1711b004a273be4f30202778856368683bd9a83c4c7dcc8f848847606831a4e3" +dependencies = [ + "asynchronous-codec", + "either", + "futures", + "futures-bounded", + "futures-timer", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "lru", + "quick-protobuf", + "quick-protobuf-codec", + "smallvec", + "thiserror 1.0.69", + "tracing", + "void", +] + +[[package]] +name = "libp2p-identity" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c7892c221730ba55f7196e98b0b8ba5e04b4155651736036628e9f73ed6fc3" +dependencies = [ + "bs58", + "ed25519-dalek", + "hkdf", + "multihash", + "quick-protobuf", + "rand 0.8.5", + "serde", + "sha2 0.10.9", + "thiserror 2.0.17", + "tracing", + "zeroize", +] + +[[package]] +name = "libp2p-kad" +version = "0.46.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced237d0bd84bbebb7c2cad4c073160dacb4fe40534963c32ed6d4c6bb7702a3" +dependencies = [ + "arrayvec 0.7.6", + "asynchronous-codec", + "bytes", + "either", + "fnv", + "futures", + "futures-bounded", + "futures-timer", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "quick-protobuf", + "quick-protobuf-codec", + "rand 0.8.5", + "serde", + "sha2 0.10.9", + "smallvec", + "thiserror 1.0.69", + "tracing", + "uint 0.9.5", + "void", + "web-time", +] + +[[package]] +name = "libp2p-mdns" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b8546b6644032565eb29046b42744aee1e9f261ed99671b2c93fb140dba417" +dependencies = [ + "data-encoding", + "futures", + "hickory-proto", + "if-watch", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "rand 0.8.5", + "smallvec", + "socket2 0.5.10", + "tokio", + "tracing", + "void", +] + +[[package]] +name = "libp2p-metrics" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ebafa94a717c8442d8db8d3ae5d1c6a15e30f2d347e0cd31d057ca72e42566" +dependencies = [ + "futures", + "libp2p-core", + "libp2p-gossipsub", + "libp2p-identify", + "libp2p-identity", + "libp2p-kad", + "libp2p-swarm", + "pin-project", + "prometheus-client", + "web-time", +] + +[[package]] +name = "libp2p-noise" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36b137cb1ae86ee39f8e5d6245a296518912014eaa87427d24e6ff58cfc1b28c" +dependencies = [ + "asynchronous-codec", + "bytes", + "curve25519-dalek 4.1.3", + "futures", + "libp2p-core", + "libp2p-identity", + "multiaddr", + "multihash", + "once_cell", + "quick-protobuf", + "rand 0.8.5", + "sha2 0.10.9", + "snow", + "static_assertions", + "thiserror 1.0.69", + "tracing", + "x25519-dalek", + "zeroize", +] + +[[package]] +name = "libp2p-quic" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46352ac5cd040c70e88e7ff8257a2ae2f891a4076abad2c439584a31c15fd24e" +dependencies = [ + "bytes", + "futures", + "futures-timer", + "if-watch", + "libp2p-core", + "libp2p-identity", + "libp2p-tls", + "parking_lot 0.12.5", + "quinn", + "rand 0.8.5", + "ring 0.17.14", + "rustls", + "socket2 0.5.10", + "thiserror 1.0.69", + "tokio", + "tracing", +] + +[[package]] +name = "libp2p-swarm" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7dd6741793d2c1fb2088f67f82cf07261f25272ebe3c0b0c311e0c6b50e851a" +dependencies = [ + "either", + "fnv", + "futures", + "futures-timer", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm-derive", + "lru", + "multistream-select", + "once_cell", + "rand 0.8.5", + "smallvec", + "tokio", + "tracing", + "void", + "web-time", +] + +[[package]] +name = "libp2p-swarm-derive" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206e0aa0ebe004d778d79fb0966aa0de996c19894e2c0605ba2f8524dd4443d8" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "libp2p-tcp" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad964f312c59dcfcac840acd8c555de8403e295d39edf96f5240048b5fcaa314" +dependencies = [ + "futures", + "futures-timer", + "if-watch", + "libc", + "libp2p-core", + "libp2p-identity", + "socket2 0.5.10", + "tokio", + "tracing", +] + +[[package]] +name = "libp2p-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b23dddc2b9c355f73c1e36eb0c3ae86f7dc964a3715f0731cfad352db4d847" +dependencies = [ + "futures", + "futures-rustls", + "libp2p-core", + "libp2p-identity", + "rcgen", + "ring 0.17.14", + "rustls", + "rustls-webpki 0.101.7", + "thiserror 1.0.69", + "x509-parser", + "yasna", +] + +[[package]] +name = "libp2p-upnp" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01bf2d1b772bd3abca049214a3304615e6a36fa6ffc742bdd1ba774486200b8f" +dependencies = [ + "futures", + "futures-timer", + "igd-next", + "libp2p-core", + "libp2p-swarm", + "tokio", + "tracing", + "void", +] + +[[package]] +name = "libp2p-yamux" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "788b61c80789dba9760d8c669a5bedb642c8267555c803fabd8396e4ca5c5882" +dependencies = [ + "either", + "futures", + "libp2p-core", + "thiserror 1.0.69", + "tracing", + "yamux 0.12.1", + "yamux 0.13.8", +] + +[[package]] +name = "libredox" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df15f6eac291ed1cf25865b1ee60399f57e7c227e7f51bdbd4c5270396a9ed50" +dependencies = [ + "bitflags 2.10.0", + "libc", +] + +[[package]] +name = "libsecp256k1" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79019718125edc905a079a70cfa5f3820bc76139fc91d6f9abc27ea2a887139" +dependencies = [ + "arrayref", + "base64 0.22.1", + "digest 0.9.0", + "hmac-drbg", + "libsecp256k1-core", + "libsecp256k1-gen-ecmult", + "libsecp256k1-gen-genmult", + "rand 0.8.5", + "serde", + "sha2 0.9.9", + "typenum", +] + +[[package]] +name = "libsecp256k1-core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" +dependencies = [ + "crunchy", + "digest 0.9.0", + "subtle", +] + +[[package]] +name = "libsecp256k1-gen-ecmult" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" +dependencies = [ + "libsecp256k1-core", +] + +[[package]] +name = "libsecp256k1-gen-genmult" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" +dependencies = [ + "libsecp256k1-core", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "lru" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +dependencies = [ + "hashbrown 0.15.5", +] + +[[package]] +name = "lru-cache" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" +dependencies = [ + "linked-hash-map", +] + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "lz4_flex" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08ab2867e3eeeca90e844d1940eab391c9dc5228783db2ed999acbc0a9ed375a" +dependencies = [ + "twox-hash 2.1.2", +] + +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" +dependencies = [ + "libc", +] + +[[package]] +name = "mach2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d640282b302c0bb0a2a8e0233ead9035e3bed871f0b7e81fe4a1ec829765db44" +dependencies = [ + "libc", +] + +[[package]] +name = "match-lookup" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "757aee279b8bdbb9f9e676796fd459e4207a1f986e87886700abf589f5abf771" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata 0.4.13", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "memfd" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad38eb12aea514a0466ea40a80fd8cc83637065948eb4a426e4aa46261175227" +dependencies = [ + "rustix 1.1.2", +] + +[[package]] +name = "memoffset" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +dependencies = [ + "autocfg", +] + +[[package]] +name = "memory-db" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e300c54e3239a86f9c61cc63ab0f03862eb40b1c6e065dc6fd6ceaeff6da93d" +dependencies = [ + "foldhash", + "hash-db", + "hashbrown 0.15.5", +] + +[[package]] +name = "merlin" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58c38e2799fc0978b65dfff8023ec7843e2330bb462f19198840b34b6582397d" +dependencies = [ + "byteorder", + "keccak", + "rand_core 0.6.4", + "zeroize", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "mock-subtensor" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "axum", + "bincode", + "bs58", + "chrono", + "clap", + "futures", + "hex", + "parking_lot 0.12.5", + "platform-bittensor", + "rand 0.8.5", + "serde", + "serde_json", + "sha2 0.10.9", + "thiserror 2.0.17", + "tokio", + "tokio-tungstenite", + "tower 0.4.13", + "tower-http 0.6.8", + "tracing", + "tracing-subscriber 0.3.22", + "uuid", +] + +[[package]] +name = "multi-stash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" + +[[package]] +name = "multiaddr" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6351f60b488e04c1d21bc69e56b89cb3f5e8f5d22557d6e8031bdfd79b6961" +dependencies = [ + "arrayref", + "byteorder", + "data-encoding", + "libp2p-identity", + "multibase", + "multihash", + "percent-encoding", + "serde", + "static_assertions", + "unsigned-varint 0.8.0", + "url", +] + +[[package]] +name = "multibase" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8694bb4835f452b0e3bb06dbebb1d6fc5385b6ca1caf2e55fd165c042390ec77" +dependencies = [ + "base-x", + "base256emoji", + "data-encoding", + "data-encoding-macro", +] + +[[package]] +name = "multihash" +version = "0.19.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b430e7953c29dd6a09afc29ff0bb69c6e306329ee6794700aee27b76a1aea8d" +dependencies = [ + "core2", + "serde", + "unsigned-varint 0.8.0", +] + +[[package]] +name = "multistream-select" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0df8e5eec2298a62b326ee4f0d7fe1a6b90a09dfcf9df37b38f947a8c42f19" +dependencies = [ + "bytes", + "futures", + "log", + "pin-project", + "smallvec", + "unsigned-varint 0.7.2", +] + +[[package]] +name = "native-tls" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework 2.11.1", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "netlink-packet-core" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72724faf704479d67b388da142b186f916188505e7e0b26719019c525882eda4" +dependencies = [ + "anyhow", + "byteorder", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-route" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053998cea5a306971f88580d0829e90f270f940befd7cf928da179d4187a5a66" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "byteorder", + "libc", + "netlink-packet-core", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-utils" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ede8a08c71ad5a95cdd0e4e52facd37190977039a4704eb82a283f713747d34" +dependencies = [ + "anyhow", + "byteorder", + "paste", + "thiserror 1.0.69", +] + +[[package]] +name = "netlink-proto" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72452e012c2f8d612410d89eea01e2d9b56205274abb35d53f60200b2ec41d60" +dependencies = [ + "bytes", + "futures", + "log", + "netlink-packet-core", + "netlink-sys", + "thiserror 2.0.17", +] + +[[package]] +name = "netlink-sys" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6c30ed10fa69cc491d491b85cc971f6bdeb8e7367b7cde2ee6cc878d583fae" +dependencies = [ + "bytes", + "futures-util", + "libc", + "log", + "tokio", +] + +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", +] + +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" + +[[package]] +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-format" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a652d9771a63711fd3c3deb670acfbe5c30a4072e664d7a3bf5a9e1056ac72c3" +dependencies = [ + "arrayvec 0.7.6", + "itoa", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi 0.5.2", + "libc", +] + +[[package]] +name = "object" +version = "0.30.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03b4680b86d9cfafba8fc491dc9b6df26b68cf40e9e6cd73909194759a63c385" +dependencies = [ + "crc32fast", + "hashbrown 0.13.2", + "indexmap 1.9.3", + "memchr", +] + +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "object" +version = "0.37.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +dependencies = [ + "crc32fast", + "hashbrown 0.15.5", + "indexmap 2.12.1", + "memchr", +] + +[[package]] +name = "oid-registry" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8d8034d9489cdaf79228eb9f6a3b8d7bb32ba00d6645ebd48eef4077ceb5bd9" +dependencies = [ + "asn1-rs", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + +[[package]] +name = "openssl" +version = "0.10.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "openssl-probe" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" + +[[package]] +name = "openssl-sys" +version = "0.9.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "parity-bip39" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e69bf016dc406eff7d53a7d3f7cf1c2e72c82b9088aac1118591e36dd2cd3e9" +dependencies = [ + "bitcoin_hashes 0.13.0", + "rand 0.8.5", + "rand_core 0.6.4", + "serde", + "unicode-normalization", +] + +[[package]] +name = "parity-scale-codec" +version = "3.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799781ae679d79a948e13d4824a40970bfa500058d245760dd857301059810fa" +dependencies = [ + "arrayvec 0.7.6", + "bitvec", + "byte-slice-cast", + "bytes", + "const_format", + "impl-trait-for-tuples", + "parity-scale-codec-derive", + "rustversion", + "serde", +] + +[[package]] +name = "parity-scale-codec-derive" +version = "3.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b4653168b563151153c9e4c08ebed57fb8262bebfa79711552fa983c623e7a" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.12", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "winapi", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.18", + "smallvec", + "windows-link", +] + +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest 0.10.7", + "password-hash", +] + +[[package]] +name = "pem" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be" +dependencies = [ + "base64 0.22.1", + "serde_core", +] + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +dependencies = [ + "fixedbitset", + "indexmap 2.12.1", +] + +[[package]] +name = "pin-project" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "piper" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + +[[package]] +name = "platform-bittensor" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "bittensor-rs", + "chrono", + "futures", + "hex", + "parking_lot 0.12.5", + "platform-challenge-sdk", + "platform-core", + "rand 0.8.5", + "reqwest 0.11.27", + "serde", + "serde_json", + "sha2 0.10.9", + "sp-core 38.1.0", + "thiserror 2.0.17", + "tokio", + "tracing", +] + +[[package]] +name = "platform-challenge-registry" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "bincode", + "chrono", + "futures", + "hex", + "parking_lot 0.12.5", + "platform-challenge-sdk", + "platform-core", + "platform-storage", + "reqwest 0.12.25", + "semver", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tokio-test", + "tracing", + "uuid", + "wasm-runtime-interface", +] + +[[package]] +name = "platform-challenge-sdk" +version = "0.1.0" +dependencies = [ + "aes-gcm", + "anyhow", + "async-trait", + "axum", + "bincode", + "chrono", + "futures", + "hex", + "parity-scale-codec", + "parking_lot 0.12.5", + "platform-core", + "rand 0.8.5", + "serde", + "serde_json", + "sha2 0.10.9", + "sled", + "sp-core 38.1.0", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tokio-tungstenite", + "tower 0.5.2", + "tower-http 0.6.8", + "tracing", + "uuid", +] + +[[package]] +name = "platform-challenge-sdk-wasm" +version = "0.1.0" +dependencies = [ + "bincode", + "serde", +] + +[[package]] +name = "platform-cli" +version = "0.1.0" +dependencies = [ + "anyhow", + "chrono", + "clap", + "dirs", + "reqwest 0.12.25", + "semver", + "serde", + "serde_json", + "tokio", + "toml 0.8.23", + "tracing", + "tracing-subscriber 0.3.22", +] + +[[package]] +name = "platform-core" +version = "0.1.0" +dependencies = [ + "anyhow", + "bincode", + "bs58", + "chrono", + "hex", + "rand 0.8.5", + "schnorrkel", + "serde", + "serde_json", + "sha2 0.10.9", + "sp-core 31.0.0", + "tempfile", + "thiserror 2.0.17", + "tracing", + "uuid", + "wasm-runtime-interface", +] + +[[package]] +name = "platform-distributed-storage" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "base64 0.22.1", + "bincode", + "chrono", + "futures", + "hex", + "parking_lot 0.12.5", + "platform-core", + "serde", + "serde_json", + "sha2 0.10.9", + "sled", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tokio-test", + "tracing", + "uuid", +] + +[[package]] +name = "platform-e2e-tests" +version = "0.1.0" +dependencies = [ + "bincode", + "chrono", + "hex", + "lz4_flex", + "parking_lot 0.12.5", + "platform-bittensor", + "platform-challenge-sdk", + "platform-core", + "platform-p2p-consensus", + "platform-storage", + "rand 0.8.5", + "reqwest 0.12.25", + "serde", + "serde_json", + "sp-core 38.1.0", + "tempfile", + "tokio", + "uuid", +] + +[[package]] +name = "platform-epoch" +version = "0.1.0" +dependencies = [ + "async-trait", + "bincode", + "chrono", + "hex", + "parking_lot 0.12.5", + "platform-challenge-sdk", + "platform-core", + "serde", + "serde_json", + "sha2 0.10.9", + "thiserror 2.0.17", + "tokio", + "tracing", + "uuid", +] + +[[package]] +name = "platform-p2p-consensus" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "bincode", + "chrono", + "futures", + "hex", + "libp2p", + "parking_lot 0.12.5", + "platform-core", + "rand 0.8.5", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tokio-test", + "tracing", +] + +[[package]] +name = "platform-rpc" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "axum", + "bincode", + "chrono", + "hex", + "parking_lot 0.12.5", + "platform-challenge-sdk", + "platform-core", + "platform-subnet-manager", + "reqwest 0.12.25", + "serde", + "serde_json", + "sp-core 31.0.0", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tower 0.4.13", + "tower-http 0.5.2", + "tracing", + "uuid", +] + +[[package]] +name = "platform-storage" +version = "0.1.0" +dependencies = [ + "anyhow", + "bincode", + "chrono", + "hex", + "lz4_flex", + "parking_lot 0.12.5", + "platform-core", + "serde", + "serde_json", + "sha2 0.10.9", + "sled", + "tempfile", + "thiserror 2.0.17", + "tracing", + "uuid", +] + +[[package]] +name = "platform-subnet-manager" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "bincode", + "chrono", + "futures", + "hex", + "parking_lot 0.12.5", + "platform-challenge-sdk", + "platform-core", + "platform-storage", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tracing", + "uuid", +] + +[[package]] +name = "polkavm-common" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c99f7eee94e7be43ba37eef65ad0ee8cbaf89b7c00001c3f6d2be985cb1817" + +[[package]] +name = "polkavm-common" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49a5794b695626ba70d29e66e3f4f4835767452a6723f3a0bc20884b07088fe8" + +[[package]] +name = "polkavm-derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79fa916f7962348bd1bb1a65a83401675e6fc86c51a0fdbcf92a3108e58e6125" +dependencies = [ + "polkavm-derive-impl-macro 0.8.0", +] + +[[package]] +name = "polkavm-derive" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95282a203ae1f6828a04ff334145c3f6dc718bba6d3959805d273358b45eab93" +dependencies = [ + "polkavm-derive-impl-macro 0.26.0", +] + +[[package]] +name = "polkavm-derive-impl" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c10b2654a8a10a83c260bfb93e97b262cf0017494ab94a65d389e0eda6de6c9c" +dependencies = [ + "polkavm-common 0.8.0", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "polkavm-derive-impl" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6069dc7995cde6e612b868a02ce48b54397c6d2582bd1b97b63aabbe962cd779" +dependencies = [ + "polkavm-common 0.26.0", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "polkavm-derive-impl-macro" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15e85319a0d5129dc9f021c62607e0804f5fb777a05cdda44d750ac0732def66" +dependencies = [ + "polkavm-derive-impl 0.8.0", + "syn 2.0.111", +] + +[[package]] +name = "polkavm-derive-impl-macro" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581d34cafec741dc5ffafbb341933c205b6457f3d76257a9d99fb56687219c91" +dependencies = [ + "polkavm-derive-impl 0.26.0", + "syn 2.0.111", +] + +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi 0.5.2", + "pin-project-lite", + "rustix 1.1.2", + "windows-sys 0.61.2", +] + +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.111", +] + +[[package]] +name = "primitive-types" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" +dependencies = [ + "fixed-hash", + "impl-codec 0.6.0", + "impl-serde 0.4.0", + "scale-info", + "uint 0.9.5", +] + +[[package]] +name = "primitive-types" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d15600a7d856470b7d278b3fe0e311fe28c2526348549f8ef2ff7db3299c87f5" +dependencies = [ + "fixed-hash", + "impl-codec 0.7.1", + "impl-num-traits", + "impl-serde 0.5.0", + "scale-info", + "uint 0.10.0", +] + +[[package]] +name = "proc-macro-crate" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983" +dependencies = [ + "toml_edit 0.23.9", +] + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prometheus" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1" +dependencies = [ + "cfg-if", + "fnv", + "lazy_static", + "memchr", + "parking_lot 0.12.5", + "thiserror 1.0.69", +] + +[[package]] +name = "prometheus-client" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504ee9ff529add891127c4827eb481bd69dc0ebc72e9a682e187db4caa60c3ca" +dependencies = [ + "dtoa", + "itoa", + "parking_lot 0.12.5", + "prometheus-client-derive-encode", +] + +[[package]] +name = "prometheus-client-derive-encode" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "psm" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d11f2fedc3b7dafdc2851bc52f277377c5473d378859be234bc7ebb593144d01" +dependencies = [ + "ar_archive_writer", + "cc", +] + +[[package]] +name = "pulley-interpreter" +version = "41.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01051a5b172e07f9197b85060e6583b942aec679dac08416647bf7e7dc916b65" +dependencies = [ + "cranelift-bitset", + "log", + "pulley-macros", + "wasmtime-internal-math", +] + +[[package]] +name = "pulley-macros" +version = "41.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cf194f5b1a415ef3a44ee35056f4009092cc4038a9f7e3c7c1e392f48ee7dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "quick-protobuf" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6da84cc204722a989e01ba2f6e1e276e190f22263d0cb6ce8526fcdb0d2e1f" +dependencies = [ + "byteorder", +] + +[[package]] +name = "quick-protobuf-codec" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15a0580ab32b169745d7a39db2ba969226ca16738931be152a3209b409de2474" +dependencies = [ + "asynchronous-codec", + "bytes", + "quick-protobuf", + "thiserror 1.0.69", + "unsigned-varint 0.8.0", +] + +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases", + "futures-io", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2 0.6.1", + "thiserror 2.0.17", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" +dependencies = [ + "bytes", + "getrandom 0.3.4", + "lru-slab", + "rand 0.9.2", + "ring 0.17.14", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.17", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2 0.6.1", + "tracing", + "windows-sys 0.60.2", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_xoshiro" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] +name = "rayon" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "rcgen" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52c4f3084aa3bc7dfbba4eff4fab2a54db4324965d8872ab933565e6fbd83bc6" +dependencies = [ + "pem", + "ring 0.16.20", + "time", + "yasna", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.10.0", +] + +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.16", + "libredox", + "thiserror 1.0.69", +] + +[[package]] +name = "ref-cast" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f354300ae66f76f1c85c5f84693f0ce81d747e2c3f21a45fef496d89c960bf7d" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7186006dcb21920990093f30e3dea63b7d6e977bf1256be20c3563a5db070da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "regalloc2" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08effbc1fa53aaebff69521a5c05640523fab037b34a4a2c109506bc938246fa" +dependencies = [ + "allocator-api2", + "bumpalo", + "hashbrown 0.15.5", + "log", + "rustc-hash", + "smallvec", +] + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.13", + "regex-syntax 0.8.8", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.8", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.3.27", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.32", + "hyper-tls 0.5.0", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration 0.5.1", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "reqwest" +version = "0.12.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6eff9328d40131d43bd911d42d79eb6a47312002a4daefc9e37f17e74a7701a" +dependencies = [ + "base64 0.22.1", + "bytes", + "encoding_rs", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.4.12", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.8.1", + "hyper-rustls", + "hyper-tls 0.6.0", + "hyper-util", + "js-sys", + "log", + "mime", + "native-tls", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 1.0.2", + "tokio", + "tokio-native-tls", + "tokio-rustls", + "tower 0.5.2", + "tower-http 0.6.8", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "resolv-conf" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7" + +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac 0.12.1", + "subtle", +] + +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin 0.5.2", + "untrusted 0.7.1", + "web-sys", + "winapi", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.16", + "libc", + "untrusted 0.9.0", + "windows-sys 0.52.0", +] + +[[package]] +name = "rtnetlink" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a552eb82d19f38c3beed3f786bd23aa434ceb9ac43ab44419ca6d67a7e186c0" +dependencies = [ + "futures", + "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-packet-utils", + "netlink-proto", + "netlink-sys", + "nix", + "thiserror 1.0.69", + "tokio", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustc-hex" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rusticata-macros" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632" +dependencies = [ + "nom 7.1.3", +] + +[[package]] +name = "rustix" +version = "0.36.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "305efbd14fde4139eb501df5f136994bb520b033fa9fbdce287507dc23b8c7ed" +dependencies = [ + "bitflags 1.3.2", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys 0.1.4", + "windows-sys 0.45.0", +] + +[[package]] +name = "rustix" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +dependencies = [ + "bitflags 2.10.0", + "errno", + "libc", + "linux-raw-sys 0.11.0", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "533f54bc6a7d4f647e46ad909549eda97bf5afc1585190ef692b4286b198bd8f" +dependencies = [ + "log", + "once_cell", + "ring 0.17.14", + "rustls-pki-types", + "rustls-webpki 0.103.8", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9980d917ebb0c0536119ba501e90834767bffc3d60641457fd84a1f3fd337923" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework 3.5.1", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + +[[package]] +name = "rustls-pki-types" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "708c0f9d5f54ba0272468c1d306a52c495b31fa155e91bc25371e6df7996908c" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-platform-verifier" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19787cda76408ec5404443dc8b31795c87cd8fec49762dc75fa727740d34acc1" +dependencies = [ + "core-foundation 0.10.1", + "core-foundation-sys", + "jni", + "log", + "once_cell", + "rustls", + "rustls-native-certs", + "rustls-platform-verifier-android", + "rustls-webpki 0.103.8", + "security-framework 3.5.1", + "security-framework-sys", + "webpki-root-certs 0.26.11", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustls-platform-verifier-android" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f87165f0995f63a9fbeea62b64d10b4d9d8e78ec6d7d51fb2125fda7bb36788f" + +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring 0.17.14", + "untrusted 0.9.0", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ffdfa2f5286e2247234e03f680868ac2815974dc39e00ea15adc445d0aafe52" +dependencies = [ + "ring 0.17.14", + "rustls-pki-types", + "untrusted 0.9.0", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ruzstd" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ff0cc5e135c8870a775d3320910cd9b564ec036b4dc0b8741629020be63f01" + +[[package]] +name = "rw-stream-sink" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c9026ff5d2f23da5e45bbc283f156383001bfb09c4e44256d02c1a685fe9a1" +dependencies = [ + "futures", + "pin-project", + "static_assertions", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "scale-bits" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27243ab0d2d6235072b017839c5f0cd1a3b1ce45c0f7a715363b0c7d36c76c94" +dependencies = [ + "parity-scale-codec", + "scale-info", + "scale-type-resolver", + "serde", +] + +[[package]] +name = "scale-decode" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d6ed61699ad4d54101ab5a817169259b5b0efc08152f8632e61482d8a27ca3d" +dependencies = [ + "parity-scale-codec", + "primitive-types 0.13.1", + "scale-bits", + "scale-decode-derive", + "scale-type-resolver", + "smallvec", + "thiserror 2.0.17", +] + +[[package]] +name = "scale-decode-derive" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cb245f7fdb489e7ba43a616cbd34427fe3ba6fe0edc1d0d250085e6c84f3ec" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "scale-encode" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64901733157f9d25ef86843bd783eda439fac7efb0ad5a615d12d2cf3a29464b" +dependencies = [ + "parity-scale-codec", + "primitive-types 0.13.1", + "scale-bits", + "scale-encode-derive", + "scale-type-resolver", + "smallvec", + "thiserror 2.0.17", +] + +[[package]] +name = "scale-encode-derive" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78a3993a13b4eafa89350604672c8757b7ea84c7c5947d4b3691e3169c96379b" +dependencies = [ + "darling", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "scale-info" +version = "2.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346a3b32eba2640d17a9cb5927056b08f3de90f65b72fe09402c2ad07d684d0b" +dependencies = [ + "bitvec", + "cfg-if", + "derive_more 1.0.0", + "parity-scale-codec", + "scale-info-derive", + "serde", +] + +[[package]] +name = "scale-info-derive" +version = "2.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6630024bf739e2179b91fb424b28898baf819414262c5d376677dbff1fe7ebf" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "scale-type-resolver" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0cded6518aa0bd6c1be2b88ac81bf7044992f0f154bfbabd5ad34f43512abcb" +dependencies = [ + "scale-info", + "smallvec", +] + +[[package]] +name = "scale-typegen" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c61b6b706a3eaad63b506ab50a1d2319f817ae01cf753adcc3f055f9f0fcd6" +dependencies = [ + "proc-macro2", + "quote", + "scale-info", + "syn 2.0.111", + "thiserror 2.0.17", +] + +[[package]] +name = "scale-value" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884aab179aba344c67ddcd1d7dd8e3f8fee202f2e570d97ec34ec8688442a5b3" +dependencies = [ + "base58", + "blake2", + "either", + "parity-scale-codec", + "scale-bits", + "scale-decode", + "scale-encode", + "scale-type-resolver", + "serde", + "thiserror 2.0.17", + "yap", +] + +[[package]] +name = "schannel" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891d81b926048e76efe18581bf793546b4c0eaf8448d72be8de2bbee5fd166e1" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "schnellru" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "356285bbf17bea63d9e52e96bd18f039672ac92b55b8cb997d6162a2a37d1649" +dependencies = [ + "ahash 0.8.12", + "cfg-if", + "hashbrown 0.13.2", +] + +[[package]] +name = "schnorrkel" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9fcb6c2e176e86ec703e22560d99d65a5ee9056ae45a08e13e84ebf796296f" +dependencies = [ + "aead", + "arrayref", + "arrayvec 0.7.6", + "curve25519-dalek 4.1.3", + "getrandom_or_panic", + "merlin", + "rand_core 0.6.4", + "serde_bytes", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der", + "generic-array", + "pkcs8", + "serdect", + "subtle", + "zeroize", +] + +[[package]] +name = "secp256k1" +version = "0.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" +dependencies = [ + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb" +dependencies = [ + "cc", +] + +[[package]] +name = "secrecy" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e" +dependencies = [ + "zeroize", +] + +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.10.0", + "core-foundation 0.9.4", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3297343eaf830f66ede390ea39da1d462b6b0c1b000f420d0a83f898bbbe6ef" +dependencies = [ + "bitflags 2.10.0", + "core-foundation 0.10.1", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc1f0cbffaac4852523ce30d8bd3c5cdc873501d96ff467ca09b6767bb8cd5c0" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde_cbor" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" +dependencies = [ + "half", + "serde", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "serde_json" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", + "serde_core", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_spanned" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.12.1", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "serdect" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a84f14a19e9a014bb9f4512488d9829a68e04ecabffb0f9904cd1ace94598177" +dependencies = [ + "base16ct", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] + +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest 0.10.7", + "keccak", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7664a098b8e616bdfcc2dc0e9ac44eb231eedf41db4e9fe95d8d32ec728dedad" +dependencies = [ + "libc", +] + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest 0.10.7", + "rand_core 0.6.4", +] + +[[package]] +name = "simple-mermaid" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "620a1d43d70e142b1d46a929af51d44f383db9c7a2ec122de2cd992ccfcf3c18" + +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + +[[package]] +name = "sized-chunks" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e" +dependencies = [ + "bitmaps", + "typenum", +] + +[[package]] +name = "slab" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589" + +[[package]] +name = "sled" +version = "0.34.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f96b4737c2ce5987354855aed3797279def4ebf734436c6aa4552cf8e169935" +dependencies = [ + "crc32fast", + "crossbeam-epoch", + "crossbeam-utils", + "fs2", + "fxhash", + "libc", + "log", + "parking_lot 0.11.2", +] + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +dependencies = [ + "serde", +] + +[[package]] +name = "smol" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33bd3e260892199c3ccfc487c88b2da2265080acb316cd920da72fdfd7c599f" +dependencies = [ + "async-channel", + "async-executor", + "async-fs", + "async-io", + "async-lock", + "async-net", + "async-process", + "blocking", + "futures-lite", +] + +[[package]] +name = "smoldot" +version = "0.19.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16e5723359f0048bf64bfdfba64e5732a56847d42c4fd3fe56f18280c813413" +dependencies = [ + "arrayvec 0.7.6", + "async-lock", + "atomic-take", + "base64 0.22.1", + "bip39", + "blake2-rfc", + "bs58", + "chacha20", + "crossbeam-queue", + "derive_more 2.1.0", + "ed25519-zebra 4.1.0", + "either", + "event-listener", + "fnv", + "futures-lite", + "futures-util", + "hashbrown 0.15.5", + "hex", + "hmac 0.12.1", + "itertools 0.14.0", + "libm", + "libsecp256k1", + "merlin", + "nom 8.0.0", + "num-bigint", + "num-rational", + "num-traits", + "pbkdf2", + "pin-project", + "poly1305", + "rand 0.8.5", + "rand_chacha 0.3.1", + "ruzstd", + "schnorrkel", + "serde", + "serde_json", + "sha2 0.10.9", + "sha3", + "siphasher", + "slab", + "smallvec", + "soketto", + "twox-hash 2.1.2", + "wasmi", + "x25519-dalek", + "zeroize", +] + +[[package]] +name = "smoldot-light" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bba9e591716567d704a8252feeb2f1261a286e1e2cbdd4e49e9197c34a14e2" +dependencies = [ + "async-channel", + "async-lock", + "base64 0.22.1", + "blake2-rfc", + "bs58", + "derive_more 2.1.0", + "either", + "event-listener", + "fnv", + "futures-channel", + "futures-lite", + "futures-util", + "hashbrown 0.15.5", + "hex", + "itertools 0.14.0", + "log", + "lru", + "parking_lot 0.12.5", + "pin-project", + "rand 0.8.5", + "rand_chacha 0.3.1", + "serde", + "serde_json", + "siphasher", + "slab", + "smol", + "smoldot", + "zeroize", +] + +[[package]] +name = "snow" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "850948bee068e713b8ab860fe1adc4d109676ab4c3b621fd8147f06b261f2f85" +dependencies = [ + "aes-gcm", + "blake2", + "chacha20poly1305", + "curve25519-dalek 4.1.3", + "rand_core 0.6.4", + "ring 0.17.14", + "rustc_version", + "sha2 0.10.9", + "subtle", +] + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "socket2" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] +name = "soketto" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e859df029d160cb88608f5d7df7fb4753fd20fdfb4de5644f3d8b8440841721" +dependencies = [ + "base64 0.22.1", + "bytes", + "futures", + "httparse", + "log", + "rand 0.8.5", + "sha1", +] + +[[package]] +name = "sp-application-crypto" +version = "43.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6067f30cf3fb9270471cf24a65d73b33330f32573abab2d97196f83fc076de0" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 38.1.0", + "sp-io", +] + +[[package]] +name = "sp-arithmetic" +version = "28.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5f4755af7cc57f4a2a830e134b403fc832caa5d93dacb970ffc7ac717f38c40" +dependencies = [ + "docify", + "integer-sqrt", + "num-traits", + "parity-scale-codec", + "scale-info", + "serde", + "static_assertions", +] + +[[package]] +name = "sp-core" +version = "31.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d7a0fd8f16dcc3761198fc83be12872f823b37b749bc72a3a6a1f702509366" +dependencies = [ + "array-bytes", + "bitflags 1.3.2", + "blake2", + "bounded-collections 0.2.4", + "bs58", + "dyn-clonable", + "ed25519-zebra 3.1.0", + "futures", + "hash-db", + "hash256-std-hasher", + "impl-serde 0.4.0", + "itertools 0.10.5", + "k256", + "libsecp256k1", + "log", + "merlin", + "parity-bip39", + "parity-scale-codec", + "parking_lot 0.12.5", + "paste", + "primitive-types 0.12.2", + "rand 0.8.5", + "scale-info", + "schnorrkel", + "secp256k1", + "secrecy", + "serde", + "sp-crypto-hashing", + "sp-debug-derive", + "sp-externalities 0.27.0", + "sp-runtime-interface 26.0.0", + "sp-std", + "sp-storage 20.0.0", + "ss58-registry", + "substrate-bip39 0.5.0", + "thiserror 1.0.69", + "tracing", + "w3f-bls 0.1.9", + "zeroize", +] + +[[package]] +name = "sp-core" +version = "38.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707602208776d0e19d4269bb3f68c5306cacbdfabbb2e4d8d499af7b907bb0a3" +dependencies = [ + "ark-vrf", + "array-bytes", + "bitflags 1.3.2", + "blake2", + "bounded-collections 0.3.2", + "bs58", + "dyn-clone", + "ed25519-zebra 4.1.0", + "futures", + "hash-db", + "hash256-std-hasher", + "impl-serde 0.5.0", + "itertools 0.11.0", + "k256", + "libsecp256k1", + "log", + "merlin", + "parity-bip39", + "parity-scale-codec", + "parking_lot 0.12.5", + "paste", + "primitive-types 0.13.1", + "rand 0.8.5", + "scale-info", + "schnorrkel", + "secp256k1", + "secrecy", + "serde", + "sha2 0.10.9", + "sp-crypto-hashing", + "sp-debug-derive", + "sp-externalities 0.30.0", + "sp-std", + "sp-storage 22.0.0", + "ss58-registry", + "substrate-bip39 0.6.0", + "thiserror 1.0.69", + "tracing", + "w3f-bls 0.1.9", + "zeroize", +] + +[[package]] +name = "sp-crypto-hashing" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9927a7f81334ed5b8a98a4a978c81324d12bd9713ec76b5c68fd410174c5eb" +dependencies = [ + "blake2b_simd", + "byteorder", + "digest 0.10.7", + "sha2 0.10.9", + "sha3", + "twox-hash 1.6.3", +] + +[[package]] +name = "sp-debug-derive" +version = "14.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48d09fa0a5f7299fb81ee25ae3853d26200f7a348148aed6de76be905c007dbe" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "sp-externalities" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d6a4572eadd4a63cff92509a210bf425501a0c5e76574b30a366ac77653787" +dependencies = [ + "environmental", + "parity-scale-codec", + "sp-std", + "sp-storage 20.0.0", +] + +[[package]] +name = "sp-externalities" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30cbf059dce180a8bf8b6c8b08b6290fa3d1c7f069a60f1df038ab5dd5fc0ba6" +dependencies = [ + "environmental", + "parity-scale-codec", + "sp-storage 22.0.0", +] + +[[package]] +name = "sp-io" +version = "43.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2059e3b338c0174e8dc9e144cc7e612165ca4c960c3a23c6c99c29ef34768f" +dependencies = [ + "bytes", + "docify", + "ed25519-dalek", + "libsecp256k1", + "log", + "parity-scale-codec", + "polkavm-derive 0.26.0", + "rustversion", + "secp256k1", + "sp-core 38.1.0", + "sp-crypto-hashing", + "sp-externalities 0.30.0", + "sp-keystore", + "sp-runtime-interface 32.0.0", + "sp-state-machine", + "sp-tracing 19.0.0", + "sp-trie", + "tracing", + "tracing-core", +] + +[[package]] +name = "sp-keystore" +version = "0.44.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a5c0b829014afc22e992be2c198f2677592db43267fc218e9f3207dbbfb6fbb" +dependencies = [ + "parity-scale-codec", + "parking_lot 0.12.5", + "sp-core 38.1.0", + "sp-externalities 0.30.0", +] + +[[package]] +name = "sp-panic-handler" +version = "13.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8b52e69a577cbfdea62bfaf16f59eb884422ce98f78b5cd8d9bf668776bced1" +dependencies = [ + "backtrace", + "regex", +] + +[[package]] +name = "sp-runtime" +version = "44.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee57bb77e94c26306501426ac82aca401bb80ee2279ecdba148f68e76cf58247" +dependencies = [ + "binary-merkle-tree", + "docify", + "either", + "hash256-std-hasher", + "impl-trait-for-tuples", + "log", + "num-traits", + "parity-scale-codec", + "paste", + "rand 0.8.5", + "scale-info", + "serde", + "simple-mermaid", + "sp-application-crypto", + "sp-arithmetic", + "sp-core 38.1.0", + "sp-io", + "sp-std", + "sp-trie", + "sp-weights", + "tracing", + "tuplex", +] + +[[package]] +name = "sp-runtime-interface" +version = "26.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a675ea4858333d4d755899ed5ed780174aa34fec15953428d516af5452295" +dependencies = [ + "bytes", + "impl-trait-for-tuples", + "parity-scale-codec", + "polkavm-derive 0.8.0", + "primitive-types 0.12.2", + "sp-externalities 0.27.0", + "sp-runtime-interface-proc-macro 18.0.0", + "sp-std", + "sp-storage 20.0.0", + "sp-tracing 16.0.0", + "sp-wasm-interface 20.0.0", + "static_assertions", +] + +[[package]] +name = "sp-runtime-interface" +version = "32.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efdc2bc2adbfb9b4396ae07c7d94db20414d2351608e29e1f44e4f643b387c70" +dependencies = [ + "bytes", + "impl-trait-for-tuples", + "parity-scale-codec", + "polkavm-derive 0.26.0", + "sp-externalities 0.30.0", + "sp-runtime-interface-proc-macro 20.0.0", + "sp-std", + "sp-storage 22.0.0", + "sp-tracing 19.0.0", + "sp-wasm-interface 24.0.0", + "static_assertions", +] + +[[package]] +name = "sp-runtime-interface-proc-macro" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0195f32c628fee3ce1dfbbf2e7e52a30ea85f3589da9fe62a8b816d70fc06294" +dependencies = [ + "Inflector", + "expander", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "sp-runtime-interface-proc-macro" +version = "20.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04178084ae654b3924934a56943ee73e3562db4d277e948393561b08c3b5b5fe" +dependencies = [ + "Inflector", + "expander", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.111", +] + +[[package]] +name = "sp-state-machine" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "042677239cca40eb6a0d70e0b220f5693516f59853c2d678de471a79652cd16e" +dependencies = [ + "hash-db", + "log", + "parity-scale-codec", + "parking_lot 0.12.5", + "rand 0.8.5", + "smallvec", + "sp-core 38.1.0", + "sp-externalities 0.30.0", + "sp-panic-handler", + "sp-trie", + "thiserror 1.0.69", + "tracing", + "trie-db", +] + +[[package]] +name = "sp-std" +version = "14.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f8ee986414b0a9ad741776762f4083cd3a5128449b982a3919c4df36874834" + +[[package]] +name = "sp-storage" +version = "20.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dba5791cb3978e95daf99dad919ecb3ec35565604e88cd38d805d9d4981e8bd" +dependencies = [ + "impl-serde 0.4.0", + "parity-scale-codec", + "ref-cast", + "serde", + "sp-debug-derive", + "sp-std", +] + +[[package]] +name = "sp-storage" +version = "22.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee3b70ca340e41cde9d2e069d354508a6e37a6573d66f7cc38f11549002f64ec" +dependencies = [ + "impl-serde 0.5.0", + "parity-scale-codec", + "ref-cast", + "serde", + "sp-debug-derive", +] + +[[package]] +name = "sp-tracing" +version = "16.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0351810b9d074df71c4514c5228ed05c250607cba131c1c9d1526760ab69c05c" +dependencies = [ + "parity-scale-codec", + "sp-std", + "tracing", + "tracing-core", + "tracing-subscriber 0.2.25", +] + +[[package]] +name = "sp-tracing" +version = "19.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2c7372456c39cc81e15befe54d0caab8378f2b30fd34d1bcb5f0f56631c6b6e" +dependencies = [ + "parity-scale-codec", + "regex", + "tracing", + "tracing-core", + "tracing-subscriber 0.3.22", +] + +[[package]] +name = "sp-trie" +version = "41.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd2a05942903900c23aaa5fded094fa8186523e646ae8874bff3fce74985d0e5" +dependencies = [ + "ahash 0.8.12", + "foldhash", + "hash-db", + "hashbrown 0.15.5", + "memory-db", + "nohash-hasher", + "parity-scale-codec", + "parking_lot 0.12.5", + "rand 0.8.5", + "scale-info", + "schnellru", + "sp-core 38.1.0", + "sp-externalities 0.30.0", + "substrate-prometheus-endpoint", + "thiserror 1.0.69", + "tracing", + "trie-db", + "trie-root", +] + +[[package]] +name = "sp-wasm-interface" +version = "20.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ef97172c42eb4c6c26506f325f48463e9bc29b2034a587f1b9e48c751229bee" +dependencies = [ + "anyhow", + "impl-trait-for-tuples", + "log", + "parity-scale-codec", + "sp-std", + "wasmtime 8.0.1", +] + +[[package]] +name = "sp-wasm-interface" +version = "24.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd177d0658f3df0492f28bd39d665133a7868db5aa66c8642c949b6265430719" +dependencies = [ + "anyhow", + "impl-trait-for-tuples", + "log", + "parity-scale-codec", +] + +[[package]] +name = "sp-weights" +version = "33.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beb3f1b1373a0926b44ddabfa55a608ea78c20ee356f35575c031db2f0202545" +dependencies = [ + "bounded-collections 0.3.2", + "parity-scale-codec", + "scale-info", + "serde", + "smallvec", + "sp-arithmetic", + "sp-debug-derive", +] + +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "ss58-registry" +version = "1.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19409f13998e55816d1c728395af0b52ec066206341d939e22e7766df9b494b8" +dependencies = [ + "Inflector", + "num-format", + "proc-macro2", + "quote", + "serde", + "serde_json", + "unicode-xid", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "substrate-bip39" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2b564c293e6194e8b222e52436bcb99f60de72043c7f845cf6c4406db4df121" +dependencies = [ + "hmac 0.12.1", + "pbkdf2", + "schnorrkel", + "sha2 0.10.9", + "zeroize", +] + +[[package]] +name = "substrate-bip39" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca58ffd742f693dc13d69bdbb2e642ae239e0053f6aab3b104252892f856700a" +dependencies = [ + "hmac 0.12.1", + "pbkdf2", + "schnorrkel", + "sha2 0.10.9", + "zeroize", +] + +[[package]] +name = "substrate-prometheus-endpoint" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d23e4bc8e910a312820d589047ab683928b761242dbe31dee081fbdb37cbe0be" +dependencies = [ + "http-body-util", + "hyper 1.8.1", + "hyper-util", + "log", + "prometheus", + "thiserror 1.0.69", + "tokio", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "subxt" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddbf938ac1d86a361a84709a71cdbae5d87f370770b563651d1ec052eed9d0b4" +dependencies = [ + "async-trait", + "derive-where", + "either", + "frame-metadata", + "futures", + "hex", + "jsonrpsee", + "parity-scale-codec", + "primitive-types 0.13.1", + "scale-bits", + "scale-decode", + "scale-encode", + "scale-info", + "scale-value", + "serde", + "serde_json", + "sp-crypto-hashing", + "subxt-core", + "subxt-lightclient", + "subxt-macro", + "subxt-metadata", + "subxt-rpcs", + "thiserror 2.0.17", + "tokio", + "tokio-util", + "tracing", + "url", + "wasm-bindgen-futures", + "web-time", +] + +[[package]] +name = "subxt-codegen" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c250ad8cd102d40ae47977b03295a2ff791375f30ddc7474d399fb56efb793b" +dependencies = [ + "heck", + "parity-scale-codec", + "proc-macro2", + "quote", + "scale-info", + "scale-typegen", + "subxt-metadata", + "syn 2.0.111", + "thiserror 2.0.17", +] + +[[package]] +name = "subxt-core" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5705c5b420294524e41349bf23c6b11aa474ce731de7317f4153390e1927f702" +dependencies = [ + "base58", + "blake2", + "derive-where", + "frame-decode", + "frame-metadata", + "hashbrown 0.14.5", + "hex", + "impl-serde 0.5.0", + "keccak-hash", + "parity-scale-codec", + "primitive-types 0.13.1", + "scale-bits", + "scale-decode", + "scale-encode", + "scale-info", + "scale-value", + "serde", + "serde_json", + "sp-crypto-hashing", + "subxt-metadata", + "thiserror 2.0.17", + "tracing", +] + +[[package]] +name = "subxt-lightclient" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e02732a6c9ae46bc282c1a741b3d3e494021b3e87e7e92cfb3620116d92911" +dependencies = [ + "futures", + "futures-util", + "serde", + "serde_json", + "smoldot-light", + "thiserror 2.0.17", + "tokio", + "tokio-stream", + "tracing", +] + +[[package]] +name = "subxt-macro" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501bf358698f5ab02a6199a1fcd3f1b482e2f5b6eb5d185411e6a74a175ec8e8" +dependencies = [ + "darling", + "parity-scale-codec", + "proc-macro-error2", + "quote", + "scale-typegen", + "subxt-codegen", + "subxt-metadata", + "subxt-utils-fetchmetadata", + "syn 2.0.111", +] + +[[package]] +name = "subxt-metadata" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01fb7c0bfafad78dda7084c6a2444444744af3bbf7b2502399198b9b4c20eddf" +dependencies = [ + "frame-decode", + "frame-metadata", + "hashbrown 0.14.5", + "parity-scale-codec", + "scale-info", + "sp-crypto-hashing", + "thiserror 2.0.17", +] + +[[package]] +name = "subxt-rpcs" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab68a9c20ecedb0cb7d62d64f884e6add91bb70485783bf40aa8eac5c389c6e0" +dependencies = [ + "derive-where", + "frame-metadata", + "futures", + "hex", + "impl-serde 0.5.0", + "jsonrpsee", + "parity-scale-codec", + "primitive-types 0.13.1", + "serde", + "serde_json", + "subxt-core", + "subxt-lightclient", + "thiserror 2.0.17", + "tokio-util", + "tracing", + "url", +] + +[[package]] +name = "subxt-utils-fetchmetadata" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e450f6812a653c5a3e63a079aa3b60a3f4c362722753c3222286eaa1800f9002" +dependencies = [ + "hex", + "parity-scale-codec", + "thiserror 2.0.17", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "getrandom" -version = "0.3.4" +name = "system-configuration" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ - "cfg-if", - "js-sys", - "libc", - "r-efi", - "wasip2", - "wasm-bindgen", + "bitflags 1.3.2", + "core-foundation 0.9.4", + "system-configuration-sys 0.5.0", ] [[package]] -name = "hashbrown" -version = "0.15.5" +name = "system-configuration" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" dependencies = [ - "allocator-api2", - "equivalent", - "foldhash", + "bitflags 2.10.0", + "core-foundation 0.9.4", + "system-configuration-sys 0.6.0", ] [[package]] -name = "heck" +name = "system-configuration-sys" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] [[package]] -name = "http" -version = "1.4.0" +name = "system-configuration-sys" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" dependencies = [ - "bytes", - "itoa", + "core-foundation-sys", + "libc", ] [[package]] -name = "http-body" +name = "tap" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" -dependencies = [ - "bytes", - "http", -] +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] -name = "http-body-util" -version = "0.1.3" +name = "target-lexicon" +version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" -dependencies = [ - "bytes", - "futures-core", - "http", - "http-body", - "pin-project-lite", -] +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] -name = "httparse" -version = "1.10.1" +name = "target-lexicon" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +checksum = "b1dd07eb858a2067e2f3c7155d54e929265c264e6f37efe3ee7a8d1b5a1dd0ba" [[package]] -name = "hyper" -version = "1.8.1" +name = "tempfile" +version = "3.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" dependencies = [ - "atomic-waker", - "bytes", - "futures-channel", - "futures-core", - "http", - "http-body", - "httparse", - "itoa", - "pin-project-lite", - "pin-utils", - "smallvec", - "tokio", - "want", + "fastrand", + "getrandom 0.3.4", + "once_cell", + "rustix 1.1.2", + "windows-sys 0.61.2", ] [[package]] -name = "hyper-rustls" -version = "0.27.7" +name = "termcolor" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ - "http", - "hyper", - "hyper-util", - "rustls", - "rustls-pki-types", - "tokio", - "tokio-rustls", - "tower-service", - "webpki-roots", + "winapi-util", ] [[package]] -name = "hyper-util" -version = "0.1.20" +name = "thiserror" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "base64", - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "hyper", - "ipnet", - "libc", - "percent-encoding", - "pin-project-lite", - "socket2", - "tokio", - "tower-service", - "tracing", + "thiserror-impl 1.0.69", ] [[package]] -name = "iana-time-zone" -version = "0.1.65" +name = "thiserror" +version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", + "thiserror-impl 2.0.17", ] [[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" +name = "thiserror-impl" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ - "cc", + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "icu_collections" -version = "2.1.1" +name = "thiserror-impl" +version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" dependencies = [ - "displaydoc", - "potential_utf", - "yoke", - "zerofrom", - "zerovec", + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "icu_locale_core" -version = "2.1.1" +name = "thread_local" +version = "1.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" dependencies = [ - "displaydoc", - "litemap", - "tinystr", - "writeable", - "zerovec", + "cfg-if", ] [[package]] -name = "icu_normalizer" -version = "2.1.1" +name = "time" +version = "0.3.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" dependencies = [ - "icu_collections", - "icu_normalizer_data", - "icu_properties", - "icu_provider", - "smallvec", - "zerovec", + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", ] [[package]] -name = "icu_normalizer_data" -version = "2.1.1" +name = "time-core" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" +checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b" [[package]] -name = "icu_properties" -version = "2.1.2" +name = "time-macros" +version = "0.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +checksum = "30cfb0125f12d9c277f35663a0a33f8c30190f4e4574868a330595412d34ebf3" dependencies = [ - "icu_collections", - "icu_locale_core", - "icu_properties_data", - "icu_provider", - "zerotrie", - "zerovec", + "num-conv", + "time-core", ] [[package]] -name = "icu_properties_data" -version = "2.1.2" +name = "tiny-keccak" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] [[package]] -name = "icu_provider" -version = "2.1.1" +name = "tinystr" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" dependencies = [ "displaydoc", - "icu_locale_core", - "writeable", - "yoke", - "zerofrom", - "zerotrie", "zerovec", ] [[package]] -name = "ident_case" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" - -[[package]] -name = "idna" -version = "1.1.0" +name = "tinyvec" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" dependencies = [ - "idna_adapter", - "smallvec", - "utf8_iter", + "tinyvec_macros", ] [[package]] -name = "idna_adapter" -version = "1.2.1" +name = "tinyvec_macros" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" -dependencies = [ - "icu_normalizer", - "icu_properties", +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tle" +version = "0.1.0" +source = "git+https://github.com/ideal-lab5/timelock?rev=5416406cfd32799e31e1795393d4916894de4468#5416406cfd32799e31e1795393d4916894de4468" +dependencies = [ + "aes-gcm", + "ark-bls12-377", + "ark-bls12-381 0.4.0", + "ark-ec 0.4.2", + "ark-ff 0.4.2", + "ark-poly 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "array-bytes", + "chacha20poly1305", + "generic-array", + "parity-scale-codec", + "rand_chacha 0.3.1", + "rand_core 0.6.4", + "scale-info", + "serde", + "serde_cbor", + "serde_json", + "sha2 0.10.9", + "sha3", + "w3f-bls 0.1.3", ] [[package]] -name = "indoc" -version = "2.0.7" +name = "tokio" +version = "1.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" dependencies = [ - "rustversion", + "bytes", + "libc", + "mio", + "parking_lot 0.12.5", + "pin-project-lite", + "signal-hook-registry", + "socket2 0.6.1", + "tokio-macros", + "windows-sys 0.61.2", ] [[package]] -name = "instability" -version = "0.3.11" +name = "tokio-macros" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357b7205c6cd18dd2c86ed312d1e70add149aea98e7ef72b9fdf0270e555c11d" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" dependencies = [ - "darling", - "indoc", "proc-macro2", "quote", - "syn", + "syn 2.0.111", ] [[package]] -name = "ipnet" -version = "2.11.0" +name = "tokio-native-tls" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] [[package]] -name = "iri-string" -version = "0.7.10" +name = "tokio-rustls" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c91338f0783edbd6195decb37bae672fd3b165faffb89bf7b9e6942f8b1a731a" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" dependencies = [ - "memchr", - "serde", + "rustls", + "tokio", ] [[package]] -name = "is_terminal_polyfill" -version = "1.70.2" +name = "tokio-stream" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" +checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] [[package]] -name = "itertools" -version = "0.13.0" +name = "tokio-test" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7" dependencies = [ - "either", + "async-stream", + "bytes", + "futures-core", + "tokio", + "tokio-stream", ] [[package]] -name = "itoa" -version = "1.0.17" +name = "tokio-tungstenite" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" +checksum = "edc5f74e248dc973e0dbb7b74c7e0d6fcc301c694ff50049504004ef4d0cdcd9" +dependencies = [ + "futures-util", + "log", + "native-tls", + "tokio", + "tokio-native-tls", + "tungstenite", +] [[package]] -name = "js-sys" -version = "0.3.85" +name = "tokio-util" +version = "0.7.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +checksum = "2efa149fe76073d6e8fd97ef4f4eca7b67f599660115591483572e406e165594" dependencies = [ - "once_cell", - "wasm-bindgen", + "bytes", + "futures-core", + "futures-io", + "futures-sink", + "pin-project-lite", + "tokio", ] [[package]] -name = "lazy_static" -version = "1.5.0" +name = "toml" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_edit 0.22.27", +] [[package]] -name = "libc" -version = "0.2.182" +name = "toml" +version = "0.9.12+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" +checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863" +dependencies = [ + "indexmap 2.12.1", + "serde_core", + "serde_spanned 1.0.4", + "toml_datetime 0.7.5+spec-1.1.0", + "toml_parser", + "toml_writer", + "winnow", +] [[package]] -name = "linux-raw-sys" -version = "0.4.15" +name = "toml_datetime" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] [[package]] -name = "litemap" -version = "0.8.1" +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] [[package]] -name = "lock_api" -version = "0.4.14" +name = "toml_edit" +version = "0.22.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" dependencies = [ - "scopeguard", + "indexmap 2.12.1", + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_write", + "winnow", ] [[package]] -name = "log" -version = "0.4.29" +name = "toml_edit" +version = "0.23.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +checksum = "5d7cbc3b4b49633d57a0509303158ca50de80ae32c265093b24c414705807832" +dependencies = [ + "indexmap 2.12.1", + "toml_datetime 0.7.5+spec-1.1.0", + "toml_parser", + "winnow", +] [[package]] -name = "lru" -version = "0.12.5" +name = "toml_parser" +version = "1.0.8+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +checksum = "0742ff5ff03ea7e67c8ae6c93cac239e0d9784833362da3f9a9c1da8dfefcbdc" dependencies = [ - "hashbrown", + "winnow", ] [[package]] -name = "lru-slab" +name = "toml_write" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" [[package]] -name = "matchers" -version = "0.2.0" +name = "toml_writer" +version = "1.0.6+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" -dependencies = [ - "regex-automata", -] +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" [[package]] -name = "memchr" -version = "2.8.0" +name = "tower" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "tower-layer", + "tower-service", + "tracing", +] [[package]] -name = "mio" -version = "1.1.1" +name = "tower" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ - "libc", - "log", - "wasi", - "windows-sys 0.61.2", + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 1.0.2", + "tokio", + "tower-layer", + "tower-service", + "tracing", ] [[package]] -name = "nu-ansi-term" -version = "0.50.3" +name = "tower-http" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" dependencies = [ - "windows-sys 0.61.2", + "bitflags 2.10.0", + "bytes", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", ] [[package]] -name = "num-traits" -version = "0.2.19" +name = "tower-http" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8" dependencies = [ - "autocfg", + "bitflags 2.10.0", + "bytes", + "futures-util", + "http 1.4.0", + "http-body 1.0.1", + "iri-string", + "pin-project-lite", + "tower 0.5.2", + "tower-layer", + "tower-service", + "tracing", ] [[package]] -name = "once_cell" -version = "1.21.3" +name = "tower-layer" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" [[package]] -name = "once_cell_polyfill" -version = "1.70.2" +name = "tower-service" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] -name = "parking_lot" -version = "0.12.5" +name = "tracing" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647" dependencies = [ - "lock_api", - "parking_lot_core", + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", ] [[package]] -name = "parking_lot_core" -version = "0.9.12" +name = "tracing-attributes" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-link", + "proc-macro2", + "quote", + "syn 2.0.111", ] [[package]] -name = "paste" -version = "1.0.15" +name = "tracing-core" +version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c" +dependencies = [ + "once_cell", + "valuable", +] [[package]] -name = "percent-encoding" -version = "2.3.2" +name = "tracing-log" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +checksum = "f751112709b4e791d8ce53e32c4ed2d353565a795ce84da2285393f41557bdf2" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] [[package]] -name = "pin-project-lite" -version = "0.2.16" +name = "tracing-log" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] [[package]] -name = "pin-utils" -version = "0.1.0" +name = "tracing-serde" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "platform-challenge-sdk-wasm" -version = "0.1.0" -source = "git+https://github.com/PlatformNetwork/platform-v2?branch=main#ae9b049a3abbaf9808aca38fdc8a782f813ce734" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" dependencies = [ - "bincode", "serde", + "tracing-core", ] [[package]] -name = "potential_utf" -version = "0.1.4" +name = "tracing-subscriber" +version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" dependencies = [ - "zerovec", + "ansi_term", + "chrono", + "lazy_static", + "matchers 0.0.1", + "regex", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log 0.1.4", + "tracing-serde", ] [[package]] -name = "ppv-lite86" -version = "0.2.21" +name = "tracing-subscriber" +version = "0.3.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" dependencies = [ - "zerocopy", + "matchers 0.2.0", + "nu-ansi-term", + "once_cell", + "regex-automata 0.4.13", + "sharded-slab", + "smallvec", + "thread_local", + "time", + "tracing", + "tracing-core", + "tracing-log 0.2.0", ] [[package]] -name = "proc-macro2" -version = "1.0.106" +name = "trie-db" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +checksum = "6c0670ab45a6b7002c7df369fee950a27cf29ae0474343fd3a15aa15f691e7a6" dependencies = [ - "unicode-ident", + "hash-db", + "log", + "rustc-hex", + "smallvec", ] [[package]] -name = "quinn" -version = "0.11.9" +name = "trie-root" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +checksum = "d4ed310ef5ab98f5fa467900ed906cb9232dd5376597e00fd4cba2a449d06c0b" dependencies = [ - "bytes", - "cfg_aliases", - "pin-project-lite", - "quinn-proto", - "quinn-udp", - "rustc-hash", - "rustls", - "socket2", - "thiserror", - "tokio", - "tracing", - "web-time", + "hash-db", ] [[package]] -name = "quinn-proto" -version = "0.11.13" +name = "trust-dns-proto" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" +checksum = "3119112651c157f4488931a01e586aa459736e9d6046d3bd9105ffb69352d374" dependencies = [ - "bytes", - "getrandom 0.3.4", - "lru-slab", - "rand", - "ring", - "rustc-hash", - "rustls", - "rustls-pki-types", - "slab", - "thiserror", + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna 0.4.0", + "ipnet", + "once_cell", + "rand 0.8.5", + "smallvec", + "thiserror 1.0.69", "tinyvec", + "tokio", "tracing", - "web-time", + "url", ] [[package]] -name = "quinn-udp" -version = "0.5.14" +name = "trust-dns-resolver" +version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +checksum = "10a3e6c3aff1718b3c73e395d1f35202ba2ffa847c6a62eea0db8fb4cfe30be6" dependencies = [ - "cfg_aliases", - "libc", + "cfg-if", + "futures-util", + "ipconfig", + "lru-cache", "once_cell", - "socket2", + "parking_lot 0.12.5", + "rand 0.8.5", + "resolv-conf", + "smallvec", + "thiserror 1.0.69", + "tokio", "tracing", - "windows-sys 0.52.0", + "trust-dns-proto", ] [[package]] -name = "quote" -version = "1.0.44" +name = "try-lock" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18e5b8366ee7a95b16d32197d0b2604b43a0be89dc5fac9f8e96ccafbaedda8a" dependencies = [ - "proc-macro2", + "byteorder", + "bytes", + "data-encoding", + "http 1.4.0", + "httparse", + "log", + "native-tls", + "rand 0.8.5", + "sha1", + "thiserror 1.0.69", + "utf-8", ] [[package]] -name = "r-efi" -version = "5.3.0" +name = "tuplex" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +checksum = "676ac81d5454c4dcf37955d34fa8626ede3490f744b86ca14a7b90168d2a08aa" [[package]] -name = "rand" -version = "0.9.2" +name = "twox-hash" +version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "rand_chacha", - "rand_core", + "cfg-if", + "digest 0.10.7", + "rand 0.8.5", + "static_assertions", ] [[package]] -name = "rand_chacha" -version = "0.9.0" +name = "twox-hash" +version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" -dependencies = [ - "ppv-lite86", - "rand_core", -] +checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" [[package]] -name = "rand_core" -version = "0.9.5" +name = "typenum" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" -dependencies = [ - "getrandom 0.3.4", -] +checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] -name = "ratatui" -version = "0.29.0" +name = "uint" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b" +checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52" dependencies = [ - "bitflags", - "cassowary", - "compact_str", - "crossterm", - "indoc", - "instability", - "itertools", - "lru", - "paste", - "strum", - "unicode-segmentation", - "unicode-truncate", - "unicode-width 0.2.0", + "byteorder", + "crunchy", + "hex", + "static_assertions", ] [[package]] -name = "redox_syscall" -version = "0.5.18" +name = "uint" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +checksum = "909988d098b2f738727b161a106cfc7cab00c539c2687a8836f8e565976fb53e" dependencies = [ - "bitflags", + "byteorder", + "crunchy", + "hex", + "static_assertions", ] [[package]] -name = "regex-automata" -version = "0.4.14" +name = "unicode-bidi" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" [[package]] -name = "regex-syntax" -version = "0.8.9" +name = "unicode-ident" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] -name = "reqwest" -version = "0.12.28" +name = "unicode-normalization" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" dependencies = [ - "base64", - "bytes", - "futures-core", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-rustls", - "hyper-util", - "js-sys", - "log", - "percent-encoding", - "pin-project-lite", - "quinn", - "rustls", - "rustls-pki-types", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper", - "tokio", - "tokio-rustls", - "tower", - "tower-http", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots", + "tinyvec", ] [[package]] -name = "ring" -version = "0.17.14" +name = "unicode-segmentation" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" -dependencies = [ - "cc", - "cfg-if", - "getrandom 0.2.17", - "libc", - "untrusted", - "windows-sys 0.52.0", -] +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" [[package]] -name = "rustc-hash" -version = "2.1.1" +name = "unicode-width" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" [[package]] -name = "rustix" -version = "0.38.44" +name = "unicode-xid" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.59.0", -] +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] -name = "rustls" -version = "0.23.36" +name = "universal-hash" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ - "once_cell", - "ring", - "rustls-pki-types", - "rustls-webpki", + "crypto-common", "subtle", - "zeroize", ] [[package]] -name = "rustls-pki-types" -version = "1.14.0" +name = "unsafe-libyaml" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd" -dependencies = [ - "web-time", - "zeroize", -] +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" [[package]] -name = "rustls-webpki" -version = "0.103.9" +name = "unsigned-varint" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53" -dependencies = [ - "ring", - "rustls-pki-types", - "untrusted", -] +checksum = "6889a77d49f1f013504cec6bf97a2c730394adedaeb1deb5ea08949a50541105" [[package]] -name = "rustversion" -version = "1.0.22" +name = "unsigned-varint" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +checksum = "eb066959b24b5196ae73cb057f45598450d2c5f71460e98c49b738086eff9c06" [[package]] -name = "ryu" -version = "1.0.23" +name = "untrusted" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] -name = "scopeguard" -version = "1.2.0" +name = "untrusted" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] -name = "serde" -version = "1.0.228" +name = "url" +version = "2.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" dependencies = [ - "serde_core", - "serde_derive", + "form_urlencoded", + "idna 1.1.0", + "percent-encoding", + "serde", ] [[package]] -name = "serde_core" -version = "1.0.228" +name = "utf-8" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] -name = "serde_derive" -version = "1.0.228" +name = "utf8_iter" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "utils" +version = "0.1.0" dependencies = [ - "proc-macro2", - "quote", - "syn", + "hex", + "sp-core 31.0.0", ] [[package]] -name = "serde_json" -version = "1.0.149" +name = "uuid" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +checksum = "e2e054861b4bd027cd373e18e8d8d8e6548085000e41290d95ce0c373a654b4a" dependencies = [ - "itoa", - "memchr", - "serde", + "getrandom 0.3.4", + "js-sys", "serde_core", - "zmij", + "wasm-bindgen", ] [[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +name = "validator-node" +version = "0.1.0" dependencies = [ - "form_urlencoded", - "itoa", - "ryu", + "anyhow", + "bincode", + "bittensor-rs", + "chrono", + "clap", + "futures", + "hex", + "libp2p", + "parking_lot 0.12.5", + "platform-bittensor", + "platform-challenge-sdk", + "platform-core", + "platform-distributed-storage", + "platform-p2p-consensus", + "platform-storage", "serde", + "serde_json", + "sha2 0.10.9", + "sp-core 38.1.0", + "tokio", + "tracing", + "tracing-subscriber 0.3.22", + "uuid", + "wasm-runtime-interface", ] [[package]] -name = "sharded-slab" -version = "0.1.7" +name = "valuable" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" [[package]] -name = "shlex" -version = "1.3.0" +name = "vcpkg" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] -name = "signal-hook" -version = "0.3.18" +name = "version_check" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" -dependencies = [ - "libc", - "signal-hook-registry", +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "w3f-bls" +version = "0.1.3" +source = "git+https://github.com/opentensor/bls?branch=fix-no-std#4ac443d11a6c9fdebe329d113702ad7387ba1688" +dependencies = [ + "ark-bls12-377", + "ark-bls12-381 0.4.0", + "ark-ec 0.4.2", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-serialize-derive 0.4.2", + "arrayref", + "digest 0.10.7", + "rand 0.8.5", + "rand_chacha 0.3.1", + "rand_core 0.6.4", + "sha2 0.10.9", + "sha3", + "zeroize", ] [[package]] -name = "signal-hook-mio" -version = "0.2.5" +name = "w3f-bls" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6bfb937b3d12077654a9e43e32a4e9c20177dd9fea0f3aba673e7840bb54f32" +dependencies = [ + "ark-bls12-377", + "ark-bls12-381 0.4.0", + "ark-ec 0.4.2", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-serialize-derive 0.4.2", + "arrayref", + "digest 0.10.7", + "rand 0.8.5", + "rand_chacha 0.3.1", + "rand_core 0.6.4", + "sha2 0.10.9", + "sha3", + "zeroize", +] + +[[package]] +name = "w3f-pcs" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc" +checksum = "fbe7a8d5c914b69392ab3b267f679a2e546fe29afaddce47981772ac71bd02e1" dependencies = [ - "libc", - "mio", - "signal-hook", + "ark-ec 0.5.0", + "ark-ff 0.5.0", + "ark-poly 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "merlin", ] [[package]] -name = "signal-hook-registry" -version = "1.4.8" +name = "w3f-plonk-common" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +checksum = "1aca389e494fe08c5c108b512e2328309036ee1c0bc7bdfdb743fef54d448c8c" dependencies = [ - "errno", - "libc", + "ark-ec 0.5.0", + "ark-ff 0.5.0", + "ark-poly 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "getrandom_or_panic", + "rand_core 0.6.4", + "w3f-pcs", ] [[package]] -name = "slab" -version = "0.4.12" +name = "w3f-ring-proof" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +checksum = "8a639379402ad51504575dbd258740383291ac8147d3b15859bdf1ea48c677de" +dependencies = [ + "ark-ec 0.5.0", + "ark-ff 0.5.0", + "ark-poly 0.5.0", + "ark-serialize 0.5.0", + "ark-std 0.5.0", + "ark-transcript", + "w3f-pcs", + "w3f-plonk-common", +] [[package]] -name = "smallvec" -version = "1.15.1" +name = "walkdir" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] [[package]] -name = "socket2" -version = "0.6.2" +name = "want" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" dependencies = [ - "libc", - "windows-sys 0.60.2", + "try-lock", ] [[package]] -name = "stable_deref_trait" -version = "1.2.1" +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" [[package]] -name = "static_assertions" -version = "1.1.0" +name = "wasip2" +version = "1.0.1+wasi-0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] [[package]] -name = "strsim" -version = "0.11.1" +name = "wasm-bindgen" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] [[package]] -name = "strum" -version = "0.26.3" +name = "wasm-bindgen-futures" +version = "0.4.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +checksum = "836d9622d604feee9e5de25ac10e3ea5f2d65b41eac0d9ce72eb5deae707ce7c" dependencies = [ - "strum_macros", + "cfg-if", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", ] [[package]] -name = "strum_macros" -version = "0.26.4" +name = "wasm-bindgen-macro" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" dependencies = [ - "heck", - "proc-macro2", "quote", - "rustversion", - "syn", + "wasm-bindgen-macro-support", ] [[package]] -name = "subtle" -version = "2.6.1" +name = "wasm-bindgen-macro-support" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.111", + "wasm-bindgen-shared", +] [[package]] -name = "syn" -version = "2.0.116" +name = "wasm-bindgen-shared" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3df424c70518695237746f84cede799c9c58fcb37450d7b23716568cc8bc69cb" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" dependencies = [ - "proc-macro2", - "quote", "unicode-ident", ] [[package]] -name = "sync_wrapper" -version = "1.0.2" +name = "wasm-compose" +version = "0.243.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +checksum = "af801b6f36459023eaec63fdbaedad2fd5a4ab7dc74ecc110a8b5d375c5775e4" dependencies = [ - "futures-core", + "anyhow", + "heck", + "im-rc", + "indexmap 2.12.1", + "log", + "petgraph", + "serde", + "serde_derive", + "serde_yaml", + "smallvec", + "wasm-encoder 0.243.0", + "wasmparser 0.243.0", + "wat", ] [[package]] -name = "synstructure" -version = "0.13.2" +name = "wasm-encoder" +version = "0.243.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +checksum = "c55db9c896d70bd9fa535ce83cd4e1f2ec3726b0edd2142079f594fc3be1cb35" dependencies = [ - "proc-macro2", - "quote", - "syn", + "leb128fmt", + "wasmparser 0.243.0", ] [[package]] -name = "term-challenge-wasm" -version = "0.1.0" +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" dependencies = [ - "bincode", - "platform-challenge-sdk-wasm", - "serde", + "leb128fmt", + "wasmparser 0.244.0", ] [[package]] -name = "term-cli" +name = "wasm-runtime-interface" version = "0.1.0" dependencies = [ - "anyhow", + "bincode", "chrono", - "clap", - "crossterm", - "ratatui", - "reqwest", + "ipnet", + "platform-challenge-sdk-wasm", + "reqwest 0.12.25", "serde", "serde_json", - "tokio", + "sha2 0.10.9", + "thiserror 2.0.17", "tracing", - "tracing-subscriber", + "trust-dns-resolver", + "url", + "wasmtime 41.0.3", ] [[package]] -name = "thiserror" -version = "2.0.18" +name = "wasmi" +version = "0.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +checksum = "a19af97fcb96045dd1d6b4d23e2b4abdbbe81723dbc5c9f016eb52145b320063" dependencies = [ - "thiserror-impl", + "arrayvec 0.7.6", + "multi-stash", + "smallvec", + "spin 0.9.8", + "wasmi_collections", + "wasmi_core", + "wasmi_ir", + "wasmparser 0.221.3", ] [[package]] -name = "thiserror-impl" -version = "2.0.18" +name = "wasmi_collections" +version = "0.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] +checksum = "e80d6b275b1c922021939d561574bf376613493ae2b61c6963b15db0e8813562" [[package]] -name = "thread_local" -version = "1.1.9" +name = "wasmi_core" +version = "0.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +checksum = "3a8c51482cc32d31c2c7ff211cd2bedd73c5bd057ba16a2ed0110e7a96097c33" dependencies = [ - "cfg-if", + "downcast-rs", + "libm", ] [[package]] -name = "tinystr" -version = "0.8.2" +name = "wasmi_ir" +version = "0.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +checksum = "6e431a14c186db59212a88516788bd68ed51f87aa1e08d1df742522867b5289a" dependencies = [ - "displaydoc", - "zerovec", + "wasmi_core", ] [[package]] -name = "tinyvec" -version = "1.10.0" +name = "wasmparser" +version = "0.102.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +checksum = "48134de3d7598219ab9eaf6b91b15d8e50d31da76b8519fe4ecfcec2cf35104b" dependencies = [ - "tinyvec_macros", + "indexmap 1.9.3", + "url", ] [[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - -[[package]] -name = "tokio" -version = "1.49.0" +name = "wasmparser" +version = "0.221.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" +checksum = "d06bfa36ab3ac2be0dee563380147a5b81ba10dd8885d7fbbc9eb574be67d185" dependencies = [ - "bytes", - "libc", - "mio", - "parking_lot", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", - "windows-sys 0.61.2", + "bitflags 2.10.0", ] [[package]] -name = "tokio-macros" -version = "2.6.0" +name = "wasmparser" +version = "0.243.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +checksum = "f6d8db401b0528ec316dfbe579e6ab4152d61739cfe076706d2009127970159d" dependencies = [ - "proc-macro2", - "quote", - "syn", + "bitflags 2.10.0", + "hashbrown 0.15.5", + "indexmap 2.12.1", + "semver", + "serde", ] [[package]] -name = "tokio-rustls" -version = "0.26.4" +name = "wasmparser" +version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" dependencies = [ - "rustls", - "tokio", + "bitflags 2.10.0", + "indexmap 2.12.1", + "semver", ] [[package]] -name = "tower" -version = "0.5.3" +name = "wasmprinter" +version = "0.243.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +checksum = "eb2b6035559e146114c29a909a3232928ee488d6507a1504d8934e8607b36d7b" dependencies = [ - "futures-core", - "futures-util", - "pin-project-lite", - "sync_wrapper", - "tokio", - "tower-layer", - "tower-service", + "anyhow", + "termcolor", + "wasmparser 0.243.0", ] [[package]] -name = "tower-http" -version = "0.6.8" +name = "wasmtime" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8" +checksum = "f907fdead3153cb9bfb7a93bbd5b62629472dc06dee83605358c64c52ed3dda9" dependencies = [ - "bitflags", - "bytes", - "futures-util", - "http", - "http-body", - "iri-string", - "pin-project-lite", - "tower", - "tower-layer", - "tower-service", + "anyhow", + "bincode", + "cfg-if", + "indexmap 1.9.3", + "libc", + "log", + "object 0.30.4", + "once_cell", + "paste", + "psm", + "serde", + "target-lexicon 0.12.16", + "wasmparser 0.102.0", + "wasmtime-environ 8.0.1", + "wasmtime-jit", + "wasmtime-runtime", + "windows-sys 0.45.0", ] [[package]] -name = "tower-layer" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" - -[[package]] -name = "tower-service" -version = "0.3.3" +name = "wasmtime" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" +checksum = "a19f56cece843fa95dd929f5568ff8739c7e3873b530ceea9eda2aa02a0b4142" +dependencies = [ + "addr2line 0.25.1", + "anyhow", + "async-trait", + "bitflags 2.10.0", + "bumpalo", + "cc", + "cfg-if", + "encoding_rs", + "futures", + "fxprof-processed-profile", + "gimli 0.32.3", + "hashbrown 0.15.5", + "indexmap 2.12.1", + "ittapi", + "libc", + "log", + "mach2", + "memfd", + "object 0.37.3", + "once_cell", + "postcard", + "pulley-interpreter", + "rayon", + "rustix 1.1.2", + "semver", + "serde", + "serde_derive", + "serde_json", + "smallvec", + "target-lexicon 0.13.4", + "tempfile", + "wasm-compose", + "wasm-encoder 0.243.0", + "wasmparser 0.243.0", + "wasmtime-environ 41.0.3", + "wasmtime-internal-cache", + "wasmtime-internal-component-macro", + "wasmtime-internal-component-util", + "wasmtime-internal-cranelift", + "wasmtime-internal-fiber", + "wasmtime-internal-jit-debug", + "wasmtime-internal-jit-icache-coherence", + "wasmtime-internal-math", + "wasmtime-internal-slab", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", + "wasmtime-internal-winch", + "wat", + "windows-sys 0.61.2", +] [[package]] -name = "tracing" -version = "0.1.44" +name = "wasmtime-asm-macros" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +checksum = "d3b9daa7c14cd4fa3edbf69de994408d5f4b7b0959ac13fa69d465f6597f810d" dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", + "cfg-if", ] [[package]] -name = "tracing-attributes" -version = "0.1.31" +name = "wasmtime-environ" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +checksum = "a990198cee4197423045235bf89d3359e69bd2ea031005f4c2d901125955c949" dependencies = [ - "proc-macro2", - "quote", - "syn", + "anyhow", + "cranelift-entity 0.95.1", + "gimli 0.27.3", + "indexmap 1.9.3", + "log", + "object 0.30.4", + "serde", + "target-lexicon 0.12.16", + "thiserror 1.0.69", + "wasmparser 0.102.0", + "wasmtime-types", ] [[package]] -name = "tracing-core" -version = "0.1.36" +name = "wasmtime-environ" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +checksum = "3bf9dff572c950258548cbbaf39033f68f8dcd0b43b22e80def9fe12d532d3e5" dependencies = [ - "once_cell", - "valuable", + "anyhow", + "cpp_demangle 0.4.5", + "cranelift-bitset", + "cranelift-entity 0.128.3", + "gimli 0.32.3", + "indexmap 2.12.1", + "log", + "object 0.37.3", + "postcard", + "rustc-demangle", + "semver", + "serde", + "serde_derive", + "smallvec", + "target-lexicon 0.13.4", + "wasm-encoder 0.243.0", + "wasmparser 0.243.0", + "wasmprinter", + "wasmtime-internal-component-util", ] [[package]] -name = "tracing-log" -version = "0.2.0" +name = "wasmtime-internal-cache" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +checksum = "7f52a985f5b5dae53147fc596f3a313c334e2c24fd1ba708634e1382f6ecd727" dependencies = [ + "base64 0.22.1", + "directories-next", "log", - "once_cell", - "tracing-core", + "postcard", + "rustix 1.1.2", + "serde", + "serde_derive", + "sha2 0.10.9", + "toml 0.9.12+spec-1.1.0", + "wasmtime-environ 41.0.3", + "windows-sys 0.61.2", + "zstd", ] [[package]] -name = "tracing-subscriber" -version = "0.3.22" +name = "wasmtime-internal-component-macro" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +checksum = "7920dc7dcb608352f5fe93c52582e65075b7643efc5dac3fc717c1645a8d29a0" dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex-automata", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", + "anyhow", + "proc-macro2", + "quote", + "syn 2.0.111", + "wasmtime-internal-component-util", + "wasmtime-internal-wit-bindgen", + "wit-parser", ] [[package]] -name = "try-lock" -version = "0.2.5" +name = "wasmtime-internal-component-util" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +checksum = "066f5aed35aa60580a2ac0df145c0f0d4b04319862fee1d6036693e1cca43a12" [[package]] -name = "unicode-ident" -version = "1.0.24" +name = "wasmtime-internal-cranelift" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "afb8002dc415b7773d7949ee360c05ee8f91627ec25a7a0b01ee03831bdfdda1" +dependencies = [ + "cfg-if", + "cranelift-codegen", + "cranelift-control", + "cranelift-entity 0.128.3", + "cranelift-frontend", + "cranelift-native", + "gimli 0.32.3", + "itertools 0.14.0", + "log", + "object 0.37.3", + "pulley-interpreter", + "smallvec", + "target-lexicon 0.13.4", + "thiserror 2.0.17", + "wasmparser 0.243.0", + "wasmtime-environ 41.0.3", + "wasmtime-internal-math", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", +] [[package]] -name = "unicode-segmentation" -version = "1.12.0" +name = "wasmtime-internal-fiber" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +checksum = "7f9c562c5a272bc9f615d8f0c085a4360bafa28eef9aa5947e63d204b1129b22" +dependencies = [ + "cc", + "cfg-if", + "libc", + "rustix 1.1.2", + "wasmtime-environ 41.0.3", + "wasmtime-internal-versioned-export-macros", + "windows-sys 0.61.2", +] [[package]] -name = "unicode-truncate" -version = "1.1.0" +name = "wasmtime-internal-jit-debug" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" +checksum = "db673148f26e1211db3913c12c75594be9e3858a71fa297561e9162b1a49cfb0" dependencies = [ - "itertools", - "unicode-segmentation", - "unicode-width 0.1.14", + "cc", + "object 0.37.3", + "rustix 1.1.2", + "wasmtime-internal-versioned-export-macros", ] [[package]] -name = "unicode-width" -version = "0.1.14" +name = "wasmtime-internal-jit-icache-coherence" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +checksum = "bada5ca1cc47df7d14100e2254e187c2486b426df813cea2dd2553a7469f7674" +dependencies = [ + "anyhow", + "cfg-if", + "libc", + "windows-sys 0.61.2", +] [[package]] -name = "unicode-width" -version = "0.2.0" +name = "wasmtime-internal-math" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" +checksum = "cf6f615d528eda9adc6eefb062135f831b5215c348f4c3ec3e143690c730605b" +dependencies = [ + "libm", +] [[package]] -name = "untrusted" -version = "0.9.0" +name = "wasmtime-internal-slab" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +checksum = "da169d4f789b586e1b2612ba8399c653ed5763edf3e678884ba785bb151d018f" [[package]] -name = "url" -version = "2.5.8" +name = "wasmtime-internal-unwinder" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +checksum = "4888301f3393e4e8c75c938cce427293fade300fee3fc8fd466fdf3e54ae068e" dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", - "serde", + "cfg-if", + "cranelift-codegen", + "log", + "object 0.37.3", + "wasmtime-environ 41.0.3", ] [[package]] -name = "utf8_iter" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" - -[[package]] -name = "utf8parse" -version = "0.2.2" +name = "wasmtime-internal-versioned-export-macros" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +checksum = "63ba3124cc2cbcd362672f9f077303ccc4cd61daa908f73447b7fdaece75ff9f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] [[package]] -name = "valuable" -version = "0.1.1" +name = "wasmtime-internal-winch" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +checksum = "90a4182515dabba776656de4ebd62efad03399e261cf937ecccb838ce8823534" +dependencies = [ + "cranelift-codegen", + "gimli 0.32.3", + "log", + "object 0.37.3", + "target-lexicon 0.13.4", + "wasmparser 0.243.0", + "wasmtime-environ 41.0.3", + "wasmtime-internal-cranelift", + "winch-codegen", +] [[package]] -name = "want" -version = "0.3.1" +name = "wasmtime-internal-wit-bindgen" +version = "41.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +checksum = "87acbd416227cdd279565ba49e57cf7f08d112657c3b3f39b70250acdfd094fe" dependencies = [ - "try-lock", + "anyhow", + "bitflags 2.10.0", + "heck", + "indexmap 2.12.1", + "wit-parser", ] [[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" +name = "wasmtime-jit" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +checksum = "0de48df552cfca1c9b750002d3e07b45772dd033b0b206d5c0968496abf31244" +dependencies = [ + "addr2line 0.19.0", + "anyhow", + "bincode", + "cfg-if", + "cpp_demangle 0.3.5", + "gimli 0.27.3", + "log", + "object 0.30.4", + "rustc-demangle", + "serde", + "target-lexicon 0.12.16", + "wasmtime-environ 8.0.1", + "wasmtime-jit-icache-coherence", + "wasmtime-runtime", + "windows-sys 0.45.0", +] [[package]] -name = "wasip2" -version = "1.0.2+wasi-0.2.9" +name = "wasmtime-jit-debug" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5" +checksum = "6e0554b84c15a27d76281d06838aed94e13a77d7bf604bbbaf548aa20eb93846" dependencies = [ - "wit-bindgen", + "once_cell", ] [[package]] -name = "wasm-bindgen" -version = "0.2.108" +name = "wasmtime-jit-icache-coherence" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", +checksum = "aecae978b13f7f67efb23bd827373ace4578f2137ec110bbf6a4a7cde4121bbd" +dependencies = [ + "cfg-if", + "libc", + "windows-sys 0.45.0", ] [[package]] -name = "wasm-bindgen-futures" -version = "0.4.58" +name = "wasmtime-runtime" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f" +checksum = "658cf6f325232b6760e202e5255d823da5e348fdea827eff0a2a22319000b441" dependencies = [ + "anyhow", + "cc", "cfg-if", - "futures-util", - "js-sys", - "once_cell", - "wasm-bindgen", - "web-sys", + "indexmap 1.9.3", + "libc", + "log", + "mach", + "memfd", + "memoffset", + "paste", + "rand 0.8.5", + "rustix 0.36.17", + "wasmtime-asm-macros", + "wasmtime-environ 8.0.1", + "wasmtime-jit-debug", + "windows-sys 0.45.0", ] [[package]] -name = "wasm-bindgen-macro" -version = "0.2.108" +name = "wasmtime-types" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +checksum = "a4f6fffd2a1011887d57f07654dd112791e872e3ff4a2e626aee8059ee17f06f" dependencies = [ - "quote", - "wasm-bindgen-macro-support", + "cranelift-entity 0.95.1", + "serde", + "thiserror 1.0.69", + "wasmparser 0.102.0", ] [[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.108" +name = "wast" +version = "244.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +checksum = "b2e7b9f9e23311275920e3d6b56d64137c160cf8af4f84a7283b36cfecbf4acb" dependencies = [ "bumpalo", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", + "leb128fmt", + "memchr", + "unicode-width", + "wasm-encoder 0.244.0", ] [[package]] -name = "wasm-bindgen-shared" -version = "0.2.108" +name = "wat" +version = "1.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +checksum = "bbf35b87ed352f9ab6cd0732abde5a67dd6153dfd02c493e61459218b19456fa" dependencies = [ - "unicode-ident", + "wast", ] [[package]] name = "web-sys" -version = "0.3.85" +version = "0.3.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598" +checksum = "9b32828d774c412041098d182a8b38b16ea816958e07cf40eec2bc080ae137ac" dependencies = [ "js-sys", "wasm-bindgen", @@ -1803,6 +9366,24 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-root-certs" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75c7f0ef91146ebfb530314f5f1d24528d7f0767efbfd31dce919275413e393e" +dependencies = [ + "webpki-root-certs 1.0.4", +] + +[[package]] +name = "webpki-root-certs" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee3e3b5f5e80bc89f30ce8d0343bf4e5f12341c51f3e26cbeecbc7c85443e85b" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "webpki-roots" version = "1.0.6" @@ -1812,6 +9393,12 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + [[package]] name = "winapi" version = "0.3.9" @@ -1828,12 +9415,61 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "winch-codegen" +version = "41.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4f31dcfdfaf9d6df9e1124d7c8ee6fc29af5b99b89d11ae731c138e0f5bd77b" +dependencies = [ + "anyhow", + "cranelift-assembler-x64", + "cranelift-codegen", + "gimli 0.32.3", + "regalloc2", + "smallvec", + "target-lexicon 0.13.4", + "thiserror 2.0.17", + "wasmparser 0.243.0", + "wasmtime-environ 41.0.3", + "wasmtime-internal-cranelift", + "wasmtime-internal-math", +] + +[[package]] +name = "windows" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efc5cf48f83140dcaab716eeaea345f9e93d0018fb81162753a3f76c3397b538" +dependencies = [ + "windows-core 0.53.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcc5b895a6377f1ab9fa55acedab1fd5ac0db66ad1e6c7f47e28a22e446a5dd" +dependencies = [ + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + [[package]] name = "windows-core" version = "0.62.2" @@ -1843,7 +9479,7 @@ dependencies = [ "windows-implement", "windows-interface", "windows-link", - "windows-result", + "windows-result 0.4.1", "windows-strings", ] @@ -1855,7 +9491,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", ] [[package]] @@ -1866,7 +9502,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", ] [[package]] @@ -1875,6 +9511,26 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link", + "windows-result 0.4.1", + "windows-strings", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.4.1" @@ -1893,6 +9549,24 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -1929,6 +9603,36 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -1962,6 +9666,18 @@ dependencies = [ "windows_x86_64_msvc 0.53.1", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" @@ -1974,6 +9690,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" @@ -1986,6 +9714,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -2010,6 +9750,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" @@ -2022,6 +9774,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" @@ -2034,6 +9798,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" @@ -2046,6 +9822,18 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" @@ -2058,11 +9846,48 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" +[[package]] +name = "winnow" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" +dependencies = [ + "memchr", +] + +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "wit-bindgen" -version = "0.51.0" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "wit-parser" +version = "0.243.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +checksum = "df983a8608e513d8997f435bb74207bf0933d0e49ca97aa9d8a6157164b9b7fc" +dependencies = [ + "anyhow", + "id-arena", + "indexmap 2.12.1", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser 0.243.0", +] [[package]] name = "writeable" @@ -2070,6 +9895,105 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + +[[package]] +name = "x25519-dalek" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" +dependencies = [ + "curve25519-dalek 4.1.3", + "rand_core 0.6.4", + "serde", + "zeroize", +] + +[[package]] +name = "x509-parser" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcbc162f30700d6f3f82a24bf7cc62ffe7caea42c0b2cba8bf7f3ae50cf51f69" +dependencies = [ + "asn1-rs", + "data-encoding", + "der-parser", + "lazy_static", + "nom 7.1.3", + "oid-registry", + "rusticata-macros", + "thiserror 1.0.69", + "time", +] + +[[package]] +name = "xml-rs" +version = "0.8.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae8337f8a065cfc972643663ea4279e04e7256de865aa66fe25cec5fb912d3f" + +[[package]] +name = "xmltree" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d8a75eaf6557bb84a65ace8609883db44a29951042ada9b393151532e41fcb" +dependencies = [ + "xml-rs", +] + +[[package]] +name = "yamux" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed0164ae619f2dc144909a9f082187ebb5893693d8c0196e8085283ccd4b776" +dependencies = [ + "futures", + "log", + "nohash-hasher", + "parking_lot 0.12.5", + "pin-project", + "rand 0.8.5", + "static_assertions", +] + +[[package]] +name = "yamux" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deab71f2e20691b4728b349c6cee8fc7223880fa67b6b4f92225ec32225447e5" +dependencies = [ + "futures", + "log", + "nohash-hasher", + "parking_lot 0.12.5", + "pin-project", + "rand 0.9.2", + "static_assertions", + "web-time", +] + +[[package]] +name = "yap" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfe269e7b803a5e8e20cbd97860e136529cd83bf2c9c6d37b142467e7e1f051f" + +[[package]] +name = "yasna" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd" +dependencies = [ + "time", +] + [[package]] name = "yoke" version = "0.8.1" @@ -2089,28 +10013,28 @@ checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", "synstructure", ] [[package]] name = "zerocopy" -version = "0.8.39" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a" +checksum = "fd74ec98b9250adb3ca554bdde269adf631549f51d8a8f8f0a10b50f1cb298c3" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.39" +version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517" +checksum = "d8a8d209fdf45cf5138cbb5a506f6b52522a25afccc534d1475dad8e31105c6a" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", ] [[package]] @@ -2130,7 +10054,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", "synstructure", ] @@ -2139,6 +10063,20 @@ name = "zeroize" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] [[package]] name = "zerotrie" @@ -2170,11 +10108,33 @@ checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.111", +] + +[[package]] +name = "zstd" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" +dependencies = [ + "zstd-safe", ] [[package]] -name = "zmij" -version = "1.0.21" +name = "zstd-safe" +version = "7.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" +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 f34f0296b..347c6d7bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,112 @@ [workspace] resolver = "2" -members = ["wasm", "cli"] -default-members = ["wasm"] +members = [ + "crates/core", + "crates/storage", + "crates/distributed-storage", + "crates/challenge-sdk", + "crates/challenge-registry", + "crates/epoch", + "crates/bittensor-integration", + "crates/subnet-manager", + "crates/rpc-server", + "crates/p2p-consensus", + "crates/wasm-runtime-interface", + "crates/challenge-sdk-wasm", + "bins/validator-node", + "bins/utils", + "bins/mock-subtensor", + "bins/platform-cli", + "tests", +] +# Note: Challenges are in separate repositories and import platform-challenge-sdk as a git dependency +# Note: WASM runtime removed - updates via git, version checked at handshake +# Note: P2P-only architecture - no centralized platform-server + +# Challenge crates can be added here or as optional path/git dependencies +# Example: +# "challenges/example-challenge", [workspace.package] version = "0.1.0" edition = "2021" authors = ["Platform Network"] license = "Apache-2.0" -repository = "https://github.com/PlatformNetwork/term-challenge" + +[workspace.dependencies] +# Bittensor (with CRv4 timelock encryption support and new Subtensor API) +# Updated to 9cf5991: export get_tempo and get_reveal_period functions +bittensor-rs = { git = "https://github.com/CortexLM/bittensor-rs", rev = "9cf5991" } + +# Async runtime +tokio = { version = "1.40", features = ["full", "sync", "macros", "rt-multi-thread"] } +futures = "0.3" +async-trait = "0.1" + +# Serialization +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +bincode = "1.3" + +# Cryptography +ed25519-dalek = { version = "2.1", features = ["rand_core", "serde"] } +sha2 = "0.10" +rand = "0.8" +hex = "0.4" +sp-core = "38.1.0" +parity-scale-codec = { version = "3.7.5", features = ["derive"] } + +# Storage +sled = "0.34" + +# Logging +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } + +# Error monitoring (optional - enabled via SENTRY_DSN env var) +sentry = { version = "0.35", default-features = false, features = ["backtrace", "contexts", "tracing", "reqwest", "rustls"] } +sentry-tracing = "0.35" + +# Error handling +anyhow = "1.0" +thiserror = "2.0" + +# Utils +uuid = { version = "1.10", features = ["v4", "serde"] } +chrono = { version = "0.4", features = ["serde"] } +parking_lot = "0.12" + +# CLI +clap = { version = "4.5", features = ["derive", "env"] } + +# Testing +tempfile = "3.12" +mockall = "0.13" +wiremock = "0.6" +rstest = "0.23" +tokio-test = "0.4" + +# HTTP/Web (for P2P communication and challenge SDK) +reqwest = { version = "0.12", features = ["json"] } + +# Patch for TLE/CRv4 compatibility (w3f-bls version conflict) +[patch.crates-io] +w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std" } + +# Clippy lints configuration +[workspace.lints.clippy] +# Allow these patterns that are intentional in this codebase +too_many_arguments = "allow" +large_enum_variant = "allow" +type_complexity = "allow" +await_holding_lock = "warn" +collapsible_match = "allow" +collapsible_if = "allow" + +# Workspace-level feature flags for challenge integration +# Individual crates can enable these by adding features in their Cargo.toml: +# [features] +# dynamic-challenges = ["libloading"] +[workspace.metadata.challenge-features] +# Enable dynamic challenge loading (crates opt-in via features) +dynamic-loading-available = true \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..7cec1d947 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,154 @@ +# ============================================================================= +# Platform Network - Validator Docker Image +# ============================================================================= +# Fully decentralized P2P architecture +# +# Build: +# docker build -t platform:latest . +# ============================================================================= + +# Build stage +FROM rust:1.92-bookworm AS builder +ARG RUSTUP_TOOLCHAIN=stable +ENV RUSTUP_TOOLCHAIN=${RUSTUP_TOOLCHAIN} + +ARG PLATFORM_NIGHTLY_RUSTFLAGS="" +ARG PLATFORM_LINKER_RUSTFLAGS="" +ARG PLATFORM_FAST_LINKER_RUSTFLAGS="" +ARG INSTALL_FAST_LINKER=auto +ENV PLATFORM_NIGHTLY_RUSTFLAGS=${PLATFORM_NIGHTLY_RUSTFLAGS} +ENV PLATFORM_LINKER_RUSTFLAGS=${PLATFORM_LINKER_RUSTFLAGS} +ENV PLATFORM_FAST_LINKER_RUSTFLAGS=${PLATFORM_FAST_LINKER_RUSTFLAGS} +ENV INSTALL_FAST_LINKER=${INSTALL_FAST_LINKER} + +# Install dependencies +RUN apt-get update \ + && apt-get install -y \ + pkg-config \ + libssl-dev \ + protobuf-compiler \ + cmake \ + clang \ + libclang-dev \ + lld \ + && if [ "$INSTALL_FAST_LINKER" = "mold" ]; then \ + apt-get install -y mold; \ + fi \ + && if [ "$RUSTUP_TOOLCHAIN" = "nightly" ]; then \ + rustup toolchain install nightly; \ + fi \ + && rm -rf /var/lib/apt/lists/* + +# Set up cargo-chef for caching +RUN cargo install cargo-chef --locked + +WORKDIR /app + +# Prepare recipe for caching dependencies +COPY . . +RUN cargo +${RUSTUP_TOOLCHAIN} chef prepare --recipe-path recipe.json + +# Cache dependencies +FROM rust:1.92-bookworm AS cacher +ARG RUSTUP_TOOLCHAIN=stable +ENV RUSTUP_TOOLCHAIN=${RUSTUP_TOOLCHAIN} + +ARG PLATFORM_NIGHTLY_RUSTFLAGS="" +ARG PLATFORM_LINKER_RUSTFLAGS="" +ARG PLATFORM_FAST_LINKER_RUSTFLAGS="" +ARG INSTALL_FAST_LINKER=auto +ENV PLATFORM_NIGHTLY_RUSTFLAGS=${PLATFORM_NIGHTLY_RUSTFLAGS} +ENV PLATFORM_LINKER_RUSTFLAGS=${PLATFORM_LINKER_RUSTFLAGS} +ENV PLATFORM_FAST_LINKER_RUSTFLAGS=${PLATFORM_FAST_LINKER_RUSTFLAGS} +ENV INSTALL_FAST_LINKER=${INSTALL_FAST_LINKER} +RUN apt-get update \ + && apt-get install -y \ + pkg-config \ + libssl-dev \ + protobuf-compiler \ + cmake \ + clang \ + libclang-dev \ + lld \ + && if [ "$INSTALL_FAST_LINKER" = "mold" ]; then \ + apt-get install -y mold; \ + fi \ + && rm -rf /var/lib/apt/lists/* +RUN cargo install cargo-chef --locked +WORKDIR /app +COPY --from=builder /app/recipe.json recipe.json +RUN cargo +${RUSTUP_TOOLCHAIN} chef cook --release --recipe-path recipe.json + +# Build stage +FROM rust:1.92-bookworm AS final-builder +ARG RUSTUP_TOOLCHAIN=stable +ENV RUSTUP_TOOLCHAIN=${RUSTUP_TOOLCHAIN} + +ARG PLATFORM_NIGHTLY_RUSTFLAGS="" +ARG PLATFORM_LINKER_RUSTFLAGS="" +ARG PLATFORM_FAST_LINKER_RUSTFLAGS="" +ARG INSTALL_FAST_LINKER=auto +ENV PLATFORM_NIGHTLY_RUSTFLAGS=${PLATFORM_NIGHTLY_RUSTFLAGS} +ENV PLATFORM_LINKER_RUSTFLAGS=${PLATFORM_LINKER_RUSTFLAGS} +ENV PLATFORM_FAST_LINKER_RUSTFLAGS=${PLATFORM_FAST_LINKER_RUSTFLAGS} +ENV INSTALL_FAST_LINKER=${INSTALL_FAST_LINKER} +RUN apt-get update \ + && apt-get install -y \ + pkg-config \ + libssl-dev \ + protobuf-compiler \ + cmake \ + clang \ + libclang-dev \ + lld \ + && if [ "$INSTALL_FAST_LINKER" = "mold" ]; then \ + apt-get install -y mold; \ + fi \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +COPY --from=cacher /app/target target +COPY --from=cacher /usr/local/cargo /usr/local/cargo +COPY . . + +# Build the validator +RUN cargo +${RUSTUP_TOOLCHAIN} build --release -p validator-node + +# Runtime stage (Ubuntu 24.04 for glibc 2.39 compatibility) +FROM ubuntu:24.04 + +# Install runtime dependencies +RUN apt-get update && apt-get install -y \ + ca-certificates \ + libssl3t64 \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Copy binary +COPY --from=final-builder /app/target/release/validator-node /usr/local/bin/validator-node + +# Create data directory with restricted permissions +# Note: Using 755 instead of 777 for security; the container runs as root by default +RUN mkdir -p /data && chmod 755 /data + +# Environment defaults +ENV RUST_LOG=info,validator_node=debug,platform_p2p_consensus=info +ENV DATA_DIR=/data +ENV SUBTENSOR_ENDPOINT=wss://entrypoint-finney.opentensor.ai:443 +ENV NETUID=100 + +# Expose P2P port +EXPOSE 9000 + +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ + CMD test -e /data/distributed.db || exit 1 + +# Default entrypoint +ENTRYPOINT ["validator-node"] +CMD ["--data-dir", "/data", "--listen-addr", "/ip4/0.0.0.0/tcp/9000"] + +# Labels +LABEL org.opencontainers.image.source="https://github.com/PlatformNetwork/platform" +LABEL org.opencontainers.image.description="Platform Validator Node - Decentralized P2P" +LABEL org.opencontainers.image.licenses="Apache-2.0" diff --git a/README.md b/README.md index 2e1e43fc5..f1b450070 100644 --- a/README.md +++ b/README.md @@ -1,309 +1,177 @@
-# ฯ„ฮตrm chฮฑllฮตฮทgฮต +# ฯlฮฑฯ„fฮฟrm -**Terminal Benchmark Challenge โ€” WASM Evaluation Module for Platform-v2** +**Distributed validator network for decentralized AI evaluation on Bittensor** -[![License](https://img.shields.io/github/license/PlatformNetwork/term-challenge)](https://github.com/PlatformNetwork/term-challenge/blob/main/LICENSE) +[![CI](https://github.com/PlatformNetwork/platform/actions/workflows/ci.yml/badge.svg)](https://github.com/PlatformNetwork/platform/actions/workflows/ci.yml) +[![Coverage](https://platformnetwork.github.io/platform/badges/coverage.svg)](https://github.com/PlatformNetwork/platform/actions) +[![License](https://img.shields.io/github/license/PlatformNetwork/platform)](https://github.com/PlatformNetwork/platform/blob/main/LICENSE) +[![GitHub stars](https://img.shields.io/github/stars/PlatformNetwork/platform)](https://github.com/PlatformNetwork/platform/stargazers) [![Rust](https://img.shields.io/badge/rust-1.90+-orange.svg)](https://www.rust-lang.org/) -![Term Challenge Banner](assets/banner.jpg) +![Platform Banner](assets/banner.jpg) -
- -Term Challenge is a WASM evaluation module for AI agents on the Bittensor network. It runs inside [platform-v2](https://github.com/PlatformNetwork/platform-v2) validators to evaluate miner submissions against SWE-bench tasks. Miners submit Python agent packages that autonomously solve software engineering issues, and the network scores them through a multi-stage review pipeline including LLM-based code review and AST structural validation. - ---- +![Alt](https://repobeats.axiom.co/api/embed/4b44b7f7c97e0591af537309baea88689aefe810.svg "Repobeats analytics image") -## System Architecture - -```mermaid -flowchart LR - Miner[Miner] -->|Submit Agent ZIP| RPC[Validator RPC] - RPC --> Validators[Validator Network] - Validators --> WASM[term-challenge WASM] - WASM --> Storage[(Blockchain Storage)] - Validators --> Executor[term-executor] - Executor -->|Task Results| Validators - Validators -->|Scores + Weights| BT[Bittensor Chain] - CLI[term-cli TUI] -->|JSON-RPC| RPC - CLI -->|Display| Monitor[Leaderboard / Progress / Logs] -``` + --- -## Evaluation Pipeline +## Overview -```mermaid -sequenceDiagram - participant M as Miner - participant V as Validators - participant LLM as LLM Reviewers (ร—3) - participant AST as AST Reviewers (ร—3) - participant W as WASM Module - participant E as term-executor - participant BT as Bittensor +Platform is a **WASM-first, peer-to-peer validator network** for deterministic evaluation of miner submissions on Bittensor. Validators execute challenge logic in a hardened WASM runtime, reach stake-weighted consensus over libp2p, and submit finalized weights to the chain. **Docker is reserved for local and CI test harnesses only.** - M->>V: Submit agent zip + metadata - V->>W: validate(submission) - W-->>V: Approved (>50% consensus) - V->>LLM: Assign LLM code review - V->>AST: Assign AST structural review - LLM-->>V: LLM review scores - AST-->>V: AST review scores - V->>E: Execute agent on SWE-bench tasks - E-->>V: Task results + scores - V->>W: evaluate(results) - W-->>V: Aggregate score + weight - V->>V: Store agent code & logs - V->>V: Log consensus (>50% hash agreement) - V->>BT: Submit weights at epoch boundary -``` +**Core principles** +- Decentralized libp2p mesh (gossipsub + DHT) with no centralized relays. +- Stake-weighted PBFT-style consensus for challenge state and weight aggregation. +- Deterministic WASM execution with strict runtime policy and auditability. +- Explicit separation of production runtime (WASM) and test-only containers (Docker). --- -## Validator Assignment +## Documentation Index -```mermaid -flowchart TB - Sub[New Submission] --> Seed[Deterministic Seed from submission_id] - Seed --> Select[Select 6 Validators] - Select --> LLM[3 LLM Reviewers] - Select --> AST[3 AST Reviewers] - LLM --> LR1[LLM Reviewer 1] - LLM --> LR2[LLM Reviewer 2] - LLM --> LR3[LLM Reviewer 3] - AST --> AR1[AST Reviewer 1] - AST --> AR2[AST Reviewer 2] - AST --> AR3[AST Reviewer 3] - LR1 & LR2 & LR3 -->|Timeout?| TD1{Responded?} - AR1 & AR2 & AR3 -->|Timeout?| TD2{Responded?} - TD1 -->|No| Rep1[Replacement Validator] - TD1 -->|Yes| Agg[Result Aggregation] - TD2 -->|No| Rep2[Replacement Validator] - TD2 -->|Yes| Agg - Rep1 --> Agg - Rep2 --> Agg - Agg --> Score[Final Score] -``` +- [Architecture](docs/architecture.md) +- [Security Model](docs/security.md) +- [Challenges](docs/challenges.md) +- [Challenge Integration Guide](docs/challenge-integration.md) +- [Validator Guide](docs/validator.md) +- [Validator Operations](docs/operations/validator.md) --- -## Submission Flow +## Network Architecture ```mermaid flowchart LR - Register[Register Name] -->|First-register-owns| Name[Submission Name] - Name --> Version[Auto-increment Version] - Version --> Pack[Package Agent ZIP โ‰ค 1MB] - Pack --> Sign[Sign with sr25519] - Sign --> Submit[Submit via RPC] - Submit --> RateCheck{Epoch Rate Limit OK?} - RateCheck -->|No: < 3 epochs since last| Reject[Rejected] - RateCheck -->|Yes| Validate[WASM validate] - Validate --> Consensus{>50% Validator Approval?} - Consensus -->|No| Reject - Consensus -->|Yes| Evaluate[Evaluation Pipeline] - Evaluate --> Store[Store Code + Hash + Logs] + Owner[Sudo Owner] -->|Signed challenge actions| Mesh[(libp2p Mesh)] + Mesh --> DHT[(DHT: submissions + consensus state)] + Mesh --> V1[Validator 1] + Mesh --> V2[Validator 2] + Mesh --> VN[Validator N] + V1 -->|Evaluations + votes| Mesh + V2 -->|Evaluations + votes| Mesh + VN -->|Evaluations + votes| Mesh + V1 -->|Final weights| BT[Bittensor Chain] + V2 -->|Final weights| BT + VN -->|Final weights| BT ``` --- -## Decay Mechanism +## Consensus & Weight Submission ```mermaid -flowchart LR - Top[Top Score Achieved] --> Grace[72h Grace Period] - Grace -->|Within grace| Full[100% Weight Retained] - Grace -->|After grace| Decay[Exponential Decay Begins] - Decay --> Half[50% per 24h half-life] - Half --> Min[Decay to 0.0 min multiplier] - Min --> Burn[Weight Burns to UID 0] -``` - ---- - -## CLI Data Flow +sequenceDiagram + participant L as Leader + participant V1 as Validator 1 + participant V2 as Validator 2 + participant Vn as Validator N + participant BT as Bittensor -```mermaid -flowchart TB - CLI[term-cli] -->|epoch_current| RPC[Validator RPC] - CLI -->|challenge_call /leaderboard| RPC - CLI -->|evaluation_getProgress| RPC - CLI -->|agent_getLogs| RPC - CLI -->|system_health| RPC - CLI -->|validator_count| RPC - RPC --> State[Chain State] - State --> LB[Leaderboard Data] - State --> Eval[Evaluation Progress] - State --> Logs[Validated Logs] + L->>V1: Proposal(action, height) + L->>V2: Proposal(action, height) + L->>Vn: Proposal(action, height) + V1-->>L: Vote(approve/reject) + V2-->>L: Vote(approve/reject) + Vn-->>L: Vote(approve/reject) + L-->>V1: Commit(>=2f+1 approvals) + L-->>V2: Commit(>=2f+1 approvals) + L-->>Vn: Commit(>=2f+1 approvals) + V1->>BT: Submit weights + V2->>BT: Submit weights + Vn->>BT: Submit weights ``` --- -## Agent Log Consensus +## Runtime Policy (WASM-First) ```mermaid flowchart LR - V1[Validator 1] -->|Log Proposal| P2P[(P2P Network)] - V2[Validator 2] -->|Log Proposal| P2P - V3[Validator 3] -->|Log Proposal| P2P - P2P --> Consensus{Hash Match >50%?} - Consensus -->|Yes| Store[Validated Logs] - Consensus -->|No| Reject[Rejected] + Validator[Validator Node] --> Runtime[WASM Runtime] + Runtime --> Policy[Runtime Policy] + Runtime --> HostFns[Whitelisted Host Functions] + Runtime --> Audit[Audit Logs] + Policy --> Runtime + HostFns --> Runtime + Runtime -->|Deterministic outputs| Validator ``` --- -## Agent Code Storage +## WASM Route Handling ```mermaid -flowchart TB - Submit[Agent Submission] --> Validate{package_zip โ‰ค 1MB?} - Validate -->|Yes| Store[Blockchain Storage] - Validate -->|No| Reject[Rejected] - Store --> Code[agent_code:hotkey:epoch] - Store --> Hash[agent_hash:hotkey:epoch] - Store --> Logs[agent_logs:hotkey:epoch โ‰ค 256KB] +sequenceDiagram + participant Client + participant RPC as RPC Server + participant WE as WASM Executor + participant WM as WASM Module + + Client->>RPC: challenge_call(id, method, path) + RPC->>WE: execute_handle_route(request) + WE->>WM: handle_route(serialized_request) + WM-->>WE: serialized_response + WE-->>RPC: WasmRouteResponse + RPC-->>Client: JSON-RPC result ``` --- -## Route Architecture +## Review Assignment Flow ```mermaid flowchart LR - Client[Client] -->|JSON-RPC| RPC[RPC Server] - RPC -->|challenge_call| WE[WASM Executor] - WE -->|handle_route request| WM[WASM Module] - WM --> Router{Route Match} - Router --> LB[/leaderboard] - Router --> Subs[/submissions] - Router --> DS[/dataset] - Router --> Stats[/stats] - Router --> Agent[/agent/:hotkey/code] - LB & Subs & DS & Stats & Agent --> Storage[(Storage)] - Storage --> Response[Serialized Response] - Response --> WE - WE --> RPC - RPC --> Client -``` - ---- - -## Features - -- **WASM Module**: Compiles to `wasm32-unknown-unknown`, loaded by platform-v2 validators -- **SWE-bench Evaluation**: Tasks selected from HuggingFace CortexLM/swe-bench datasets -- **LLM Code Review**: 3 validators perform LLM-based code review via host functions -- **AST Structural Validation**: 3 validators perform AST-based structural analysis -- **Submission Versioning**: Auto-incrementing versions with full history tracking -- **Timeout Handling**: Unresponsive reviewers are replaced with alternate validators -- **Route Handlers**: WASM-native route handling for leaderboard, submissions, dataset, and agent data -- **Epoch Rate Limiting**: 1 submission per 3 epochs per miner -- **Top Agent Decay**: 72h grace period, 50% daily decay to 0 weight -- **P2P Dataset Consensus**: Validators collectively select 50 evaluation tasks -- **Zip Package Submissions**: Agents submitted as zip packages (no compilation step) -- **Agent Code Storage**: Submitted agent packages (โ‰ค 1MB) stored on-chain with hash verification -- **Log Consensus**: Evaluation logs validated across validators with >50% hash agreement -- **CLI (term-cli)**: Native TUI for monitoring leaderboards, evaluation progress, submissions, and network health - ---- - -## Building - -```bash -# Build WASM module -cargo build --release --target wasm32-unknown-unknown -p term-challenge-wasm - -# The output .wasm file is at: -# target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm - -# Build CLI (native) -cargo build --release -p term-cli + Submit[Submission] --> Select[Validator Selection] + Select --> LLM[3 LLM Reviewers] + Select --> AST[3 AST Reviewers] + LLM --> |Review Results| Aggregate[Result Aggregation] + AST --> |Review Results| Aggregate + Aggregate --> Score[Final Score] + LLM -.-> |Timeout| Replace1[Replacement Validator] + AST -.-> |Timeout| Replace2[Replacement Validator] ``` --- -## Architecture +## Subnet Owner Resolution -This repository contains the WASM evaluation module and a native CLI for monitoring. All infrastructure (P2P networking, RPC server, blockchain storage, validator coordination) is provided by [platform-v2](https://github.com/PlatformNetwork/platform-v2). - -``` -term-challenge/ -โ”œโ”€โ”€ wasm/ # WASM evaluation module -โ”‚ โ””โ”€โ”€ src/ -โ”‚ โ”œโ”€โ”€ lib.rs # Challenge trait implementation (validate + evaluate) -โ”‚ โ”œโ”€โ”€ types.rs # Submission, task, config, route, and log types -โ”‚ โ”œโ”€โ”€ scoring.rs # Score aggregation, decay, and weight calculation -โ”‚ โ”œโ”€โ”€ tasks.rs # Active dataset management and history -โ”‚ โ”œโ”€โ”€ dataset.rs # Dataset selection and P2P consensus logic -โ”‚ โ”œโ”€โ”€ routes.rs # WASM route definitions for RPC (handle_route) -โ”‚ โ””โ”€โ”€ agent_storage.rs # Agent code, hash, and log storage functions -โ”œโ”€โ”€ cli/ # Native TUI monitoring tool -โ”‚ โ””โ”€โ”€ src/ -โ”‚ โ”œโ”€โ”€ main.rs # Entry point, event loop -โ”‚ โ”œโ”€โ”€ app.rs # Application state -โ”‚ โ”œโ”€โ”€ ui.rs # Ratatui UI rendering -โ”‚ โ””โ”€โ”€ rpc.rs # JSON-RPC 2.0 client -โ”œโ”€โ”€ docs/ -โ”‚ โ”œโ”€โ”€ architecture.md # System architecture and internals -โ”‚ โ”œโ”€โ”€ miner/ -โ”‚ โ”‚ โ”œโ”€โ”€ how-to-mine.md # Complete miner guide -โ”‚ โ”‚ โ””โ”€โ”€ submission.md # Submission format and review process -โ”‚ โ””โ”€โ”€ validator/ -โ”‚ โ””โ”€โ”€ setup.md # Validator setup and operations -โ”œโ”€โ”€ AGENTS.md # Development guide -โ””โ”€โ”€ README.md +```mermaid +flowchart TB + Sync[Metagraph Sync] --> Parse[Parse Neurons] + Parse --> UID0{UID 0 Found?} + UID0 -->|Yes| Update[Update ChainState.sudo_key] + UID0 -->|No| Keep[Keep Existing] + Update --> Owner[Subnet Owner = UID 0 Hotkey] ``` --- -## How It Works - -1. Miners submit zip packages with agent code and SWE-bench task results -2. Platform-v2 validators load this WASM module -3. `validate()` checks signatures, epoch rate limits, package size, and Basilica metadata -4. **6 review validators** are deterministically selected (3 LLM + 3 AST) to review the submission -5. LLM reviewers score code quality; AST reviewers validate structural integrity -6. Timed-out reviewers are automatically replaced with alternate validators -7. `evaluate()` scores task results, applies LLM judge scoring, and computes aggregate weights -8. Agent code and hash are stored on-chain for auditability (โ‰ค 1MB per package) -9. Evaluation logs are proposed and validated via P2P consensus (>50% hash agreement) -10. Scores are aggregated via P2P consensus and submitted to Bittensor at epoch boundaries -11. Top agents enter a decay cycle: 72h grace โ†’ 50% daily decay โ†’ weight burns to UID 0 - ---- - -## CLI Usage +## Quick Start (Validator) ```bash -# Install via platform CLI -platform download term-challenge - -# Or build from source -cargo build --release -p term-cli - -# Run the TUI -term-cli --rpc-url http://chain.platform.network:9944 - -# With miner hotkey filter -term-cli --hotkey 5GrwvaEF... --tab leaderboard - -# Available tabs: leaderboard, evaluation, submission, network +git clone https://github.com/PlatformNetwork/platform.git +cd platform +cp .env.example .env +# Edit .env: add your VALIDATOR_SECRET_KEY (BIP39 mnemonic) +mkdir -p data +cargo build --release --bin validator-node +./target/release/validator-node --data-dir ./data --secret-key "${VALIDATOR_SECRET_KEY}" ``` +See [Validator Operations](docs/operations/validator.md) for hardware, configuration, and monitoring. + --- -## Documentation +## Docker Policy -- [Architecture Overview](docs/architecture.md) โ€” System components, host functions, P2P messages, storage schema -- [Miner Guide](docs/miner/how-to-mine.md) โ€” How to build and submit agents -- [Submission Guide](docs/miner/submission.md) โ€” Naming, versioning, and review process -- [Validator Setup](docs/validator/setup.md) โ€” Hardware requirements, configuration, and operations +- **Production**: WASM runtime only. +- **Testing**: Docker-backed harnesses only (e.g., `./scripts/test-comprehensive.sh`). --- ## License -Apache-2.0 +MIT diff --git a/assets/banner.jpg b/assets/banner.jpg index 8e53e890599e13d289ae69069daed6b7f1c7cfd5..7688fbe89be47fb4ef74d7b02e94eece3377a905 100644 GIT binary patch literal 135143 zcmbTdcQhPt{69Ktv3d&$Q5LJW=vG}Kdha!&x9CI-y9kLy??jDGBq3@LL|G+D5WOV` zB6?@7-+sQ|bMF1;{&DY}Jv(Ri>@zd(_dGMt^L~}NoxNQJs5DhIQ~?kO06@48aJv8~ z19+hSmjBjx|1J37|J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Gx*2huC$`_!0 zMF3I#fntlq+SkM%0r-wJ)HFZ%S31t8{f&ISZKvB{xBbP}lG#r>J Self { + let timestamp = Utc::now().timestamp(); + let mut hasher = Sha256::new(); + hasher.update(number.to_le_bytes()); + hasher.update(parent_hash); + hasher.update(timestamp.to_le_bytes()); + + let hash: [u8; 32] = hasher.finalize().into(); + + Self { + number, + hash, + parent_hash, + state_root: hash, // Simplified + extrinsics_root: [0u8; 32], + timestamp, + } + } +} + +/// Block +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Block { + pub header: BlockHeader, + pub hash: [u8; 32], + pub extrinsics: Vec, +} + +/// Extrinsic (transaction) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Extrinsic { + pub hash: [u8; 32], + pub method: String, + pub params: serde_json::Value, + pub success: bool, + pub block_number: u64, + pub timestamp: i64, +} + +impl Extrinsic { + pub fn new(method: &str, params: serde_json::Value, block_number: u64) -> Self { + let timestamp = Utc::now().timestamp(); + let mut hasher = Sha256::new(); + hasher.update(method.as_bytes()); + hasher.update(serde_json::to_vec(¶ms).unwrap_or_default()); + hasher.update(block_number.to_le_bytes()); + + Self { + hash: hasher.finalize().into(), + method: method.to_string(), + params, + success: true, + block_number, + timestamp, + } + } +} + +/// Chain state +pub struct Chain { + pub blocks: HashMap, + pub block_hashes: HashMap, + pub pending_extrinsics: Vec, + pub finalized_number: u64, + pub config: ChainConfig, +} + +#[derive(Clone, Debug)] +pub struct ChainConfig { + pub tempo: u64, + pub netuid: u16, + pub commit_reveal: bool, + pub reveal_period: u64, + pub ss58_format: u16, + pub token_decimals: u8, +} + +impl Chain { + pub fn new(config: &Config) -> Self { + let chain_config = ChainConfig { + tempo: config.tempo, + netuid: config.netuid, + commit_reveal: config.commit_reveal, + reveal_period: config.reveal_period, + ss58_format: 42, + token_decimals: 9, + }; + + let mut chain = Self { + blocks: HashMap::new(), + block_hashes: HashMap::new(), + pending_extrinsics: Vec::new(), + finalized_number: 0, + config: chain_config, + }; + + // Create genesis block + chain.create_genesis(); + chain + } + + fn create_genesis(&mut self) { + let header = BlockHeader::new(0, [0u8; 32]); + let block = Block { + hash: header.hash, + header, + extrinsics: Vec::new(), + }; + + let hash_hex = format!("0x{}", hex::encode(block.hash)); + self.block_hashes.insert(hash_hex, 0); + self.blocks.insert(0, block); + } + + /// Produce a new block + pub fn produce_block(&mut self) -> Block { + let current_number = self.best_number(); + let new_number = current_number + 1; + + // Get parent hash + let parent_hash = if let Some(block) = self.blocks.get(¤t_number) { + block.hash + } else { + [0u8; 32] + }; + + // Include pending extrinsics + let extrinsics: Vec = self.pending_extrinsics.drain(..).collect(); + + let header = BlockHeader::new(new_number, parent_hash); + let block = Block { + hash: header.hash, + header: header.clone(), + extrinsics: extrinsics.clone(), + }; + + // Store block + let hash_hex = format!("0x{}", hex::encode(block.hash)); + self.block_hashes.insert(hash_hex, new_number); + self.blocks.insert(new_number, block.clone()); + + // Update finalized (simplified: finalize 3 blocks back) + if new_number >= 3 { + self.finalized_number = new_number - 3; + } + + block + } + + /// Get best (latest) block number + pub fn best_number(&self) -> u64 { + self.blocks.keys().max().copied().unwrap_or(0) + } + + /// Get block by number + pub fn get_block(&self, number: u64) -> Option<&Block> { + self.blocks.get(&number) + } + + /// Get block by hash + pub fn get_block_by_hash(&self, hash: &str) -> Option<&Block> { + self.block_hashes.get(hash).and_then(|n| self.blocks.get(n)) + } + + /// Get finalized block number + pub fn finalized_number(&self) -> u64 { + self.finalized_number + } + + /// Submit extrinsic + pub fn submit_extrinsic(&mut self, method: &str, params: serde_json::Value) -> Extrinsic { + let block_number = self.best_number(); + let extrinsic = Extrinsic::new(method, params, block_number); + self.pending_extrinsics.push(extrinsic.clone()); + extrinsic + } + + /// Get runtime version + pub fn runtime_version(&self) -> serde_json::Value { + serde_json::json!({ + "specName": "subtensor", + "implName": "mock-subtensor", + "authoringVersion": 1, + "specVersion": 100, + "implVersion": 1, + "apis": [ + ["0xdf6acb689907609b", 3], + ["0x37e397fc7c91f5e4", 1], + ["0x40fe3ad401f8949", 5], + ["0xd2bc9897eed08f15", 3], + ["0xf78b278be53f454c", 2], + ["0xaf2c0297a23e6d3d", 2], + ["0xed99c5acb25eedf5", 2], + ["0xcbca25e39f142387", 2], + ["0x687ad44ad37b03f2", 1], + ["0xab3c0572291feb8b", 1], + ["0xbc9d89904f5b923f", 1], + ["0x37c8bb1350a9a2a8", 1], + ], + "transactionVersion": 10, + "stateVersion": 0, + }) + } + + /// Get chain properties + pub fn properties(&self) -> serde_json::Value { + serde_json::json!({ + "ss58Format": self.config.ss58_format, + "tokenDecimals": self.config.token_decimals, + "tokenSymbol": "TAO" + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_config() -> Config { + Config { + bind: "127.0.0.1:9944".parse().unwrap(), + tempo: 12, + netuid: 100, + validator_count: 256, + min_stake: 1_000_000_000_000, + commit_reveal: true, + reveal_period: 12, + log_level: "info".to_string(), + inspection: true, + } + } + + #[test] + fn test_chain_genesis() { + let chain = Chain::new(&test_config()); + assert_eq!(chain.best_number(), 0); + assert!(chain.get_block(0).is_some()); + } + + #[test] + fn test_block_production() { + let mut chain = Chain::new(&test_config()); + let block = chain.produce_block(); + assert_eq!(block.header.number, 1); + assert_eq!(chain.best_number(), 1); + } + + #[test] + fn test_block_hash_lookup() { + let mut chain = Chain::new(&test_config()); + let block = chain.produce_block(); + let hash_hex = format!("0x{}", hex::encode(block.hash)); + + assert!(chain.get_block_by_hash(&hash_hex).is_some()); + assert_eq!(chain.get_block_by_hash(&hash_hex).unwrap().header.number, 1); + } + + #[test] + fn test_extrinsic_submission() { + let mut chain = Chain::new(&test_config()); + let ext = chain.submit_extrinsic("test_method", serde_json::json!({"test": true})); + + assert_eq!(ext.method, "test_method"); + assert!(ext.success); + + // Produce block to include extrinsic + let block = chain.produce_block(); + assert_eq!(block.extrinsics.len(), 1); + } + + #[test] + fn test_runtime_version() { + let chain = Chain::new(&test_config()); + let version = chain.runtime_version(); + + assert!(version.get("specName").is_some()); + assert!(version.get("apis").is_some()); + } + + #[test] + fn test_chain_properties() { + let chain = Chain::new(&test_config()); + let props = chain.properties(); + + assert_eq!(props["ss58Format"], 42); + assert_eq!(props["tokenSymbol"], "TAO"); + assert_eq!(props["tokenDecimals"], 9); + } +} diff --git a/bins/mock-subtensor/src/jsonrpc.rs b/bins/mock-subtensor/src/jsonrpc.rs new file mode 100644 index 000000000..d9871966d --- /dev/null +++ b/bins/mock-subtensor/src/jsonrpc.rs @@ -0,0 +1,968 @@ +//! JSON-RPC 2.0 module - Substrate-compatible RPC handlers + +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use std::sync::Arc; +use tracing::{trace, warn}; + +use crate::{state::WeightCommitment, AppState}; + +/// JSON-RPC 2.0 Request +#[derive(Debug, Clone, Deserialize)] +pub struct JsonRpcRequest { + pub jsonrpc: String, + pub method: String, + #[serde(default)] + pub params: Value, + pub id: Value, +} + +/// JSON-RPC 2.0 Response +#[derive(Debug, Clone, Serialize)] +pub struct JsonRpcResponse { + pub jsonrpc: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + pub id: Value, +} + +/// JSON-RPC 2.0 Error +#[derive(Debug, Clone, Serialize)] +pub struct RpcError { + pub code: i32, + pub message: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, +} + +impl JsonRpcResponse { + /// Create a successful response + pub fn result(id: Value, result: Value) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: Some(result), + error: None, + id, + } + } + + /// Create an error response + pub fn error(id: Value, code: i32, message: impl Into) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: None, + error: Some(RpcError { + code, + message: message.into(), + data: None, + }), + id, + } + } + + /// Create an error with data + #[allow(dead_code)] + pub fn error_with_data(id: Value, code: i32, message: impl Into, data: Value) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: None, + error: Some(RpcError { + code, + message: message.into(), + data: Some(data), + }), + id, + } + } +} + +// Standard JSON-RPC error codes +#[allow(dead_code)] +pub const PARSE_ERROR: i32 = -32700; +#[allow(dead_code)] +pub const INVALID_REQUEST: i32 = -32600; +#[allow(dead_code)] +pub const METHOD_NOT_FOUND: i32 = -32601; +#[allow(dead_code)] +pub const INVALID_PARAMS: i32 = -32602; +#[allow(dead_code)] +pub const INTERNAL_ERROR: i32 = -32603; + +/// RPC Handler +pub struct RpcHandler { + state: Arc, +} + +impl RpcHandler { + pub fn new(state: Arc) -> Self { + Self { state } + } + + /// Handle a JSON-RPC request + pub fn handle(&self, req: JsonRpcRequest) -> JsonRpcResponse { + trace!("RPC: {}", req.method); + + // Verify JSON-RPC version + if req.jsonrpc != "2.0" { + return JsonRpcResponse::error(req.id, INVALID_REQUEST, "Invalid JSON-RPC version"); + } + + // Route to appropriate handler + match req.method.as_str() { + // System namespace + "system_health" => self.system_health(req.id), + "system_version" => self.system_version(req.id), + "system_name" => self.system_name(req.id), + "system_properties" => self.system_properties(req.id), + "system_peers" => self.system_peers(req.id), + "system_chain" => self.system_chain(req.id), + "system_chainType" => self.system_chain_type(req.id), + "system_syncState" => self.system_sync_state(req.id), + "system_addLogFilter" => self.system_add_log_filter(req.id, req.params), + + // Chain namespace + "chain_getHeader" => self.chain_get_header(req.id, req.params), + "chain_getHead" => self.chain_get_head(req.id), + "chain_getBlock" => self.chain_get_block(req.id, req.params), + "chain_getBlockHash" => self.chain_get_block_hash(req.id, req.params), + "chain_getFinalizedHead" => self.chain_get_finalized_head(req.id), + "chain_getFinalizedBlock" => self.chain_get_finalized_block(req.id), + "chain_subscribeNewHeads" => self.chain_subscribe_new_heads(req.id), + "chain_subscribeFinalizedHeads" => self.chain_subscribe_finalized_heads(req.id), + "chain_unsubscribeNewHeads" => self.chain_unsubscribe_new_heads(req.id, req.params), + "chain_unsubscribeFinalizedHeads" => { + self.chain_unsubscribe_finalized_heads(req.id, req.params) + } + + // State namespace + "state_getStorage" => self.state_get_storage(req.id, req.params), + "state_getKeys" => self.state_get_keys(req.id, req.params), + "state_getKeysPaged" => self.state_get_keys_paged(req.id, req.params), + "state_getMetadata" => self.state_get_metadata(req.id), + "state_getRuntimeVersion" => self.state_get_runtime_version(req.id), + "state_subscribeStorage" => self.state_subscribe_storage(req.id, req.params), + "state_unsubscribeStorage" => self.state_unsubscribe_storage(req.id, req.params), + "state_queryStorageAt" => self.state_query_storage_at(req.id, req.params), + + // Author namespace + "author_submitExtrinsic" => self.author_submit_extrinsic(req.id, req.params), + "author_pendingExtrinsics" => self.author_pending_extrinsics(req.id), + "author_submitAndWatchExtrinsic" => { + self.author_submit_and_watch_extrinsic(req.id, req.params) + } + + // Subtensor-specific methods + "subtensor_getNeurons" => self.subtensor_get_neurons(req.id, req.params), + "subtensor_getNeuronLite" => self.subtensor_get_neuron_lite(req.id, req.params), + "subtensor_getSubnetInfo" => self.subtensor_get_subnet_info(req.id, req.params), + "subtensor_getBalance" => self.subtensor_get_balance(req.id, req.params), + "subtensor_commitWeights" => self.subtensor_commit_weights(req.id, req.params), + "subtensor_revealWeights" => self.subtensor_reveal_weights(req.id, req.params), + + // Bittensor compatibility + "bt_getNeurons" => self.subtensor_get_neurons(req.id, req.params), + "bt_getBalance" => self.subtensor_get_balance(req.id, req.params), + + // RPC discovery + "rpc_methods" => self.rpc_methods(req.id), + "rpc_discover" => self.rpc_discover(req.id), + + _ => { + warn!("Unknown RPC method: {}", req.method); + JsonRpcResponse::error( + req.id, + METHOD_NOT_FOUND, + format!("Method not found: {}", req.method), + ) + } + } + } + + // ==================== System Namespace ==================== + + fn system_health(&self, id: Value) -> JsonRpcResponse { + let _chain = self.state.chain.read(); + let metagraph = self.state.metagraph.read(); + + JsonRpcResponse::result( + id, + json!({ + "peers": metagraph.validators.len(), + "isSyncing": false, + "shouldHavePeers": true, + "genesisHash": format!("0x{}", hex::encode([0u8; 32])), + }), + ) + } + + fn system_version(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!("mock-subtensor/1.0.0")) + } + + fn system_name(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!("mock-subtensor")) + } + + fn system_properties(&self, id: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + JsonRpcResponse::result(id, chain.properties()) + } + + fn system_peers(&self, id: Value) -> JsonRpcResponse { + let metagraph = self.state.metagraph.read(); + let peers: Vec = metagraph + .validators + .values() + .filter(|v| v.validator_permit) + .map(|v| { + json!({ + "peerId": format!("12D3KooW{}", &v.hotkey[4..16]), + "roles": "FULL", + "bestHash": format!("0x{}", hex::encode(&v.hotkey.as_bytes()[0..4])), + "bestNumber": 0, + }) + }) + .collect(); + + JsonRpcResponse::result(id, json!(peers)) + } + + fn system_chain(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!("Bittensor")) + } + + fn system_chain_type(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!("Live")) + } + + fn system_sync_state(&self, id: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + JsonRpcResponse::result( + id, + json!({ + "startingBlock": 0, + "currentBlock": chain.best_number(), + "highestBlock": chain.best_number(), + "syncPeer": 1, + "warpSyncProgress": null, + }), + ) + } + + fn system_add_log_filter(&self, id: Value, _params: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(null)) + } + + // ==================== Chain Namespace ==================== + + fn chain_get_head(&self, id: Value) -> JsonRpcResponse { + self.chain_get_header(id, Value::Null) + } + + fn chain_get_header(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + + // Parse block hash parameter if provided + let block_number = if let Some(hash) = params.get(0).and_then(|h| h.as_str()) { + chain + .get_block_by_hash(hash) + .map(|b| b.header.number) + .unwrap_or_else(|| chain.best_number()) + } else { + chain.best_number() + }; + + let block = chain + .get_block(block_number) + .cloned() + .unwrap_or_else(|| chain.get_block(0).cloned().unwrap()); + + JsonRpcResponse::result( + id, + json!({ + "number": block.header.number, + "hash": format!("0x{}", hex::encode(block.hash)), + "parentHash": format!("0x{}", hex::encode(block.header.parent_hash)), + "stateRoot": format!("0x{}", hex::encode(block.header.state_root)), + "extrinsicsRoot": format!("0x{}", hex::encode(block.header.extrinsics_root)), + "digest": { + "logs": [] + }, + }), + ) + } + + fn chain_get_block(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + + let block_number = if let Some(hash) = params.get(0).and_then(|h| h.as_str()) { + chain + .get_block_by_hash(hash) + .map(|b| b.header.number) + .unwrap_or(0) + } else { + chain.best_number() + }; + + let block = match chain.get_block(block_number) { + Some(b) => b, + None => return JsonRpcResponse::result(id, Value::Null), + }; + + let extrinsics: Vec = block + .extrinsics + .iter() + .map(|e| format!("0x{}", hex::encode(e.hash))) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "block": { + "header": { + "number": block.header.number, + "hash": format!("0x{}", hex::encode(block.hash)), + "parentHash": format!("0x{}", hex::encode(block.header.parent_hash)), + "stateRoot": format!("0x{}", hex::encode(block.header.state_root)), + "extrinsicsRoot": format!("0x{}", hex::encode(block.header.extrinsics_root)), + "digest": { + "logs": [] + }, + }, + "extrinsics": extrinsics, + }, + "justifications": null, + }), + ) + } + + fn chain_get_block_hash(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + + let block_number = params + .get(0) + .and_then(|n| n.as_u64()) + .unwrap_or_else(|| chain.best_number()); + + match chain.get_block(block_number) { + Some(block) => { + JsonRpcResponse::result(id, json!(format!("0x{}", hex::encode(block.hash)))) + } + None => JsonRpcResponse::result(id, Value::Null), + } + } + + fn chain_get_finalized_head(&self, id: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + let finalized = chain.finalized_number(); + + match chain.get_block(finalized) { + Some(block) => { + JsonRpcResponse::result(id, json!(format!("0x{}", hex::encode(block.hash)))) + } + None => JsonRpcResponse::result(id, Value::Null), + } + } + + fn chain_get_finalized_block(&self, id: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + let finalized = chain.finalized_number(); + + match chain.get_block(finalized) { + Some(b) => JsonRpcResponse::result( + id, + json!({ + "number": b.header.number, + "hash": format!("0x{}", hex::encode(b.hash)), + }), + ), + None => JsonRpcResponse::result(id, Value::Null), + } + } + + fn chain_subscribe_new_heads(&self, id: Value) -> JsonRpcResponse { + let subscription_id = format!("{:?}", id); + JsonRpcResponse::result( + id, + json!({ + "subscription": subscription_id, + "result": null, + }), + ) + } + + fn chain_subscribe_finalized_heads(&self, id: Value) -> JsonRpcResponse { + let subscription_id = format!("{:?}", id); + JsonRpcResponse::result( + id, + json!({ + "subscription": subscription_id, + "result": null, + }), + ) + } + + fn chain_unsubscribe_new_heads(&self, id: Value, _params: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(true)) + } + + fn chain_unsubscribe_finalized_heads(&self, id: Value, _params: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(true)) + } + + // ==================== State Namespace ==================== + + fn state_get_storage(&self, id: Value, params: Value) -> JsonRpcResponse { + let key = params.get(0).and_then(|k| k.as_str()); + + if key.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing storage key"); + } + + let key = key.unwrap(); + let _chain = self.state.chain.read(); + let metagraph = self.state.metagraph.read(); + + // Parse storage key (simplified) + let result = if key.starts_with("0x") && key.len() > 66 && key.contains("Balances") { + // Balance query - return mock balance + json!(format!("0x{:016x}", 1000000000000u64)) + } else if key.contains("SubtensorModule") { + // Subtensor storage query + if key.contains("Neurons") { + let neurons = metagraph.get_neurons(); + json!(hex::encode( + serde_json::to_vec(&neurons).unwrap_or_default() + )) + } else if key.contains("Uids") { + json!(hex::encode( + (metagraph.validators.len() as u16).to_le_bytes() + )) + } else { + json!(null) + } + } else { + json!(null) + }; + + JsonRpcResponse::result(id, result) + } + + fn state_get_keys(&self, id: Value, params: Value) -> JsonRpcResponse { + let prefix = params.get(0).and_then(|p| p.as_str()).unwrap_or(""); + + // Return some mock keys + let keys: Vec = vec![ + format!("{}/Balances/TotalIssuance", prefix), + format!("{}/SubtensorModule/Uids", prefix), + format!("{}/System/BlockHash", prefix), + ]; + + JsonRpcResponse::result(id, json!(keys)) + } + + fn state_get_keys_paged(&self, id: Value, params: Value) -> JsonRpcResponse { + self.state_get_keys(id, params) + } + + fn state_get_metadata(&self, id: Value) -> JsonRpcResponse { + let _chain = self.state.chain.read(); + let metagraph = self.state.metagraph.read(); + + JsonRpcResponse::result( + id, + json!({ + "version": 14, + "modules": [ + { + "name": "System", + "storage": [{"name": "BlockHash", "modifier": "Default", "type": "map"}], + "calls": [], + "events": [], + "constants": [], + "errors": [], + }, + { + "name": "Balances", + "storage": [{"name": "TotalIssuance", "modifier": "Default", "type": "value"}], + "calls": ["transfer", "set_balance"], + "events": [], + "constants": [], + "errors": [], + }, + { + "name": "SubtensorModule", + "storage": [ + {"name": "Neurons", "modifier": "Default", "type": "map"}, + {"name": "Uids", "modifier": "Default", "type": "value"}, + ], + "calls": ["add_stake", "remove_stake", "commit_weights", "reveal_weights"], + "events": [], + "constants": [], + "errors": [], + }, + ], + "extrinsic": { + "version": 4, + "signedExtensions": [ + "CheckSpecVersion", + "CheckTxVersion", + "CheckGenesis", + "CheckMortality", + "CheckNonce", + "CheckWeight", + "ChargeTransactionPayment", + ], + }, + "netuid": self.state.config.netuid, + "tempo": self.state.config.tempo, + "validator_count": metagraph.validator_count, + }), + ) + } + + fn state_get_runtime_version(&self, id: Value) -> JsonRpcResponse { + let _chain = self.state.chain.read(); + JsonRpcResponse::result(id, self.state.chain.read().runtime_version()) + } + + fn state_subscribe_storage(&self, id: Value, _params: Value) -> JsonRpcResponse { + let subscription_id = format!("{:?}", id); + JsonRpcResponse::result( + id, + json!({ + "subscription": subscription_id, + "result": null, + }), + ) + } + + fn state_unsubscribe_storage(&self, id: Value, _params: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(true)) + } + + fn state_query_storage_at(&self, id: Value, params: Value) -> JsonRpcResponse { + let keys = params.get(0).and_then(|k| k.as_array()); + + if keys.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing keys array"); + } + + let results: Vec = keys + .unwrap() + .iter() + .map(|k| { + json!({ + "block": format!("0x{}", hex::encode([0u8; 32])), + "key": k, + "value": null, + }) + }) + .collect(); + + JsonRpcResponse::result(id, json!(results)) + } + + // ==================== Author Namespace ==================== + + fn author_submit_extrinsic(&self, id: Value, params: Value) -> JsonRpcResponse { + let extrinsic_data = params.get(0).and_then(|e| e.as_str()); + + if extrinsic_data.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing extrinsic data"); + } + + let extrinsic_hex = extrinsic_data.unwrap(); + let mut chain = self.state.chain.write(); + + // Parse extrinsic (simplified) + let method = "author_submitExtrinsic"; + let params = json!({ + "extrinsic": extrinsic_hex, + }); + + let extrinsic = chain.submit_extrinsic(method, params); + + JsonRpcResponse::result(id, json!(format!("0x{}", hex::encode(extrinsic.hash)))) + } + + fn author_pending_extrinsics(&self, id: Value) -> JsonRpcResponse { + let chain = self.state.chain.read(); + + let extrinsics: Vec = chain + .pending_extrinsics + .iter() + .map(|e| format!("0x{}", hex::encode(e.hash))) + .collect(); + + JsonRpcResponse::result(id, json!(extrinsics)) + } + + fn author_submit_and_watch_extrinsic(&self, id: Value, params: Value) -> JsonRpcResponse { + // Same as submit_extrinsic but returns subscription + self.author_submit_extrinsic(id, params) + } + + // ==================== Subtensor Namespace ==================== + + fn subtensor_get_neurons(&self, id: Value, params: Value) -> JsonRpcResponse { + let netuid = params.get(0).and_then(|n| n.as_u64()).unwrap_or(0) as u16; + + if netuid != self.state.config.netuid { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("NetUID {} not found", netuid), + ); + } + + let metagraph = self.state.metagraph.read(); + let neurons = metagraph.get_neurons(); + + JsonRpcResponse::result(id, json!(neurons)) + } + + fn subtensor_get_neuron_lite(&self, id: Value, params: Value) -> JsonRpcResponse { + let netuid = params.get(0).and_then(|n| n.as_u64()).unwrap_or(0) as u16; + let uid = params.get(1).and_then(|u| u.as_u64()).map(|u| u as u16); + + let metagraph = self.state.metagraph.read(); + + if netuid != self.state.config.netuid { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("NetUID {} not found", netuid), + ); + } + + if let Some(uid) = uid { + match metagraph.get_validator(uid) { + Some(v) => JsonRpcResponse::result(id, v.to_neuron_info_lite()), + None => JsonRpcResponse::error(id, INVALID_PARAMS, "UID not found"), + } + } else { + // Return all neurons lite + let neurons = metagraph.get_neurons_lite(); + JsonRpcResponse::result(id, json!(neurons)) + } + } + + fn subtensor_get_subnet_info(&self, id: Value, params: Value) -> JsonRpcResponse { + let netuid = params.get(0).and_then(|n| n.as_u64()).unwrap_or(0) as u16; + let metagraph = self.state.metagraph.read(); + let chain = self.state.chain.read(); + + if netuid != self.state.config.netuid { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("NetUID {} not found", netuid), + ); + } + + JsonRpcResponse::result( + id, + json!({ + "netuid": netuid, + "rho": 10, + "kappa": 32767, + "min_allowed_weights": 8, + "max_weights_limit": 512, + "tempo": metagraph.tempo, + "difficulty": 1000000000000u64, + "immunity_period": 7200, + "max_allowed_validators": metagraph.validator_count, + "min_allowed_uids": 8, + "max_allowed_uids": metagraph.max_uids, + "blocks_since_last_step": 0, + "blocks_until_next_epoch": 100 - (chain.best_number() % 100), + "activity_cutoff": 5000, + "max_stake": u64::MAX, + "min_stake": metagraph.min_stake, + "total_stake": metagraph.total_stake, + }), + ) + } + + fn subtensor_get_balance(&self, id: Value, params: Value) -> JsonRpcResponse { + let address = params.get(0).and_then(|a| a.as_str()); + + if address.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing address"); + } + + // Return mock balance (100 TAO) + JsonRpcResponse::result( + id, + json!(100000000000u64), // 100 TAO in RAO + ) + } + + fn subtensor_commit_weights(&self, id: Value, params: Value) -> JsonRpcResponse { + // Parse parameters + let netuid = params.get(0).and_then(|n| n.as_u64()).unwrap_or(0) as u16; + let uids = params.get(1).and_then(|u| u.as_array()); + let commitment_hash = params.get(2).and_then(|c| c.as_str()); + let hotkey = params.get(3).and_then(|h| h.as_str()); + + if hotkey.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing hotkey"); + } + + let mut metagraph = self.state.metagraph.write(); + let chain = self.state.chain.read(); + + // Verify hotkey is registered + if metagraph.get_validator_by_hotkey(hotkey.unwrap()).is_none() { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("Hotkey {} not registered", hotkey.unwrap()), + ); + } + + // Create commitment + let uids: Vec = uids + .unwrap_or(&vec![]) + .iter() + .filter_map(|v| v.as_u64().map(|u| u as u16)) + .collect(); + + let commitment = WeightCommitment::new( + hotkey.unwrap().to_string(), + netuid, + uids.clone(), + vec![65535; uids.len()], // Mock weights + commitment_hash.unwrap_or("").to_string(), + chain.best_number(), + ); + + match metagraph.commit_weights(commitment) { + Ok(_) => JsonRpcResponse::result(id, json!(true)), + Err(e) => JsonRpcResponse::error(id, INTERNAL_ERROR, e), + } + } + + fn subtensor_reveal_weights(&self, id: Value, params: Value) -> JsonRpcResponse { + // Parse parameters + let _netuid = params.get(0).and_then(|n| n.as_u64()).unwrap_or(0) as u16; + let uids = params.get(1).and_then(|u| u.as_array()); + let weights = params.get(2).and_then(|w| w.as_array()); + let salt = params.get(3).and_then(|s| s.as_str()); + let hotkey = params.get(4).and_then(|h| h.as_str()); + + if hotkey.is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing hotkey"); + } + + let mut metagraph = self.state.metagraph.write(); + let chain = self.state.chain.read(); + + let uids: Vec = uids + .unwrap_or(&vec![]) + .iter() + .filter_map(|v| v.as_u64().map(|u| u as u16)) + .collect(); + + let weights: Vec = weights + .unwrap_or(&vec![]) + .iter() + .filter_map(|v| v.as_u64().map(|u| u as u16)) + .collect(); + + match metagraph.reveal_weights( + hotkey.unwrap(), + uids, + weights, + salt.unwrap_or("").to_string(), + chain.best_number(), + ) { + Ok(_) => JsonRpcResponse::result(id, json!(true)), + Err(e) => JsonRpcResponse::error(id, INTERNAL_ERROR, e), + } + } + + // ==================== RPC Discovery ==================== + + fn rpc_methods(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result( + id, + json!({ + "methods": [ + "system_health", + "system_version", + "system_name", + "system_properties", + "system_peers", + "system_chain", + "system_chainType", + "chain_getHeader", + "chain_getHead", + "chain_getBlock", + "chain_getBlockHash", + "chain_getFinalizedHead", + "chain_getFinalizedBlock", + "chain_subscribeNewHeads", + "chain_subscribeFinalizedHeads", + "state_getStorage", + "state_getKeys", + "state_getKeysPaged", + "state_getMetadata", + "state_getRuntimeVersion", + "state_subscribeStorage", + "state_unsubscribeStorage", + "state_queryStorageAt", + "author_submitExtrinsic", + "author_pendingExtrinsics", + "subtensor_getNeurons", + "subtensor_getNeuronLite", + "subtensor_getSubnetInfo", + "subtensor_getBalance", + "subtensor_commitWeights", + "subtensor_revealWeights", + "rpc_methods", + "rpc_discover", + ], + "version": 1, + }), + ) + } + + fn rpc_discover(&self, id: Value) -> JsonRpcResponse { + // OpenRPC discovery + JsonRpcResponse::result( + id, + json!({ + "openrpc": "1.0.0", + "info": { + "title": "Mock Subtensor RPC", + "version": "1.0.0", + }, + "methods": [ + { + "name": "system_health", + "description": "Returns the current health status of the node", + "params": [], + "result": {"name": "health", "schema": {"type": "object"}}, + }, + { + "name": "chain_getBlock", + "description": "Get block by hash or number", + "params": [{"name": "hash", "schema": {"type": "string"}}], + "result": {"name": "block", "schema": {"type": "object"}}, + }, + { + "name": "subtensor_getNeurons", + "description": "Get all neurons for a subnet", + "params": [{"name": "netuid", "schema": {"type": "integer"}}], + "result": {"name": "neurons", "schema": {"type": "array"}}, + }, + ], + }), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_state() -> Arc { + let config = crate::Config { + bind: "127.0.0.1:9944".parse().unwrap(), + tempo: 12, + netuid: 100, + validator_count: 256, + min_stake: 1_000_000_000_000, + commit_reveal: true, + reveal_period: 12, + log_level: "info".to_string(), + inspection: true, + }; + Arc::new(AppState::new(config)) + } + + #[test] + fn test_system_health() { + let state = test_state(); + let handler = RpcHandler::new(state); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_health".to_string(), + params: json!(null), + id: json!(1), + }; + + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_chain_get_head() { + let state = test_state(); + let handler = RpcHandler::new(state); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getHead".to_string(), + params: json!(null), + id: json!(1), + }; + + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_subtensor_get_neurons() { + let state = test_state(); + let handler = RpcHandler::new(state); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "subtensor_getNeurons".to_string(), + params: json!([100]), + id: json!(1), + }; + + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(!result.as_array().unwrap().is_empty()); + } + + #[test] + fn test_unknown_method() { + let state = test_state(); + let handler = RpcHandler::new(state); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "unknown_method".to_string(), + params: json!(null), + id: json!(1), + }; + + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, METHOD_NOT_FOUND); + } + + #[test] + fn test_rpc_methods() { + let state = test_state(); + let handler = RpcHandler::new(state); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "rpc_methods".to_string(), + params: json!(null), + id: json!(1), + }; + + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + let methods = result["methods"].as_array().unwrap(); + assert!(methods.len() > 10); + } +} diff --git a/bins/mock-subtensor/src/main.rs b/bins/mock-subtensor/src/main.rs new file mode 100644 index 000000000..a8170617c --- /dev/null +++ b/bins/mock-subtensor/src/main.rs @@ -0,0 +1,194 @@ +//! Mock Subtensor Binary +//! +//! Simulates a Bittensor RPC node for testing without real chain connectivity. +//! Implements WebSocket JSON-RPC 2.0 server with Substrate-compatible methods: +//! - chain_getHeader, chain_getBlock, chain_getBlockHash, chain_getFinalizedHead +//! - state_getMetadata, state_getStorage, state_getKeys, state_getRuntimeVersion +//! - system_health, system_version, system_name, system_properties, system_peers +//! - author_submitExtrinsic +//! +//! Features: +//! - Simulated block production with configurable tempo (default 12s blocks) +//! - Mock metagraph state with 256 synthetic validators and realistic stake distribution +//! - Weight submissions with commit-reveal mechanism simulation +//! - Test inspection endpoints + +use anyhow::Result; +use clap::Parser; +use parking_lot::RwLock; +use serde_json::{json, Value}; +use std::net::SocketAddr; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::broadcast; +use tokio::time::interval; +use tracing::info; + +mod chain; +mod jsonrpc; +mod state; +mod websocket; + +use chain::Chain; +use state::MockMetagraph; +use websocket::WsServer; + +/// Mock Subtensor Configuration +#[derive(Parser, Debug, Clone)] +#[command(name = "mock-subtensor")] +#[command(about = "Mock Bittensor RPC node for testing")] +pub struct Config { + /// HTTP/WS listen address + #[arg(short, long, default_value = "0.0.0.0:9944")] + pub bind: SocketAddr, + + /// Block production tempo in seconds + #[arg(short, long, default_value = "12")] + pub tempo: u64, + + /// Subnet UID (netuid) + #[arg(long, default_value = "100")] + pub netuid: u16, + + /// Number of synthetic validators + #[arg(long, default_value = "256")] + pub validator_count: u16, + + /// Minimum stake for validators (in RAO) + #[arg(long, default_value = "1000000000000")] + pub min_stake: u64, + + /// Enable commit-reveal mechanism + #[arg(long, default_value = "true")] + pub commit_reveal: bool, + + /// Reveal period in blocks + #[arg(long, default_value = "12")] + pub reveal_period: u64, + + /// Log level + #[arg(short, long, default_value = "info")] + pub log_level: String, + + /// Enable test inspection endpoints + #[arg(long, default_value = "true")] + pub inspection: bool, +} + +/// Shared application state +pub struct AppState { + pub chain: Arc>, + pub metagraph: Arc>, + pub config: Config, + pub broadcast_tx: broadcast::Sender, +} + +impl AppState { + pub fn new(config: Config) -> Self { + let chain = Arc::new(RwLock::new(Chain::new(&config))); + let metagraph = Arc::new(RwLock::new(MockMetagraph::new(&config))); + let (broadcast_tx, _rx) = broadcast::channel(256); + + Self { + chain, + metagraph, + config, + broadcast_tx, + } + } +} + +#[tokio::main] +async fn main() -> Result<()> { + let config = Config::parse(); + + // Initialize tracing + let filter = tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(&config.log_level)); + + tracing_subscriber::fmt() + .with_env_filter(filter) + .with_target(true) + .with_thread_ids(false) + .init(); + + info!("โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—"); + info!("โ•‘ Mock Subtensor RPC Node โ•‘"); + info!("โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"); + info!("Bind address: {}", config.bind); + info!("Block tempo: {}s", config.tempo); + info!("NetUID: {}", config.netuid); + info!("Validators: {}", config.validator_count); + info!("Commit-reveal: {}", config.commit_reveal); + info!(""); + info!("Methods available:"); + info!(" - chain_getHeader, chain_getBlock, chain_getBlockHash"); + info!(" - state_getMetadata, state_getStorage, state_getKeys"); + info!(" - system_health, system_version, system_name"); + info!(" - author_submitExtrinsic"); + info!(" - subtensor_commitWeights, subtensor_revealWeights"); + info!(" - subtensor_getNeurons, subtensor_getNeuronLite"); + info!(""); + info!("Test endpoints:"); + info!(" - GET /test/state - Current chain state"); + info!(" - GET /test/metagraph - Full metagraph info"); + info!(" - GET /test/weights - Pending weight commits"); + info!(" - POST /test/advance - Advance block manually"); + info!(""); + + let state = Arc::new(AppState::new(config.clone())); + + // Spawn block production task + let block_state = state.clone(); + let _block_task = tokio::spawn(block_production_task(block_state, config.tempo)); + + // Spawn WebSocket server + let ws_server = WsServer::new(state); + ws_server.run(config.bind).await +} + +/// Block production background task +async fn block_production_task(state: Arc, tempo: u64) { + let mut ticker = interval(Duration::from_secs(tempo)); + ticker.tick().await; // Skip first tick + + loop { + ticker.tick().await; + + let mut chain = state.chain.write(); + let block = chain.produce_block(); + let block_number = block.header.number; + let block_hash = format!("0x{}", hex::encode(&block.hash[0..4])); + + info!("Block #{} produced (hash: ...{})", block_number, block_hash); + + // Drop lock before broadcasting + drop(chain); + + // Notify WebSocket subscribers + let notification = json!({ + "jsonrpc": "2.0", + "method": "chain_newHead", + "params": { + "result": { + "number": block_number, + "hash": format!("0x{}", hex::encode(block.hash)), + "parentHash": format!("0x{}", hex::encode(block.header.parent_hash)), + }, + "subscription": "chain" + } + }); + + let _ = state.broadcast_tx.send(notification); + + // Check epoch boundary for commit-reveal + let blocks_per_epoch = 100u64; + let block_in_epoch = block_number % blocks_per_epoch; + + if block_in_epoch == 75 { + info!("=== COMMIT WINDOW OPEN (epoch boundary) ==="); + } else if block_in_epoch == 88 { + info!("=== REVEAL WINDOW OPEN ==="); + } + } +} diff --git a/bins/mock-subtensor/src/state.rs b/bins/mock-subtensor/src/state.rs new file mode 100644 index 000000000..88b824666 --- /dev/null +++ b/bins/mock-subtensor/src/state.rs @@ -0,0 +1,600 @@ +//! State module - Mock metagraph and validator management + +use super::Config; +use chrono::Utc; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; + +/// Validator in the mock metagraph +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Validator { + pub uid: u16, + pub hotkey: String, + pub coldkey: String, + pub stake: u64, + pub trust: f64, + pub validator_trust: f64, + pub consensus: f64, + pub incentive: f64, + pub dividends: f64, + pub emission: u64, + pub validator_permit: bool, + pub last_update: u64, + pub active: bool, + pub axon_info: AxonInfo, + pub prometheus_info: PrometheusInfo, +} + +impl Validator { + pub fn with_uid(uid: u16, netuid: u16) -> Self { + let mut rng = StdRng::seed_from_u64(uid as u64 + netuid as u64); + + // Deterministic hotkey generation + let mut hasher = Sha256::new(); + hasher.update(b"validator"); + hasher.update(uid.to_le_bytes()); + hasher.update(netuid.to_le_bytes()); + let hotkey_bytes: [u8; 32] = hasher.finalize().into(); + + // Generate SS58 address + let hotkey = ss58_encode(42, &hotkey_bytes); + + // Coldkey is different + let mut hasher_cold = Sha256::new(); + hasher_cold.update(b"coldkey"); + hasher_cold.update(uid.to_le_bytes()); + let coldkey_bytes: [u8; 32] = hasher_cold.finalize().into(); + let coldkey = ss58_encode(42, &coldkey_bytes); + + // Realistic stake distribution - most validators have moderate stake + // Pareto-like distribution: few validators with high stake, many with low + let stake_tao = match uid % 10 { + 0 => rng.gen_range(5000.0..50000.0), // 10% whales + 1..=3 => rng.gen_range(1000.0..5000.0), // 30% high + _ => rng.gen_range(100.0..1000.0), // 60% moderate + }; + let stake = (stake_tao * 1_000_000_000.0) as u64; // Convert to RAO + + // Generate realistic performance metrics + let trust = rng.gen_range(0.5..1.0); + let validator_trust = if uid.is_multiple_of(4) { + rng.gen_range(0.7..1.0) // Validators + } else { + rng.gen_range(0.0..0.5) // Miners + }; + let consensus = rng.gen_range(0.5..1.0); + let incentive = rng.gen_range(0.0..1.0); + let dividends = rng.gen_range(0.0..1.0); + let emission = rng.gen_range(0..100_000_000_000); // 0-100 TAO + + // Validator permit based on stake and trust + let validator_permit = stake > 1_000_000_000_000 && trust > 0.8; + + Self { + uid, + hotkey, + coldkey, + stake, + trust, + validator_trust, + consensus, + incentive, + dividends, + emission, + validator_permit, + last_update: rng.gen_range(100..10000), + active: true, + axon_info: AxonInfo::generate(&mut rng), + prometheus_info: PrometheusInfo::generate(&mut rng), + } + } + + pub fn to_neuron_info(&self) -> serde_json::Value { + serde_json::json!({ + "hotkey": self.hotkey, + "coldkey": self.coldkey, + "uid": self.uid, + "netuid": 0, + "active": self.active, + "axon_info": { + "block": self.axon_info.block, + "version": self.axon_info.version, + "ip": self.axon_info.ip, + "port": self.axon_info.port, + "ip_type": self.axon_info.ip_type, + "protocol": self.axon_info.protocol, + "placeholder1": 0, + "placeholder2": 0, + }, + "prometheus_info": { + "block": self.prometheus_info.block, + "version": self.prometheus_info.version, + "ip": self.prometheus_info.ip, + "port": self.prometheus_info.port, + "ip_type": self.prometheus_info.ip_type, + }, + "stake": [ + [self.coldkey.clone(), self.stake.to_string()] + ], + "rank": 0.0, + "emission": self.emission, + "incentive": self.incentive, + "consensus": self.consensus, + "trust": self.trust, + "validator_trust": self.validator_trust, + "dividends": self.dividends, + "weights": [], + "bonds": [], + "validator_permit": self.validator_permit, + }) + } + + pub fn to_neuron_info_lite(&self) -> serde_json::Value { + serde_json::json!({ + "hotkey": self.hotkey, + "coldkey": self.coldkey, + "uid": self.uid, + "stake": self.stake, + "trust": self.trust, + "validator_trust": self.validator_trust, + "consensus": self.consensus, + "incentive": self.incentive, + "dividends": self.dividends, + "emission": self.emission, + "validator_permit": self.validator_permit, + "axon_info": { + "ip": self.axon_info.ip, + "port": self.axon_info.port, + }, + }) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AxonInfo { + pub block: u64, + pub version: u32, + pub ip: u32, + pub port: u16, + pub ip_type: u8, + pub protocol: u8, +} + +impl AxonInfo { + fn generate(rng: &mut R) -> Self { + Self { + block: rng.gen_range(1000..100000), + version: rng.gen_range(1..10), + ip: rng.gen::(), + port: rng.gen_range(8000..9000), + ip_type: 4, + protocol: rng.gen_range(0..3), + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PrometheusInfo { + pub block: u64, + pub version: u32, + pub ip: u32, + pub port: u16, + pub ip_type: u8, +} + +impl PrometheusInfo { + fn generate(rng: &mut R) -> Self { + Self { + block: rng.gen_range(1000..100000), + version: rng.gen_range(1..10), + ip: rng.gen::(), + port: rng.gen_range(7000..8000), + ip_type: 4, + } + } +} + +/// Weight commitment for commit-reveal +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightCommitment { + pub hotkey: String, + pub netuid: u16, + pub uids: Vec, + pub commitment_hash: String, + pub salt: Option, + pub revealed_weights: Option>, + pub commit_block: u64, + pub reveal_block: Option, + pub revealed: bool, +} + +impl WeightCommitment { + pub fn new( + hotkey: String, + netuid: u16, + uids: Vec, + weights: Vec, + salt: String, + commit_block: u64, + ) -> Self { + // Calculate commitment hash + let mut hasher = Sha256::new(); + hasher.update(hotkey.as_bytes()); + hasher.update(netuid.to_le_bytes()); + for uid in &uids { + hasher.update(uid.to_le_bytes()); + } + for weight in &weights { + hasher.update(weight.to_le_bytes()); + } + hasher.update(salt.as_bytes()); + + let commitment_hash = format!("0x{}", hex::encode(hasher.finalize())); + + Self { + hotkey, + netuid, + uids, + commitment_hash, + salt: Some(salt), + revealed_weights: Some(weights), + commit_block, + reveal_block: None, + revealed: false, + } + } + + pub fn verify_reveal(&self, uids: &[u16], weights: &[u16], salt: &str) -> bool { + let mut hasher = Sha256::new(); + hasher.update(self.hotkey.as_bytes()); + hasher.update(self.netuid.to_le_bytes()); + for uid in uids { + hasher.update(uid.to_le_bytes()); + } + for weight in weights { + hasher.update(weight.to_le_bytes()); + } + hasher.update(salt.as_bytes()); + + let calculated_hash = format!("0x{}", hex::encode(hasher.finalize())); + calculated_hash == self.commitment_hash + } +} + +/// Mock metagraph state +pub struct MockMetagraph { + pub validators: HashMap, + pub validator_count: u16, + pub netuid: u16, + pub tempo: u16, + pub max_uids: u16, + pub min_stake: u64, + pub total_stake: u64, + pub weight_commitments: HashMap, // hotkey -> commitment + pub last_weight_update: HashMap, // hotkey -> block +} + +impl MockMetagraph { + pub fn new(config: &Config) -> Self { + let mut validators = HashMap::new(); + let mut total_stake = 0u64; + + for uid in 0..config.validator_count { + let validator = Validator::with_uid(uid, config.netuid); + total_stake += validator.stake; + validators.insert(uid, validator); + } + + Self { + validators, + validator_count: config.validator_count, + netuid: config.netuid, + tempo: config.tempo as u16, + max_uids: config.validator_count, + min_stake: config.min_stake, + total_stake, + weight_commitments: HashMap::new(), + last_weight_update: HashMap::new(), + } + } + + /// Get validator by UID + pub fn get_validator(&self, uid: u16) -> Option<&Validator> { + self.validators.get(&uid) + } + + /// Get validator by hotkey + pub fn get_validator_by_hotkey(&self, hotkey: &str) -> Option<&Validator> { + self.validators.values().find(|v| v.hotkey == hotkey) + } + + /// Get all neuron info + pub fn get_neurons(&self) -> Vec { + let mut neurons: Vec<_> = self + .validators + .values() + .map(|v| v.to_neuron_info()) + .collect(); + + // Sort by UID + neurons.sort_by(|a, b| { + let uid_a = a["uid"].as_u64().unwrap_or(0); + let uid_b = b["uid"].as_u64().unwrap_or(0); + uid_a.cmp(&uid_b) + }); + + neurons + } + + /// Get neuron info lite for all validators + pub fn get_neurons_lite(&self) -> Vec { + let mut neurons: Vec<_> = self + .validators + .values() + .map(|v| v.to_neuron_info_lite()) + .collect(); + + neurons.sort_by(|a, b| { + let uid_a = a["uid"].as_u64().unwrap_or(0); + let uid_b = b["uid"].as_u64().unwrap_or(0); + uid_a.cmp(&uid_b) + }); + + neurons + } + + /// Get metagraph summary + pub fn get_summary(&self) -> serde_json::Value { + let active_count = self.validators.values().filter(|v| v.active).count() as u16; + let validator_count = self + .validators + .values() + .filter(|v| v.validator_permit) + .count(); + + serde_json::json!({ + "netuid": self.netuid, + "n": self.validator_count, + "block": Utc::now().timestamp(), + "tempo": self.tempo, + "total_stake": self.total_stake, + "min_stake": self.min_stake, + "validators": validator_count, + "active_validators": active_count, + "pending_commits": self.weight_commitments.len(), + }) + } + + /// Add weight commitment + pub fn commit_weights(&mut self, commitment: WeightCommitment) -> Result<(), String> { + // Check if validator exists + if !self + .validators + .values() + .any(|v| v.hotkey == commitment.hotkey) + { + return Err(format!("Hotkey {} not registered", commitment.hotkey)); + } + + self.weight_commitments + .insert(commitment.hotkey.clone(), commitment); + + Ok(()) + } + + /// Reveal weights + pub fn reveal_weights( + &mut self, + hotkey: &str, + uids: Vec, + weights: Vec, + salt: String, + block: u64, + ) -> Result<(), String> { + let commitment = self + .weight_commitments + .get_mut(hotkey) + .ok_or_else(|| format!("No pending commitment for hotkey {}", hotkey))?; + + // Verify the reveal + if !commitment.verify_reveal(&uids, &weights, &salt) { + return Err("Invalid reveal: commitment hash mismatch".to_string()); + } + + commitment.revealed = true; + commitment.revealed_weights = Some(weights); + commitment.reveal_block = Some(block); + + self.last_weight_update.insert(hotkey.to_string(), block); + + Ok(()) + } + + /// Get pending commitments + pub fn get_pending_commits(&self) -> Vec<&WeightCommitment> { + self.weight_commitments + .values() + .filter(|c| !c.revealed) + .collect() + } + + /// Get revealed commitments + pub fn get_revealed_commits(&self) -> Vec<&WeightCommitment> { + self.weight_commitments + .values() + .filter(|c| c.revealed) + .collect() + } + + /// Clean old commitments (older than reveal_period blocks) + pub fn clean_old_commits(&mut self, current_block: u64, reveal_period: u64) { + let to_remove: Vec = self + .weight_commitments + .iter() + .filter(|(_, c)| current_block - c.commit_block > reveal_period && !c.revealed) + .map(|(k, _)| k.clone()) + .collect(); + + for key in to_remove { + self.weight_commitments.remove(&key); + } + } +} + +/// Encode to SS58 address +fn ss58_encode(prefix: u16, public_key: &[u8; 32]) -> String { + // Simplified SS58 encoding (not cryptographically accurate but deterministic) + use sha2::{Digest, Sha256}; + + let mut hasher = Sha256::new(); + hasher.update(prefix.to_le_bytes()); + hasher.update(public_key); + let hash: [u8; 32] = hasher.finalize().into(); + + // Take first 2 bytes as checksum + let checksum = &hash[0..2]; + + // Combine: prefix (1 byte if < 64, 2 otherwise) + public_key + checksum + let mut bytes = Vec::new(); + if prefix < 64 { + bytes.push(prefix as u8); + } else { + bytes.push(((prefix & 0b1111_1100_0000_0000) >> 8) as u8 | 0b01000000); + bytes.push((prefix & 0b1111_1111) as u8); + } + bytes.extend_from_slice(public_key); + bytes.extend_from_slice(checksum); + + // Base58 encode + bs58::encode(bytes).into_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_config() -> Config { + Config { + bind: "127.0.0.1:9944".parse().unwrap(), + tempo: 12, + netuid: 100, + validator_count: 256, + min_stake: 1_000_000_000_000, + commit_reveal: true, + reveal_period: 12, + log_level: "info".to_string(), + inspection: true, + } + } + + #[test] + fn test_validator_generation() { + let validator = Validator::with_uid(0, 100); + assert_eq!(validator.uid, 0); + assert!(!validator.hotkey.is_empty()); + assert!(!validator.coldkey.is_empty()); + assert!(validator.stake > 0); + } + + #[test] + fn test_metagraph_creation() { + let metagraph = MockMetagraph::new(&test_config()); + assert_eq!(metagraph.validators.len(), 256); + assert_eq!(metagraph.validator_count, 256); + } + + #[test] + fn test_get_validator_by_uid() { + let metagraph = MockMetagraph::new(&test_config()); + let validator = metagraph.get_validator(0); + assert!(validator.is_some()); + assert_eq!(validator.unwrap().uid, 0); + } + + #[test] + fn test_get_neurons() { + let metagraph = MockMetagraph::new(&test_config()); + let neurons = metagraph.get_neurons(); + assert_eq!(neurons.len(), 256); + } + + #[test] + fn test_weight_commitment() { + let hotkey = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(); + let uids = vec![1, 2, 3]; + let weights = vec![100, 200, 300]; + let salt = "random_salt".to_string(); + + let commitment = WeightCommitment::new( + hotkey.clone(), + 100, + uids.clone(), + weights.clone(), + salt.clone(), + 1000, + ); + + assert!(commitment.verify_reveal(&uids, &weights, &salt)); + assert!(!commitment.verify_reveal(&uids, &weights, "wrong_salt")); + } + + #[test] + fn test_commit_reveal_flow() { + let mut metagraph = MockMetagraph::new(&test_config()); + + // Get a validator's hotkey + let hotkey = metagraph.get_validator(0).unwrap().hotkey.clone(); + + // Create commitment + let uids = vec![1, 2, 3]; + let weights = vec![100, 200, 300]; + let salt = "test_salt".to_string(); + + let commitment = WeightCommitment::new( + hotkey.clone(), + 100, + uids.clone(), + weights.clone(), + salt.clone(), + 1000, + ); + + // Commit + metagraph.commit_weights(commitment).unwrap(); + assert_eq!(metagraph.get_pending_commits().len(), 1); + + // Reveal + metagraph + .reveal_weights(&hotkey, uids, weights, salt, 1010) + .unwrap(); + assert_eq!(metagraph.get_revealed_commits().len(), 1); + assert!(metagraph.get_pending_commits().is_empty()); + } + + #[test] + fn test_stake_distribution() { + let mut high_stake = 0; + let mut _moderate_stake = 0; + let mut low_stake = 0; + + for uid in 0..256 { + let validator = Validator::with_uid(uid, 100); + let stake_tao = validator.stake as f64 / 1_000_000_000.0; + + if stake_tao >= 1000.0 { + high_stake += 1; + } else if stake_tao >= 500.0 { + _moderate_stake += 1; + } else { + low_stake += 1; + } + } + + // Verify distribution + assert!(high_stake > 0, "Should have some high-stake validators"); + assert!(low_stake > 0, "Should have some low-stake validators"); + } +} diff --git a/bins/mock-subtensor/src/websocket.rs b/bins/mock-subtensor/src/websocket.rs new file mode 100644 index 000000000..24eeb470f --- /dev/null +++ b/bins/mock-subtensor/src/websocket.rs @@ -0,0 +1,456 @@ +//! WebSocket server module - Handles WebSocket connections for JSON-RPC 2.0 + +use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade}; +use axum::extract::{ConnectInfo, State}; +use axum::http::StatusCode; +use axum::response::IntoResponse; +use axum::routing::{any, get, post}; +use axum::{Json, Router}; +use serde_json::{json, Value}; +use std::net::SocketAddr; +use std::sync::Arc; +use tokio::sync::mpsc; +use tower_http::cors::{Any, CorsLayer}; +use tower_http::trace::TraceLayer; +use tracing::{debug, info, warn}; + +use crate::jsonrpc::{JsonRpcRequest, JsonRpcResponse, RpcHandler}; +use crate::AppState; + +/// WebSocket server +pub struct WsServer { + state: Arc, +} + +impl WsServer { + pub fn new(state: Arc) -> Self { + Self { state } + } + + /// Build the router with all routes + fn router(&self) -> Router { + let state = self.state.clone(); + + let mut router = Router::new() + // WebSocket endpoint (primary for Substrate) + .route("/", any(ws_handler)) + // HTTP RPC endpoint for compatibility + .route("/rpc", post(post_rpc_handler)) + .route("/jsonrpc", post(post_rpc_handler)) + // Test inspection endpoints + .route("/test/state", get(get_state_handler)) + .route("/test/metagraph", get(get_metagraph_handler)) + .route("/test/weights", get(get_weights_handler)) + .route("/test/advance", post(post_advance_handler)) + .route("/health", get(health_handler)) + .with_state(state) + .layer(TraceLayer::new_for_http()); + + // CORS + router = router.layer( + CorsLayer::new() + .allow_origin(Any) + .allow_methods(Any) + .allow_headers(Any), + ); + + router + } + + /// Run the server + pub async fn run(self, addr: SocketAddr) -> anyhow::Result<()> { + let router = self.router(); + + info!("Mock Subtensor WebSocket server starting on {}", addr); + info!(" - WebSocket: ws://{}/", addr); + info!(" - HTTP RPC: http://{}/rpc", addr); + info!(" - Health: http://{}/health", addr); + + let listener = tokio::net::TcpListener::bind(addr).await?; + axum::serve(listener, router).await?; + + Ok(()) + } +} + +/// WebSocket handler +async fn ws_handler( + ws: WebSocketUpgrade, + State(state): State>, + ConnectInfo(addr): ConnectInfo, +) -> impl IntoResponse { + info!("WebSocket connection from {}", addr); + ws.on_upgrade(move |socket| handle_socket(socket, state)) +} + +/// Handle a WebSocket connection +async fn handle_socket(mut socket: WebSocket, state: Arc) { + let (tx, mut rx) = mpsc::unbounded_channel::(); + let handler = Arc::new(RpcHandler::new(state.clone())); + + // Subscribe to block notifications + let mut broadcast_rx = state.broadcast_tx.subscribe(); + + // Spawn task to handle outgoing messages + let tx_for_send = tx.clone(); + tokio::spawn(async move { + loop { + tokio::select! { + // Handle broadcast messages + Ok(notification) = broadcast_rx.recv() => { + if let Ok(msg) = serde_json::to_string(¬ification) { + if tx_for_send.send(Message::Text(msg)).is_err() { + break; + } + } + } + // Handle direct messages from main task + Some(msg) = rx.recv() => { + if tx_for_send.send(msg).is_err() { + break; + } + } + else => break, + } + } + }); + + // Process incoming messages + while let Some(msg) = socket.recv().await { + match msg { + Ok(Message::Text(text)) => { + debug!("Received: {}", text); + + // Parse JSON-RPC request + match serde_json::from_str::(&text) { + Ok(value) => { + // Handle batch requests + if let Some(array) = value.as_array() { + let mut responses = Vec::new(); + for item in array { + let req = + match serde_json::from_value::(item.clone()) { + Ok(r) => r, + Err(e) => { + responses.push(JsonRpcResponse::error( + item.get("id").cloned().unwrap_or(Value::Null), + -32700, + format!("Parse error: {}", e), + )); + continue; + } + }; + let resp = handler.handle(req); + responses.push(resp); + } + + if let Ok(json) = serde_json::to_string(&responses) { + if tx.send(Message::Text(json)).is_err() { + break; + } + } + } else { + // Single request + let req = match serde_json::from_value::(value) { + Ok(r) => r, + Err(e) => { + let resp = JsonRpcResponse::error( + Value::Null, + -32700, + format!("Parse error: {}", e), + ); + if let Ok(json) = serde_json::to_string(&resp) { + let _ = tx.send(Message::Text(json)); + } + continue; + } + }; + + let resp = handler.handle(req); + if let Ok(json) = serde_json::to_string(&resp) { + if tx.send(Message::Text(json)).is_err() { + break; + } + } + } + } + Err(e) => { + warn!("Failed to parse JSON: {}", e); + let resp = JsonRpcResponse::error( + Value::Null, + -32700, + format!("Parse error: {}", e), + ); + if let Ok(json) = serde_json::to_string(&resp) { + let _ = tx.send(Message::Text(json)); + } + } + } + } + Ok(Message::Close(_)) => { + debug!("Client closed connection"); + break; + } + Ok(Message::Ping(data)) => { + if tx.send(Message::Pong(data)).is_err() { + break; + } + } + Err(e) => { + warn!("WebSocket error: {}", e); + break; + } + _ => {} + } + } + + debug!("WebSocket connection closed"); +} + +/// HTTP POST handler for JSON-RPC +async fn post_rpc_handler( + State(state): State>, + Json(body): Json, +) -> impl IntoResponse { + let handler = RpcHandler::new(state.clone()); + + // Handle batch requests + if let Some(array) = body.as_array() { + let mut responses = Vec::new(); + for item in array { + let req = match serde_json::from_value::(item.clone()) { + Ok(r) => r, + Err(e) => { + responses.push(JsonRpcResponse::error( + item.get("id").cloned().unwrap_or(Value::Null), + -32700, + format!("Parse error: {}", e), + )); + continue; + } + }; + let resp = handler.handle(req); + responses.push(resp); + } + + (StatusCode::OK, Json(json!(responses))) + } else { + // Single request + let req = match serde_json::from_value::(body) { + Ok(r) => r, + Err(e) => { + let resp = + JsonRpcResponse::error(Value::Null, -32700, format!("Parse error: {}", e)); + return (StatusCode::OK, Json(json!(resp))); + } + }; + + let resp = handler.handle(req); + (StatusCode::OK, Json(json!(resp))) + } +} + +/// Health check handler +async fn health_handler(State(state): State>) -> impl IntoResponse { + let chain = state.chain.read(); + let metagraph = state.metagraph.read(); + + ( + StatusCode::OK, + Json(json!({ + "status": "healthy", + "block_number": chain.best_number(), + "finalized_number": chain.finalized_number(), + "validator_count": metagraph.validators.len(), + "netuid": state.config.netuid, + "tempo": state.config.tempo, + })), + ) +} + +/// Get current chain state +async fn get_state_handler(State(state): State>) -> impl IntoResponse { + let chain = state.chain.read(); + + ( + StatusCode::OK, + Json(json!({ + "best_number": chain.best_number(), + "finalized_number": chain.finalized_number(), + "pending_extrinsics": chain.pending_extrinsics.len(), + "config": { + "tempo": chain.config.tempo, + "netuid": chain.config.netuid, + "commit_reveal": chain.config.commit_reveal, + "reveal_period": chain.config.reveal_period, + "token_decimals": chain.config.token_decimals, + "ss58_format": chain.config.ss58_format, + }, + })), + ) +} + +/// Get metagraph information +async fn get_metagraph_handler(State(state): State>) -> impl IntoResponse { + let metagraph = state.metagraph.read(); + + (StatusCode::OK, Json(metagraph.get_summary())) +} + +/// Get weight commitments +async fn get_weights_handler(State(state): State>) -> impl IntoResponse { + let metagraph = state.metagraph.read(); + + let pending: Vec<_> = metagraph + .get_pending_commits() + .iter() + .map(|c| { + json!({ + "hotkey": c.hotkey, + "netuid": c.netuid, + "uids": c.uids, + "commitment_hash": c.commitment_hash, + "commit_block": c.commit_block, + "revealed": c.revealed, + }) + }) + .collect(); + + let revealed: Vec<_> = metagraph + .get_revealed_commits() + .iter() + .map(|c| { + json!({ + "hotkey": c.hotkey, + "netuid": c.netuid, + "uids": c.uids, + "weights": c.revealed_weights, + "reveal_block": c.reveal_block, + "revealed": c.revealed, + }) + }) + .collect(); + + ( + StatusCode::OK, + Json(json!({ + "pending": pending, + "revealed": revealed, + "total_pending": pending.len(), + "total_revealed": revealed.len(), + })), + ) +} + +/// Advance block manually +async fn post_advance_handler(State(state): State>) -> impl IntoResponse { + let mut chain = state.chain.write(); + let block = chain.produce_block(); + let block_number = block.header.number; + + // Broadcast notification + let notification = json!({ + "jsonrpc": "2.0", + "method": "chain_newHead", + "params": { + "result": { + "number": block_number, + "hash": format!("0x{}", hex::encode(block.hash)), + }, + "subscription": "chain" + } + }); + + drop(chain); + let _ = state.broadcast_tx.send(notification); + + ( + StatusCode::OK, + Json(json!({ + "success": true, + "block_number": block_number, + "block_hash": format!("0x{}", hex::encode(block.hash)), + })), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn test_state() -> Arc { + let config = crate::Config { + bind: "127.0.0.1:9944".parse().unwrap(), + tempo: 12, + netuid: 100, + validator_count: 256, + min_stake: 1_000_000_000_000, + commit_reveal: true, + reveal_period: 12, + log_level: "info".to_string(), + inspection: true, + }; + Arc::new(AppState::new(config)) + } + + #[tokio::test] + async fn test_router_creation() { + let state = test_state(); + let server = WsServer::new(state); + let _router = server.router(); + + let _ = _router; + } + + #[tokio::test] + async fn test_health_handler() { + let state = test_state(); + let _response = health_handler(State(state)).await; + + let _ = _response; + } + + #[tokio::test] + async fn test_get_state_handler() { + let state = test_state(); + let _response = get_state_handler(State(state)).await; + + let _ = _response; + } + + #[tokio::test] + async fn test_get_metagraph_handler() { + let state = test_state(); + let _response = get_metagraph_handler(State(state)).await; + + let _ = _response; + } + + #[tokio::test] + async fn test_advance_block() { + let state = test_state(); + let initial_number = state.chain.read().best_number(); + + let _response = post_advance_handler(State(state.clone())).await; + + // Verify block advanced + let new_number = state.chain.read().best_number(); + assert!(new_number > initial_number); + } + + #[tokio::test] + async fn test_post_rpc_handler() { + let state = test_state(); + let request = json!({ + "jsonrpc": "2.0", + "method": "system_health", + "params": [], + "id": 1 + }); + + let response = post_rpc_handler(State(state), Json(request)).await; + + let response = response.into_response(); + assert!(response.status().is_success()); + } +} diff --git a/bins/platform-cli/Cargo.toml b/bins/platform-cli/Cargo.toml new file mode 100644 index 000000000..f7e94f0df --- /dev/null +++ b/bins/platform-cli/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "platform-cli" +version.workspace = true +edition.workspace = true +description = "Platform CLI โ€” download and manage challenge CLIs" + +[[bin]] +name = "platform" +path = "src/main.rs" + +[dependencies] +clap = { workspace = true } +reqwest = { workspace = true, features = ["json"] } +serde = { workspace = true } +serde_json = { workspace = true } +tokio = { workspace = true } +anyhow = { workspace = true } +tracing = { workspace = true } +tracing-subscriber = { workspace = true } +chrono = { workspace = true } +toml = "0.8" +dirs = "5" +semver = { version = "1", features = ["serde"] } diff --git a/bins/platform-cli/src/main.rs b/bins/platform-cli/src/main.rs new file mode 100644 index 000000000..c054758dd --- /dev/null +++ b/bins/platform-cli/src/main.rs @@ -0,0 +1,694 @@ +//! Platform CLI โ€” download and manage challenge CLIs +//! +//! Provides subcommands to download, update, list, run, and configure +//! challenge CLI binaries from GitHub releases. + +use anyhow::{Context, Result}; +use chrono::{DateTime, Utc}; +use clap::{Parser, Subcommand}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use tracing::{debug, info}; + +// ==================== Constants ==================== + +const PLATFORM_DIR_NAME: &str = ".platform"; +const CONFIG_FILE_NAME: &str = "platform.toml"; +const VERSIONS_FILE_NAME: &str = "versions.json"; +const BIN_DIR_NAME: &str = "bin"; +const GITHUB_API_BASE: &str = "https://api.github.com"; + +// ==================== Config ==================== + +#[derive(Debug, Serialize, Deserialize)] +struct PlatformConfig { + network: NetworkConfig, + #[serde(default)] + challenges: HashMap, +} + +#[derive(Debug, Serialize, Deserialize)] +struct NetworkConfig { + rpc_endpoint: String, + netuid: u16, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct ChallengeConfig { + github_repo: String, + binary_name: String, + command_alias: String, + #[serde(default = "default_true")] + auto_update: bool, +} + +fn default_true() -> bool { + true +} + +impl Default for PlatformConfig { + fn default() -> Self { + Self { + network: NetworkConfig { + rpc_endpoint: "wss://chain.platform.network".to_string(), + netuid: 100, + }, + challenges: HashMap::new(), + } + } +} + +// ==================== Version Tracking ==================== + +#[derive(Debug, Serialize, Deserialize)] +struct VersionInfo { + version: String, + binary_path: String, + installed_at: DateTime, + github_repo: String, +} + +type VersionStore = HashMap; + +// ==================== GitHub API Types ==================== + +#[derive(Debug, Deserialize)] +struct GitHubRelease { + tag_name: String, + assets: Vec, +} + +#[derive(Debug, Deserialize)] +struct GitHubAsset { + name: String, + browser_download_url: String, +} + +// ==================== CLI ==================== + +#[derive(Parser)] +#[command(name = "platform")] +#[command(about = "Platform CLI โ€” download and manage challenge CLIs")] +struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + /// Download a challenge CLI binary from GitHub releases + Download { + /// Name of the challenge to download + challenge_name: String, + }, + /// Check for and install updates for a challenge CLI + Update { + /// Name of the challenge to update + challenge_name: String, + }, + /// List installed challenge CLIs + List, + /// Run an installed challenge CLI + Run { + /// Name of the challenge to run (or a command alias) + challenge_name: String, + /// Arguments to forward to the challenge CLI + #[arg(trailing_var_arg = true, allow_hyphen_values = true)] + args: Vec, + }, + /// Show current platform.toml config + Config, +} + +// ==================== Path Helpers ==================== + +fn platform_dir() -> Result { + let home = dirs::home_dir().context("Could not determine home directory")?; + Ok(home.join(PLATFORM_DIR_NAME)) +} + +fn config_path() -> Result { + Ok(platform_dir()?.join(CONFIG_FILE_NAME)) +} + +fn versions_path() -> Result { + Ok(platform_dir()?.join(VERSIONS_FILE_NAME)) +} + +fn bin_dir() -> Result { + Ok(platform_dir()?.join(BIN_DIR_NAME)) +} + +/// Validate that a binary name does not contain path separators or traversal sequences. +/// +/// Prevents a malicious config from escaping the `~/.platform/bin/` directory +/// via names like `../../usr/bin/evil` or `foo/bar`. +fn validate_binary_name(name: &str) -> Result<()> { + if name.is_empty() { + anyhow::bail!("Binary name must not be empty"); + } + if name.contains('/') || name.contains('\\') || name.contains("..") { + anyhow::bail!( + "Invalid binary name '{}': must not contain path separators or '..'", + name + ); + } + if name.starts_with('.') || name.starts_with('-') { + anyhow::bail!( + "Invalid binary name '{}': must not start with '.' or '-'", + name + ); + } + Ok(()) +} + +// ==================== Config I/O ==================== + +fn load_config() -> Result { + let path = config_path()?; + if !path.exists() { + info!("Config not found at {}, creating default", path.display()); + let config = PlatformConfig::default(); + save_config(&config)?; + return Ok(config); + } + let contents = std::fs::read_to_string(&path) + .with_context(|| format!("Failed to read config from {}", path.display()))?; + let config: PlatformConfig = toml::from_str(&contents) + .with_context(|| format!("Failed to parse config at {}", path.display()))?; + Ok(config) +} + +fn save_config(config: &PlatformConfig) -> Result<()> { + let path = config_path()?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent) + .with_context(|| format!("Failed to create directory {}", parent.display()))?; + } + let contents = toml::to_string_pretty(config).context("Failed to serialize config")?; + std::fs::write(&path, contents) + .with_context(|| format!("Failed to write config to {}", path.display()))?; + debug!("Config saved to {}", path.display()); + Ok(()) +} + +// ==================== Version Store I/O ==================== + +fn load_versions() -> Result { + let path = versions_path()?; + if !path.exists() { + return Ok(HashMap::new()); + } + let contents = std::fs::read_to_string(&path) + .with_context(|| format!("Failed to read versions from {}", path.display()))?; + let versions: VersionStore = serde_json::from_str(&contents) + .with_context(|| format!("Failed to parse versions at {}", path.display()))?; + Ok(versions) +} + +fn save_versions(versions: &VersionStore) -> Result<()> { + let path = versions_path()?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent) + .with_context(|| format!("Failed to create directory {}", parent.display()))?; + } + let contents = + serde_json::to_string_pretty(versions).context("Failed to serialize versions")?; + std::fs::write(&path, contents) + .with_context(|| format!("Failed to write versions to {}", path.display()))?; + debug!("Versions saved to {}", path.display()); + Ok(()) +} + +// ==================== Platform Detection ==================== + +fn platform_identifier() -> String { + let os = match std::env::consts::OS { + "linux" => "linux", + "macos" => "darwin", + "windows" => "windows", + other => other, + }; + let arch = std::env::consts::ARCH; + format!("{}-{}", os, arch) +} + +fn find_matching_asset(assets: &[GitHubAsset]) -> Option<&GitHubAsset> { + let platform = platform_identifier(); + debug!("Looking for asset matching platform: {}", platform); + + assets + .iter() + .find(|asset| asset.name.contains(&platform)) + .or_else(|| { + let os = std::env::consts::OS; + let arch = std::env::consts::ARCH; + assets + .iter() + .find(|asset| asset.name.contains(os) && asset.name.contains(arch)) + }) +} + +// ==================== GitHub API ==================== + +/// Validate that a GitHub repo string is in the expected `owner/repo` format. +/// +/// Prevents URL path injection when the value is interpolated into API URLs. +/// Only alphanumeric characters, hyphens, underscores, and dots are permitted +/// in each segment. +fn validate_github_repo(repo: &str) -> Result<()> { + let parts: Vec<&str> = repo.split('/').collect(); + if parts.len() != 2 { + anyhow::bail!( + "Invalid github_repo '{}': must be in 'owner/repo' format", + repo + ); + } + for part in &parts { + if part.is_empty() { + anyhow::bail!( + "Invalid github_repo '{}': owner and repo must not be empty", + repo + ); + } + if !part + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.') + { + anyhow::bail!( + "Invalid github_repo '{}': contains disallowed characters", + repo + ); + } + } + Ok(()) +} + +async fn fetch_latest_release( + client: &reqwest::Client, + github_repo: &str, +) -> Result { + validate_github_repo(github_repo)?; + let url = format!("{}/repos/{}/releases/latest", GITHUB_API_BASE, github_repo); + debug!("Fetching latest release from {}", url); + + let response = client + .get(&url) + .header("User-Agent", "platform-cli") + .header("Accept", "application/vnd.github.v3+json") + .send() + .await + .with_context(|| format!("Failed to fetch releases from {}", url))?; + + if !response.status().is_success() { + let status = response.status(); + let body = response + .text() + .await + .unwrap_or_else(|_| "".to_string()); + anyhow::bail!( + "GitHub API returned {} for {}: {}", + status, + github_repo, + body + ); + } + + let release: GitHubRelease = response + .json() + .await + .context("Failed to parse GitHub release response")?; + + Ok(release) +} + +async fn download_binary(client: &reqwest::Client, url: &str, dest: &Path) -> Result<()> { + let parsed_url = + reqwest::Url::parse(url).with_context(|| format!("Invalid download URL: {}", url))?; + if parsed_url.scheme() != "https" { + anyhow::bail!( + "Refusing to download from non-HTTPS URL: {}", + parsed_url.scheme() + ); + } + info!("Downloading binary from {}", url); + + let response = client + .get(url) + .header("User-Agent", "platform-cli") + .send() + .await + .with_context(|| format!("Failed to download from {}", url))?; + + if !response.status().is_success() { + let status = response.status(); + anyhow::bail!("Download failed with status {}", status); + } + + if let Some(parent) = dest.parent() { + std::fs::create_dir_all(parent) + .with_context(|| format!("Failed to create directory {}", parent.display()))?; + } + + let bytes = response + .bytes() + .await + .context("Failed to read download response body")?; + + std::fs::write(dest, &bytes) + .with_context(|| format!("Failed to write binary to {}", dest.display()))?; + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let perms = std::fs::Permissions::from_mode(0o755); + std::fs::set_permissions(dest, perms).with_context(|| { + format!("Failed to set executable permissions on {}", dest.display()) + })?; + } + + info!("Binary saved to {}", dest.display()); + Ok(()) +} + +// ==================== Challenge Lookup ==================== + +fn resolve_challenge_name( + config: &PlatformConfig, + name: &str, +) -> Option<(String, ChallengeConfig)> { + if let Some(challenge) = config.challenges.get(name) { + return Some((name.to_string(), challenge.clone())); + } + + for (challenge_name, challenge) in &config.challenges { + if challenge.command_alias == name { + return Some((challenge_name.clone(), challenge.clone())); + } + } + + None +} + +// ==================== Subcommand Handlers ==================== + +async fn cmd_download(challenge_name: &str) -> Result<()> { + let config = load_config()?; + let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) + .with_context(|| { + format!( + "Challenge '{}' not found in config. Add it to {} first.", + challenge_name, + config_path() + .map(|p| p.display().to_string()) + .unwrap_or_else(|_| "~/.platform/platform.toml".to_string()) + ) + })?; + + validate_binary_name(&challenge.binary_name)?; + + info!( + "Downloading challenge '{}' from {}", + canonical_name, challenge.github_repo + ); + + let client = reqwest::Client::new(); + let release = fetch_latest_release(&client, &challenge.github_repo).await?; + + let version = release.tag_name.trim_start_matches('v').to_string(); + info!("Latest release: v{}", version); + + let asset = find_matching_asset(&release.assets).with_context(|| { + let available: Vec<&str> = release.assets.iter().map(|a| a.name.as_str()).collect(); + format!( + "No binary found for platform '{}'. Available assets: {:?}", + platform_identifier(), + available + ) + })?; + + let dest = bin_dir()?.join(&challenge.binary_name); + download_binary(&client, &asset.browser_download_url, &dest).await?; + + let mut versions = load_versions()?; + versions.insert( + canonical_name.clone(), + VersionInfo { + version: version.clone(), + binary_path: dest.display().to_string(), + installed_at: Utc::now(), + github_repo: challenge.github_repo.clone(), + }, + ); + save_versions(&versions)?; + + info!( + "Successfully installed {} v{} to {}", + canonical_name, + version, + dest.display() + ); + println!( + "โœ“ {} v{} installed to {}", + canonical_name, + version, + dest.display() + ); + + Ok(()) +} + +async fn cmd_update(challenge_name: &str) -> Result<()> { + let config = load_config()?; + let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) + .with_context(|| format!("Challenge '{}' not found in config", challenge_name))?; + + validate_binary_name(&challenge.binary_name)?; + + let versions = load_versions()?; + let current_version = versions + .get(&canonical_name) + .map(|v| v.version.clone()) + .unwrap_or_default(); + + info!( + "Checking for updates to '{}' (current: {})", + canonical_name, + if current_version.is_empty() { + "not installed" + } else { + ¤t_version + } + ); + + let client = reqwest::Client::new(); + let release = fetch_latest_release(&client, &challenge.github_repo).await?; + let latest_version = release.tag_name.trim_start_matches('v').to_string(); + + if !current_version.is_empty() { + let current = semver::Version::parse(¤t_version); + let latest = semver::Version::parse(&latest_version); + + match (current, latest) { + (Ok(cur), Ok(lat)) if lat <= cur => { + println!( + "โœ“ {} is already up to date (v{})", + canonical_name, current_version + ); + return Ok(()); + } + _ => {} + } + } + + info!( + "Updating {} from v{} to v{}", + canonical_name, current_version, latest_version + ); + + let asset = find_matching_asset(&release.assets) + .with_context(|| format!("No binary found for platform '{}'", platform_identifier()))?; + + let dest = bin_dir()?.join(&challenge.binary_name); + download_binary(&client, &asset.browser_download_url, &dest).await?; + + let mut versions = load_versions()?; + versions.insert( + canonical_name.clone(), + VersionInfo { + version: latest_version.clone(), + binary_path: dest.display().to_string(), + installed_at: Utc::now(), + github_repo: challenge.github_repo.clone(), + }, + ); + save_versions(&versions)?; + + println!( + "โœ“ {} updated to v{} at {}", + canonical_name, + latest_version, + dest.display() + ); + + Ok(()) +} + +fn cmd_list() -> Result<()> { + let versions = load_versions()?; + + if versions.is_empty() { + println!("No challenge CLIs installed."); + println!("Use 'platform download ' to install one."); + return Ok(()); + } + + let header_installed = "INSTALLED"; + println!( + "{:<20} {:<12} {:<40} {}", + "CHALLENGE", "VERSION", "PATH", header_installed + ); + println!("{}", "-".repeat(90)); + + let mut entries: Vec<_> = versions.iter().collect(); + entries.sort_by_key(|(name, _)| (*name).clone()); + + for (name, info) in entries { + println!( + "{:<20} {:<12} {:<40} {}", + name, + info.version, + info.binary_path, + info.installed_at.format("%Y-%m-%d %H:%M:%S UTC") + ); + } + + Ok(()) +} + +async fn cmd_run(challenge_name: &str, args: &[String]) -> Result<()> { + let config = load_config()?; + let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) + .with_context(|| format!("Challenge '{}' not found in config", challenge_name))?; + + validate_binary_name(&challenge.binary_name)?; + + let versions = load_versions()?; + let version_info = versions.get(&canonical_name).with_context(|| { + format!( + "Challenge '{}' is not installed. Run 'platform download {}' first.", + canonical_name, canonical_name + ) + })?; + + let binary_path = Path::new(&version_info.binary_path); + if !binary_path.exists() { + anyhow::bail!( + "Binary not found at {}. Run 'platform download {}' to reinstall.", + binary_path.display(), + canonical_name + ); + } + + if challenge.auto_update { + let repo = challenge.github_repo.clone(); + let current_version = version_info.version.clone(); + let name_for_log = canonical_name.clone(); + tokio::spawn(async move { + match check_for_update_quietly(&repo, ¤t_version).await { + Ok(Some(new_version)) => { + eprintln!( + "โ„น A new version of {} is available: v{} (current: v{}). Run 'platform update {}'", + name_for_log, new_version, current_version, name_for_log + ); + } + Ok(None) => {} + Err(e) => { + debug!("Auto-update check failed for {}: {}", name_for_log, e); + } + } + }); + } + + debug!("Running {} with args: {:?}", binary_path.display(), args); + + let status = std::process::Command::new(binary_path) + .args(args) + .stdin(std::process::Stdio::inherit()) + .stdout(std::process::Stdio::inherit()) + .stderr(std::process::Stdio::inherit()) + .status() + .with_context(|| format!("Failed to execute {}", binary_path.display()))?; + + if !status.success() { + let code = status.code().unwrap_or(1); + std::process::exit(code); + } + + Ok(()) +} + +async fn check_for_update_quietly( + github_repo: &str, + current_version: &str, +) -> Result> { + let client = reqwest::Client::builder() + .timeout(std::time::Duration::from_secs(5)) + .build()?; + + let release = fetch_latest_release(&client, github_repo).await?; + let latest_version = release.tag_name.trim_start_matches('v').to_string(); + + let current = semver::Version::parse(current_version)?; + let latest = semver::Version::parse(&latest_version)?; + + if latest > current { + Ok(Some(latest_version)) + } else { + Ok(None) + } +} + +fn cmd_config() -> Result<()> { + let path = config_path()?; + if !path.exists() { + info!("No config found, creating default at {}", path.display()); + let config = PlatformConfig::default(); + save_config(&config)?; + } + + let contents = std::fs::read_to_string(&path) + .with_context(|| format!("Failed to read config from {}", path.display()))?; + + println!("# Config: {}", path.display()); + println!(); + print!("{}", contents); + + Ok(()) +} + +// ==================== Main ==================== + +#[tokio::main] +async fn main() -> Result<()> { + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "info,platform_cli=debug".into()), + ) + .init(); + + let cli = Cli::parse(); + + match cli.command { + Commands::Download { challenge_name } => cmd_download(&challenge_name).await, + Commands::Update { challenge_name } => cmd_update(&challenge_name).await, + Commands::List => cmd_list(), + Commands::Run { + challenge_name, + args, + } => cmd_run(&challenge_name, &args).await, + Commands::Config => cmd_config(), + } +} diff --git a/bins/utils/Cargo.toml b/bins/utils/Cargo.toml new file mode 100644 index 000000000..37db9ea9f --- /dev/null +++ b/bins/utils/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "utils" +version = "0.1.0" +edition = "2021" + +[dependencies] +sp-core = { version = "31.0", default-features = false, features = ["std"] } +hex = "0.4" diff --git a/bins/utils/src/main.rs b/bins/utils/src/main.rs new file mode 100644 index 000000000..2ed979fd7 --- /dev/null +++ b/bins/utils/src/main.rs @@ -0,0 +1,13 @@ +use sp_core::{sr25519, Pair}; + +fn main() { + let secret_hex = "0000000000000000000000000000000000000000000000000000000000000001"; + let bytes = hex::decode(secret_hex).expect("Invalid hex"); + let mut arr = [0u8; 32]; + arr.copy_from_slice(&bytes); + + // Use sr25519 (Substrate/Bittensor standard) + let pair = sr25519::Pair::from_seed(&arr); + let public = pair.public(); + println!("{}", hex::encode(public.0)); +} diff --git a/bins/validator-node/Cargo.toml b/bins/validator-node/Cargo.toml new file mode 100644 index 000000000..327de0084 --- /dev/null +++ b/bins/validator-node/Cargo.toml @@ -0,0 +1,48 @@ +[package] +name = "validator-node" +version.workspace = true +edition.workspace = true +description = "Platform Validator Node - Decentralized P2P Architecture" + +[[bin]] +name = "validator-node" +path = "src/main.rs" + +[dependencies] +# Platform crates +platform-core = { path = "../../crates/core" } +platform-storage = { path = "../../crates/storage" } +platform-bittensor = { path = "../../crates/bittensor-integration" } +platform-p2p-consensus = { path = "../../crates/p2p-consensus" } +platform-distributed-storage = { path = "../../crates/distributed-storage" } +platform-challenge-sdk = { path = "../../crates/challenge-sdk" } +wasm-runtime-interface = { path = "../../crates/wasm-runtime-interface" } + +# Bittensor +bittensor-rs = { workspace = true } + +# Networking +libp2p = { version = "0.54", features = ["tokio", "gossipsub", "kad", "identify", "noise", "yamux", "tcp", "dns"] } + +# Async +tokio = { workspace = true } +futures = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } + +# Logging +tracing = { workspace = true } +tracing-subscriber = { workspace = true } + +# CLI +clap = { workspace = true, features = ["env"] } +anyhow = { workspace = true } +hex = { workspace = true } +parking_lot = { workspace = true } +sp-core = { workspace = true } +uuid = { workspace = true } +chrono = { workspace = true } +bincode = { workspace = true } +sha2 = { workspace = true } \ No newline at end of file diff --git a/bins/validator-node/src/challenge_storage.rs b/bins/validator-node/src/challenge_storage.rs new file mode 100644 index 000000000..70c4ace2d --- /dev/null +++ b/bins/validator-node/src/challenge_storage.rs @@ -0,0 +1,55 @@ +use platform_distributed_storage::{ + DistributedStore, GetOptions as DGetOptions, LocalStorage, PutOptions as DPutOptions, + StorageKey as DStorageKey, +}; +use sha2::{Digest, Sha256}; +use std::sync::Arc; +use wasm_runtime_interface::storage::{StorageBackend, StorageHostError}; + +pub struct ChallengeStorageBackend { + storage: Arc, +} + +impl ChallengeStorageBackend { + pub fn new(storage: Arc) -> Self { + Self { storage } + } +} + +impl StorageBackend for ChallengeStorageBackend { + fn get(&self, challenge_id: &str, key: &[u8]) -> Result>, StorageHostError> { + let storage_key = DStorageKey::new(challenge_id, hex::encode(key)); + let result = tokio::runtime::Handle::current() + .block_on(self.storage.get(&storage_key, DGetOptions::default())) + .map_err(|e| StorageHostError::StorageError(e.to_string()))?; + Ok(result.map(|v| v.data)) + } + + fn propose_write( + &self, + challenge_id: &str, + key: &[u8], + value: &[u8], + ) -> Result<[u8; 32], StorageHostError> { + let storage_key = DStorageKey::new(challenge_id, hex::encode(key)); + tokio::runtime::Handle::current() + .block_on( + self.storage + .put(storage_key, value.to_vec(), DPutOptions::default()), + ) + .map_err(|e| StorageHostError::StorageError(e.to_string()))?; + + let mut hasher = Sha256::new(); + hasher.update(challenge_id.as_bytes()); + hasher.update(key); + hasher.update(value); + Ok(hasher.finalize().into()) + } + + fn delete(&self, challenge_id: &str, key: &[u8]) -> Result { + let storage_key = DStorageKey::new(challenge_id, hex::encode(key)); + tokio::runtime::Handle::current() + .block_on(self.storage.delete(&storage_key)) + .map_err(|e| StorageHostError::StorageError(e.to_string())) + } +} diff --git a/bins/validator-node/src/main.rs b/bins/validator-node/src/main.rs new file mode 100644 index 000000000..4edd24ca3 --- /dev/null +++ b/bins/validator-node/src/main.rs @@ -0,0 +1,1569 @@ +//! Platform Validator Node +//! +//! Fully decentralized P2P validator for the Platform network. +//! Uses libp2p for gossipsub consensus and Kademlia DHT for storage. +//! Submits weights to Bittensor at epoch boundaries. + +mod challenge_storage; +mod wasm_executor; + +use anyhow::Result; +use bittensor_rs::chain::{signer_from_seed, BittensorSigner, ExtrinsicWait}; +use clap::Parser; +use parking_lot::RwLock; +use platform_bittensor::{ + sync_metagraph, BittensorClient, BlockSync, BlockSyncConfig, BlockSyncEvent, Metagraph, + Subtensor, SubtensorClient, +}; +use platform_core::{ + checkpoint::{ + CheckpointData, CheckpointManager, CompletedEvaluationState, PendingEvaluationState, + WeightVoteState, + }, + ChallengeId, Hotkey, Keypair, SUDO_KEY_SS58, +}; +use platform_distributed_storage::{ + DistributedStoreExt, LocalStorage, LocalStorageBuilder, StorageKey, +}; +use platform_p2p_consensus::{ + ChainState, ConsensusEngine, EvaluationMessage, EvaluationMetrics, EvaluationRecord, JobRecord, + JobStatus, NetworkEvent, P2PConfig, P2PMessage, P2PNetwork, StateManager, TaskProgressRecord, + ValidatorRecord, ValidatorSet, +}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::time::Duration; +use tracing::{debug, error, info, warn}; +use wasm_executor::{WasmChallengeExecutor, WasmExecutorConfig}; + +/// Storage key for persisted chain state +const STATE_STORAGE_KEY: &str = "chain_state"; + +/// Maximum length for user-provided strings logged from P2P messages +const MAX_LOG_FIELD_LEN: usize = 256; +const JOB_TIMEOUT_MS: i64 = 300_000; + +/// Sanitize a user-provided string for safe logging. +/// +/// Replaces control characters (newlines, tabs, ANSI escapes) with spaces +/// and truncates to `MAX_LOG_FIELD_LEN` to prevent log injection attacks. +fn sanitize_for_log(s: &str) -> String { + let truncated = if s.len() > MAX_LOG_FIELD_LEN { + &s[..MAX_LOG_FIELD_LEN] + } else { + s + }; + truncated + .chars() + .map(|c| if c.is_control() { ' ' } else { c }) + .collect() +} + +// ==================== Shutdown Handler ==================== + +/// Handles graceful shutdown with state persistence +struct ShutdownHandler { + checkpoint_manager: CheckpointManager, + state_manager: Arc, + netuid: u16, +} + +impl ShutdownHandler { + fn new(checkpoint_dir: &Path, state_manager: Arc, netuid: u16) -> Result { + let checkpoint_manager = CheckpointManager::new(checkpoint_dir.join("checkpoints"), 10)?; + Ok(Self { + checkpoint_manager, + state_manager, + netuid, + }) + } + + /// Create checkpoint from current state + fn create_checkpoint(&mut self) -> Result<()> { + let state = self.state_manager.snapshot(); + + let mut checkpoint_data = CheckpointData::new(state.sequence, state.epoch, self.netuid); + + // Convert pending evaluations + for (id, record) in &state.pending_evaluations { + let pending = PendingEvaluationState { + submission_id: id.clone(), + challenge_id: record.challenge_id, + miner: record.miner.clone(), + submission_hash: record.agent_hash.clone(), + scores: record + .evaluations + .iter() + .map(|(k, v)| (k.clone(), v.score)) + .collect(), + created_at: record.created_at, + finalizing: record.finalized, + }; + checkpoint_data.add_pending(pending); + } + + // Convert completed evaluations (current epoch only) + if let Some(completed) = state.completed_evaluations.get(&state.epoch) { + for record in completed { + if let Some(score) = record.aggregated_score { + let completed_state = CompletedEvaluationState { + submission_id: record.submission_id.clone(), + challenge_id: record.challenge_id, + final_score: score, + epoch: state.epoch, + completed_at: record.finalized_at.unwrap_or(record.created_at), + }; + checkpoint_data.add_completed(completed_state); + } + } + } + + // Convert weight votes + if let Some(ref votes) = state.weight_votes { + checkpoint_data.weight_votes = Some(WeightVoteState { + epoch: votes.epoch, + netuid: votes.netuid, + votes: votes.votes.clone(), + finalized: votes.finalized, + final_weights: votes.final_weights.clone(), + }); + } + + checkpoint_data.bittensor_block = state.bittensor_block; + + self.checkpoint_manager + .create_checkpoint(&checkpoint_data)?; + info!("Shutdown checkpoint created at sequence {}", state.sequence); + + Ok(()) + } +} + +// ==================== CLI ==================== + +#[derive(Parser)] +#[command(name = "validator-node")] +#[command(about = "Platform Validator - Decentralized P2P Architecture")] +struct Args { + /// Secret key (hex or mnemonic) + #[arg(short = 'k', long, env = "VALIDATOR_SECRET_KEY")] + secret_key: Option, + + /// Data directory + #[arg(short, long, default_value = "./data")] + data_dir: PathBuf, + + /// P2P listen address + #[arg(long, default_value = "/ip4/0.0.0.0/tcp/9000")] + listen_addr: String, + + /// Bootstrap peers (multiaddr format) + #[arg(long)] + bootstrap: Vec, + + /// Subtensor endpoint + #[arg( + long, + env = "SUBTENSOR_ENDPOINT", + default_value = "wss://entrypoint-finney.opentensor.ai:443" + )] + subtensor_endpoint: String, + + /// Network UID + #[arg(long, env = "NETUID", default_value = "100")] + netuid: u16, + + /// Version key + #[arg(long, env = "VERSION_KEY", default_value = "1")] + version_key: u64, + + /// Disable Bittensor (for testing) + #[arg(long)] + no_bittensor: bool, + + /// Directory where WASM challenge modules are stored + #[arg(long, env = "WASM_MODULE_DIR", default_value = "./wasm_modules")] + wasm_module_dir: PathBuf, + + /// Maximum memory for WASM execution in bytes (default: 512MB) + #[arg(long, env = "WASM_MAX_MEMORY", default_value = "536870912")] + wasm_max_memory: u64, + + /// Enable fuel metering for WASM execution + #[arg(long, env = "WASM_ENABLE_FUEL")] + wasm_enable_fuel: bool, + + /// Fuel limit per WASM execution (requires --wasm-enable-fuel) + #[arg(long, env = "WASM_FUEL_LIMIT")] + wasm_fuel_limit: Option, +} + +impl std::fmt::Debug for Args { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Args") + .field( + "secret_key", + &self.secret_key.as_ref().map(|_| "[REDACTED]"), + ) + .field("data_dir", &self.data_dir) + .field("listen_addr", &self.listen_addr) + .field("bootstrap", &self.bootstrap) + .field("subtensor_endpoint", &self.subtensor_endpoint) + .field("netuid", &self.netuid) + .field("version_key", &self.version_key) + .field("no_bittensor", &self.no_bittensor) + .field("wasm_module_dir", &self.wasm_module_dir) + .field("wasm_max_memory", &self.wasm_max_memory) + .field("wasm_enable_fuel", &self.wasm_enable_fuel) + .field("wasm_fuel_limit", &self.wasm_fuel_limit) + .finish() + } +} + +// ==================== Main ==================== + +#[tokio::main] +async fn main() -> Result<()> { + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { + "info,validator_node=debug,platform_p2p_consensus=debug".into() + }), + ) + .init(); + + let args = Args::parse(); + + info!("Starting decentralized validator"); + info!("SudoOwner: {}", SUDO_KEY_SS58); + + // Load keypair + let keypair = load_keypair(&args)?; + let validator_hotkey = keypair.ss58_address(); + info!("Validator hotkey: {}", validator_hotkey); + + // Create data directory + std::fs::create_dir_all(&args.data_dir)?; + let data_dir = std::fs::canonicalize(&args.data_dir)?; + + // Initialize distributed storage + let storage = LocalStorageBuilder::new(&validator_hotkey) + .path( + data_dir + .join("distributed.db") + .to_string_lossy() + .to_string(), + ) + .build()?; + let storage = Arc::new(storage); + info!("Distributed storage initialized"); + + if args.bootstrap.is_empty() { + return Err(anyhow::anyhow!( + "No bootstrap peers configured. Provide --bootstrap to connect to the P2P validator mesh." + )); + } + let p2p_config = P2PConfig::default() + .with_listen_addr(&args.listen_addr) + .with_bootstrap_peers(args.bootstrap.clone()) + .with_netuid(args.netuid) + .with_min_stake(1_000_000_000_000); // 1000 TAO + + // Initialize validator set (ourselves first) + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), p2p_config.min_stake)); + info!("P2P network config initialized"); + + // Initialize state manager, loading persisted state if available + let state_manager = Arc::new( + load_state_from_storage(&storage, args.netuid) + .await + .unwrap_or_else(|| { + info!("No persisted state found, starting fresh"); + StateManager::for_netuid(args.netuid) + }), + ); + + // Create event channel for network events + let (event_tx, mut event_rx) = tokio::sync::mpsc::channel::(256); + + // Initialize P2P network + let network = Arc::new(P2PNetwork::new( + keypair.clone(), + p2p_config, + validator_set.clone(), + event_tx.clone(), + )?); + info!( + "P2P network initialized, local peer: {:?}", + network.local_peer_id() + ); + + // Initialize consensus engine + let consensus = Arc::new(RwLock::new(ConsensusEngine::new( + keypair.clone(), + validator_set.clone(), + state_manager.clone(), + ))); + + // Connect to Bittensor + let subtensor: Option>; + let subtensor_signer: Option>; + let mut subtensor_client: Option; + let bittensor_client_for_metagraph: Option>; + let mut block_rx: Option> = None; + + if !args.no_bittensor { + info!("Connecting to Bittensor: {}", args.subtensor_endpoint); + + let state_path = data_dir.join("subtensor_state.json"); + match Subtensor::with_persistence(&args.subtensor_endpoint, state_path).await { + Ok(st) => { + let secret = args + .secret_key + .as_ref() + .ok_or_else(|| anyhow::anyhow!("VALIDATOR_SECRET_KEY required"))?; + + let signer = signer_from_seed(secret).map_err(|e| { + anyhow::anyhow!( + "Failed to create Bittensor signer from secret key: {}. \ + A valid signer is required for weight submission. \ + Use --no-bittensor flag if running without Bittensor.", + e + ) + })?; + info!("Bittensor signer initialized: {}", signer.account_id()); + subtensor_signer = Some(Arc::new(signer)); + + subtensor = Some(Arc::new(st)); + + // Create SubtensorClient for metagraph + let mut client = SubtensorClient::new(platform_bittensor::BittensorConfig { + endpoint: args.subtensor_endpoint.clone(), + netuid: args.netuid, + ..Default::default() + }); + + let bittensor_client = + Arc::new(BittensorClient::new(&args.subtensor_endpoint).await?); + match sync_metagraph(&bittensor_client, args.netuid).await { + Ok(mg) => { + info!("Metagraph synced: {} neurons", mg.n); + + // Update validator set from metagraph + update_validator_set_from_metagraph(&mg, &validator_set); + info!( + "Validator set: {} active validators", + validator_set.active_count() + ); + + client.set_metagraph(mg); + } + Err(e) => warn!("Metagraph sync failed: {}", e), + } + + subtensor_client = Some(client); + + // Store bittensor client for metagraph refreshes + bittensor_client_for_metagraph = Some(bittensor_client.clone()); + + // Block sync + let mut sync = BlockSync::new(BlockSyncConfig { + netuid: args.netuid, + ..Default::default() + }); + let rx = sync.take_event_receiver(); + + if let Err(e) = sync.connect(bittensor_client).await { + warn!("Block sync failed: {}", e); + } else { + tokio::spawn(async move { + if let Err(e) = sync.start().await { + error!("Block sync error: {}", e); + } + }); + block_rx = rx; + info!("Block sync started"); + } + } + Err(e) => { + error!("Subtensor connection failed: {}", e); + subtensor = None; + subtensor_signer = None; + subtensor_client = None; + bittensor_client_for_metagraph = None; + } + } + } else { + info!("Bittensor disabled"); + subtensor = None; + subtensor_signer = None; + subtensor_client = None; + bittensor_client_for_metagraph = None; + } + + // Initialize WASM challenge executor + let wasm_module_dir = if args.wasm_module_dir.is_relative() { + data_dir.join(&args.wasm_module_dir) + } else { + args.wasm_module_dir.clone() + }; + std::fs::create_dir_all(&wasm_module_dir)?; + + let challenges_subdir = wasm_module_dir.join("challenges"); + std::fs::create_dir_all(&challenges_subdir)?; + + let wasm_executor = match WasmChallengeExecutor::new(WasmExecutorConfig { + module_dir: wasm_module_dir.clone(), + max_memory_bytes: args.wasm_max_memory, + enable_fuel: args.wasm_enable_fuel, + fuel_limit: args.wasm_fuel_limit, + storage_host_config: wasm_runtime_interface::StorageHostConfig::default(), + storage_backend: std::sync::Arc::new(challenge_storage::ChallengeStorageBackend::new( + storage.clone(), + )), + chutes_api_key: None, + }) { + Ok(executor) => { + info!( + module_dir = %wasm_module_dir.display(), + max_memory = args.wasm_max_memory, + fuel_enabled = args.wasm_enable_fuel, + "WASM challenge executor ready" + ); + Some(Arc::new(executor)) + } + Err(e) => { + error!( + "Failed to initialize WASM executor: {}. WASM evaluations disabled.", + e + ); + None + } + }; + + // Initialize shutdown handler for graceful checkpoint persistence + let mut shutdown_handler = + match ShutdownHandler::new(&data_dir, state_manager.clone(), args.netuid) { + Ok(handler) => { + info!("Shutdown handler initialized with checkpoint directory"); + Some(handler) + } + Err(e) => { + warn!( + "Failed to initialize shutdown handler: {}. Checkpoints disabled.", + e + ); + None + } + }; + + info!("Decentralized validator running. Press Ctrl+C to stop."); + + let netuid = args.netuid; + let version_key = args.version_key; + let mut heartbeat_interval = tokio::time::interval(Duration::from_secs(30)); + let mut metagraph_interval = tokio::time::interval(Duration::from_secs(300)); + let mut stale_check_interval = tokio::time::interval(Duration::from_secs(60)); + let mut state_persist_interval = tokio::time::interval(Duration::from_secs(60)); + let mut checkpoint_interval = tokio::time::interval(Duration::from_secs(300)); // 5 minutes + let mut wasm_eval_interval = tokio::time::interval(Duration::from_secs(5)); + let mut stale_job_interval = tokio::time::interval(Duration::from_secs(120)); + + let (eval_broadcast_tx, mut eval_broadcast_rx) = tokio::sync::mpsc::channel::(256); + + loop { + tokio::select! { + // P2P network events + Some(event) = event_rx.recv() => { + handle_network_event( + event, + &consensus, + &validator_set, + &state_manager, + &storage, + &wasm_module_dir, + ).await; + } + + // Outbound evaluation broadcasts + Some(msg) = eval_broadcast_rx.recv() => { + if let Err(e) = event_tx.send(NetworkEvent::Message { + source: network.local_peer_id(), + message: msg, + }).await { + warn!("Failed to forward evaluation broadcast: {}", e); + } + } + + // Bittensor block events + Some(event) = async { + match block_rx.as_mut() { + Some(rx) => rx.recv().await, + None => std::future::pending().await, + } + } => { + handle_block_event( + event, + &subtensor, + &subtensor_signer, + &subtensor_client, + &state_manager, + netuid, + version_key, + ).await; + } + + // Heartbeat + _ = heartbeat_interval.tick() => { + let state_hash = state_manager.state_hash(); + let sequence = state_manager.sequence(); + debug!("Heartbeat: sequence={}, state_hash={}", sequence, hex::encode(&state_hash[..8])); + } + + // Periodic state persistence + _ = state_persist_interval.tick() => { + if let Err(e) = persist_state_to_storage(&storage, &state_manager).await { + warn!("Failed to persist state: {}", e); + } else { + debug!("State persisted to storage"); + } + } + + // Metagraph refresh + _ = metagraph_interval.tick() => { + if let Some(client) = bittensor_client_for_metagraph.as_ref() { + debug!("Refreshing metagraph from Bittensor..."); + match sync_metagraph(client, netuid).await { + Ok(mg) => { + info!("Metagraph refreshed: {} neurons", mg.n); + update_validator_set_from_metagraph(&mg, &validator_set); + if let Some(sc) = subtensor_client.as_mut() { + sc.set_metagraph(mg); + } + info!("Validator set updated: {} active validators", validator_set.active_count()); + } + Err(e) => { + warn!("Metagraph refresh failed: {}. Will retry on next interval.", e); + } + } + } else { + debug!("Metagraph refresh skipped (Bittensor not connected)"); + } + } + + // Check for stale validators + _ = stale_check_interval.tick() => { + validator_set.mark_stale_validators(); + debug!("Active validators: {}", validator_set.active_count()); + } + + // WASM evaluation check + _ = wasm_eval_interval.tick() => { + if let Some(ref executor) = wasm_executor { + process_wasm_evaluations( + executor, + &state_manager, + &keypair, + &eval_broadcast_tx, + ).await; + } + } + + // Stale job cleanup + _ = stale_job_interval.tick() => { + let now = chrono::Utc::now().timestamp_millis(); + let stale = state_manager.apply(|state| state.cleanup_stale_jobs(now)); + if !stale.is_empty() { + info!(count = stale.len(), "Cleaned up stale jobs"); + } + } + + // Periodic checkpoint + _ = checkpoint_interval.tick() => { + if let Some(handler) = shutdown_handler.as_mut() { + if let Err(e) = handler.create_checkpoint() { + warn!("Failed to create periodic checkpoint: {}", e); + } else { + debug!("Periodic checkpoint created"); + } + } + } + + // Ctrl+C + _ = tokio::signal::ctrl_c() => { + info!("Received shutdown signal, creating final checkpoint..."); + if let Some(handler) = shutdown_handler.as_mut() { + if let Err(e) = handler.create_checkpoint() { + error!("Failed to create shutdown checkpoint: {}", e); + } else { + info!("Shutdown checkpoint saved successfully"); + } + } + info!("Shutting down..."); + break; + } + } + } + + info!("Stopped."); + Ok(()) +} + +fn load_keypair(args: &Args) -> Result { + let secret = args + .secret_key + .as_ref() + .ok_or_else(|| anyhow::anyhow!("VALIDATOR_SECRET_KEY required"))? + .trim(); + + let hex = secret.strip_prefix("0x").unwrap_or(secret); + + if hex.len() == 64 { + if let Ok(bytes) = hex::decode(hex) { + if bytes.len() == 32 { + let mut arr = [0u8; 32]; + arr.copy_from_slice(&bytes); + return Ok(Keypair::from_seed(&arr)?); + } + } + } + + Ok(Keypair::from_mnemonic(secret)?) +} + +/// Load persisted state from distributed storage +async fn load_state_from_storage(storage: &Arc, netuid: u16) -> Option { + let key = StorageKey::new("state", STATE_STORAGE_KEY); + match storage.get_json::(&key).await { + Ok(Some(state)) => { + // Verify the state is for the correct netuid + if state.netuid != netuid { + warn!( + "Persisted state has different netuid ({} vs {}), ignoring", + state.netuid, netuid + ); + return None; + } + info!( + "Loaded persisted state: sequence={}, epoch={}, validators={}", + state.sequence, + state.epoch, + state.validators.len() + ); + Some(StateManager::new(state)) + } + Ok(None) => { + debug!("No persisted state found in storage"); + None + } + Err(e) => { + warn!("Failed to load persisted state: {}", e); + None + } + } +} + +/// Persist current state to distributed storage +async fn persist_state_to_storage( + storage: &Arc, + state_manager: &Arc, +) -> Result<()> { + let state = state_manager.snapshot(); + let key = StorageKey::new("state", STATE_STORAGE_KEY); + storage.put_json(key, &state).await?; + Ok(()) +} + +/// Update validator set from metagraph data +fn update_validator_set_from_metagraph(metagraph: &Metagraph, validator_set: &Arc) { + for neuron in metagraph.neurons.values() { + let hotkey_bytes: [u8; 32] = neuron.hotkey.clone().into(); + let hotkey = Hotkey(hotkey_bytes); + // Get effective stake capped to u64::MAX (neuron.stake is u128) + let stake = neuron.stake.min(u64::MAX as u128) as u64; + let record = ValidatorRecord::new(hotkey, stake); + if let Err(e) = validator_set.register_validator(record) { + debug!("Skipping validator registration: {}", e); + } + } +} + +async fn handle_network_event( + event: NetworkEvent, + consensus: &Arc>, + validator_set: &Arc, + state_manager: &Arc, + storage: &Arc, + wasm_module_dir: &Path, +) { + match event { + NetworkEvent::Message { source, message } => match message { + P2PMessage::Proposal(proposal) => { + let engine = consensus.write(); + match engine.handle_proposal(proposal) { + Ok(_prepare) => { + debug!("Proposal handled, prepare sent"); + } + Err(e) => { + warn!("Failed to handle proposal: {}", e); + } + } + } + P2PMessage::PrePrepare(_pp) => { + debug!("Received pre-prepare from {:?}", source); + } + P2PMessage::Prepare(p) => { + let engine = consensus.write(); + match engine.handle_prepare(p) { + Ok(Some(_commit)) => { + debug!("Prepare quorum reached, commit created"); + } + Ok(None) => { + debug!("Prepare received, waiting for quorum"); + } + Err(e) => { + warn!("Failed to handle prepare: {}", e); + } + } + } + P2PMessage::Commit(c) => { + let engine = consensus.write(); + match engine.handle_commit(c) { + Ok(Some(decision)) => { + info!("Consensus achieved for sequence {}", decision.sequence); + } + Ok(None) => { + debug!("Commit received, waiting for quorum"); + } + Err(e) => { + warn!("Failed to handle commit: {}", e); + } + } + } + P2PMessage::ViewChange(vc) => { + let engine = consensus.write(); + match engine.handle_view_change(vc) { + Ok(Some(new_view)) => { + info!("View change completed, new view: {}", new_view.view); + } + Ok(None) => { + debug!("View change in progress"); + } + Err(e) => { + warn!("View change error: {}", e); + } + } + } + P2PMessage::NewView(nv) => { + let engine = consensus.write(); + if let Err(e) = engine.handle_new_view(nv) { + warn!("Failed to handle new view: {}", e); + } + } + P2PMessage::Heartbeat(hb) => { + if let Err(e) = validator_set.update_from_heartbeat( + &hb.validator, + hb.state_hash, + hb.sequence, + hb.stake, + ) { + debug!("Heartbeat update skipped: {}", e); + } + } + P2PMessage::Submission(sub) => { + info!( + submission_id = %sub.submission_id, + challenge_id = %sub.challenge_id, + miner = %sub.miner.to_hex(), + "Received submission from P2P network" + ); + let already_exists = state_manager + .read(|state| state.pending_evaluations.contains_key(&sub.submission_id)); + if already_exists { + debug!( + submission_id = %sub.submission_id, + "Submission already exists in pending evaluations, skipping" + ); + } else { + let record = EvaluationRecord { + submission_id: sub.submission_id.clone(), + challenge_id: sub.challenge_id, + miner: sub.miner, + agent_hash: sub.agent_hash, + evaluations: std::collections::HashMap::new(), + aggregated_score: None, + finalized: false, + created_at: sub.timestamp, + finalized_at: None, + }; + state_manager.apply(|state| { + state.add_evaluation(record); + }); + info!( + submission_id = %sub.submission_id, + "Submission added to pending evaluations" + ); + } + } + P2PMessage::Evaluation(eval) => { + info!( + submission_id = %eval.submission_id, + validator = %eval.validator.to_hex(), + score = eval.score, + "Received evaluation from peer validator" + ); + let validator_hotkey = eval.validator.clone(); + let stake = validator_set + .get_validator(&validator_hotkey) + .map(|v| v.stake) + .unwrap_or(0); + let validator_eval = platform_p2p_consensus::ValidatorEvaluation { + score: eval.score, + stake, + timestamp: eval.timestamp, + signature: eval.signature.clone(), + }; + state_manager.apply(|state| { + if let Err(e) = state.add_validator_evaluation( + &eval.submission_id, + validator_hotkey.clone(), + validator_eval, + &eval.signature, + ) { + warn!( + submission_id = %eval.submission_id, + validator = %validator_hotkey.to_hex(), + error = %e, + "Failed to add peer evaluation to state" + ); + } else { + debug!( + submission_id = %eval.submission_id, + validator = %validator_hotkey.to_hex(), + score = eval.score, + "Peer evaluation recorded in state" + ); + } + }); + } + P2PMessage::StateRequest(req) => { + debug!( + requester = %req.requester.to_hex(), + sequence = req.current_sequence, + "Received state request" + ); + } + P2PMessage::StateResponse(resp) => { + debug!( + responder = %resp.responder.to_hex(), + sequence = resp.sequence, + "Received state response" + ); + } + P2PMessage::WeightVote(wv) => { + debug!( + validator = %wv.validator.to_hex(), + epoch = wv.epoch, + "Received weight vote" + ); + } + P2PMessage::PeerAnnounce(pa) => { + debug!( + validator = %pa.validator.to_hex(), + peer_id = %pa.peer_id, + addresses = pa.addresses.len(), + "Received peer announce" + ); + } + P2PMessage::JobClaim(claim) => { + info!( + validator = %claim.validator.to_hex(), + challenge_id = %claim.challenge_id, + max_jobs = claim.max_jobs, + "Received job claim" + ); + } + P2PMessage::JobAssignment(assignment) => { + info!( + submission_id = %assignment.submission_id, + challenge_id = %assignment.challenge_id, + assigned_validator = %assignment.assigned_validator.to_hex(), + assigner = %assignment.assigner.to_hex(), + "Received job assignment" + ); + let job = JobRecord { + submission_id: assignment.submission_id.clone(), + challenge_id: assignment.challenge_id, + assigned_validator: assignment.assigned_validator, + assigned_at: assignment.timestamp, + timeout_at: assignment.timestamp + JOB_TIMEOUT_MS, + status: JobStatus::Pending, + }; + state_manager.apply(|state| { + state.assign_job(job); + }); + } + P2PMessage::DataRequest(req) => { + info!( + request_id = %req.request_id, + requester = %req.requester.to_hex(), + challenge_id = %req.challenge_id, + data_type = %req.data_type, + "Received data request" + ); + if req.data_type == "wasm_module" { + let challenge_id_str = req.challenge_id.to_string(); + let wasm_key = StorageKey::new("wasm_modules", &challenge_id_str); + match storage.get_json::>(&wasm_key).await { + Ok(Some(wasm_bytes)) => { + info!( + request_id = %req.request_id, + challenge_id = %req.challenge_id, + wasm_bytes = wasm_bytes.len(), + "Found WASM module for data request" + ); + } + Ok(None) => { + debug!( + request_id = %req.request_id, + challenge_id = %req.challenge_id, + "No WASM module found for data request" + ); + } + Err(e) => { + warn!( + request_id = %req.request_id, + error = %e, + "Failed to read WASM module for data request" + ); + } + } + } + } + P2PMessage::DataResponse(resp) => { + debug!( + request_id = %resp.request_id, + responder = %resp.responder.to_hex(), + challenge_id = %resp.challenge_id, + data_bytes = resp.data.len(), + "Received data response" + ); + if resp.data_type == "wasm_module" && !resp.data.is_empty() { + let challenge_id_str = resp.challenge_id.to_string(); + let module_path = wasm_module_dir.join(format!("{}.wasm", challenge_id_str)); + match tokio::fs::write(&module_path, &resp.data).await { + Ok(()) => { + info!( + request_id = %resp.request_id, + challenge_id = %resp.challenge_id, + path = %module_path.display(), + bytes = resp.data.len(), + "Saved WASM module from data response to filesystem" + ); + } + Err(e) => { + error!( + request_id = %resp.request_id, + challenge_id = %resp.challenge_id, + error = %e, + "Failed to write WASM module to filesystem" + ); + } + } + let wasm_key = StorageKey::new("wasm_modules", &challenge_id_str); + if let Err(e) = storage.put_json(wasm_key, &resp.data).await { + warn!( + request_id = %resp.request_id, + challenge_id = %resp.challenge_id, + error = %e, + "Failed to store WASM module in distributed storage" + ); + } + } + } + P2PMessage::TaskProgress(progress) => { + debug!( + submission_id = %progress.submission_id, + challenge_id = %progress.challenge_id, + validator = %progress.validator.to_hex(), + task_index = progress.task_index, + total_tasks = progress.total_tasks, + progress_pct = progress.progress_pct, + "Received task progress" + ); + let record = TaskProgressRecord { + submission_id: progress.submission_id.clone(), + challenge_id: progress.challenge_id, + validator: progress.validator, + task_index: progress.task_index, + total_tasks: progress.total_tasks, + status: progress.status, + progress_pct: progress.progress_pct, + updated_at: progress.timestamp, + }; + state_manager.apply(|state| { + state.update_task_progress(record); + }); + } + P2PMessage::TaskResult(result) => { + info!( + submission_id = %result.submission_id, + challenge_id = %result.challenge_id, + validator = %result.validator.to_hex(), + task_id = %result.task_id, + passed = result.passed, + score = result.score, + execution_time_ms = result.execution_time_ms, + "Received task result" + ); + } + P2PMessage::LeaderboardRequest(req) => { + debug!( + requester = %req.requester.to_hex(), + challenge_id = %req.challenge_id, + limit = req.limit, + offset = req.offset, + "Received leaderboard request" + ); + } + P2PMessage::LeaderboardResponse(resp) => { + debug!( + responder = %resp.responder.to_hex(), + challenge_id = %resp.challenge_id, + total_count = resp.total_count, + "Received leaderboard response" + ); + } + P2PMessage::ChallengeUpdate(update) => { + info!( + challenge_id = %update.challenge_id, + updater = %update.updater.to_hex(), + update_type = %update.update_type, + data_bytes = update.data.len(), + "Received challenge update" + ); + } + P2PMessage::StorageProposal(proposal) => { + debug!( + proposal_id = %hex::encode(&proposal.proposal_id[..8]), + challenge_id = %proposal.challenge_id, + proposer = %proposal.proposer.to_hex(), + key_len = proposal.key.len(), + value_len = proposal.value.len(), + "Received storage proposal" + ); + } + P2PMessage::StorageVote(vote) => { + debug!( + proposal_id = %hex::encode(&vote.proposal_id[..8]), + voter = %vote.voter.to_hex(), + approve = vote.approve, + "Received storage vote" + ); + } + P2PMessage::ReviewAssignment(msg) => { + debug!( + submission_id = %msg.submission_id, + assigner = %msg.assigner.to_hex(), + assigned_count = msg.assigned_validators.len(), + "Received review assignment" + ); + } + P2PMessage::ReviewDecline(msg) => { + let safe_reason = sanitize_for_log(&msg.reason); + debug!( + submission_id = %msg.submission_id, + validator = %msg.validator.to_hex(), + reason = %safe_reason, + "Received review decline" + ); + } + P2PMessage::ReviewResult(msg) => { + debug!( + submission_id = %msg.submission_id, + validator = %msg.validator.to_hex(), + score = msg.score, + "Received review result" + ); + } + P2PMessage::AgentLogProposal(msg) => { + debug!( + submission_id = %msg.submission_id, + validator = %msg.validator_hotkey.to_hex(), + "Received agent log proposal" + ); + } + P2PMessage::SudoAction(msg) => { + info!( + signer = %msg.signer.to_hex(), + "Received sudo action from P2P network" + ); + let is_sudo = state_manager.read(|state| state.is_sudo(&msg.signer)); + if !is_sudo { + warn!( + signer = %msg.signer.to_hex(), + "Rejected sudo action: signer is not the sudo key" + ); + } else { + match msg.action { + platform_core::SudoAction::AddChallenge { + ref name, + description: _, + ref wasm_code, + owner: _, + config: _, + weight, + } => { + let challenge_id = ChallengeId::new(); + info!( + challenge_id = %challenge_id, + name = %name, + weight = weight, + wasm_bytes = wasm_code.len(), + "Sudo: adding challenge" + ); + let challenge_id_str = challenge_id.to_string(); + let module_path = + wasm_module_dir.join(format!("{}.wasm", challenge_id_str)); + if let Err(e) = tokio::fs::write(&module_path, wasm_code).await { + error!( + challenge_id = %challenge_id, + error = %e, + "Failed to write WASM module to filesystem" + ); + } else { + info!( + challenge_id = %challenge_id, + path = %module_path.display(), + "WASM module written to filesystem" + ); + } + let wasm_key = StorageKey::new("wasm_modules", &challenge_id_str); + if let Err(e) = storage.put_json(wasm_key, wasm_code).await { + warn!( + challenge_id = %challenge_id, + error = %e, + "Failed to store WASM module in distributed storage" + ); + } + let signer = msg.signer.clone(); + let challenge_name = name.clone(); + state_manager.apply(|state| { + state.add_challenge_from_sudo( + challenge_id, + challenge_name, + weight, + signer, + ); + }); + info!( + challenge_id = %challenge_id, + "Challenge registered in state" + ); + } + platform_core::SudoAction::RemoveChallenge { ref challenge_id } => { + info!(challenge_id = %challenge_id, "Sudo: removing challenge"); + } + platform_core::SudoAction::EditChallenge { + ref challenge_id, .. + } => { + info!(challenge_id = %challenge_id, "Sudo: editing challenge"); + } + platform_core::SudoAction::StopNetwork { ref reason } => { + let safe_reason = sanitize_for_log(reason); + info!(reason = %safe_reason, "Sudo: stopping network (burn mode)"); + state_manager.apply(|state| { + state.stop_network(reason.clone()); + }); + } + _ => { + debug!("Received other sudo action type"); + } + } + } + } + }, + NetworkEvent::PeerConnected(peer_id) => { + info!("Peer connected: {}", peer_id); + } + NetworkEvent::PeerDisconnected(peer_id) => { + info!("Peer disconnected: {}", peer_id); + } + NetworkEvent::PeerIdentified { + peer_id, + hotkey, + addresses, + } => { + info!( + "Peer identified: {} with {} addresses", + peer_id, + addresses.len() + ); + if let Some(hk) = hotkey { + debug!(" Hotkey: {:?}", hk); + } + } + } +} + +async fn handle_block_event( + event: BlockSyncEvent, + subtensor: &Option>, + signer: &Option>, + _client: &Option, + state_manager: &Arc, + netuid: u16, + version_key: u64, +) { + match event { + BlockSyncEvent::NewBlock { block_number, .. } => { + debug!("Block {}", block_number); + // Link state to Bittensor block (block hash not available in event, use zeros) + state_manager.apply(|state| { + state.link_to_bittensor_block(block_number, [0u8; 32]); + }); + } + BlockSyncEvent::EpochTransition { + old_epoch, + new_epoch, + block, + } => { + info!( + "Epoch transition: {} -> {} (block {})", + old_epoch, new_epoch, block + ); + + // Transition state to next epoch + state_manager.apply(|state| { + state.next_epoch(); + }); + } + BlockSyncEvent::CommitWindowOpen { epoch, block } => { + info!( + "=== COMMIT WINDOW OPEN: epoch {} block {} ===", + epoch, block + ); + + // Get weights from decentralized state + if let (Some(st), Some(sig)) = (subtensor.as_ref(), signer.as_ref()) { + let network_stopped = state_manager.read(|state| state.network_stopped); + if network_stopped { + info!("Network is stopped - submitting burn weights to UID 0"); + match st + .set_mechanism_weights( + sig, + netuid, + 0, + &[0u16], + &[65535u16], + version_key, + ExtrinsicWait::Finalized, + ) + .await + { + Ok(resp) if resp.success => { + info!( + "Burn weights submitted (network stopped): {:?}", + resp.tx_hash + ); + } + Ok(resp) => warn!("Burn weight submission issue: {}", resp.message), + Err(e) => error!("Burn weight submission failed: {}", e), + } + return; + } + + let final_weights = state_manager.apply(|state| state.finalize_weights()); + + match final_weights { + Some(weights) if !weights.is_empty() => { + // Convert to arrays for submission + let uids: Vec = weights.iter().map(|(uid, _)| *uid).collect(); + let vals: Vec = weights.iter().map(|(_, w)| *w).collect(); + + info!("Submitting weights for {} UIDs", uids.len()); + match st + .set_mechanism_weights( + sig, + netuid, + 0, + &uids, + &vals, + version_key, + ExtrinsicWait::Finalized, + ) + .await + { + Ok(resp) if resp.success => { + info!("Weights submitted: {:?}", resp.tx_hash); + } + Ok(resp) => warn!("Weight submission issue: {}", resp.message), + Err(e) => error!("Weight submission failed: {}", e), + } + } + _ => { + info!("No weights for epoch {} - submitting burn weights", epoch); + // Submit burn weights (uid 0 with max weight) + match st + .set_mechanism_weights( + sig, + netuid, + 0, + &[0u16], + &[65535u16], + version_key, + ExtrinsicWait::Finalized, + ) + .await + { + Ok(resp) if resp.success => { + info!("Burn weights submitted: {:?}", resp.tx_hash); + } + Ok(resp) => warn!("Burn weight submission issue: {}", resp.message), + Err(e) => error!("Burn weight submission failed: {}", e), + } + } + } + } + } + BlockSyncEvent::RevealWindowOpen { epoch, block } => { + info!( + "=== REVEAL WINDOW OPEN: epoch {} block {} ===", + epoch, block + ); + + if let (Some(st), Some(sig)) = (subtensor.as_ref(), signer.as_ref()) { + if st.has_pending_commits().await { + info!("Revealing pending commits..."); + match st.reveal_all_pending(sig, ExtrinsicWait::Finalized).await { + Ok(results) => { + for resp in results { + if resp.success { + info!("Revealed: {:?}", resp.tx_hash); + } else { + warn!("Reveal issue: {}", resp.message); + } + } + } + Err(e) => error!("Reveal failed: {}", e), + } + } else { + debug!("No pending commits to reveal"); + } + } + } + BlockSyncEvent::PhaseChange { + old_phase, + new_phase, + epoch, + .. + } => { + debug!( + "Phase change: {:?} -> {:?} (epoch {})", + old_phase, new_phase, epoch + ); + } + BlockSyncEvent::Disconnected(reason) => { + warn!("Bittensor disconnected: {}", reason); + } + BlockSyncEvent::Reconnected => { + info!("Bittensor reconnected"); + } + } +} + +async fn process_wasm_evaluations( + executor: &Arc, + state_manager: &Arc, + keypair: &Keypair, + eval_broadcast_tx: &tokio::sync::mpsc::Sender, +) { + let pending: Vec<(String, ChallengeId, String)> = state_manager.read(|state| { + state + .pending_evaluations + .iter() + .filter(|(_, record)| { + !record.finalized && !record.evaluations.contains_key(&keypair.hotkey()) + }) + .map(|(id, record)| (id.clone(), record.challenge_id, record.agent_hash.clone())) + .collect() + }); + + if pending.is_empty() { + return; + } + + for (submission_id, challenge_id, _agent_hash) in pending { + let module_filename = format!("{}.wasm", challenge_id); + + if !executor.module_exists(&module_filename) { + debug!( + submission_id = %submission_id, + challenge_id = %challenge_id, + "No WASM module found for challenge, skipping WASM evaluation" + ); + continue; + } + + let network_policy = wasm_runtime_interface::NetworkPolicy::default(); + + let input_data = submission_id.as_bytes().to_vec(); + let challenge_id_str = challenge_id.to_string(); + + let executor = Arc::clone(executor); + let module_filename_clone = module_filename.clone(); + + let result = tokio::task::spawn_blocking(move || { + executor.execute_evaluation( + &module_filename_clone, + &network_policy, + &input_data, + &challenge_id_str, + &[], + ) + }) + .await; + + let (score, eval_metrics) = match result { + Ok(Ok((output, metrics))) => { + info!( + submission_id = %submission_id, + challenge_id = %challenge_id, + score = output.score, + valid = output.valid, + message = %output.message, + execution_time_ms = metrics.execution_time_ms, + memory_bytes = metrics.memory_used_bytes, + network_requests = metrics.network_requests_made, + fuel_consumed = ?metrics.fuel_consumed, + "WASM evaluation succeeded" + ); + let normalized = (output.score as f64) / i64::MAX as f64; + let em = EvaluationMetrics { + primary_score: normalized, + secondary_metrics: vec![], + execution_time_ms: metrics.execution_time_ms as u64, + memory_usage_bytes: Some(metrics.memory_used_bytes), + timed_out: false, + error: None, + }; + (normalized, em) + } + Ok(Err(e)) => { + warn!( + submission_id = %submission_id, + challenge_id = %challenge_id, + error = %e, + "WASM evaluation failed, reporting score 0" + ); + let em = EvaluationMetrics { + primary_score: 0.0, + secondary_metrics: vec![], + execution_time_ms: 0, + memory_usage_bytes: None, + timed_out: false, + error: Some(e.to_string()), + }; + (0.0, em) + } + Err(e) => { + error!( + submission_id = %submission_id, + challenge_id = %challenge_id, + error = %e, + "WASM evaluation task panicked, reporting score 0" + ); + let em = EvaluationMetrics { + primary_score: 0.0, + secondary_metrics: vec![], + execution_time_ms: 0, + memory_usage_bytes: None, + timed_out: false, + error: Some(e.to_string()), + }; + (0.0, em) + } + }; + + let score_clamped = score.clamp(0.0, 1.0); + let validator_hotkey = keypair.hotkey(); + let timestamp = chrono::Utc::now().timestamp_millis(); + + #[derive(serde::Serialize)] + struct EvaluationSigningData<'a> { + submission_id: &'a str, + score: f64, + } + let signing_data = EvaluationSigningData { + submission_id: &submission_id, + score: score_clamped, + }; + let signing_bytes = match bincode::serialize(&signing_data) { + Ok(bytes) => bytes, + Err(e) => { + error!( + submission_id = %submission_id, + error = %e, + "Failed to serialize evaluation signing data" + ); + continue; + } + }; + let signature = match keypair.sign_bytes(&signing_bytes) { + Ok(sig) => sig, + Err(e) => { + error!( + submission_id = %submission_id, + error = %e, + "Failed to sign evaluation" + ); + continue; + } + }; + + let eval = platform_p2p_consensus::ValidatorEvaluation { + score: score_clamped, + stake: 0, + timestamp, + signature: signature.clone(), + }; + + state_manager.apply(|state| { + if let Err(e) = state.add_validator_evaluation( + &submission_id, + validator_hotkey.clone(), + eval, + &signature, + ) { + warn!( + submission_id = %submission_id, + error = %e, + "Failed to add WASM evaluation to state" + ); + } else { + debug!( + submission_id = %submission_id, + score = score_clamped, + "WASM evaluation recorded in state" + ); + } + }); + + let eval_msg = P2PMessage::Evaluation(EvaluationMessage { + submission_id: submission_id.clone(), + challenge_id, + validator: validator_hotkey, + score: score_clamped, + metrics: eval_metrics, + signature, + timestamp, + }); + if let Err(e) = eval_broadcast_tx.send(eval_msg).await { + warn!( + submission_id = %submission_id, + error = %e, + "Failed to queue evaluation broadcast" + ); + } + } +} diff --git a/bins/validator-node/src/wasm_executor.rs b/bins/validator-node/src/wasm_executor.rs new file mode 100644 index 000000000..ac3fe0897 --- /dev/null +++ b/bins/validator-node/src/wasm_executor.rs @@ -0,0 +1,853 @@ +use anyhow::{Context, Result}; +use bincode::Options; +use parking_lot::RwLock; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Instant; +use tracing::{debug, info}; +use wasm_runtime_interface::{ + ConsensusPolicy, ExecPolicy, InMemoryStorageBackend, InstanceConfig, LlmPolicy, + NetworkHostFunctions, NetworkPolicy, RuntimeConfig, SandboxPolicy, StorageBackend, + StorageHostConfig, TerminalPolicy, TimePolicy, WasmModule, WasmRuntime, WasmRuntimeError, +}; + +const MAX_EVALUATION_OUTPUT_SIZE: u64 = 64 * 1024 * 1024; +#[allow(dead_code)] +const MAX_ROUTE_OUTPUT_SIZE: u64 = 16 * 1024 * 1024; +#[allow(dead_code)] +const MAX_TASK_OUTPUT_SIZE: u64 = 16 * 1024 * 1024; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationInput { + pub agent_data: Vec, + pub challenge_id: String, + pub params: Vec, + #[serde(default)] + pub task_definition: Option>, + #[serde(default)] + pub environment_config: Option>, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationOutput { + pub score: i64, + pub valid: bool, + pub message: String, + #[serde(default)] + pub metrics: Option>, + #[serde(default)] + pub details: Option>, +} + +#[allow(dead_code)] +impl EvaluationOutput { + pub fn success(score: i64, message: &str) -> Self { + Self { + score, + valid: true, + message: String::from(message), + metrics: None, + details: None, + } + } + + pub fn failure(message: &str) -> Self { + Self { + score: 0, + valid: false, + message: String::from(message), + metrics: None, + details: None, + } + } +} + +pub struct WasmExecutorConfig { + pub module_dir: PathBuf, + pub max_memory_bytes: u64, + pub enable_fuel: bool, + pub fuel_limit: Option, + pub storage_host_config: StorageHostConfig, + pub storage_backend: Arc, + pub chutes_api_key: Option, +} + +impl std::fmt::Debug for WasmExecutorConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("WasmExecutorConfig") + .field("module_dir", &self.module_dir) + .field("max_memory_bytes", &self.max_memory_bytes) + .field("enable_fuel", &self.enable_fuel) + .field("fuel_limit", &self.fuel_limit) + .field( + "chutes_api_key", + &self.chutes_api_key.as_ref().map(|_| "[REDACTED]"), + ) + .finish() + } +} + +impl Default for WasmExecutorConfig { + fn default() -> Self { + Self { + module_dir: PathBuf::from("./wasm_modules"), + max_memory_bytes: 512 * 1024 * 1024, + enable_fuel: false, + fuel_limit: None, + storage_host_config: StorageHostConfig::default(), + storage_backend: Arc::new(InMemoryStorageBackend::new()), + chutes_api_key: None, + } + } +} + +pub struct ExecutionMetrics { + pub execution_time_ms: u128, + pub memory_used_bytes: u64, + pub network_requests_made: u32, + pub fuel_consumed: Option, +} + +pub struct WasmChallengeExecutor { + runtime: WasmRuntime, + config: WasmExecutorConfig, + module_cache: RwLock>>, +} + +#[allow(dead_code)] +impl WasmChallengeExecutor { + pub fn new(config: WasmExecutorConfig) -> Result { + let runtime_config = RuntimeConfig { + max_memory_bytes: config.max_memory_bytes, + max_instances: 32, + allow_fuel: config.enable_fuel, + fuel_limit: config.fuel_limit, + }; + + let runtime = WasmRuntime::new(runtime_config) + .map_err(|e| anyhow::anyhow!("Failed to create WASM runtime: {}", e))?; + + info!( + module_dir = %config.module_dir.display(), + max_memory_bytes = config.max_memory_bytes, + fuel_enabled = config.enable_fuel, + "WASM challenge executor initialized" + ); + + Ok(Self { + runtime, + config, + module_cache: RwLock::new(HashMap::new()), + }) + } + + pub fn execute_evaluation( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + agent_data: &[u8], + challenge_id: &str, + params: &[u8], + ) -> Result<(EvaluationOutput, ExecutionMetrics)> { + self.execute_evaluation_with_sandbox( + module_path, + network_policy, + &SandboxPolicy::default(), + agent_data, + challenge_id, + params, + ) + } + + pub fn execute_evaluation_with_sandbox( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + sandbox_policy: &SandboxPolicy, + agent_data: &[u8], + challenge_id: &str, + params: &[u8], + ) -> Result<(EvaluationOutput, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let input = EvaluationInput { + agent_data: agent_data.to_vec(), + challenge_id: challenge_id.to_string(), + params: params.to_vec(), + task_definition: None, + environment_config: None, + }; + + let serialized = + bincode::serialize(&input).context("Failed to serialize EvaluationInput")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: sandbox_policy.clone(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: challenge_id.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig { + allow_direct_writes: true, + require_consensus: false, + ..self.config.storage_host_config.clone() + }, + storage_backend: Arc::clone(&self.config.storage_backend), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let ptr = self.allocate_input(&mut instance, &serialized)?; + + instance + .write_memory(ptr as usize, &serialized) + .map_err(|e| anyhow::anyhow!("Failed to write input data to WASM memory: {}", e))?; + + let result = instance + .call_i32_i32_return_i64("evaluate", ptr, serialized.len() as i32) + .map_err(|e| match &e { + WasmRuntimeError::FuelExhausted => { + anyhow::anyhow!("WASM execution exceeded fuel limit") + } + WasmRuntimeError::Execution(msg) if msg.contains("timeout") => { + anyhow::anyhow!("WASM execution timed out") + } + _ => anyhow::anyhow!("WASM evaluate call failed: {}", e), + })?; + + let out_len = (result >> 32) as i32; + let out_ptr = result as i32; + + if out_ptr == 0 && out_len == 0 { + return Err(anyhow::anyhow!( + "WASM evaluate returned null pointer, deserialization failed inside module" + )); + } + + let output_bytes = instance + .read_memory(out_ptr as usize, out_len as usize) + .map_err(|e| { + anyhow::anyhow!("Failed to read evaluation output from WASM memory: {}", e) + })?; + + let output: EvaluationOutput = bincode::DefaultOptions::new() + .with_limit(MAX_EVALUATION_OUTPUT_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&output_bytes) + .context("Failed to deserialize EvaluationOutput from WASM module")?; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + challenge_id, + score = output.score, + valid = output.valid, + message = %output.message, + execution_time_ms = metrics.execution_time_ms, + memory_bytes = metrics.memory_used_bytes, + network_requests = metrics.network_requests_made, + fuel_consumed = ?metrics.fuel_consumed, + "WASM evaluation completed" + ); + + Ok((output, metrics)) + } + + pub fn execute_validation( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + agent_data: &[u8], + challenge_id: &str, + params: &[u8], + ) -> Result<(bool, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let input = EvaluationInput { + agent_data: agent_data.to_vec(), + challenge_id: challenge_id.to_string(), + params: params.to_vec(), + task_definition: None, + environment_config: None, + }; + + let serialized = + bincode::serialize(&input).context("Failed to serialize EvaluationInput")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: SandboxPolicy::default(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: challenge_id.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig { + allow_direct_writes: true, + require_consensus: false, + ..self.config.storage_host_config.clone() + }, + storage_backend: Arc::clone(&self.config.storage_backend), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let ptr = self.allocate_input(&mut instance, &serialized)?; + + instance + .write_memory(ptr as usize, &serialized) + .map_err(|e| anyhow::anyhow!("Failed to write input data to WASM memory: {}", e))?; + + let result = instance + .call_i32_i32_return_i32("validate", ptr, serialized.len() as i32) + .map_err(|e| match &e { + WasmRuntimeError::FuelExhausted => { + anyhow::anyhow!("WASM execution exceeded fuel limit") + } + WasmRuntimeError::Execution(msg) if msg.contains("timeout") => { + anyhow::anyhow!("WASM execution timed out") + } + _ => anyhow::anyhow!("WASM validate call failed: {}", e), + })?; + + let valid = result != 0; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + challenge_id, + valid, + execution_time_ms = metrics.execution_time_ms, + memory_bytes = metrics.memory_used_bytes, + network_requests = metrics.network_requests_made, + fuel_consumed = ?metrics.fuel_consumed, + "WASM validation completed" + ); + + Ok((valid, metrics)) + } + + fn allocate_input( + &self, + instance: &mut wasm_runtime_interface::ChallengeInstance, + input_data: &[u8], + ) -> Result { + if let Ok(p) = instance.call_i32_return_i32("alloc", input_data.len() as i32) { + return Ok(p); + } + + if let Ok(p) = instance.call_i32_i32_return_i32("allocate", input_data.len() as i32, 0) { + return Ok(p); + } + + let mem_size = instance.memory().data_size(instance.store()); + let offset = mem_size.saturating_sub(input_data.len() + 1024); + if offset == 0 { + return Err(anyhow::anyhow!( + "WASM module has insufficient memory for input data" + )); + } + Ok(offset as i32) + } + + pub fn execute_get_tasks( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + sandbox_policy: &SandboxPolicy, + ) -> Result<(Vec, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: sandbox_policy.clone(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: module_path.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig::default(), + storage_backend: Arc::new(InMemoryStorageBackend::new()), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let result = instance + .call_return_i64("get_tasks") + .map_err(|e| anyhow::anyhow!("WASM get_tasks call failed: {}", e))?; + + let out_len = (result >> 32) as i32; + let out_ptr = (result & 0xFFFF_FFFF) as i32; + + if out_len > 0 && out_len as u64 > MAX_TASK_OUTPUT_SIZE { + return Err(anyhow::anyhow!( + "WASM get_tasks output size {} exceeds maximum allowed {}", + out_len, + MAX_TASK_OUTPUT_SIZE + )); + } + + let result_data = if out_ptr > 0 && out_len > 0 { + instance + .read_memory(out_ptr as usize, out_len as usize) + .map_err(|e| { + anyhow::anyhow!("failed to read WASM memory for get_tasks output: {}", e) + })? + } else { + Vec::new() + }; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + result_bytes = result_data.len(), + execution_time_ms = metrics.execution_time_ms, + "WASM get_tasks completed" + ); + + Ok((result_data, metrics)) + } + + pub fn execute_configure( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + sandbox_policy: &SandboxPolicy, + config_data: &[u8], + ) -> Result<(i32, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: sandbox_policy.clone(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: module_path.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig::default(), + storage_backend: Arc::new(InMemoryStorageBackend::new()), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let ptr = self.allocate_input(&mut instance, config_data)?; + + instance + .write_memory(ptr as usize, config_data) + .map_err(|e| anyhow::anyhow!("Failed to write config data to WASM memory: {}", e))?; + + let result = instance + .call_i32_i32_return_i32("configure", ptr, config_data.len() as i32) + .map_err(|e| anyhow::anyhow!("WASM configure call failed: {}", e))?; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + result, + execution_time_ms = metrics.execution_time_ms, + "WASM configure completed" + ); + + Ok((result, metrics)) + } + + pub fn execute_get_routes( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + sandbox_policy: &SandboxPolicy, + ) -> Result<(Vec, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: sandbox_policy.clone(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: module_path.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig::default(), + storage_backend: Arc::new(InMemoryStorageBackend::new()), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let result = instance + .call_return_i64("get_routes") + .map_err(|e| anyhow::anyhow!("WASM get_routes call failed: {}", e))?; + + let out_len = (result >> 32) as i32; + let out_ptr = (result & 0xFFFF_FFFF) as i32; + + if out_len > 0 && out_len as u64 > MAX_ROUTE_OUTPUT_SIZE { + return Err(anyhow::anyhow!( + "WASM get_routes output size {} exceeds maximum allowed {}", + out_len, + MAX_ROUTE_OUTPUT_SIZE + )); + } + + let result_data = if out_ptr > 0 && out_len > 0 { + instance + .read_memory(out_ptr as usize, out_len as usize) + .map_err(|e| { + anyhow::anyhow!("failed to read WASM memory for get_routes output: {}", e) + })? + } else { + Vec::new() + }; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + result_bytes = result_data.len(), + execution_time_ms = metrics.execution_time_ms, + "WASM get_routes completed" + ); + + Ok((result_data, metrics)) + } + + pub fn execute_handle_route( + &self, + module_path: &str, + network_policy: &NetworkPolicy, + sandbox_policy: &SandboxPolicy, + request_data: &[u8], + ) -> Result<(Vec, ExecutionMetrics)> { + let start = Instant::now(); + + let module = self + .load_module(module_path) + .context("Failed to load WASM module")?; + + let network_host_fns = Arc::new(NetworkHostFunctions::all()); + + let instance_config = InstanceConfig { + network_policy: network_policy.clone(), + sandbox_policy: sandbox_policy.clone(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: "memory".to_string(), + challenge_id: module_path.to_string(), + validator_id: "validator".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig { + allow_direct_writes: true, + require_consensus: false, + ..self.config.storage_host_config.clone() + }, + storage_backend: Arc::clone(&self.config.storage_backend), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + llm_policy: match &self.config.chutes_api_key { + Some(key) => LlmPolicy::with_api_key(key.clone()), + None => LlmPolicy::default(), + }, + ..Default::default() + }; + + let mut instance = self + .runtime + .instantiate(&module, instance_config, Some(network_host_fns)) + .map_err(|e| anyhow::anyhow!("WASM instantiation failed: {}", e))?; + + let initial_fuel = instance.fuel_remaining(); + + let ptr = self.allocate_input(&mut instance, request_data)?; + + instance + .write_memory(ptr as usize, request_data) + .map_err(|e| anyhow::anyhow!("Failed to write request data to WASM memory: {}", e))?; + + let result = instance + .call_i32_i32_return_i64("handle_route", ptr, request_data.len() as i32) + .map_err(|e| match &e { + WasmRuntimeError::FuelExhausted => { + anyhow::anyhow!("WASM execution exceeded fuel limit") + } + WasmRuntimeError::Execution(msg) if msg.contains("timeout") => { + anyhow::anyhow!("WASM execution timed out") + } + _ => anyhow::anyhow!("WASM handle_route call failed: {}", e), + })?; + + let out_len = (result >> 32) as i32; + let out_ptr = (result & 0xFFFF_FFFF) as i32; + + if out_len > 0 && out_len as u64 > MAX_ROUTE_OUTPUT_SIZE { + return Err(anyhow::anyhow!( + "WASM handle_route output size {} exceeds maximum allowed {}", + out_len, + MAX_ROUTE_OUTPUT_SIZE + )); + } + + let result_data = if out_ptr > 0 && out_len > 0 { + instance + .read_memory(out_ptr as usize, out_len as usize) + .map_err(|e| { + anyhow::anyhow!("failed to read WASM memory for handle_route output: {}", e) + })? + } else { + Vec::new() + }; + + let fuel_consumed = match (initial_fuel, instance.fuel_remaining()) { + (Some(initial), Some(remaining)) => Some(initial.saturating_sub(remaining)), + _ => None, + }; + + let metrics = ExecutionMetrics { + execution_time_ms: start.elapsed().as_millis(), + memory_used_bytes: instance.memory().data_size(instance.store()) as u64, + network_requests_made: instance.network_requests_made(), + fuel_consumed, + }; + + info!( + module = module_path, + result_bytes = result_data.len(), + execution_time_ms = metrics.execution_time_ms, + "WASM handle_route completed" + ); + + Ok((result_data, metrics)) + } + + fn load_module(&self, module_path: &str) -> Result> { + { + let cache = self.module_cache.read(); + if let Some(module) = cache.get(module_path) { + debug!(module = module_path, "WASM module loaded from cache"); + return Ok(Arc::clone(module)); + } + } + + let full_path = self.config.module_dir.join(module_path); + let wasm_bytes = std::fs::read(&full_path) + .with_context(|| format!("Failed to read WASM module from {}", full_path.display()))?; + + info!( + module = module_path, + size_bytes = wasm_bytes.len(), + "Compiling WASM module" + ); + + let module = self + .runtime + .compile_module(&wasm_bytes) + .map_err(|e| anyhow::anyhow!("WASM compilation failed: {}", e))?; + + let module = Arc::new(module); + + { + let mut cache = self.module_cache.write(); + cache.insert(module_path.to_string(), Arc::clone(&module)); + } + + info!(module = module_path, "WASM module compiled and cached"); + Ok(module) + } + + pub fn invalidate_cache(&self, module_path: &str) { + let mut cache = self.module_cache.write(); + if cache.remove(module_path).is_some() { + info!(module = module_path, "WASM module cache entry invalidated"); + } + } + + pub fn clear_cache(&self) { + let mut cache = self.module_cache.write(); + let count = cache.len(); + cache.clear(); + info!(cleared = count, "WASM module cache cleared"); + } + + pub fn cached_module_count(&self) -> usize { + self.module_cache.read().len() + } + + pub fn resolve_module_path(&self, module_path: &str) -> PathBuf { + self.config.module_dir.join(module_path) + } + + pub fn module_exists(&self, module_path: &str) -> bool { + self.resolve_module_path(module_path).exists() + } +} diff --git a/challenges/.gitkeep b/challenges/.gitkeep new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/challenges/.gitkeep @@ -0,0 +1 @@ + diff --git a/challenges/README.md b/challenges/README.md new file mode 100644 index 000000000..b9d5306fc --- /dev/null +++ b/challenges/README.md @@ -0,0 +1,76 @@ +# Platform Challenge Crates + +This directory contains challenge crates that integrate with the Platform validator network. All challenge execution is **WASM-only**. + +## Directory Structure + +``` +challenges/ +โ”œโ”€โ”€ README.md # This file +โ”œโ”€โ”€ compiled/ # Built WASM artifacts (generated by build-wasm.sh) +โ””โ”€โ”€ [your-challenge]/ # Your custom challenge crate +``` + +## Challenge Lifecycle + +```mermaid +sequenceDiagram + participant Owner as Sudo Owner + participant Registry as Challenge Registry + participant Validators as Validator Set + participant Runtime as WASM Runtime + + Owner->>Registry: Signed metadata update + Registry->>Validators: Broadcast metadata + Validators->>Runtime: Load WASM module + Runtime-->>Validators: Policy + sandbox ready + Validators-->>Owner: Consensus approval +``` + +## Adding a New Challenge Crate + +1. Create your challenge crate in this directory or reference it as a git dependency. +2. Implement the `Challenge` trait from `platform-challenge-sdk-wasm` (WASM) or the `ServerChallenge` trait from `platform-challenge-sdk` (server-side). +3. Register your challenge metadata via the challenge registry flow. +4. Update the workspace `Cargo.toml` if adding locally. + +## Challenge Crate Requirements + +### WASM Challenges (Recommended) + +- Must implement `platform-challenge-sdk-wasm::Challenge` trait. +- Must use `register_challenge!` macro to export the required WASM ABI functions. +- Must produce deterministic results for consensus. +- WASM module exports: `evaluate(ptr, len) -> i64`, `validate(ptr, len) -> i32`, `alloc(size) -> i32`. +- Input/output serialized with `bincode` across the WASM boundary using `EvaluationInput` / `EvaluationOutput`. + +## Build WASM Artifacts + +```bash +# Build a specific challenge crate +./scripts/build-wasm.sh + +# Build all challenge crates (discovers crates under challenges/*/) +./scripts/build-wasm.sh +``` + +The build script will: +- Compile the crate for `wasm32-unknown-unknown` +- Strip debug info with `wasm-strip` (if available) +- Optimize with `wasm-opt` (if available) +- Copy the artifact to `challenges/compiled/` +- Print the SHA256 hash for registry verification + +## External Challenge Crates + +Challenge crates can also live in external repositories. They should: + +- Import `platform-challenge-sdk-wasm` as a dependency for WASM challenges. +- Implement the `Challenge` trait and use `register_challenge!` macro. +- Publish a WASM artifact for production use. + +## Documentation + +- [Challenge Integration Guide](../docs/challenge-integration.md) +- [Challenges](../docs/challenges.md) +- [Architecture](../docs/architecture.md) diff --git a/cli/Cargo.toml b/cli/Cargo.toml deleted file mode 100644 index 16798f408..000000000 --- a/cli/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "term-cli" -version.workspace = true -edition.workspace = true -authors.workspace = true -license.workspace = true -description = "Terminal Benchmark Challenge CLI โ€” ratatui TUI for monitoring" - -[[bin]] -name = "term-cli" -path = "src/main.rs" - -[dependencies] -ratatui = "0.29" -crossterm = "0.28" -tokio = { version = "1.40", features = ["full"] } -reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -clap = { version = "4.5", features = ["derive"] } -anyhow = "1.0" -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } -chrono = { version = "0.4", features = ["serde"] } diff --git a/cli/src/app.rs b/cli/src/app.rs deleted file mode 100644 index a3482f387..000000000 --- a/cli/src/app.rs +++ /dev/null @@ -1,295 +0,0 @@ -use crate::rpc::RpcClient; -use chrono::{DateTime, Utc}; - -#[derive(Clone, Copy, PartialEq, Eq)] -pub enum Tab { - Leaderboard, - Evaluation, - Submission, - Network, -} - -impl Tab { - pub const ALL: [Tab; 4] = [ - Tab::Leaderboard, - Tab::Evaluation, - Tab::Submission, - Tab::Network, - ]; - - pub fn label(self) -> &'static str { - match self { - Tab::Leaderboard => "Leaderboard", - Tab::Evaluation => "Evaluation", - Tab::Submission => "Submission", - Tab::Network => "Network", - } - } - - pub fn index(self) -> usize { - match self { - Tab::Leaderboard => 0, - Tab::Evaluation => 1, - Tab::Submission => 2, - Tab::Network => 3, - } - } -} - -pub struct LeaderboardRow { - pub rank: u32, - pub miner_hotkey: String, - pub score: f64, - pub pass_rate: f64, - pub submissions: u32, - pub last_submission: String, -} - -pub struct EvalTaskRow { - pub task_id: String, - pub status: String, - pub score: f64, - pub duration_ms: u64, - pub error: Option, -} - -pub struct NetworkStatus { - pub epoch: u64, - pub phase: String, - pub block_height: u64, - pub validators: usize, - pub connected: bool, - pub total_submissions: u64, - pub active_miners: u64, -} - -impl Default for NetworkStatus { - fn default() -> Self { - Self { - epoch: 0, - phase: "unknown".to_string(), - block_height: 0, - validators: 0, - connected: false, - total_submissions: 0, - active_miners: 0, - } - } -} - -pub struct DecayStatus { - pub agent_hash: String, - pub score: f64, - pub achieved_epoch: u64, - pub epochs_stale: u64, - pub decay_active: bool, - pub current_burn_percent: f64, -} - -pub struct App { - pub tab: Tab, - pub rpc_url: String, - pub hotkey: Option, - pub challenge_id: Option, - pub leaderboard: Vec, - pub evaluation_progress: Vec, - pub network_status: NetworkStatus, - pub decay_status: Option, - pub submission_history: Option, - pub scroll_offset: usize, - pub last_refresh: Option>, - pub error_message: Option, - pub should_quit: bool, -} - -impl App { - pub fn new(rpc_url: String, hotkey: Option, challenge_id: Option) -> Self { - Self { - tab: Tab::Leaderboard, - rpc_url, - hotkey, - challenge_id, - leaderboard: Vec::new(), - evaluation_progress: Vec::new(), - network_status: NetworkStatus::default(), - decay_status: None, - submission_history: None, - scroll_offset: 0, - last_refresh: None, - error_message: None, - should_quit: false, - } - } - - pub fn set_tab_from_str(&mut self, s: &str) { - self.tab = match s.to_lowercase().as_str() { - "leaderboard" => Tab::Leaderboard, - "evaluation" => Tab::Evaluation, - "submission" => Tab::Submission, - "network" => Tab::Network, - _ => Tab::Leaderboard, - }; - self.scroll_offset = 0; - } - - pub fn next_tab(&mut self) { - let idx = self.tab.index(); - let next = (idx + 1) % Tab::ALL.len(); - self.tab = Tab::ALL[next]; - self.scroll_offset = 0; - } - - pub fn prev_tab(&mut self) { - let idx = self.tab.index(); - let prev = if idx == 0 { - Tab::ALL.len() - 1 - } else { - idx - 1 - }; - self.tab = Tab::ALL[prev]; - self.scroll_offset = 0; - } - - pub fn scroll_up(&mut self) { - self.scroll_offset = self.scroll_offset.saturating_sub(1); - } - - pub fn scroll_down(&mut self) { - self.scroll_offset = self.scroll_offset.saturating_add(1); - } - - pub async fn refresh(&mut self, rpc: &RpcClient) { - self.error_message = None; - - if let Err(e) = self.refresh_network(rpc).await { - self.error_message = Some(format!("Network: {e}")); - self.network_status.connected = false; - return; - } - self.network_status.connected = true; - - if self.challenge_id.is_none() { - match rpc.fetch_challenge_list().await { - Ok(challenges) if challenges.len() == 1 => { - self.challenge_id = Some(challenges[0].id.clone()); - } - Ok(_) => {} - Err(e) => { - self.error_message = Some(format!("Challenges: {e}")); - } - } - } - - if let Some(cid) = &self.challenge_id { - let cid = cid.clone(); - match rpc.fetch_leaderboard(&cid).await { - Ok(rows) => self.leaderboard = rows, - Err(e) => { - self.error_message = Some(format!("Leaderboard: {e}")); - } - } - - match rpc.fetch_stats(&cid).await { - Ok(stats) => { - self.network_status.total_submissions = stats - .get("total_submissions") - .and_then(|v| v.as_u64()) - .unwrap_or(0); - self.network_status.active_miners = stats - .get("active_miners") - .and_then(|v| v.as_u64()) - .unwrap_or(0); - } - Err(e) => { - tracing::debug!("Stats: {e}"); - } - } - - match rpc.fetch_decay_status(&cid).await { - Ok(decay) => { - if let Some(body) = decay.get("body") { - if !body.is_null() { - self.decay_status = Some(DecayStatus { - agent_hash: body - .get("agent_hash") - .and_then(|v| v.as_str()) - .unwrap_or_default() - .to_string(), - score: body.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0), - achieved_epoch: body - .get("achieved_epoch") - .and_then(|v| v.as_u64()) - .unwrap_or(0), - epochs_stale: body - .get("epochs_stale") - .and_then(|v| v.as_u64()) - .unwrap_or(0), - decay_active: body - .get("decay_active") - .and_then(|v| v.as_bool()) - .unwrap_or(false), - current_burn_percent: body - .get("current_burn_percent") - .and_then(|v| v.as_f64()) - .unwrap_or(0.0), - }); - } - } - } - Err(e) => { - tracing::debug!("Decay status: {e}"); - } - } - } - - if let Some(hotkey) = &self.hotkey { - let hotkey = hotkey.clone(); - match rpc.fetch_evaluation_progress(&hotkey).await { - Ok(tasks) => self.evaluation_progress = tasks, - Err(e) => { - tracing::debug!("Evaluation progress: {e}"); - } - } - - if let Some(cid) = &self.challenge_id { - match rpc.fetch_agent_journey(cid, &hotkey).await { - Ok(_journey) => { - tracing::debug!("Agent journey fetched"); - } - Err(e) => { - tracing::debug!("Agent journey: {e}"); - } - } - - match rpc.fetch_submission_history(cid, &hotkey).await { - Ok(history) => { - self.submission_history = Some(history); - } - Err(e) => { - tracing::debug!("Submission history: {e}"); - } - } - } - } - - self.last_refresh = Some(Utc::now()); - } - - async fn refresh_network(&mut self, rpc: &RpcClient) -> anyhow::Result<()> { - let _ = rpc.fetch_system_health().await?; - - let epoch_info = rpc.fetch_epoch_info().await?; - self.network_status.epoch = epoch_info.epoch; - self.network_status.phase = epoch_info.phase; - self.network_status.block_height = epoch_info.block_height; - - match rpc.fetch_validator_count().await { - Ok(count) => self.network_status.validators = count, - Err(e) => { - tracing::warn!("Failed to fetch validator count: {e}"); - } - } - - Ok(()) - } -} diff --git a/cli/src/main.rs b/cli/src/main.rs deleted file mode 100644 index cc419a42a..000000000 --- a/cli/src/main.rs +++ /dev/null @@ -1,105 +0,0 @@ -mod app; -mod rpc; -mod ui; - -use std::time::{Duration, Instant}; - -use anyhow::Result; -use clap::Parser; -use crossterm::event::{self, Event, KeyCode, KeyEventKind}; -use tracing_subscriber::EnvFilter; - -use crate::app::App; -use crate::rpc::RpcClient; - -#[derive(Parser)] -#[command(name = "term-cli", about = "Terminal Benchmark Challenge Monitor")] -struct Cli { - /// RPC endpoint URL - #[arg(long, default_value = "http://chain.platform.network:9944")] - rpc_url: String, - - /// Your miner hotkey for filtered views - #[arg(long)] - hotkey: Option, - - /// Challenge ID (auto-detected if single challenge) - #[arg(long)] - challenge_id: Option, - - /// Initial tab to display - #[arg(long, default_value = "leaderboard")] - tab: String, -} - -#[tokio::main] -async fn main() -> Result<()> { - let cli = Cli::parse(); - - tracing_subscriber::fmt() - .with_env_filter( - EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn")), - ) - .with_writer(std::io::stderr) - .init(); - - let mut terminal = ratatui::try_init()?; - - let result = run(&mut terminal, cli).await; - - ratatui::try_restore()?; - - result -} - -async fn run(terminal: &mut ratatui::DefaultTerminal, cli: Cli) -> Result<()> { - let mut app = App::new(cli.rpc_url.clone(), cli.hotkey, cli.challenge_id); - app.set_tab_from_str(&cli.tab); - - let rpc = RpcClient::new(&cli.rpc_url); - - app.refresh(&rpc).await; - - let tick_rate = Duration::from_secs(10); - let mut last_tick = Instant::now(); - - loop { - terminal.draw(|f| ui::draw(f, &app))?; - - let timeout = tick_rate - .checked_sub(last_tick.elapsed()) - .unwrap_or_default(); - - if event::poll(timeout)? { - if let Event::Key(key) = event::read()? { - if key.kind == KeyEventKind::Press { - match key.code { - KeyCode::Char('q') => { - app.should_quit = true; - } - KeyCode::Tab => app.next_tab(), - KeyCode::BackTab => app.prev_tab(), - KeyCode::Up => app.scroll_up(), - KeyCode::Down => app.scroll_down(), - KeyCode::Char('r') => { - app.refresh(&rpc).await; - last_tick = Instant::now(); - } - _ => {} - } - } - } - } - - if app.should_quit { - break; - } - - if last_tick.elapsed() >= tick_rate { - app.refresh(&rpc).await; - last_tick = Instant::now(); - } - } - - Ok(()) -} diff --git a/cli/src/rpc.rs b/cli/src/rpc.rs deleted file mode 100644 index 70666db56..000000000 --- a/cli/src/rpc.rs +++ /dev/null @@ -1,264 +0,0 @@ -use std::sync::atomic::{AtomicU64, Ordering}; - -use anyhow::{anyhow, Context}; -use serde::{Deserialize, Serialize}; - -use crate::app::{EvalTaskRow, LeaderboardRow}; - -pub struct RpcClient { - url: String, - client: reqwest::Client, - request_id: AtomicU64, -} - -#[derive(Serialize)] -struct JsonRpcRequest<'a> { - jsonrpc: &'a str, - id: u64, - method: &'a str, - params: serde_json::Value, -} - -#[derive(Deserialize)] -struct JsonRpcResponse { - result: Option, - error: Option, - #[serde(rename = "id")] - _id: Option, -} - -#[derive(Deserialize)] -struct JsonRpcError { - code: i64, - message: String, -} - -pub struct EpochInfo { - pub epoch: u64, - pub phase: String, - pub block_height: u64, -} - -#[derive(Deserialize)] -struct EpochInfoRaw { - #[serde(default)] - epoch: u64, - #[serde(default)] - phase: String, - #[serde(default)] - block_height: u64, -} - -pub struct ChallengeInfo { - pub id: String, -} - -#[derive(Deserialize)] -struct ChallengeInfoRaw { - #[serde(default)] - id: String, -} - -#[derive(Deserialize)] -struct LeaderboardRowRaw { - #[serde(default)] - rank: u32, - #[serde(default)] - miner_hotkey: String, - #[serde(default)] - score: f64, - #[serde(default)] - pass_rate: f64, - #[serde(default)] - submissions: u32, - #[serde(default)] - last_submission: String, -} - -#[derive(Deserialize)] -struct EvalTaskRowRaw { - #[serde(default)] - task_id: String, - #[serde(default)] - status: String, - #[serde(default)] - score: f64, - #[serde(default)] - duration_ms: u64, - #[serde(default)] - error: Option, -} - -impl RpcClient { - pub fn new(url: &str) -> Self { - Self { - url: url.to_string(), - client: reqwest::Client::new(), - request_id: AtomicU64::new(1), - } - } - - pub async fn call( - &self, - method: &str, - params: serde_json::Value, - ) -> anyhow::Result { - let id = self.request_id.fetch_add(1, Ordering::Relaxed); - let request = JsonRpcRequest { - jsonrpc: "2.0", - id, - method, - params, - }; - - let response = self - .client - .post(&self.url) - .json(&request) - .send() - .await - .context("Failed to send RPC request")?; - - let status = response.status(); - if !status.is_success() { - return Err(anyhow!("RPC HTTP error: {status}")); - } - - let rpc_response: JsonRpcResponse = response - .json() - .await - .context("Failed to parse RPC response")?; - - if let Some(err) = rpc_response.error { - return Err(anyhow!("RPC error {}: {}", err.code, err.message)); - } - - rpc_response - .result - .ok_or_else(|| anyhow!("RPC response missing result")) - } - - pub async fn fetch_leaderboard( - &self, - challenge_id: &str, - ) -> anyhow::Result> { - let params = serde_json::json!({ - "challenge_id": challenge_id, - "path": "/leaderboard" - }); - let result = self.call("challenge_call", params).await?; - let raw: Vec = - serde_json::from_value(result).context("Failed to parse leaderboard data")?; - Ok(raw - .into_iter() - .map(|r| LeaderboardRow { - rank: r.rank, - miner_hotkey: r.miner_hotkey, - score: r.score, - pass_rate: r.pass_rate, - submissions: r.submissions, - last_submission: r.last_submission, - }) - .collect()) - } - - pub async fn fetch_epoch_info(&self) -> anyhow::Result { - let result = self.call("epoch_current", serde_json::json!({})).await?; - let raw: EpochInfoRaw = - serde_json::from_value(result).context("Failed to parse epoch info")?; - Ok(EpochInfo { - epoch: raw.epoch, - phase: raw.phase, - block_height: raw.block_height, - }) - } - - pub async fn fetch_system_health(&self) -> anyhow::Result { - self.call("system_health", serde_json::json!({})).await - } - - pub async fn fetch_validator_count(&self) -> anyhow::Result { - let result = self.call("validator_count", serde_json::json!({})).await?; - let count = result.as_u64().unwrap_or_default() as usize; - Ok(count) - } - - pub async fn fetch_evaluation_progress( - &self, - submission_id: &str, - ) -> anyhow::Result> { - let params = serde_json::json!({ - "submission_id": submission_id - }); - let result = self.call("evaluation_getProgress", params).await?; - let raw: Vec = - serde_json::from_value(result).context("Failed to parse evaluation progress")?; - Ok(raw - .into_iter() - .map(|r| EvalTaskRow { - task_id: r.task_id, - status: r.status, - score: r.score, - duration_ms: r.duration_ms, - error: r.error, - }) - .collect()) - } - - pub async fn fetch_challenge_list(&self) -> anyhow::Result> { - let result = self.call("challenge_list", serde_json::json!({})).await?; - let raw: Vec = - serde_json::from_value(result).context("Failed to parse challenge list")?; - Ok(raw - .into_iter() - .map(|r| ChallengeInfo { id: r.id }) - .collect()) - } - - pub async fn fetch_agent_journey( - &self, - challenge_id: &str, - hotkey: &str, - ) -> anyhow::Result { - let params = serde_json::json!({ - "challengeId": challenge_id, - "method": "GET", - "path": format!("/agent/{}/journey", hotkey) - }); - self.call("challenge_call", params).await - } - - pub async fn fetch_submission_history( - &self, - challenge_id: &str, - hotkey: &str, - ) -> anyhow::Result { - let params = serde_json::json!({ - "challengeId": challenge_id, - "method": "GET", - "path": format!("/agent/{}/logs", hotkey) - }); - self.call("challenge_call", params).await - } - - pub async fn fetch_stats(&self, challenge_id: &str) -> anyhow::Result { - let params = serde_json::json!({ - "challengeId": challenge_id, - "method": "GET", - "path": "/stats" - }); - self.call("challenge_call", params).await - } - - pub async fn fetch_decay_status( - &self, - challenge_id: &str, - ) -> anyhow::Result { - let params = serde_json::json!({ - "challengeId": challenge_id, - "method": "GET", - "path": "/decay" - }); - self.call("challenge_call", params).await - } -} diff --git a/cli/src/ui.rs b/cli/src/ui.rs deleted file mode 100644 index 2e1cf0654..000000000 --- a/cli/src/ui.rs +++ /dev/null @@ -1,423 +0,0 @@ -use ratatui::{ - layout::{Constraint, Direction, Layout, Rect}, - style::{Color, Modifier, Style, Stylize}, - text::{Line, Span}, - widgets::{Block, Borders, Cell, Paragraph, Row, Table, Tabs}, - Frame, -}; - -use crate::app::{App, Tab}; - -pub fn draw(frame: &mut Frame, app: &App) { - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Length(3), - Constraint::Min(0), - Constraint::Length(3), - ]) - .split(frame.area()); - - draw_tabs(frame, chunks[0], app); - - match app.tab { - Tab::Leaderboard => draw_leaderboard(frame, chunks[1], app), - Tab::Evaluation => draw_evaluation(frame, chunks[1], app), - Tab::Submission => draw_submission(frame, chunks[1], app), - Tab::Network => draw_network(frame, chunks[1], app), - } - - draw_status_bar(frame, chunks[2], app); -} - -fn draw_tabs(frame: &mut Frame, area: Rect, app: &App) { - let titles: Vec<&str> = Tab::ALL.iter().map(|t| t.label()).collect(); - let tabs = Tabs::new(titles) - .block(Block::default().borders(Borders::ALL).title("Term CLI")) - .select(app.tab.index()) - .style(Style::default().fg(Color::Gray)) - .highlight_style( - Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::BOLD), - ); - frame.render_widget(tabs, area); -} - -fn draw_leaderboard(frame: &mut Frame, area: Rect, app: &App) { - let header = Row::new(vec![ - Cell::from("Rank"), - Cell::from("Miner"), - Cell::from("Score"), - Cell::from("Pass Rate"), - Cell::from("Submissions"), - Cell::from("Last Submission"), - ]) - .style( - Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::BOLD), - ); - - let visible_rows = visible_row_count(area); - let rows: Vec = app - .leaderboard - .iter() - .skip(app.scroll_offset) - .take(visible_rows) - .map(|entry| { - let hotkey_display = truncate_hotkey(&entry.miner_hotkey, 8); - Row::new(vec![ - Cell::from(entry.rank.to_string()), - Cell::from(hotkey_display), - Cell::from(format!("{:.4}", entry.score)), - Cell::from(format!("{:.1}%", entry.pass_rate * 100.0)), - Cell::from(entry.submissions.to_string()), - Cell::from(entry.last_submission.clone()), - ]) - }) - .collect(); - - let widths = [ - Constraint::Length(6), - Constraint::Length(14), - Constraint::Length(10), - Constraint::Length(12), - Constraint::Length(12), - Constraint::Min(20), - ]; - - let table = Table::new(rows, widths) - .header(header) - .block(Block::default().borders(Borders::ALL).title("Leaderboard")) - .row_highlight_style(Style::default().add_modifier(Modifier::BOLD)); - - frame.render_widget(table, area); - - if app.leaderboard.is_empty() { - draw_empty_message(frame, area, "No leaderboard data available"); - } -} - -fn draw_evaluation(frame: &mut Frame, area: Rect, app: &App) { - let inner_chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Length(3), Constraint::Min(0)]) - .split(area); - - let total = app.evaluation_progress.len(); - let completed = app - .evaluation_progress - .iter() - .filter(|t| t.status == "completed") - .count(); - let progress_text = if total > 0 { - format!( - "Progress: {completed}/{total} ({:.0}%)", - (completed as f64 / total as f64) * 100.0 - ) - } else { - "No evaluation tasks".to_string() - }; - let progress_bar = Paragraph::new(progress_text).block( - Block::default() - .borders(Borders::ALL) - .title("Overall Progress"), - ); - frame.render_widget(progress_bar, inner_chunks[0]); - - let header = Row::new(vec![ - Cell::from("Task ID"), - Cell::from("Status"), - Cell::from("Score"), - Cell::from("Duration (ms)"), - Cell::from("Error"), - ]) - .style( - Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::BOLD), - ); - - let visible_rows = visible_row_count(inner_chunks[1]); - let rows: Vec = app - .evaluation_progress - .iter() - .skip(app.scroll_offset) - .take(visible_rows) - .map(|task| { - let status_style = match task.status.as_str() { - "completed" => Style::default().fg(Color::Green), - "failed" => Style::default().fg(Color::Red), - "running" => Style::default().fg(Color::Cyan), - _ => Style::default().fg(Color::Gray), - }; - Row::new(vec![ - Cell::from(task.task_id.clone()), - Cell::from(Span::styled(task.status.clone(), status_style)), - Cell::from(format!("{:.4}", task.score)), - Cell::from(task.duration_ms.to_string()), - Cell::from(task.error.clone().unwrap_or_default()), - ]) - }) - .collect(); - - let widths = [ - Constraint::Length(20), - Constraint::Length(12), - Constraint::Length(10), - Constraint::Length(14), - Constraint::Min(20), - ]; - - let table = Table::new(rows, widths).header(header).block( - Block::default() - .borders(Borders::ALL) - .title("Evaluation Tasks"), - ); - - frame.render_widget(table, inner_chunks[1]); - - if app.evaluation_progress.is_empty() { - draw_empty_message(frame, inner_chunks[1], "No evaluation data available"); - } -} - -fn draw_submission(frame: &mut Frame, area: Rect, app: &App) { - let block = Block::default().borders(Borders::ALL).title("Submissions"); - - match &app.hotkey { - Some(hotkey) => { - let filtered: Vec<&crate::app::LeaderboardRow> = app - .leaderboard - .iter() - .filter(|r| r.miner_hotkey == *hotkey) - .collect(); - - if filtered.is_empty() { - let text = Paragraph::new(format!( - "No submissions found for hotkey: {}", - truncate_hotkey(hotkey, 16) - )) - .block(block); - frame.render_widget(text, area); - return; - } - - let mut lines = Vec::new(); - for entry in &filtered { - lines.push(Line::from(vec![ - Span::styled("Rank: ", Style::default().fg(Color::Yellow)), - Span::raw(entry.rank.to_string()), - ])); - lines.push(Line::from(vec![ - Span::styled("Hotkey: ", Style::default().fg(Color::Yellow)), - Span::raw(entry.miner_hotkey.clone()), - ])); - lines.push(Line::from(vec![ - Span::styled("Score: ", Style::default().fg(Color::Yellow)), - Span::raw(format!("{:.4}", entry.score)), - ])); - lines.push(Line::from(vec![ - Span::styled("Pass Rate: ", Style::default().fg(Color::Yellow)), - Span::raw(format!("{:.1}%", entry.pass_rate * 100.0)), - ])); - lines.push(Line::from(vec![ - Span::styled("Submissions: ", Style::default().fg(Color::Yellow)), - Span::raw(entry.submissions.to_string()), - ])); - lines.push(Line::from(vec![ - Span::styled("Last Submission: ", Style::default().fg(Color::Yellow)), - Span::raw(entry.last_submission.clone()), - ])); - lines.push(Line::from("")); - } - - let paragraph = Paragraph::new(lines).block(block); - frame.render_widget(paragraph, area); - } - None => { - let text = Paragraph::new("No hotkey specified. Use --hotkey to filter submissions.") - .block(block); - frame.render_widget(text, area); - } - } -} - -fn draw_network(frame: &mut Frame, area: Rect, app: &App) { - let ns = &app.network_status; - let connected_style = if ns.connected { - Style::default().fg(Color::Green) - } else { - Style::default().fg(Color::Red) - }; - let connected_text = if ns.connected { - "Connected" - } else { - "Disconnected" - }; - - let mut lines = vec![ - Line::from(vec![ - Span::styled("Status: ", Style::default().fg(Color::Yellow).bold()), - Span::styled(connected_text, connected_style), - ]), - Line::from(""), - Line::from(vec![ - Span::styled("Epoch: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(ns.epoch.to_string()), - ]), - Line::from(vec![ - Span::styled("Phase: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(ns.phase.clone()), - ]), - Line::from(vec![ - Span::styled("Block Height:", Style::default().fg(Color::Yellow).bold()), - Span::raw(format!(" {}", ns.block_height)), - ]), - Line::from(vec![ - Span::styled("Validators: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(ns.validators.to_string()), - ]), - Line::from(vec![ - Span::styled("Submissions: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(ns.total_submissions.to_string()), - ]), - Line::from(vec![ - Span::styled("Miners: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(ns.active_miners.to_string()), - ]), - Line::from(""), - Line::from(vec![ - Span::styled("RPC URL: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(app.rpc_url.clone()), - ]), - ]; - - if let Some(cid) = &app.challenge_id { - lines.push(Line::from(vec![ - Span::styled("Challenge: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(cid.clone()), - ])); - } - - if let Some(decay) = &app.decay_status { - lines.push(Line::from("")); - lines.push(Line::from(Span::styled( - "โ”€โ”€ Top Agent Decay โ”€โ”€", - Style::default().fg(Color::Cyan).bold(), - ))); - lines.push(Line::from(vec![ - Span::styled("Agent: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(truncate_hotkey(&decay.agent_hash, 16)), - ])); - lines.push(Line::from(vec![ - Span::styled("Score: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(format!("{:.4}", decay.score)), - ])); - lines.push(Line::from(vec![ - Span::styled("Achieved: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(format!("epoch {}", decay.achieved_epoch)), - ])); - lines.push(Line::from(vec![ - Span::styled("Stale: ", Style::default().fg(Color::Yellow).bold()), - Span::raw(format!("{} epochs", decay.epochs_stale)), - ])); - let decay_style = if decay.decay_active { - Style::default().fg(Color::Red) - } else { - Style::default().fg(Color::Green) - }; - let decay_text = if decay.decay_active { - format!("Active ({:.1}% burned)", decay.current_burn_percent) - } else { - "Inactive".to_string() - }; - lines.push(Line::from(vec![ - Span::styled("Decay: ", Style::default().fg(Color::Yellow).bold()), - Span::styled(decay_text, decay_style), - ])); - } - - let paragraph = Paragraph::new(lines).block( - Block::default() - .borders(Borders::ALL) - .title("Network Status"), - ); - frame.render_widget(paragraph, area); -} - -fn draw_status_bar(frame: &mut Frame, area: Rect, app: &App) { - let ns = &app.network_status; - let refresh_str = app - .last_refresh - .map(|t| t.format("%H:%M:%S UTC").to_string()) - .unwrap_or_else(|| "never".to_string()); - - let mut spans = vec![ - Span::styled(" Epoch: ", Style::default().fg(Color::Yellow)), - Span::raw(ns.epoch.to_string()), - Span::raw(" | "), - Span::styled("Phase: ", Style::default().fg(Color::Yellow)), - Span::raw(ns.phase.clone()), - Span::raw(" | "), - Span::styled("Block: ", Style::default().fg(Color::Yellow)), - Span::raw(ns.block_height.to_string()), - Span::raw(" | "), - Span::styled("Validators: ", Style::default().fg(Color::Yellow)), - Span::raw(ns.validators.to_string()), - Span::raw(" | "), - Span::styled("Refresh: ", Style::default().fg(Color::Yellow)), - Span::raw(refresh_str), - ]; - - if let Some(err) = &app.error_message { - spans.push(Span::raw(" | ")); - spans.push(Span::styled( - format!("Error: {err}"), - Style::default().fg(Color::Red), - )); - } - - let status = Paragraph::new(Line::from(spans)) - .block(Block::default().borders(Borders::ALL).title("Status")); - frame.render_widget(status, area); -} - -fn truncate_hotkey(hotkey: &str, max_len: usize) -> String { - if hotkey.len() > max_len { - format!("{}...", &hotkey[..max_len]) - } else { - hotkey.to_string() - } -} - -fn visible_row_count(area: Rect) -> usize { - area.height.saturating_sub(4) as usize -} - -fn draw_empty_message(frame: &mut Frame, area: Rect, message: &str) { - let inner = centered_rect(60, 20, area); - let text = Paragraph::new(message).style(Style::default().fg(Color::DarkGray)); - frame.render_widget(text, inner); -} - -fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { - let popup_layout = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Percentage((100 - percent_y) / 2), - Constraint::Percentage(percent_y), - Constraint::Percentage((100 - percent_y) / 2), - ]) - .split(r); - - Layout::default() - .direction(Direction::Horizontal) - .constraints([ - Constraint::Percentage((100 - percent_x) / 2), - Constraint::Percentage(percent_x), - Constraint::Percentage((100 - percent_x) / 2), - ]) - .split(popup_layout[1])[1] -} diff --git a/crates/bittensor-integration/Cargo.toml b/crates/bittensor-integration/Cargo.toml new file mode 100644 index 000000000..7a8dd6b5d --- /dev/null +++ b/crates/bittensor-integration/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "platform-bittensor" +version.workspace = true +edition.workspace = true +description = "Bittensor integration for Platform Chain validators" + +[features] +default = [] +# Enable test utilities (mock types) for integration tests in other crates +test-utils = [] + +[dependencies] +platform-core = { path = "../core" } +platform-challenge-sdk = { path = "../challenge-sdk" } +bittensor-rs = { workspace = true } + +# Async +tokio = { workspace = true } +async-trait = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } + +# Crypto (for AccountId32) +sp-core = "38.1.0" + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +hex = { workspace = true } +rand = { workspace = true } +parking_lot = { workspace = true } +chrono = { workspace = true } +futures = { workspace = true } +reqwest = { version = "0.11", features = ["json"] } + +# For mock module (hash-based hotkey generation) +sha2 = { workspace = true } diff --git a/crates/bittensor-integration/examples/check_metagraph.rs b/crates/bittensor-integration/examples/check_metagraph.rs new file mode 100644 index 000000000..2a583c623 --- /dev/null +++ b/crates/bittensor-integration/examples/check_metagraph.rs @@ -0,0 +1,72 @@ +use bittensor_rs::metagraph::sync_metagraph; +use bittensor_rs::BittensorClient; + +#[tokio::main] +async fn main() -> Result<(), Box> { + println!("Connecting to Bittensor finney..."); + + let client = BittensorClient::new("wss://entrypoint-finney.opentensor.ai:443").await?; + + println!("Querying subnet 100 metagraph...\n"); + + let metagraph = sync_metagraph(&client, 100).await?; + + println!("Total neurons: {}", metagraph.n); + println!("\nTop 30 by effective stake (alpha + root):"); + println!("{:-<90}", ""); + + // Collect stakes + let mut stakes: Vec<(u16, String, u128, u128, u128)> = metagraph + .neurons + .iter() + .map(|(uid, neuron)| { + let alpha = neuron.stake; + let root = neuron.root_stake; + let total = alpha.saturating_add(root); + let hotkey_str = format!("{}", neuron.hotkey); + (*uid as u16, hotkey_str, alpha, root, total) + }) + .filter(|(_, _, _, _, total)| *total > 0) + .collect(); + + // Sort by total stake descending + stakes.sort_by(|a, b| b.4.cmp(&a.4)); + + println!( + "{:<6} {:<50} {:>12} {:>12} {:>12}", + "UID", "Hotkey", "Alpha", "Root", "Total TAO" + ); + println!("{:-<90}", ""); + + for (uid, hotkey, alpha, root, total) in stakes.iter().take(30) { + let alpha_tao = *alpha as f64 / 1_000_000_000.0; + let root_tao = *root as f64 / 1_000_000_000.0; + let total_tao = *total as f64 / 1_000_000_000.0; + println!( + "{:<6} {:<50} {:>12.2} {:>12.2} {:>12.2}", + uid, hotkey, alpha_tao, root_tao, total_tao + ); + } + + let gte_1000 = stakes + .iter() + .filter(|(_, _, _, _, t)| *t as f64 / 1e9 >= 1000.0) + .count(); + let gte_100 = stakes + .iter() + .filter(|(_, _, _, _, t)| *t as f64 / 1e9 >= 100.0) + .count(); + let gte_10 = stakes + .iter() + .filter(|(_, _, _, _, t)| *t as f64 / 1e9 >= 10.0) + .count(); + let gt_0 = stakes.len(); + + println!("\n{:-<90}", ""); + println!("Validators with >= 1000 TAO: {}", gte_1000); + println!("Validators with >= 100 TAO: {}", gte_100); + println!("Validators with >= 10 TAO: {}", gte_10); + println!("Validators with > 0 TAO: {}", gt_0); + + Ok(()) +} diff --git a/crates/bittensor-integration/src/block_sync.rs b/crates/bittensor-integration/src/block_sync.rs new file mode 100644 index 000000000..b0b919ad1 --- /dev/null +++ b/crates/bittensor-integration/src/block_sync.rs @@ -0,0 +1,602 @@ +//! Block synchronization with Bittensor +//! +//! Syncs platform blocks with Bittensor finalized blocks +//! to ensure epochs are aligned with on-chain state. + +pub use bittensor_rs::blocks::{ + BlockEvent, BlockListener, BlockListenerConfig, EpochInfo, EpochPhase, EpochTransition, +}; +use bittensor_rs::chain::BittensorClient; +use std::sync::Arc; +use tokio::sync::{broadcast, mpsc, RwLock}; +use tracing::{info, warn}; + +/// Events emitted by the block sync +#[derive(Debug, Clone)] +pub enum BlockSyncEvent { + /// New block from Bittensor + NewBlock { + block_number: u64, + epoch_info: EpochInfo, + }, + /// Epoch transition on Bittensor + EpochTransition { + old_epoch: u64, + new_epoch: u64, + block: u64, + }, + /// Phase changed (evaluation -> commit -> reveal) + PhaseChange { + block_number: u64, + old_phase: EpochPhase, + new_phase: EpochPhase, + epoch: u64, + }, + /// Time to commit weights + CommitWindowOpen { epoch: u64, block: u64 }, + /// Time to reveal weights + RevealWindowOpen { epoch: u64, block: u64 }, + /// Connection lost (will retry) + Disconnected(String), + /// Reconnected + Reconnected, +} + +/// Block synchronizer configuration +#[derive(Debug, Clone)] +pub struct BlockSyncConfig { + /// Subnet UID + pub netuid: u16, + /// Event channel capacity + pub channel_capacity: usize, +} + +impl Default for BlockSyncConfig { + fn default() -> Self { + Self { + netuid: 1, + channel_capacity: 100, + } + } +} + +/// Synchronizes platform with Bittensor blocks +pub struct BlockSync { + config: BlockSyncConfig, + listener: Option, + client: Option>, + running: Arc>, + event_tx: mpsc::Sender, + event_rx: Option>, + current_block: Arc>, + current_epoch: Arc>, + current_phase: Arc>, + tempo: Arc>, +} + +impl BlockSync { + /// Create a new block sync + pub fn new(config: BlockSyncConfig) -> Self { + let (event_tx, event_rx) = mpsc::channel(config.channel_capacity); + + Self { + config, + listener: None, + client: None, + running: Arc::new(RwLock::new(false)), + event_tx, + event_rx: Some(event_rx), + current_block: Arc::new(RwLock::new(0)), + current_epoch: Arc::new(RwLock::new(0)), + current_phase: Arc::new(RwLock::new(EpochPhase::Evaluation)), + tempo: Arc::new(RwLock::new(360)), // default Bittensor tempo + } + } + + /// Take the event receiver (can only be called once) + pub fn take_event_receiver(&mut self) -> Option> { + self.event_rx.take() + } + + /// Connect to Bittensor and start syncing blocks + pub async fn connect(&mut self, client: Arc) -> anyhow::Result<()> { + let listener_config = BlockListenerConfig { + netuid: self.config.netuid, + channel_capacity: self.config.channel_capacity, + auto_reconnect: true, + reconnect_delay_ms: 5000, + }; + + let listener = BlockListener::new(listener_config); + listener.init(&client).await?; + + // Get initial block info + let epoch_info = listener.current_epoch_info(&client).await?; + *self.current_block.write().await = epoch_info.current_block; + *self.current_epoch.write().await = epoch_info.epoch_number; + *self.current_phase.write().await = epoch_info.phase; + *self.tempo.write().await = epoch_info.tempo; + + let secs_remaining = epoch_info.blocks_remaining * 12; + let mins = secs_remaining / 60; + let secs = secs_remaining % 60; + info!( + "BlockSync connected: block={}, epoch={}, phase={}, tempo={}", + epoch_info.current_block, epoch_info.epoch_number, epoch_info.phase, epoch_info.tempo + ); + info!( + "Next epoch in {} blocks (~{}m{}s)", + epoch_info.blocks_remaining, mins, secs + ); + + self.listener = Some(listener); + self.client = Some(client); + + Ok(()) + } + + /// Get the Bittensor tempo (blocks per epoch) + pub async fn tempo(&self) -> u64 { + *self.tempo.read().await + } + + /// Start the block sync loop + pub async fn start(&self) -> anyhow::Result<()> { + let listener = self + .listener + .as_ref() + .ok_or_else(|| anyhow::anyhow!("Not connected - call connect() first"))?; + + let client = self + .client + .as_ref() + .ok_or_else(|| anyhow::anyhow!("Not connected"))?; + + // Check if already running + { + let mut running = self.running.write().await; + if *running { + return Ok(()); + } + *running = true; + } + + // Subscribe to block events + let mut block_rx = listener.subscribe(); + let event_tx = self.event_tx.clone(); + let running = self.running.clone(); + let current_block = self.current_block.clone(); + let current_epoch = self.current_epoch.clone(); + let current_phase = self.current_phase.clone(); + + // Start the listener + listener.start(client.clone()).await?; + + // Process events in background + tokio::spawn(async move { + let mut was_disconnected = false; + + loop { + if !*running.read().await { + break; + } + + match block_rx.recv().await { + Ok(event) => { + let should_break = BlockSync::handle_block_event( + event, + &event_tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + if should_break { + break; + } + } + Err(broadcast::error::RecvError::Lagged(n)) => { + warn!("Block sync lagged by {} events", n); + } + Err(broadcast::error::RecvError::Closed) => { + info!("Block event channel closed"); + break; + } + } + } + }); + + Ok(()) + } + + async fn handle_block_event( + event: BlockEvent, + event_tx: &mpsc::Sender, + current_block: &Arc>, + current_epoch: &Arc>, + current_phase: &Arc>, + was_disconnected: &mut bool, + ) -> bool { + match event { + BlockEvent::NewBlock { + block_number, + epoch_info, + } => { + *current_block.write().await = block_number; + *current_epoch.write().await = epoch_info.epoch_number; + *current_phase.write().await = epoch_info.phase; + + if let Err(e) = event_tx + .send(BlockSyncEvent::NewBlock { + block_number, + epoch_info, + }) + .await + { + warn!("Failed to send NewBlock event: {}", e); + } + + if *was_disconnected { + *was_disconnected = false; + if let Err(e) = event_tx.send(BlockSyncEvent::Reconnected).await { + warn!("Failed to send Reconnected event: {}", e); + } + } + } + BlockEvent::EpochTransition(EpochTransition::NewEpoch { + old_epoch, + new_epoch, + block, + }) => { + info!( + "Bittensor epoch transition: {} -> {} at block {}", + old_epoch, new_epoch, block + ); + if let Err(e) = event_tx + .send(BlockSyncEvent::EpochTransition { + old_epoch, + new_epoch, + block, + }) + .await + { + warn!("Failed to send EpochTransition event: {}", e); + } + } + BlockEvent::PhaseChange { + block_number, + old_phase, + new_phase, + epoch, + } => { + info!( + "Bittensor phase change: {} -> {} at block {} (epoch {})", + old_phase, new_phase, block_number, epoch + ); + + if let Err(e) = event_tx + .send(BlockSyncEvent::PhaseChange { + block_number, + old_phase, + new_phase, + epoch, + }) + .await + { + warn!("Failed to send PhaseChange event: {}", e); + } + + match new_phase { + EpochPhase::CommitWindow => { + if let Err(e) = event_tx + .send(BlockSyncEvent::CommitWindowOpen { + epoch, + block: block_number, + }) + .await + { + warn!("Failed to send CommitWindowOpen event: {}", e); + } + } + EpochPhase::RevealWindow => { + if let Err(e) = event_tx + .send(BlockSyncEvent::RevealWindowOpen { + epoch, + block: block_number, + }) + .await + { + warn!("Failed to send RevealWindowOpen event: {}", e); + } + } + _ => {} + } + } + BlockEvent::ConnectionError(e) => { + warn!("Bittensor connection error: {}", e); + *was_disconnected = true; + if let Err(send_err) = event_tx.send(BlockSyncEvent::Disconnected(e)).await { + warn!("Failed to send Disconnected event: {}", send_err); + } + } + BlockEvent::Stopped => { + info!("Block listener stopped"); + return true; + } + } + + false + } + + /// Stop the block sync + pub async fn stop(&self) { + *self.running.write().await = false; + if let Some(ref listener) = self.listener { + listener.stop().await; + } + } + + /// Get current Bittensor block number + pub async fn current_block(&self) -> u64 { + *self.current_block.read().await + } + + /// Get current Bittensor epoch number + pub async fn current_epoch(&self) -> u64 { + *self.current_epoch.read().await + } + + /// Get current Bittensor epoch phase + pub async fn current_phase(&self) -> EpochPhase { + *self.current_phase.read().await + } + + /// Check if connected + pub fn is_connected(&self) -> bool { + self.client.is_some() + } + + /// Check if running + pub async fn is_running(&self) -> bool { + *self.running.read().await + } +} + +// Re-export types from bittensor_rs for convenience (already imported at top) + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_block_sync_config_default() { + let config = BlockSyncConfig::default(); + assert_eq!(config.netuid, 1); + assert_eq!(config.channel_capacity, 100); + } + + #[tokio::test] + async fn test_block_sync_initial_state() { + let mut sync = BlockSync::new(BlockSyncConfig { + netuid: 42, + channel_capacity: 8, + }); + + assert!(!sync.is_connected()); + assert!(!sync.is_running().await); + assert_eq!(sync.current_block().await, 0); + assert_eq!(sync.current_epoch().await, 0); + assert!(matches!(sync.current_phase().await, EpochPhase::Evaluation)); + + let first_receiver = sync.take_event_receiver(); + assert!(first_receiver.is_some()); + assert!(sync.take_event_receiver().is_none()); + } + + fn sample_epoch_info(block: u64, epoch: u64, phase: EpochPhase) -> EpochInfo { + EpochInfo { + current_block: block, + tempo: 360, + epoch_start_block: epoch * 360, + next_epoch_start_block: epoch * 360 + 360, + blocks_remaining: 10, + epoch_number: epoch, + phase, + commit_reveal_enabled: true, + reveal_period_epochs: 1, + } + } + + #[tokio::test] + async fn test_handle_block_event_new_block_emits_reconnect() { + let (tx, mut rx) = mpsc::channel(4); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = true; + + let epoch_info = sample_epoch_info(123, 9, EpochPhase::CommitWindow); + + let should_break = BlockSync::handle_block_event( + BlockEvent::NewBlock { + block_number: 123, + epoch_info: epoch_info.clone(), + }, + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + assert!(!should_break); + assert_eq!(*current_block.read().await, 123); + assert_eq!(*current_epoch.read().await, 9); + assert!(matches!( + *current_phase.read().await, + EpochPhase::CommitWindow + )); + + let first = rx.recv().await.unwrap(); + assert!(matches!(first, BlockSyncEvent::NewBlock { .. })); + let second = rx.recv().await.unwrap(); + assert!(matches!(second, BlockSyncEvent::Reconnected)); + assert!(!was_disconnected); + } + + #[tokio::test] + async fn test_handle_block_event_phase_change_emits_windows() { + let (tx, mut rx) = mpsc::channel(4); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = false; + + let should_break = BlockSync::handle_block_event( + BlockEvent::PhaseChange { + block_number: 200, + old_phase: EpochPhase::Evaluation, + new_phase: EpochPhase::CommitWindow, + epoch: 7, + }, + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + assert!(!should_break); + assert_eq!(*current_epoch.read().await, 0); // unchanged for phase events + assert!(matches!( + *current_phase.read().await, + EpochPhase::Evaluation + )); + + let phase_event = rx.recv().await.unwrap(); + assert!(matches!(phase_event, BlockSyncEvent::PhaseChange { .. })); + let window_event = rx.recv().await.unwrap(); + assert!(matches!( + window_event, + BlockSyncEvent::CommitWindowOpen { .. } + )); + } + + #[tokio::test] + async fn test_handle_block_event_stopped_breaks_loop() { + let (tx, mut rx) = mpsc::channel(1); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = false; + + let should_break = BlockSync::handle_block_event( + BlockEvent::Stopped, + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + assert!(should_break); + assert!(rx.try_recv().is_err()); + } + + #[tokio::test] + async fn test_handle_block_event_epoch_transition_emits_event() { + let (tx, mut rx) = mpsc::channel(1); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = false; + + BlockSync::handle_block_event( + BlockEvent::EpochTransition(EpochTransition::NewEpoch { + old_epoch: 5, + new_epoch: 6, + block: 1234, + }), + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + let evt = rx.recv().await.unwrap(); + assert!(matches!( + evt, + BlockSyncEvent::EpochTransition { + old_epoch: 5, + new_epoch: 6, + block: 1234 + } + )); + } + + #[tokio::test] + async fn test_handle_block_event_reveal_window_emits_open_event() { + let (tx, mut rx) = mpsc::channel(3); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = false; + + BlockSync::handle_block_event( + BlockEvent::PhaseChange { + block_number: 500, + old_phase: EpochPhase::CommitWindow, + new_phase: EpochPhase::RevealWindow, + epoch: 11, + }, + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + let phase_event = rx.recv().await.unwrap(); + assert!(matches!(phase_event, BlockSyncEvent::PhaseChange { .. })); + let reveal_event = rx.recv().await.unwrap(); + assert!(matches!( + reveal_event, + BlockSyncEvent::RevealWindowOpen { + epoch: 11, + block: 500 + } + )); + } + + #[tokio::test] + async fn test_handle_block_event_connection_error_sets_flag() { + let (tx, mut rx) = mpsc::channel(1); + let current_block = Arc::new(RwLock::new(0)); + let current_epoch = Arc::new(RwLock::new(0)); + let current_phase = Arc::new(RwLock::new(EpochPhase::Evaluation)); + let mut was_disconnected = false; + + let should_break = BlockSync::handle_block_event( + BlockEvent::ConnectionError("network wobble".into()), + &tx, + ¤t_block, + ¤t_epoch, + ¤t_phase, + &mut was_disconnected, + ) + .await; + + assert!(!should_break); + assert!(was_disconnected); + let evt = rx.recv().await.unwrap(); + assert!(matches!(evt, BlockSyncEvent::Disconnected(_))); + } +} diff --git a/crates/bittensor-integration/src/challenge_weight_collector.rs b/crates/bittensor-integration/src/challenge_weight_collector.rs new file mode 100644 index 000000000..75d014244 --- /dev/null +++ b/crates/bittensor-integration/src/challenge_weight_collector.rs @@ -0,0 +1,846 @@ +//! Challenge Weight Collector +//! +//! Concurrently fetches weights from all challenge endpoints on epoch transition +//! and batch submits them to Bittensor. + +use crate::SubtensorClient; +use anyhow::Result; +use bittensor_rs::chain::ExtrinsicWait; +use bittensor_rs::validator::utility::batch_set_mechanism_weights; +use futures::future::join_all; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::RwLock; +use tokio::time::timeout; +use tracing::{debug, error, info, warn}; + +/// Default timeout for fetching weights from a challenge endpoint +pub const DEFAULT_CHALLENGE_TIMEOUT_SECS: u64 = 60; + +/// Challenge endpoint configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeEndpoint { + /// Challenge name for logging + pub name: String, + /// Mechanism ID on Bittensor (0-15) + pub mechanism_id: u8, + /// HTTP endpoint to fetch weights from + pub endpoint: String, + /// Timeout in seconds (default: 60) + #[serde(default = "default_timeout")] + pub timeout_secs: u64, + /// Is this challenge active + #[serde(default = "default_true")] + pub active: bool, +} + +fn default_timeout() -> u64 { + DEFAULT_CHALLENGE_TIMEOUT_SECS +} + +fn default_true() -> bool { + true +} + +/// Weight entry with hotkey (challenge returns this format) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HotkeyWeightEntry { + /// Miner hotkey (SS58 address) + pub hotkey: String, + /// Weight (0.0 - 1.0, normalized) + pub weight: f64, +} + +/// Weights response from challenge endpoint +/// +/// Challenges return weights with hotkeys, not UIDs. +/// The collector converts hotkeys to UIDs using the metagraph. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeWeightsResponse { + /// Epoch these weights are for + pub epoch: u64, + /// Weights per miner hotkey (preferred format) + #[serde(default)] + pub weights: Vec, + /// Legacy: UIDs (if challenge already converted) + #[serde(default)] + pub uids: Vec, + /// Legacy: Corresponding weights in u16 format + #[serde(default, rename = "weight_values")] + pub weight_values: Vec, + /// Optional: challenge name + #[serde(default)] + pub challenge_name: Option, + /// Optional: mechanism ID (for verification) + #[serde(default)] + pub mechanism_id: Option, +} + +/// UID 0 is the burn address - weights for unknown hotkeys go here +pub const BURN_UID: u16 = 0; + +/// Maximum weight value for Bittensor +pub const MAX_WEIGHT: u16 = 65535; + +/// Result of fetching weights from a single challenge +#[derive(Clone, Debug)] +pub struct ChallengeWeightResult { + pub mechanism_id: u8, + pub challenge_name: String, + pub uids: Vec, + pub weights: Vec, + pub success: bool, + pub error: Option, + pub duration_ms: u64, +} + +/// Collector status +#[derive(Clone, Debug, Default)] +pub struct CollectorStatus { + pub last_epoch: u64, + pub last_collection_time: Option>, + pub successful_challenges: usize, + pub failed_challenges: usize, + pub last_tx_hash: Option, +} + +/// Challenge Weight Collector +/// +/// Fetches weights from all challenge endpoints concurrently and +/// batch submits to Bittensor. +pub struct ChallengeWeightCollector { + /// Subtensor client for weight submission + client: SubtensorClient, + /// Challenge endpoints + endpoints: Arc>>, + /// HTTP client for fetching weights + http_client: reqwest::Client, + /// Status + status: Arc>, +} + +impl ChallengeWeightCollector { + /// Create a new collector + pub fn new(client: SubtensorClient) -> Self { + let http_client = reqwest::Client::builder() + .timeout(Duration::from_secs(DEFAULT_CHALLENGE_TIMEOUT_SECS + 5)) + .build() + .expect("Failed to create HTTP client"); + + Self { + client, + endpoints: Arc::new(RwLock::new(Vec::new())), + http_client, + status: Arc::new(RwLock::new(CollectorStatus::default())), + } + } + + /// Register a challenge endpoint + pub async fn register_endpoint(&self, endpoint: ChallengeEndpoint) { + let mut endpoints = self.endpoints.write().await; + + // Check if already registered + if endpoints + .iter() + .any(|e| e.mechanism_id == endpoint.mechanism_id) + { + warn!( + "Endpoint for mechanism {} already registered, updating", + endpoint.mechanism_id + ); + endpoints.retain(|e| e.mechanism_id != endpoint.mechanism_id); + } + + info!( + "Registered challenge endpoint: {} (mechanism {}) at {}", + endpoint.name, endpoint.mechanism_id, endpoint.endpoint + ); + endpoints.push(endpoint); + } + + /// Register multiple endpoints + pub async fn register_endpoints(&self, endpoints: Vec) { + for endpoint in endpoints { + self.register_endpoint(endpoint).await; + } + } + + /// Get registered endpoints + pub async fn get_endpoints(&self) -> Vec { + self.endpoints.read().await.clone() + } + + /// Get collector status + pub async fn status(&self) -> CollectorStatus { + self.status.read().await.clone() + } + + /// Convert hotkey weights to UID weights using metagraph + /// + /// - Hotkeys found in metagraph get their corresponding UIDs + /// - Hotkeys NOT found have their weight accumulated to UID 0 (burn) + /// - Returns (uids, weights) in Bittensor u16 format + fn convert_hotkeys_to_uids( + &self, + hotkey_weights: &[HotkeyWeightEntry], + ) -> (Vec, Vec) { + Self::convert_hotkeys_with_resolver(hotkey_weights, |hotkey| { + self.client.get_uid_for_hotkey(hotkey) + }) + } + + fn convert_hotkeys_with_resolver( + hotkey_weights: &[HotkeyWeightEntry], + mut resolver: F, + ) -> (Vec, Vec) + where + F: FnMut(&str) -> Option, + { + if hotkey_weights.is_empty() { + return (vec![BURN_UID], vec![MAX_WEIGHT]); + } + + let mut uid_weight_map: BTreeMap = BTreeMap::new(); + let mut burn_weight: f64 = 0.0; + let mut resolved_count = 0; + let mut unresolved_count = 0; + + for entry in hotkey_weights { + // Look up UID from metagraph + if let Some(uid) = resolver(&entry.hotkey) { + // Skip UID 0 from challenge weights - it's reserved for burn + if uid == BURN_UID { + debug!( + "Hotkey {} resolved to UID 0 (burn), adding to burn weight", + entry.hotkey + ); + burn_weight += entry.weight; + } else { + let weight_u16 = (entry.weight.clamp(0.0, 1.0) * MAX_WEIGHT as f64) as u64; + *uid_weight_map.entry(uid).or_insert(0) += weight_u16; + resolved_count += 1; + } + } else { + // Hotkey not found in metagraph - add weight to burn (UID 0) + warn!( + "Hotkey {} not found in metagraph, adding {:.4} weight to burn (UID 0)", + entry.hotkey, entry.weight + ); + burn_weight += entry.weight; + unresolved_count += 1; + } + } + + // Calculate total weight to ensure normalization + let total_assigned: u64 = uid_weight_map.values().sum(); + let burn_weight_u16 = (burn_weight.clamp(0.0, 1.0) * MAX_WEIGHT as f64) as u64; + + // Build final vectors + let mut uids = Vec::with_capacity(uid_weight_map.len() + 1); + let mut weights = Vec::with_capacity(uid_weight_map.len() + 1); + + // Add burn weight first if any + let final_burn = if burn_weight_u16 > 0 || unresolved_count > 0 { + // Ensure we don't exceed MAX_WEIGHT total + let remaining = MAX_WEIGHT as u64 - total_assigned.min(MAX_WEIGHT as u64); + remaining.min(burn_weight_u16) as u16 + } else { + 0 + }; + + if final_burn > 0 || uid_weight_map.is_empty() { + uids.push(BURN_UID); + weights.push(if uid_weight_map.is_empty() { + MAX_WEIGHT + } else { + final_burn + }); + } + + // Add resolved weights + for (uid, weight) in uid_weight_map { + uids.push(uid); + weights.push(weight.min(MAX_WEIGHT as u64) as u16); + } + + info!( + "Converted {} hotkeys: {} resolved to UIDs, {} unresolved -> burn (UID 0)", + hotkey_weights.len(), + resolved_count, + unresolved_count + ); + + (uids, weights) + } + + /// Fetch weights from a single challenge endpoint + async fn fetch_challenge_weights( + &self, + endpoint: &ChallengeEndpoint, + epoch: u64, + ) -> ChallengeWeightResult { + let start = std::time::Instant::now(); + let timeout_duration = Duration::from_secs(endpoint.timeout_secs); + + // Build URL with epoch parameter + let url = if endpoint.endpoint.contains('?') { + format!("{}&epoch={}", endpoint.endpoint, epoch) + } else { + format!("{}?epoch={}", endpoint.endpoint, epoch) + }; + + debug!( + "Fetching weights from {} (mechanism {}) with {}s timeout", + endpoint.name, endpoint.mechanism_id, endpoint.timeout_secs + ); + + let result = timeout(timeout_duration, async { + let response = self.http_client.get(&url).send().await?; + + if !response.status().is_success() { + return Err(anyhow::anyhow!( + "HTTP error: {} - {}", + response.status(), + response.text().await.unwrap_or_default() + )); + } + + let weights: ChallengeWeightsResponse = response.json().await?; + Ok(weights) + }) + .await; + + let duration_ms = start.elapsed().as_millis() as u64; + + match result { + Ok(Ok(response)) => { + // Check if challenge returned hotkey-based weights (preferred format) + let (uids, weights) = if !response.weights.is_empty() { + // Convert hotkeys to UIDs using metagraph + self.convert_hotkeys_to_uids(&response.weights) + } else if !response.uids.is_empty() && !response.weight_values.is_empty() { + // Legacy format: challenge already provided UIDs + if response.uids.len() != response.weight_values.len() { + return ChallengeWeightResult { + mechanism_id: endpoint.mechanism_id, + challenge_name: endpoint.name.clone(), + uids: vec![], + weights: vec![], + success: false, + error: Some("UIDs and weights length mismatch".to_string()), + duration_ms, + }; + } + (response.uids, response.weight_values) + } else { + // No weights returned - default to 100% burn + warn!("Challenge {} returned empty weights", endpoint.name); + (vec![BURN_UID], vec![MAX_WEIGHT]) + }; + + info!( + "Fetched weights from {} in {}ms: {} entries", + endpoint.name, + duration_ms, + uids.len() + ); + + ChallengeWeightResult { + mechanism_id: endpoint.mechanism_id, + challenge_name: endpoint.name.clone(), + uids, + weights, + success: true, + error: None, + duration_ms, + } + } + Ok(Err(e)) => { + error!("Failed to fetch weights from {}: {}", endpoint.name, e); + ChallengeWeightResult { + mechanism_id: endpoint.mechanism_id, + challenge_name: endpoint.name.clone(), + uids: vec![], + weights: vec![], + success: false, + error: Some(e.to_string()), + duration_ms, + } + } + Err(_) => { + error!( + "Timeout fetching weights from {} after {}s", + endpoint.name, endpoint.timeout_secs + ); + ChallengeWeightResult { + mechanism_id: endpoint.mechanism_id, + challenge_name: endpoint.name.clone(), + uids: vec![], + weights: vec![], + success: false, + error: Some(format!("Timeout after {}s", endpoint.timeout_secs)), + duration_ms, + } + } + } + } + + /// Collect weights from all challenges concurrently + pub async fn collect_all_weights(&self, epoch: u64) -> Vec { + let endpoints = self.endpoints.read().await.clone(); + let active_endpoints: Vec<_> = endpoints.into_iter().filter(|e| e.active).collect(); + + if active_endpoints.is_empty() { + warn!("No active challenge endpoints registered"); + return vec![]; + } + + info!( + "Collecting weights from {} challenges for epoch {}", + active_endpoints.len(), + epoch + ); + + // Fetch all concurrently + let futures: Vec<_> = active_endpoints + .iter() + .map(|endpoint| self.fetch_challenge_weights(endpoint, epoch)) + .collect(); + + let results = join_all(futures).await; + + // Update status + let successful = results.iter().filter(|r| r.success).count(); + let failed = results.len() - successful; + + let mut status = self.status.write().await; + status.last_epoch = epoch; + status.last_collection_time = Some(chrono::Utc::now()); + status.successful_challenges = successful; + status.failed_challenges = failed; + + info!( + "Weight collection complete: {}/{} successful", + successful, + results.len() + ); + + results + } + + /// Collect weights and batch submit to Bittensor + /// + /// Returns the transaction hash on success. + /// If a challenge fails, its weights default to 100% for UID 0 (burn). + pub async fn collect_and_submit(&self, epoch: u64) -> Result { + let results = self.collect_all_weights(epoch).await; + + if results.is_empty() { + return Err(anyhow::anyhow!("No challenge endpoints registered")); + } + + // Build weights for all mechanisms - use default (100% to UID 0) for failed challenges + let all_weights: Vec<(u8, Vec, Vec)> = results + .iter() + .map(|r| { + if r.success && !r.uids.is_empty() { + // Success - use actual weights + (r.mechanism_id, r.uids.clone(), r.weights.clone()) + } else { + // Failed - default to 100% weight on UID 0 (burn) + warn!( + "Challenge {} (mechanism {}) failed, defaulting to 100% UID 0: {:?}", + r.challenge_name, r.mechanism_id, r.error + ); + (r.mechanism_id, vec![0], vec![65535]) // 100% to UID 0 + } + }) + .collect(); + + info!( + "Batch submitting weights for {} mechanisms", + all_weights.len() + ); + + // Log which challenges are being submitted + for (mec_id, uids, _) in &all_weights { + if uids.len() == 1 && uids[0] == 0 { + debug!(" Mechanism {}: DEFAULT (100% to UID 0)", mec_id); + } else { + debug!(" Mechanism {}: {} weights", mec_id, uids.len()); + } + } + + // Submit batch + let tx_hash = batch_set_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + all_weights, + self.client.version_key(), + ExtrinsicWait::Finalized, + ) + .await?; + + // Update status + let mut status = self.status.write().await; + status.last_tx_hash = Some(tx_hash.clone()); + + info!("Batch weight submission successful: {}", tx_hash); + Ok(tx_hash) + } + + /// Sync metagraph before weight collection + /// + /// This MUST be called before collect_and_submit to ensure + /// hotkeys can be converted to UIDs correctly. + pub async fn sync_metagraph(&mut self) -> Result<()> { + info!("Syncing metagraph for hotkey->UID conversion..."); + self.client.sync_metagraph().await?; + + if let Some(metagraph) = self.client.metagraph() { + info!("Metagraph synced: {} neurons", metagraph.neurons.len()); + } + Ok(()) + } + + /// Handle new epoch event + /// + /// Called when a new epoch starts. Syncs metagraph, collects weights and submits. + pub async fn on_new_epoch(&mut self, epoch: u64) -> Result { + info!("New epoch {} - starting weight collection", epoch); + + // Sync metagraph to get latest hotkey->UID mappings + self.sync_metagraph().await?; + + self.collect_and_submit(epoch).await + } + + /// Get mutable access to client + pub fn client_mut(&mut self) -> &mut SubtensorClient { + &mut self.client + } + + /// Get client reference + pub fn client(&self) -> &SubtensorClient { + &self.client + } +} + +/// Builder for ChallengeWeightCollector +pub struct ChallengeWeightCollectorBuilder { + client: SubtensorClient, + endpoints: Vec, +} + +impl ChallengeWeightCollectorBuilder { + pub fn new(client: SubtensorClient) -> Self { + Self { + client, + endpoints: Vec::new(), + } + } + + pub fn add_endpoint(mut self, endpoint: ChallengeEndpoint) -> Self { + self.endpoints.push(endpoint); + self + } + + pub fn add_endpoints(mut self, endpoints: Vec) -> Self { + self.endpoints.extend(endpoints); + self + } + + pub async fn build(self) -> ChallengeWeightCollector { + let collector = ChallengeWeightCollector::new(self.client); + collector.register_endpoints(self.endpoints).await; + collector + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::BittensorConfig; + + fn sample_endpoint(name: &str, mechanism_id: u8, url: &str) -> ChallengeEndpoint { + ChallengeEndpoint { + name: name.to_string(), + mechanism_id, + endpoint: url.to_string(), + timeout_secs: 5, + active: true, + } + } + + #[test] + fn test_challenge_endpoint_serde() { + let endpoint = ChallengeEndpoint { + name: "Test Challenge".to_string(), + mechanism_id: 1, + endpoint: "http://localhost:8080/weights".to_string(), + timeout_secs: 60, + active: true, + }; + + let json = serde_json::to_string(&endpoint).unwrap(); + let parsed: ChallengeEndpoint = serde_json::from_str(&json).unwrap(); + + assert_eq!(parsed.mechanism_id, 1); + assert_eq!(parsed.timeout_secs, 60); + } + + #[test] + fn test_hotkey_weight_entry_serde() { + let entry = HotkeyWeightEntry { + hotkey: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), + weight: 0.5, + }; + + let json = serde_json::to_string(&entry).unwrap(); + let parsed: HotkeyWeightEntry = serde_json::from_str(&json).unwrap(); + + assert_eq!(parsed.weight, 0.5); + assert!(parsed.hotkey.starts_with("5G")); + } + + #[test] + fn test_convert_hotkeys_all_resolved() { + let entries = vec![ + HotkeyWeightEntry { + hotkey: "hk1".to_string(), + weight: 0.6, + }, + HotkeyWeightEntry { + hotkey: "hk2".to_string(), + weight: 0.4, + }, + ]; + + let (uids, weights) = ChallengeWeightCollector::convert_hotkeys_with_resolver( + &entries, + |hotkey| match hotkey { + "hk1" => Some(1), + "hk2" => Some(2), + _ => None, + }, + ); + + assert_eq!(uids, vec![1, 2]); + assert_eq!(weights.len(), 2); + assert!(weights[0] > weights[1]); + } + + #[test] + fn test_convert_hotkeys_unresolved_go_to_burn() { + let entries = vec![HotkeyWeightEntry { + hotkey: "missing".to_string(), + weight: 1.0, + }]; + + let (uids, weights) = + ChallengeWeightCollector::convert_hotkeys_with_resolver(&entries, |_| None); + + assert_eq!(uids, vec![BURN_UID]); + assert_eq!(weights, vec![MAX_WEIGHT]); + } + + #[test] + fn test_convert_hotkeys_empty_defaults_to_burn() { + let (uids, weights) = + ChallengeWeightCollector::convert_hotkeys_with_resolver(&[], |_| Some(1)); + assert_eq!(uids, vec![BURN_UID]); + assert_eq!(weights, vec![MAX_WEIGHT]); + } + + #[test] + fn test_convert_hotkeys_accumulates_duplicates() { + let entries = vec![ + HotkeyWeightEntry { + hotkey: "hk".to_string(), + weight: 0.3, + }, + HotkeyWeightEntry { + hotkey: "hk".to_string(), + weight: 0.2, + }, + ]; + + let (uids, weights) = + ChallengeWeightCollector::convert_hotkeys_with_resolver(&entries, |_| Some(10)); + + assert_eq!(uids, vec![10]); + assert!(weights[0] >= (MAX_WEIGHT / 2)); + } + + #[test] + fn test_weights_response_with_hotkeys() { + // New format: weights with hotkeys + let response = ChallengeWeightsResponse { + epoch: 100, + weights: vec![ + HotkeyWeightEntry { + hotkey: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), + weight: 0.6, + }, + HotkeyWeightEntry { + hotkey: "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty".to_string(), + weight: 0.4, + }, + ], + uids: vec![], + weight_values: vec![], + challenge_name: Some("Test".to_string()), + mechanism_id: Some(1), + }; + + let json = serde_json::to_string(&response).unwrap(); + let parsed: ChallengeWeightsResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(parsed.epoch, 100); + assert_eq!(parsed.weights.len(), 2); + assert!(parsed.uids.is_empty()); + } + + #[test] + fn test_weights_response_legacy_format() { + // Legacy format: UIDs directly + let response = ChallengeWeightsResponse { + epoch: 100, + weights: vec![], + uids: vec![1, 2, 3], + weight_values: vec![20000, 30000, 15535], + challenge_name: Some("Test".to_string()), + mechanism_id: Some(1), + }; + + let json = serde_json::to_string(&response).unwrap(); + let parsed: ChallengeWeightsResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(parsed.epoch, 100); + assert_eq!(parsed.uids.len(), 3); + assert_eq!(parsed.weight_values.len(), 3); + } + + #[tokio::test] + async fn test_register_endpoint_replaces_existing_mechanism() { + let client = SubtensorClient::new(BittensorConfig::local(42)); + let collector = ChallengeWeightCollector::new(client); + + collector + .register_endpoint(sample_endpoint("first", 7, "http://one")) + .await; + collector + .register_endpoint(sample_endpoint("second", 7, "http://two")) + .await; + + let endpoints = collector.get_endpoints().await; + assert_eq!(endpoints.len(), 1); + assert_eq!(endpoints[0].name, "second"); + assert_eq!(endpoints[0].endpoint, "http://two"); + } + + #[tokio::test] + async fn test_collect_all_weights_returns_empty_when_no_endpoints() { + let client = SubtensorClient::new(BittensorConfig::local(1)); + let collector = ChallengeWeightCollector::new(client); + + let results = collector.collect_all_weights(123).await; + assert!(results.is_empty()); + + let status = collector.status().await; + assert_eq!(status.successful_challenges, 0); + assert_eq!(status.failed_challenges, 0); + assert_eq!(status.last_epoch, 0); + } + + #[tokio::test] + async fn test_register_endpoints_adds_all_entries() { + let client = SubtensorClient::new(BittensorConfig::local(9)); + let collector = ChallengeWeightCollector::new(client); + + collector + .register_endpoints(vec![ + sample_endpoint("one", 1, "http://one"), + sample_endpoint("two", 2, "http://two"), + ]) + .await; + + let endpoints = collector.get_endpoints().await; + assert_eq!(endpoints.len(), 2); + let names: Vec<_> = endpoints.into_iter().map(|e| e.name).collect(); + assert!(names.contains(&"one".to_string())); + assert!(names.contains(&"two".to_string())); + } + + #[test] + fn test_convert_hotkeys_with_resolver_burn_uid() { + let entries = vec![HotkeyWeightEntry { + hotkey: "burn-hotkey".to_string(), + weight: 0.75, + }]; + + let (uids, weights) = + ChallengeWeightCollector::convert_hotkeys_with_resolver(&entries, |_| Some(BURN_UID)); + + assert_eq!(uids, vec![BURN_UID]); + assert_eq!(weights.len(), 1); + assert!(weights[0] > (MAX_WEIGHT / 2)); + } + + #[test] + fn test_convert_hotkeys_to_uids_uses_client_lookup() { + let mut client = SubtensorClient::new(BittensorConfig::local(3)); + client.set_uid_overrides(vec![("hk-a".to_string(), 4), ("hk-b".to_string(), 7)]); + let collector = ChallengeWeightCollector::new(client); + + let entries = vec![ + HotkeyWeightEntry { + hotkey: "hk-a".to_string(), + weight: 0.4, + }, + HotkeyWeightEntry { + hotkey: "hk-b".to_string(), + weight: 0.6, + }, + ]; + + let (uids, weights) = collector.convert_hotkeys_to_uids(&entries); + + assert_eq!(uids, vec![4, 7]); + assert_eq!(weights.len(), 2); + assert!(weights.iter().all(|w| *w > 0)); + } + + #[tokio::test] + async fn test_challenge_weight_collector_builder_registers_endpoints() { + let client = SubtensorClient::new(BittensorConfig::local(5)); + let collector = ChallengeWeightCollectorBuilder::new(client) + .add_endpoint(sample_endpoint("alpha", 1, "http://alpha")) + .add_endpoints(vec![sample_endpoint("beta", 2, "http://beta")]) + .build() + .await; + + let endpoints = collector.get_endpoints().await; + assert_eq!(endpoints.len(), 2); + assert!(endpoints.iter().any(|e| e.name == "alpha")); + assert!(endpoints.iter().any(|e| e.name == "beta")); + } + + #[test] + fn test_collector_client_accessors() { + let client = SubtensorClient::new(BittensorConfig::local(8)); + let mut collector = ChallengeWeightCollector::new(client); + assert_eq!(collector.client().netuid(), 8); + collector + .client_mut() + .set_uid_overrides(vec![("hk".to_string(), 12)]); + let (uids, _) = collector.convert_hotkeys_to_uids(&[HotkeyWeightEntry { + hotkey: "hk".to_string(), + weight: 1.0, + }]); + assert_eq!(uids, vec![12]); + } +} diff --git a/crates/bittensor-integration/src/client.rs b/crates/bittensor-integration/src/client.rs new file mode 100644 index 000000000..fa143d5d9 --- /dev/null +++ b/crates/bittensor-integration/src/client.rs @@ -0,0 +1,238 @@ +//! Bittensor client wrapper + +use crate::BittensorConfig; +use anyhow::Result; +use bittensor_rs::chain::{signer_from_seed, BittensorClient, BittensorSigner}; +use bittensor_rs::metagraph::{sync_metagraph, Metagraph}; +use std::collections::HashMap; +use tracing::info; + +/// Wrapper around bittensor-rs client for Mini-Chain +pub struct SubtensorClient { + config: BittensorConfig, + client: Option, + signer: Option, + metagraph: Option, + uid_overrides: HashMap, +} + +impl SubtensorClient { + /// Create a new client (not connected yet) + pub fn new(config: BittensorConfig) -> Self { + Self { + config, + client: None, + signer: None, + metagraph: None, + uid_overrides: HashMap::new(), + } + } + + /// Connect to Subtensor + pub async fn connect(&mut self) -> Result<()> { + info!("Connecting to Subtensor: {}", self.config.endpoint); + + let client = BittensorClient::new(&self.config.endpoint).await?; + self.client = Some(client); + + info!("Connected to Subtensor"); + Ok(()) + } + + /// Set the signer from a seed phrase or key + pub fn set_signer(&mut self, seed: &str) -> Result<()> { + let signer = signer_from_seed(seed)?; + self.signer = Some(signer); + Ok(()) + } + + /// Get the inner client + pub fn client(&self) -> Result<&BittensorClient> { + self.client + .as_ref() + .ok_or_else(|| anyhow::anyhow!("Not connected to Subtensor")) + } + + /// Get the signer (returns Result) + pub fn signer(&self) -> Result<&BittensorSigner> { + self.signer + .as_ref() + .ok_or_else(|| anyhow::anyhow!("Signer not set")) + } + + /// Get the signer (returns Option) + pub fn get_signer(&self) -> Option<&BittensorSigner> { + self.signer.as_ref() + } + + /// Get the netuid + pub fn netuid(&self) -> u16 { + self.config.netuid + } + + /// Get the version key + pub fn version_key(&self) -> u64 { + self.config.version_key + } + + /// Check if commit-reveal is enabled + pub fn use_commit_reveal(&self) -> bool { + self.config.use_commit_reveal + } + + /// Sync and get the current metagraph + pub async fn sync_metagraph(&mut self) -> Result<&Metagraph> { + let client = self.client()?; + let metagraph = sync_metagraph(client, self.config.netuid).await?; + self.metagraph = Some(metagraph); + Ok(self.metagraph.as_ref().unwrap()) + } + + /// Get cached metagraph (sync first if needed) + pub fn metagraph(&self) -> Option<&Metagraph> { + self.metagraph.as_ref() + } + + /// Set the metagraph directly (useful when synced externally) + pub fn set_metagraph(&mut self, metagraph: Metagraph) { + self.metagraph = Some(metagraph); + } + + /// Get UID for a hotkey from cached metagraph + pub fn get_uid_for_hotkey(&self, hotkey: &str) -> Option { + if let Some(uid) = self.uid_overrides.get(hotkey) { + return Some(*uid); + } + self.lookup_uid_in_metagraph(hotkey) + } + + /// Get UIDs for a list of hotkeys + pub fn get_uids_for_hotkeys(&self, hotkeys: &[String]) -> Vec<(String, u16)> { + let mut results = Vec::new(); + for hotkey in hotkeys { + if let Some(uid) = self.get_uid_for_hotkey(hotkey) { + results.push((hotkey.clone(), uid)); + } + } + results + } + + fn lookup_uid_in_metagraph(&self, hotkey: &str) -> Option { + let metagraph = self.metagraph.as_ref()?; + + use sp_core::crypto::Ss58Codec; + let account = sp_core::crypto::AccountId32::from_ss58check(hotkey).ok()?; + let account_bytes: &[u8; 32] = account.as_ref(); + + for (uid, neuron) in &metagraph.neurons { + let neuron_hotkey: &[u8; 32] = neuron.hotkey.as_ref(); + if neuron_hotkey == account_bytes { + return Some(*uid as u16); + } + } + None + } + + pub fn set_uid_overrides(&mut self, entries: Vec<(String, u16)>) { + self.uid_overrides = entries.into_iter().collect(); + } + + /// Get the number of mechanisms for the subnet + /// Returns count of mechanisms (0 to count-1 are valid IDs) + pub async fn get_mechanism_count(&self) -> Result { + use bittensor_rs::get_mechanism_count; + let client = self.client()?; + get_mechanism_count(client, self.config.netuid).await + } + + /// Get current epoch from Bittensor + pub async fn get_current_epoch(&self) -> Result { + use bittensor_rs::blocks::{BlockListener, BlockListenerConfig}; + + let client = self.client()?; + let config = BlockListenerConfig { + netuid: self.config.netuid, + ..Default::default() + }; + let listener = BlockListener::new(config); + let epoch_info = listener.current_epoch_info(client).await?; + Ok(epoch_info.epoch_number) + } + + /// Get current epoch info including phase from Bittensor + pub async fn get_current_epoch_info(&self) -> Result { + use bittensor_rs::blocks::{BlockListener, BlockListenerConfig}; + + let client = self.client()?; + let config = BlockListenerConfig { + netuid: self.config.netuid, + ..Default::default() + }; + let listener = BlockListener::new(config); + listener.current_epoch_info(client).await + } + + /// Check if currently in reveal phase + pub async fn is_in_reveal_phase(&self) -> Result { + use bittensor_rs::blocks::EpochPhase; + let info = self.get_current_epoch_info().await?; + Ok(matches!(info.phase, EpochPhase::RevealWindow)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::BittensorConfig; + + #[test] + fn test_set_signer_initializes_signer_field() { + let mut client = SubtensorClient::new(BittensorConfig::local(33)); + client.set_signer("//Alice").expect("set signer"); + assert!(client.get_signer().is_some()); + } + + #[test] + fn test_client_returns_error_when_not_connected() { + let client = SubtensorClient::new(BittensorConfig::local(9)); + assert!(client.client().is_err()); + assert!(client.signer().is_err()); + assert_eq!(client.netuid(), 9); + assert!(!client.use_commit_reveal()); + } + + #[test] + fn test_version_key_reflects_config() { + let config = BittensorConfig { + endpoint: "ws://node".into(), + netuid: 12, + use_commit_reveal: false, + version_key: 99, + }; + let client = SubtensorClient::new(config.clone()); + assert_eq!(client.version_key(), 99); + assert_eq!(client.netuid(), config.netuid); + assert_eq!(client.use_commit_reveal(), config.use_commit_reveal); + } + + #[test] + fn test_get_uid_for_hotkey_uses_overrides() { + let mut client = SubtensorClient::new(BittensorConfig::local(2)); + client.set_uid_overrides(vec![("hk-a".to_string(), 5), ("hk-b".to_string(), 7)]); + + assert_eq!(client.get_uid_for_hotkey("hk-a"), Some(5)); + assert_eq!(client.get_uid_for_hotkey("hk-b"), Some(7)); + assert!(client.get_uid_for_hotkey("missing").is_none()); + } + + #[test] + fn test_get_uids_for_hotkeys_filters_missing() { + let mut client = SubtensorClient::new(BittensorConfig::local(4)); + client.set_uid_overrides(vec![("hk".to_string(), 11)]); + + let pairs = client.get_uids_for_hotkeys(&["hk".to_string(), "unknown".to_string()]); + + assert_eq!(pairs.len(), 1); + assert_eq!(pairs[0], ("hk".to_string(), 11)); + } +} diff --git a/crates/bittensor-integration/src/config.rs b/crates/bittensor-integration/src/config.rs new file mode 100644 index 000000000..0c3f25f46 --- /dev/null +++ b/crates/bittensor-integration/src/config.rs @@ -0,0 +1,75 @@ +//! Bittensor configuration + +use serde::{Deserialize, Serialize}; + +/// Default NETUID for the platform +pub const DEFAULT_NETUID: u16 = 100; + +/// Bittensor network configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BittensorConfig { + /// Subtensor WebSocket endpoint + pub endpoint: String, + + /// Subnet UID (netuid) - default is 100 + pub netuid: u16, + + /// Use commit-reveal for weights (vs direct set_weights) + pub use_commit_reveal: bool, + + /// Version key for weight submissions + pub version_key: u64, +} + +impl Default for BittensorConfig { + fn default() -> Self { + Self { + endpoint: "wss://entrypoint-finney.opentensor.ai:443".to_string(), + netuid: DEFAULT_NETUID, + use_commit_reveal: true, + version_key: 1, + } + } +} + +impl BittensorConfig { + /// Create config for testnet + pub fn testnet(netuid: u16) -> Self { + Self { + endpoint: "wss://test.finney.opentensor.ai:443".to_string(), + netuid, + use_commit_reveal: true, + version_key: 1, + } + } + + /// Create config for local network + pub fn local(netuid: u16) -> Self { + Self { + endpoint: "ws://127.0.0.1:9944".to_string(), + netuid, + use_commit_reveal: false, + version_key: 1, + } + } + + /// Create config for mainnet with default NETUID (100) + pub fn mainnet() -> Self { + Self::default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mainnet_returns_default_config() { + let cfg = BittensorConfig::mainnet(); + let default = BittensorConfig::default(); + assert_eq!(cfg.endpoint, default.endpoint); + assert_eq!(cfg.netuid, DEFAULT_NETUID); + assert_eq!(cfg.use_commit_reveal, default.use_commit_reveal); + assert_eq!(cfg.version_key, default.version_key); + } +} diff --git a/crates/bittensor-integration/src/lib.rs b/crates/bittensor-integration/src/lib.rs new file mode 100644 index 000000000..8a3ca0242 --- /dev/null +++ b/crates/bittensor-integration/src/lib.rs @@ -0,0 +1,50 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Bittensor Integration for Mini-Chain +//! +//! Connects the Mini-Chain P2P layer to Bittensor blockchain +//! for submitting weights and reading metagraph state. +//! +//! Features: +//! - Validators synced from Bittensor metagraph +//! - Block subscription for epoch synchronization +//! - Weight submission via mechanism-based batching +//! - Concurrent weight collection from challenge endpoints +//! +//! The `BlockSync` module subscribes to finalized Bittensor blocks +//! to synchronize platform epochs with on-chain state. + +mod block_sync; +mod challenge_weight_collector; +mod client; +mod config; +mod validator_sync; +mod weights; + +// Mock module for testing - available in tests and to other crates for integration tests +#[cfg(any(test, feature = "test-utils"))] +pub mod mock; + +#[cfg(test)] +mod tests; + +pub use block_sync::*; +pub use challenge_weight_collector::*; +pub use client::*; +pub use config::*; +pub use validator_sync::*; +pub use weights::*; + +// Re-export bittensor-rs types for convenience +pub use bittensor_rs::{sync_metagraph, BittensorClient, Metagraph}; + +// Re-export high-level Subtensor API (use this directly instead of custom wrappers) +pub use bittensor_rs::{ + PendingCommit, Salt, Subtensor, SubtensorBuilder, SubtensorState, WeightResponse, + WeightResponseData, +}; + +// Re-export tempo/epoch functions +pub use bittensor_rs::{get_reveal_period, get_tempo}; + +// Re-export chain types needed for weight submission +pub use bittensor_rs::chain::{signer_from_seed, BittensorSigner, ExtrinsicWait}; diff --git a/crates/bittensor-integration/src/mock.rs b/crates/bittensor-integration/src/mock.rs new file mode 100644 index 000000000..d1352b91f --- /dev/null +++ b/crates/bittensor-integration/src/mock.rs @@ -0,0 +1,573 @@ +//! Mock implementations for Bittensor integration tests +//! +//! Provides mock structures that can simulate Bittensor metagraph state +//! without connecting to the real network. + +use bittensor_rs::metagraph::Metagraph; +use bittensor_rs::types::NeuronInfo; +use sp_core::crypto::AccountId32; +use std::collections::HashMap; + +/// Builder for creating mock neurons with configurable parameters +#[derive(Clone, Debug)] +pub struct MockNeuronBuilder { + uid: u64, + netuid: u16, + hotkey: [u8; 32], + coldkey: [u8; 32], + stake: u128, + root_stake: u128, + incentive: f64, + trust: f64, + consensus: f64, + dividends: f64, + emission: f64, + validator_permit: bool, + last_update: u64, + pruning_score: u64, + validator_trust: f64, + rank: f64, + active: bool, +} + +impl MockNeuronBuilder { + /// Create a new neuron builder with default values + pub fn new(uid: u64) -> Self { + let mut hotkey = [0u8; 32]; + hotkey[0..8].copy_from_slice(&uid.to_le_bytes()); + + Self { + uid, + netuid: 1, + hotkey, + coldkey: [0u8; 32], + stake: 0, + root_stake: 0, + incentive: 0.0, + trust: 0.0, + consensus: 0.0, + dividends: 0.0, + emission: 0.0, + validator_permit: false, + last_update: 0, + pruning_score: 0, + validator_trust: 0.0, + rank: 0.0, + active: true, + } + } + + /// Set the netuid + pub fn netuid(mut self, netuid: u16) -> Self { + self.netuid = netuid; + self + } + + /// Set the hotkey bytes + pub fn hotkey(mut self, hotkey: [u8; 32]) -> Self { + self.hotkey = hotkey; + self + } + + /// Set the coldkey bytes + pub fn coldkey(mut self, coldkey: [u8; 32]) -> Self { + self.coldkey = coldkey; + self + } + + /// Set the stake in RAO (1 TAO = 1e9 RAO) + pub fn stake(mut self, stake: u128) -> Self { + self.stake = stake; + self + } + + /// Set stake in TAO (convenience method) + pub fn stake_tao(mut self, tao: f64) -> Self { + self.stake = (tao * 1_000_000_000.0) as u128; + self + } + + /// Set root stake in RAO + pub fn root_stake(mut self, root_stake: u128) -> Self { + self.root_stake = root_stake; + self + } + + /// Set root stake in TAO (convenience method) + pub fn root_stake_tao(mut self, tao: f64) -> Self { + self.root_stake = (tao * 1_000_000_000.0) as u128; + self + } + + /// Set incentive score (0.0 - 1.0 normalized, stored as f64) + pub fn incentive(mut self, incentive: f64) -> Self { + self.incentive = incentive * u16::MAX as f64; + self + } + + /// Set trust score (0.0 - 1.0 normalized, stored as f64) + pub fn trust(mut self, trust: f64) -> Self { + self.trust = trust * u16::MAX as f64; + self + } + + /// Set consensus score (0.0 - 1.0 normalized, stored as f64) + pub fn consensus(mut self, consensus: f64) -> Self { + self.consensus = consensus * u16::MAX as f64; + self + } + + /// Set dividends + pub fn dividends(mut self, dividends: f64) -> Self { + self.dividends = dividends * u16::MAX as f64; + self + } + + /// Set emission + pub fn emission(mut self, emission: f64) -> Self { + self.emission = emission; + self + } + + /// Set validator permit + pub fn validator_permit(mut self, permit: bool) -> Self { + self.validator_permit = permit; + self + } + + /// Set last update block + pub fn last_update(mut self, block: u64) -> Self { + self.last_update = block; + self + } + + /// Set pruning score + pub fn pruning_score(mut self, score: u64) -> Self { + self.pruning_score = score; + self + } + + /// Set validator trust + pub fn validator_trust(mut self, trust: f64) -> Self { + self.validator_trust = trust * u16::MAX as f64; + self + } + + /// Set rank + pub fn rank(mut self, rank: f64) -> Self { + self.rank = rank * u16::MAX as f64; + self + } + + /// Set active status + pub fn active(mut self, active: bool) -> Self { + self.active = active; + self + } + + /// Get the uid + pub fn get_uid(&self) -> u64 { + self.uid + } + + /// Get the hotkey bytes + pub fn get_hotkey(&self) -> [u8; 32] { + self.hotkey + } + + /// Build the neuron and return data needed to add to metagraph + pub fn build(self) -> MockNeuronData { + MockNeuronData { + uid: self.uid, + netuid: self.netuid, + hotkey: self.hotkey, + coldkey: self.coldkey, + stake: self.stake, + root_stake: self.root_stake, + incentive: self.incentive, + trust: self.trust, + consensus: self.consensus, + dividends: self.dividends, + emission: self.emission, + validator_permit: self.validator_permit, + last_update: self.last_update, + pruning_score: self.pruning_score, + validator_trust: self.validator_trust, + rank: self.rank, + active: self.active, + } + } +} + +/// Data for a mock neuron +#[derive(Clone, Debug)] +pub struct MockNeuronData { + pub uid: u64, + pub netuid: u16, + pub hotkey: [u8; 32], + pub coldkey: [u8; 32], + pub stake: u128, + pub root_stake: u128, + pub incentive: f64, + pub trust: f64, + pub consensus: f64, + pub dividends: f64, + pub emission: f64, + pub validator_permit: bool, + pub last_update: u64, + pub pruning_score: u64, + pub validator_trust: f64, + pub rank: f64, + pub active: bool, +} + +impl MockNeuronData { + /// Convert to NeuronInfo for use in Metagraph + pub fn to_neuron_info(&self) -> NeuronInfo { + let hotkey = AccountId32::new(self.hotkey); + let coldkey = AccountId32::new(self.coldkey); + + NeuronInfo { + uid: self.uid, + netuid: self.netuid, + hotkey, + coldkey, + stake: self.stake, + stake_dict: HashMap::new(), + total_stake: self.stake, + root_stake: self.root_stake, + stake_weight: 0, + rank: self.rank, + trust: self.trust, + consensus: self.consensus, + validator_trust: self.validator_trust, + incentive: self.incentive, + emission: self.emission, + dividends: self.dividends, + active: self.active, + last_update: self.last_update, + validator_permit: self.validator_permit, + version: 0, + weights: Vec::new(), + bonds: Vec::new(), + pruning_score: self.pruning_score, + prometheus_info: None, + axon_info: None, + is_null: false, + } + } +} + +/// Builder for creating mock metagraphs +#[derive(Clone, Debug)] +pub struct MockMetagraphBuilder { + netuid: u16, + neurons: Vec, + block: u64, + version: u64, +} + +impl MockMetagraphBuilder { + /// Create a new metagraph builder for the given netuid + pub fn new(netuid: u16) -> Self { + Self { + netuid, + neurons: Vec::new(), + block: 0, + version: 1, + } + } + + /// Set the current block number + pub fn block(mut self, block: u64) -> Self { + self.block = block; + self + } + + /// Set the version + pub fn version(mut self, version: u64) -> Self { + self.version = version; + self + } + + /// Add a neuron using the builder pattern + pub fn add_neuron(mut self, neuron: MockNeuronData) -> Self { + self.neurons.push(neuron); + self + } + + /// Add multiple neurons + pub fn add_neurons(mut self, neurons: Vec) -> Self { + self.neurons.extend(neurons); + self + } + + /// Add a validator with specified parameters (convenience method) + pub fn add_validator(self, uid: u64, hotkey: [u8; 32], stake_tao: f64) -> Self { + let neuron = MockNeuronBuilder::new(uid) + .netuid(self.netuid) + .hotkey(hotkey) + .stake_tao(stake_tao) + .validator_permit(true) + .active(true) + .build(); + self.add_neuron(neuron) + } + + /// Add a miner with specified parameters (convenience method) + pub fn add_miner(self, uid: u64, hotkey: [u8; 32]) -> Self { + let neuron = MockNeuronBuilder::new(uid) + .netuid(self.netuid) + .hotkey(hotkey) + .stake(0) + .active(true) + .build(); + self.add_neuron(neuron) + } + + /// Build the mock metagraph + pub fn build(self) -> Metagraph { + let mut neurons = HashMap::new(); + + for data in &self.neurons { + let neuron_info = data.to_neuron_info(); + neurons.insert(data.uid, neuron_info); + } + + Metagraph { + netuid: self.netuid, + n: neurons.len() as u64, + block: self.block, + neurons, + axons: HashMap::new(), + version: self.version, + } + } +} + +/// Helper function to create a random hotkey +pub fn random_hotkey() -> [u8; 32] { + use rand::Rng; + let mut rng = rand::thread_rng(); + let mut hotkey = [0u8; 32]; + rng.fill(&mut hotkey); + hotkey +} + +/// Helper function to create a hotkey from a deterministic seed +pub fn hotkey_from_seed(seed: u64) -> [u8; 32] { + use sha2::{Digest, Sha256}; + let mut hasher = Sha256::new(); + hasher.update(seed.to_le_bytes()); + let result = hasher.finalize(); + let mut hotkey = [0u8; 32]; + hotkey.copy_from_slice(&result[..]); + hotkey +} + +/// Helper function to create multiple validators with sequential UIDs +pub fn create_validators( + count: u16, + min_stake_tao: f64, + max_stake_tao: f64, +) -> Vec { + use rand::Rng; + let mut rng = rand::thread_rng(); + + (0..count) + .map(|uid| { + let stake_tao = rng.gen_range(min_stake_tao..=max_stake_tao); + MockNeuronBuilder::new(uid as u64) + .hotkey(hotkey_from_seed(uid as u64)) + .stake_tao(stake_tao) + .validator_permit(true) + .active(true) + .incentive(rng.gen_range(0.0..1.0)) + .trust(rng.gen_range(0.0..1.0)) + .consensus(rng.gen_range(0.0..1.0)) + .build() + }) + .collect() +} + +/// Helper function to create a metagraph with a specified number of validators +pub fn create_test_metagraph(netuid: u16, validator_count: u16, min_stake_tao: f64) -> Metagraph { + let validators = create_validators(validator_count, min_stake_tao, min_stake_tao * 10.0); + MockMetagraphBuilder::new(netuid) + .add_neurons(validators) + .build() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mock_neuron_builder_defaults() { + let neuron = MockNeuronBuilder::new(5).build(); + + assert_eq!(neuron.uid, 5); + assert_eq!(neuron.stake, 0); + assert_eq!(neuron.root_stake, 0); + assert_eq!(neuron.incentive, 0.0); + } + + #[test] + fn test_mock_neuron_builder_stake_tao() { + let neuron = MockNeuronBuilder::new(1) + .stake_tao(100.0) + .root_stake_tao(50.0) + .build(); + + assert_eq!(neuron.stake, 100_000_000_000); // 100 TAO in RAO + assert_eq!(neuron.root_stake, 50_000_000_000); // 50 TAO in RAO + } + + #[test] + fn test_mock_neuron_builder_scores() { + let neuron = MockNeuronBuilder::new(1) + .incentive(0.5) + .trust(0.8) + .consensus(0.9) + .build(); + + // Scores are stored as f64 * u16::MAX + let expected_incentive = 0.5 * u16::MAX as f64; + let expected_trust = 0.8 * u16::MAX as f64; + let expected_consensus = 0.9 * u16::MAX as f64; + + assert!((neuron.incentive - expected_incentive).abs() < 0.001); + assert!((neuron.trust - expected_trust).abs() < 0.001); + assert!((neuron.consensus - expected_consensus).abs() < 0.001); + } + + #[test] + fn test_mock_metagraph_builder() { + let hotkey1 = [1u8; 32]; + let hotkey2 = [2u8; 32]; + + let metagraph = MockMetagraphBuilder::new(100) + .block(5000) + .add_validator(0, hotkey1, 1000.0) + .add_validator(1, hotkey2, 500.0) + .build(); + + assert_eq!(metagraph.netuid, 100); + assert_eq!(metagraph.n, 2); + assert_eq!(metagraph.block, 5000); + assert_eq!(metagraph.neurons.len(), 2); + + let neuron0 = metagraph.neurons.get(&0).expect("neuron 0 exists"); + let neuron1 = metagraph.neurons.get(&1).expect("neuron 1 exists"); + + assert_eq!(neuron0.stake, 1_000_000_000_000); + assert_eq!(neuron1.stake, 500_000_000_000); + } + + #[test] + fn test_mock_metagraph_add_miner() { + let metagraph = MockMetagraphBuilder::new(1) + .add_miner(0, [10u8; 32]) + .add_miner(1, [11u8; 32]) + .build(); + + assert_eq!(metagraph.n, 2); + + for neuron in metagraph.neurons.values() { + assert_eq!(neuron.stake, 0); + } + } + + #[test] + fn test_hotkey_from_seed_deterministic() { + let hotkey1 = hotkey_from_seed(42); + let hotkey2 = hotkey_from_seed(42); + let hotkey3 = hotkey_from_seed(43); + + assert_eq!(hotkey1, hotkey2); + assert_ne!(hotkey1, hotkey3); + } + + #[test] + fn test_create_validators() { + let validators = create_validators(5, 100.0, 1000.0); + + assert_eq!(validators.len(), 5); + + for (i, v) in validators.iter().enumerate() { + assert_eq!(v.uid, i as u64); + assert!(v.validator_permit); + // Stake should be between 100 and 1000 TAO + let stake_tao = v.stake as f64 / 1_000_000_000.0; + assert!((100.0..=1000.0).contains(&stake_tao)); + } + } + + #[test] + fn test_create_test_metagraph() { + let metagraph = create_test_metagraph(100, 10, 500.0); + + assert_eq!(metagraph.netuid, 100); + assert_eq!(metagraph.n, 10); + assert_eq!(metagraph.neurons.len(), 10); + } + + #[test] + fn test_metagraph_neuron_hotkey_conversion() { + use sp_core::crypto::Ss58Codec; + + let hotkey_bytes = [1u8; 32]; + let metagraph = MockMetagraphBuilder::new(1) + .add_validator(0, hotkey_bytes, 100.0) + .build(); + + let neuron = metagraph.neurons.get(&0).expect("neuron exists"); + + // Verify the hotkey can be converted to SS58 + let ss58 = neuron.hotkey.to_ss58check(); + assert!(!ss58.is_empty()); + + // Verify we can get the bytes back + let hotkey_ref: &[u8; 32] = neuron.hotkey.as_ref(); + assert_eq!(hotkey_ref, &hotkey_bytes); + } + + #[test] + fn test_metagraph_has_required_fields() { + let metagraph = MockMetagraphBuilder::new(100) + .block(1000) + .version(2) + .add_validator(0, [1u8; 32], 500.0) + .build(); + + assert_eq!(metagraph.netuid, 100); + assert_eq!(metagraph.block, 1000); + assert_eq!(metagraph.version, 2); + assert!(metagraph.axons.is_empty()); // No axons by default + } + + #[test] + fn test_neuron_info_conversion() { + let data = MockNeuronBuilder::new(5) + .netuid(100) + .hotkey([1u8; 32]) + .stake_tao(1000.0) + .root_stake_tao(500.0) + .incentive(0.8) + .trust(0.7) + .consensus(0.9) + .validator_permit(true) + .active(true) + .build(); + + let neuron_info = data.to_neuron_info(); + + assert_eq!(neuron_info.uid, 5); + assert_eq!(neuron_info.netuid, 100); + assert_eq!(neuron_info.stake, 1_000_000_000_000); + assert_eq!(neuron_info.root_stake, 500_000_000_000); + assert!(neuron_info.validator_permit); + assert!(neuron_info.active); + assert!(!neuron_info.is_null); + } +} diff --git a/crates/bittensor-integration/src/tests.rs b/crates/bittensor-integration/src/tests.rs new file mode 100644 index 000000000..556484f8e --- /dev/null +++ b/crates/bittensor-integration/src/tests.rs @@ -0,0 +1,411 @@ +//! Tests for Bittensor integration + +#[cfg(test)] +mod mock_metagraph_tests { + use crate::mock::{ + create_test_metagraph, create_validators, hotkey_from_seed, MockMetagraphBuilder, + MockNeuronBuilder, + }; + use crate::validator_sync::{MetagraphValidator, ValidatorSync}; + use platform_core::{ChainState, Hotkey, Stake, ValidatorInfo}; + use sp_core::crypto::Ss58Codec; + use std::collections::HashSet; + use std::sync::Arc; + + #[test] + fn test_mock_metagraph_with_validators() { + let metagraph = MockMetagraphBuilder::new(100) + .block(1000) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(1000.0) + .root_stake_tao(500.0) + .incentive(0.8) + .trust(0.9) + .consensus(0.85) + .validator_permit(true) + .build(), + ) + .add_neuron( + MockNeuronBuilder::new(1) + .hotkey(hotkey_from_seed(1)) + .stake_tao(500.0) + .incentive(0.6) + .trust(0.7) + .consensus(0.65) + .validator_permit(true) + .build(), + ) + .add_neuron( + MockNeuronBuilder::new(2) + .hotkey(hotkey_from_seed(2)) + .stake(0) // Miner with no stake + .build(), + ) + .build(); + + assert_eq!(metagraph.netuid, 100); + assert_eq!(metagraph.block, 1000); + assert_eq!(metagraph.n, 3); + assert_eq!(metagraph.neurons.len(), 3); + + // Verify validator 0 + let v0 = metagraph.neurons.get(&0).expect("validator 0"); + assert_eq!(v0.stake, 1_000_000_000_000); // 1000 TAO + assert_eq!(v0.root_stake, 500_000_000_000); // 500 TAO + + // Verify validator 1 + let v1 = metagraph.neurons.get(&1).expect("validator 1"); + assert_eq!(v1.stake, 500_000_000_000); // 500 TAO + + // Verify miner has no stake + let miner = metagraph.neurons.get(&2).expect("miner"); + assert_eq!(miner.stake, 0); + } + + #[test] + fn test_mock_metagraph_hotkey_is_valid_ss58() { + let hotkey = hotkey_from_seed(42); + let metagraph = MockMetagraphBuilder::new(1) + .add_validator(0, hotkey, 100.0) + .build(); + + let neuron = metagraph.neurons.get(&0).expect("neuron"); + let ss58 = neuron.hotkey.to_ss58check(); + + // Should be a valid SS58 address + assert!(ss58.starts_with('5')); // Substrate addresses start with 5 + assert!(ss58.len() > 40); + } + + #[test] + fn test_create_validators_with_varying_stakes() { + let validators = create_validators(10, 100.0, 1000.0); + + assert_eq!(validators.len(), 10); + + for (i, v) in validators.iter().enumerate() { + assert_eq!(v.uid, i as u64); + let stake_tao = v.stake as f64 / 1_000_000_000.0; + assert!( + (100.0..=1000.0).contains(&stake_tao), + "Validator {} stake {} TAO out of range", + i, + stake_tao + ); + } + } + + #[test] + fn test_parse_metagraph_extracts_validators_above_min_stake() { + // Create metagraph with mix of high and low stake validators + let metagraph = MockMetagraphBuilder::new(100) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(2000.0) // Above min + .build(), + ) + .add_neuron( + MockNeuronBuilder::new(1) + .hotkey(hotkey_from_seed(1)) + .stake_tao(500.0) // Below min + .build(), + ) + .add_neuron( + MockNeuronBuilder::new(2) + .hotkey(hotkey_from_seed(2)) + .stake_tao(1500.0) // Above min + .build(), + ) + .build(); + + // Parse with min_stake of 1000 TAO + let min_stake: u64 = 1_000_000_000_000; // 1000 TAO in RAO + let mut above_min = Vec::new(); + + for (uid, neuron) in &metagraph.neurons { + let effective_stake = neuron.stake.saturating_add(neuron.root_stake); + let stake = effective_stake.min(u64::MAX as u128) as u64; + if stake >= min_stake { + above_min.push(*uid); + } + } + + assert_eq!(above_min.len(), 2); + assert!(above_min.contains(&0)); + assert!(above_min.contains(&2)); + assert!(!above_min.contains(&1)); + } + + #[test] + fn test_metagraph_iteration_for_validator_sync() { + let metagraph = create_test_metagraph(100, 5, 500.0); + + let mut validators = Vec::new(); + let min_stake: u64 = 100_000_000_000; // 100 TAO + + for (uid, neuron) in &metagraph.neurons { + let hotkey_bytes: &[u8; 32] = neuron.hotkey.as_ref(); + let hotkey = Hotkey(*hotkey_bytes); + + let effective_stake = neuron.stake.saturating_add(neuron.root_stake); + let stake = effective_stake.min(u64::MAX as u128) as u64; + + if stake >= min_stake { + let incentive = neuron.incentive / u16::MAX as f64; + let trust = neuron.trust / u16::MAX as f64; + let consensus = neuron.consensus / u16::MAX as f64; + + validators.push(MetagraphValidator { + hotkey, + uid: *uid as u16, + stake, + active: stake > 0, + incentive, + trust, + consensus, + }); + } + } + + assert_eq!(validators.len(), 5); + for v in &validators { + assert!(v.active); + assert!(v.stake >= min_stake); + } + } + + #[test] + fn test_mock_metagraph_supports_removing_validators() { + // Simulate metagraph state at block 1000 + let metagraph_t1 = MockMetagraphBuilder::new(100) + .block(1000) + .add_validator(0, hotkey_from_seed(0), 1000.0) + .add_validator(1, hotkey_from_seed(1), 800.0) + .add_validator(2, hotkey_from_seed(2), 600.0) + .build(); + + // Simulate metagraph state at block 2000 (validator 1 removed) + let metagraph_t2 = MockMetagraphBuilder::new(100) + .block(2000) + .add_validator(0, hotkey_from_seed(0), 1000.0) + .add_validator(2, hotkey_from_seed(2), 600.0) + .build(); + + assert_eq!(metagraph_t1.neurons.len(), 3); + assert_eq!(metagraph_t2.neurons.len(), 2); + + // Verify validator 1 was removed + assert!(metagraph_t1.neurons.contains_key(&1)); + assert!(!metagraph_t2.neurons.contains_key(&1)); + } + + #[test] + fn test_mock_metagraph_supports_stake_changes() { + // Initial state + let metagraph_t1 = MockMetagraphBuilder::new(100) + .block(1000) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(1000.0) + .build(), + ) + .build(); + + // After stake increase + let metagraph_t2 = MockMetagraphBuilder::new(100) + .block(2000) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(2000.0) // Stake doubled + .build(), + ) + .build(); + + let v0_t1 = metagraph_t1.neurons.get(&0).expect("validator at t1"); + let v0_t2 = metagraph_t2.neurons.get(&0).expect("validator at t2"); + + assert_eq!(v0_t1.stake, 1_000_000_000_000); + assert_eq!(v0_t2.stake, 2_000_000_000_000); + } + + #[test] + fn test_mock_metagraph_with_incentive_scores() { + let metagraph = MockMetagraphBuilder::new(100) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(1000.0) + .incentive(0.9) // 90% incentive + .trust(0.8) // 80% trust + .consensus(0.85) // 85% consensus + .build(), + ) + .build(); + + let neuron = metagraph.neurons.get(&0).expect("neuron"); + + // Extract normalized scores as ValidatorSync does + let incentive = neuron.incentive / u16::MAX as f64; + let trust = neuron.trust / u16::MAX as f64; + let consensus = neuron.consensus / u16::MAX as f64; + + assert!((incentive - 0.9).abs() < 0.001); + assert!((trust - 0.8).abs() < 0.001); + assert!((consensus - 0.85).abs() < 0.001); + } + + #[test] + fn test_hotkey_from_seed_is_deterministic_across_calls() { + let hotkey1 = hotkey_from_seed(12345); + let hotkey2 = hotkey_from_seed(12345); + + assert_eq!(hotkey1, hotkey2); + + // Build two metagraphs with same seed + let mg1 = MockMetagraphBuilder::new(1) + .add_validator(0, hotkey_from_seed(100), 500.0) + .build(); + let mg2 = MockMetagraphBuilder::new(1) + .add_validator(0, hotkey_from_seed(100), 500.0) + .build(); + + let n1 = mg1.neurons.get(&0).expect("neuron 1"); + let n2 = mg2.neurons.get(&0).expect("neuron 2"); + + // Hotkeys should match + let hk1: &[u8; 32] = n1.hotkey.as_ref(); + let hk2: &[u8; 32] = n2.hotkey.as_ref(); + assert_eq!(hk1, hk2); + } + + #[test] + fn test_effective_stake_combines_alpha_and_root() { + let metagraph = MockMetagraphBuilder::new(100) + .add_neuron( + MockNeuronBuilder::new(0) + .hotkey(hotkey_from_seed(0)) + .stake_tao(1000.0) // Alpha stake + .root_stake_tao(500.0) // Root stake + .build(), + ) + .build(); + + let neuron = metagraph.neurons.get(&0).expect("neuron"); + + let effective_stake = neuron.stake.saturating_add(neuron.root_stake); + let effective_tao = effective_stake as f64 / 1_000_000_000.0; + + assert!((effective_tao - 1500.0).abs() < 0.001); + } +} + +#[cfg(test)] +mod bittensor_tests { + use crate::{BittensorConfig, SubtensorClient, WeightSubmitter, DEFAULT_NETUID}; + use platform_challenge_sdk::WeightAssignment; + + #[test] + fn test_config_default() { + let config = BittensorConfig::default(); + assert_eq!(config.netuid, DEFAULT_NETUID); // NETUID 100 by default + assert!(config.use_commit_reveal); + assert!(config.endpoint.contains("finney")); + } + + #[test] + fn test_config_testnet() { + let config = BittensorConfig::testnet(42); + assert_eq!(config.netuid, 42); + assert!(config.endpoint.contains("test")); + } + + #[test] + fn test_config_local() { + let config = BittensorConfig::local(1); + assert_eq!(config.netuid, 1); + assert!(!config.use_commit_reveal); + assert!(config.endpoint.contains("127.0.0.1")); + } + + #[test] + fn test_client_creation() { + let config = BittensorConfig::local(1); + let client = SubtensorClient::new(config); + assert_eq!(client.netuid(), 1); + assert!(!client.use_commit_reveal()); + } + + #[test] + fn test_submitter_creation() { + let config = BittensorConfig::local(1); + let client = SubtensorClient::new(config); + let submitter = WeightSubmitter::new(client, None); + assert!(!submitter.has_pending_commit()); + } + + #[test] + fn test_normalize_to_u16() { + use crate::weights::normalize_to_u16; + + let weights = vec![0.5, 0.3, 0.2]; + let normalized = normalize_to_u16(&weights); + + // Should sum to ~65535 + let sum: u32 = normalized.iter().map(|&w| w as u32).sum(); + assert!(sum > 65000 && sum <= 65535); + + // First should be largest + assert!(normalized[0] > normalized[1]); + assert!(normalized[1] > normalized[2]); + } + + #[test] + fn test_normalize_to_u16_zero() { + use crate::weights::normalize_to_u16; + + let weights = vec![0.0, 0.0, 0.0]; + let normalized = normalize_to_u16(&weights); + + assert_eq!(normalized, vec![0, 0, 0]); + } + + #[test] + fn test_weight_assignment_conversion() { + // Test that WeightAssignment can be created + let assignment = WeightAssignment::new( + "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), + 0.5, + ); + assert_eq!(assignment.weight, 0.5); + } + + // Integration tests (require network) + #[tokio::test] + #[ignore] // Run with: cargo test -- --ignored + async fn test_connect_to_testnet() { + let config = BittensorConfig::testnet(1); + let mut client = SubtensorClient::new(config); + + let result = client.connect().await; + assert!(result.is_ok(), "Failed to connect to testnet"); + } + + #[tokio::test] + #[ignore] + async fn test_sync_metagraph() { + let config = BittensorConfig::testnet(1); + let mut client = SubtensorClient::new(config); + + client.connect().await.expect("Failed to connect"); + let metagraph = client.sync_metagraph().await; + + assert!(metagraph.is_ok(), "Failed to sync metagraph"); + let mg = metagraph.unwrap(); + assert!(mg.n > 0, "Metagraph should have neurons"); + } +} diff --git a/crates/bittensor-integration/src/validator_sync.rs b/crates/bittensor-integration/src/validator_sync.rs new file mode 100644 index 000000000..42a75e3ea --- /dev/null +++ b/crates/bittensor-integration/src/validator_sync.rs @@ -0,0 +1,460 @@ +//! Validator Sync from Bittensor Metagraph +//! +//! Automatically syncs validators from the Bittensor blockchain. +//! Validators join/leave based on their registration status. +//! Stake is proportional to their Bittensor stake (power). + +use crate::SubtensorClient; +use bittensor_rs::metagraph::Metagraph; +use parking_lot::RwLock; +use platform_core::{ChainState, Hotkey, Stake, ValidatorInfo}; +use std::collections::HashMap; +use std::sync::Arc; +use tokio::sync::Mutex as TokioMutex; +use tracing::{debug, info}; + +/// UID of the subnet owner (always the first registered neuron). +const SUBNET_OWNER_UID: u16 = 0; + +/// Validator info from Bittensor metagraph +#[derive(Clone, Debug)] +pub struct MetagraphValidator { + /// Hotkey (SS58 address converted to bytes) + pub hotkey: Hotkey, + /// UID on the subnet + pub uid: u16, + /// Stake in RAO (1 TAO = 1e9 RAO) + pub stake: u64, + /// Is validator active + pub active: bool, + /// Incentive score + pub incentive: f64, + /// Trust score + pub trust: f64, + /// Consensus score + pub consensus: f64, +} + +/// Validator sync manager +pub struct ValidatorSync { + /// Bittensor client (needs async lock for sync) + client: Arc>, + /// Netuid + netuid: u16, + /// Minimum stake to be considered a validator (in RAO) + min_stake: u64, + /// Last sync block + last_sync_block: u64, + /// Sync interval (blocks) + sync_interval: u64, +} + +impl ValidatorSync { + /// Create a new validator sync manager + pub fn new(client: Arc>, netuid: u16, min_stake: u64) -> Self { + Self { + client, + netuid, + min_stake, + last_sync_block: 0, + sync_interval: 100, // Sync every 100 blocks (~20 minutes) + } + } + + /// Set sync interval + pub fn with_sync_interval(mut self, blocks: u64) -> Self { + self.sync_interval = blocks; + self + } + + /// Check if sync is needed + pub fn needs_sync(&self, current_block: u64) -> bool { + current_block >= self.last_sync_block + self.sync_interval + } + + /// Sync validators from Bittensor metagraph + /// Pass banned_validators set to skip banned validators + pub async fn sync( + &mut self, + state: &Arc>, + banned_validators: Option<&std::collections::HashSet>, + ) -> Result { + info!( + "Syncing validators from Bittensor metagraph (netuid={})", + self.netuid + ); + + // Get metagraph data from Bittensor + let mut client = self.client.lock().await; + let metagraph = client + .sync_metagraph() + .await + .map_err(|e| SyncError::ClientError(e.to_string()))?; + + // Parse validators and all hotkeys from metagraph + let (bt_validators, all_hotkeys) = self.parse_metagraph(metagraph)?; + + // Extract subnet owner hotkey before dropping client borrow + let uid0_hotkey = metagraph + .neurons + .get(&(SUBNET_OWNER_UID as u64)) + .map(|neuron| { + let hotkey_bytes: &[u8; 32] = neuron.hotkey.as_ref(); + Hotkey(*hotkey_bytes) + }); + + drop(client); // Release lock + + // Update registered hotkeys in state (all miners + validators) + { + let mut state_guard = state.write(); + state_guard.registered_hotkeys = all_hotkeys; + } + + // Update state with validators + let result = self.update_state(state, bt_validators, banned_validators); + + // Resolve subnet owner from UID 0 + if let Some(hotkey) = uid0_hotkey { + let mut state_guard = state.write(); + state_guard.sudo_key = hotkey; + debug!("Subnet owner set to UID 0 hotkey: {}", state_guard.sudo_key); + } + + // Update last sync block + self.last_sync_block = state.read().block_height; + + info!( + "Validator sync complete: {} added, {} removed, {} updated, {} skipped (banned)", + result.added, result.removed, result.updated, result.skipped_banned + ); + + Ok(result) + } + + /// Parse metagraph data to extract validators and all registered hotkeys + fn parse_metagraph( + &self, + metagraph: &Metagraph, + ) -> Result<(Vec, std::collections::HashSet), SyncError> { + let mut validators = Vec::new(); + let mut all_hotkeys = std::collections::HashSet::new(); + + // Parse neurons from metagraph + for (uid, neuron) in &metagraph.neurons { + // Convert AccountId32 hotkey to our Hotkey type + let hotkey_bytes: &[u8; 32] = neuron.hotkey.as_ref(); + let hotkey = Hotkey(*hotkey_bytes); + + // Add ALL hotkeys to the registered set (miners + validators) + all_hotkeys.insert(hotkey.clone()); + + // Get effective stake: alpha stake + root stake (TAO on root subnet) + // This matches how Bittensor calculates validator weight + let alpha_stake = neuron.stake; + let root_stake = neuron.root_stake; + let effective_stake = alpha_stake.saturating_add(root_stake); + let stake = effective_stake.min(u64::MAX as u128) as u64; + + // Only add to validators if above minimum stake + if stake >= self.min_stake { + // Extract normalized scores (u16 -> f64, divide by u16::MAX) + let incentive = neuron.incentive / u16::MAX as f64; + let trust = neuron.trust / u16::MAX as f64; + let consensus = neuron.consensus / u16::MAX as f64; + + // Check if active (has stake) + let active = stake > 0; + + validators.push(MetagraphValidator { + hotkey, + uid: *uid as u16, + stake, + active, + incentive, + trust, + consensus, + }); + } + } + + debug!( + "Parsed {} validators and {} total hotkeys from metagraph", + validators.len(), + all_hotkeys.len() + ); + Ok((validators, all_hotkeys)) + } + + /// Update chain state with validators from Bittensor + fn update_state( + &self, + state: &Arc>, + bt_validators: Vec, + banned_validators: Option<&std::collections::HashSet>, + ) -> SyncResult { + let mut state = state.write(); + let mut result = SyncResult::default(); + + // Create map of Bittensor validators + let bt_map: HashMap = bt_validators + .into_iter() + .map(|v| (v.hotkey.clone(), v)) + .collect(); + + // Remove validators not in Bittensor metagraph + let current_hotkeys: Vec = state.validators.keys().cloned().collect(); + for hotkey in current_hotkeys { + if !bt_map.contains_key(&hotkey) && !state.is_sudo(&hotkey) { + state.validators.remove(&hotkey); + result.removed += 1; + debug!("Removed validator not in metagraph: {}", hotkey); + } + } + + // Add/update validators from Bittensor + for (hotkey, bt_val) in bt_map { + // Skip banned validators + if let Some(banned) = banned_validators { + if banned.contains(&hotkey.to_hex()) { + result.skipped_banned += 1; + debug!("Skipping banned validator: {}", hotkey); + continue; + } + } + + if let Some(existing) = state.validators.get_mut(&hotkey) { + // Update stake if changed + let new_stake = Stake::new(bt_val.stake); + if existing.stake != new_stake { + existing.stake = new_stake; + existing.is_active = bt_val.active; + result.updated += 1; + debug!("Updated validator stake: {} -> {}", hotkey, bt_val.stake); + } + } else { + // Add new validator + let info = ValidatorInfo::new(hotkey.clone(), Stake::new(bt_val.stake)); + if state.add_validator(info).is_ok() { + result.added += 1; + debug!( + "Added validator from metagraph: {} (stake={})", + hotkey, bt_val.stake + ); + } + } + } + + result.total = state.validators.len(); + result + } + + /// Get current sync status + pub fn status(&self) -> SyncStatus { + SyncStatus { + last_sync_block: self.last_sync_block, + sync_interval: self.sync_interval, + netuid: self.netuid, + min_stake: self.min_stake, + } + } +} + +/// Sync result +#[derive(Clone, Debug, Default)] +pub struct SyncResult { + /// Validators added + pub added: usize, + /// Validators removed + pub removed: usize, + /// Validators updated (stake changed) + pub updated: usize, + /// Validators skipped (banned) + pub skipped_banned: usize, + /// Total validators after sync + pub total: usize, +} + +/// Sync status +#[derive(Clone, Debug)] +pub struct SyncStatus { + pub last_sync_block: u64, + pub sync_interval: u64, + pub netuid: u16, + pub min_stake: u64, +} + +/// Sync error +#[derive(Debug, thiserror::Error)] +pub enum SyncError { + #[error("Client error: {0}")] + ClientError(String), + + #[error("Parse error: {0}")] + ParseError(String), + + #[error("State error: {0}")] + StateError(String), +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::BittensorConfig; + use platform_core::{Keypair, NetworkConfig, Stake}; + use std::sync::Arc; + use tokio::sync::Mutex as TokioMutex; + + #[test] + fn test_sync_result() { + let result = SyncResult { + added: 5, + removed: 2, + updated: 3, + skipped_banned: 1, + total: 10, + }; + + assert_eq!(result.added, 5); + assert_eq!(result.skipped_banned, 1); + assert_eq!(result.total, 10); + } + + #[test] + fn test_sync_status() { + let status = SyncStatus { + last_sync_block: 1000, + sync_interval: 100, + netuid: 1, + min_stake: 1_000_000_000, + }; + + assert_eq!(status.netuid, 1); + } + + #[test] + fn test_validator_sync_needs_sync_respects_interval() { + let client = Arc::new(TokioMutex::new(SubtensorClient::new( + BittensorConfig::local(1), + ))); + let sync = ValidatorSync::new(client, 1, 1_000_000_000); + + assert!(!sync.needs_sync(50)); + assert!(sync.needs_sync(100)); + assert!(sync.needs_sync(150)); + } + + #[test] + fn test_validator_sync_with_sync_interval_customizes_threshold() { + let client = Arc::new(TokioMutex::new(SubtensorClient::new( + BittensorConfig::local(1), + ))); + let sync = ValidatorSync::new(client, 1, 1_000_000_000).with_sync_interval(10); + + assert!(!sync.needs_sync(9)); + assert!(sync.needs_sync(10)); + } + + fn sample_metagraph_validator(hotkey: Hotkey, stake: u64) -> MetagraphValidator { + MetagraphValidator { + hotkey, + uid: 1, + stake, + active: true, + incentive: 0.5, + trust: 0.5, + consensus: 0.5, + } + } + + #[test] + fn test_update_state_skips_banned_validators() { + let client = Arc::new(TokioMutex::new(SubtensorClient::new( + BittensorConfig::local(1), + ))); + let sync = ValidatorSync::new(client, 1, 1_000_000_000); + let state = Arc::new(RwLock::new(ChainState::default())); + + let hotkey = Hotkey([7u8; 32]); + let banned = std::collections::HashSet::from([hotkey.to_hex()]); + let validators = vec![sample_metagraph_validator(hotkey.clone(), 2_000_000_000)]; + + let result = sync.update_state(&state, validators, Some(&banned)); + + assert_eq!(result.added, 0); + assert_eq!(result.skipped_banned, 1); + assert!(state.read().validators.is_empty()); + } + + #[test] + fn test_update_state_removes_and_updates_validators() { + let client = Arc::new(TokioMutex::new(SubtensorClient::new( + BittensorConfig::local(1), + ))); + let sync = ValidatorSync::new(client, 1, 1_000_000_000); + let state = Arc::new(RwLock::new(ChainState::default())); + + let keep_hotkey = Hotkey([1u8; 32]); + let remove_hotkey = Hotkey([2u8; 32]); + + { + let mut guard = state.write(); + guard + .add_validator(ValidatorInfo::new( + keep_hotkey.clone(), + Stake::new(1_500_000_000), + )) + .unwrap(); + guard + .add_validator(ValidatorInfo::new( + remove_hotkey.clone(), + Stake::new(1_500_000_000), + )) + .unwrap(); + } + + let bt_validators = vec![MetagraphValidator { + hotkey: keep_hotkey.clone(), + uid: 9, + stake: 3_000_000_000, + active: false, + incentive: 0.4, + trust: 0.3, + consensus: 0.2, + }]; + + let result = sync.update_state(&state, bt_validators, None); + + assert_eq!(result.removed, 1); + assert_eq!(result.updated, 1); + assert_eq!(result.total, 1); + + let guard = state.read(); + assert!(!guard.validators.contains_key(&remove_hotkey)); + let updated = guard.validators.get(&keep_hotkey).unwrap(); + assert_eq!(updated.stake, Stake::new(3_000_000_000)); + assert!(!updated.is_active); + } + + #[test] + fn test_update_state_adds_new_validator() { + let client = Arc::new(TokioMutex::new(SubtensorClient::new( + BittensorConfig::local(1), + ))); + let sync = ValidatorSync::new(client, 1, 1_000_000_000); + let state = Arc::new(RwLock::new(ChainState::default())); + + let new_hotkey = Hotkey([9u8; 32]); + let validators = vec![sample_metagraph_validator( + new_hotkey.clone(), + 2_500_000_000, + )]; + + let result = sync.update_state(&state, validators, None); + + assert_eq!(result.added, 1); + assert_eq!(result.total, 1); + let guard = state.read(); + assert!(guard.validators.contains_key(&new_hotkey)); + } +} diff --git a/crates/bittensor-integration/src/weights.rs b/crates/bittensor-integration/src/weights.rs new file mode 100644 index 000000000..8904ae9c8 --- /dev/null +++ b/crates/bittensor-integration/src/weights.rs @@ -0,0 +1,1432 @@ +//! Weight submission to Bittensor +//! +//! This module handles submitting weights to the Bittensor network using the +//! commit-reveal pattern that matches subtensor's exact format. +//! +//! ## CRv4 Support +//! When commit_reveal_version == 4, uses timelock encryption (TLE) for automatic +//! on-chain reveal. The chain decrypts weights when DRAND pulse becomes available. +//! +//! ## Persistence +//! Commits are persisted to disk to survive restarts. This ensures that if +//! the validator restarts between commit and reveal, it can still reveal +//! the previously committed weights. + +use crate::SubtensorClient; +use anyhow::Result; +use bittensor_rs::chain::ExtrinsicWait; +use bittensor_rs::validator_weights::{ + commit_weights, prepare_commit_reveal, prepare_mechanism_commit_reveal, reveal_weights, + set_weights, +}; +use bittensor_rs::{ + commit_mechanism_weights, get_next_epoch_start_block, reveal_mechanism_weights, + set_mechanism_weights, +}; +// CRv4 imports (no persistence needed - chain auto-reveals) +use bittensor_rs::crv4::{ + calculate_reveal_round, commit_timelocked_mechanism_weights, get_commit_reveal_version, + get_last_drand_round, get_mechid_storage_index, get_reveal_period, get_tempo, + prepare_crv4_commit, DEFAULT_COMMIT_REVEAL_VERSION, +}; +use platform_challenge_sdk::WeightAssignment; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::path::PathBuf; +use tracing::{debug, error, info, warn}; + +/// Default path for commit persistence +const DEFAULT_COMMITS_FILE: &str = "pending_commits.json"; + +/// Persisted state for weight commits +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +pub struct PersistedCommitState { + /// Epoch when commits were made + pub committed_epoch: Option, + /// Pending mechanism commits (mechanism_id -> commit data) + pub pending_mechanism_commits: HashMap, + /// Standard pending commit (non-mechanism) + pub pending_commit: Option, + /// Last revealed epoch per mechanism + pub last_revealed_epoch: HashMap, +} + +impl PersistedCommitState { + /// Load from file, returning default if file doesn't exist + pub fn load(path: &PathBuf) -> Self { + match std::fs::read_to_string(path) { + Ok(content) => match serde_json::from_str(&content) { + Ok(state) => { + info!("Loaded persisted commit state from {:?}", path); + state + } + Err(e) => { + warn!("Failed to parse commit state file: {}", e); + Self::default() + } + }, + Err(_) => { + debug!("No existing commit state file at {:?}", path); + Self::default() + } + } + } + + /// Save to file + pub fn save(&self, path: &PathBuf) -> Result<()> { + let content = serde_json::to_string_pretty(self)?; + std::fs::write(path, content)?; + debug!("Saved commit state to {:?}", path); + Ok(()) + } + + /// Check if we have commits for a specific epoch + pub fn has_commits_for_epoch(&self, epoch: u64) -> bool { + self.committed_epoch == Some(epoch) && !self.pending_mechanism_commits.is_empty() + } + + /// Check if we already revealed for this epoch + pub fn has_revealed_for_epoch(&self, mechanism_id: u8, epoch: u64) -> bool { + self.last_revealed_epoch + .get(&mechanism_id) + .map(|e| *e >= epoch) + .unwrap_or(false) + } + + /// Clear old state for new epoch + pub fn new_epoch(&mut self, epoch: u64) { + // Keep pending commits if they haven't been revealed yet + // Clear the committed_epoch to allow new commits + if self.committed_epoch != Some(epoch) { + // New epoch - clear old unrevealed commits (they're now invalid) + if !self.pending_mechanism_commits.is_empty() { + warn!( + "Clearing {} unrevealed commits from previous epoch", + self.pending_mechanism_commits.len() + ); + self.pending_mechanism_commits.clear(); + } + self.pending_commit = None; + } + } +} + +/// Weight submission manager with persistence +pub struct WeightSubmitter { + client: SubtensorClient, + /// Persisted commit state (for hash-based commit-reveal v2/v3) + state: PersistedCommitState, + /// Path to persistence file + persist_path: PathBuf, + /// Current epoch (updated externally) + current_epoch: u64, + /// Cached commit-reveal version from chain + cached_crv_version: Option, +} + +/// Pending mechanism commit data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PendingMechanismCommit { + pub mechanism_id: u8, + pub hash: String, + pub uids: Vec, + pub weights: Vec, + /// Salt stored as hex string to avoid JSON serialization issues with u16 + pub salt_hex: String, + pub version_key: u64, + pub epoch: u64, +} + +impl PendingMechanismCommit { + /// Get salt as Vec from hex storage + pub fn get_salt(&self) -> Vec { + // Decode hex to bytes, then convert pairs of bytes to u16 (little-endian) + let bytes = hex::decode(&self.salt_hex).unwrap_or_default(); + bytes + .chunks(2) + .map(|chunk| { + if chunk.len() == 2 { + u16::from_le_bytes([chunk[0], chunk[1]]) + } else { + chunk[0] as u16 + } + }) + .collect() + } + + /// Create salt_hex from Vec + pub fn salt_to_hex(salt: &[u16]) -> String { + // Convert each u16 to 2 bytes (little-endian) and hex encode + let bytes: Vec = salt.iter().flat_map(|s| s.to_le_bytes()).collect(); + hex::encode(bytes) + } +} + +/// Pending commit data using subtensor-compatible format (v2) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PendingCommitV2 { + pub hash: String, + pub uids: Vec, + pub weights: Vec, + /// Salt stored as hex string to avoid JSON serialization issues + pub salt_hex: String, + pub version_key: u64, + pub epoch: u64, +} + +impl PendingCommitV2 { + /// Get salt as Vec from hex storage + pub fn get_salt(&self) -> Vec { + let bytes = hex::decode(&self.salt_hex).unwrap_or_default(); + bytes + .chunks(2) + .map(|chunk| { + if chunk.len() == 2 { + u16::from_le_bytes([chunk[0], chunk[1]]) + } else { + chunk[0] as u16 + } + }) + .collect() + } + + /// Create salt_hex from Vec + pub fn salt_to_hex(salt: &[u16]) -> String { + let bytes: Vec = salt.iter().flat_map(|s| s.to_le_bytes()).collect(); + hex::encode(bytes) + } +} + +impl WeightSubmitter { + /// Create a new weight submitter with persistence + pub fn new(client: SubtensorClient, data_dir: Option) -> Self { + let persist_path = data_dir + .unwrap_or_else(|| PathBuf::from(".")) + .join(DEFAULT_COMMITS_FILE); + + let state = PersistedCommitState::load(&persist_path); + + if !state.pending_mechanism_commits.is_empty() { + info!( + "Loaded {} pending commits from previous session (epoch {:?})", + state.pending_mechanism_commits.len(), + state.committed_epoch + ); + } + + Self { + client, + state, + persist_path, + current_epoch: 0, + cached_crv_version: None, + } + } + + /// Get commit-reveal version from chain (cached) + pub async fn get_crv_version(&mut self) -> Result { + if let Some(v) = self.cached_crv_version { + return Ok(v); + } + + let client = self.client.client()?; + let version = get_commit_reveal_version(client) + .await + .unwrap_or(DEFAULT_COMMIT_REVEAL_VERSION); + self.cached_crv_version = Some(version); + info!("Commit-reveal version from chain: {}", version); + Ok(version) + } + + /// Check if CRv4 (timelock encryption) is enabled + pub async fn is_crv4_enabled(&mut self) -> bool { + self.get_crv_version().await.unwrap_or(0) >= 4 + } + + /// Get mutable access to the client + pub fn client_mut(&mut self) -> &mut SubtensorClient { + &mut self.client + } + + /// Update current epoch and handle epoch transition + pub fn set_epoch(&mut self, epoch: u64) { + if epoch != self.current_epoch { + info!( + "Weight submitter epoch update: {} -> {}", + self.current_epoch, epoch + ); + self.current_epoch = epoch; + self.state.new_epoch(epoch); + if let Err(e) = self.state.save(&self.persist_path) { + error!("Failed to save commit state: {}", e); + } + } + } + + /// Check if we already committed for current epoch + pub fn has_committed_for_epoch(&self, epoch: u64) -> bool { + self.state.has_commits_for_epoch(epoch) + } + + /// Save state to disk + fn persist(&self) { + if let Err(e) = self.state.save(&self.persist_path) { + error!("Failed to persist commit state: {}", e); + } + } + + /// Submit weights to Bittensor + /// + /// If commit-reveal is enabled: + /// 1. First call commits the hash + /// 2. Second call reveals the weights (after tempo blocks) + /// + /// If not: + /// - Directly calls set_weights + pub async fn submit_weights(&mut self, weights: &[WeightAssignment]) -> Result { + if self.client.use_commit_reveal() { + self.submit_with_commit_reveal(weights).await + } else { + self.submit_direct(weights).await + } + } + + /// Direct weight submission (no commit-reveal) + async fn submit_direct(&self, weights: &[WeightAssignment]) -> Result { + let (uids, weight_values) = self.prepare_weights(weights)?; + + if uids.is_empty() { + return Err(anyhow::anyhow!("No valid UIDs found for weights")); + } + + // Convert u16 weights to f32 for set_weights + let weight_f32: Vec = weight_values.iter().map(|w| *w as f32 / 65535.0).collect(); + + info!("Submitting {} weights directly", uids.len()); + + let tx_hash = set_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + &uids, + &weight_f32, + Some(self.client.version_key()), + ExtrinsicWait::Finalized, + ) + .await?; + + info!("Weights submitted: {}", tx_hash); + Ok(tx_hash) + } + + /// Submit with commit-reveal pattern (v2 - subtensor compatible) + async fn submit_with_commit_reveal(&mut self, weights: &[WeightAssignment]) -> Result { + // If we have a pending commit, reveal it + if let Some(pending) = self.state.pending_commit.take() { + self.persist(); + return self.reveal_pending_v2(pending).await; + } + + // Otherwise, create new commit using v2 format + let (uids, weight_values) = self.prepare_weights(weights)?; + + if uids.is_empty() { + return Err(anyhow::anyhow!("No valid UIDs found for weights")); + } + + // Convert to u16 for subtensor + let uids_u16: Vec = uids.iter().map(|u| *u as u16).collect(); + + // Get account public key for hash + let account = self.client.signer()?.account_id().0; + let version_key = self.client.version_key(); + + // Generate commit using v2 format (subtensor compatible) + let commit_data = prepare_commit_reveal( + &account, + self.client.netuid(), + &uids_u16, + &weight_values, + version_key, + 8, // salt length + ); + + info!("Committing weights hash (v2): {}", commit_data.commit_hash); + + let tx_hash = commit_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + &commit_data.commit_hash, + ExtrinsicWait::Finalized, + ) + .await?; + + // Store pending commit for reveal (salt as hex to avoid serialization issues) + self.state.pending_commit = Some(PendingCommitV2 { + hash: commit_data.commit_hash, + uids: commit_data.uids, + weights: commit_data.weights, + salt_hex: PendingCommitV2::salt_to_hex(&commit_data.salt), + version_key: commit_data.version_key, + epoch: self.current_epoch, + }); + self.persist(); + + info!("Weights committed: {}", tx_hash); + Ok(tx_hash) + } + + /// Reveal pending commit (v2 format) + async fn reveal_pending_v2(&mut self, pending: PendingCommitV2) -> Result { + info!("Revealing weights for commit: {}", pending.hash); + + // Convert uids to u64 for reveal_weights API + let uids_u64: Vec = pending.uids.iter().map(|u| *u as u64).collect(); + let salt = pending.get_salt(); + + debug!( + "Revealing: uids={:?}, weights={:?}, salt_hex={}, salt={:?}", + pending.uids, pending.weights, pending.salt_hex, salt + ); + + let tx_hash = reveal_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + &uids_u64, + &pending.weights, + &salt, // Now correctly passing &[u16] + pending.version_key, + ExtrinsicWait::Finalized, + ) + .await?; + + info!("Weights revealed: {}", tx_hash); + Ok(tx_hash) + } + + /// Check if we have a pending commit to reveal + pub fn has_pending_commit(&self) -> bool { + self.state.pending_commit.is_some() + } + + /// Prepare weights for submission + /// Converts WeightAssignment to (UIDs, normalized u16 weights) + fn prepare_weights(&self, weights: &[WeightAssignment]) -> Result<(Vec, Vec)> { + // Get hotkeys from weights + let hotkeys: Vec = weights.iter().map(|w| w.hotkey.clone()).collect(); + + // Lookup UIDs for hotkeys from cached metagraph + let uid_map = self.client.get_uids_for_hotkeys(&hotkeys); + + let mut uids = Vec::new(); + let mut weight_values = Vec::new(); + + for weight in weights { + if let Some((_, uid)) = uid_map.iter().find(|(h, _)| h == &weight.hotkey) { + uids.push(*uid as u64); + // Convert 0-1 weight to u16 (0-65535) + let w_u16 = (weight.weight.clamp(0.0, 1.0) * 65535.0) as u16; + weight_values.push(w_u16); + } else { + warn!("No UID found for hotkey: {}", weight.hotkey); + } + } + + debug!( + "Prepared {} weights from {} assignments", + uids.len(), + weights.len() + ); + Ok((uids, weight_values)) + } + + /// Force reveal without new commit (if we have pending) + pub async fn force_reveal(&mut self) -> Result> { + if let Some(pending) = self.state.pending_commit.take() { + self.persist(); + let tx = self.reveal_pending_v2(pending).await?; + Ok(Some(tx)) + } else { + Ok(None) + } + } + + /// Submit weights for multiple mechanisms in a single batch transaction + /// This is used at epoch end to submit all mechanism weights at once. + /// + /// mechanism_weights: Vec<(mechanism_id, uids, weights)> + /// + /// Automatically selects the appropriate method: + /// - CRv4: Uses timelock encryption (chain auto-reveals) + /// - CRv2/v3: Uses hash-based commit-reveal (needs manual reveal) + /// - Direct: No commit-reveal + pub async fn submit_mechanism_weights_batch( + &mut self, + mechanism_weights: &[(u8, Vec, Vec)], + ) -> Result { + if mechanism_weights.is_empty() { + return Err(anyhow::anyhow!("No mechanism weights to submit")); + } + + // Check commit-reveal mode + if !self.client.use_commit_reveal() { + return self + .submit_mechanism_weights_batch_direct(mechanism_weights) + .await; + } + + // Check CRv4 (timelock encryption) + let crv_version = self.get_crv_version().await.unwrap_or(0); + if crv_version >= 4 { + info!("Using CRv4 (timelock encryption) for weight submission"); + return self + .submit_mechanism_weights_batch_crv4(mechanism_weights) + .await; + } + + // Fall back to hash-based commit-reveal + self.submit_mechanism_weights_batch_commit_reveal(mechanism_weights) + .await + } + + /// Submit mechanism weights using CRv4 (timelock encryption) + /// No manual reveal needed - chain decrypts automatically when DRAND pulse arrives + async fn submit_mechanism_weights_batch_crv4( + &mut self, + mechanism_weights: &[(u8, Vec, Vec)], + ) -> Result { + let client = self.client.client()?; + let signer = self.client.signer()?; + let netuid = self.client.netuid(); + let version_key = self.client.version_key(); + let hotkey_bytes = signer.account_id().0.to_vec(); + + // Get chain parameters for reveal round calculation + let current_block = client.block_number().await?; + let tempo = get_tempo(client, netuid).await.unwrap_or(360); + let reveal_period = get_reveal_period(client, netuid).await.unwrap_or(1); + let block_time = 12.0; // Standard Bittensor block time + let crv_version = self + .cached_crv_version + .unwrap_or(DEFAULT_COMMIT_REVEAL_VERSION); + + // Get chain's last DRAND round (CRITICAL: must use chain state, not system time) + let chain_last_drand_round = get_last_drand_round(client).await?; + + let mut last_tx = String::new(); + let mut committed_count = 0; + + for (mechanism_id, uids, weights) in mechanism_weights { + // Calculate reveal round for this mechanism (relative to chain's DRAND state) + let storage_index = get_mechid_storage_index(netuid, *mechanism_id); + let reveal_round = calculate_reveal_round( + tempo, + current_block, + storage_index, + reveal_period, + block_time, + chain_last_drand_round, + ); + + // Encrypt payload using TLE + let encrypted = + prepare_crv4_commit(&hotkey_bytes, uids, weights, version_key, reveal_round)?; + + info!( + "CRv4 committing mechanism {}: {} uids, chain_last_drand={}, reveal_round={}", + mechanism_id, + uids.len(), + chain_last_drand_round, + reveal_round + ); + + // Submit timelocked commit - no persistence needed, chain auto-reveals + let tx_hash = commit_timelocked_mechanism_weights( + client, + signer, + netuid, + *mechanism_id, + &encrypted, + reveal_round, + crv_version, + ExtrinsicWait::Finalized, + ) + .await?; + + info!( + "CRv4 mechanism {} committed: {} (auto-reveal at round {})", + mechanism_id, tx_hash, reveal_round + ); + + last_tx = tx_hash; + committed_count += 1; + } + + info!( + "CRv4 batch complete: {} mechanisms committed (no reveal needed)", + committed_count + ); + Ok(last_tx) + } + + /// Submit mechanism weights directly (without commit-reveal) + async fn submit_mechanism_weights_batch_direct( + &mut self, + mechanism_weights: &[(u8, Vec, Vec)], + ) -> Result { + use bittensor_rs::validator::utility::batch_set_mechanism_weights; + + let weights_for_batch: Vec<(u8, Vec, Vec)> = mechanism_weights.to_vec(); + + info!( + "Batch submitting weights directly for {} mechanisms", + weights_for_batch.len() + ); + + let tx_hash = batch_set_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + weights_for_batch, + self.client.version_key(), + ExtrinsicWait::Finalized, + ) + .await?; + + info!("Batch mechanism weights submitted: {}", tx_hash); + Ok(tx_hash) + } + + /// Submit mechanism weights using commit-reveal pattern + async fn submit_mechanism_weights_batch_commit_reveal( + &mut self, + mechanism_weights: &[(u8, Vec, Vec)], + ) -> Result { + use bittensor_rs::commit_hash_to_hex; + use bittensor_rs::generate_mechanism_commit_hash; + use bittensor_rs::generate_salt; + use bittensor_rs::validator::utility::{batch_all, BatchCall}; + + let account = self.client.signer()?.account_id().0; + let netuid = self.client.netuid(); + let version_key = self.client.version_key(); + + // Generate commits for all mechanisms + let mut batch_calls = Vec::new(); + let mut pending_commits = Vec::new(); + + for (mechanism_id, uids, weights) in mechanism_weights { + // Generate salt (8 u16 values) + let salt = generate_salt(8); + let salt_hex = PendingMechanismCommit::salt_to_hex(&salt); + + debug!( + "Commit mechanism {}: uids={:?}, weights={:?}, salt={:?}, salt_hex={}, version_key={}", + mechanism_id, uids, weights, salt, salt_hex, version_key + ); + + // Generate commit hash + let commit_hash = generate_mechanism_commit_hash( + &account, + netuid, + *mechanism_id, + uids, + weights, + &salt, + version_key, + ); + + let commit_hash_hex = commit_hash_to_hex(&commit_hash); + info!( + "Generated commit for mechanism {}: {} (salt_hex={})", + mechanism_id, + &commit_hash_hex[..16], + &salt_hex[..16] + ); + + // Add to batch + batch_calls.push(BatchCall::commit_mechanism_weights( + netuid, + *mechanism_id, + &commit_hash, + )); + + // Store pending commit for later reveal (salt as hex to avoid serialization issues) + pending_commits.push(PendingMechanismCommit { + mechanism_id: *mechanism_id, + hash: commit_hash_hex, + uids: uids.clone(), + weights: weights.clone(), + salt_hex, // Already computed above + version_key, + epoch: self.current_epoch, + }); + } + + info!( + "Batch committing weights for {} mechanisms", + batch_calls.len() + ); + + // Submit all commits in one batch transaction + let tx_hash = batch_all( + self.client.client()?, + self.client.signer()?, + batch_calls, + ExtrinsicWait::Finalized, + ) + .await?; + + // Store pending commits for reveal and persist + for pending in pending_commits { + self.state + .pending_mechanism_commits + .insert(pending.mechanism_id, pending); + } + self.state.committed_epoch = Some(self.current_epoch); + self.persist(); + + info!( + "Batch mechanism commits submitted: {} (reveals pending, epoch {})", + tx_hash, self.current_epoch + ); + Ok(tx_hash) + } + + /// Reveal all pending mechanism commits + pub async fn reveal_pending_mechanism_commits(&mut self) -> Result> { + use bittensor_rs::reveal_mechanism_weights; + + if self.state.pending_mechanism_commits.is_empty() { + return Ok(None); + } + + let pending: Vec<_> = self.state.pending_mechanism_commits.drain().collect(); + self.persist(); + + info!("Revealing {} pending mechanism commits", pending.len()); + + // Reveal each mechanism's weights + // Batch reveal via mechanism weights API + let mut last_tx = String::new(); + let mut revealed_mechanisms = Vec::new(); + + for (_, commit) in pending { + let mechanism_id = commit.mechanism_id; + let epoch = commit.epoch; + let salt = commit.get_salt(); + + debug!( + "Revealing mechanism {}: uids={:?}, weights={:?}, salt_hex={}, salt={:?}, version_key={}", + mechanism_id, commit.uids, commit.weights, commit.salt_hex, salt, commit.version_key + ); + + let tx_hash = reveal_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + mechanism_id, + &commit.uids, + &commit.weights, + &salt, + commit.version_key, + ExtrinsicWait::Finalized, + ) + .await?; + + info!( + "Revealed mechanism {} weights: {} (epoch {})", + mechanism_id, tx_hash, epoch + ); + revealed_mechanisms.push((mechanism_id, epoch)); + last_tx = tx_hash; + } + + // Track revealed epochs + for (mechanism_id, epoch) in revealed_mechanisms { + self.state.last_revealed_epoch.insert(mechanism_id, epoch); + } + self.persist(); + + Ok(Some(last_tx)) + } + + /// Check if there are pending mechanism commits to reveal + pub fn has_pending_mechanism_commits(&self) -> bool { + !self.state.pending_mechanism_commits.is_empty() + } + + /// Get pending commit info for logging + pub fn pending_commits_info(&self) -> String { + if self.state.pending_mechanism_commits.is_empty() { + "none".to_string() + } else { + let ids: Vec<_> = self.state.pending_mechanism_commits.keys().collect(); + format!( + "{} mechanisms {:?} (epoch {:?})", + ids.len(), + ids, + self.state.committed_epoch + ) + } + } +} + +/// Utility to convert f64 weights to normalized u16 +pub fn normalize_to_u16(weights: &[f64]) -> Vec { + let sum: f64 = weights.iter().sum(); + if sum == 0.0 { + return vec![0; weights.len()]; + } + + weights + .iter() + .map(|w| ((w / sum) * 65535.0) as u16) + .collect() +} + +/// Mechanism weight manager for handling per-challenge weights +pub struct MechanismWeightManager { + client: SubtensorClient, + /// Track last epoch we set weights for each mechanism + last_weight_epoch: HashMap, + /// Pending mechanism commits (mechanism_id -> PendingMechanismCommitV2) + pending_mechanism_commits: HashMap, +} + +fn convert_assignments_with_lookup( + weights: &[WeightAssignment], + uid_lookup: &HashMap, +) -> (Vec, Vec, Vec) { + let mut uids = Vec::new(); + let mut weight_values = Vec::new(); + let mut unresolved = Vec::new(); + + for assignment in weights { + if let Some(uid) = uid_lookup.get(&assignment.hotkey) { + uids.push(*uid as u64); + let w_u16 = (assignment.weight.clamp(0.0, 1.0) * 65535.0) as u16; + weight_values.push(w_u16); + } else { + unresolved.push(assignment.hotkey.clone()); + } + } + + (uids, weight_values, unresolved) +} + +fn fill_with_burn(mut uids: Vec, mut weight_values: Vec) -> (Vec, Vec) { + const TARGET_SUM: u32 = 65535; + + if uids.is_empty() { + info!("No valid weights, defaulting to UID 0"); + return (vec![0], vec![TARGET_SUM as u16]); + } + + let sum: u32 = weight_values.iter().map(|w| *w as u32).sum(); + if sum < TARGET_SUM { + let remaining = (TARGET_SUM - sum) as u16; + if remaining > 0 { + if let Some(pos) = uids.iter().position(|u| *u == 0) { + weight_values[pos] = weight_values[pos].saturating_add(remaining); + } else { + uids.push(0); + weight_values.push(remaining); + } + info!( + "Adding {} weight to UID 0 to fill to sum=1", + remaining as f64 / 65535.0 + ); + } + } + + (uids, weight_values) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::BittensorConfig; + use std::collections::HashMap; + + fn sample_pending_mechanism_commit() -> PendingMechanismCommit { + PendingMechanismCommit { + mechanism_id: 1, + hash: "abc".to_string(), + uids: vec![1, 2], + weights: vec![10, 20], + salt_hex: PendingMechanismCommit::salt_to_hex(&[1, 2, 3]), + version_key: 7, + epoch: 9, + } + } + + fn sample_pending_commit_v2() -> PendingCommitV2 { + PendingCommitV2 { + hash: "def".to_string(), + uids: vec![3, 4], + weights: vec![30, 40], + salt_hex: PendingCommitV2::salt_to_hex(&[4, 5]), + version_key: 11, + epoch: 10, + } + } + + #[test] + fn test_persisted_commit_state_round_trip() { + let mut state = PersistedCommitState { + committed_epoch: Some(5), + ..Default::default() + }; + state + .pending_mechanism_commits + .insert(1, sample_pending_mechanism_commit()); + state.pending_commit = Some(sample_pending_commit_v2()); + state.last_revealed_epoch.insert(1, 4); + + let path = std::env::temp_dir().join(format!( + "commit_state_test_{}.json", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + + state.save(&path).expect("should save state"); + let loaded = PersistedCommitState::load(&path); + std::fs::remove_file(&path).ok(); + + assert_eq!(loaded.committed_epoch, Some(5)); + assert!(loaded.pending_mechanism_commits.contains_key(&1)); + assert!(loaded.pending_commit.is_some()); + assert_eq!(loaded.last_revealed_epoch.get(&1), Some(&4)); + } + + #[test] + fn test_new_epoch_clears_old_commits() { + let mut state = PersistedCommitState { + committed_epoch: Some(1), + ..Default::default() + }; + state + .pending_mechanism_commits + .insert(1, sample_pending_mechanism_commit()); + state.pending_commit = Some(sample_pending_commit_v2()); + + state.new_epoch(2); + + assert!(state.pending_mechanism_commits.is_empty()); + assert!(state.pending_commit.is_none()); + } + + #[test] + fn test_new_epoch_same_epoch_preserves_commits() { + let mut state = PersistedCommitState { + committed_epoch: Some(7), + ..Default::default() + }; + state + .pending_mechanism_commits + .insert(1, sample_pending_mechanism_commit()); + + state.new_epoch(7); + + assert!(!state.pending_mechanism_commits.is_empty()); + } + + #[test] + fn test_pending_mechanism_commit_salt_round_trip() { + let salt = vec![1, 65535, 42]; + let hex = PendingMechanismCommit::salt_to_hex(&salt); + let commit = PendingMechanismCommit { + salt_hex: hex, + ..sample_pending_mechanism_commit() + }; + + assert_eq!(commit.get_salt(), salt); + } + + #[test] + fn test_pending_commit_v2_salt_round_trip() { + let salt = vec![100, 200]; + let hex = PendingCommitV2::salt_to_hex(&salt); + let commit = PendingCommitV2 { + salt_hex: hex, + ..sample_pending_commit_v2() + }; + + assert_eq!(commit.get_salt(), salt); + } + + #[test] + fn test_has_commits_for_epoch() { + let mut state = PersistedCommitState { + committed_epoch: Some(3), + ..Default::default() + }; + state + .pending_mechanism_commits + .insert(1, sample_pending_mechanism_commit()); + + assert!(state.has_commits_for_epoch(3)); + assert!(!state.has_commits_for_epoch(4)); + } + + #[test] + fn test_has_revealed_for_epoch_tracks_state() { + let mut state = PersistedCommitState::default(); + assert!(!state.has_revealed_for_epoch(2, 5)); + + state.last_revealed_epoch.insert(2, 10); + assert!(state.has_revealed_for_epoch(2, 9)); + assert!(state.has_revealed_for_epoch(2, 10)); + assert!(!state.has_revealed_for_epoch(2, 11)); + } + + #[test] + fn test_convert_assignments_with_lookup_resolves_and_clamps() { + let assignments = vec![ + WeightAssignment::new("hk1".to_string(), 0.25), + WeightAssignment::new("hk2".to_string(), 1.5), + ]; + + let mut lookup = HashMap::new(); + lookup.insert("hk1".to_string(), 5); + lookup.insert("hk2".to_string(), 7); + + let (uids, weights, unresolved) = convert_assignments_with_lookup(&assignments, &lookup); + + assert_eq!(uids, vec![5_u64, 7_u64]); + assert_eq!(weights.len(), 2); + assert_eq!(weights[1], 65535); + assert!(unresolved.is_empty()); + } + + #[test] + fn test_convert_assignments_with_lookup_tracks_unresolved() { + let assignments = vec![ + WeightAssignment::new("known".to_string(), 0.4), + WeightAssignment::new("missing".to_string(), 0.6), + ]; + + let mut lookup = HashMap::new(); + lookup.insert("known".to_string(), 9); + + let (uids, weights, unresolved) = convert_assignments_with_lookup(&assignments, &lookup); + + assert_eq!(uids, vec![9_u64]); + assert_eq!(weights.len(), 1); + assert_eq!(unresolved, vec!["missing".to_string()]); + } + + #[test] + fn test_convert_assignments_with_lookup_clamps_negative_weights() { + let assignments = vec![ + WeightAssignment::new("known".to_string(), -0.4), + WeightAssignment::new("extra".to_string(), 0.3), + ]; + + let mut lookup = HashMap::new(); + lookup.insert("known".to_string(), 11); + + let (uids, weights, unresolved) = convert_assignments_with_lookup(&assignments, &lookup); + + assert_eq!(uids, vec![11_u64]); + assert_eq!(weights, vec![0]); + assert_eq!(unresolved, vec!["extra".to_string()]); + } + + #[test] + fn test_fill_with_burn_defaults_to_uid_zero() { + let (uids, weights) = fill_with_burn(vec![], vec![]); + assert_eq!(uids, vec![0]); + assert_eq!(weights, vec![65535]); + } + + #[test] + fn test_fill_with_burn_adds_new_burn_entry() { + let (uids, weights) = fill_with_burn(vec![2], vec![20000]); + assert_eq!(uids.len(), 2); + assert!(uids.contains(&0)); + let burn_index = uids.iter().position(|u| *u == 0).unwrap(); + assert_eq!(weights[burn_index] as u32 + 20000, 65535); + } + + #[test] + fn test_fill_with_burn_updates_existing_burn_entry() { + let (uids, weights) = fill_with_burn(vec![0, 3], vec![1000, 2000]); + assert_eq!(uids[0], 0); + assert_eq!(weights[0] as u32 + weights[1] as u32, 65535); + } + + #[test] + fn test_fill_with_burn_noop_when_sum_already_complete() { + let (uids, weights) = fill_with_burn(vec![2], vec![65535]); + assert_eq!(uids, vec![2]); + assert_eq!(weights, vec![65535]); + } + + #[test] + fn test_mechanism_weight_manager_pending_helpers() { + let config = BittensorConfig::local(1); + let client = SubtensorClient::new(config); + let mut manager = MechanismWeightManager::new(client); + + assert!(!manager.has_pending_commits()); + assert!(manager.pending_mechanism_ids().is_empty()); + + manager.pending_mechanism_commits.insert( + 3, + PendingMechanismCommitV2 { + mechanism_id: 3, + hash: "hash".to_string(), + uids: vec![1, 2], + weights: vec![10, 20], + salt: vec![7, 8], + version_key: 11, + epoch: 9, + }, + ); + + assert!(manager.has_pending_commits()); + assert_eq!(manager.pending_mechanism_ids(), vec![3]); + + manager.last_weight_epoch.insert(5, 6); + manager.reset_epoch_tracking(); + assert!(manager.last_weight_epoch.is_empty()); + } + + #[test] + fn test_weight_submitter_epoch_transition_clears_pending_commits() { + let temp_dir = std::env::temp_dir().join(format!( + "weight_submitter_epoch_test_{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir_all(&temp_dir).unwrap(); + + let config = BittensorConfig::local(1); + let client = SubtensorClient::new(config); + let mut submitter = WeightSubmitter::new(client, Some(temp_dir.clone())); + + submitter + .state + .pending_mechanism_commits + .insert(1, sample_pending_mechanism_commit()); + submitter.state.pending_commit = Some(sample_pending_commit_v2()); + submitter.state.committed_epoch = Some(1); + + submitter.set_epoch(2); + + assert!(submitter.state.pending_mechanism_commits.is_empty()); + assert!(submitter.state.pending_commit.is_none()); + + let _ = std::fs::remove_file(temp_dir.join(DEFAULT_COMMITS_FILE)); + let _ = std::fs::remove_dir_all(&temp_dir); + } + + #[test] + fn test_persisted_commit_state_load_returns_default_on_error() { + let path = std::env::temp_dir().join("commit_state_invalid.json"); + std::fs::write(&path, "not-json").unwrap(); + + let loaded = PersistedCommitState::load(&path); + assert!(loaded.committed_epoch.is_none()); + assert!(loaded.pending_mechanism_commits.is_empty()); + let _ = std::fs::remove_file(path); + } + + #[test] + fn test_pending_mechanism_commit_get_salt_handles_partial_chunk() { + let mut commit = sample_pending_mechanism_commit(); + commit.salt_hex = "010203".to_string(); // 3 bytes -> last chunk has len 1 + let salt = commit.get_salt(); + assert_eq!(salt.len(), 2); + assert_eq!(salt[1], 0x03); + } + + #[test] + fn test_pending_commit_v2_get_salt_handles_partial_chunk() { + let mut commit = sample_pending_commit_v2(); + commit.salt_hex = "0a0b0c".to_string(); + let salt = commit.get_salt(); + assert_eq!(salt.len(), 2); + assert_eq!(salt[1], 0x0c); + } +} + +/// Pending mechanism commit data using subtensor-compatible format (v2) +#[derive(Clone, Debug)] +struct PendingMechanismCommitV2 { + mechanism_id: u8, + hash: String, + uids: Vec, // u16 to match subtensor + weights: Vec, + salt: Vec, // u16 salt as required by subtensor + version_key: u64, + epoch: u64, +} + +impl MechanismWeightManager { + /// Create a new mechanism weight manager + pub fn new(client: SubtensorClient) -> Self { + Self { + client, + last_weight_epoch: HashMap::new(), + pending_mechanism_commits: HashMap::new(), + } + } + + /// Get mutable access to the client + pub fn client_mut(&mut self) -> &mut SubtensorClient { + &mut self.client + } + + /// Get the next epoch start block + pub async fn get_next_epoch(&self) -> Result { + let client = self.client.client()?; + let next_epoch = get_next_epoch_start_block(client, self.client.netuid(), None) + .await? + .unwrap_or(0); + Ok(next_epoch) + } + + /// Check if weights have already been set for this mechanism in current epoch + pub fn has_set_weights_for_epoch(&self, mechanism_id: u8, epoch: u64) -> bool { + self.last_weight_epoch + .get(&mechanism_id) + .map(|e| *e >= epoch) + .unwrap_or(false) + } + + /// Submit mechanism weights for a specific challenge + /// Returns Ok(None) if weights already set for this epoch + pub async fn submit_mechanism_weights( + &mut self, + mechanism_id: u8, + weights: &[WeightAssignment], + epoch: u64, + ) -> Result> { + // Check if already set for this epoch + if self.has_set_weights_for_epoch(mechanism_id, epoch) { + info!( + "Weights already set for mechanism {} in epoch {}, skipping", + mechanism_id, epoch + ); + return Ok(None); + } + + // Prepare weights with fallback to UID 0 + let (uids, weight_values) = self.prepare_weights_with_fallback(weights)?; + + if self.client.use_commit_reveal() { + self.submit_mechanism_with_commit_reveal(mechanism_id, uids, weight_values, epoch) + .await + } else { + self.submit_mechanism_direct(mechanism_id, uids, weight_values, epoch) + .await + } + } + + /// Submit mechanism weights directly (no commit-reveal) + async fn submit_mechanism_direct( + &mut self, + mechanism_id: u8, + uids: Vec, + weights: Vec, + epoch: u64, + ) -> Result> { + // Convert u16 weights to f32 for set_mechanism_weights + let weight_f32: Vec = weights.iter().map(|w| *w as f32 / 65535.0).collect(); + + info!( + "Submitting {} mechanism {} weights directly", + uids.len(), + mechanism_id + ); + + let tx_hash = set_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + mechanism_id, + &uids, + &weight_f32, + Some(self.client.version_key()), + ExtrinsicWait::Finalized, + ) + .await?; + + // Mark as set for this epoch + self.last_weight_epoch.insert(mechanism_id, epoch); + + info!("Mechanism {} weights submitted: {}", mechanism_id, tx_hash); + Ok(Some(tx_hash)) + } + + /// Submit mechanism weights with commit-reveal pattern (v2 - subtensor compatible) + async fn submit_mechanism_with_commit_reveal( + &mut self, + mechanism_id: u8, + uids: Vec, + weights: Vec, + epoch: u64, + ) -> Result> { + // Check if we have a pending commit to reveal + if let Some(pending) = self.pending_mechanism_commits.remove(&mechanism_id) { + return self.reveal_mechanism_pending_v2(pending).await; + } + + // Convert to u16 for subtensor + let uids_u16: Vec = uids.iter().map(|u| *u as u16).collect(); + + // Get account public key for hash + let account = self.client.signer()?.account_id().0; + let version_key = self.client.version_key(); + + // Generate commit using v2 format (subtensor compatible) + let commit_data = prepare_mechanism_commit_reveal( + &account, + self.client.netuid(), + mechanism_id, + &uids_u16, + &weights, + version_key, + 8, // salt length + ); + + info!( + "Committing mechanism {} weights hash (v2): {}", + mechanism_id, commit_data.commit_hash + ); + + let tx_hash = commit_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + mechanism_id, + &commit_data.commit_hash, + ExtrinsicWait::Finalized, + ) + .await?; + + // Store pending commit for reveal + self.pending_mechanism_commits.insert( + mechanism_id, + PendingMechanismCommitV2 { + mechanism_id, + hash: commit_data.commit_hash, + uids: commit_data.uids, + weights: commit_data.weights, + salt: commit_data.salt, + version_key: commit_data.version_key, + epoch, + }, + ); + + info!("Mechanism {} weights committed: {}", mechanism_id, tx_hash); + Ok(Some(tx_hash)) + } + + /// Reveal pending mechanism commit (v2 format) + async fn reveal_mechanism_pending_v2( + &mut self, + pending: PendingMechanismCommitV2, + ) -> Result> { + info!( + "Revealing mechanism {} weights for commit: {}", + pending.mechanism_id, pending.hash + ); + + let tx_hash = reveal_mechanism_weights( + self.client.client()?, + self.client.signer()?, + self.client.netuid(), + pending.mechanism_id, + &pending.uids, + &pending.weights, + &pending.salt, + pending.version_key, + ExtrinsicWait::Finalized, + ) + .await?; + + // Mark as set for this epoch + self.last_weight_epoch + .insert(pending.mechanism_id, pending.epoch); + + info!( + "Mechanism {} weights revealed: {}", + pending.mechanism_id, tx_hash + ); + Ok(Some(tx_hash)) + } + + /// Prepare weights with fallback to UID 0 if empty or sum < 1 + fn prepare_weights_with_fallback( + &self, + weights: &[WeightAssignment], + ) -> Result<(Vec, Vec)> { + let (uids, weight_values) = self.prepare_weights(weights)?; + Ok(fill_with_burn(uids, weight_values)) + } + + /// Prepare weights for submission (convert hotkeys to UIDs) + fn prepare_weights(&self, weights: &[WeightAssignment]) -> Result<(Vec, Vec)> { + let hotkeys: Vec = weights.iter().map(|w| w.hotkey.clone()).collect(); + + let uid_lookup: HashMap = self + .client + .get_uids_for_hotkeys(&hotkeys) + .into_iter() + .collect(); + let (uids, weight_values, unresolved) = + convert_assignments_with_lookup(weights, &uid_lookup); + + for hotkey in unresolved { + warn!("No UID found for hotkey: {}", hotkey); + } + + debug!( + "Prepared {} weights from {} assignments", + uids.len(), + weights.len() + ); + Ok((uids, weight_values)) + } + + /// Check if we have pending commits to reveal for any mechanism + pub fn has_pending_commits(&self) -> bool { + !self.pending_mechanism_commits.is_empty() + } + + /// Get list of mechanisms with pending commits + pub fn pending_mechanism_ids(&self) -> Vec { + self.pending_mechanism_commits.keys().cloned().collect() + } + + /// Force reveal all pending mechanism commits + pub async fn reveal_all_pending(&mut self) -> Result> { + let mut results = Vec::new(); + let pending_ids: Vec = self.pending_mechanism_commits.keys().cloned().collect(); + + for mec_id in pending_ids { + if let Some(pending) = self.pending_mechanism_commits.remove(&mec_id) { + if let Some(tx) = self.reveal_mechanism_pending_v2(pending).await? { + results.push((mec_id, tx)); + } + } + } + + Ok(results) + } + + /// Reset epoch tracking (call at epoch boundary) + pub fn reset_epoch_tracking(&mut self) { + self.last_weight_epoch.clear(); + } +} diff --git a/crates/challenge-registry/Cargo.toml b/crates/challenge-registry/Cargo.toml new file mode 100644 index 000000000..f327dc6e5 --- /dev/null +++ b/crates/challenge-registry/Cargo.toml @@ -0,0 +1,43 @@ +[package] +name = "platform-challenge-registry" +version.workspace = true +edition.workspace = true +description = "Challenge registry and lifecycle management for Platform Network" + +[dependencies] +wasm-runtime-interface = { path = "../wasm-runtime-interface" } +platform-core = { path = "../core" } +platform-challenge-sdk = { path = "../challenge-sdk" } +platform-storage = { path = "../storage" } + +# Async +tokio = { workspace = true } +async-trait = { workspace = true } +futures = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +chrono = { workspace = true } +parking_lot = { workspace = true } +uuid = { workspace = true } + +# Crypto for checksums +sha2 = { workspace = true } +hex = { workspace = true } + +# Versioning +semver = "1.0" + +# Health checks +reqwest = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } +tokio-test = { workspace = true } \ No newline at end of file diff --git a/crates/challenge-registry/src/discovery.rs b/crates/challenge-registry/src/discovery.rs new file mode 100644 index 000000000..aad86764f --- /dev/null +++ b/crates/challenge-registry/src/discovery.rs @@ -0,0 +1,387 @@ +//! Challenge discovery and auto-registration +//! +//! Discovers challenges from: +//! - File system (local development) +//! - WASM module directories +//! - Network announcements (P2P) + +use crate::error::{RegistryError, RegistryResult}; +use crate::version::ChallengeVersion; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use wasm_runtime_interface::SandboxPolicy; + +/// A discovered challenge that can be registered +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct DiscoveredChallenge { + /// Challenge name + pub name: String, + /// Challenge version + pub version: ChallengeVersion, + /// Local path (for development) + pub local_path: Option, + /// Health endpoint URL + pub health_endpoint: Option, + /// Evaluation endpoint URL + pub evaluation_endpoint: Option, + /// Challenge metadata + pub metadata: ChallengeMetadata, + /// Sandbox policy loaded from companion .policy.json + pub sandbox_policy: Option, + /// Source of discovery + pub source: DiscoverySource, +} + +/// Metadata about a challenge +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct ChallengeMetadata { + /// Human-readable description + pub description: Option, + /// Challenge author + pub author: Option, + /// Repository URL + pub repository: Option, + /// License + pub license: Option, + /// Tags for categorization + pub tags: Vec, + /// Minimum platform version required + pub min_platform_version: Option, +} + +/// Source where a challenge was discovered +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum DiscoverySource { + /// Discovered from local filesystem + LocalFilesystem(PathBuf), + /// Discovered from WASM module directory + WasmDirectory(PathBuf), + /// Announced via P2P network + P2PNetwork(String), + /// Manually configured + Manual, +} + +/// Configuration for challenge discovery +#[derive(Clone, Debug)] +pub struct DiscoveryConfig { + /// Local paths to scan + pub local_paths: Vec, + /// WASM module directories to scan + pub wasm_paths: Vec, + /// Enable P2P discovery + pub enable_p2p: bool, + /// Auto-register discovered challenges + pub auto_register: bool, + /// Scan interval in seconds + pub scan_interval_secs: u64, +} + +impl Default for DiscoveryConfig { + fn default() -> Self { + Self { + local_paths: vec![], + wasm_paths: vec![], + enable_p2p: true, + auto_register: false, + scan_interval_secs: 300, // 5 minutes + } + } +} + +/// Discovers challenges from various sources +pub struct ChallengeDiscovery { + /// Configuration + config: DiscoveryConfig, + /// Discovered but not yet registered challenges + discovered: parking_lot::RwLock>, +} + +impl ChallengeDiscovery { + /// Create a new discovery service with default config + pub fn new() -> Self { + Self { + config: DiscoveryConfig::default(), + discovered: parking_lot::RwLock::new(Vec::new()), + } + } + + /// Create with custom config + pub fn with_config(config: DiscoveryConfig) -> Self { + Self { + config, + discovered: parking_lot::RwLock::new(Vec::new()), + } + } + + /// Get the current configuration + pub fn config(&self) -> &DiscoveryConfig { + &self.config + } + + /// Discover challenges from all configured sources + pub fn discover_all(&self) -> RegistryResult> { + let mut all_discovered = Vec::new(); + + // Discover from local paths + for path in &self.config.local_paths { + match self.discover_from_local(path) { + Ok(challenges) => all_discovered.extend(challenges), + Err(e) => { + tracing::warn!(path = ?path, error = %e, "Failed to discover from local path"); + } + } + } + + // Discover from WASM directories + for path in &self.config.wasm_paths { + match self.discover_from_wasm_dir(path) { + Ok(challenges) => all_discovered.extend(challenges), + Err(e) => { + tracing::warn!(path = ?path, error = %e, "Failed to discover from WASM directory"); + } + } + } + + // Update internal state + let mut discovered = self.discovered.write(); + *discovered = all_discovered.clone(); + + Ok(all_discovered) + } + + /// Discover challenges from a local path + pub fn discover_from_local(&self, path: &PathBuf) -> RegistryResult> { + if !path.exists() { + return Err(RegistryError::InvalidConfig(format!( + "Path does not exist: {:?}", + path + ))); + } + + let mut challenges = Vec::new(); + + // Look for challenge.toml or Cargo.toml with challenge metadata + if path.is_dir() { + let challenge_toml = path.join("challenge.toml"); + let cargo_toml = path.join("Cargo.toml"); + + if challenge_toml.exists() { + // In a real implementation, parse challenge.toml + let name = path + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("unknown") + .to_string(); + + challenges.push(DiscoveredChallenge { + name, + version: ChallengeVersion::default(), + local_path: Some(path.clone()), + health_endpoint: None, + evaluation_endpoint: None, + metadata: ChallengeMetadata::default(), + sandbox_policy: None, + source: DiscoverySource::LocalFilesystem(path.clone()), + }); + } else if cargo_toml.exists() { + // Extract name from Cargo.toml + let name = path + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("unknown") + .to_string(); + + challenges.push(DiscoveredChallenge { + name, + version: ChallengeVersion::default(), + local_path: Some(path.clone()), + health_endpoint: None, + evaluation_endpoint: None, + metadata: ChallengeMetadata::default(), + sandbox_policy: None, + source: DiscoverySource::LocalFilesystem(path.clone()), + }); + } + } + + Ok(challenges) + } + + /// Discover challenges from a WASM module directory + pub fn discover_from_wasm_dir( + &self, + path: &PathBuf, + ) -> RegistryResult> { + if !path.exists() { + return Err(RegistryError::InvalidConfig(format!( + "WASM directory does not exist: {:?}", + path + ))); + } + + let mut challenges = Vec::new(); + + if path.is_dir() { + Self::scan_wasm_dir(path, &mut challenges); + + let challenges_subdir = path.join("challenges"); + if challenges_subdir.is_dir() { + Self::scan_wasm_dir(&challenges_subdir, &mut challenges); + } + } + + Ok(challenges) + } + + fn load_sandbox_policy(wasm_path: &std::path::Path) -> Option { + let policy_path = wasm_path.with_extension("policy.json"); + if policy_path.exists() { + match std::fs::read_to_string(&policy_path) { + Ok(contents) => match serde_json::from_str::(&contents) { + Ok(policy) => { + tracing::info!(path = ?policy_path, "Loaded sandbox policy"); + Some(policy) + } + Err(e) => { + tracing::warn!(path = ?policy_path, error = %e, "Failed to parse sandbox policy"); + None + } + }, + Err(e) => { + tracing::warn!(path = ?policy_path, error = %e, "Failed to read sandbox policy file"); + None + } + } + } else { + None + } + } + + fn scan_wasm_dir(dir: &std::path::Path, challenges: &mut Vec) { + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let entry_path = entry.path(); + if entry_path.extension().and_then(|e| e.to_str()) == Some("wasm") { + let name = entry_path + .file_stem() + .and_then(|n| n.to_str()) + .unwrap_or("unknown") + .to_string(); + + let sandbox_policy = Self::load_sandbox_policy(&entry_path); + + challenges.push(DiscoveredChallenge { + name, + version: ChallengeVersion::default(), + local_path: Some(entry_path.clone()), + health_endpoint: None, + evaluation_endpoint: None, + metadata: ChallengeMetadata::default(), + sandbox_policy, + source: DiscoverySource::WasmDirectory(entry_path), + }); + } + } + } + } + + /// Manually add a discovered challenge + pub fn add_discovered(&self, challenge: DiscoveredChallenge) { + let mut discovered = self.discovered.write(); + discovered.push(challenge); + } + + /// Get all discovered challenges + pub fn get_discovered(&self) -> Vec { + self.discovered.read().clone() + } + + /// Clear discovered challenges + pub fn clear_discovered(&self) { + self.discovered.write().clear(); + } + + /// Check if auto-registration is enabled + pub fn auto_register_enabled(&self) -> bool { + self.config.auto_register + } +} + +impl Default for ChallengeDiscovery { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_discovery_source_equality() { + assert_eq!(DiscoverySource::Manual, DiscoverySource::Manual); + assert_ne!( + DiscoverySource::Manual, + DiscoverySource::P2PNetwork("test".to_string()) + ); + } + + #[test] + fn test_discovered_challenge() { + let challenge = DiscoveredChallenge { + name: "test-challenge".to_string(), + version: ChallengeVersion::new(1, 0, 0), + local_path: None, + health_endpoint: Some("http://localhost:8080/health".to_string()), + evaluation_endpoint: Some("http://localhost:8080/evaluate".to_string()), + metadata: ChallengeMetadata { + description: Some("A test challenge".to_string()), + author: Some("Platform".to_string()), + ..Default::default() + }, + sandbox_policy: None, + source: DiscoverySource::Manual, + }; + + assert_eq!(challenge.name, "test-challenge"); + } + + #[test] + fn test_discovery_service() { + let discovery = ChallengeDiscovery::new(); + + assert!(discovery.get_discovered().is_empty()); + + discovery.add_discovered(DiscoveredChallenge { + name: "manual".to_string(), + version: ChallengeVersion::new(1, 0, 0), + local_path: None, + health_endpoint: None, + evaluation_endpoint: None, + metadata: ChallengeMetadata::default(), + sandbox_policy: None, + source: DiscoverySource::Manual, + }); + + assert_eq!(discovery.get_discovered().len(), 1); + + discovery.clear_discovered(); + assert!(discovery.get_discovered().is_empty()); + } + + #[test] + fn test_discovery_config() { + let config = DiscoveryConfig { + local_paths: vec![PathBuf::from("/challenges")], + wasm_paths: vec![], + enable_p2p: false, + auto_register: true, + scan_interval_secs: 60, + }; + + let discovery = ChallengeDiscovery::with_config(config); + assert!(discovery.auto_register_enabled()); + assert_eq!(discovery.config().scan_interval_secs, 60); + } +} diff --git a/crates/challenge-registry/src/error.rs b/crates/challenge-registry/src/error.rs new file mode 100644 index 000000000..3f3e6a7fa --- /dev/null +++ b/crates/challenge-registry/src/error.rs @@ -0,0 +1,248 @@ +//! Error types for challenge registry + +use thiserror::Error; + +/// Result type for registry operations +pub type RegistryResult = Result; + +/// Errors that can occur in the challenge registry +#[derive(Error, Debug)] +pub enum RegistryError { + #[error("Challenge not found: {0}")] + ChallengeNotFound(String), + + #[error("Challenge already registered: {0}")] + AlreadyRegistered(String), + + #[error("Version conflict: {0}")] + VersionConflict(String), + + #[error("Migration failed: {0}")] + MigrationFailed(String), + + #[error("Health check failed: {0}")] + HealthCheckFailed(String), + + #[error("State persistence error: {0}")] + StatePersistence(String), + + #[error("State restoration error: {0}")] + StateRestoration(String), + + #[error("Invalid challenge configuration: {0}")] + InvalidConfig(String), + + #[error("Serialization error: {0}")] + Serialization(String), + + #[error("Network error: {0}")] + Network(String), + + #[error("Internal error: {0}")] + Internal(String), +} + +impl From for RegistryError { + fn from(err: std::io::Error) -> Self { + RegistryError::Internal(err.to_string()) + } +} + +impl From for RegistryError { + fn from(err: serde_json::Error) -> Self { + RegistryError::Serialization(err.to_string()) + } +} + +impl From for RegistryError { + fn from(err: bincode::Error) -> Self { + RegistryError::Serialization(err.to_string()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::{Error as IoError, ErrorKind}; + + #[test] + fn test_registry_error_display_challenge_not_found() { + let err = RegistryError::ChallengeNotFound("test-challenge".to_string()); + assert_eq!(err.to_string(), "Challenge not found: test-challenge"); + } + + #[test] + fn test_registry_error_display_already_registered() { + let err = RegistryError::AlreadyRegistered("my-challenge".to_string()); + assert_eq!( + err.to_string(), + "Challenge already registered: my-challenge" + ); + } + + #[test] + fn test_registry_error_display_version_conflict() { + let err = RegistryError::VersionConflict("v1.0.0 vs v2.0.0".to_string()); + assert_eq!(err.to_string(), "Version conflict: v1.0.0 vs v2.0.0"); + } + + #[test] + fn test_registry_error_display_all_variants() { + let test_cases = vec![ + ( + RegistryError::ChallengeNotFound("challenge-id".to_string()), + "Challenge not found: challenge-id", + ), + ( + RegistryError::AlreadyRegistered("existing".to_string()), + "Challenge already registered: existing", + ), + ( + RegistryError::VersionConflict("mismatch".to_string()), + "Version conflict: mismatch", + ), + ( + RegistryError::MigrationFailed("migration error".to_string()), + "Migration failed: migration error", + ), + ( + RegistryError::HealthCheckFailed("health issue".to_string()), + "Health check failed: health issue", + ), + ( + RegistryError::StatePersistence("persist error".to_string()), + "State persistence error: persist error", + ), + ( + RegistryError::StateRestoration("restore error".to_string()), + "State restoration error: restore error", + ), + ( + RegistryError::InvalidConfig("bad config".to_string()), + "Invalid challenge configuration: bad config", + ), + ( + RegistryError::Serialization("serde error".to_string()), + "Serialization error: serde error", + ), + ( + RegistryError::Network("connection refused".to_string()), + "Network error: connection refused", + ), + ( + RegistryError::Internal("unexpected".to_string()), + "Internal error: unexpected", + ), + ]; + + for (error, expected_message) in test_cases { + assert_eq!( + error.to_string(), + expected_message, + "Display mismatch for {:?}", + error + ); + } + } + + #[test] + fn test_from_io_error() { + let io_err = IoError::new(ErrorKind::NotFound, "file not found"); + let registry_err: RegistryError = io_err.into(); + + match registry_err { + RegistryError::Internal(msg) => { + assert!( + msg.contains("file not found"), + "Expected message to contain 'file not found', got: {}", + msg + ); + } + other => panic!("Expected Internal variant, got: {:?}", other), + } + } + + #[test] + fn test_from_serde_json_error() { + // Create an invalid JSON to trigger a parse error + let invalid_json = "{ invalid json }"; + let serde_err = serde_json::from_str::(invalid_json).unwrap_err(); + let registry_err: RegistryError = serde_err.into(); + + match registry_err { + RegistryError::Serialization(msg) => { + assert!( + !msg.is_empty(), + "Serialization error message should not be empty" + ); + } + other => panic!("Expected Serialization variant, got: {:?}", other), + } + } + + #[test] + fn test_from_bincode_error() { + // Create invalid bincode data to trigger an error + let invalid_data: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; + let bincode_err: bincode::Error = bincode::deserialize::(invalid_data).unwrap_err(); + let registry_err: RegistryError = bincode_err.into(); + + match registry_err { + RegistryError::Serialization(msg) => { + assert!( + !msg.is_empty(), + "Serialization error message should not be empty" + ); + } + other => panic!("Expected Serialization variant, got: {:?}", other), + } + } + + #[test] + fn test_registry_result_type() { + // Test that RegistryResult works as expected with Ok + fn returns_ok() -> RegistryResult { + Ok(42) + } + assert_eq!(returns_ok().unwrap(), 42); + + // Test that RegistryResult works as expected with Err + fn returns_err() -> RegistryResult { + Err(RegistryError::Internal("test error".to_string())) + } + assert!(returns_err().is_err()); + + // Test with different types + fn returns_string() -> RegistryResult { + Ok("success".to_string()) + } + assert_eq!(returns_string().unwrap(), "success"); + } + + #[test] + fn test_error_debug_impl() { + let err = RegistryError::ChallengeNotFound("debug-test".to_string()); + let debug_str = format!("{:?}", err); + + // Debug format should contain the variant name and the inner value + assert!( + debug_str.contains("ChallengeNotFound"), + "Debug should contain variant name, got: {}", + debug_str + ); + assert!( + debug_str.contains("debug-test"), + "Debug should contain inner value, got: {}", + debug_str + ); + + // Test debug for another variant + let err2 = RegistryError::Network("connection timeout".to_string()); + let debug_str2 = format!("{:?}", err2); + assert!( + debug_str2.contains("Network"), + "Debug should contain variant name, got: {}", + debug_str2 + ); + } +} diff --git a/crates/challenge-registry/src/health.rs b/crates/challenge-registry/src/health.rs new file mode 100644 index 000000000..0f2218bb6 --- /dev/null +++ b/crates/challenge-registry/src/health.rs @@ -0,0 +1,437 @@ +//! Health monitoring for challenges +//! +//! Monitors challenge health through: +//! - HTTP health endpoints +//! - Container status +//! - Resource usage + +use parking_lot::RwLock; +use platform_core::ChallengeId; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::time::Duration; + +/// Health status of a challenge +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum HealthStatus { + /// Health status is unknown (not yet checked) + #[default] + Unknown, + /// Challenge is healthy + Healthy, + /// Challenge is degraded but operational + Degraded(String), + /// Challenge is unhealthy + Unhealthy(String), +} + +/// Detailed health information for a challenge +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeHealth { + /// Challenge identifier + pub challenge_id: ChallengeId, + /// Current health status + pub status: HealthStatus, + /// Last successful health check timestamp (millis) + pub last_check_at: i64, + /// Number of consecutive failures + pub consecutive_failures: u32, + /// Average response time in milliseconds + pub avg_response_time_ms: f64, + /// Additional health metrics + pub metrics: HashMap, +} + +impl ChallengeHealth { + /// Create new health info for a challenge + pub fn new(challenge_id: ChallengeId) -> Self { + Self { + challenge_id, + status: HealthStatus::Unknown, + last_check_at: 0, + consecutive_failures: 0, + avg_response_time_ms: 0.0, + metrics: HashMap::new(), + } + } + + /// Check if the challenge is considered healthy + pub fn is_healthy(&self) -> bool { + matches!(self.status, HealthStatus::Healthy) + } + + /// Check if the challenge is operational (healthy or degraded) + pub fn is_operational(&self) -> bool { + matches!( + self.status, + HealthStatus::Healthy | HealthStatus::Degraded(_) + ) + } + + /// Record a successful health check + pub fn record_success(&mut self, response_time_ms: f64) { + self.status = HealthStatus::Healthy; + self.last_check_at = chrono::Utc::now().timestamp_millis(); + self.consecutive_failures = 0; + + // Exponential moving average for response time + if self.avg_response_time_ms == 0.0 { + self.avg_response_time_ms = response_time_ms; + } else { + self.avg_response_time_ms = self.avg_response_time_ms * 0.8 + response_time_ms * 0.2; + } + } + + /// Record a failed health check + pub fn record_failure(&mut self, reason: String) { + self.consecutive_failures += 1; + self.last_check_at = chrono::Utc::now().timestamp_millis(); + + if self.consecutive_failures >= 3 { + self.status = HealthStatus::Unhealthy(reason); + } else { + self.status = HealthStatus::Degraded(reason); + } + } +} + +/// Configuration for health monitoring +#[derive(Clone, Debug)] +pub struct HealthConfig { + /// Interval between health checks + pub check_interval: Duration, + /// Timeout for health check requests + pub check_timeout: Duration, + /// Number of failures before marking unhealthy + pub failure_threshold: u32, + /// Number of successes to recover from unhealthy + pub recovery_threshold: u32, +} + +impl Default for HealthConfig { + fn default() -> Self { + Self { + check_interval: Duration::from_secs(30), + check_timeout: Duration::from_secs(5), + failure_threshold: 3, + recovery_threshold: 2, + } + } +} + +/// Monitors health of registered challenges +pub struct HealthMonitor { + /// Health state for each challenge + health_state: RwLock>, + /// Configuration + config: HealthConfig, +} + +impl HealthMonitor { + /// Create a new health monitor with default config + pub fn new() -> Self { + Self { + health_state: RwLock::new(HashMap::new()), + config: HealthConfig::default(), + } + } + + /// Create a health monitor with custom config + pub fn with_config(config: HealthConfig) -> Self { + Self { + health_state: RwLock::new(HashMap::new()), + config, + } + } + + /// Register a challenge for health monitoring + pub fn register(&self, challenge_id: ChallengeId) { + let mut state = self.health_state.write(); + state.insert(challenge_id, ChallengeHealth::new(challenge_id)); + } + + /// Unregister a challenge from health monitoring + pub fn unregister(&self, challenge_id: &ChallengeId) { + let mut state = self.health_state.write(); + state.remove(challenge_id); + } + + /// Get health status for a challenge + pub fn get_health(&self, challenge_id: &ChallengeId) -> Option { + self.health_state.read().get(challenge_id).cloned() + } + + /// Get health status for all challenges + pub fn get_all_health(&self) -> Vec { + self.health_state.read().values().cloned().collect() + } + + /// Update health status after a check + pub fn update_health(&self, challenge_id: &ChallengeId, status: HealthStatus) { + let mut state = self.health_state.write(); + if let Some(health) = state.get_mut(challenge_id) { + health.status = status; + health.last_check_at = chrono::Utc::now().timestamp_millis(); + } + } + + /// Record a successful health check + pub fn record_success(&self, challenge_id: &ChallengeId, response_time_ms: f64) { + let mut state = self.health_state.write(); + if let Some(health) = state.get_mut(challenge_id) { + health.record_success(response_time_ms); + } + } + + /// Record a failed health check + pub fn record_failure(&self, challenge_id: &ChallengeId, reason: String) { + let mut state = self.health_state.write(); + if let Some(health) = state.get_mut(challenge_id) { + health.record_failure(reason); + } + } + + /// Get the health config + pub fn config(&self) -> &HealthConfig { + &self.config + } +} + +impl Default for HealthMonitor { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_health_status() { + let mut health = ChallengeHealth::new(ChallengeId::new()); + + assert_eq!(health.status, HealthStatus::Unknown); + assert!(!health.is_healthy()); + + health.record_success(50.0); + assert!(health.is_healthy()); + assert!(health.is_operational()); + + health.record_failure("timeout".to_string()); + assert!(!health.is_healthy()); + assert!(health.is_operational()); // Still degraded + + health.record_failure("timeout".to_string()); + health.record_failure("timeout".to_string()); + assert!(!health.is_operational()); // Now unhealthy + } + + #[test] + fn test_health_monitor() { + let monitor = HealthMonitor::new(); + let id = ChallengeId::new(); + + monitor.register(id); + assert!(monitor.get_health(&id).is_some()); + + monitor.record_success(&id, 100.0); + let health = monitor.get_health(&id).unwrap(); + assert!(health.is_healthy()); + + monitor.unregister(&id); + assert!(monitor.get_health(&id).is_none()); + } + + #[test] + fn test_response_time_averaging() { + let mut health = ChallengeHealth::new(ChallengeId::new()); + + health.record_success(100.0); + assert_eq!(health.avg_response_time_ms, 100.0); + + health.record_success(200.0); + // 100 * 0.8 + 200 * 0.2 = 80 + 40 = 120 + assert!((health.avg_response_time_ms - 120.0).abs() < 0.01); + } + + #[test] + fn test_health_status_default() { + let status = HealthStatus::default(); + assert_eq!(status, HealthStatus::Unknown); + } + + #[test] + fn test_challenge_health_new() { + let challenge_id = ChallengeId::new(); + let health = ChallengeHealth::new(challenge_id); + + assert_eq!(health.challenge_id, challenge_id); + assert_eq!(health.status, HealthStatus::Unknown); + assert_eq!(health.last_check_at, 0); + assert_eq!(health.consecutive_failures, 0); + assert_eq!(health.avg_response_time_ms, 0.0); + assert!(health.metrics.is_empty()); + } + + #[test] + fn test_challenge_health_metrics() { + let mut health = ChallengeHealth::new(ChallengeId::new()); + + health.metrics.insert("cpu_usage".to_string(), 45.5); + health.metrics.insert("memory_mb".to_string(), 512.0); + health + .metrics + .insert("requests_per_sec".to_string(), 1000.0); + + assert_eq!(health.metrics.len(), 3); + assert_eq!(health.metrics.get("cpu_usage"), Some(&45.5)); + assert_eq!(health.metrics.get("memory_mb"), Some(&512.0)); + assert_eq!(health.metrics.get("requests_per_sec"), Some(&1000.0)); + assert_eq!(health.metrics.get("nonexistent"), None); + } + + #[test] + fn test_health_config_default() { + let config = HealthConfig::default(); + + assert_eq!(config.check_interval, Duration::from_secs(30)); + assert_eq!(config.check_timeout, Duration::from_secs(5)); + assert_eq!(config.failure_threshold, 3); + assert_eq!(config.recovery_threshold, 2); + } + + #[test] + fn test_health_config_custom() { + let config = HealthConfig { + check_interval: Duration::from_secs(60), + check_timeout: Duration::from_secs(10), + failure_threshold: 5, + recovery_threshold: 3, + }; + + assert_eq!(config.check_interval, Duration::from_secs(60)); + assert_eq!(config.check_timeout, Duration::from_secs(10)); + assert_eq!(config.failure_threshold, 5); + assert_eq!(config.recovery_threshold, 3); + } + + #[test] + fn test_health_monitor_with_config() { + let config = HealthConfig { + check_interval: Duration::from_secs(120), + check_timeout: Duration::from_secs(15), + failure_threshold: 10, + recovery_threshold: 5, + }; + + let monitor = HealthMonitor::with_config(config); + let monitor_config = monitor.config(); + + assert_eq!(monitor_config.check_interval, Duration::from_secs(120)); + assert_eq!(monitor_config.check_timeout, Duration::from_secs(15)); + assert_eq!(monitor_config.failure_threshold, 10); + assert_eq!(monitor_config.recovery_threshold, 5); + } + + #[test] + fn test_health_monitor_get_all_health() { + let monitor = HealthMonitor::new(); + let id1 = ChallengeId::new(); + let id2 = ChallengeId::new(); + let id3 = ChallengeId::new(); + + assert!(monitor.get_all_health().is_empty()); + + monitor.register(id1); + monitor.register(id2); + monitor.register(id3); + + let all_health = monitor.get_all_health(); + assert_eq!(all_health.len(), 3); + + let ids: Vec = all_health.iter().map(|h| h.challenge_id).collect(); + assert!(ids.contains(&id1)); + assert!(ids.contains(&id2)); + assert!(ids.contains(&id3)); + } + + #[test] + fn test_health_monitor_update_health() { + let monitor = HealthMonitor::new(); + let id = ChallengeId::new(); + + monitor.register(id); + let health = monitor.get_health(&id).expect("should be registered"); + assert_eq!(health.status, HealthStatus::Unknown); + + monitor.update_health(&id, HealthStatus::Healthy); + let health = monitor.get_health(&id).expect("should be registered"); + assert_eq!(health.status, HealthStatus::Healthy); + assert!(health.last_check_at > 0); + + monitor.update_health(&id, HealthStatus::Degraded("high latency".to_string())); + let health = monitor.get_health(&id).expect("should be registered"); + assert_eq!( + health.status, + HealthStatus::Degraded("high latency".to_string()) + ); + + monitor.update_health(&id, HealthStatus::Unhealthy("connection lost".to_string())); + let health = monitor.get_health(&id).expect("should be registered"); + assert_eq!( + health.status, + HealthStatus::Unhealthy("connection lost".to_string()) + ); + } + + #[test] + fn test_health_status_variants() { + let unknown = HealthStatus::Unknown; + let healthy = HealthStatus::Healthy; + let degraded = HealthStatus::Degraded("slow response".to_string()); + let unhealthy = HealthStatus::Unhealthy("service down".to_string()); + + assert_eq!(unknown, HealthStatus::Unknown); + assert_eq!(healthy, HealthStatus::Healthy); + assert_eq!( + degraded, + HealthStatus::Degraded("slow response".to_string()) + ); + assert_eq!( + unhealthy, + HealthStatus::Unhealthy("service down".to_string()) + ); + + assert_ne!(unknown, healthy); + assert_ne!(healthy, degraded); + assert_ne!(degraded, unhealthy); + + let degraded_clone = degraded.clone(); + assert_eq!(degraded, degraded_clone); + } + + #[test] + fn test_challenge_health_consecutive_successes() { + let mut health = ChallengeHealth::new(ChallengeId::new()); + + health.record_failure("error 1".to_string()); + health.record_failure("error 2".to_string()); + assert_eq!(health.consecutive_failures, 2); + assert!(matches!(health.status, HealthStatus::Degraded(_))); + + health.record_success(50.0); + assert_eq!(health.consecutive_failures, 0); + assert_eq!(health.status, HealthStatus::Healthy); + + health.record_failure("error 3".to_string()); + health.record_failure("error 4".to_string()); + health.record_failure("error 5".to_string()); + assert_eq!(health.consecutive_failures, 3); + assert!(matches!(health.status, HealthStatus::Unhealthy(_))); + + health.record_success(75.0); + assert_eq!(health.consecutive_failures, 0); + assert_eq!(health.status, HealthStatus::Healthy); + } +} diff --git a/crates/challenge-registry/src/lib.rs b/crates/challenge-registry/src/lib.rs new file mode 100644 index 000000000..6161212cb --- /dev/null +++ b/crates/challenge-registry/src/lib.rs @@ -0,0 +1,41 @@ +//! Challenge Registry for Platform Network +//! +//! Manages the lifecycle of challenge crates including: +//! - Challenge discovery and registration +//! - Version management and migrations +//! - Hot-reload support with state preservation +//! - Health monitoring +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ Challenge Registry โ”‚ +//! โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +//! โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +//! โ”‚ โ”‚ Discovery โ”‚ โ”‚ Lifecycle โ”‚ โ”‚ Health โ”‚ โ”‚ +//! โ”‚ โ”‚ Manager โ”‚ โ”‚ Manager โ”‚ โ”‚ Monitor โ”‚ โ”‚ +//! โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +//! โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +//! โ”‚ Challenge State Store โ”‚ +//! โ”‚ (evaluations, checkpoints, migrations) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` + +pub mod discovery; +pub mod error; +pub mod health; +pub mod lifecycle; +pub mod migration; +pub mod registry; +pub mod state; +pub mod version; + +pub use discovery::{ChallengeDiscovery, DiscoveredChallenge}; +pub use error::{RegistryError, RegistryResult}; +pub use health::{ChallengeHealth, HealthMonitor, HealthStatus}; +pub use lifecycle::{ChallengeLifecycle, LifecycleEvent, LifecycleState}; +pub use migration::{ChallengeMigration, MigrationPlan, MigrationStatus}; +pub use registry::{ChallengeEntry, ChallengeRegistry, RegisteredChallenge}; +pub use state::{ChallengeState, StateSnapshot, StateStore}; +pub use version::{ChallengeVersion, VersionConstraint, VersionedChallenge}; diff --git a/crates/challenge-registry/src/lifecycle.rs b/crates/challenge-registry/src/lifecycle.rs new file mode 100644 index 000000000..fca281a31 --- /dev/null +++ b/crates/challenge-registry/src/lifecycle.rs @@ -0,0 +1,432 @@ +//! Challenge lifecycle management +//! +//! Handles state transitions for challenges: +//! Registered -> Starting -> Running -> Stopping -> Stopped + +use crate::version::ChallengeVersion; +use platform_core::ChallengeId; +use serde::{Deserialize, Serialize}; + +/// State of a challenge in its lifecycle +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum LifecycleState { + /// Challenge is registered but not started + #[default] + Registered, + /// Challenge is starting up + Starting, + /// Challenge is running and accepting evaluations + Running, + /// Challenge is being stopped gracefully + Stopping, + /// Challenge is stopped + Stopped, + /// Challenge failed to start or crashed + Failed(String), + /// Challenge is being migrated to a new version + Migrating, +} + +/// Events emitted during lifecycle transitions +#[derive(Clone, Debug)] +pub enum LifecycleEvent { + /// Challenge was registered + Registered { challenge_id: ChallengeId }, + /// Challenge was unregistered + Unregistered { challenge_id: ChallengeId }, + /// Challenge state changed + StateChanged { + challenge_id: ChallengeId, + old_state: LifecycleState, + new_state: LifecycleState, + }, + /// Challenge version changed (hot-reload) + VersionChanged { + challenge_id: ChallengeId, + old_version: ChallengeVersion, + new_version: ChallengeVersion, + }, + /// Challenge restart configuration changed + Restarted { + challenge_id: ChallengeId, + previous_restart_id: Option, + new_restart_id: Option, + previous_config_version: u64, + new_config_version: u64, + }, +} + +/// Manages challenge lifecycle transitions +pub struct ChallengeLifecycle { + /// Whether to allow automatic restarts on failure + auto_restart: bool, + /// Maximum restart attempts + max_restart_attempts: u32, +} + +impl ChallengeLifecycle { + /// Create a new lifecycle manager + pub fn new() -> Self { + Self { + auto_restart: true, + max_restart_attempts: 3, + } + } + + /// Configure auto-restart behavior + pub fn with_auto_restart(mut self, enabled: bool, max_attempts: u32) -> Self { + self.auto_restart = enabled; + self.max_restart_attempts = max_attempts; + self + } + + /// Check if a state transition is valid + pub fn is_valid_transition(&self, from: &LifecycleState, to: &LifecycleState) -> bool { + match (from, to) { + // From Registered + (LifecycleState::Registered, LifecycleState::Starting) => true, + (LifecycleState::Registered, LifecycleState::Stopped) => true, + + // From Starting + (LifecycleState::Starting, LifecycleState::Running) => true, + (LifecycleState::Starting, LifecycleState::Failed(_)) => true, + + // From Running + (LifecycleState::Running, LifecycleState::Stopping) => true, + (LifecycleState::Running, LifecycleState::Failed(_)) => true, + (LifecycleState::Running, LifecycleState::Migrating) => true, + + // From Stopping + (LifecycleState::Stopping, LifecycleState::Stopped) => true, + + // From Stopped + (LifecycleState::Stopped, LifecycleState::Starting) => true, + (LifecycleState::Stopped, LifecycleState::Registered) => true, + + // From Failed + (LifecycleState::Failed(_), LifecycleState::Starting) => true, + (LifecycleState::Failed(_), LifecycleState::Stopped) => true, + + // From Migrating + (LifecycleState::Migrating, LifecycleState::Running) => true, + (LifecycleState::Migrating, LifecycleState::Failed(_)) => true, + + _ => false, + } + } + + /// Check if auto-restart is enabled + pub fn auto_restart_enabled(&self) -> bool { + self.auto_restart + } + + /// Check if restart configuration should trigger a restart + pub fn restart_required( + &self, + previous_restart_id: Option<&str>, + new_restart_id: Option<&str>, + previous_config_version: u64, + new_config_version: u64, + ) -> bool { + if previous_config_version != new_config_version { + return true; + } + + match (previous_restart_id, new_restart_id) { + (Some(prev), Some(next)) => prev != next, + (None, Some(_)) => true, + (Some(_), None) => true, + (None, None) => false, + } + } + pub fn max_restart_attempts(&self) -> u32 { + self.max_restart_attempts + } +} + +impl Default for ChallengeLifecycle { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_valid_transitions() { + let lifecycle = ChallengeLifecycle::new(); + + assert!( + lifecycle.is_valid_transition(&LifecycleState::Registered, &LifecycleState::Starting) + ); + assert!(lifecycle.is_valid_transition(&LifecycleState::Starting, &LifecycleState::Running)); + assert!(lifecycle.is_valid_transition(&LifecycleState::Running, &LifecycleState::Stopping)); + assert!(lifecycle.is_valid_transition(&LifecycleState::Stopping, &LifecycleState::Stopped)); + } + + #[test] + fn test_invalid_transitions() { + let lifecycle = ChallengeLifecycle::new(); + + assert!( + !lifecycle.is_valid_transition(&LifecycleState::Registered, &LifecycleState::Running) + ); + assert!(!lifecycle.is_valid_transition(&LifecycleState::Stopped, &LifecycleState::Running)); + } + + #[test] + fn test_lifecycle_config() { + let lifecycle = ChallengeLifecycle::new().with_auto_restart(false, 5); + + assert!(!lifecycle.auto_restart_enabled()); + assert_eq!(lifecycle.max_restart_attempts(), 5); + } + + #[test] + fn test_lifecycle_state_default() { + let state = LifecycleState::default(); + assert_eq!(state, LifecycleState::Registered); + } + + #[test] + fn test_lifecycle_default() { + let default_lifecycle = ChallengeLifecycle::default(); + let new_lifecycle = ChallengeLifecycle::new(); + + assert_eq!( + default_lifecycle.auto_restart_enabled(), + new_lifecycle.auto_restart_enabled() + ); + assert_eq!( + default_lifecycle.max_restart_attempts(), + new_lifecycle.max_restart_attempts() + ); + } + + #[test] + fn test_all_valid_transition_paths() { + let lifecycle = ChallengeLifecycle::new(); + + // From Registered + assert!( + lifecycle.is_valid_transition(&LifecycleState::Registered, &LifecycleState::Starting) + ); + assert!( + lifecycle.is_valid_transition(&LifecycleState::Registered, &LifecycleState::Stopped) + ); + + // From Starting + assert!(lifecycle.is_valid_transition(&LifecycleState::Starting, &LifecycleState::Running)); + assert!(lifecycle.is_valid_transition( + &LifecycleState::Starting, + &LifecycleState::Failed("error".to_string()) + )); + + // From Running + assert!(lifecycle.is_valid_transition(&LifecycleState::Running, &LifecycleState::Stopping)); + assert!(lifecycle.is_valid_transition( + &LifecycleState::Running, + &LifecycleState::Failed("crash".to_string()) + )); + assert!(lifecycle.is_valid_transition(&LifecycleState::Running, &LifecycleState::Migrating)); + + // From Stopping + assert!(lifecycle.is_valid_transition(&LifecycleState::Stopping, &LifecycleState::Stopped)); + + // From Stopped + assert!(lifecycle.is_valid_transition(&LifecycleState::Stopped, &LifecycleState::Starting)); + assert!( + lifecycle.is_valid_transition(&LifecycleState::Stopped, &LifecycleState::Registered) + ); + + // From Failed + assert!(lifecycle.is_valid_transition( + &LifecycleState::Failed("any error".to_string()), + &LifecycleState::Starting + )); + assert!(lifecycle.is_valid_transition( + &LifecycleState::Failed("any error".to_string()), + &LifecycleState::Stopped + )); + + // From Migrating + assert!(lifecycle.is_valid_transition(&LifecycleState::Migrating, &LifecycleState::Running)); + assert!(lifecycle.is_valid_transition( + &LifecycleState::Migrating, + &LifecycleState::Failed("migration failed".to_string()) + )); + } + + #[test] + fn test_failed_state_with_message() { + let error_message = "Connection timeout after 30s".to_string(); + let failed_state = LifecycleState::Failed(error_message.clone()); + + match failed_state { + LifecycleState::Failed(msg) => { + assert_eq!(msg, error_message); + } + _ => panic!("Expected Failed state"), + } + } + + #[test] + fn test_lifecycle_event_variants() { + let challenge_id = ChallengeId::new(); + + // Test Registered event + let registered_event = LifecycleEvent::Registered { challenge_id }; + match registered_event { + LifecycleEvent::Registered { challenge_id: id } => { + assert_eq!(id, challenge_id); + } + _ => panic!("Expected Registered event"), + } + + // Test Unregistered event + let unregistered_event = LifecycleEvent::Unregistered { challenge_id }; + match unregistered_event { + LifecycleEvent::Unregistered { challenge_id: id } => { + assert_eq!(id, challenge_id); + } + _ => panic!("Expected Unregistered event"), + } + + // Test StateChanged event + let state_changed_event = LifecycleEvent::StateChanged { + challenge_id, + old_state: LifecycleState::Registered, + new_state: LifecycleState::Starting, + }; + match state_changed_event { + LifecycleEvent::StateChanged { + challenge_id: id, + old_state, + new_state, + } => { + assert_eq!(id, challenge_id); + assert_eq!(old_state, LifecycleState::Registered); + assert_eq!(new_state, LifecycleState::Starting); + } + _ => panic!("Expected StateChanged event"), + } + + // Test VersionChanged event + let old_version = ChallengeVersion::new(1, 0, 0); + let new_version = ChallengeVersion::new(1, 1, 0); + let version_changed_event = LifecycleEvent::VersionChanged { + challenge_id, + old_version: old_version.clone(), + new_version: new_version.clone(), + }; + match version_changed_event { + LifecycleEvent::VersionChanged { + challenge_id: id, + old_version: old_v, + new_version: new_v, + } => { + assert_eq!(id, challenge_id); + assert_eq!(old_v, old_version); + assert_eq!(new_v, new_version); + } + _ => panic!("Expected VersionChanged event"), + } + + // Test Restarted event + let restarted_event = LifecycleEvent::Restarted { + challenge_id, + previous_restart_id: Some("old".to_string()), + new_restart_id: Some("new".to_string()), + previous_config_version: 1, + new_config_version: 2, + }; + match restarted_event { + LifecycleEvent::Restarted { + challenge_id: id, + previous_restart_id, + new_restart_id, + previous_config_version, + new_config_version, + } => { + assert_eq!(id, challenge_id); + assert_eq!(previous_restart_id, Some("old".to_string())); + assert_eq!(new_restart_id, Some("new".to_string())); + assert_eq!(previous_config_version, 1); + assert_eq!(new_config_version, 2); + } + _ => panic!("Expected Restarted event"), + } + } + + #[test] + fn test_restart_required() { + let lifecycle = ChallengeLifecycle::new(); + + assert!(lifecycle.restart_required(Some("a"), Some("b"), 0, 0)); + assert!(lifecycle.restart_required(None, Some("b"), 0, 0)); + assert!(lifecycle.restart_required(Some("a"), None, 0, 0)); + assert!(!lifecycle.restart_required(None, None, 0, 0)); + assert!(lifecycle.restart_required(Some("a"), Some("a"), 1, 2)); + assert!(!lifecycle.restart_required(Some("a"), Some("a"), 2, 2)); + } + + #[test] + fn test_auto_restart_default_values() { + let lifecycle = ChallengeLifecycle::new(); + + assert!(lifecycle.auto_restart_enabled()); + assert_eq!(lifecycle.max_restart_attempts(), 3); + } + + #[test] + fn test_with_auto_restart_builder() { + // Test disabling auto-restart + let lifecycle_disabled = ChallengeLifecycle::new().with_auto_restart(false, 0); + assert!(!lifecycle_disabled.auto_restart_enabled()); + assert_eq!(lifecycle_disabled.max_restart_attempts(), 0); + + // Test custom max attempts + let lifecycle_custom = ChallengeLifecycle::new().with_auto_restart(true, 10); + assert!(lifecycle_custom.auto_restart_enabled()); + assert_eq!(lifecycle_custom.max_restart_attempts(), 10); + + // Test chaining after default + let lifecycle_chained = ChallengeLifecycle::default().with_auto_restart(false, 1); + assert!(!lifecycle_chained.auto_restart_enabled()); + assert_eq!(lifecycle_chained.max_restart_attempts(), 1); + } + + #[test] + fn test_migrating_transitions() { + let lifecycle = ChallengeLifecycle::new(); + + // Valid: Running -> Migrating + assert!(lifecycle.is_valid_transition(&LifecycleState::Running, &LifecycleState::Migrating)); + + // Valid: Migrating -> Running (successful migration) + assert!(lifecycle.is_valid_transition(&LifecycleState::Migrating, &LifecycleState::Running)); + + // Valid: Migrating -> Failed (migration failed) + assert!(lifecycle.is_valid_transition( + &LifecycleState::Migrating, + &LifecycleState::Failed("migration error".to_string()) + )); + + // Invalid: Migrating -> Stopped (must go through Failed first) + assert!( + !lifecycle.is_valid_transition(&LifecycleState::Migrating, &LifecycleState::Stopped) + ); + + // Invalid: Migrating -> Starting + assert!( + !lifecycle.is_valid_transition(&LifecycleState::Migrating, &LifecycleState::Starting) + ); + + // Invalid: Registered -> Migrating (can't migrate without running first) + assert!( + !lifecycle.is_valid_transition(&LifecycleState::Registered, &LifecycleState::Migrating) + ); + } +} diff --git a/crates/challenge-registry/src/migration.rs b/crates/challenge-registry/src/migration.rs new file mode 100644 index 000000000..c9b2121a3 --- /dev/null +++ b/crates/challenge-registry/src/migration.rs @@ -0,0 +1,518 @@ +//! Challenge migration support +//! +//! Handles version migrations for challenges: +//! - Schema migrations +//! - State transformations +//! - Rollback support + +use crate::error::{RegistryError, RegistryResult}; +use crate::version::ChallengeVersion; +use platform_core::ChallengeId; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Status of a migration +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum MigrationStatus { + /// Migration is pending + Pending, + /// Migration is in progress + InProgress, + /// Migration completed successfully + Completed, + /// Migration failed + Failed(String), + /// Migration was rolled back + RolledBack, +} + +/// Migration metadata describing schema changes +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationMetadata { + /// Registry schema version when migration starts + pub registry_schema_version: u32, + /// Whether WASM module metadata was introduced + pub adds_wasm_module_metadata: bool, +} + +/// A single migration step +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationStep { + /// Step identifier + pub id: String, + /// Description of what this step does + pub description: String, + /// From version + pub from_version: ChallengeVersion, + /// To version + pub to_version: ChallengeVersion, + /// Whether this step is reversible + pub reversible: bool, + /// Estimated duration in seconds + pub estimated_duration_secs: u64, + /// Optional metadata describing schema changes + #[serde(default)] + pub metadata: Option, +} + +impl MigrationStep { + /// Create a new migration step + pub fn new( + id: String, + description: String, + from: ChallengeVersion, + to: ChallengeVersion, + ) -> Self { + Self { + id, + description, + from_version: from, + to_version: to, + reversible: true, + estimated_duration_secs: 60, + metadata: None, + } + } + + /// Mark step as irreversible + pub fn irreversible(mut self) -> Self { + self.reversible = false; + self + } + + /// Set estimated duration + pub fn with_duration(mut self, secs: u64) -> Self { + self.estimated_duration_secs = secs; + self + } + + /// Attach migration metadata + pub fn with_metadata(mut self, metadata: MigrationMetadata) -> Self { + self.metadata = Some(metadata); + self + } +} + +/// A plan for migrating a challenge between versions +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationPlan { + /// Challenge being migrated + pub challenge_id: ChallengeId, + /// Challenge name + pub challenge_name: String, + /// Source version + pub from_version: ChallengeVersion, + /// Target version + pub to_version: ChallengeVersion, + /// Ordered list of migration steps + pub steps: Vec, + /// Current status + pub status: MigrationStatus, + /// Index of current step (0-based) + pub current_step: usize, + /// Plan creation timestamp + pub created_at: i64, + /// Plan start timestamp (if started) + pub started_at: Option, + /// Plan completion timestamp (if completed) + pub completed_at: Option, +} + +impl MigrationPlan { + /// Create a new migration plan + pub fn new( + challenge_id: ChallengeId, + challenge_name: String, + from_version: ChallengeVersion, + to_version: ChallengeVersion, + ) -> Self { + Self { + challenge_id, + challenge_name, + from_version, + to_version, + steps: Vec::new(), + status: MigrationStatus::Pending, + current_step: 0, + created_at: chrono::Utc::now().timestamp_millis(), + started_at: None, + completed_at: None, + } + } + + /// Add a migration step + pub fn add_step(&mut self, step: MigrationStep) { + self.steps.push(step); + } + + /// Check if the plan has any steps + pub fn is_empty(&self) -> bool { + self.steps.is_empty() + } + + /// Get total number of steps + pub fn total_steps(&self) -> usize { + self.steps.len() + } + + /// Get estimated total duration + pub fn estimated_duration_secs(&self) -> u64 { + self.steps.iter().map(|s| s.estimated_duration_secs).sum() + } + + /// Check if migration is complete + pub fn is_complete(&self) -> bool { + matches!( + self.status, + MigrationStatus::Completed | MigrationStatus::RolledBack + ) + } + + /// Check if migration can be rolled back + pub fn can_rollback(&self) -> bool { + // Can rollback if all executed steps are reversible + self.steps + .iter() + .take(self.current_step) + .all(|s| s.reversible) + } + + /// Get progress as percentage + pub fn progress_percent(&self) -> f64 { + if self.steps.is_empty() { + return 100.0; + } + (self.current_step as f64 / self.steps.len() as f64) * 100.0 + } +} + +/// Record of a completed migration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationRecord { + /// Migration plan + pub plan: MigrationPlan, + /// Execution logs + pub logs: Vec, +} + +/// Log entry for migration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationLog { + /// Timestamp + pub timestamp: i64, + /// Log level + pub level: LogLevel, + /// Message + pub message: String, + /// Associated step ID (if any) + pub step_id: Option, +} + +/// Log level for migration logs +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum LogLevel { + Info, + Warning, + Error, +} + +/// Manages challenge migrations +pub struct ChallengeMigration { + /// Active migration plans + active_plans: parking_lot::RwLock>, + /// Migration history + history: parking_lot::RwLock>, + /// Maximum history to retain + max_history: usize, +} + +impl ChallengeMigration { + /// Create a new migration manager + pub fn new() -> Self { + Self { + active_plans: parking_lot::RwLock::new(HashMap::new()), + history: parking_lot::RwLock::new(Vec::new()), + max_history: 100, + } + } + + /// Create a migration plan between versions + pub fn create_plan( + &self, + challenge_id: ChallengeId, + challenge_name: String, + from_version: ChallengeVersion, + to_version: ChallengeVersion, + ) -> RegistryResult { + // Check if there's already an active migration + if self.active_plans.read().contains_key(&challenge_id) { + return Err(RegistryError::MigrationFailed( + "Migration already in progress".to_string(), + )); + } + + let mut plan = MigrationPlan::new( + challenge_id, + challenge_name, + from_version.clone(), + to_version.clone(), + ); + + // Generate migration steps based on version difference + // This is a simplified version - real implementation would analyze schemas + if from_version.major != to_version.major { + plan.add_step( + MigrationStep::new( + "major_upgrade".to_string(), + format!( + "Major version upgrade from {} to {}", + from_version.major, to_version.major + ), + from_version.clone(), + to_version.clone(), + ) + .irreversible() + .with_metadata(MigrationMetadata { + registry_schema_version: 2, + adds_wasm_module_metadata: true, + }) + .with_duration(300), + ); + } else if from_version.minor != to_version.minor { + plan.add_step( + MigrationStep::new( + "minor_upgrade".to_string(), + format!( + "Minor version upgrade from {} to {}", + from_version, to_version + ), + from_version.clone(), + to_version.clone(), + ) + .with_metadata(MigrationMetadata { + registry_schema_version: 2, + adds_wasm_module_metadata: true, + }) + .with_duration(60), + ); + } else if from_version.patch != to_version.patch { + plan.add_step( + MigrationStep::new( + "patch_upgrade".to_string(), + format!( + "Patch version upgrade from {} to {}", + from_version, to_version + ), + from_version, + to_version, + ) + .with_metadata(MigrationMetadata { + registry_schema_version: 2, + adds_wasm_module_metadata: true, + }) + .with_duration(10), + ); + } + + Ok(plan) + } + + /// Start executing a migration plan + pub fn start_migration(&self, plan: MigrationPlan) -> RegistryResult<()> { + let challenge_id = plan.challenge_id; + + let mut plans = self.active_plans.write(); + if plans.contains_key(&challenge_id) { + return Err(RegistryError::MigrationFailed( + "Migration already in progress".to_string(), + )); + } + + let mut plan = plan; + plan.status = MigrationStatus::InProgress; + plan.started_at = Some(chrono::Utc::now().timestamp_millis()); + + plans.insert(challenge_id, plan); + Ok(()) + } + + /// Get active migration for a challenge + pub fn get_active_migration(&self, challenge_id: &ChallengeId) -> Option { + self.active_plans.read().get(challenge_id).cloned() + } + + /// Complete a migration step + pub fn complete_step(&self, challenge_id: &ChallengeId) -> RegistryResult { + let mut plans = self.active_plans.write(); + let plan = plans + .get_mut(challenge_id) + .ok_or_else(|| RegistryError::MigrationFailed("No active migration".to_string()))?; + + plan.current_step += 1; + + // Check if all steps complete + if plan.current_step >= plan.steps.len() { + plan.status = MigrationStatus::Completed; + plan.completed_at = Some(chrono::Utc::now().timestamp_millis()); + Ok(true) + } else { + Ok(false) + } + } + + /// Fail a migration + pub fn fail_migration(&self, challenge_id: &ChallengeId, reason: String) -> RegistryResult<()> { + let mut plans = self.active_plans.write(); + let plan = plans + .get_mut(challenge_id) + .ok_or_else(|| RegistryError::MigrationFailed("No active migration".to_string()))?; + + plan.status = MigrationStatus::Failed(reason); + plan.completed_at = Some(chrono::Utc::now().timestamp_millis()); + + Ok(()) + } + + /// Finalize and archive a completed migration + pub fn finalize_migration(&self, challenge_id: &ChallengeId) -> RegistryResult { + let plan = self + .active_plans + .write() + .remove(challenge_id) + .ok_or_else(|| RegistryError::MigrationFailed("No active migration".to_string()))?; + + if !plan.is_complete() { + return Err(RegistryError::MigrationFailed( + "Migration not complete".to_string(), + )); + } + + // Add to history + let record = MigrationRecord { + plan: plan.clone(), + logs: Vec::new(), + }; + + let mut history = self.history.write(); + history.push(record); + + // Trim history + while history.len() > self.max_history { + history.remove(0); + } + + Ok(plan) + } + + /// Get migration history for a challenge + pub fn get_history(&self, challenge_id: &ChallengeId) -> Vec { + self.history + .read() + .iter() + .filter(|r| r.plan.challenge_id == *challenge_id) + .cloned() + .collect() + } + + /// Get all migration history + pub fn get_all_history(&self) -> Vec { + self.history.read().clone() + } +} + +impl Default for ChallengeMigration { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_migration_plan_creation() { + let migration = ChallengeMigration::new(); + let id = ChallengeId::new(); + + let plan = migration + .create_plan( + id, + "test".to_string(), + ChallengeVersion::new(1, 0, 0), + ChallengeVersion::new(1, 1, 0), + ) + .unwrap(); + + assert_eq!(plan.total_steps(), 1); + assert!(!plan.is_complete()); + assert_eq!(plan.progress_percent(), 0.0); + } + + #[test] + fn test_migration_execution() { + let migration = ChallengeMigration::new(); + let id = ChallengeId::new(); + + let plan = migration + .create_plan( + id, + "test".to_string(), + ChallengeVersion::new(1, 0, 0), + ChallengeVersion::new(1, 0, 1), + ) + .unwrap(); + + migration.start_migration(plan).unwrap(); + + let active = migration.get_active_migration(&id); + assert!(active.is_some()); + assert!(matches!( + active.unwrap().status, + MigrationStatus::InProgress + )); + + let complete = migration.complete_step(&id).unwrap(); + assert!(complete); + + let finalized = migration.finalize_migration(&id).unwrap(); + assert!(matches!(finalized.status, MigrationStatus::Completed)); + } + + #[test] + fn test_duplicate_migration_prevention() { + let migration = ChallengeMigration::new(); + let id = ChallengeId::new(); + + let plan = migration + .create_plan( + id, + "test".to_string(), + ChallengeVersion::new(1, 0, 0), + ChallengeVersion::new(1, 1, 0), + ) + .unwrap(); + + migration.start_migration(plan.clone()).unwrap(); + let result = migration.start_migration(plan); + assert!(result.is_err()); + } + + #[test] + fn test_major_version_migration() { + let migration = ChallengeMigration::new(); + let id = ChallengeId::new(); + + let plan = migration + .create_plan( + id, + "test".to_string(), + ChallengeVersion::new(1, 0, 0), + ChallengeVersion::new(2, 0, 0), + ) + .unwrap(); + + // Major version migrations are irreversible + assert!(!plan.steps[0].reversible); + } +} diff --git a/crates/challenge-registry/src/registry.rs b/crates/challenge-registry/src/registry.rs new file mode 100644 index 000000000..9fa6639fb --- /dev/null +++ b/crates/challenge-registry/src/registry.rs @@ -0,0 +1,616 @@ +//! Main challenge registry implementation + +use crate::error::{RegistryError, RegistryResult}; +use crate::health::{HealthMonitor, HealthStatus}; +use crate::lifecycle::{ChallengeLifecycle, LifecycleEvent, LifecycleState}; +use crate::state::StateStore; +use crate::version::ChallengeVersion; +use parking_lot::RwLock; +use platform_core::ChallengeId; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::sync::Arc; +use tracing::{debug, info, warn}; +use wasm_runtime_interface::{NetworkPolicy, SandboxPolicy}; + +/// WASM module metadata for a challenge +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmModuleMetadata { + /// Hash of the WASM module + pub module_hash: String, + /// Location of the WASM module (URL or path) + pub module_location: String, + /// Entrypoint function name + pub entrypoint: String, + /// Network policy for WASM execution + #[serde(default)] + pub network_policy: NetworkPolicy, + /// Sandbox policy for challenge execution + #[serde(default)] + pub sandbox_policy: Option, + /// Restartable configuration identifier + #[serde(default)] + pub restart_id: Option, + /// Configuration version for hot-restarts + #[serde(default)] + pub config_version: u64, +} + +impl WasmModuleMetadata { + pub fn new( + module_hash: String, + module_location: String, + entrypoint: String, + network_policy: NetworkPolicy, + ) -> Self { + Self { + module_hash, + module_location, + entrypoint, + network_policy, + sandbox_policy: None, + restart_id: None, + config_version: 0, + } + } + + pub fn with_sandbox_policy(mut self, policy: SandboxPolicy) -> Self { + self.sandbox_policy = Some(policy); + self + } + + /// Verify that the given module bytes match the stored hash + pub fn verify_hash(&self, module_bytes: &[u8]) -> bool { + use sha2::{Digest, Sha256}; + let computed = hex::encode(Sha256::digest(module_bytes)); + computed == self.module_hash + } +} + +/// Entry for a registered challenge +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeEntry { + /// Unique challenge ID + pub id: ChallengeId, + /// Challenge name + pub name: String, + /// Current version + pub version: ChallengeVersion, + /// WASM module metadata + #[serde(default)] + pub wasm_module: Option, + /// Restartable configuration identifier + #[serde(default)] + pub restart_id: Option, + /// Configuration version for hot-restarts + #[serde(default)] + pub config_version: u64, + /// Current lifecycle state + pub lifecycle_state: LifecycleState, + /// Health status + pub health_status: HealthStatus, + /// Registration timestamp + pub registered_at: i64, + /// Last updated timestamp + pub updated_at: i64, + /// Configuration metadata + pub metadata: serde_json::Value, +} + +impl ChallengeEntry { + pub fn new(name: String, version: ChallengeVersion) -> Self { + let now = chrono::Utc::now().timestamp_millis(); + Self { + id: ChallengeId::new(), + name, + version, + wasm_module: None, + restart_id: None, + config_version: 0, + lifecycle_state: LifecycleState::Registered, + health_status: HealthStatus::Unknown, + registered_at: now, + updated_at: now, + metadata: serde_json::Value::Null, + } + } + + pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self { + self.metadata = metadata; + self + } + + pub fn with_wasm_module(mut self, wasm_module: WasmModuleMetadata) -> Self { + self.wasm_module = Some(wasm_module); + self + } + + /// Check if this challenge has a valid WASM module configured + pub fn is_wasm_ready(&self) -> bool { + self.wasm_module.is_some() + } +} + +/// A registered challenge with its full state +#[derive(Clone, Debug)] +pub struct RegisteredChallenge { + pub entry: ChallengeEntry, + pub state_store: Arc, +} + +type LifecycleListeners = Vec>; + +/// Main challenge registry +pub struct ChallengeRegistry { + /// Registered challenges by ID + challenges: RwLock>, + /// Name to ID mapping for lookups + name_index: RwLock>, + /// Lifecycle manager + lifecycle: Arc, + /// Health monitor + health_monitor: Arc, + /// Event listeners + event_listeners: RwLock, +} + +impl ChallengeRegistry { + /// Create a new challenge registry + pub fn new() -> Self { + Self { + challenges: RwLock::new(HashMap::new()), + name_index: RwLock::new(HashMap::new()), + lifecycle: Arc::new(ChallengeLifecycle::new()), + health_monitor: Arc::new(HealthMonitor::new()), + event_listeners: RwLock::new(Vec::new()), + } + } + + /// Register a new challenge + pub fn register(&self, entry: ChallengeEntry) -> RegistryResult { + let mut challenges = self.challenges.write(); + let mut name_index = self.name_index.write(); + + // Check if already registered by name + if name_index.contains_key(&entry.name) { + return Err(RegistryError::AlreadyRegistered(entry.name.clone())); + } + + // Validate: wasm_module must be configured + if entry.wasm_module.is_none() { + return Err(RegistryError::InvalidConfig( + "Challenge must have a wasm_module configured".to_string(), + )); + } + + let id = entry.id; + let name = entry.name.clone(); + + let state_store = Arc::new(StateStore::new(id)); + let registered = RegisteredChallenge { entry, state_store }; + + challenges.insert(id, registered); + name_index.insert(name.clone(), id); + + info!(challenge_id = %id, name = %name, "Challenge registered"); + self.emit_event(LifecycleEvent::Registered { challenge_id: id }); + + Ok(id) + } + + /// Register a WASM-primary challenge from a WASM file on disk + pub fn register_wasm_challenge( + &self, + name: String, + version: ChallengeVersion, + wasm_path: &std::path::Path, + entrypoint: String, + network_policy: NetworkPolicy, + ) -> RegistryResult { + if !wasm_path.exists() { + return Err(RegistryError::InvalidConfig(format!( + "WASM file not found: {}", + wasm_path.display() + ))); + } + + let wasm_bytes = std::fs::read(wasm_path)?; + + use sha2::{Digest, Sha256}; + let module_hash = hex::encode(Sha256::digest(&wasm_bytes)); + let module_location = wasm_path.display().to_string(); + + let wasm_module = + WasmModuleMetadata::new(module_hash, module_location, entrypoint, network_policy); + + let entry = ChallengeEntry::new(name, version).with_wasm_module(wasm_module); + + self.register(entry) + } + + /// Unregister a challenge + pub fn unregister(&self, id: &ChallengeId) -> RegistryResult { + let mut challenges = self.challenges.write(); + let mut name_index = self.name_index.write(); + + let registered = challenges + .remove(id) + .ok_or_else(|| RegistryError::ChallengeNotFound(id.to_string()))?; + + name_index.remove(®istered.entry.name); + + info!(challenge_id = %id, "Challenge unregistered"); + self.emit_event(LifecycleEvent::Unregistered { challenge_id: *id }); + + Ok(registered.entry) + } + + /// Get a challenge by ID + pub fn get(&self, id: &ChallengeId) -> Option { + self.challenges.read().get(id).cloned() + } + + /// Get a challenge by name + pub fn get_by_name(&self, name: &str) -> Option { + let name_index = self.name_index.read(); + let id = name_index.get(name)?; + self.challenges.read().get(id).cloned() + } + + /// List all registered challenges + pub fn list(&self) -> Vec { + self.challenges + .read() + .values() + .map(|r| r.entry.clone()) + .collect() + } + + /// List active challenges (running and healthy) + pub fn list_active(&self) -> Vec { + self.challenges + .read() + .values() + .filter(|r| { + r.entry.lifecycle_state == LifecycleState::Running + && r.entry.health_status == HealthStatus::Healthy + }) + .map(|r| r.entry.clone()) + .collect() + } + + /// Update challenge lifecycle state + pub fn update_state(&self, id: &ChallengeId, new_state: LifecycleState) -> RegistryResult<()> { + let mut challenges = self.challenges.write(); + let registered = challenges + .get_mut(id) + .ok_or_else(|| RegistryError::ChallengeNotFound(id.to_string()))?; + + let old_state = registered.entry.lifecycle_state.clone(); + registered.entry.lifecycle_state = new_state.clone(); + registered.entry.updated_at = chrono::Utc::now().timestamp_millis(); + + debug!( + challenge_id = %id, + old_state = ?old_state, + new_state = ?new_state, + "Challenge state updated" + ); + + self.emit_event(LifecycleEvent::StateChanged { + challenge_id: *id, + old_state, + new_state, + }); + + Ok(()) + } + + /// Update challenge health status + pub fn update_health(&self, id: &ChallengeId, status: HealthStatus) -> RegistryResult<()> { + let mut challenges = self.challenges.write(); + let registered = challenges + .get_mut(id) + .ok_or_else(|| RegistryError::ChallengeNotFound(id.to_string()))?; + + registered.entry.health_status = status; + registered.entry.updated_at = chrono::Utc::now().timestamp_millis(); + + Ok(()) + } + + /// Update challenge version (for hot-reload) + pub fn update_version( + &self, + id: &ChallengeId, + new_version: ChallengeVersion, + ) -> RegistryResult { + let mut challenges = self.challenges.write(); + let registered = challenges + .get_mut(id) + .ok_or_else(|| RegistryError::ChallengeNotFound(id.to_string()))?; + + let old_version = registered.entry.version.clone(); + + if !new_version.is_compatible_with(&old_version) { + warn!( + challenge_id = %id, + old = %old_version, + new = %new_version, + "Breaking version change detected" + ); + } + + registered.entry.version = new_version.clone(); + registered.entry.updated_at = chrono::Utc::now().timestamp_millis(); + + info!( + challenge_id = %id, + old_version = %old_version, + new_version = %new_version, + "Challenge version updated" + ); + + self.emit_event(LifecycleEvent::VersionChanged { + challenge_id: *id, + old_version: old_version.clone(), + new_version, + }); + + Ok(old_version) + } + + /// Update restart configuration metadata + pub fn update_restart_config( + &self, + id: &ChallengeId, + restart_id: Option, + config_version: u64, + ) -> RegistryResult<(Option, u64)> { + let mut challenges = self.challenges.write(); + let registered = challenges + .get_mut(id) + .ok_or_else(|| RegistryError::ChallengeNotFound(id.to_string()))?; + + let previous_restart_id = registered.entry.restart_id.clone(); + let previous_config_version = registered.entry.config_version; + + let restart_required = self.lifecycle.restart_required( + previous_restart_id.as_deref(), + restart_id.as_deref(), + previous_config_version, + config_version, + ); + + registered.entry.restart_id = restart_id.clone(); + registered.entry.config_version = config_version; + registered.entry.updated_at = chrono::Utc::now().timestamp_millis(); + + if let Some(wasm_module) = registered.entry.wasm_module.as_mut() { + wasm_module.restart_id = restart_id.clone(); + wasm_module.config_version = config_version; + } + + if restart_required { + info!( + challenge_id = %id, + previous_restart_id = ?previous_restart_id, + new_restart_id = ?restart_id, + previous_config_version = previous_config_version, + new_config_version = config_version, + "Challenge restart configuration updated" + ); + self.emit_event(LifecycleEvent::Restarted { + challenge_id: *id, + previous_restart_id: previous_restart_id.clone(), + new_restart_id: restart_id, + previous_config_version, + new_config_version: config_version, + }); + } + + Ok((previous_restart_id, previous_config_version)) + } + + /// Get state store for a challenge + pub fn state_store(&self, id: &ChallengeId) -> Option> { + self.challenges + .read() + .get(id) + .map(|r| r.state_store.clone()) + } + + /// Add event listener + pub fn on_event(&self, listener: F) + where + F: Fn(LifecycleEvent) + Send + Sync + 'static, + { + self.event_listeners.write().push(Box::new(listener)); + } + + /// Emit lifecycle event to all listeners + fn emit_event(&self, event: LifecycleEvent) { + for listener in self.event_listeners.read().iter() { + listener(event.clone()); + } + } + + /// Get lifecycle manager + pub fn lifecycle(&self) -> Arc { + self.lifecycle.clone() + } + + /// Get health monitor + pub fn health_monitor(&self) -> Arc { + self.health_monitor.clone() + } + + /// Challenge count + pub fn count(&self) -> usize { + self.challenges.read().len() + } +} + +impl Default for ChallengeRegistry { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn make_wasm_module() -> WasmModuleMetadata { + WasmModuleMetadata::new( + "hash".to_string(), + "module.wasm".to_string(), + "evaluate".to_string(), + NetworkPolicy::default(), + ) + } + + #[test] + fn test_register_challenge() { + let registry = ChallengeRegistry::new(); + let entry = + ChallengeEntry::new("test-challenge".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + assert!(registry.get(&id).is_some()); + assert_eq!(registry.count(), 1); + } + + #[test] + fn test_duplicate_registration() { + let registry = ChallengeRegistry::new(); + let entry1 = + ChallengeEntry::new("test-challenge".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + let entry2 = + ChallengeEntry::new("test-challenge".to_string(), ChallengeVersion::new(2, 0, 0)) + .with_wasm_module(make_wasm_module()); + + registry.register(entry1).unwrap(); + let result = registry.register(entry2); + assert!(matches!(result, Err(RegistryError::AlreadyRegistered(_)))); + } + + #[test] + fn test_get_by_name() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("my-challenge".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + registry.register(entry).unwrap(); + let found = registry.get_by_name("my-challenge"); + assert!(found.is_some()); + assert_eq!(found.unwrap().entry.name, "my-challenge"); + } + + #[test] + fn test_unregister() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + assert_eq!(registry.count(), 1); + + registry.unregister(&id).unwrap(); + assert_eq!(registry.count(), 0); + } + + #[test] + fn test_update_state() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + registry.update_state(&id, LifecycleState::Running).unwrap(); + + let challenge = registry.get(&id).unwrap(); + assert_eq!(challenge.entry.lifecycle_state, LifecycleState::Running); + } + + #[test] + fn test_update_version() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + let old = registry + .update_version(&id, ChallengeVersion::new(1, 1, 0)) + .unwrap(); + + assert_eq!(old, ChallengeVersion::new(1, 0, 0)); + + let challenge = registry.get(&id).unwrap(); + assert_eq!(challenge.entry.version, ChallengeVersion::new(1, 1, 0)); + } + + #[test] + fn test_update_restart_config() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + let previous = registry + .update_restart_config(&id, Some("restart-1".to_string()), 1) + .unwrap(); + + assert_eq!(previous, (None, 0)); + + let challenge = registry.get(&id).unwrap(); + assert_eq!(challenge.entry.restart_id, Some("restart-1".to_string())); + assert_eq!(challenge.entry.config_version, 1); + let wasm_module = challenge.entry.wasm_module.unwrap(); + assert_eq!(wasm_module.restart_id, Some("restart-1".to_string())); + assert_eq!(wasm_module.config_version, 1); + } + + #[test] + fn test_list_active() { + let registry = ChallengeRegistry::new(); + + let entry1 = ChallengeEntry::new("active".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + let entry2 = ChallengeEntry::new("inactive".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id1 = registry.register(entry1).unwrap(); + registry.register(entry2).unwrap(); + + registry + .update_state(&id1, LifecycleState::Running) + .unwrap(); + registry.update_health(&id1, HealthStatus::Healthy).unwrap(); + + let active = registry.list_active(); + assert_eq!(active.len(), 1); + assert_eq!(active[0].name, "active"); + } + + #[test] + fn test_entry_builders() { + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_metadata(serde_json::json!({"key": "value"})); + + assert_eq!(entry.metadata["key"], "value"); + } + + #[test] + fn test_state_store_access() { + let registry = ChallengeRegistry::new(); + let entry = ChallengeEntry::new("test".to_string(), ChallengeVersion::new(1, 0, 0)) + .with_wasm_module(make_wasm_module()); + + let id = registry.register(entry).unwrap(); + let store = registry.state_store(&id); + assert!(store.is_some()); + } +} diff --git a/crates/challenge-registry/src/state.rs b/crates/challenge-registry/src/state.rs new file mode 100644 index 000000000..14e2e1cda --- /dev/null +++ b/crates/challenge-registry/src/state.rs @@ -0,0 +1,316 @@ +//! State management for challenge hot-reload +//! +//! Provides state persistence and restoration to support +//! hot-reloading challenges without losing evaluation state. + +use crate::error::{RegistryError, RegistryResult}; +use parking_lot::RwLock; +use platform_core::ChallengeId; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Snapshot of challenge state at a point in time +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateSnapshot { + /// Challenge ID this snapshot belongs to + pub challenge_id: ChallengeId, + /// Version when snapshot was taken + pub version: String, + /// Timestamp when snapshot was created (millis) + pub created_at: i64, + /// Serialized state data + pub data: Vec, + /// Checksum for integrity verification + pub checksum: String, +} + +impl StateSnapshot { + /// Create a new state snapshot + pub fn new(challenge_id: ChallengeId, version: String, data: Vec) -> Self { + use sha2::{Digest, Sha256}; + + let mut hasher = Sha256::new(); + hasher.update(&data); + let checksum = hex::encode(hasher.finalize()); + + Self { + challenge_id, + version, + created_at: chrono::Utc::now().timestamp_millis(), + data, + checksum, + } + } + + /// Verify snapshot integrity + pub fn verify(&self) -> bool { + use sha2::{Digest, Sha256}; + + let mut hasher = Sha256::new(); + hasher.update(&self.data); + let computed = hex::encode(hasher.finalize()); + + computed == self.checksum + } + + /// Get the size of the snapshot data + pub fn size(&self) -> usize { + self.data.len() + } +} + +/// State of a challenge that can be preserved across hot-reloads +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeState { + /// Challenge ID + pub challenge_id: ChallengeId, + /// Active evaluations being tracked + pub active_evaluations: HashMap, + /// Completed evaluation count + pub completed_count: u64, + /// Last activity timestamp + pub last_activity_at: i64, + /// Custom state data from the challenge + pub custom_data: serde_json::Value, +} + +/// State of an in-progress evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationState { + /// Evaluation job ID + pub job_id: String, + /// When evaluation started (millis) + pub started_at: i64, + /// Current progress (0.0 - 1.0) + pub progress: f64, + /// Checkpoint data for resumption + pub checkpoint: Option>, +} + +impl ChallengeState { + /// Create new empty state for a challenge + pub fn new(challenge_id: ChallengeId) -> Self { + Self { + challenge_id, + active_evaluations: HashMap::new(), + completed_count: 0, + last_activity_at: chrono::Utc::now().timestamp_millis(), + custom_data: serde_json::Value::Null, + } + } + + /// Check if there are active evaluations + pub fn has_active_evaluations(&self) -> bool { + !self.active_evaluations.is_empty() + } + + /// Get count of active evaluations + pub fn active_evaluation_count(&self) -> usize { + self.active_evaluations.len() + } +} + +/// Store for challenge state with persistence support +#[derive(Debug)] +pub struct StateStore { + /// Challenge this store belongs to + challenge_id: ChallengeId, + /// In-memory state + state: RwLock, + /// Snapshots for recovery + snapshots: RwLock>, + /// Maximum snapshots to retain + max_snapshots: usize, +} + +impl StateStore { + /// Create a new state store for a challenge + pub fn new(challenge_id: ChallengeId) -> Self { + Self { + challenge_id, + state: RwLock::new(ChallengeState::new(challenge_id)), + snapshots: RwLock::new(Vec::new()), + max_snapshots: 5, + } + } + + /// Create a state store with custom snapshot limit + pub fn with_max_snapshots(challenge_id: ChallengeId, max_snapshots: usize) -> Self { + Self { + challenge_id, + state: RwLock::new(ChallengeState::new(challenge_id)), + snapshots: RwLock::new(Vec::new()), + max_snapshots, + } + } + + /// Get current state (read-only) + pub fn get_state(&self) -> ChallengeState { + self.state.read().clone() + } + + /// Update state with a function + pub fn update_state(&self, f: F) + where + F: FnOnce(&mut ChallengeState), + { + let mut state = self.state.write(); + f(&mut state); + state.last_activity_at = chrono::Utc::now().timestamp_millis(); + } + + /// Track a new evaluation + pub fn track_evaluation(&self, job_id: String) { + let mut state = self.state.write(); + state.active_evaluations.insert( + job_id.clone(), + EvaluationState { + job_id, + started_at: chrono::Utc::now().timestamp_millis(), + progress: 0.0, + checkpoint: None, + }, + ); + state.last_activity_at = chrono::Utc::now().timestamp_millis(); + } + + /// Update evaluation progress + pub fn update_evaluation_progress(&self, job_id: &str, progress: f64) { + let mut state = self.state.write(); + if let Some(eval) = state.active_evaluations.get_mut(job_id) { + eval.progress = progress.clamp(0.0, 1.0); + } + state.last_activity_at = chrono::Utc::now().timestamp_millis(); + } + + /// Complete an evaluation + pub fn complete_evaluation(&self, job_id: &str) { + let mut state = self.state.write(); + state.active_evaluations.remove(job_id); + state.completed_count += 1; + state.last_activity_at = chrono::Utc::now().timestamp_millis(); + } + + /// Create a snapshot of current state + pub fn create_snapshot(&self, version: String) -> RegistryResult { + let state = self.state.read(); + // Use JSON for serialization since ChallengeState contains serde_json::Value + let data = serde_json::to_vec(&*state) + .map_err(|e| RegistryError::StatePersistence(e.to_string()))?; + + let snapshot = StateSnapshot::new(self.challenge_id, version, data); + + let mut snapshots = self.snapshots.write(); + snapshots.push(snapshot.clone()); + + // Trim old snapshots + while snapshots.len() > self.max_snapshots { + snapshots.remove(0); + } + + Ok(snapshot) + } + + /// Restore state from a snapshot + pub fn restore_snapshot(&self, snapshot: &StateSnapshot) -> RegistryResult<()> { + if !snapshot.verify() { + return Err(RegistryError::StateRestoration( + "Snapshot checksum mismatch".to_string(), + )); + } + + // Use JSON for deserialization since ChallengeState contains serde_json::Value + let restored: ChallengeState = serde_json::from_slice(&snapshot.data) + .map_err(|e| RegistryError::StateRestoration(e.to_string()))?; + + let mut state = self.state.write(); + *state = restored; + + Ok(()) + } + + /// Get list of available snapshots + pub fn list_snapshots(&self) -> Vec { + self.snapshots.read().clone() + } + + /// Get the latest snapshot + pub fn latest_snapshot(&self) -> Option { + self.snapshots.read().last().cloned() + } + + /// Clear all state + pub fn clear(&self) { + let mut state = self.state.write(); + *state = ChallengeState::new(self.challenge_id); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_state_store() { + let id = ChallengeId::new(); + let store = StateStore::new(id); + + store.track_evaluation("job1".to_string()); + let state = store.get_state(); + assert_eq!(state.active_evaluation_count(), 1); + + store.update_evaluation_progress("job1", 0.5); + let state = store.get_state(); + let eval = state.active_evaluations.get("job1").unwrap(); + assert_eq!(eval.progress, 0.5); + + store.complete_evaluation("job1"); + let state = store.get_state(); + assert_eq!(state.active_evaluation_count(), 0); + assert_eq!(state.completed_count, 1); + } + + #[test] + fn test_snapshot_creation() { + let id = ChallengeId::new(); + let store = StateStore::new(id); + + store.track_evaluation("job1".to_string()); + let snapshot = store.create_snapshot("1.0.0".to_string()).unwrap(); + + assert!(snapshot.verify()); + assert_eq!(snapshot.version, "1.0.0"); + } + + #[test] + fn test_snapshot_restoration() { + let id = ChallengeId::new(); + let store = StateStore::new(id); + + store.track_evaluation("job1".to_string()); + store.track_evaluation("job2".to_string()); + let snapshot = store.create_snapshot("1.0.0".to_string()).unwrap(); + + // Clear and verify empty + store.clear(); + assert_eq!(store.get_state().active_evaluation_count(), 0); + + // Restore and verify + store.restore_snapshot(&snapshot).unwrap(); + assert_eq!(store.get_state().active_evaluation_count(), 2); + } + + #[test] + fn test_snapshot_limit() { + let id = ChallengeId::new(); + let store = StateStore::with_max_snapshots(id, 3); + + for i in 0..5 { + store.create_snapshot(format!("{}.0.0", i)).unwrap(); + } + + let snapshots = store.list_snapshots(); + assert_eq!(snapshots.len(), 3); + assert_eq!(snapshots[0].version, "2.0.0"); + } +} diff --git a/crates/challenge-registry/src/version.rs b/crates/challenge-registry/src/version.rs new file mode 100644 index 000000000..fdedd694a --- /dev/null +++ b/crates/challenge-registry/src/version.rs @@ -0,0 +1,408 @@ +//! Challenge versioning support + +use serde::{Deserialize, Serialize}; +use std::cmp::Ordering; +use std::fmt; + +/// Semantic version for challenges +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +pub struct ChallengeVersion { + pub major: u32, + pub minor: u32, + pub patch: u32, + pub prerelease: Option, +} + +impl ChallengeVersion { + pub fn new(major: u32, minor: u32, patch: u32) -> Self { + Self { + major, + minor, + patch, + prerelease: None, + } + } + + pub fn parse(s: &str) -> Option { + let s = s.strip_prefix('v').unwrap_or(s); + let parts: Vec<&str> = s.split('-').collect(); + let version_parts: Vec<&str> = parts[0].split('.').collect(); + + if version_parts.len() < 3 { + return None; + } + + Some(Self { + major: version_parts[0].parse().ok()?, + minor: version_parts[1].parse().ok()?, + patch: version_parts[2].parse().ok()?, + prerelease: parts.get(1).map(|s| s.to_string()), + }) + } + + /// Check if this version is compatible with another (same major version) + pub fn is_compatible_with(&self, other: &Self) -> bool { + self.major == other.major + } + + /// Check if this version is newer than another + pub fn is_newer_than(&self, other: &Self) -> bool { + self > other + } +} + +impl fmt::Display for ChallengeVersion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self.prerelease { + Some(pre) => write!(f, "{}.{}.{}-{}", self.major, self.minor, self.patch, pre), + None => write!(f, "{}.{}.{}", self.major, self.minor, self.patch), + } + } +} + +impl PartialOrd for ChallengeVersion { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for ChallengeVersion { + fn cmp(&self, other: &Self) -> Ordering { + match self.major.cmp(&other.major) { + Ordering::Equal => match self.minor.cmp(&other.minor) { + Ordering::Equal => self.patch.cmp(&other.patch), + ord => ord, + }, + ord => ord, + } + } +} + +impl Default for ChallengeVersion { + fn default() -> Self { + Self::new(0, 1, 0) + } +} + +/// Version constraint for challenge compatibility +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum VersionConstraint { + /// Exact version match + Exact(ChallengeVersion), + /// Minimum version (>=) + AtLeast(ChallengeVersion), + /// Version range [min, max) + Range { + min: ChallengeVersion, + max: ChallengeVersion, + }, + /// Compatible with major version (^) + Compatible(ChallengeVersion), + /// Any version + Any, +} + +impl VersionConstraint { + pub fn satisfies(&self, version: &ChallengeVersion) -> bool { + match self { + Self::Exact(v) => version == v, + Self::AtLeast(v) => version >= v, + Self::Range { min, max } => version >= min && version < max, + Self::Compatible(v) => version.major == v.major && version >= v, + Self::Any => true, + } + } +} + +/// A challenge with version information +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct VersionedChallenge { + pub challenge_id: String, + pub version: ChallengeVersion, + pub min_platform_version: Option, + pub deprecated: bool, + pub deprecation_message: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + #[test] + fn test_version_parsing() { + let v = ChallengeVersion::parse("1.2.3").unwrap(); + assert_eq!(v.major, 1); + assert_eq!(v.minor, 2); + assert_eq!(v.patch, 3); + + let v2 = ChallengeVersion::parse("v2.0.0-beta").unwrap(); + assert_eq!(v2.major, 2); + assert_eq!(v2.prerelease, Some("beta".to_string())); + } + + #[test] + fn test_version_comparison() { + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(1, 1, 0); + let v3 = ChallengeVersion::new(2, 0, 0); + + assert!(v2.is_newer_than(&v1)); + assert!(v3.is_newer_than(&v2)); + assert!(v1.is_compatible_with(&v2)); + assert!(!v1.is_compatible_with(&v3)); + } + + #[test] + fn test_version_constraints() { + let v = ChallengeVersion::new(1, 5, 0); + + assert!(VersionConstraint::Any.satisfies(&v)); + assert!(VersionConstraint::AtLeast(ChallengeVersion::new(1, 0, 0)).satisfies(&v)); + assert!(!VersionConstraint::Exact(ChallengeVersion::new(1, 0, 0)).satisfies(&v)); + assert!(VersionConstraint::Compatible(ChallengeVersion::new(1, 0, 0)).satisfies(&v)); + } + + #[test] + fn test_version_default() { + let v = ChallengeVersion::default(); + assert_eq!(v.major, 0); + assert_eq!(v.minor, 1); + assert_eq!(v.patch, 0); + assert_eq!(v.prerelease, None); + } + + #[test] + fn test_version_display() { + let v1 = ChallengeVersion::new(1, 2, 3); + assert_eq!(format!("{}", v1), "1.2.3"); + + let v2 = ChallengeVersion { + major: 2, + minor: 0, + patch: 0, + prerelease: Some("alpha".to_string()), + }; + assert_eq!(format!("{}", v2), "2.0.0-alpha"); + + let v3 = ChallengeVersion { + major: 0, + minor: 0, + patch: 1, + prerelease: Some("rc1".to_string()), + }; + assert_eq!(format!("{}", v3), "0.0.1-rc1"); + + let v4 = ChallengeVersion::new(10, 20, 30); + assert_eq!(v4.to_string(), "10.20.30"); + } + + #[test] + fn test_version_parsing_invalid() { + assert!(ChallengeVersion::parse("").is_none()); + assert!(ChallengeVersion::parse("1").is_none()); + assert!(ChallengeVersion::parse("1.2").is_none()); + assert!(ChallengeVersion::parse("a.b.c").is_none()); + assert!(ChallengeVersion::parse("1.2.x").is_none()); + assert!(ChallengeVersion::parse("-1.2.3").is_none()); + assert!(ChallengeVersion::parse("1.2.3.4").is_some()); // Extra parts are ignored + } + + #[test] + fn test_version_parsing_edge_cases() { + let v1 = ChallengeVersion::parse("0.0.0").unwrap(); + assert_eq!(v1.major, 0); + assert_eq!(v1.minor, 0); + assert_eq!(v1.patch, 0); + + let v2 = ChallengeVersion::parse("99.99.99").unwrap(); + assert_eq!(v2.major, 99); + assert_eq!(v2.minor, 99); + assert_eq!(v2.patch, 99); + + let v3 = ChallengeVersion::parse("v0.0.1").unwrap(); + assert_eq!(v3.major, 0); + assert_eq!(v3.minor, 0); + assert_eq!(v3.patch, 1); + + let v4 = ChallengeVersion::parse("1.0.0-beta.1").unwrap(); + assert_eq!(v4.prerelease, Some("beta.1".to_string())); + } + + #[test] + fn test_version_ordering() { + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(1, 0, 1); + let v3 = ChallengeVersion::new(1, 1, 0); + let v4 = ChallengeVersion::new(2, 0, 0); + let v5 = ChallengeVersion::new(0, 9, 9); + + assert!(v1 < v2); + assert!(v2 < v3); + assert!(v3 < v4); + assert!(v5 < v1); + + let mut versions = vec![v4.clone(), v2.clone(), v5.clone(), v1.clone(), v3.clone()]; + versions.sort(); + assert_eq!(versions, vec![v5, v1, v2, v3, v4]); + } + + #[test] + fn test_version_partial_ord() { + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(1, 0, 1); + let v3 = ChallengeVersion::new(1, 0, 0); + + assert_eq!(v1.partial_cmp(&v2), Some(Ordering::Less)); + assert_eq!(v2.partial_cmp(&v1), Some(Ordering::Greater)); + assert_eq!(v1.partial_cmp(&v3), Some(Ordering::Equal)); + } + + #[test] + fn test_version_equality() { + let v1 = ChallengeVersion::new(1, 2, 3); + let v2 = ChallengeVersion::new(1, 2, 3); + let v3 = ChallengeVersion::new(1, 2, 4); + + assert_eq!(v1, v2); + assert_ne!(v1, v3); + + let v4 = ChallengeVersion { + major: 1, + minor: 2, + patch: 3, + prerelease: Some("alpha".to_string()), + }; + let v5 = ChallengeVersion { + major: 1, + minor: 2, + patch: 3, + prerelease: Some("alpha".to_string()), + }; + let v6 = ChallengeVersion { + major: 1, + minor: 2, + patch: 3, + prerelease: Some("beta".to_string()), + }; + + assert_eq!(v4, v5); + assert_ne!(v4, v6); + assert_ne!(v1, v4); + } + + #[test] + fn test_version_hash() { + let mut map: HashMap = HashMap::new(); + + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(2, 0, 0); + let v3 = ChallengeVersion::new(1, 0, 0); + + map.insert(v1.clone(), "version_one"); + map.insert(v2.clone(), "version_two"); + + assert_eq!(map.get(&v1), Some(&"version_one")); + assert_eq!(map.get(&v2), Some(&"version_two")); + assert_eq!(map.get(&v3), Some(&"version_one")); + + map.insert(v3, "version_one_updated"); + assert_eq!(map.len(), 2); + assert_eq!( + map.get(&ChallengeVersion::new(1, 0, 0)), + Some(&"version_one_updated") + ); + } + + #[test] + fn test_version_constraint_range() { + let min = ChallengeVersion::new(1, 0, 0); + let max = ChallengeVersion::new(2, 0, 0); + let range = VersionConstraint::Range { + min: min.clone(), + max: max.clone(), + }; + + assert!(range.satisfies(&ChallengeVersion::new(1, 0, 0))); + assert!(range.satisfies(&ChallengeVersion::new(1, 5, 0))); + assert!(range.satisfies(&ChallengeVersion::new(1, 99, 99))); + assert!(!range.satisfies(&ChallengeVersion::new(2, 0, 0))); + assert!(!range.satisfies(&ChallengeVersion::new(0, 9, 9))); + assert!(!range.satisfies(&ChallengeVersion::new(3, 0, 0))); + + let tight_range = VersionConstraint::Range { + min: ChallengeVersion::new(1, 2, 3), + max: ChallengeVersion::new(1, 2, 5), + }; + assert!(!tight_range.satisfies(&ChallengeVersion::new(1, 2, 2))); + assert!(tight_range.satisfies(&ChallengeVersion::new(1, 2, 3))); + assert!(tight_range.satisfies(&ChallengeVersion::new(1, 2, 4))); + assert!(!tight_range.satisfies(&ChallengeVersion::new(1, 2, 5))); + } + + #[test] + fn test_versioned_challenge_creation() { + let challenge = VersionedChallenge { + challenge_id: "test-challenge".to_string(), + version: ChallengeVersion::new(1, 0, 0), + min_platform_version: Some(ChallengeVersion::new(0, 5, 0)), + deprecated: false, + deprecation_message: None, + }; + + assert_eq!(challenge.challenge_id, "test-challenge"); + assert_eq!(challenge.version, ChallengeVersion::new(1, 0, 0)); + assert_eq!( + challenge.min_platform_version, + Some(ChallengeVersion::new(0, 5, 0)) + ); + assert!(!challenge.deprecated); + assert!(challenge.deprecation_message.is_none()); + + let deprecated_challenge = VersionedChallenge { + challenge_id: "old-challenge".to_string(), + version: ChallengeVersion::new(0, 1, 0), + min_platform_version: None, + deprecated: true, + deprecation_message: Some("Use new-challenge instead".to_string()), + }; + + assert!(deprecated_challenge.deprecated); + assert_eq!( + deprecated_challenge.deprecation_message, + Some("Use new-challenge instead".to_string()) + ); + } + + #[test] + fn test_version_compatible_same_major() { + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(1, 1, 0); + let v3 = ChallengeVersion::new(1, 99, 99); + + assert!(v1.is_compatible_with(&v2)); + assert!(v2.is_compatible_with(&v1)); + assert!(v1.is_compatible_with(&v3)); + assert!(v3.is_compatible_with(&v1)); + assert!(v2.is_compatible_with(&v3)); + + let v0 = ChallengeVersion::new(0, 1, 0); + let v0_2 = ChallengeVersion::new(0, 2, 0); + assert!(v0.is_compatible_with(&v0_2)); + } + + #[test] + fn test_version_compatible_different_major() { + let v1 = ChallengeVersion::new(1, 0, 0); + let v2 = ChallengeVersion::new(2, 0, 0); + let v3 = ChallengeVersion::new(3, 5, 10); + let v0 = ChallengeVersion::new(0, 9, 9); + + assert!(!v1.is_compatible_with(&v2)); + assert!(!v2.is_compatible_with(&v1)); + assert!(!v1.is_compatible_with(&v3)); + assert!(!v2.is_compatible_with(&v3)); + assert!(!v0.is_compatible_with(&v1)); + assert!(!v1.is_compatible_with(&v0)); + } +} diff --git a/crates/challenge-sdk-wasm/Cargo.toml b/crates/challenge-sdk-wasm/Cargo.toml new file mode 100644 index 000000000..dbf51c8d0 --- /dev/null +++ b/crates/challenge-sdk-wasm/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "platform-challenge-sdk-wasm" +version.workspace = true +edition.workspace = true +description = "WASM Guest SDK for building challenges targeting wasm32-unknown-unknown" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } +bincode = { version = "1.3", default-features = false } + +[features] +default = [] +large-arena = [] # 4 MiB arena instead of the default 1 MiB +huge-arena = [] # 16 MiB arena for complex challenges with large payloads diff --git a/crates/challenge-sdk-wasm/src/alloc_impl.rs b/crates/challenge-sdk-wasm/src/alloc_impl.rs new file mode 100644 index 000000000..b4e7a9d78 --- /dev/null +++ b/crates/challenge-sdk-wasm/src/alloc_impl.rs @@ -0,0 +1,71 @@ +use core::cell::UnsafeCell; + +#[cfg(feature = "huge-arena")] +const ARENA_SIZE: usize = 16 * 1024 * 1024; + +#[cfg(all(feature = "large-arena", not(feature = "huge-arena")))] +const ARENA_SIZE: usize = 4 * 1024 * 1024; + +#[cfg(not(any(feature = "large-arena", feature = "huge-arena")))] +const ARENA_SIZE: usize = 1024 * 1024; + +struct BumpAllocator { + arena: UnsafeCell<[u8; ARENA_SIZE]>, + offset: UnsafeCell, +} + +unsafe impl Sync for BumpAllocator {} + +impl BumpAllocator { + const fn new() -> Self { + Self { + arena: UnsafeCell::new([0u8; ARENA_SIZE]), + offset: UnsafeCell::new(0), + } + } + + fn alloc(&self, size: usize, align: usize) -> *mut u8 { + unsafe { + let offset = &mut *self.offset.get(); + let aligned = (*offset + align - 1) & !(align - 1); + let new_offset = aligned + size; + if new_offset > ARENA_SIZE { + return core::ptr::null_mut(); + } + *offset = new_offset; + let arena = &mut *self.arena.get(); + arena.as_mut_ptr().add(aligned) + } + } + + fn reset(&self) { + unsafe { + *self.offset.get() = 0; + } + } +} + +static ALLOCATOR: BumpAllocator = BumpAllocator::new(); + +#[no_mangle] +pub extern "C" fn alloc(size: i32) -> i32 { + let ptr = ALLOCATOR.alloc(size as usize, 8); + if ptr.is_null() { + 0 + } else { + ptr as i32 + } +} + +#[no_mangle] +pub extern "C" fn allocate(size: i32, _align: i32) -> i32 { + alloc(size) +} + +pub fn sdk_alloc(size: usize) -> *mut u8 { + ALLOCATOR.alloc(size, 8) +} + +pub fn sdk_reset() { + ALLOCATOR.reset(); +} diff --git a/crates/challenge-sdk-wasm/src/host_functions.rs b/crates/challenge-sdk-wasm/src/host_functions.rs new file mode 100644 index 000000000..f0562338d --- /dev/null +++ b/crates/challenge-sdk-wasm/src/host_functions.rs @@ -0,0 +1,308 @@ +use alloc::vec; +use alloc::vec::Vec; + +const RESPONSE_BUF_SMALL: usize = 4096; +const RESPONSE_BUF_MEDIUM: usize = 64 * 1024; +const RESPONSE_BUF_LARGE: usize = 256 * 1024; + +#[link(wasm_import_module = "platform_network")] +extern "C" { + fn http_get(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32) -> i32; + fn http_post(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32, extra: i32) -> i32; + fn dns_resolve(req_ptr: i32, req_len: i32, resp_ptr: i32) -> i32; +} + +#[link(wasm_import_module = "platform_storage")] +extern "C" { + fn storage_get(key_ptr: i32, key_len: i32, value_ptr: i32) -> i32; + fn storage_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32) -> i32; +} + +#[link(wasm_import_module = "platform_terminal")] +extern "C" { + fn terminal_exec(cmd_ptr: i32, cmd_len: i32, result_ptr: i32, result_len: i32) -> i32; + fn terminal_read_file(path_ptr: i32, path_len: i32, buf_ptr: i32, buf_len: i32) -> i32; + fn terminal_write_file(path_ptr: i32, path_len: i32, data_ptr: i32, data_len: i32) -> i32; + fn terminal_list_dir(path_ptr: i32, path_len: i32, buf_ptr: i32, buf_len: i32) -> i32; + fn terminal_get_time() -> i64; + fn terminal_random_seed(buf_ptr: i32, buf_len: i32) -> i32; +} + +pub fn host_http_get(request: &[u8]) -> Result, i32> { + let mut response_buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { + http_get( + request.as_ptr() as i32, + request.len() as i32, + response_buf.as_mut_ptr() as i32, + response_buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + response_buf.truncate(status as usize); + Ok(response_buf) +} + +pub fn host_http_post(request: &[u8], body: &[u8]) -> Result, i32> { + let mut response_buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { + http_post( + request.as_ptr() as i32, + request.len() as i32, + response_buf.as_mut_ptr() as i32, + response_buf.len() as i32, + body.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + response_buf.truncate(status as usize); + Ok(response_buf) +} + +pub fn host_dns_resolve(request: &[u8]) -> Result, i32> { + let mut response_buf = vec![0u8; RESPONSE_BUF_SMALL]; + let status = unsafe { + dns_resolve( + request.as_ptr() as i32, + request.len() as i32, + response_buf.as_mut_ptr() as i32, + ) + }; + if status < 0 { + return Err(status); + } + response_buf.truncate(status as usize); + Ok(response_buf) +} + +pub fn host_storage_get(key: &[u8]) -> Result, i32> { + let mut value_buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { + storage_get( + key.as_ptr() as i32, + key.len() as i32, + value_buf.as_mut_ptr() as i32, + ) + }; + if status < 0 { + return Err(status); + } + value_buf.truncate(status as usize); + Ok(value_buf) +} + +pub fn host_storage_set(key: &[u8], value: &[u8]) -> Result<(), i32> { + let status = unsafe { + storage_set( + key.as_ptr() as i32, + key.len() as i32, + value.as_ptr() as i32, + value.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + Ok(()) +} + +pub fn host_terminal_exec(request: &[u8]) -> Result, i32> { + let mut result_buf = vec![0u8; RESPONSE_BUF_LARGE]; + let status = unsafe { + terminal_exec( + request.as_ptr() as i32, + request.len() as i32, + result_buf.as_mut_ptr() as i32, + result_buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + result_buf.truncate(status as usize); + Ok(result_buf) +} + +pub fn host_read_file(path: &[u8]) -> Result, i32> { + let mut buf = vec![0u8; RESPONSE_BUF_LARGE]; + let status = unsafe { + terminal_read_file( + path.as_ptr() as i32, + path.len() as i32, + buf.as_mut_ptr() as i32, + buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + buf.truncate(status as usize); + Ok(buf) +} + +pub fn host_write_file(path: &[u8], data: &[u8]) -> Result<(), i32> { + let status = unsafe { + terminal_write_file( + path.as_ptr() as i32, + path.len() as i32, + data.as_ptr() as i32, + data.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + Ok(()) +} + +pub fn host_list_dir(path: &[u8]) -> Result, i32> { + let mut buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { + terminal_list_dir( + path.as_ptr() as i32, + path.len() as i32, + buf.as_mut_ptr() as i32, + buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + buf.truncate(status as usize); + Ok(buf) +} + +pub fn host_get_time() -> i64 { + unsafe { terminal_get_time() } +} + +pub fn host_random_seed(buf: &mut [u8]) -> Result<(), i32> { + let status = unsafe { terminal_random_seed(buf.as_mut_ptr() as i32, buf.len() as i32) }; + if status < 0 { + return Err(status); + } + Ok(()) +} + +#[link(wasm_import_module = "platform_sandbox")] +extern "C" { + fn sandbox_exec(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32) -> i32; + fn get_timestamp() -> i64; + fn log_message(level: i32, msg_ptr: i32, msg_len: i32); +} + +pub fn host_sandbox_exec(request: &[u8]) -> Result, i32> { + let mut response_buf = vec![0u8; RESPONSE_BUF_LARGE]; + let status = unsafe { + sandbox_exec( + request.as_ptr() as i32, + request.len() as i32, + response_buf.as_mut_ptr() as i32, + response_buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + response_buf.truncate(status as usize); + Ok(response_buf) +} + +pub fn host_get_timestamp() -> i64 { + unsafe { get_timestamp() } +} + +pub fn host_log(level: u8, msg: &str) { + unsafe { log_message(level as i32, msg.as_ptr() as i32, msg.len() as i32) } +} + +#[link(wasm_import_module = "platform_llm")] +extern "C" { + fn llm_chat_completion(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32) -> i32; + fn llm_is_available() -> i32; +} + +pub fn host_llm_chat_completion(request: &[u8]) -> Result, i32> { + let mut response_buf = vec![0u8; RESPONSE_BUF_LARGE]; + let status = unsafe { + llm_chat_completion( + request.as_ptr() as i32, + request.len() as i32, + response_buf.as_mut_ptr() as i32, + response_buf.len() as i32, + ) + }; + if status < 0 { + return Err(status); + } + response_buf.truncate(status as usize); + Ok(response_buf) +} + +pub fn host_llm_is_available() -> bool { + unsafe { llm_is_available() == 1 } +} + +#[link(wasm_import_module = "platform_consensus")] +extern "C" { + fn consensus_get_epoch() -> i64; + fn consensus_get_validators(buf_ptr: i32, buf_len: i32) -> i32; + fn consensus_propose_weight(uid: i32, weight: i32) -> i32; + fn consensus_get_votes(buf_ptr: i32, buf_len: i32) -> i32; + fn consensus_get_state_hash(buf_ptr: i32) -> i32; + fn consensus_get_submission_count() -> i32; + fn consensus_get_block_height() -> i64; +} + +pub fn host_consensus_get_epoch() -> i64 { + unsafe { consensus_get_epoch() } +} + +pub fn host_consensus_get_validators() -> Result, i32> { + let mut buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { consensus_get_validators(buf.as_mut_ptr() as i32, buf.len() as i32) }; + if status < 0 { + return Err(status); + } + buf.truncate(status as usize); + Ok(buf) +} + +pub fn host_consensus_propose_weight(uid: i32, weight: i32) -> Result<(), i32> { + let status = unsafe { consensus_propose_weight(uid, weight) }; + if status < 0 { + return Err(status); + } + Ok(()) +} + +pub fn host_consensus_get_votes() -> Result, i32> { + let mut buf = vec![0u8; RESPONSE_BUF_MEDIUM]; + let status = unsafe { consensus_get_votes(buf.as_mut_ptr() as i32, buf.len() as i32) }; + if status < 0 { + return Err(status); + } + buf.truncate(status as usize); + Ok(buf) +} + +pub fn host_consensus_get_state_hash() -> Result<[u8; 32], i32> { + let mut buf = [0u8; 32]; + let status = unsafe { consensus_get_state_hash(buf.as_mut_ptr() as i32) }; + if status < 0 { + return Err(status); + } + Ok(buf) +} + +pub fn host_consensus_get_submission_count() -> i32 { + unsafe { consensus_get_submission_count() } +} + +pub fn host_consensus_get_block_height() -> i64 { + unsafe { consensus_get_block_height() } +} diff --git a/crates/challenge-sdk-wasm/src/lib.rs b/crates/challenge-sdk-wasm/src/lib.rs new file mode 100644 index 000000000..0037a58a0 --- /dev/null +++ b/crates/challenge-sdk-wasm/src/lib.rs @@ -0,0 +1,267 @@ +#![no_std] + +extern crate alloc; + +pub mod alloc_impl; +pub mod host_functions; +pub mod llm_types; +pub mod types; + +pub use llm_types::{LlmMessage, LlmRequest, LlmResponse, LlmUsage}; +pub use types::{ + score_f64_scaled, SandboxExecRequest, SandboxExecResponse, TaskDefinition, TaskResult, +}; +pub use types::{EvaluationInput, EvaluationOutput}; +pub use types::{WasmRouteDefinition, WasmRouteRequest, WasmRouteResponse}; + +pub trait Challenge { + fn name(&self) -> &'static str; + fn version(&self) -> &'static str; + fn evaluate(&self, input: EvaluationInput) -> EvaluationOutput; + fn validate(&self, input: EvaluationInput) -> bool; + + fn generate_task(&self, _params: &[u8]) -> alloc::vec::Vec { + alloc::vec::Vec::new() + } + + fn setup_environment(&self, _config: &[u8]) -> bool { + true + } + + fn tasks(&self) -> alloc::vec::Vec { + alloc::vec::Vec::new() + } + + fn configure(&self, _config: &[u8]) {} + + /// Return serialized [`WasmRouteDefinition`]s describing the HTTP routes + /// this challenge exposes. The default implementation returns an empty + /// vector (no custom routes). + fn routes(&self) -> alloc::vec::Vec { + alloc::vec::Vec::new() + } + + /// Handle an incoming route request and return a serialized + /// [`WasmRouteResponse`]. The `request` parameter is a bincode-encoded + /// [`WasmRouteRequest`]. The default implementation returns an empty + /// vector. + fn handle_route(&self, _request: &[u8]) -> alloc::vec::Vec { + alloc::vec::Vec::new() + } +} + +/// Pack a pointer and length into a single i64 value. +/// +/// The high 32 bits hold the length and the low 32 bits hold the pointer. +/// The host runtime uses this convention to locate serialized data in WASM +/// linear memory. +pub fn pack_ptr_len(ptr: i32, len: i32) -> i64 { + ((len as i64) << 32) | ((ptr as u32) as i64) +} + +/// Register a [`Challenge`] implementation and export the required WASM ABI +/// functions (`evaluate`, `validate`, `get_name`, `get_version`, +/// `generate_task`, `setup_environment`, `get_tasks`, `configure`, +/// `get_routes`, `handle_route`, and `alloc`). +/// +/// The type must provide a `const fn new() -> Self` constructor so that the +/// challenge instance can be placed in a `static`. +/// +/// # Usage +/// +/// ```ignore +/// struct MyChallenge; +/// +/// impl MyChallenge { +/// pub const fn new() -> Self { Self } +/// } +/// +/// impl platform_challenge_sdk_wasm::Challenge for MyChallenge { +/// fn name(&self) -> &'static str { "my-challenge" } +/// fn version(&self) -> &'static str { "0.1.0" } +/// fn evaluate(&self, input: EvaluationInput) -> EvaluationOutput { +/// EvaluationOutput::success(100, "ok") +/// } +/// fn validate(&self, input: EvaluationInput) -> bool { true } +/// } +/// +/// platform_challenge_sdk_wasm::register_challenge!(MyChallenge); +/// ``` +/// +/// A custom const initializer can be supplied when `Default::default()` is not +/// const-evaluable: +/// +/// ```ignore +/// platform_challenge_sdk_wasm::register_challenge!(MyChallenge, MyChallenge::new()); +/// ``` +#[macro_export] +macro_rules! register_challenge { + ($ty:ty) => { + $crate::register_challenge!($ty, <$ty as Default>::default()); + }; + ($ty:ty, $init:expr) => { + static _CHALLENGE: $ty = $init; + + #[no_mangle] + pub extern "C" fn evaluate(agent_ptr: i32, agent_len: i32) -> i64 { + let slice = + unsafe { core::slice::from_raw_parts(agent_ptr as *const u8, agent_len as usize) }; + let input: $crate::EvaluationInput = match bincode::deserialize(slice) { + Ok(v) => v, + Err(_) => { + return $crate::pack_ptr_len(0, 0); + } + }; + let output = <$ty as $crate::Challenge>::evaluate(&_CHALLENGE, input); + let encoded = match bincode::serialize(&output) { + Ok(v) => v, + Err(_) => { + return $crate::pack_ptr_len(0, 0); + } + }; + let ptr = $crate::alloc_impl::sdk_alloc(encoded.len()); + if ptr.is_null() { + return $crate::pack_ptr_len(0, 0); + } + unsafe { + core::ptr::copy_nonoverlapping(encoded.as_ptr(), ptr, encoded.len()); + } + $crate::pack_ptr_len(ptr as i32, encoded.len() as i32) + } + + #[no_mangle] + pub extern "C" fn validate(agent_ptr: i32, agent_len: i32) -> i32 { + let slice = + unsafe { core::slice::from_raw_parts(agent_ptr as *const u8, agent_len as usize) }; + let input: $crate::EvaluationInput = match bincode::deserialize(slice) { + Ok(v) => v, + Err(_) => return 0, + }; + if <$ty as $crate::Challenge>::validate(&_CHALLENGE, input) { + 1 + } else { + 0 + } + } + + #[no_mangle] + pub extern "C" fn get_name() -> i32 { + let name = <$ty as $crate::Challenge>::name(&_CHALLENGE); + let ptr = $crate::alloc_impl::sdk_alloc(4 + name.len()); + if ptr.is_null() { + return 0; + } + let len_bytes = (name.len() as u32).to_le_bytes(); + unsafe { + core::ptr::copy_nonoverlapping(len_bytes.as_ptr(), ptr, 4); + core::ptr::copy_nonoverlapping(name.as_ptr(), ptr.add(4), name.len()); + } + ptr as i32 + } + + #[no_mangle] + pub extern "C" fn get_version() -> i32 { + let ver = <$ty as $crate::Challenge>::version(&_CHALLENGE); + let ptr = $crate::alloc_impl::sdk_alloc(4 + ver.len()); + if ptr.is_null() { + return 0; + } + let len_bytes = (ver.len() as u32).to_le_bytes(); + unsafe { + core::ptr::copy_nonoverlapping(len_bytes.as_ptr(), ptr, 4); + core::ptr::copy_nonoverlapping(ver.as_ptr(), ptr.add(4), ver.len()); + } + ptr as i32 + } + + #[no_mangle] + pub extern "C" fn generate_task(params_ptr: i32, params_len: i32) -> i64 { + let slice = unsafe { + core::slice::from_raw_parts(params_ptr as *const u8, params_len as usize) + }; + let output = <$ty as $crate::Challenge>::generate_task(&_CHALLENGE, slice); + if output.is_empty() { + return $crate::pack_ptr_len(0, 0); + } + let ptr = $crate::alloc_impl::sdk_alloc(output.len()); + if ptr.is_null() { + return $crate::pack_ptr_len(0, 0); + } + unsafe { + core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); + } + $crate::pack_ptr_len(ptr as i32, output.len() as i32) + } + + #[no_mangle] + pub extern "C" fn setup_environment(config_ptr: i32, config_len: i32) -> i32 { + let slice = unsafe { + core::slice::from_raw_parts(config_ptr as *const u8, config_len as usize) + }; + if <$ty as $crate::Challenge>::setup_environment(&_CHALLENGE, slice) { + 1 + } else { + 0 + } + } + + #[no_mangle] + pub extern "C" fn get_tasks() -> i64 { + let output = <$ty as $crate::Challenge>::tasks(&_CHALLENGE); + if output.is_empty() { + return $crate::pack_ptr_len(0, 0); + } + let ptr = $crate::alloc_impl::sdk_alloc(output.len()); + if ptr.is_null() { + return $crate::pack_ptr_len(0, 0); + } + unsafe { + core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); + } + $crate::pack_ptr_len(ptr as i32, output.len() as i32) + } + + #[no_mangle] + pub extern "C" fn configure(config_ptr: i32, config_len: i32) -> i32 { + let slice = unsafe { + core::slice::from_raw_parts(config_ptr as *const u8, config_len as usize) + }; + <$ty as $crate::Challenge>::configure(&_CHALLENGE, slice); + 1 + } + + #[no_mangle] + pub extern "C" fn get_routes() -> i64 { + let output = <$ty as $crate::Challenge>::routes(&_CHALLENGE); + if output.is_empty() { + return $crate::pack_ptr_len(0, 0); + } + let ptr = $crate::alloc_impl::sdk_alloc(output.len()); + if ptr.is_null() { + return $crate::pack_ptr_len(0, 0); + } + unsafe { + core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); + } + $crate::pack_ptr_len(ptr as i32, output.len() as i32) + } + + #[no_mangle] + pub extern "C" fn handle_route(req_ptr: i32, req_len: i32) -> i64 { + let slice = + unsafe { core::slice::from_raw_parts(req_ptr as *const u8, req_len as usize) }; + let output = <$ty as $crate::Challenge>::handle_route(&_CHALLENGE, slice); + if output.is_empty() { + return $crate::pack_ptr_len(0, 0); + } + let ptr = $crate::alloc_impl::sdk_alloc(output.len()); + if ptr.is_null() { + return $crate::pack_ptr_len(0, 0); + } + unsafe { + core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); + } + $crate::pack_ptr_len(ptr as i32, output.len() as i32) + } + }; +} diff --git a/crates/challenge-sdk-wasm/src/llm_types.rs b/crates/challenge-sdk-wasm/src/llm_types.rs new file mode 100644 index 000000000..76a2ec778 --- /dev/null +++ b/crates/challenge-sdk-wasm/src/llm_types.rs @@ -0,0 +1,30 @@ +use alloc::string::String; +use alloc::vec::Vec; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LlmRequest { + pub model: String, + pub messages: Vec, + pub max_tokens: u32, + pub temperature: f32, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LlmMessage { + pub role: String, + pub content: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LlmResponse { + pub content: String, + pub usage: Option, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LlmUsage { + pub prompt_tokens: u32, + pub completion_tokens: u32, + pub total_tokens: u32, +} diff --git a/crates/challenge-sdk-wasm/src/types.rs b/crates/challenge-sdk-wasm/src/types.rs new file mode 100644 index 000000000..454dd7ccc --- /dev/null +++ b/crates/challenge-sdk-wasm/src/types.rs @@ -0,0 +1,170 @@ +use alloc::string::String; +use alloc::vec::Vec; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationInput { + pub agent_data: Vec, + pub challenge_id: String, + pub params: Vec, + pub task_definition: Option>, + pub environment_config: Option>, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationOutput { + pub score: i64, + pub valid: bool, + pub message: String, + pub metrics: Option>, + pub details: Option>, +} + +impl EvaluationOutput { + pub fn success(score: i64, message: &str) -> Self { + Self { + score, + valid: true, + message: String::from(message), + metrics: None, + details: None, + } + } + + pub fn failure(message: &str) -> Self { + Self { + score: 0, + valid: false, + message: String::from(message), + metrics: None, + details: None, + } + } + + pub fn with_metrics(mut self, metrics: Vec) -> Self { + self.metrics = Some(metrics); + self + } + + pub fn with_details(mut self, details: Vec) -> Self { + self.details = Some(details); + self + } +} + +pub fn score_f64_scaled(value: f64) -> i64 { + (value * 10_000.0) as i64 +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskDefinition { + pub task_id: String, + pub description: String, + pub command: String, + pub expected_output: Option, + pub timeout_ms: u64, + pub scoring_criteria: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxExecRequest { + pub command: String, + pub args: Vec, + pub env_vars: Vec<(String, String)>, + pub working_dir: Option, + pub stdin: Option>, + pub timeout_ms: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SandboxExecResponse { + pub exit_code: i32, + pub stdout: Vec, + pub stderr: Vec, + pub duration_ms: u64, +} + +impl SandboxExecResponse { + pub fn is_success(&self) -> bool { + self.exit_code == 0 + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskResult { + pub task_id: String, + pub passed: bool, + pub score: f64, + pub output: Option, + pub metrics: Option>, +} + +impl TaskResult { + pub fn success(task_id: &str, score: f64) -> Self { + Self { + task_id: String::from(task_id), + passed: true, + score, + output: None, + metrics: None, + } + } + + pub fn failure(task_id: &str, output: &str) -> Self { + Self { + task_id: String::from(task_id), + passed: false, + score: 0.0, + output: Some(String::from(output)), + metrics: None, + } + } +} + +/// Definition of a route exposed by a WASM challenge module. +/// +/// Challenge implementations return a serialized list of these definitions from +/// [Challenge::routes] so the validator can register HTTP endpoints. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmRouteDefinition { + /// HTTP method (e.g. "GET", "POST"). + pub method: String, + /// URL path pattern (e.g. "/status", "/submit"). + pub path: String, + /// Human-readable description of the route. + pub description: String, + /// Whether the route requires hotkey authentication. + pub requires_auth: bool, +} + +/// Incoming request forwarded to a WASM challenge route handler. +/// +/// The validator serializes this struct and passes it to +/// [Challenge::handle_route]. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmRouteRequest { + /// HTTP method of the incoming request. + pub method: String, + /// Matched URL path. + pub path: String, + /// Path parameters extracted from the URL pattern. + pub params: Vec<(String, String)>, + /// Query-string key/value pairs. + pub query: Vec<(String, String)>, + /// Raw request body bytes. + pub body: Vec, + /// Authenticated caller hotkey, if present. + pub auth_hotkey: Option, +} + +/// Response returned by a WASM challenge route handler. +/// +/// The WASM module serializes this struct and returns it from +/// [Challenge::handle_route]. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmRouteResponse { + /// HTTP status code to return to the caller. + pub status: u16, + /// Raw response body bytes. + pub body: Vec, +} diff --git a/crates/challenge-sdk/Cargo.toml b/crates/challenge-sdk/Cargo.toml new file mode 100644 index 000000000..260ae3ab3 --- /dev/null +++ b/crates/challenge-sdk/Cargo.toml @@ -0,0 +1,51 @@ +[package] +name = "platform-challenge-sdk" +version.workspace = true +edition.workspace = true +description = "SDK for developing challenges on Platform Network" + +[features] +default = [] +# Enable HTTP server mode (requires axum) +http-server = ["axum", "tower", "tower-http"] + +[dependencies] +platform-core = { path = "../core" } + +# Async +tokio = { workspace = true } +tokio-tungstenite = { version = "0.24", features = ["native-tls"] } +async-trait = { workspace = true } +futures = { workspace = true } + +# Web server (optional, for http-server feature) +axum = { version = "0.7", features = ["ws", "json"], optional = true } +tower = { version = "0.5", optional = true } +tower-http = { version = "0.6", features = ["cors"], optional = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Database (each challenge gets its own) +sled = { workspace = true } + +# Crypto +sha2 = { workspace = true } +hex = { workspace = true } +sp-core = { workspace = true } +parity-scale-codec = { workspace = true } +aes-gcm = "0.10" +rand = { workspace = true } + +# Utils +uuid = { workspace = true } +chrono = { workspace = true } +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +parking_lot = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } diff --git a/crates/challenge-sdk/src/data.rs b/crates/challenge-sdk/src/data.rs new file mode 100644 index 000000000..005a4608c --- /dev/null +++ b/crates/challenge-sdk/src/data.rs @@ -0,0 +1,618 @@ +//! Challenge Data Submission System +//! +//! Enables challenges to define what data validators can store +//! and how it should be verified. +//! +//! # Example +//! +//! ```text +//! use platform_challenge_sdk::data::*; +//! +//! impl Challenge for MyChallenge { +//! // Define what data keys are allowed +//! fn allowed_data_keys(&self) -> Vec { +//! vec![ +//! DataKeySpec::new("score") +//! .validator_scoped() +//! .with_schema(json!({"type": "number", "minimum": 0, "maximum": 100})), +//! DataKeySpec::new("leaderboard") +//! .challenge_scoped() +//! .max_size(1024 * 1024) +//! .ttl_blocks(100), +//! ] +//! } +//! +//! // Verify submitted data +//! async fn verify_data(&self, ctx: &ChallengeContext, submission: &DataSubmission) -> DataVerification { +//! match submission.key.as_str() { +//! "score" => { +//! // Verify score is valid +//! if let Ok(score) = serde_json::from_slice::(&submission.value) { +//! if score >= 0.0 && score <= 100.0 { +//! return DataVerification::accept(); +//! } +//! } +//! DataVerification::reject("Invalid score format") +//! } +//! _ => DataVerification::reject("Unknown key"), +//! } +//! } +//! } +//! ``` + +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; + +/// Specification for a data key that validators can write to +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DataKeySpec { + /// Key name + pub key: String, + /// Scope of the data + pub scope: DataScope, + /// Maximum size in bytes (0 = unlimited) + pub max_size: usize, + /// TTL in blocks (0 = permanent) + pub ttl_blocks: u64, + /// JSON schema for validation (optional) + pub schema: Option, + /// Description + pub description: String, + /// Whether this key requires consensus + pub requires_consensus: bool, + /// Minimum validators needed for consensus + pub min_consensus: usize, +} + +impl DataKeySpec { + /// Create a new data key spec + pub fn new(key: impl Into) -> Self { + Self { + key: key.into(), + scope: DataScope::Validator, + max_size: 0, + ttl_blocks: 0, + schema: None, + description: String::new(), + requires_consensus: true, + min_consensus: 1, + } + } + + /// Set scope to validator (each validator has own value) + pub fn validator_scoped(mut self) -> Self { + self.scope = DataScope::Validator; + self + } + + /// Set scope to challenge (single value for entire challenge) + pub fn challenge_scoped(mut self) -> Self { + self.scope = DataScope::Challenge; + self + } + + /// Set scope to global (shared across challenges) + pub fn global_scoped(mut self) -> Self { + self.scope = DataScope::Global; + self + } + + /// Set maximum size in bytes + pub fn max_size(mut self, size: usize) -> Self { + self.max_size = size; + self + } + + /// Set TTL in blocks + pub fn ttl_blocks(mut self, blocks: u64) -> Self { + self.ttl_blocks = blocks; + self + } + + /// Set JSON schema for validation + pub fn with_schema(mut self, schema: Value) -> Self { + self.schema = Some(schema); + self + } + + /// Set description + pub fn with_description(mut self, desc: impl Into) -> Self { + self.description = desc.into(); + self + } + + /// Disable consensus requirement + pub fn no_consensus(mut self) -> Self { + self.requires_consensus = false; + self + } + + /// Set minimum consensus validators + pub fn min_consensus(mut self, count: usize) -> Self { + self.min_consensus = count; + self + } +} + +/// Scope of data storage +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub enum DataScope { + /// Each validator has their own value + Validator, + /// Single value for the entire challenge + Challenge, + /// Shared across all challenges + Global, +} + +/// Data submission from a validator +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DataSubmission { + /// Key being written to + pub key: String, + /// Value to store + pub value: Vec, + /// Submitting validator + pub validator: String, + /// Block height + pub block_height: u64, + /// Epoch + pub epoch: u64, + /// Additional metadata + pub metadata: HashMap, +} + +impl DataSubmission { + /// Create a new submission + pub fn new(key: impl Into, value: Vec, validator: impl Into) -> Self { + Self { + key: key.into(), + value, + validator: validator.into(), + block_height: 0, + epoch: 0, + metadata: HashMap::new(), + } + } + + /// Set block height + pub fn at_block(mut self, height: u64) -> Self { + self.block_height = height; + self + } + + /// Set epoch + pub fn at_epoch(mut self, epoch: u64) -> Self { + self.epoch = epoch; + self + } + + /// Add metadata + pub fn with_metadata(mut self, key: impl Into, value: Value) -> Self { + self.metadata.insert(key.into(), value); + self + } + + /// Parse value as JSON + pub fn value_json Deserialize<'de>>(&self) -> Result { + serde_json::from_slice(&self.value) + } + + /// Parse value as string + pub fn value_string(&self) -> Result { + String::from_utf8(self.value.clone()) + } +} + +/// Result of data verification +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DataVerification { + /// Whether to accept the data + pub accepted: bool, + /// Reason for rejection (if rejected) + pub reason: Option, + /// Transform the value before storing + pub transformed_value: Option>, + /// Override TTL (blocks) + pub ttl_override: Option, + /// Additional data to emit as events + pub events: Vec, +} + +impl DataVerification { + /// Accept the data + pub fn accept() -> Self { + Self { + accepted: true, + reason: None, + transformed_value: None, + ttl_override: None, + events: vec![], + } + } + + /// Reject the data + pub fn reject(reason: impl Into) -> Self { + Self { + accepted: false, + reason: Some(reason.into()), + transformed_value: None, + ttl_override: None, + events: vec![], + } + } + + /// Accept with transformed value + pub fn accept_with_transform(value: Vec) -> Self { + Self { + accepted: true, + reason: None, + transformed_value: Some(value), + ttl_override: None, + events: vec![], + } + } + + /// Set TTL override + pub fn with_ttl(mut self, blocks: u64) -> Self { + self.ttl_override = Some(blocks); + self + } + + /// Add an event to emit + pub fn with_event(mut self, event: DataEvent) -> Self { + self.events.push(event); + self + } +} + +/// Event emitted during data verification +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DataEvent { + /// Event type + pub event_type: String, + /// Event data + pub data: Value, +} + +impl DataEvent { + /// Create a new event + pub fn new(event_type: impl Into, data: Value) -> Self { + Self { + event_type: event_type.into(), + data, + } + } +} + +/// Stored data entry +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StoredData { + /// Key + pub key: String, + /// Value + pub value: Vec, + /// Scope + pub scope: DataScope, + /// Validator who submitted (for Validator scope) + pub validator: Option, + /// Block when stored + pub stored_at_block: u64, + /// Block when expires (if any) + pub expires_at_block: Option, + /// Version (incremented on update) + pub version: u64, +} + +impl StoredData { + /// Check if expired + pub fn is_expired(&self, current_block: u64) -> bool { + self.expires_at_block + .map(|e| current_block >= e) + .unwrap_or(false) + } + + /// Parse value as JSON + pub fn value_json Deserialize<'de>>(&self) -> Result { + serde_json::from_slice(&self.value) + } +} + +/// Query for retrieving stored data +#[derive(Debug, Clone)] +pub struct DataQuery { + /// Key pattern (supports * wildcard) + pub key_pattern: Option, + /// Scope filter + pub scope: Option, + /// Validator filter (for Validator scope) + pub validator: Option, + /// Include expired + pub include_expired: bool, + /// Limit results + pub limit: Option, + /// Offset for pagination + pub offset: Option, +} + +impl DataQuery { + /// Create a new query + pub fn new() -> Self { + Self { + key_pattern: None, + scope: None, + validator: None, + include_expired: false, + limit: None, + offset: None, + } + } + + /// Filter by key pattern + pub fn key(mut self, pattern: impl Into) -> Self { + self.key_pattern = Some(pattern.into()); + self + } + + /// Filter by scope + pub fn scope(mut self, scope: DataScope) -> Self { + self.scope = Some(scope); + self + } + + /// Filter by validator + pub fn validator(mut self, validator: impl Into) -> Self { + self.validator = Some(validator.into()); + self + } + + /// Include expired entries + pub fn include_expired(mut self) -> Self { + self.include_expired = true; + self + } + + /// Limit results + pub fn limit(mut self, limit: usize) -> Self { + self.limit = Some(limit); + self + } + + /// Offset for pagination + pub fn offset(mut self, offset: usize) -> Self { + self.offset = Some(offset); + self + } +} + +impl Default for DataQuery { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn test_data_key_spec() { + let spec = DataKeySpec::new("score") + .validator_scoped() + .max_size(1024) + .ttl_blocks(100) + .with_description("Player score"); + + assert_eq!(spec.key, "score"); + assert_eq!(spec.scope, DataScope::Validator); + assert_eq!(spec.max_size, 1024); + assert_eq!(spec.ttl_blocks, 100); + } + + #[test] + fn test_challenge_scoped() { + let spec = DataKeySpec::new("leaderboard").challenge_scoped(); + assert_eq!(spec.scope, DataScope::Challenge); + } + + #[test] + fn test_global_scoped() { + let spec = DataKeySpec::new("global_config").global_scoped(); + assert_eq!(spec.scope, DataScope::Global); + } + + #[test] + fn test_with_schema() { + let schema = json!({"type": "number", "minimum": 0}); + let spec = DataKeySpec::new("score").with_schema(schema.clone()); + assert_eq!(spec.schema, Some(schema)); + } + + #[test] + fn test_no_consensus() { + let spec = DataKeySpec::new("local_data").no_consensus(); + assert!(!spec.requires_consensus); + } + + #[test] + fn test_min_consensus() { + let spec = DataKeySpec::new("important_data").min_consensus(5); + assert_eq!(spec.min_consensus, 5); + } + + #[test] + fn test_data_verification() { + let accept = DataVerification::accept(); + assert!(accept.accepted); + + let reject = DataVerification::reject("Bad data"); + assert!(!reject.accepted); + assert_eq!(reject.reason, Some("Bad data".to_string())); + } + + #[test] + fn test_accept_with_transform() { + let transformed = vec![4, 5, 6]; + let verification = DataVerification::accept_with_transform(transformed.clone()); + assert!(verification.accepted); + assert_eq!(verification.transformed_value, Some(transformed)); + } + + #[test] + fn test_with_ttl() { + let verification = DataVerification::accept().with_ttl(500); + assert_eq!(verification.ttl_override, Some(500)); + } + + #[test] + fn test_with_event() { + let event = DataEvent::new("update", json!({"key": "value"})); + let verification = DataVerification::accept().with_event(event.clone()); + assert_eq!(verification.events.len(), 1); + assert_eq!(verification.events[0].event_type, "update"); + } + + #[test] + fn test_data_event_new() { + let event = DataEvent::new("test_event", json!({"data": 123})); + assert_eq!(event.event_type, "test_event"); + assert_eq!(event.data, json!({"data": 123})); + } + + #[test] + fn test_data_submission() { + let sub = DataSubmission::new("score", vec![1, 2, 3], "validator1") + .at_block(100) + .at_epoch(5); + + assert_eq!(sub.key, "score"); + assert_eq!(sub.block_height, 100); + assert_eq!(sub.epoch, 5); + } + + #[test] + fn test_data_submission_with_metadata() { + let sub = DataSubmission::new("score", vec![1, 2, 3], "validator1") + .with_metadata("source", json!("test")); + + assert_eq!(sub.metadata.get("source"), Some(&json!("test"))); + } + + #[test] + fn test_value_json() { + let data = json!({"score": 85}); + let json_str = serde_json::to_vec(&data).unwrap(); + let sub = DataSubmission::new("score", json_str, "validator1"); + + let parsed: serde_json::Value = sub.value_json().unwrap(); + assert_eq!(parsed, data); + } + + #[test] + fn test_value_string() { + let text = "Hello, World!"; + let sub = DataSubmission::new("message", text.as_bytes().to_vec(), "validator1"); + + let parsed = sub.value_string().unwrap(); + assert_eq!(parsed, text); + } + + #[test] + fn test_stored_data_is_expired() { + let stored = StoredData { + key: "test".to_string(), + value: vec![1, 2, 3], + scope: DataScope::Validator, + validator: Some("validator1".to_string()), + stored_at_block: 100, + expires_at_block: Some(200), + version: 1, + }; + + assert!(!stored.is_expired(150)); + assert!(stored.is_expired(200)); + assert!(stored.is_expired(250)); + + // Test permanent storage (no expiry) + let permanent = StoredData { + expires_at_block: None, + ..stored + }; + assert!(!permanent.is_expired(1000000)); + } + + #[test] + fn test_stored_data_value_json() { + let data = json!({"result": "success"}); + let json_bytes = serde_json::to_vec(&data).unwrap(); + + let stored = StoredData { + key: "result".to_string(), + value: json_bytes, + scope: DataScope::Challenge, + validator: None, + stored_at_block: 100, + expires_at_block: None, + version: 1, + }; + + let parsed: serde_json::Value = stored.value_json().unwrap(); + assert_eq!(parsed, data); + } + + #[test] + fn test_data_query_new() { + let query = DataQuery::new(); + assert!(query.key_pattern.is_none()); + assert!(query.scope.is_none()); + assert!(query.validator.is_none()); + assert!(!query.include_expired); + assert!(query.limit.is_none()); + assert!(query.offset.is_none()); + } + + #[test] + fn test_data_query_key() { + let query = DataQuery::new().key("score*"); + assert_eq!(query.key_pattern, Some("score*".to_string())); + } + + #[test] + fn test_data_query_scope() { + let query = DataQuery::new().scope(DataScope::Challenge); + assert_eq!(query.scope, Some(DataScope::Challenge)); + } + + #[test] + fn test_data_query_validator() { + let query = DataQuery::new().validator("validator1"); + assert_eq!(query.validator, Some("validator1".to_string())); + } + + #[test] + fn test_data_query_include_expired() { + let query = DataQuery::new().include_expired(); + assert!(query.include_expired); + } + + #[test] + fn test_data_query_limit() { + let query = DataQuery::new().limit(50); + assert_eq!(query.limit, Some(50)); + } + + #[test] + fn test_data_query_offset() { + let query = DataQuery::new().offset(100); + assert_eq!(query.offset, Some(100)); + } + + #[test] + fn test_data_query_default() { + let query = DataQuery::default(); + assert!(query.key_pattern.is_none()); + assert!(!query.include_expired); + } +} diff --git a/crates/challenge-sdk/src/database.rs b/crates/challenge-sdk/src/database.rs new file mode 100644 index 000000000..1484c3b9f --- /dev/null +++ b/crates/challenge-sdk/src/database.rs @@ -0,0 +1,579 @@ +//! Challenge-specific database +//! +//! Each challenge gets its own isolated sled database. + +use crate::{AgentInfo, ChallengeError, ChallengeId, EvaluationResult, Result}; +use serde::{de::DeserializeOwned, Serialize}; +use std::path::Path; + +/// Challenge database (sled-based, isolated per challenge) +pub struct ChallengeDatabase { + db: sled::Db, + challenge_id: ChallengeId, + + // Pre-opened trees for common operations + agents_tree: sled::Tree, + results_tree: sled::Tree, + kv_tree: sled::Tree, + meta_tree: sled::Tree, +} + +impl ChallengeDatabase { + /// Open or create a database for a challenge + pub fn open>(base_path: P, challenge_id: ChallengeId) -> Result { + let db_path = base_path + .as_ref() + .join(format!("challenge_{}", challenge_id)); + + let db = sled::open(&db_path) + .map_err(|e| ChallengeError::Database(format!("Failed to open database: {}", e)))?; + + let agents_tree = db + .open_tree("agents") + .map_err(|e| ChallengeError::Database(format!("Failed to open agents tree: {}", e)))?; + + let results_tree = db + .open_tree("results") + .map_err(|e| ChallengeError::Database(format!("Failed to open results tree: {}", e)))?; + + let kv_tree = db + .open_tree("kv") + .map_err(|e| ChallengeError::Database(format!("Failed to open kv tree: {}", e)))?; + + let meta_tree = db + .open_tree("meta") + .map_err(|e| ChallengeError::Database(format!("Failed to open meta tree: {}", e)))?; + + tracing::info!("Opened challenge database at {:?}", db_path); + + Ok(Self { + db, + challenge_id, + agents_tree, + results_tree, + kv_tree, + meta_tree, + }) + } + + /// Get challenge ID + pub fn challenge_id(&self) -> ChallengeId { + self.challenge_id + } + + // ==================== Agents ==================== + + /// Save agent information + pub fn save_agent(&self, agent: &AgentInfo) -> Result<()> { + let data = + bincode::serialize(agent).map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + self.agents_tree + .insert(agent.hash.as_bytes(), data) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + Ok(()) + } + + /// Get agent by hash + pub fn get_agent(&self, hash: &str) -> Result> { + let data = self + .agents_tree + .get(hash.as_bytes()) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + match data { + Some(bytes) => { + let agent: AgentInfo = bincode::deserialize(&bytes) + .map_err(|e| ChallengeError::Serialization(e.to_string()))?; + Ok(Some(agent)) + } + None => Ok(None), + } + } + + /// List all agents + pub fn list_agents(&self) -> Result> { + let mut agents = Vec::new(); + + for result in self.agents_tree.iter() { + let (_, value) = result.map_err(|e| ChallengeError::Database(e.to_string()))?; + + let agent: AgentInfo = bincode::deserialize(&value) + .map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + agents.push(agent); + } + + Ok(agents) + } + + // ==================== Results ==================== + + /// Save evaluation result + pub fn save_result(&self, result: &EvaluationResult) -> Result<()> { + // Key: agent_hash:job_id + let key = format!("{}:{}", result.agent_hash, result.job_id); + let data = + bincode::serialize(result).map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + self.results_tree + .insert(key.as_bytes(), data) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + Ok(()) + } + + /// Get results for an agent + pub fn get_results_for_agent(&self, agent_hash: &str) -> Result> { + let prefix = format!("{}:", agent_hash); + let mut results = Vec::new(); + + for item in self.results_tree.scan_prefix(prefix.as_bytes()) { + let (_, value) = item.map_err(|e| ChallengeError::Database(e.to_string()))?; + + let result: EvaluationResult = bincode::deserialize(&value) + .map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + results.push(result); + } + + Ok(results) + } + + /// Get all results + pub fn get_all_results(&self) -> Result> { + let mut results = Vec::new(); + + for item in self.results_tree.iter() { + let (_, value) = item.map_err(|e| ChallengeError::Database(e.to_string()))?; + + let result: EvaluationResult = bincode::deserialize(&value) + .map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + results.push(result); + } + + Ok(results) + } + + /// Get latest result for each agent + pub fn get_latest_results(&self) -> Result> { + let mut latest: std::collections::HashMap = + std::collections::HashMap::new(); + + for result in self.get_all_results()? { + let existing = latest.get(&result.agent_hash); + if existing.is_none() || existing.unwrap().timestamp < result.timestamp { + latest.insert(result.agent_hash.clone(), result); + } + } + + Ok(latest.into_values().collect()) + } + + // ==================== Key-Value Store ==================== + + /// Set a value in the KV store + pub fn kv_set(&self, key: &str, value: &T) -> Result<()> { + let data = + bincode::serialize(value).map_err(|e| ChallengeError::Serialization(e.to_string()))?; + + self.kv_tree + .insert(key.as_bytes(), data) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + Ok(()) + } + + /// Get a value from the KV store + pub fn kv_get(&self, key: &str) -> Result> { + let data = self + .kv_tree + .get(key.as_bytes()) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + match data { + Some(bytes) => { + let value: T = bincode::deserialize(&bytes) + .map_err(|e| ChallengeError::Serialization(e.to_string()))?; + Ok(Some(value)) + } + None => Ok(None), + } + } + + /// Delete a value from the KV store + pub fn kv_delete(&self, key: &str) -> Result { + let removed = self + .kv_tree + .remove(key.as_bytes()) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + Ok(removed.is_some()) + } + + /// List all keys in the KV store + pub fn kv_keys(&self) -> Result> { + let mut keys = Vec::new(); + + for item in self.kv_tree.iter() { + let (key, _) = item.map_err(|e| ChallengeError::Database(e.to_string()))?; + + if let Ok(key_str) = std::str::from_utf8(&key) { + keys.push(key_str.to_string()); + } + } + + Ok(keys) + } + + // ==================== Metadata ==================== + + /// Set metadata value + pub fn set_meta(&self, key: &str, value: &str) -> Result<()> { + self.meta_tree + .insert(key.as_bytes(), value.as_bytes()) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + Ok(()) + } + + /// Get metadata value + pub fn get_meta(&self, key: &str) -> Result> { + let data = self + .meta_tree + .get(key.as_bytes()) + .map_err(|e| ChallengeError::Database(e.to_string()))?; + + match data { + Some(bytes) => Ok(Some(String::from_utf8_lossy(&bytes).to_string())), + None => Ok(None), + } + } + + /// Get database version + pub fn get_version(&self) -> Result { + self.get_meta("db_version")? + .and_then(|v| v.parse().ok()) + .ok_or_else(|| ChallengeError::Database("No database version".to_string())) + .or(Ok(0)) + } + + /// Set database version + pub fn set_version(&self, version: u32) -> Result<()> { + self.set_meta("db_version", &version.to_string()) + } + + // ==================== Custom Trees ==================== + + /// Open a custom tree + pub fn open_tree(&self, name: &str) -> Result { + self.db + .open_tree(name) + .map_err(|e| ChallengeError::Database(format!("Failed to open tree '{}': {}", name, e))) + } + + /// Flush to disk + pub fn flush(&self) -> Result<()> { + self.db + .flush() + .map_err(|e| ChallengeError::Database(e.to_string()))?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_database_open() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()); + assert!(db.is_ok()); + } + + #[test] + fn test_challenge_id() { + let dir = tempdir().unwrap(); + let challenge_id = ChallengeId::new(); + let db = ChallengeDatabase::open(dir.path(), challenge_id).unwrap(); + + assert_eq!(db.challenge_id(), challenge_id); + } + + #[test] + fn test_agent_storage() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let agent = AgentInfo::new("test_hash_123".to_string()); + db.save_agent(&agent).unwrap(); + + let loaded = db.get_agent("test_hash_123").unwrap(); + assert!(loaded.is_some()); + assert_eq!(loaded.unwrap().hash, "test_hash_123"); + } + + #[test] + fn test_list_agents() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let agent1 = AgentInfo::new("hash1".to_string()); + let agent2 = AgentInfo::new("hash2".to_string()); + + db.save_agent(&agent1).unwrap(); + db.save_agent(&agent2).unwrap(); + + let agents = db.list_agents().unwrap(); + assert_eq!(agents.len(), 2); + } + + #[test] + fn test_result_storage() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let result = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.85); + + db.save_result(&result).unwrap(); + + let results = db.get_results_for_agent("agent1").unwrap(); + assert_eq!(results.len(), 1); + assert_eq!(results[0].score, 0.85); + } + + #[test] + fn test_get_all_results() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let result1 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.85); + let result2 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent2".to_string(), 0.90); + + db.save_result(&result1).unwrap(); + db.save_result(&result2).unwrap(); + + let results = db.get_all_results().unwrap(); + assert_eq!(results.len(), 2); + } + + #[test] + fn test_get_latest_results() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + // Save multiple results for same agent (agent1) + let mut result1 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.70); + result1.timestamp = chrono::Utc::now() - chrono::Duration::hours(1); + + let result2 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.90); + + db.save_result(&result1).unwrap(); + db.save_result(&result2).unwrap(); + + // Add result for a different agent (agent2) + let result3 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent2".to_string(), 0.80); + db.save_result(&result3).unwrap(); + + let latest = db.get_latest_results().unwrap(); + // Should have one result per agent (agent1 and agent2) + assert_eq!(latest.len(), 2); + + // Find results by agent + let agent1_result = latest.iter().find(|r| r.agent_hash == "agent1").unwrap(); + let agent2_result = latest.iter().find(|r| r.agent_hash == "agent2").unwrap(); + + // Verify agent1 has the latest score (0.90, not 0.70) + assert_eq!(agent1_result.score, 0.90); + // Verify agent2 has its only score + assert_eq!(agent2_result.score, 0.80); + } + + #[test] + fn test_kv_store() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.kv_set("my_key", &42i32).unwrap(); + + let value: Option = db.kv_get("my_key").unwrap(); + assert_eq!(value, Some(42)); + } + + #[test] + fn test_kv_delete() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.kv_set("key_to_delete", &"value").unwrap(); + + let deleted = db.kv_delete("key_to_delete").unwrap(); + assert!(deleted); + + let value: Option = db.kv_get("key_to_delete").unwrap(); + assert!(value.is_none()); + + // Delete non-existent key + let deleted = db.kv_delete("non_existent").unwrap(); + assert!(!deleted); + } + + #[test] + fn test_kv_keys() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.kv_set("key1", &1).unwrap(); + db.kv_set("key2", &2).unwrap(); + db.kv_set("key3", &3).unwrap(); + + let keys = db.kv_keys().unwrap(); + assert_eq!(keys.len(), 3); + assert!(keys.contains(&"key1".to_string())); + assert!(keys.contains(&"key2".to_string())); + assert!(keys.contains(&"key3".to_string())); + } + + #[test] + fn test_set_meta() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.set_meta("author", "test_author").unwrap(); + + let value = db.get_meta("author").unwrap(); + assert_eq!(value, Some("test_author".to_string())); + } + + #[test] + fn test_get_meta() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let value = db.get_meta("non_existent").unwrap(); + assert!(value.is_none()); + + db.set_meta("key", "value").unwrap(); + let value = db.get_meta("key").unwrap(); + assert_eq!(value, Some("value".to_string())); + } + + #[test] + fn test_get_version() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + // Should return 0 for new database + let version = db.get_version().unwrap(); + assert_eq!(version, 0); + } + + #[test] + fn test_set_version() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.set_version(5).unwrap(); + + let version = db.get_version().unwrap(); + assert_eq!(version, 5); + } + + #[test] + fn test_open_tree() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + let custom_tree = db.open_tree("custom_data").unwrap(); + + custom_tree.insert(b"key", b"value").unwrap(); + let value = custom_tree.get(b"key").unwrap(); + assert_eq!(value.as_ref().map(|v| v.as_ref()), Some(b"value".as_ref())); + } + + #[test] + fn test_flush() { + let dir = tempdir().unwrap(); + let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap(); + + db.kv_set("test_key", &"test_value").unwrap(); + + // Flush should succeed + let result = db.flush(); + assert!(result.is_ok()); + } + + #[test] + fn test_data_persistence_across_reopens() { + // Test that data persists after closing and reopening the database + let dir = tempdir().unwrap(); + let challenge_id = ChallengeId::new(); + + // First session: write data + { + let db = ChallengeDatabase::open(dir.path(), challenge_id).unwrap(); + + // Save an agent + let agent = AgentInfo::new("persistent_agent".to_string()); + db.save_agent(&agent).unwrap(); + + // Save a result + let result = + EvaluationResult::new(uuid::Uuid::new_v4(), "persistent_agent".to_string(), 0.95); + db.save_result(&result).unwrap(); + + // Save KV data + db.kv_set("persistent_key", &"persistent_value").unwrap(); + + // Save metadata + db.set_meta("test_meta", "meta_value").unwrap(); + db.set_version(42).unwrap(); + + // Explicitly flush to disk + db.flush().unwrap(); + + // Drop db to close it + } + + // Second session: verify data persists + { + let db = ChallengeDatabase::open(dir.path(), challenge_id).unwrap(); + + // Verify agent persists + let agent = db.get_agent("persistent_agent").unwrap(); + assert!(agent.is_some()); + assert_eq!(agent.unwrap().hash, "persistent_agent"); + + // Verify results persist + let results = db.get_results_for_agent("persistent_agent").unwrap(); + assert_eq!(results.len(), 1); + assert_eq!(results[0].score, 0.95); + + // Verify KV data persists + let value: Option = db.kv_get("persistent_key").unwrap(); + assert_eq!(value, Some("persistent_value".to_string())); + + // Verify metadata persists + let meta = db.get_meta("test_meta").unwrap(); + assert_eq!(meta, Some("meta_value".to_string())); + + let version = db.get_version().unwrap(); + assert_eq!(version, 42); + } + + // Third session: verify data still persists (double check) + { + let db = ChallengeDatabase::open(dir.path(), challenge_id).unwrap(); + + let agents = db.list_agents().unwrap(); + assert_eq!(agents.len(), 1); + assert_eq!(agents[0].hash, "persistent_agent"); + + let all_results = db.get_all_results().unwrap(); + assert_eq!(all_results.len(), 1); + } + } +} diff --git a/crates/challenge-sdk/src/decentralized.rs b/crates/challenge-sdk/src/decentralized.rs new file mode 100644 index 000000000..7fad6695b --- /dev/null +++ b/crates/challenge-sdk/src/decentralized.rs @@ -0,0 +1,537 @@ +//! Decentralized challenge runner +//! +//! Runs a challenge in P2P mode without central server. This module provides +//! the glue between the P2P communication layer and challenge evaluation. +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ Decentralized Runner โ”‚ +//! โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +//! โ”‚ โ”‚ P2P Layer โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚ Decentralizedโ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚ Challenge โ”‚ โ”‚ +//! โ”‚ โ”‚ (libp2p) โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚ Runner โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚ Evaluation โ”‚ โ”‚ +//! โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` +//! +//! # Example +//! +//! ```text +//! use platform_challenge_sdk::decentralized::run_decentralized; +//! use platform_challenge_sdk::server::ServerChallenge; +//! +//! let (tx, rx) = tokio::sync::mpsc::channel(100); +//! +//! run_decentralized( +//! my_challenge, +//! tx, +//! rx, +//! "validator-hotkey".to_string(), +//! ).await?; +//! ``` + +use crate::error::ChallengeError; +use crate::p2p_client::{P2PChallengeMessage, PendingSubmission}; +use crate::server::{EvaluationRequest, ServerChallenge}; +use std::sync::Arc; +use tokio::sync::mpsc; +use tracing::{debug, error, info, warn}; + +/// Run challenge in decentralized P2P mode +/// +/// This function runs a challenge in P2P mode, processing incoming messages +/// from the network and evaluating submissions without a central server. +/// +/// # Arguments +/// +/// * `challenge` - The challenge implementation to run +/// * `message_tx` - Channel to send messages to the P2P layer +/// * `message_rx` - Channel to receive messages from the P2P layer +/// * `validator_hotkey` - The validator's hotkey for authentication +/// +/// # Returns +/// +/// Returns `Ok(())` when the challenge runner completes (channel closes), +/// or an error if something goes wrong during execution. +/// +/// # Example +/// +/// ```text +/// use platform_challenge_sdk::decentralized::run_decentralized; +/// +/// let (tx, rx) = tokio::sync::mpsc::channel(100); +/// +/// run_decentralized( +/// my_challenge, +/// tx, +/// rx, +/// "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), +/// ).await?; +/// ``` +pub async fn run_decentralized( + challenge: C, + message_tx: mpsc::Sender, + mut message_rx: mpsc::Receiver, + validator_hotkey: String, +) -> Result<(), ChallengeError> { + let challenge = Arc::new(challenge); + let challenge_id = challenge.challenge_id().to_string(); + + info!( + challenge_id = %challenge_id, + validator = %validator_hotkey, + "Starting decentralized challenge runner" + ); + + // Process incoming messages + while let Some(msg) = message_rx.recv().await { + match msg { + P2PChallengeMessage::SubmissionsResponse { + submissions, + challenge_id: msg_challenge_id, + } => { + // Verify this is for our challenge + if msg_challenge_id != challenge_id { + warn!( + expected = %challenge_id, + received = %msg_challenge_id, + "Received submissions for wrong challenge, ignoring" + ); + continue; + } + + debug!( + challenge_id = %challenge_id, + count = %submissions.len(), + "Processing submissions batch" + ); + + // Evaluate each submission + for submission in submissions { + let result = + evaluate_submission(&challenge, &challenge_id, &submission, &message_tx) + .await; + + if let Err(e) = result { + error!( + challenge_id = %challenge_id, + submission_hash = %submission.submission_hash, + error = %e, + "Failed to process submission" + ); + } + } + } + P2PChallengeMessage::RequestSubmissions { .. } => { + // This is a request message, not something we handle here + // The P2P layer handles routing these to the right place + debug!("Ignoring RequestSubmissions message in runner"); + } + P2PChallengeMessage::EvaluationResult { .. } => { + // This is an outbound message type, not something we handle + debug!("Ignoring EvaluationResult message in runner"); + } + P2PChallengeMessage::WeightVote { .. } => { + // Weight votes are handled by the P2P layer's consensus + debug!("Ignoring WeightVote message in runner"); + } + P2PChallengeMessage::RequestWeights { .. } => { + // Request for weights, handled by P2P layer + debug!("Ignoring RequestWeights message in runner"); + } + P2PChallengeMessage::WeightsResponse { .. } => { + // Weights response, may be used for verification + debug!("Received weights response in runner"); + } + P2PChallengeMessage::StoreSubmission { + submission, + challenge_id: msg_challenge_id, + } => { + if msg_challenge_id == challenge_id { + debug!( + challenge_id = %challenge_id, + submission_hash = %submission.submission_hash, + miner_hotkey = %submission.miner_hotkey, + "Received submission to store - forwarding to evaluation queue" + ); + // Submissions are auto-queued for evaluation when stored via P2P + // The P2P layer handles distributed storage and replication + } else { + warn!( + expected = %challenge_id, + received = %msg_challenge_id, + "Received StoreSubmission for wrong challenge, ignoring" + ); + } + } + P2PChallengeMessage::RequestEvaluationStatus { .. } => { + // Request for evaluation status is handled by the P2P layer + debug!("Ignoring RequestEvaluationStatus message in runner - handled by P2P layer"); + } + P2PChallengeMessage::EvaluationStatusResponse { .. } => { + // Evaluation status responses are handled by the client layer + debug!("Ignoring EvaluationStatusResponse message in runner"); + } + } + } + + info!( + challenge_id = %challenge_id, + "Decentralized challenge runner completed" + ); + + Ok(()) +} + +/// Evaluate a single submission and send the result back to the P2P network +async fn evaluate_submission( + challenge: &Arc, + challenge_id: &str, + submission: &PendingSubmission, + message_tx: &mpsc::Sender, +) -> Result<(), ChallengeError> { + let start_time = std::time::Instant::now(); + + debug!( + challenge_id = %challenge_id, + submission_hash = %submission.submission_hash, + miner = %submission.miner_hotkey, + "Starting evaluation" + ); + + // Build evaluation request from submission + let req = EvaluationRequest { + request_id: uuid::Uuid::new_v4().to_string(), + submission_id: submission.submission_hash.clone(), + participant_id: submission.miner_hotkey.clone(), + data: serde_json::json!({ + "source_code": submission.source_code, + "metadata": submission.metadata, + }), + metadata: Some(serde_json::json!({ + "submitted_at": submission.submitted_at, + "p2p_mode": true, + })), + epoch: 0, // Epoch is tracked by the P2P layer + deadline: None, + }; + + // Perform evaluation + match challenge.evaluate(req).await { + Ok(resp) => { + let execution_time_ms = start_time.elapsed().as_millis() as u64; + + info!( + challenge_id = %challenge_id, + submission_hash = %submission.submission_hash, + score = %resp.score, + execution_time_ms = %execution_time_ms, + "Evaluation completed successfully" + ); + + // Send result back to network + let result_msg = P2PChallengeMessage::EvaluationResult { + challenge_id: challenge_id.to_string(), + submission_hash: submission.submission_hash.clone(), + score: resp.score, + execution_time_ms, + result_data: resp.results, + }; + + message_tx.send(result_msg).await.map_err(|e| { + ChallengeError::Network(format!("Failed to send evaluation result: {}", e)) + })?; + } + Err(e) => { + warn!( + challenge_id = %challenge_id, + submission_hash = %submission.submission_hash, + error = %e, + "Evaluation failed" + ); + + // Send failure result with zero score + let result_msg = P2PChallengeMessage::EvaluationResult { + challenge_id: challenge_id.to_string(), + submission_hash: submission.submission_hash.clone(), + score: 0.0, + execution_time_ms: start_time.elapsed().as_millis() as u64, + result_data: serde_json::json!({ + "error": e.to_string(), + "success": false, + }), + }; + + message_tx.send(result_msg).await.map_err(|send_err| { + ChallengeError::Network(format!("Failed to send error result: {}", send_err)) + })?; + } + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::server::{ + EvaluationResponse, ServerChallenge, ValidationRequest, ValidationResponse, + }; + use async_trait::async_trait; + use serde_json::json; + + struct MockChallenge { + should_fail: bool, + } + + #[async_trait] + impl ServerChallenge for MockChallenge { + fn challenge_id(&self) -> &str { + "mock-challenge" + } + + fn name(&self) -> &str { + "Mock Challenge" + } + + fn version(&self) -> &str { + "1.0.0" + } + + async fn evaluate( + &self, + request: EvaluationRequest, + ) -> Result { + if self.should_fail { + return Err(ChallengeError::Evaluation("Mock failure".to_string())); + } + + Ok(EvaluationResponse::success( + &request.request_id, + 0.85, + json!({"mock": true, "passed": 17, "total": 20}), + )) + } + } + + #[tokio::test] + async fn test_run_decentralized_processes_submissions() { + let (tx, rx) = mpsc::channel(10); + let (result_tx, mut result_rx) = mpsc::channel(10); + + let challenge = MockChallenge { should_fail: false }; + + // Start runner in background + let runner_handle = tokio::spawn(async move { + run_decentralized(challenge, result_tx, rx, "test-validator".to_string()).await + }); + + // Send submissions + let submission = PendingSubmission { + submission_hash: "hash123".to_string(), + miner_hotkey: "miner1".to_string(), + source_code: "fn main() {}".to_string(), + metadata: json!({}), + submitted_at: 1704067200, + }; + + tx.send(P2PChallengeMessage::SubmissionsResponse { + challenge_id: "mock-challenge".to_string(), + submissions: vec![submission], + }) + .await + .expect("Send should work"); + + // Wait for result + let result = tokio::time::timeout(std::time::Duration::from_secs(5), result_rx.recv()) + .await + .expect("Should receive result within timeout") + .expect("Should have result"); + + if let P2PChallengeMessage::EvaluationResult { + submission_hash, + score, + .. + } = result + { + assert_eq!(submission_hash, "hash123"); + assert!((score - 0.85).abs() < f64::EPSILON); + } else { + panic!("Expected EvaluationResult message"); + } + + // Close channel to stop runner + drop(tx); + + // Wait for runner to complete + let _ = tokio::time::timeout(std::time::Duration::from_secs(2), runner_handle).await; + } + + #[tokio::test] + async fn test_run_decentralized_handles_evaluation_errors() { + let (tx, rx) = mpsc::channel(10); + let (result_tx, mut result_rx) = mpsc::channel(10); + + let challenge = MockChallenge { should_fail: true }; + + tokio::spawn(async move { + let _ = run_decentralized(challenge, result_tx, rx, "test-validator".to_string()).await; + }); + + let submission = PendingSubmission { + submission_hash: "failing-hash".to_string(), + miner_hotkey: "miner2".to_string(), + source_code: "invalid".to_string(), + metadata: json!({}), + submitted_at: 1704067200, + }; + + tx.send(P2PChallengeMessage::SubmissionsResponse { + challenge_id: "mock-challenge".to_string(), + submissions: vec![submission], + }) + .await + .expect("Send should work"); + + let result = tokio::time::timeout(std::time::Duration::from_secs(5), result_rx.recv()) + .await + .expect("Should receive result within timeout") + .expect("Should have result"); + + if let P2PChallengeMessage::EvaluationResult { + score, result_data, .. + } = result + { + // Failed evaluations should have zero score + assert!((score - 0.0).abs() < f64::EPSILON); + assert!(result_data["error"].as_str().is_some()); + assert_eq!(result_data["success"], false); + } else { + panic!("Expected EvaluationResult message"); + } + + drop(tx); + } + + #[tokio::test] + async fn test_run_decentralized_ignores_wrong_challenge_id() { + let (tx, rx) = mpsc::channel(10); + let (result_tx, mut result_rx) = mpsc::channel(10); + + let challenge = MockChallenge { should_fail: false }; + + tokio::spawn(async move { + let _ = run_decentralized(challenge, result_tx, rx, "test-validator".to_string()).await; + }); + + // Send submission for wrong challenge + let submission = PendingSubmission { + submission_hash: "hash456".to_string(), + miner_hotkey: "miner3".to_string(), + source_code: "test".to_string(), + metadata: json!({}), + submitted_at: 1704067200, + }; + + tx.send(P2PChallengeMessage::SubmissionsResponse { + challenge_id: "different-challenge".to_string(), // Wrong challenge ID + submissions: vec![submission], + }) + .await + .expect("Send should work"); + + // Should not receive any result because challenge ID doesn't match + let result = + tokio::time::timeout(std::time::Duration::from_millis(500), result_rx.recv()).await; + + assert!(result.is_err(), "Should timeout because no result is sent"); + + drop(tx); + } + + #[tokio::test] + async fn test_run_decentralized_completes_when_channel_closes() { + let (tx, rx) = mpsc::channel(10); + let (result_tx, _result_rx) = mpsc::channel(10); + + let challenge = MockChallenge { should_fail: false }; + + let handle = tokio::spawn(async move { + run_decentralized(challenge, result_tx, rx, "test-validator".to_string()).await + }); + + // Close the channel + drop(tx); + + // Runner should complete successfully + let result = tokio::time::timeout(std::time::Duration::from_secs(2), handle) + .await + .expect("Should complete within timeout") + .expect("Task should not panic"); + + assert!(result.is_ok()); + } + + #[tokio::test] + async fn test_evaluate_submission_success() { + let challenge = Arc::new(MockChallenge { should_fail: false }); + let (tx, mut rx) = mpsc::channel(10); + + let submission = PendingSubmission { + submission_hash: "test-hash".to_string(), + miner_hotkey: "miner".to_string(), + source_code: "code".to_string(), + metadata: json!({"key": "value"}), + submitted_at: 1704067200, + }; + + let result = evaluate_submission(&challenge, "mock-challenge", &submission, &tx).await; + + assert!(result.is_ok()); + + let msg = rx.recv().await.expect("Should have message"); + if let P2PChallengeMessage::EvaluationResult { + challenge_id, + submission_hash, + score, + .. + } = msg + { + assert_eq!(challenge_id, "mock-challenge"); + assert_eq!(submission_hash, "test-hash"); + assert!((score - 0.85).abs() < f64::EPSILON); + } else { + panic!("Expected EvaluationResult"); + } + } + + #[tokio::test] + async fn test_evaluate_submission_failure() { + let challenge = Arc::new(MockChallenge { should_fail: true }); + let (tx, mut rx) = mpsc::channel(10); + + let submission = PendingSubmission { + submission_hash: "fail-hash".to_string(), + miner_hotkey: "miner".to_string(), + source_code: "bad code".to_string(), + metadata: json!({}), + submitted_at: 1704067200, + }; + + let result = evaluate_submission(&challenge, "mock-challenge", &submission, &tx).await; + + assert!(result.is_ok()); // Function completes even if evaluation fails + + let msg = rx.recv().await.expect("Should have message"); + if let P2PChallengeMessage::EvaluationResult { + score, result_data, .. + } = msg + { + assert!((score - 0.0).abs() < f64::EPSILON); + assert_eq!(result_data["success"], false); + } else { + panic!("Expected EvaluationResult"); + } + } +} diff --git a/crates/challenge-sdk/src/error.rs b/crates/challenge-sdk/src/error.rs new file mode 100644 index 000000000..25273dbfd --- /dev/null +++ b/crates/challenge-sdk/src/error.rs @@ -0,0 +1,138 @@ +//! Error types for challenges + +use thiserror::Error; + +/// Result type for challenge operations +pub type Result = std::result::Result; + +/// Challenge errors +#[derive(Error, Debug)] +pub enum ChallengeError { + // ========== New API errors ========== + #[error("Connection error: {0}")] + Connection(String), + + #[error("Authentication error: {0}")] + Auth(String), + + #[error("Configuration error: {0}")] + Config(String), + + #[error("IO error: {0}")] + Io(String), + + // ========== Evaluation errors ========== + #[error("Evaluation error: {0}")] + Evaluation(String), + + #[error("Validation error: {0}")] + Validation(String), + + #[error("Network error: {0}")] + Network(String), + + #[error("Timeout: {0}")] + Timeout(String), + + // ========== Legacy errors (kept for compatibility) ========== + #[error("Database error: {0}")] + Database(String), + + #[error("Serialization error: {0}")] + Serialization(String), + + #[error("Configuration error: {0}")] + #[deprecated(note = "Use Config variant instead")] + Configuration(String), + + #[error("Unsupported job type: {0}")] + UnsupportedJobType(String), + + #[error("Agent not found: {0}")] + AgentNotFound(String), + + #[error("Challenge not found: {0}")] + ChallengeNotFound(String), + + #[error("Insufficient validators: required {required}, got {got}")] + InsufficientValidators { required: usize, got: usize }, + + #[error("Epoch error: {0}")] + Epoch(String), + + #[error("Weight error: {0}")] + Weight(String), + + #[error("Commitment mismatch")] + CommitmentMismatch, + + #[error("Internal error: {0}")] + Internal(String), +} + +impl From for ChallengeError { + fn from(err: sled::Error) -> Self { + ChallengeError::Database(err.to_string()) + } +} + +impl From for ChallengeError { + fn from(err: bincode::Error) -> Self { + ChallengeError::Serialization(err.to_string()) + } +} + +impl From for ChallengeError { + fn from(err: serde_json::Error) -> Self { + ChallengeError::Serialization(err.to_string()) + } +} + +impl From for ChallengeError { + fn from(err: std::io::Error) -> Self { + ChallengeError::Internal(err.to_string()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_from_sled_error() { + let sled_err = sled::Error::Unsupported("test".to_string()); + let challenge_err: ChallengeError = sled_err.into(); + assert!(matches!(challenge_err, ChallengeError::Database(_))); + } + + #[test] + fn test_from_bincode_error() { + let bincode_err = bincode::Error::new(bincode::ErrorKind::Custom("test error".to_string())); + let challenge_err: ChallengeError = bincode_err.into(); + assert!(matches!(challenge_err, ChallengeError::Serialization(_))); + } + + #[test] + fn test_from_serde_json_error() { + let json_str = "{invalid json}"; + let json_err = serde_json::from_str::(json_str).unwrap_err(); + let challenge_err: ChallengeError = json_err.into(); + assert!(matches!(challenge_err, ChallengeError::Serialization(_))); + } + + #[test] + fn test_from_io_error() { + let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"); + let challenge_err: ChallengeError = io_err.into(); + assert!(matches!(challenge_err, ChallengeError::Internal(_))); + } + + #[test] + fn test_error_display() { + let err = ChallengeError::Database("connection failed".to_string()); + assert_eq!(err.to_string(), "Database error: connection failed"); + + let err = ChallengeError::Evaluation("failed to evaluate".to_string()); + assert_eq!(err.to_string(), "Evaluation error: failed to evaluate"); + } +} diff --git a/crates/challenge-sdk/src/lib.rs b/crates/challenge-sdk/src/lib.rs new file mode 100644 index 000000000..ba7179a17 --- /dev/null +++ b/crates/challenge-sdk/src/lib.rs @@ -0,0 +1,151 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Platform Challenge SDK +//! +//! SDK for developing challenges on Platform Network. +//! Fully decentralized P2P architecture - validators communicate directly. +//! +//! # Quick Start - Server Mode +//! +//! Challenge runs as HTTP server, validators call `/evaluate`: +//! +//! ```text +//! use platform_challenge_sdk::prelude::*; +//! +//! struct MyChallenge; +//! +//! #[async_trait] +//! impl ServerChallenge for MyChallenge { +//! fn challenge_id(&self) -> &str { "my-challenge" } +//! fn name(&self) -> &str { "My Challenge" } +//! fn version(&self) -> &str { "0.1.0" } +//! +//! async fn evaluate(&self, req: EvaluationRequest) -> Result { +//! // Your evaluation logic here +//! let score = evaluate_submission(&req.data)?; +//! Ok(EvaluationResponse::success(&req.request_id, score, json!({}))) +//! } +//! } +//! +//! #[tokio::main] +//! async fn main() -> Result<(), ChallengeError> { +//! ChallengeServer::builder(MyChallenge) +//! .port(8080) +//! .build() +//! .run() +//! .await +//! } +//! ``` +//! +//! # Quick Start - P2P Mode +//! +//! ```text +//! use platform_challenge_sdk::prelude::*; +//! +//! #[tokio::main] +//! async fn main() -> Result<(), ChallengeError> { +//! run_decentralized(MyChallenge).await +//! } +//! ``` +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ Your Challenge โ”‚ +//! โ”‚ impl ServerChallenge { evaluate(), validate(), ... } โ”‚ +//! โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +//! โ”‚ Platform Challenge SDK โ”‚ +//! โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +//! โ”‚ โ”‚ Server โ”‚ โ”‚ P2P Client โ”‚ โ”‚ Types โ”‚ โ”‚ +//! โ”‚ โ”‚ (HTTP mode) โ”‚ โ”‚ (libp2p) โ”‚ โ”‚ (generic) โ”‚ โ”‚ +//! โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +//! โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +//! โ”‚ Validator Network (P2P) โ”‚ +//! โ”‚ (gossipsub, DHT, consensus) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` +//! +//! # Note on Terminology +//! +//! This SDK is **generic** - it doesn't use challenge-specific terms like +//! "agent", "miner", etc. Each challenge defines its own terminology: +//! - `EvaluationRequest.data` contains challenge-specific submission data +//! - `EvaluationResponse.results` contains challenge-specific result data +//! - `participant_id` is generic (could be miner hotkey, user id, etc.) + +// ============================================================================ +// MODULES +// ============================================================================ + +/// Data types and utilities +pub mod data; +/// Database utilities +pub mod database; +/// Decentralized challenge runner for P2P mode +pub mod decentralized; +/// Error types +pub mod error; +/// P2P client for decentralized communication +pub mod p2p_client; +/// HTTP routes +pub mod routes; +/// Server mode - expose challenge as HTTP server +pub mod server; +/// Submission types +pub mod submission_types; +/// Test challenge implementation +pub mod test_challenge; +/// Core types +pub mod types; +/// Weight calculation types +pub mod weight_types; +/// Weight calculation utilities +pub mod weights; + +// ============================================================================ +// EXPORTS +// ============================================================================ + +pub use decentralized::run_decentralized; +pub use p2p_client::{ + P2PChallengeClient, P2PChallengeConfig, P2PChallengeMessage, PendingSubmission, + ValidatorEvaluationResult, +}; +pub use server::{ + ChallengeContext, ChallengeServer, ChallengeServerBuilder, ConfigLimits, ConfigResponse, + EvaluationRequest, EvaluationResponse, HealthResponse, ServerChallenge, ServerConfig, + ValidationRequest, ValidationResponse, +}; + +pub use data::*; +pub use database::*; +pub use error::*; +pub use routes::*; +pub use submission_types::*; +pub use test_challenge::SimpleTestChallenge; +pub use types::*; +pub use weight_types::*; +pub use weights::*; + +/// Prelude for P2P challenge development +pub mod prelude { + pub use super::error::ChallengeError; + pub use super::routes::{ChallengeRoute, RouteRequest, RouteResponse}; + pub use super::server::{ + ChallengeContext, ChallengeServer, EvaluationRequest, EvaluationResponse, ServerChallenge, + ServerConfig, ValidationRequest, ValidationResponse, + }; + + // P2P mode + pub use super::decentralized::run_decentralized; + pub use super::p2p_client::{ + P2PChallengeClient, P2PChallengeConfig, P2PChallengeMessage, PendingSubmission, + ValidatorEvaluationResult, + }; + + // Common utilities + pub use async_trait::async_trait; + pub use serde::{Deserialize, Serialize}; + pub use serde_json::{json, Value}; + pub use tracing::{debug, error, info, warn}; +} diff --git a/crates/challenge-sdk/src/p2p_client.rs b/crates/challenge-sdk/src/p2p_client.rs new file mode 100644 index 000000000..e64f0d75c --- /dev/null +++ b/crates/challenge-sdk/src/p2p_client.rs @@ -0,0 +1,1028 @@ +//! P2P client for decentralized challenge communication +//! +//! Allows challenges to communicate with validators without central server. +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ Challenge โ”‚โ—„โ”€โ”€โ”€P2Pโ”€โ”€โ”‚ Validator โ”‚ +//! โ”‚ Container โ”‚ โ”‚ Node โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ โ”‚ +//! Evaluates Submits/receives +//! submissions evaluations +//! ``` +//! +//! # Example +//! +//! ```text +//! use platform_challenge_sdk::p2p_client::{P2PChallengeClient, P2PChallengeConfig}; +//! +//! let (tx, rx) = tokio::sync::mpsc::channel(100); +//! let config = P2PChallengeConfig { +//! challenge_id: "my-challenge".to_string(), +//! validator_hotkey: "validator-hotkey".to_string(), +//! message_tx: tx, +//! message_rx: Arc::new(RwLock::new(rx)), +//! }; +//! +//! let client = P2PChallengeClient::new(config); +//! client.submit_evaluation("hash", 0.95, 1500, json!({})).await?; +//! ``` + +use crate::error::ChallengeError; +use parking_lot::RwLock; +use serde::{Deserialize, Serialize}; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::mpsc; +use tracing::{debug, warn}; + +/// Default timeout for P2P requests in seconds +const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 30; + +/// P2P challenge message types +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum P2PChallengeMessage { + /// Submit evaluation result + EvaluationResult { + /// Challenge identifier + challenge_id: String, + /// Hash of the submission being evaluated + submission_hash: String, + /// Evaluation score (0.0 - 1.0) + score: f64, + /// Execution time in milliseconds + execution_time_ms: u64, + /// Additional result data + result_data: serde_json::Value, + }, + /// Request submissions to evaluate + RequestSubmissions { + /// Challenge identifier + challenge_id: String, + /// Maximum number of submissions to return + limit: usize, + }, + /// Submissions response + SubmissionsResponse { + /// Challenge identifier + challenge_id: String, + /// List of pending submissions + submissions: Vec, + }, + /// Vote on aggregated weights + WeightVote { + /// Challenge identifier + challenge_id: String, + /// Epoch number + epoch: u64, + /// Weight votes as (hotkey, weight) pairs + weights: Vec<(String, f64)>, + }, + /// Request weight aggregation + RequestWeights { + /// Challenge identifier + challenge_id: String, + /// Epoch number + epoch: u64, + }, + /// Aggregated weights response + WeightsResponse { + /// Challenge identifier + challenge_id: String, + /// Epoch number + epoch: u64, + /// Aggregated weights as (hotkey, weight) pairs + weights: Vec<(String, f64)>, + }, + /// Store submission in distributed storage + StoreSubmission { + /// Challenge identifier + challenge_id: String, + /// Submission data + submission: PendingSubmission, + }, + /// Request evaluation status for a submission + RequestEvaluationStatus { + /// Challenge identifier + challenge_id: String, + /// Submission hash + submission_hash: String, + }, + /// Evaluation status response + EvaluationStatusResponse { + /// Challenge identifier + challenge_id: String, + /// Submission hash + submission_hash: String, + /// List of validator evaluations received + evaluations: Vec, + /// Whether consensus has been reached + consensus_reached: bool, + /// Final aggregated score (if consensus reached) + final_score: Option, + }, +} + +/// Result of a validator's evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorEvaluationResult { + /// Validator hotkey + pub validator_hotkey: String, + /// Evaluation score + pub score: f64, + /// Timestamp of evaluation + pub evaluated_at: i64, +} + +/// A submission pending evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PendingSubmission { + /// Hash of the submission + pub submission_hash: String, + /// Miner's hotkey (SS58 address) + pub miner_hotkey: String, + /// Source code or submission data + pub source_code: String, + /// Additional metadata + pub metadata: serde_json::Value, + /// Submission timestamp (unix seconds) + pub submitted_at: i64, +} + +/// Configuration for P2P challenge client +#[derive(Clone)] +pub struct P2PChallengeConfig { + /// Challenge ID + pub challenge_id: String, + /// Validator hotkey (for signing messages) + pub validator_hotkey: String, + /// Channel for sending messages to P2P layer + pub message_tx: mpsc::Sender, + /// Channel for receiving messages from P2P layer + pub message_rx: Arc>>, +} + +impl std::fmt::Debug for P2PChallengeConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("P2PChallengeConfig") + .field("challenge_id", &self.challenge_id) + .field("validator_hotkey", &self.validator_hotkey) + .finish_non_exhaustive() + } +} + +/// P2P challenge client for decentralized communication +/// +/// This client allows challenges to communicate with validators +/// through P2P channels without requiring a central server. +pub struct P2PChallengeClient { + config: P2PChallengeConfig, +} + +impl P2PChallengeClient { + /// Create a new P2P challenge client + pub fn new(config: P2PChallengeConfig) -> Self { + Self { config } + } + + /// Get the challenge ID + pub fn challenge_id(&self) -> &str { + &self.config.challenge_id + } + + /// Get the validator hotkey + pub fn validator_hotkey(&self) -> &str { + &self.config.validator_hotkey + } + + /// Submit evaluation result to P2P network + /// + /// # Arguments + /// + /// * `submission_hash` - Hash of the submission that was evaluated + /// * `score` - Evaluation score (0.0 - 1.0) + /// * `execution_time_ms` - How long the evaluation took in milliseconds + /// * `result_data` - Additional result data as JSON + /// + /// # Returns + /// + /// Returns `Ok(())` if the message was sent successfully, or an error + /// if the channel is closed or send failed. + pub async fn submit_evaluation( + &self, + submission_hash: &str, + score: f64, + execution_time_ms: u64, + result_data: serde_json::Value, + ) -> Result<(), ChallengeError> { + let msg = P2PChallengeMessage::EvaluationResult { + challenge_id: self.config.challenge_id.clone(), + submission_hash: submission_hash.to_string(), + score: score.clamp(0.0, 1.0), + execution_time_ms, + result_data, + }; + + debug!( + challenge_id = %self.config.challenge_id, + submission_hash = %submission_hash, + score = %score, + "Submitting evaluation result via P2P" + ); + + self.config + .message_tx + .send(msg) + .await + .map_err(|e| ChallengeError::Network(format!("Failed to send evaluation: {}", e)))?; + + Ok(()) + } + + /// Request pending submissions from the network + /// + /// # Arguments + /// + /// * `limit` - Maximum number of submissions to request + /// + /// # Returns + /// + /// Returns a list of pending submissions or an error on timeout/failure. + #[allow(clippy::await_holding_lock)] + pub async fn get_pending_submissions( + &self, + limit: usize, + ) -> Result, ChallengeError> { + let msg = P2PChallengeMessage::RequestSubmissions { + challenge_id: self.config.challenge_id.clone(), + limit, + }; + + debug!( + challenge_id = %self.config.challenge_id, + limit = %limit, + "Requesting pending submissions via P2P" + ); + + self.config.message_tx.send(msg).await.map_err(|e| { + ChallengeError::Network(format!("Failed to request submissions: {}", e)) + })?; + + // Wait for response (with timeout) + let mut rx = self.config.message_rx.write(); + match tokio::time::timeout(Duration::from_secs(DEFAULT_REQUEST_TIMEOUT_SECS), rx.recv()) + .await + { + Ok(Some(P2PChallengeMessage::SubmissionsResponse { submissions, .. })) => { + debug!( + challenge_id = %self.config.challenge_id, + count = %submissions.len(), + "Received pending submissions" + ); + Ok(submissions) + } + Ok(Some(other)) => { + warn!( + challenge_id = %self.config.challenge_id, + "Received unexpected message while waiting for submissions: {:?}", + other + ); + Ok(vec![]) + } + Ok(None) => { + warn!( + challenge_id = %self.config.challenge_id, + "P2P channel closed while waiting for submissions" + ); + Ok(vec![]) + } + Err(_) => Err(ChallengeError::Timeout( + "Request submissions timeout".to_string(), + )), + } + } + + /// Vote on weights for an epoch + /// + /// # Arguments + /// + /// * `epoch` - The epoch number to vote on + /// * `weights` - Weight votes as (hotkey, weight) pairs + /// + /// # Returns + /// + /// Returns `Ok(())` if the vote was submitted successfully. + pub async fn vote_weights( + &self, + epoch: u64, + weights: Vec<(String, f64)>, + ) -> Result<(), ChallengeError> { + // Clamp all weights to valid range + let clamped_weights: Vec<(String, f64)> = weights + .into_iter() + .map(|(k, w)| (k, w.clamp(0.0, 1.0))) + .collect(); + + let msg = P2PChallengeMessage::WeightVote { + challenge_id: self.config.challenge_id.clone(), + epoch, + weights: clamped_weights, + }; + + debug!( + challenge_id = %self.config.challenge_id, + epoch = %epoch, + "Voting on weights via P2P" + ); + + self.config + .message_tx + .send(msg) + .await + .map_err(|e| ChallengeError::Network(format!("Failed to vote weights: {}", e)))?; + + Ok(()) + } + + /// Get aggregated weights for an epoch + /// + /// # Arguments + /// + /// * `epoch` - The epoch number to get weights for + /// + /// # Returns + /// + /// Returns aggregated weights as (hotkey, weight) pairs or an error on timeout. + #[allow(clippy::await_holding_lock)] + pub async fn get_weights(&self, epoch: u64) -> Result, ChallengeError> { + let msg = P2PChallengeMessage::RequestWeights { + challenge_id: self.config.challenge_id.clone(), + epoch, + }; + + debug!( + challenge_id = %self.config.challenge_id, + epoch = %epoch, + "Requesting weights via P2P" + ); + + self.config + .message_tx + .send(msg) + .await + .map_err(|e| ChallengeError::Network(format!("Failed to request weights: {}", e)))?; + + // Wait for response + let mut rx = self.config.message_rx.write(); + match tokio::time::timeout(Duration::from_secs(DEFAULT_REQUEST_TIMEOUT_SECS), rx.recv()) + .await + { + Ok(Some(P2PChallengeMessage::WeightsResponse { weights, .. })) => { + debug!( + challenge_id = %self.config.challenge_id, + epoch = %epoch, + count = %weights.len(), + "Received aggregated weights" + ); + Ok(weights) + } + Ok(Some(other)) => { + warn!( + challenge_id = %self.config.challenge_id, + "Received unexpected message while waiting for weights: {:?}", + other + ); + Ok(vec![]) + } + Ok(None) => { + warn!( + challenge_id = %self.config.challenge_id, + "P2P channel closed while waiting for weights" + ); + Ok(vec![]) + } + Err(_) => Err(ChallengeError::Timeout( + "Request weights timeout".to_string(), + )), + } + } + + /// Store a submission in the distributed network + /// + /// # Arguments + /// + /// * `submission` - The submission to store in the P2P network + /// + /// # Returns + /// + /// Returns `Ok(())` if the message was sent successfully, or an error + /// if the channel is closed or send failed. + pub async fn store_submission( + &self, + submission: PendingSubmission, + ) -> Result<(), ChallengeError> { + let msg = P2PChallengeMessage::StoreSubmission { + challenge_id: self.config.challenge_id.clone(), + submission: submission.clone(), + }; + + debug!( + challenge_id = %self.config.challenge_id, + submission_hash = %submission.submission_hash, + miner_hotkey = %submission.miner_hotkey, + "Storing submission in distributed network" + ); + + self.config + .message_tx + .send(msg) + .await + .map_err(|e| ChallengeError::Network(format!("Failed to store submission: {}", e)))?; + + Ok(()) + } + + /// Get evaluation status for a submission + /// + /// Requests the evaluation status for a submission from the P2P network. + /// Returns the list of validator evaluations received so far and the + /// final aggregated score if consensus has been reached. + /// + /// # Arguments + /// + /// * `submission_hash` - Hash of the submission to check status for + /// + /// # Returns + /// + /// Returns a tuple of (evaluations, final_score) where: + /// - `evaluations` is a list of validator evaluation results + /// - `final_score` is `Some(score)` if consensus was reached, `None` otherwise + #[allow(clippy::await_holding_lock)] + pub async fn get_evaluation_status( + &self, + submission_hash: &str, + ) -> Result<(Vec, Option), ChallengeError> { + let msg = P2PChallengeMessage::RequestEvaluationStatus { + challenge_id: self.config.challenge_id.clone(), + submission_hash: submission_hash.to_string(), + }; + + debug!( + challenge_id = %self.config.challenge_id, + submission_hash = %submission_hash, + "Requesting evaluation status via P2P" + ); + + self.config + .message_tx + .send(msg) + .await + .map_err(|e| ChallengeError::Network(format!("Failed to request status: {}", e)))?; + + // Wait for response with timeout + let mut rx = self.config.message_rx.write(); + match tokio::time::timeout(Duration::from_secs(DEFAULT_REQUEST_TIMEOUT_SECS), rx.recv()) + .await + { + Ok(Some(P2PChallengeMessage::EvaluationStatusResponse { + evaluations, + final_score, + .. + })) => { + debug!( + challenge_id = %self.config.challenge_id, + submission_hash = %submission_hash, + evaluation_count = %evaluations.len(), + has_final_score = %final_score.is_some(), + "Received evaluation status" + ); + Ok((evaluations, final_score)) + } + Ok(Some(other)) => { + warn!( + challenge_id = %self.config.challenge_id, + "Received unexpected message while waiting for evaluation status: {:?}", + other + ); + Ok((vec![], None)) + } + Ok(None) => { + warn!( + challenge_id = %self.config.challenge_id, + "P2P channel closed while waiting for evaluation status" + ); + Ok((vec![], None)) + } + Err(_) => Err(ChallengeError::Timeout( + "Evaluation status timeout".to_string(), + )), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test_config() -> (P2PChallengeConfig, mpsc::Receiver) { + let (tx, rx) = mpsc::channel(100); + let (_, inner_rx) = mpsc::channel(100); + + let config = P2PChallengeConfig { + challenge_id: "test-challenge".to_string(), + validator_hotkey: "test-validator".to_string(), + message_tx: tx, + message_rx: Arc::new(RwLock::new(inner_rx)), + }; + + (config, rx) + } + + #[test] + fn test_p2p_challenge_message_serialization() { + let msg = P2PChallengeMessage::EvaluationResult { + challenge_id: "test".to_string(), + submission_hash: "hash123".to_string(), + score: 0.95, + execution_time_ms: 1500, + result_data: serde_json::json!({"passed": true}), + }; + + let json = serde_json::to_string(&msg).expect("Serialization should work"); + let deserialized: P2PChallengeMessage = + serde_json::from_str(&json).expect("Deserialization should work"); + + if let P2PChallengeMessage::EvaluationResult { + challenge_id, + score, + .. + } = deserialized + { + assert_eq!(challenge_id, "test"); + assert!((score - 0.95).abs() < f64::EPSILON); + } else { + panic!("Wrong message type after deserialization"); + } + } + + #[test] + fn test_pending_submission_serialization() { + let submission = PendingSubmission { + submission_hash: "abc123".to_string(), + miner_hotkey: "5GrwvaEF...".to_string(), + source_code: "fn main() {}".to_string(), + metadata: serde_json::json!({"version": "1.0"}), + submitted_at: 1704067200, + }; + + let json = serde_json::to_string(&submission).expect("Serialization should work"); + let deserialized: PendingSubmission = + serde_json::from_str(&json).expect("Deserialization should work"); + + assert_eq!(deserialized.submission_hash, "abc123"); + assert_eq!(deserialized.source_code, "fn main() {}"); + } + + #[test] + fn test_p2p_config_debug() { + let (config, _rx) = create_test_config(); + let debug_str = format!("{:?}", config); + + assert!(debug_str.contains("test-challenge")); + assert!(debug_str.contains("test-validator")); + } + + #[test] + fn test_p2p_client_accessors() { + let (config, _rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + assert_eq!(client.challenge_id(), "test-challenge"); + assert_eq!(client.validator_hotkey(), "test-validator"); + } + + #[tokio::test] + async fn test_submit_evaluation() { + let (config, mut rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + let result = client + .submit_evaluation("hash123", 0.85, 1000, serde_json::json!({"test": true})) + .await; + + assert!(result.is_ok()); + + // Verify message was sent + let msg = rx.recv().await.expect("Should receive message"); + if let P2PChallengeMessage::EvaluationResult { + submission_hash, + score, + execution_time_ms, + .. + } = msg + { + assert_eq!(submission_hash, "hash123"); + assert!((score - 0.85).abs() < f64::EPSILON); + assert_eq!(execution_time_ms, 1000); + } else { + panic!("Wrong message type"); + } + } + + #[tokio::test] + async fn test_submit_evaluation_clamps_score() { + let (config, mut rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + // Score above 1.0 should be clamped + let _ = client + .submit_evaluation("hash", 1.5, 100, serde_json::json!({})) + .await; + + let msg = rx.recv().await.expect("Should receive message"); + if let P2PChallengeMessage::EvaluationResult { score, .. } = msg { + assert!((score - 1.0).abs() < f64::EPSILON); + } else { + panic!("Wrong message type"); + } + } + + #[tokio::test] + async fn test_vote_weights() { + let (config, mut rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + let weights = vec![("hotkey1".to_string(), 0.6), ("hotkey2".to_string(), 0.4)]; + + let result = client.vote_weights(5, weights).await; + assert!(result.is_ok()); + + let msg = rx.recv().await.expect("Should receive message"); + if let P2PChallengeMessage::WeightVote { epoch, weights, .. } = msg { + assert_eq!(epoch, 5); + assert_eq!(weights.len(), 2); + } else { + panic!("Wrong message type"); + } + } + + #[tokio::test] + async fn test_vote_weights_clamps_values() { + let (config, mut rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + let weights = vec![ + ("hotkey1".to_string(), -0.5), // Should clamp to 0.0 + ("hotkey2".to_string(), 1.5), // Should clamp to 1.0 + ]; + + let _ = client.vote_weights(1, weights).await; + + let msg = rx.recv().await.expect("Should receive message"); + if let P2PChallengeMessage::WeightVote { weights, .. } = msg { + assert!((weights[0].1 - 0.0).abs() < f64::EPSILON); + assert!((weights[1].1 - 1.0).abs() < f64::EPSILON); + } else { + panic!("Wrong message type"); + } + } + + #[tokio::test] + async fn test_request_submissions_sends_message() { + let (tx, mut rx) = mpsc::channel(100); + let (response_tx, response_rx) = mpsc::channel(100); + + let config = P2PChallengeConfig { + challenge_id: "test-challenge".to_string(), + validator_hotkey: "test-validator".to_string(), + message_tx: tx, + message_rx: Arc::new(RwLock::new(response_rx)), + }; + + let client = P2PChallengeClient::new(config); + + // Send request in select! so we can check both branches + let request_future = client.get_pending_submissions(10); + + // Use select! to check that request was sent without waiting for timeout + tokio::select! { + _ = request_future => { + // If we get here, means we got a response or error + } + msg = rx.recv() => { + let msg = msg.expect("Should receive request message"); + if let P2PChallengeMessage::RequestSubmissions { + challenge_id, + limit, + } = msg + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(limit, 10); + } else { + panic!("Wrong message type"); + } + } + } + } + + #[tokio::test] + async fn test_request_weights_sends_message() { + let (tx, mut rx) = mpsc::channel(100); + let (response_tx, response_rx) = mpsc::channel(100); + + let config = P2PChallengeConfig { + challenge_id: "test-challenge".to_string(), + validator_hotkey: "test-validator".to_string(), + message_tx: tx, + message_rx: Arc::new(RwLock::new(response_rx)), + }; + + let client = P2PChallengeClient::new(config); + + let request_future = client.get_weights(42); + + tokio::select! { + _ = request_future => { + // If we get here, means we got a response or error + } + msg = rx.recv() => { + let msg = msg.expect("Should receive request message"); + if let P2PChallengeMessage::RequestWeights { + challenge_id, + epoch, + } = msg + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(epoch, 42); + } else { + panic!("Wrong message type"); + } + } + } + } + + #[test] + fn test_all_message_variants_serialize() { + let messages = vec![ + P2PChallengeMessage::EvaluationResult { + challenge_id: "c1".to_string(), + submission_hash: "h1".to_string(), + score: 0.5, + execution_time_ms: 100, + result_data: serde_json::json!({}), + }, + P2PChallengeMessage::RequestSubmissions { + challenge_id: "c2".to_string(), + limit: 10, + }, + P2PChallengeMessage::SubmissionsResponse { + challenge_id: "c3".to_string(), + submissions: vec![], + }, + P2PChallengeMessage::WeightVote { + challenge_id: "c4".to_string(), + epoch: 1, + weights: vec![], + }, + P2PChallengeMessage::RequestWeights { + challenge_id: "c5".to_string(), + epoch: 2, + }, + P2PChallengeMessage::WeightsResponse { + challenge_id: "c6".to_string(), + epoch: 3, + weights: vec![], + }, + P2PChallengeMessage::StoreSubmission { + challenge_id: "c7".to_string(), + submission: PendingSubmission { + submission_hash: "sh1".to_string(), + miner_hotkey: "miner1".to_string(), + source_code: "code".to_string(), + metadata: serde_json::json!({}), + submitted_at: 1704067200, + }, + }, + P2PChallengeMessage::RequestEvaluationStatus { + challenge_id: "c8".to_string(), + submission_hash: "sh2".to_string(), + }, + P2PChallengeMessage::EvaluationStatusResponse { + challenge_id: "c9".to_string(), + submission_hash: "sh3".to_string(), + evaluations: vec![], + consensus_reached: false, + final_score: None, + }, + ]; + + for msg in messages { + let json = serde_json::to_string(&msg).expect("All message variants should serialize"); + let _: P2PChallengeMessage = + serde_json::from_str(&json).expect("All message variants should deserialize"); + } + } + + #[test] + fn test_validator_evaluation_result_serialization() { + let result = ValidatorEvaluationResult { + validator_hotkey: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), + score: 0.87, + evaluated_at: 1704067200, + }; + + let json = serde_json::to_string(&result).expect("Serialization should work"); + let deserialized: ValidatorEvaluationResult = + serde_json::from_str(&json).expect("Deserialization should work"); + + assert_eq!( + deserialized.validator_hotkey, + "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" + ); + assert!((deserialized.score - 0.87).abs() < f64::EPSILON); + assert_eq!(deserialized.evaluated_at, 1704067200); + } + + #[test] + fn test_store_submission_message_serialization() { + let submission = PendingSubmission { + submission_hash: "store-hash-123".to_string(), + miner_hotkey: "5GrwvaEF...".to_string(), + source_code: "fn main() { println!(\"hello\"); }".to_string(), + metadata: serde_json::json!({"language": "rust", "version": "1.0"}), + submitted_at: 1704067200, + }; + + let msg = P2PChallengeMessage::StoreSubmission { + challenge_id: "test-challenge".to_string(), + submission: submission.clone(), + }; + + let json = serde_json::to_string(&msg).expect("Serialization should work"); + let deserialized: P2PChallengeMessage = + serde_json::from_str(&json).expect("Deserialization should work"); + + if let P2PChallengeMessage::StoreSubmission { + challenge_id, + submission: deser_submission, + } = deserialized + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(deser_submission.submission_hash, "store-hash-123"); + assert_eq!( + deser_submission.source_code, + "fn main() { println!(\"hello\"); }" + ); + } else { + panic!("Wrong message type after deserialization"); + } + } + + #[test] + fn test_evaluation_status_response_serialization() { + let evaluations = vec![ + ValidatorEvaluationResult { + validator_hotkey: "validator1".to_string(), + score: 0.85, + evaluated_at: 1704067200, + }, + ValidatorEvaluationResult { + validator_hotkey: "validator2".to_string(), + score: 0.90, + evaluated_at: 1704067300, + }, + ]; + + let msg = P2PChallengeMessage::EvaluationStatusResponse { + challenge_id: "test-challenge".to_string(), + submission_hash: "test-hash".to_string(), + evaluations: evaluations.clone(), + consensus_reached: true, + final_score: Some(0.875), + }; + + let json = serde_json::to_string(&msg).expect("Serialization should work"); + let deserialized: P2PChallengeMessage = + serde_json::from_str(&json).expect("Deserialization should work"); + + if let P2PChallengeMessage::EvaluationStatusResponse { + challenge_id, + submission_hash, + evaluations: deser_evals, + consensus_reached, + final_score, + } = deserialized + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(submission_hash, "test-hash"); + assert_eq!(deser_evals.len(), 2); + assert!(consensus_reached); + assert!((final_score.expect("Should have final score") - 0.875).abs() < f64::EPSILON); + } else { + panic!("Wrong message type after deserialization"); + } + } + + #[tokio::test] + async fn test_store_submission() { + let (config, mut rx) = create_test_config(); + let client = P2PChallengeClient::new(config); + + let submission = PendingSubmission { + submission_hash: "store-test-hash".to_string(), + miner_hotkey: "test-miner".to_string(), + source_code: "fn main() {}".to_string(), + metadata: serde_json::json!({"test": true}), + submitted_at: 1704067200, + }; + + let result = client.store_submission(submission.clone()).await; + assert!(result.is_ok()); + + // Verify message was sent + let msg = rx.recv().await.expect("Should receive message"); + if let P2PChallengeMessage::StoreSubmission { + challenge_id, + submission: recv_submission, + } = msg + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(recv_submission.submission_hash, "store-test-hash"); + assert_eq!(recv_submission.miner_hotkey, "test-miner"); + } else { + panic!("Wrong message type"); + } + } + + #[tokio::test] + async fn test_request_evaluation_status_sends_message() { + let (tx, mut rx) = mpsc::channel(100); + let (_, response_rx) = mpsc::channel(100); + + let config = P2PChallengeConfig { + challenge_id: "test-challenge".to_string(), + validator_hotkey: "test-validator".to_string(), + message_tx: tx, + message_rx: Arc::new(RwLock::new(response_rx)), + }; + + let client = P2PChallengeClient::new(config); + + let request_future = client.get_evaluation_status("test-submission-hash"); + + // Use select! to check that request was sent without waiting for timeout + tokio::select! { + _ = request_future => { + // If we get here, means we got a response or timeout + } + msg = rx.recv() => { + let msg = msg.expect("Should receive request message"); + if let P2PChallengeMessage::RequestEvaluationStatus { + challenge_id, + submission_hash, + } = msg + { + assert_eq!(challenge_id, "test-challenge"); + assert_eq!(submission_hash, "test-submission-hash"); + } else { + panic!("Wrong message type"); + } + } + } + } + + #[test] + fn test_evaluation_status_response_no_consensus() { + let msg = P2PChallengeMessage::EvaluationStatusResponse { + challenge_id: "challenge".to_string(), + submission_hash: "hash".to_string(), + evaluations: vec![ValidatorEvaluationResult { + validator_hotkey: "v1".to_string(), + score: 0.5, + evaluated_at: 1704067200, + }], + consensus_reached: false, + final_score: None, + }; + + let json = serde_json::to_string(&msg).expect("Should serialize"); + let deser: P2PChallengeMessage = serde_json::from_str(&json).expect("Should deserialize"); + + if let P2PChallengeMessage::EvaluationStatusResponse { + consensus_reached, + final_score, + evaluations, + .. + } = deser + { + assert!(!consensus_reached); + assert!(final_score.is_none()); + assert_eq!(evaluations.len(), 1); + } else { + panic!("Wrong type"); + } + } +} diff --git a/crates/challenge-sdk/src/routes.rs b/crates/challenge-sdk/src/routes.rs new file mode 100644 index 000000000..ff203f9f7 --- /dev/null +++ b/crates/challenge-sdk/src/routes.rs @@ -0,0 +1,897 @@ +//! Provides the generic route infrastructure for challenges to define custom +//! HTTP routes that get mounted on the RPC server. Each challenge declares its +//! own routes and handlers via the `ServerChallenge` trait โ€” the platform SDK +//! does NOT hardcode any challenge-specific routes. +//! use platform_challenge_sdk::server::{ServerChallenge, ChallengeContext}; +//! impl ServerChallenge for MyChallenge { +//! // ... challenge_id, name, version, evaluate ... +//! +//! on the RPC server. Each challenge can expose its own API endpoints. +//! +//! # Example +//! +//! ```text +//! use platform_challenge_sdk::routes::*; +//! +//! impl Challenge for MyChallenge { +//! fn routes(&self) -> Vec { +//! vec![ +//! ChallengeRoute::get("/leaderboard", "Get current leaderboard"), +//! ChallengeRoute::get("/stats", "Get challenge statistics"), +//! ChallengeRoute::post("/submit", "Submit evaluation result"), +//! ChallengeRoute::get("/agent/:hash", "Get agent details"), +//! ] +//! } +//! +//! async fn handle_route( +//! &self, +//! ctx: &ChallengeContext, +//! req: RouteRequest, +//! ) -> RouteResponse { +//! match (req.method.as_str(), req.path.as_str()) { +//! ("GET", "/leaderboard") => { +//! let data = self.get_leaderboard(ctx).await; +//! RouteResponse::json(data) +//! } +//! ("GET", path) if path.starts_with("/agent/") => { +//! let hash = &path[7..]; +//! let agent = self.get_agent(ctx, hash).await; +//! RouteResponse::json(agent) +//! } +//! _ => RouteResponse::not_found() +//! } +//! } +//! } +//! ``` +//! +//! // The platform SDK provides the generic building blocks (ChallengeRoute, +//! // RouteRequest, RouteResponse, RouteRegistry, RouteBuilder, RoutesManifest, +//! // HttpMethod) โ€” challenges use these to declare their own routes. +//! _ => RouteResponse::not_found() +//! } +//! } +//! } +//! ``` + +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; + +/// Routes manifest returned by /.well-known/routes endpoint +/// This is the standard format for dynamic route discovery +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct RoutesManifest { + /// Challenge name (normalized: lowercase, dashes only) + pub name: String, + /// Challenge version + pub version: String, + /// Human-readable description + pub description: String, + /// List of available routes + pub routes: Vec, + /// Optional metadata + #[serde(default)] + pub metadata: HashMap, +} + +impl RoutesManifest { + /// Create a new routes manifest + pub fn new(name: impl Into, version: impl Into) -> Self { + Self { + name: Self::normalize_name(&name.into()), + version: version.into(), + description: String::new(), + routes: Vec::new(), + metadata: HashMap::new(), + } + } + + /// Normalize challenge name: lowercase, replace spaces/underscores with dashes + pub fn normalize_name(name: &str) -> String { + name.trim() + .to_lowercase() + .replace([' ', '_'], "-") + .chars() + .filter(|c| c.is_alphanumeric() || *c == '-') + .collect::() + .trim_matches('-') + .to_string() + } + + /// Set description + pub fn with_description(mut self, description: impl Into) -> Self { + self.description = description.into(); + self + } + + /// Add a single route + pub fn add_route(mut self, route: ChallengeRoute) -> Self { + self.routes.push(route); + self + } + + /// Add multiple routes + pub fn with_routes(mut self, routes: Vec) -> Self { + self.routes.extend(routes); + self + } + + /// Add metadata + pub fn with_metadata(mut self, key: impl Into, value: Value) -> Self { + self.metadata.insert(key.into(), value); + self + } +} + +/// HTTP method for routes +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "UPPERCASE")] +pub enum HttpMethod { + Get, + Post, + Put, + Delete, + Patch, +} + +impl HttpMethod { + pub fn as_str(&self) -> &'static str { + match self { + HttpMethod::Get => "GET", + HttpMethod::Post => "POST", + HttpMethod::Put => "PUT", + HttpMethod::Delete => "DELETE", + HttpMethod::Patch => "PATCH", + } + } +} + +impl std::fmt::Display for HttpMethod { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } +} + +/// Definition of a custom route exposed by a challenge +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ChallengeRoute { + /// HTTP method (GET, POST, etc.) + pub method: HttpMethod, + /// Path pattern (e.g., "/leaderboard", "/agent/:hash") + pub path: String, + /// Description of what this route does + pub description: String, + /// Whether authentication is required + pub requires_auth: bool, + /// Rate limit (requests per minute, 0 = unlimited) + pub rate_limit: u32, +} + +impl ChallengeRoute { + /// Create a new route + pub fn new( + method: HttpMethod, + path: impl Into, + description: impl Into, + ) -> Self { + Self { + method, + path: path.into(), + description: description.into(), + requires_auth: false, + rate_limit: 0, + } + } + + /// Create a GET route + pub fn get(path: impl Into, description: impl Into) -> Self { + Self::new(HttpMethod::Get, path, description) + } + + /// Create a POST route + pub fn post(path: impl Into, description: impl Into) -> Self { + Self::new(HttpMethod::Post, path, description) + } + + /// Create a PUT route + pub fn put(path: impl Into, description: impl Into) -> Self { + Self::new(HttpMethod::Put, path, description) + } + + /// Create a DELETE route + pub fn delete(path: impl Into, description: impl Into) -> Self { + Self::new(HttpMethod::Delete, path, description) + } + + /// Require authentication for this route + pub fn with_auth(mut self) -> Self { + self.requires_auth = true; + self + } + + /// Set rate limit (requests per minute) + pub fn with_rate_limit(mut self, rpm: u32) -> Self { + self.rate_limit = rpm; + self + } + + /// Check if a request matches this route + pub fn matches(&self, method: &str, path: &str) -> Option> { + if method != self.method.as_str() { + return None; + } + + // Simple pattern matching with :param support + let pattern_parts: Vec<&str> = self.path.split('/').collect(); + let path_parts: Vec<&str> = path.split('/').collect(); + + if pattern_parts.len() != path_parts.len() { + return None; + } + + let mut params = HashMap::new(); + + for (pattern, actual) in pattern_parts.iter().zip(path_parts.iter()) { + if let Some(param_name) = pattern.strip_prefix(':') { + // This is a parameter + params.insert(param_name.to_string(), actual.to_string()); + } else if pattern != actual { + return None; + } + } + + Some(params) + } +} + +/// Incoming request to a challenge route +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct RouteRequest { + /// HTTP method + pub method: String, + /// Request path (relative to challenge) + pub path: String, + /// URL parameters extracted from path (e.g., :hash -> "abc123") + pub params: HashMap, + /// Query parameters + pub query: HashMap, + /// Request headers + pub headers: HashMap, + /// Request body (JSON) + pub body: Value, + /// Authenticated validator hotkey (if any) + pub auth_hotkey: Option, +} + +impl RouteRequest { + /// Create a new request + pub fn new(method: impl Into, path: impl Into) -> Self { + Self { + method: method.into(), + path: path.into(), + params: HashMap::new(), + query: HashMap::new(), + headers: HashMap::new(), + body: Value::Null, + auth_hotkey: None, + } + } + + /// Set path parameters + pub fn with_params(mut self, params: HashMap) -> Self { + self.params = params; + self + } + + /// Set query parameters + pub fn with_query(mut self, query: HashMap) -> Self { + self.query = query; + self + } + + /// Set request body + pub fn with_body(mut self, body: Value) -> Self { + self.body = body; + self + } + + /// Set auth hotkey + pub fn with_auth(mut self, hotkey: String) -> Self { + self.auth_hotkey = Some(hotkey); + self + } + + /// Get a path parameter + pub fn param(&self, name: &str) -> Option<&str> { + self.params.get(name).map(|s| s.as_str()) + } + + /// Get a query parameter + pub fn query_param(&self, name: &str) -> Option<&str> { + self.query.get(name).map(|s| s.as_str()) + } + + /// Parse body as type T + pub fn parse_body Deserialize<'de>>(&self) -> Result { + serde_json::from_value(self.body.clone()) + } +} + +/// Response from a challenge route +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct RouteResponse { + /// HTTP status code + pub status: u16, + /// Response headers + pub headers: HashMap, + /// Response body (JSON) + pub body: Value, +} + +impl RouteResponse { + /// Create a new response + pub fn new(status: u16, body: Value) -> Self { + Self { + status, + headers: HashMap::new(), + body, + } + } + + /// Create a 200 OK response with JSON body + pub fn ok(body: Value) -> Self { + Self::new(200, body) + } + + /// Create a 200 OK response by serializing data + pub fn json(data: T) -> Self { + Self::new(200, serde_json::to_value(data).unwrap_or(Value::Null)) + } + + /// Create a 201 Created response + pub fn created(body: Value) -> Self { + Self::new(201, body) + } + + /// Create a 204 No Content response + pub fn no_content() -> Self { + Self::new(204, Value::Null) + } + + /// Create a 400 Bad Request response + pub fn bad_request(message: impl Into) -> Self { + Self::new( + 400, + serde_json::json!({ + "error": "bad_request", + "message": message.into() + }), + ) + } + + /// Create a 401 Unauthorized response + pub fn unauthorized() -> Self { + Self::new( + 401, + serde_json::json!({ + "error": "unauthorized", + "message": "Authentication required" + }), + ) + } + + /// Create a 403 Forbidden response + pub fn forbidden(message: impl Into) -> Self { + Self::new( + 403, + serde_json::json!({ + "error": "forbidden", + "message": message.into() + }), + ) + } + + /// Create a 404 Not Found response + pub fn not_found() -> Self { + Self::new( + 404, + serde_json::json!({ + "error": "not_found", + "message": "Route not found" + }), + ) + } + + /// Create a 429 Too Many Requests response + pub fn rate_limited() -> Self { + Self::new( + 429, + serde_json::json!({ + "error": "rate_limited", + "message": "Too many requests" + }), + ) + } + + /// Create a 500 Internal Server Error response + pub fn internal_error(message: impl Into) -> Self { + Self::new( + 500, + serde_json::json!({ + "error": "internal_error", + "message": message.into() + }), + ) + } + + /// Add a header to the response + pub fn with_header(mut self, name: impl Into, value: impl Into) -> Self { + self.headers.insert(name.into(), value.into()); + self + } + + /// Check if response is successful (2xx) + pub fn is_success(&self) -> bool { + self.status >= 200 && self.status < 300 + } +} + +/// Route registry for a challenge +#[derive(Debug, Clone, Default)] +pub struct RouteRegistry { + routes: Vec, +} + +impl RouteRegistry { + /// Create a new empty registry + pub fn new() -> Self { + Self { routes: vec![] } + } + + /// Register a route + pub fn register(&mut self, route: ChallengeRoute) { + self.routes.push(route); + } + + /// Register multiple routes + pub fn register_all(&mut self, routes: Vec) { + self.routes.extend(routes); + } + + /// Find a matching route + pub fn find_route( + &self, + method: &str, + path: &str, + ) -> Option<(&ChallengeRoute, HashMap)> { + for route in &self.routes { + if let Some(params) = route.matches(method, path) { + return Some((route, params)); + } + } + None + } + + /// Get all registered routes + pub fn routes(&self) -> &[ChallengeRoute] { + &self.routes + } + + /// Check if any routes are registered + pub fn is_empty(&self) -> bool { + self.routes.is_empty() + } +} + +/// Builder for creating routes fluently +pub struct RouteBuilder { + routes: Vec, +} + +impl RouteBuilder { + pub fn new() -> Self { + Self { routes: vec![] } + } + + pub fn get(mut self, path: impl Into, desc: impl Into) -> Self { + self.routes.push(ChallengeRoute::get(path, desc)); + self + } + + pub fn post(mut self, path: impl Into, desc: impl Into) -> Self { + self.routes.push(ChallengeRoute::post(path, desc)); + self + } + + pub fn put(mut self, path: impl Into, desc: impl Into) -> Self { + self.routes.push(ChallengeRoute::put(path, desc)); + self + } + + pub fn delete(mut self, path: impl Into, desc: impl Into) -> Self { + self.routes.push(ChallengeRoute::delete(path, desc)); + self + } + + pub fn build(self) -> Vec { + self.routes + } +} + +impl Default for RouteBuilder { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_routes_manifest_new() { + let manifest = RoutesManifest::new("Test Challenge", "1.0.0"); + assert_eq!(manifest.name, "test-challenge"); // normalized + assert_eq!(manifest.version, "1.0.0"); + assert!(manifest.routes.is_empty()); + } + + #[test] + fn test_normalize_name() { + assert_eq!( + RoutesManifest::normalize_name("Test Challenge"), + "test-challenge" + ); + assert_eq!( + RoutesManifest::normalize_name("Test_Challenge"), + "test-challenge" + ); + assert_eq!( + RoutesManifest::normalize_name("Test-Challenge-123"), + "test-challenge-123" + ); + assert_eq!(RoutesManifest::normalize_name("-test-"), "test"); + // Multiple spaces get replaced with multiple dashes (no collapsing) + assert_eq!( + RoutesManifest::normalize_name("Test Challenge"), + "test--challenge" + ); + } + + #[test] + fn test_routes_manifest_with_description() { + let manifest = RoutesManifest::new("test", "1.0").with_description("A test challenge"); + assert_eq!(manifest.description, "A test challenge"); + } + + #[test] + fn test_routes_manifest_add_route() { + let route = ChallengeRoute::get("/test", "Test route"); + let manifest = RoutesManifest::new("test", "1.0").add_route(route.clone()); + + assert_eq!(manifest.routes.len(), 1); + assert_eq!(manifest.routes[0].path, "/test"); + } + + #[test] + fn test_routes_manifest_with_routes() { + let routes = vec![ + ChallengeRoute::get("/route1", "Route 1"), + ChallengeRoute::post("/route2", "Route 2"), + ]; + + let manifest = RoutesManifest::new("test", "1.0").with_routes(routes); + assert_eq!(manifest.routes.len(), 2); + } + + #[test] + fn test_routes_manifest_with_metadata() { + let manifest = RoutesManifest::new("test", "1.0") + .with_metadata("author", serde_json::json!("John Doe")) + .with_metadata("license", serde_json::json!("MIT")); + + assert_eq!(manifest.metadata.len(), 2); + assert_eq!( + manifest.metadata.get("author"), + Some(&serde_json::json!("John Doe")) + ); + } + + #[test] + fn test_http_method_display() { + assert_eq!(format!("{}", HttpMethod::Get), "GET"); + assert_eq!(format!("{}", HttpMethod::Post), "POST"); + assert_eq!(format!("{}", HttpMethod::Put), "PUT"); + assert_eq!(format!("{}", HttpMethod::Delete), "DELETE"); + assert_eq!(format!("{}", HttpMethod::Patch), "PATCH"); + } + + #[test] + fn test_http_method_as_str() { + assert_eq!(HttpMethod::Get.as_str(), "GET"); + assert_eq!(HttpMethod::Post.as_str(), "POST"); + assert_eq!(HttpMethod::Put.as_str(), "PUT"); + assert_eq!(HttpMethod::Delete.as_str(), "DELETE"); + assert_eq!(HttpMethod::Patch.as_str(), "PATCH"); + } + + #[test] + fn test_challenge_route_put() { + let route = ChallengeRoute::put("/update", "Update resource"); + assert_eq!(route.method, HttpMethod::Put); + assert_eq!(route.path, "/update"); + assert_eq!(route.description, "Update resource"); + } + + #[test] + fn test_challenge_route_delete() { + let route = ChallengeRoute::delete("/remove", "Remove resource"); + assert_eq!(route.method, HttpMethod::Delete); + assert_eq!(route.path, "/remove"); + } + + #[test] + fn test_challenge_route_with_auth() { + let route = ChallengeRoute::get("/private", "Private route").with_auth(); + assert!(route.requires_auth); + } + + #[test] + fn test_challenge_route_with_rate_limit() { + let route = ChallengeRoute::post("/submit", "Submit").with_rate_limit(10); + assert_eq!(route.rate_limit, 10); + } + + #[test] + fn test_route_matching() { + let route = ChallengeRoute::get("/agent/:hash", "Get agent"); + + // Should match + let params = route.matches("GET", "/agent/abc123"); + assert!(params.is_some()); + assert_eq!(params.unwrap().get("hash"), Some(&"abc123".to_string())); + + // Should not match wrong method + assert!(route.matches("POST", "/agent/abc123").is_none()); + + // Should not match wrong path + assert!(route.matches("GET", "/user/abc123").is_none()); + } + + #[test] + fn test_route_request_new() { + let req = RouteRequest::new("GET", "/test"); + assert_eq!(req.method, "GET"); + assert_eq!(req.path, "/test"); + assert!(req.params.is_empty()); + assert!(req.query.is_empty()); + } + + #[test] + fn test_route_request_with_params() { + let mut params = HashMap::new(); + params.insert("id".to_string(), "123".to_string()); + + let req = RouteRequest::new("GET", "/test").with_params(params); + assert_eq!(req.param("id"), Some("123")); + } + + #[test] + fn test_route_request_with_query() { + let mut query = HashMap::new(); + query.insert("limit".to_string(), "10".to_string()); + + let req = RouteRequest::new("GET", "/test").with_query(query); + assert_eq!(req.query_param("limit"), Some("10")); + } + + #[test] + fn test_route_request_with_body() { + let body = serde_json::json!({"key": "value"}); + let req = RouteRequest::new("POST", "/test").with_body(body.clone()); + assert_eq!(req.body, body); + } + + #[test] + fn test_route_request_with_auth() { + let req = RouteRequest::new("GET", "/test").with_auth("hotkey123".to_string()); + assert_eq!(req.auth_hotkey, Some("hotkey123".to_string())); + } + + #[test] + fn test_route_request_param() { + let mut params = HashMap::new(); + params.insert("id".to_string(), "abc".to_string()); + + let req = RouteRequest::new("GET", "/test").with_params(params); + assert_eq!(req.param("id"), Some("abc")); + assert_eq!(req.param("missing"), None); + } + + #[test] + fn test_route_request_query_param() { + let mut query = HashMap::new(); + query.insert("page".to_string(), "2".to_string()); + + let req = RouteRequest::new("GET", "/test").with_query(query); + assert_eq!(req.query_param("page"), Some("2")); + assert_eq!(req.query_param("missing"), None); + } + + #[test] + fn test_route_request_parse_body() { + #[derive(serde::Deserialize)] + struct TestData { + value: i32, + } + + let body = serde_json::json!({"value": 42}); + let req = RouteRequest::new("POST", "/test").with_body(body); + + let parsed: TestData = req.parse_body().unwrap(); + assert_eq!(parsed.value, 42); + } + + #[test] + fn test_route_response_ok() { + let resp = RouteResponse::ok(serde_json::json!({"status": "ok"})); + assert_eq!(resp.status, 200); + assert!(resp.is_success()); + } + + #[test] + fn test_route_response_created() { + let resp = RouteResponse::created(serde_json::json!({"id": "123"})); + assert_eq!(resp.status, 201); + assert!(resp.is_success()); + } + + #[test] + fn test_route_response_no_content() { + let resp = RouteResponse::no_content(); + assert_eq!(resp.status, 204); + assert!(resp.is_success()); + } + + #[test] + fn test_route_response_unauthorized() { + let resp = RouteResponse::unauthorized(); + assert_eq!(resp.status, 401); + assert!(!resp.is_success()); + } + + #[test] + fn test_route_response_forbidden() { + let resp = RouteResponse::forbidden("Access denied"); + assert_eq!(resp.status, 403); + assert!(!resp.is_success()); + } + + #[test] + fn test_route_response_rate_limited() { + let resp = RouteResponse::rate_limited(); + assert_eq!(resp.status, 429); + assert!(!resp.is_success()); + } + + #[test] + fn test_route_response_internal_error() { + let resp = RouteResponse::internal_error("Something went wrong"); + assert_eq!(resp.status, 500); + assert!(!resp.is_success()); + } + + #[test] + fn test_route_response_with_header() { + let resp = RouteResponse::ok(serde_json::json!({})).with_header("X-Custom", "value"); + + assert_eq!(resp.headers.get("X-Custom"), Some(&"value".to_string())); + } + + #[test] + fn test_route_response_is_success() { + assert!(RouteResponse::ok(serde_json::json!({})).is_success()); + assert!(RouteResponse::created(serde_json::json!({})).is_success()); + assert!(!RouteResponse::bad_request("error").is_success()); + assert!(!RouteResponse::not_found().is_success()); + } + + #[test] + fn test_route_registry_register_all() { + let mut registry = RouteRegistry::new(); + let routes = vec![ + ChallengeRoute::get("/a", "Route A"), + ChallengeRoute::post("/b", "Route B"), + ]; + + registry.register_all(routes); + assert_eq!(registry.routes().len(), 2); + } + + #[test] + fn test_route_registry_routes() { + let mut registry = RouteRegistry::new(); + registry.register(ChallengeRoute::get("/test", "Test")); + + let routes = registry.routes(); + assert_eq!(routes.len(), 1); + assert_eq!(routes[0].path, "/test"); + } + + #[test] + fn test_route_registry_is_empty() { + let registry = RouteRegistry::new(); + assert!(registry.is_empty()); + + let mut registry = RouteRegistry::new(); + registry.register(ChallengeRoute::get("/test", "Test")); + assert!(!registry.is_empty()); + } + + #[test] + fn test_route_builder() { + let routes = RouteBuilder::new() + .get("/leaderboard", "Get leaderboard") + .post("/submit", "Submit result") + .get("/agent/:hash", "Get agent") + .build(); + + assert_eq!(routes.len(), 3); + } + + #[test] + fn test_route_builder_default() { + let builder = RouteBuilder::default(); + let routes = builder.build(); + assert!(routes.is_empty()); + } + + #[test] + fn test_route_builder_put() { + let routes = RouteBuilder::new() + .put("/update/:id", "Update item") + .build(); + + assert_eq!(routes.len(), 1); + assert_eq!(routes[0].method, HttpMethod::Put); + } + + #[test] + fn test_route_builder_delete() { + let routes = RouteBuilder::new() + .delete("/remove/:id", "Remove item") + .build(); + + assert_eq!(routes.len(), 1); + assert_eq!(routes[0].method, HttpMethod::Delete); + } + + #[test] + fn test_route_registry_new() { + let registry = RouteRegistry::new(); + assert!(registry.routes.is_empty()); + } + + #[test] + fn test_route_registry_register() { + let mut registry = RouteRegistry::new(); + registry.register(ChallengeRoute::get("/test", "Test")); + registry.register(ChallengeRoute::get("/user/:id", "Get user")); + + let (route, params) = registry.find_route("GET", "/user/123").unwrap(); + assert_eq!(route.path, "/user/:id"); + assert_eq!(params.get("id"), Some(&"123".to_string())); + } + + #[test] + fn test_response_helpers() { + let resp = RouteResponse::json(serde_json::json!({"key": "value"})); + assert_eq!(resp.status, 200); + + let resp = RouteResponse::not_found(); + assert_eq!(resp.status, 404); + + let resp = RouteResponse::bad_request("Invalid input"); + assert_eq!(resp.status, 400); + } +} diff --git a/crates/challenge-sdk/src/server.rs b/crates/challenge-sdk/src/server.rs new file mode 100644 index 000000000..b8be80aaa --- /dev/null +++ b/crates/challenge-sdk/src/server.rs @@ -0,0 +1,1930 @@ +//! Challenge Server Mode +//! +//! Provides HTTP server functionality for challenges to expose their evaluation +//! endpoints to validators in the P2P network. +//! +//! # Usage +//! +//! ```text +//! use platform_challenge_sdk::server::{ChallengeServer, ServerConfig, ChallengeContext}; +//! use platform_challenge_sdk::routes::{ChallengeRoute, RouteRequest, RouteResponse}; +//! +//! #[async_trait] +//! impl ServerChallenge for MyChallenge { +//! fn challenge_id(&self) -> &str { "my-challenge" } +//! fn name(&self) -> &str { "My Challenge" } +//! fn version(&self) -> &str { "0.1.0" } +//! +//! async fn evaluate(&self, req: EvaluationRequest) -> Result { +//! // Your evaluation logic here +//! Ok(EvaluationResponse::success(&req.request_id, 0.95, json!({}))) +//! } +//! +//! // Declare custom routes this challenge exposes +//! fn routes(&self) -> Vec { +//! vec![ +//! ChallengeRoute::get("/leaderboard", "Get current leaderboard"), +//! ChallengeRoute::post("/submit", "Submit evaluation result"), +//! ] +//! } +//! +//! // Handle incoming route requests +//! async fn handle_route(&self, ctx: &ChallengeContext, req: RouteRequest) -> RouteResponse { +//! match (req.method.as_str(), req.path.as_str()) { +//! ("GET", "/leaderboard") => RouteResponse::json(json!({"entries": []})), +//! _ => RouteResponse::not_found(), +//! } +//! } +//! } +//! +//! #[tokio::main] +//! async fn main() -> Result<(), ChallengeError> { +//! ChallengeServer::builder(MyChallenge) +//! .port(8080) +//! .build() +//! .run() +//! .await +//! } +//! ``` +//! +//! # Platform Endpoints +//! +//! The server exposes these platform-level endpoints: +//! - `POST /evaluate` - Receive evaluation requests from platform +//! - `GET /health` - Health check +//! - `GET /config` - Challenge configuration schema +//! - `POST /validate` - Quick validation without full evaluation +//! +//! Additionally, any custom routes declared by `ServerChallenge::routes()` are +//! mounted and handled via `ServerChallenge::handle_route()`. + +use std::net::SocketAddr; +use std::sync::Arc; +use std::time::Instant; + +use serde::{Deserialize, Serialize}; +use tokio::sync::RwLock; +use tracing::{debug, error, info, warn}; + +use crate::database::ChallengeDatabase; +use crate::error::ChallengeError; +use crate::routes::{ChallengeRoute, RouteRequest, RouteResponse}; + +#[cfg(feature = "http-server")] +use axum::extract::State; + +/// Server configuration +#[derive(Debug, Clone)] +pub struct ServerConfig { + /// Host to bind to + pub host: String, + /// Port to listen on + pub port: u16, + /// Maximum concurrent evaluations + pub max_concurrent: usize, + /// Request timeout in seconds + pub timeout_secs: u64, + /// Enable CORS + pub cors_enabled: bool, +} + +impl Default for ServerConfig { + fn default() -> Self { + Self { + host: "0.0.0.0".to_string(), + port: 8080, + max_concurrent: 4, + timeout_secs: 600, + cors_enabled: true, + } + } +} + +impl ServerConfig { + /// Load from environment variables + pub fn from_env() -> Self { + Self { + host: std::env::var("CHALLENGE_HOST").unwrap_or_else(|_| "0.0.0.0".to_string()), + port: std::env::var("CHALLENGE_PORT") + .ok() + .and_then(|p| p.parse().ok()) + .unwrap_or(8080), + max_concurrent: std::env::var("MAX_CONCURRENT") + .ok() + .and_then(|n| n.parse().ok()) + .unwrap_or(4), + timeout_secs: std::env::var("TIMEOUT_SECS") + .ok() + .and_then(|n| n.parse().ok()) + .unwrap_or(600), + cors_enabled: true, + } + } +} + +// ============================================================================ +// GENERIC REQUEST/RESPONSE TYPES +// ============================================================================ + +/// Generic evaluation request from platform +/// +/// Platform sends this, challenge interprets the `data` field based on its needs +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EvaluationRequest { + /// Unique request ID + pub request_id: String, + /// Submission identifier + pub submission_id: String, + /// Participant identifier (could be miner hotkey, user id, etc.) + pub participant_id: String, + /// Submission data (challenge-specific, could be source code, config, etc.) + pub data: serde_json::Value, + /// Optional metadata + pub metadata: Option, + /// Current epoch/round + pub epoch: u64, + /// Deadline timestamp (unix seconds) + pub deadline: Option, +} + +/// Generic evaluation response to platform +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EvaluationResponse { + /// Request ID this responds to + pub request_id: String, + /// Whether evaluation succeeded + pub success: bool, + /// Error message if failed + pub error: Option, + /// Evaluation score (0.0 - 1.0) + pub score: f64, + /// Detailed results (challenge-specific) + pub results: serde_json::Value, + /// Execution time in milliseconds + pub execution_time_ms: i64, + /// Cost incurred (if applicable) + pub cost: Option, +} + +impl EvaluationResponse { + /// Create successful response + pub fn success(request_id: &str, score: f64, results: serde_json::Value) -> Self { + Self { + request_id: request_id.to_string(), + success: true, + error: None, + score, + results, + execution_time_ms: 0, + cost: None, + } + } + + /// Create error response + pub fn error(request_id: &str, error: impl Into) -> Self { + Self { + request_id: request_id.to_string(), + success: false, + error: Some(error.into()), + score: 0.0, + results: serde_json::Value::Null, + execution_time_ms: 0, + cost: None, + } + } + + /// Set execution time + pub fn with_time(mut self, ms: i64) -> Self { + self.execution_time_ms = ms; + self + } + + /// Set cost + pub fn with_cost(mut self, cost: f64) -> Self { + self.cost = Some(cost); + self + } +} + +/// Validation request (quick check without full evaluation) +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ValidationRequest { + /// Data to validate + pub data: serde_json::Value, +} + +/// Validation response +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ValidationResponse { + /// Whether data is valid + pub valid: bool, + /// Validation errors + pub errors: Vec, + /// Warnings (valid but not recommended) + pub warnings: Vec, +} + +/// Health check response +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HealthResponse { + /// Server is healthy + pub healthy: bool, + /// Current load (0.0 - 1.0) + pub load: f64, + /// Pending evaluations + pub pending: u32, + /// Uptime in seconds + pub uptime_secs: u64, + /// Version + pub version: String, + /// Challenge ID + pub challenge_id: String, +} + +/// Configuration schema response +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ConfigResponse { + /// Challenge ID + pub challenge_id: String, + /// Challenge name + pub name: String, + /// Version + pub version: String, + /// Configuration schema (JSON Schema) + pub config_schema: Option, + /// Supported features + pub features: Vec, + /// Limits + pub limits: ConfigLimits, +} + +/// Configuration limits +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct ConfigLimits { + /// Maximum submission size in bytes + pub max_submission_size: Option, + /// Maximum evaluation time in seconds + pub max_evaluation_time: Option, + /// Maximum cost per evaluation + pub max_cost: Option, +} + +// ============================================================================ +// CHALLENGE CONTEXT +// ============================================================================ + +/// Context provided to route handlers, giving access to shared resources +/// +/// Route handlers receive this to access the local sled database and chain +/// state when handling custom routes. +pub struct ChallengeContext { + /// Local challenge database (sled) + pub db: Arc, + /// Challenge ID + pub challenge_id: String, + /// Current epoch + pub epoch: u64, + /// Current block height + pub block_height: u64, +} + +// ============================================================================ +// SERVER TRAIT +// ============================================================================ + +/// Trait that challenges must implement for server mode +#[async_trait::async_trait] +pub trait ServerChallenge: Send + Sync { + /// Get challenge ID + fn challenge_id(&self) -> &str; + + /// Get challenge name + fn name(&self) -> &str; + + /// Get version + fn version(&self) -> &str; + + /// Evaluate a submission + async fn evaluate( + &self, + request: EvaluationRequest, + ) -> Result; + + /// Validate submission data (quick check) + async fn validate( + &self, + request: ValidationRequest, + ) -> Result { + // Default: accept everything + Ok(ValidationResponse { + valid: true, + errors: vec![], + warnings: vec![], + }) + } + + /// Get configuration schema + fn config(&self) -> ConfigResponse { + ConfigResponse { + challenge_id: self.challenge_id().to_string(), + name: self.name().to_string(), + version: self.version().to_string(), + config_schema: None, + features: vec![], + limits: ConfigLimits::default(), + } + } + + /// Return the custom routes this challenge exposes. + /// + /// Challenges override this to declare their own API routes (e.g., + /// `/leaderboard`, `/submit`, `/stats`). The platform SDK does not + /// hardcode any challenge-specific routes. + fn routes(&self) -> Vec { + vec![] + } + + /// Handle an incoming route request. + /// + /// Called when a request matches one of the routes declared by + /// [`routes()`](Self::routes). The `ChallengeContext` provides access + /// to the local sled database and current chain state. + async fn handle_route(&self, _ctx: &ChallengeContext, _request: RouteRequest) -> RouteResponse { + RouteResponse::not_found() + } +} + +// ============================================================================ +// SERVER STATE +// ============================================================================ + +/// Server state +pub struct ServerState { + pub challenge: Arc, + pub config: ServerConfig, + pub started_at: Instant, + pub pending_count: Arc>, +} + +// ============================================================================ +// SERVER BUILDER +// ============================================================================ + +/// Builder for challenge server +pub struct ChallengeServerBuilder { + challenge: C, + config: ServerConfig, +} + +impl ChallengeServerBuilder { + /// Create new builder + pub fn new(challenge: C) -> Self { + Self { + challenge, + config: ServerConfig::default(), + } + } + + /// Set configuration + pub fn config(mut self, config: ServerConfig) -> Self { + self.config = config; + self + } + + /// Set host + pub fn host(mut self, host: impl Into) -> Self { + self.config.host = host.into(); + self + } + + /// Set port + pub fn port(mut self, port: u16) -> Self { + self.config.port = port; + self + } + + /// Load config from environment + pub fn from_env(mut self) -> Self { + self.config = ServerConfig::from_env(); + self + } + + /// Build and return server state + pub fn build(self) -> ChallengeServer { + ChallengeServer { + state: Arc::new(ServerState { + challenge: Arc::new(self.challenge), + config: self.config, + started_at: Instant::now(), + pending_count: Arc::new(RwLock::new(0)), + }), + } + } +} + +/// Challenge HTTP server +pub struct ChallengeServer { + state: Arc>, +} + +impl ChallengeServer { + /// Create new server builder + pub fn builder(challenge: C) -> ChallengeServerBuilder { + ChallengeServerBuilder::new(challenge) + } + + /// Run the server (requires axum feature) + #[cfg(feature = "http-server")] + pub async fn run(&self) -> Result<(), ChallengeError> { + use axum::{ + extract::{Json, State}, + http::StatusCode, + routing::{get, post}, + Router, + }; + + let state = Arc::clone(&self.state); + let addr: SocketAddr = format!("{}:{}", state.config.host, state.config.port) + .parse() + .map_err(|e| ChallengeError::Config(format!("Invalid address: {}", e)))?; + + // Log custom routes declared by the challenge + let custom_routes = state.challenge.routes(); + if !custom_routes.is_empty() { + info!( + "Challenge {} declares {} custom route(s)", + state.challenge.challenge_id(), + custom_routes.len() + ); + for route in &custom_routes { + debug!( + " {} {}: {}", + route.method.as_str(), + route.path, + route.description + ); + } + } + + let app = Router::new() + .route("/health", get(health_handler::)) + .route("/config", get(config_handler::)) + .route("/evaluate", post(evaluate_handler::)) + .route("/validate", post(validate_handler::)) + .fallback(custom_route_handler::) + .with_state(state); + + info!( + "Starting challenge server {} on {}", + self.state.challenge.challenge_id(), + addr + ); + + let listener = tokio::net::TcpListener::bind(addr) + .await + .map_err(|e| ChallengeError::Io(e.to_string()))?; + + axum::serve(listener, app) + .await + .map_err(|e| ChallengeError::Io(e.to_string()))?; + + Ok(()) + } + + /// Get server address + pub fn address(&self) -> String { + format!("{}:{}", self.state.config.host, self.state.config.port) + } +} + +// ============================================================================ +// HTTP HANDLERS (when http-server feature enabled) +// ============================================================================ + +#[cfg(feature = "http-server")] +async fn health_handler( + State(state): State>>, +) -> axum::Json { + let pending = *state.pending_count.read().await; + let load = pending as f64 / state.config.max_concurrent as f64; + + axum::Json(HealthResponse { + healthy: true, + load: load.min(1.0), + pending, + uptime_secs: state.started_at.elapsed().as_secs(), + version: state.challenge.version().to_string(), + challenge_id: state.challenge.challenge_id().to_string(), + }) +} + +#[cfg(feature = "http-server")] +async fn config_handler( + State(state): State>>, +) -> axum::Json { + axum::Json(state.challenge.config()) +} + +#[cfg(feature = "http-server")] +async fn evaluate_handler( + State(state): State>>, + axum::Json(request): axum::Json, +) -> (axum::http::StatusCode, axum::Json) { + let request_id = request.request_id.clone(); + let start = Instant::now(); + + // Increment pending + { + let mut count = state.pending_count.write().await; + *count += 1; + } + + let result = state.challenge.evaluate(request).await; + + // Decrement pending + { + let mut count = state.pending_count.write().await; + *count = count.saturating_sub(1); + } + + match result { + Ok(mut response) => { + response.execution_time_ms = start.elapsed().as_millis() as i64; + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(e) => { + let response = EvaluationResponse::error(&request_id, e.to_string()) + .with_time(start.elapsed().as_millis() as i64); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } +} + +#[cfg(feature = "http-server")] +async fn validate_handler( + State(state): State>>, + axum::Json(request): axum::Json, +) -> axum::Json { + match state.challenge.validate(request).await { + Ok(response) => axum::Json(response), + Err(e) => axum::Json(ValidationResponse { + valid: false, + errors: vec![e.to_string()], + warnings: vec![], + }), + } +} + +/// Catch-all handler for custom challenge routes declared via `ServerChallenge::routes()` +#[cfg(feature = "http-server")] +async fn custom_route_handler( + State(state): State>>, + method: axum::http::Method, + uri: axum::http::Uri, + axum::extract::Query(query): axum::extract::Query>, + headers: axum::http::HeaderMap, + body: Option>, +) -> (axum::http::StatusCode, axum::Json) { + let path = uri.path().to_string(); + let method_str = method.as_str().to_string(); + + let custom_routes = state.challenge.routes(); + + // Find matching route + let mut matched_params = std::collections::HashMap::new(); + let mut found = false; + for route in &custom_routes { + if let Some(params) = route.matches(&method_str, &path) { + matched_params = params; + found = true; + break; + } + } + + if !found { + return ( + axum::http::StatusCode::NOT_FOUND, + axum::Json(serde_json::json!({ + "error": "not_found", + "message": format!("No route matches {} {}", method_str, path) + })), + ); + } + + // Build headers map + let mut headers_map = std::collections::HashMap::new(); + for (key, value) in headers.iter() { + if let Ok(v) = value.to_str() { + headers_map.insert(key.as_str().to_string(), v.to_string()); + } + } + + let request = RouteRequest { + method: method_str, + path, + params: matched_params, + query, + headers: headers_map, + body: body.map(|b| b.0).unwrap_or(serde_json::Value::Null), + auth_hotkey: None, + }; + + // Build a minimal ChallengeContext (no database in fallback handler) + // In production, the ChallengeContext would be populated by the validator node + let ctx = ChallengeContext { + db: Arc::new( + ChallengeDatabase::open(std::env::temp_dir(), crate::types::ChallengeId::new()) + .unwrap_or_else(|_| { + ChallengeDatabase::open(std::env::temp_dir(), crate::types::ChallengeId::new()) + .expect("Failed to open temporary challenge database") + }), + ), + challenge_id: state.challenge.challenge_id().to_string(), + epoch: 0, + block_height: 0, + }; + + let response = state.challenge.handle_route(&ctx, request).await; + + ( + axum::http::StatusCode::from_u16(response.status) + .unwrap_or(axum::http::StatusCode::INTERNAL_SERVER_ERROR), + axum::Json(response.body), + ) +} + +// ============================================================================ +// MACROS FOR EASY IMPLEMENTATION +// ============================================================================ + +/// Macro to implement ServerChallenge for an existing Challenge +#[macro_export] +macro_rules! impl_server_challenge { + ($type:ty, evaluate: |$self:ident, $req:ident| $body:expr) => { + #[async_trait::async_trait] + impl $crate::server::ServerChallenge for $type { + fn challenge_id(&self) -> &str { + ::id(self).as_str() + } + + fn name(&self) -> &str { + ::name(self) + } + + fn version(&self) -> &str { + ::version(self) + } + + async fn evaluate( + &$self, + $req: $crate::server::EvaluationRequest, + ) -> Result<$crate::server::EvaluationResponse, $crate::error::ChallengeError> { + $body + } + } + }; +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn test_server_config_default() { + let config = ServerConfig::default(); + assert_eq!(config.host, "0.0.0.0"); + assert_eq!(config.port, 8080); + assert_eq!(config.max_concurrent, 4); + assert_eq!(config.timeout_secs, 600); + assert!(config.cors_enabled); + } + + #[test] + fn test_server_config_from_env() { + // Should use defaults when env vars not set + let config = ServerConfig::from_env(); + assert_eq!(config.host, "0.0.0.0"); + assert_eq!(config.port, 8080); + } + + #[test] + fn test_evaluation_request() { + let req = EvaluationRequest { + request_id: "req-123".to_string(), + submission_id: "sub-456".to_string(), + participant_id: "participant-789".to_string(), + data: json!({"code": "fn main() {}"}), + metadata: Some(json!({"version": "1.0"})), + epoch: 5, + deadline: Some(1234567890), + }; + + assert_eq!(req.request_id, "req-123"); + assert_eq!(req.epoch, 5); + assert!(req.metadata.is_some()); + } + + #[test] + fn test_evaluation_response_success() { + let resp = EvaluationResponse::success("req-123", 0.95, json!({"passed": 19, "total": 20})); + + assert!(resp.success); + assert_eq!(resp.request_id, "req-123"); + assert_eq!(resp.score, 0.95); + assert!(resp.error.is_none()); + } + + #[test] + fn test_evaluation_response_error() { + let resp = EvaluationResponse::error("req-456", "Timeout occurred"); + + assert!(!resp.success); + assert_eq!(resp.request_id, "req-456"); + assert_eq!(resp.score, 0.0); + assert_eq!(resp.error, Some("Timeout occurred".to_string())); + } + + #[test] + fn test_evaluation_response_with_time() { + let resp = EvaluationResponse::success("req", 0.8, json!({})).with_time(1500); + + assert_eq!(resp.execution_time_ms, 1500); + } + + #[test] + fn test_evaluation_response_with_cost() { + let resp = EvaluationResponse::success("req", 0.8, json!({})).with_cost(0.05); + + assert_eq!(resp.cost, Some(0.05)); + } + + #[test] + fn test_validation_request() { + let req = ValidationRequest { + data: json!({"input": "test"}), + }; + + assert_eq!(req.data["input"], "test"); + } + + #[test] + fn test_validation_response() { + let resp = ValidationResponse { + valid: true, + errors: vec![], + warnings: vec!["Consider updating format".to_string()], + }; + + assert!(resp.valid); + assert!(resp.errors.is_empty()); + assert_eq!(resp.warnings.len(), 1); + } + + #[test] + fn test_health_response() { + let health = HealthResponse { + healthy: true, + load: 0.5, + pending: 2, + uptime_secs: 3600, + version: "1.0.0".to_string(), + challenge_id: "test-challenge".to_string(), + }; + + assert!(health.healthy); + assert_eq!(health.load, 0.5); + assert_eq!(health.uptime_secs, 3600); + } + + #[test] + fn test_config_response() { + let config = ConfigResponse { + challenge_id: "test".to_string(), + name: "Test Challenge".to_string(), + version: "1.0.0".to_string(), + config_schema: Some(json!({"type": "object"})), + features: vec!["feature1".to_string(), "feature2".to_string()], + limits: ConfigLimits { + max_submission_size: Some(1024 * 1024), + max_evaluation_time: Some(300), + max_cost: Some(0.1), + }, + }; + + assert_eq!(config.challenge_id, "test"); + assert_eq!(config.features.len(), 2); + assert_eq!(config.limits.max_submission_size, Some(1024 * 1024)); + } + + #[test] + fn test_config_limits_default() { + let limits = ConfigLimits::default(); + assert!(limits.max_submission_size.is_none()); + assert!(limits.max_evaluation_time.is_none()); + assert!(limits.max_cost.is_none()); + } + + // Test with a mock challenge + struct MockChallenge; + + #[async_trait::async_trait] + impl ServerChallenge for MockChallenge { + fn challenge_id(&self) -> &str { + "mock-challenge" + } + + fn name(&self) -> &str { + "Mock Challenge" + } + + fn version(&self) -> &str { + "1.0.0" + } + + async fn evaluate( + &self, + request: EvaluationRequest, + ) -> Result { + Ok(EvaluationResponse::success( + &request.request_id, + 0.75, + json!({"mock": true}), + )) + } + } + + #[tokio::test] + async fn test_mock_challenge_evaluate() { + let challenge = MockChallenge; + + let req = EvaluationRequest { + request_id: "test".to_string(), + submission_id: "sub".to_string(), + participant_id: "participant".to_string(), + data: json!({}), + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + assert!(result.success); + assert_eq!(result.score, 0.75); + } + + #[tokio::test] + async fn test_mock_challenge_validate_default() { + let challenge = MockChallenge; + + let req = ValidationRequest { data: json!({}) }; + + let result = challenge.validate(req).await.unwrap(); + assert!(result.valid); // Default implementation accepts everything + } + + #[test] + fn test_mock_challenge_config_default() { + let challenge = MockChallenge; + let config = challenge.config(); + + assert_eq!(config.challenge_id, "mock-challenge"); + assert_eq!(config.name, "Mock Challenge"); + assert_eq!(config.version, "1.0.0"); + assert!(config.features.is_empty()); + } + + #[test] + fn test_server_builder_new() { + let challenge = MockChallenge; + let builder = ChallengeServerBuilder::new(challenge); + + assert_eq!(builder.config.port, 8080); // default port + } + + #[test] + fn test_server_builder_config() { + let challenge = MockChallenge; + let custom_config = ServerConfig { + host: "127.0.0.1".to_string(), + port: 9000, + max_concurrent: 10, + timeout_secs: 120, + cors_enabled: false, + }; + + let builder = ChallengeServerBuilder::new(challenge).config(custom_config); + + assert_eq!(builder.config.host, "127.0.0.1"); + assert_eq!(builder.config.port, 9000); + } + + #[test] + fn test_server_builder_host() { + let challenge = MockChallenge; + let builder = ChallengeServerBuilder::new(challenge).host("192.168.1.1"); + + assert_eq!(builder.config.host, "192.168.1.1"); + } + + #[test] + fn test_server_builder_port() { + let challenge = MockChallenge; + let builder = ChallengeServerBuilder::new(challenge).port(3000); + + assert_eq!(builder.config.port, 3000); + } + + #[test] + fn test_server_builder_build() { + let challenge = MockChallenge; + let server = ChallengeServerBuilder::new(challenge) + .host("localhost") + .port(8888) + .build(); + + assert_eq!(server.address(), "localhost:8888"); + } + + #[test] + fn test_challenge_server_builder() { + let challenge = MockChallenge; + let builder = ChallengeServer::builder(challenge); + + assert_eq!(builder.config.port, 8080); + } + + #[test] + fn test_server_address() { + let challenge = MockChallenge; + let server = ChallengeServer::builder(challenge) + .host("0.0.0.0") + .port(8080) + .build(); + + assert_eq!(server.address(), "0.0.0.0:8080"); + } + + #[test] + fn test_server_builder_from_env() { + // Test from_env method (will use defaults when env vars not set) + let challenge = MockChallenge; + let builder = ChallengeServerBuilder::new(challenge).from_env(); + + // Should use default values from ServerConfig::from_env() + assert_eq!(builder.config.host, "0.0.0.0"); + assert_eq!(builder.config.port, 8080); + } + + #[test] + fn test_server_config_from_env_with_env_vars() { + // Note: In real usage, this would read from environment variables + // For this test, we just verify the function exists and returns defaults + let config = ServerConfig::from_env(); + + // Should return valid config (with defaults when env vars not set) + assert!(!config.host.is_empty()); + assert!(config.port > 0); + assert!(config.max_concurrent > 0); + } +} + +/// Comprehensive async tests for ServerChallenge trait implementations +#[cfg(test)] +mod async_tests { + use super::*; + use serde_json::json; + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; + use std::sync::Arc; + use std::time::Duration; + + /// Configurable test challenge for comprehensive testing + struct TestChallenge { + should_fail_evaluate: Arc, + should_fail_validate: Arc, + delay_ms: Arc, + custom_score: f64, + custom_id: String, + custom_name: String, + custom_version: String, + validation_errors: Vec, + validation_warnings: Vec, + custom_config: Option, + } + + impl Default for TestChallenge { + fn default() -> Self { + Self { + should_fail_evaluate: Arc::new(AtomicBool::new(false)), + should_fail_validate: Arc::new(AtomicBool::new(false)), + delay_ms: Arc::new(AtomicU64::new(0)), + custom_score: 0.85, + custom_id: "test-challenge".to_string(), + custom_name: "Test Challenge".to_string(), + custom_version: "1.0.0".to_string(), + validation_errors: vec![], + validation_warnings: vec![], + custom_config: None, + } + } + } + + impl TestChallenge { + fn with_failure() -> Self { + Self { + should_fail_evaluate: Arc::new(AtomicBool::new(true)), + ..Default::default() + } + } + + fn with_validation_failure() -> Self { + Self { + should_fail_validate: Arc::new(AtomicBool::new(true)), + ..Default::default() + } + } + + fn with_delay(delay_ms: u64) -> Self { + Self { + delay_ms: Arc::new(AtomicU64::new(delay_ms)), + ..Default::default() + } + } + + fn with_score(score: f64) -> Self { + Self { + custom_score: score, + ..Default::default() + } + } + + fn with_validation_errors(errors: Vec) -> Self { + Self { + validation_errors: errors, + ..Default::default() + } + } + + fn with_validation_warnings(warnings: Vec) -> Self { + Self { + validation_warnings: warnings, + ..Default::default() + } + } + + fn with_custom_config(config: ConfigResponse) -> Self { + Self { + custom_config: Some(config), + ..Default::default() + } + } + + fn with_identity(id: &str, name: &str, version: &str) -> Self { + Self { + custom_id: id.to_string(), + custom_name: name.to_string(), + custom_version: version.to_string(), + ..Default::default() + } + } + } + + #[async_trait::async_trait] + impl ServerChallenge for TestChallenge { + fn challenge_id(&self) -> &str { + &self.custom_id + } + + fn name(&self) -> &str { + &self.custom_name + } + + fn version(&self) -> &str { + &self.custom_version + } + + async fn evaluate( + &self, + request: EvaluationRequest, + ) -> Result { + // Simulate delay if configured + let delay = self.delay_ms.load(Ordering::SeqCst); + if delay > 0 { + tokio::time::sleep(Duration::from_millis(delay)).await; + } + + // Check if should fail + if self.should_fail_evaluate.load(Ordering::SeqCst) { + return Err(ChallengeError::Evaluation("Configured to fail".to_string())); + } + + // Return success with configured score + Ok(EvaluationResponse::success( + &request.request_id, + self.custom_score, + json!({ + "test": true, + "submission_id": request.submission_id, + "participant_id": request.participant_id, + "metadata_present": request.metadata.is_some(), + }), + )) + } + + async fn validate( + &self, + _request: ValidationRequest, + ) -> Result { + if self.should_fail_validate.load(Ordering::SeqCst) { + return Err(ChallengeError::Validation( + "Validation process failed".to_string(), + )); + } + + let has_errors = !self.validation_errors.is_empty(); + Ok(ValidationResponse { + valid: !has_errors, + errors: self.validation_errors.clone(), + warnings: self.validation_warnings.clone(), + }) + } + + fn config(&self) -> ConfigResponse { + if let Some(ref config) = self.custom_config { + return config.clone(); + } + ConfigResponse { + challenge_id: self.challenge_id().to_string(), + name: self.name().to_string(), + version: self.version().to_string(), + config_schema: None, + features: vec![], + limits: ConfigLimits::default(), + } + } + } + + fn create_test_request(request_id: &str) -> EvaluationRequest { + EvaluationRequest { + request_id: request_id.to_string(), + submission_id: "sub-001".to_string(), + participant_id: "participant-001".to_string(), + data: json!({"code": "fn main() {}"}), + metadata: None, + epoch: 1, + deadline: None, + } + } + + fn create_test_request_with_metadata(request_id: &str) -> EvaluationRequest { + EvaluationRequest { + request_id: request_id.to_string(), + submission_id: "sub-002".to_string(), + participant_id: "participant-002".to_string(), + data: json!({"code": "fn main() { println!(\"Hello\"); }"}), + metadata: Some(json!({ + "version": "2.0", + "language": "rust", + "test_mode": true + })), + epoch: 5, + deadline: Some(1700000000), + } + } + + // ========================================================================= + // Evaluate Endpoint Tests + // ========================================================================= + + #[tokio::test] + async fn test_evaluate_success_returns_valid_score() { + let challenge = TestChallenge::with_score(0.95); + let req = create_test_request("req-001"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 0.95); + assert!(result.error.is_none()); + } + + #[tokio::test] + async fn test_evaluate_score_bounds_zero() { + let challenge = TestChallenge::with_score(0.0); + let req = create_test_request("req-zero"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 0.0); + assert!( + result.score >= 0.0 && result.score <= 1.0, + "Score should be within valid range" + ); + } + + #[tokio::test] + async fn test_evaluate_score_bounds_one() { + let challenge = TestChallenge::with_score(1.0); + let req = create_test_request("req-one"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 1.0); + assert!( + result.score >= 0.0 && result.score <= 1.0, + "Score should be within valid range" + ); + } + + #[tokio::test] + async fn test_evaluate_error_returns_failure_response() { + let challenge = TestChallenge::with_failure(); + let req = create_test_request("req-fail"); + + let result = challenge.evaluate(req).await; + + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(matches!(err, ChallengeError::Evaluation(_))); + } + + #[tokio::test] + async fn test_evaluate_records_execution_time() { + let challenge = TestChallenge::with_delay(50); + let req = create_test_request("req-time"); + + let start = std::time::Instant::now(); + let result = challenge.evaluate(req).await.unwrap(); + let elapsed = start.elapsed(); + + // Verify the delay was applied + assert!( + elapsed.as_millis() >= 50, + "Should have delayed at least 50ms" + ); + // Note: execution_time_ms is set to 0 by default in the trait method + // The actual time tracking happens in the HTTP handler + assert!(result.success); + } + + #[tokio::test] + async fn test_evaluate_with_metadata_handling() { + let challenge = TestChallenge::default(); + let req = create_test_request_with_metadata("req-meta"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + // Verify metadata was passed through + assert_eq!(result.results["metadata_present"], true); + } + + #[tokio::test] + async fn test_evaluate_without_metadata() { + let challenge = TestChallenge::default(); + let req = create_test_request("req-no-meta"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.results["metadata_present"], false); + } + + #[tokio::test] + async fn test_evaluate_preserves_request_id() { + let challenge = TestChallenge::default(); + let req = create_test_request("unique-request-id-12345"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert_eq!(result.request_id, "unique-request-id-12345"); + } + + #[tokio::test] + async fn test_evaluate_preserves_submission_info() { + let challenge = TestChallenge::default(); + let req = create_test_request("req-info"); + + let result = challenge.evaluate(req).await.unwrap(); + + assert_eq!(result.results["submission_id"], "sub-001"); + assert_eq!(result.results["participant_id"], "participant-001"); + } + + #[tokio::test] + async fn test_evaluate_concurrent_requests() { + let challenge = Arc::new(TestChallenge::with_delay(10)); + + let handles: Vec<_> = (0..5) + .map(|i| { + let c = Arc::clone(&challenge); + tokio::spawn(async move { + let req = create_test_request(&format!("concurrent-{}", i)); + c.evaluate(req).await + }) + }) + .collect(); + + for handle in handles { + let result = handle.await.unwrap().unwrap(); + assert!(result.success); + } + } + + // ========================================================================= + // Health Endpoint Tests + // ========================================================================= + + #[test] + fn test_health_response_fields() { + let health = HealthResponse { + healthy: true, + load: 0.5, + pending: 2, + uptime_secs: 3600, + version: "1.0.0".to_string(), + challenge_id: "test".to_string(), + }; + + assert!(health.healthy); + assert!(health.load >= 0.0 && health.load <= 1.0); + assert_eq!(health.pending, 2); + assert_eq!(health.uptime_secs, 3600); + assert_eq!(health.version, "1.0.0"); + assert_eq!(health.challenge_id, "test"); + } + + #[test] + fn test_health_response_unhealthy() { + let health = HealthResponse { + healthy: false, + load: 1.0, + pending: 100, + uptime_secs: 0, + version: "1.0.0".to_string(), + challenge_id: "test".to_string(), + }; + + assert!(!health.healthy); + assert_eq!(health.load, 1.0); + } + + #[test] + fn test_health_response_serialization() { + let health = HealthResponse { + healthy: true, + load: 0.25, + pending: 1, + uptime_secs: 7200, + version: "2.0.0".to_string(), + challenge_id: "bench-challenge".to_string(), + }; + + let json = serde_json::to_string(&health).unwrap(); + let deserialized: HealthResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.healthy, health.healthy); + assert_eq!(deserialized.load, health.load); + assert_eq!(deserialized.pending, health.pending); + assert_eq!(deserialized.uptime_secs, health.uptime_secs); + assert_eq!(deserialized.version, health.version); + assert_eq!(deserialized.challenge_id, health.challenge_id); + } + + #[test] + fn test_health_response_load_boundary_zero() { + let health = HealthResponse { + healthy: true, + load: 0.0, + pending: 0, + uptime_secs: 1, + version: "1.0.0".to_string(), + challenge_id: "test".to_string(), + }; + + assert_eq!(health.load, 0.0); + } + + #[test] + fn test_health_response_load_boundary_one() { + let health = HealthResponse { + healthy: true, + load: 1.0, + pending: 4, + uptime_secs: 1, + version: "1.0.0".to_string(), + challenge_id: "test".to_string(), + }; + + assert_eq!(health.load, 1.0); + } + + // ========================================================================= + // Config Endpoint Tests + // ========================================================================= + + #[test] + fn test_config_response_limits_with_values() { + let limits = ConfigLimits { + max_submission_size: Some(10 * 1024 * 1024), // 10MB + max_evaluation_time: Some(3600), // 1 hour + max_cost: Some(1.0), + }; + + assert_eq!(limits.max_submission_size, Some(10 * 1024 * 1024)); + assert_eq!(limits.max_evaluation_time, Some(3600)); + assert_eq!(limits.max_cost, Some(1.0)); + } + + #[test] + fn test_config_response_limits_none() { + let limits = ConfigLimits::default(); + + assert!(limits.max_submission_size.is_none()); + assert!(limits.max_evaluation_time.is_none()); + assert!(limits.max_cost.is_none()); + } + + #[test] + fn test_config_response_features_empty() { + let config = ConfigResponse { + challenge_id: "test".to_string(), + name: "Test".to_string(), + version: "1.0.0".to_string(), + config_schema: None, + features: vec![], + limits: ConfigLimits::default(), + }; + + assert!(config.features.is_empty()); + } + + #[test] + fn test_config_response_features_multiple() { + let config = ConfigResponse { + challenge_id: "test".to_string(), + name: "Test".to_string(), + version: "1.0.0".to_string(), + config_schema: None, + features: vec![ + "streaming".to_string(), + "batch".to_string(), + "async_eval".to_string(), + ], + limits: ConfigLimits::default(), + }; + + assert_eq!(config.features.len(), 3); + assert!(config.features.contains(&"streaming".to_string())); + assert!(config.features.contains(&"batch".to_string())); + assert!(config.features.contains(&"async_eval".to_string())); + } + + #[test] + fn test_config_response_with_schema() { + let schema = json!({ + "type": "object", + "properties": { + "timeout": {"type": "integer"}, + "language": {"type": "string"} + } + }); + + let config = ConfigResponse { + challenge_id: "test".to_string(), + name: "Test".to_string(), + version: "1.0.0".to_string(), + config_schema: Some(schema.clone()), + features: vec![], + limits: ConfigLimits::default(), + }; + + assert!(config.config_schema.is_some()); + assert_eq!(config.config_schema.unwrap()["type"], "object"); + } + + #[test] + fn test_config_response_serialization() { + let config = ConfigResponse { + challenge_id: "test-id".to_string(), + name: "Test Challenge".to_string(), + version: "2.0.0".to_string(), + config_schema: Some(json!({"type": "object"})), + features: vec!["feature1".to_string()], + limits: ConfigLimits { + max_submission_size: Some(1024), + max_evaluation_time: Some(60), + max_cost: Some(0.5), + }, + }; + + let json = serde_json::to_string(&config).unwrap(); + let deserialized: ConfigResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.challenge_id, config.challenge_id); + assert_eq!(deserialized.name, config.name); + assert_eq!(deserialized.version, config.version); + assert_eq!(deserialized.features.len(), config.features.len()); + } + + #[test] + fn test_challenge_default_config() { + let challenge = TestChallenge::default(); + let config = challenge.config(); + + assert_eq!(config.challenge_id, "test-challenge"); + assert_eq!(config.name, "Test Challenge"); + assert_eq!(config.version, "1.0.0"); + assert!(config.features.is_empty()); + } + + #[test] + fn test_challenge_custom_config() { + let custom_config = ConfigResponse { + challenge_id: "custom".to_string(), + name: "Custom Challenge".to_string(), + version: "3.0.0".to_string(), + config_schema: Some(json!({"custom": true})), + features: vec!["custom_feature".to_string()], + limits: ConfigLimits { + max_submission_size: Some(5000), + max_evaluation_time: Some(120), + max_cost: Some(2.0), + }, + }; + + let challenge = TestChallenge::with_custom_config(custom_config); + let config = challenge.config(); + + assert_eq!(config.challenge_id, "custom"); + assert_eq!(config.features, vec!["custom_feature"]); + assert_eq!(config.limits.max_submission_size, Some(5000)); + } + + // ========================================================================= + // Validate Endpoint Tests + // ========================================================================= + + #[tokio::test] + async fn test_validate_valid_data_passes() { + let challenge = TestChallenge::default(); + let req = ValidationRequest { + data: json!({"code": "valid code"}), + }; + + let result = challenge.validate(req).await.unwrap(); + + assert!(result.valid); + assert!(result.errors.is_empty()); + } + + #[tokio::test] + async fn test_validate_invalid_data_returns_errors() { + let challenge = TestChallenge::with_validation_errors(vec![ + "Missing required field 'main'".to_string(), + "Invalid syntax at line 5".to_string(), + ]); + let req = ValidationRequest { + data: json!({"bad_code": ""}), + }; + + let result = challenge.validate(req).await.unwrap(); + + assert!(!result.valid); + assert_eq!(result.errors.len(), 2); + assert!(result.errors[0].contains("Missing required field")); + assert!(result.errors[1].contains("Invalid syntax")); + } + + #[tokio::test] + async fn test_validate_returns_warnings() { + let challenge = TestChallenge::with_validation_warnings(vec![ + "Deprecated API usage detected".to_string(), + "Consider using async/await".to_string(), + ]); + let req = ValidationRequest { + data: json!({"code": "old style code"}), + }; + + let result = challenge.validate(req).await.unwrap(); + + assert!(result.valid); // Valid but with warnings + assert!(result.errors.is_empty()); + assert_eq!(result.warnings.len(), 2); + assert!(result.warnings[0].contains("Deprecated")); + } + + #[tokio::test] + async fn test_validate_errors_and_warnings() { + let challenge = TestChallenge { + validation_errors: vec!["Error 1".to_string()], + validation_warnings: vec!["Warning 1".to_string()], + ..Default::default() + }; + + let req = ValidationRequest { data: json!({}) }; + + let result = challenge.validate(req).await.unwrap(); + + assert!(!result.valid); + assert_eq!(result.errors.len(), 1); + assert_eq!(result.warnings.len(), 1); + } + + #[tokio::test] + async fn test_validate_process_failure() { + let challenge = TestChallenge::with_validation_failure(); + let req = ValidationRequest { data: json!({}) }; + + let result = challenge.validate(req).await; + + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(matches!(err, ChallengeError::Validation(_))); + } + + #[tokio::test] + async fn test_validate_with_complex_data() { + let challenge = TestChallenge::default(); + let complex_data = json!({ + "code": "fn main() { println!(\"Hello\"); }", + "config": { + "timeout": 30, + "memory_limit": 256 + }, + "tests": [ + {"name": "test1", "expected": "pass"}, + {"name": "test2", "expected": "pass"} + ] + }); + + let req = ValidationRequest { data: complex_data }; + + let result = challenge.validate(req).await.unwrap(); + assert!(result.valid); + } + + // ========================================================================= + // ChallengeServer Builder Tests + // ========================================================================= + + #[test] + fn test_server_builder_chain() { + let challenge = TestChallenge::default(); + let server = ChallengeServer::builder(challenge) + .host("127.0.0.1") + .port(9090) + .build(); + + assert_eq!(server.address(), "127.0.0.1:9090"); + } + + #[test] + fn test_server_builder_default_address() { + let challenge = TestChallenge::default(); + let server = ChallengeServer::builder(challenge).build(); + + assert_eq!(server.address(), "0.0.0.0:8080"); + } + + #[test] + fn test_server_builder_with_full_config() { + let challenge = TestChallenge::default(); + let config = ServerConfig { + host: "10.0.0.1".to_string(), + port: 3000, + max_concurrent: 8, + timeout_secs: 1200, + cors_enabled: false, + }; + + let server = ChallengeServer::builder(challenge).config(config).build(); + + assert_eq!(server.address(), "10.0.0.1:3000"); + } + + #[test] + fn test_server_builder_method_chaining() { + let challenge = TestChallenge::with_identity("chain-test", "Chained", "1.2.3"); + let server = ChallengeServer::builder(challenge) + .host("192.168.1.100") + .port(5000) + .build(); + + assert_eq!(server.address(), "192.168.1.100:5000"); + } + + #[test] + fn test_server_builder_overwrite_values() { + let challenge = TestChallenge::default(); + let server = ChallengeServer::builder(challenge) + .host("first.host") + .port(1111) + .host("second.host") // Overwrite + .port(2222) // Overwrite + .build(); + + assert_eq!(server.address(), "second.host:2222"); + } + + #[test] + fn test_server_config_from_env_defaults() { + // Test that from_env uses sensible defaults when env vars not set + let challenge = TestChallenge::default(); + let server = ChallengeServer::builder(challenge).from_env().build(); + + // Should use default values + let addr = server.address(); + assert!(addr.contains("0.0.0.0") || addr.contains("127.0.0.1") || addr.contains("8080")); + } + + #[test] + fn test_server_builder_custom_port() { + let challenge = TestChallenge::default(); + for port in [80, 443, 3000, 8000, 8080, 9000, 65535] { + let server = ChallengeServer::builder(TestChallenge::default()) + .port(port) + .build(); + assert!(server.address().ends_with(&format!(":{}", port))); + } + } + + #[test] + fn test_server_builder_custom_hosts() { + let hosts = [ + "localhost", + "0.0.0.0", + "127.0.0.1", + "192.168.1.1", + "10.0.0.1", + ]; + for host in hosts { + let server = ChallengeServer::builder(TestChallenge::default()) + .host(host) + .build(); + assert!(server.address().starts_with(host)); + } + } + + // ========================================================================= + // EvaluationResponse Builder Pattern Tests + // ========================================================================= + + #[test] + fn test_evaluation_response_chained_builders() { + let response = EvaluationResponse::success("req-123", 0.9, json!({"test": true})) + .with_time(500) + .with_cost(0.25); + + assert!(response.success); + assert_eq!(response.request_id, "req-123"); + assert_eq!(response.score, 0.9); + assert_eq!(response.execution_time_ms, 500); + assert_eq!(response.cost, Some(0.25)); + } + + #[test] + fn test_evaluation_response_error_with_time() { + let response = EvaluationResponse::error("req-err", "Something went wrong").with_time(100); + + assert!(!response.success); + assert_eq!(response.error, Some("Something went wrong".to_string())); + assert_eq!(response.execution_time_ms, 100); + assert_eq!(response.score, 0.0); + } + + // ========================================================================= + // ServerState Tests + // ========================================================================= + + #[tokio::test] + async fn test_server_state_pending_count() { + let state = ServerState { + challenge: Arc::new(TestChallenge::default()), + config: ServerConfig::default(), + started_at: std::time::Instant::now(), + pending_count: Arc::new(RwLock::new(0)), + }; + + // Initial count is 0 + assert_eq!(*state.pending_count.read().await, 0); + + // Increment + { + let mut count = state.pending_count.write().await; + *count += 1; + } + assert_eq!(*state.pending_count.read().await, 1); + + // Decrement + { + let mut count = state.pending_count.write().await; + *count = count.saturating_sub(1); + } + assert_eq!(*state.pending_count.read().await, 0); + } + + #[test] + fn test_server_state_uptime() { + let state = ServerState { + challenge: Arc::new(TestChallenge::default()), + config: ServerConfig::default(), + started_at: std::time::Instant::now(), + pending_count: Arc::new(RwLock::new(0)), + }; + + // Uptime should be very small right after creation + let uptime = state.started_at.elapsed(); + assert!(uptime.as_secs() < 1); + } + + // ========================================================================= + // Challenge Identity Tests + // ========================================================================= + + #[test] + fn test_challenge_identity() { + let challenge = TestChallenge::with_identity("my-challenge", "My Challenge", "2.5.0"); + + assert_eq!(challenge.challenge_id(), "my-challenge"); + assert_eq!(challenge.name(), "My Challenge"); + assert_eq!(challenge.version(), "2.5.0"); + } + + #[test] + fn test_challenge_default_identity() { + let challenge = TestChallenge::default(); + + assert_eq!(challenge.challenge_id(), "test-challenge"); + assert_eq!(challenge.name(), "Test Challenge"); + assert_eq!(challenge.version(), "1.0.0"); + } + + // ========================================================================= + // Request/Response Serialization Tests + // ========================================================================= + + #[test] + fn test_evaluation_request_serialization() { + let req = create_test_request_with_metadata("serialize-test"); + + let json = serde_json::to_string(&req).unwrap(); + let deserialized: EvaluationRequest = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.request_id, req.request_id); + assert_eq!(deserialized.submission_id, req.submission_id); + assert_eq!(deserialized.participant_id, req.participant_id); + assert_eq!(deserialized.epoch, req.epoch); + assert_eq!(deserialized.deadline, req.deadline); + } + + #[test] + fn test_evaluation_response_serialization() { + let resp = EvaluationResponse::success("req-ser", 0.88, json!({"key": "value"})) + .with_time(250) + .with_cost(0.1); + + let json = serde_json::to_string(&resp).unwrap(); + let deserialized: EvaluationResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.request_id, resp.request_id); + assert_eq!(deserialized.success, resp.success); + assert_eq!(deserialized.score, resp.score); + assert_eq!(deserialized.execution_time_ms, resp.execution_time_ms); + assert_eq!(deserialized.cost, resp.cost); + } + + #[test] + fn test_validation_request_serialization() { + let req = ValidationRequest { + data: json!({"test": 123, "nested": {"value": true}}), + }; + + let json = serde_json::to_string(&req).unwrap(); + let deserialized: ValidationRequest = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.data["test"], 123); + assert_eq!(deserialized.data["nested"]["value"], true); + } + + #[test] + fn test_validation_response_serialization() { + let resp = ValidationResponse { + valid: false, + errors: vec!["Error 1".to_string(), "Error 2".to_string()], + warnings: vec!["Warning 1".to_string()], + }; + + let json = serde_json::to_string(&resp).unwrap(); + let deserialized: ValidationResponse = serde_json::from_str(&json).unwrap(); + + assert_eq!(deserialized.valid, resp.valid); + assert_eq!(deserialized.errors, resp.errors); + assert_eq!(deserialized.warnings, resp.warnings); + } +} diff --git a/crates/challenge-sdk/src/submission_types.rs b/crates/challenge-sdk/src/submission_types.rs new file mode 100644 index 000000000..1640aeb02 --- /dev/null +++ b/crates/challenge-sdk/src/submission_types.rs @@ -0,0 +1,535 @@ +//! Submission Types for Commit-Reveal Protocol +//! +//! These are the base types used by the secure submission system. +//! The actual submission management logic is implemented by each challenge. + +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +/// Encrypted submission from a miner +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EncryptedSubmission { + /// Challenge ID + pub challenge_id: String, + /// Miner's hotkey (public identifier) + pub miner_hotkey: String, + /// Miner's coldkey (for banning) + pub miner_coldkey: String, + /// Encrypted data (AES-256-GCM) + pub encrypted_data: Vec, + /// Hash of the decryption key (for verification) + pub key_hash: [u8; 32], + /// Nonce for AES-GCM (24 bytes) + pub nonce: [u8; 24], + /// Hash of the submission (encrypted_data + key_hash + nonce) + pub submission_hash: [u8; 32], + /// Hash of the ORIGINAL unencrypted content (for ownership verification) + pub content_hash: [u8; 32], + /// Signature from miner over (content_hash + miner_hotkey + epoch) + pub miner_signature: Vec, + /// Timestamp of submission + pub submitted_at: chrono::DateTime, + /// Epoch when submitted + pub epoch: u64, +} + +impl EncryptedSubmission { + /// Create a new encrypted submission + #[allow(clippy::too_many_arguments)] + pub fn new( + challenge_id: String, + miner_hotkey: String, + miner_coldkey: String, + encrypted_data: Vec, + key_hash: [u8; 32], + nonce: [u8; 24], + content_hash: [u8; 32], + miner_signature: Vec, + epoch: u64, + ) -> Self { + let submission_hash = Self::compute_hash(&encrypted_data, &key_hash, &nonce); + Self { + challenge_id, + miner_hotkey, + miner_coldkey, + encrypted_data, + key_hash, + nonce, + submission_hash, + content_hash, + miner_signature, + submitted_at: chrono::Utc::now(), + epoch, + } + } + + /// Compute submission hash + pub fn compute_hash(encrypted_data: &[u8], key_hash: &[u8; 32], nonce: &[u8; 24]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(encrypted_data); + hasher.update(key_hash); + hasher.update(nonce); + hasher.finalize().into() + } + + /// Compute content hash from original data + pub fn compute_content_hash(data: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(data); + hasher.finalize().into() + } + + /// Compute the message that must be signed by the miner + pub fn compute_signature_message( + content_hash: &[u8; 32], + miner_hotkey: &str, + epoch: u64, + ) -> Vec { + let mut msg = Vec::new(); + msg.extend_from_slice(content_hash); + msg.extend_from_slice(miner_hotkey.as_bytes()); + msg.extend_from_slice(&epoch.to_le_bytes()); + msg + } + + /// Verify submission hash is correct + pub fn verify_hash(&self) -> bool { + let computed = Self::compute_hash(&self.encrypted_data, &self.key_hash, &self.nonce); + computed == self.submission_hash + } + + /// Get submission hash as hex string + pub fn hash_hex(&self) -> String { + hex::encode(self.submission_hash) + } + + /// Get content hash as hex string + pub fn content_hash_hex(&self) -> String { + hex::encode(self.content_hash) + } +} + +/// Acknowledgment from a validator that they received a submission +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SubmissionAck { + /// Hash of the submission being acknowledged + pub submission_hash: [u8; 32], + /// Validator's hotkey + pub validator_hotkey: Hotkey, + /// Validator's stake (for weighted quorum) + pub validator_stake: u64, + /// Signature proving validator received it + pub signature: Vec, + /// Timestamp + pub timestamp: chrono::DateTime, +} + +impl SubmissionAck { + pub fn new( + submission_hash: [u8; 32], + validator_hotkey: Hotkey, + validator_stake: u64, + signature: Vec, + ) -> Self { + Self { + submission_hash, + validator_hotkey, + validator_stake, + signature, + timestamp: chrono::Utc::now(), + } + } + + pub fn submission_hash_hex(&self) -> String { + hex::encode(self.submission_hash) + } +} + +/// Decryption key reveal from miner after quorum reached +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct DecryptionKeyReveal { + /// Hash of the submission + pub submission_hash: [u8; 32], + /// The actual decryption key + pub decryption_key: Vec, + /// Miner's signature proving they own the key + pub miner_signature: Vec, + /// Timestamp + pub timestamp: chrono::DateTime, +} + +impl DecryptionKeyReveal { + pub fn new( + submission_hash: [u8; 32], + decryption_key: Vec, + miner_signature: Vec, + ) -> Self { + Self { + submission_hash, + decryption_key, + miner_signature, + timestamp: chrono::Utc::now(), + } + } + + /// Verify the key matches the hash from the original submission + pub fn verify_key_hash(&self, expected_hash: &[u8; 32]) -> bool { + let mut hasher = Sha256::new(); + hasher.update(&self.decryption_key); + let computed: [u8; 32] = hasher.finalize().into(); + &computed == expected_hash + } +} + +/// Decrypted and verified submission (after key reveal) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct VerifiedSubmission { + /// Original submission hash + pub submission_hash: [u8; 32], + /// Hash of the decrypted content (for duplicate detection) + pub content_hash: [u8; 32], + /// Challenge ID + pub challenge_id: String, + /// Miner's hotkey + pub miner_hotkey: String, + /// Miner's coldkey + pub miner_coldkey: String, + /// Decrypted data (e.g., source code) + pub data: Vec, + /// Epoch when submitted + pub epoch: u64, + /// Original submission timestamp (for priority in case of duplicate) + pub submitted_at: chrono::DateTime, + /// When the submission was verified + pub verified_at: chrono::DateTime, + /// Whether ownership was verified (content_hash matches signed hash) + pub ownership_verified: bool, +} + +/// Errors that can occur during submission processing +#[derive(Debug, Clone, thiserror::Error)] +pub enum SubmissionError { + #[error("Miner is banned")] + MinerBanned, + #[error("Invalid submission hash")] + InvalidHash, + #[error("Submission already exists")] + AlreadyExists, + #[error("Submission not found")] + NotFound, + #[error("Invalid state for operation")] + InvalidState, + #[error("Quorum not reached")] + QuorumNotReached, + #[error("Invalid decryption key")] + InvalidKey, + #[error("Decryption failed")] + DecryptionFailed, + #[error("Encryption failed")] + EncryptionFailed, + #[error("Signature verification failed")] + SignatureInvalid, + #[error("Ownership verification failed - content hash does not match signed hash")] + OwnershipVerificationFailed, + #[error("Duplicate content detected - same code already submitted")] + DuplicateContent, +} + +// ============== Crypto Helpers ============== + +/// Encrypt data using AES-256-GCM +pub fn encrypt_data( + data: &[u8], + key: &[u8; 32], + nonce: &[u8; 24], +) -> Result, SubmissionError> { + use aes_gcm::{ + aead::{Aead, KeyInit}, + Aes256Gcm, Nonce, + }; + + let cipher = Aes256Gcm::new_from_slice(key).map_err(|_| SubmissionError::EncryptionFailed)?; + + let nonce = Nonce::from_slice(&nonce[..12]); + + cipher + .encrypt(nonce, data) + .map_err(|_| SubmissionError::EncryptionFailed) +} + +/// Decrypt data using AES-256-GCM +pub fn decrypt_data( + encrypted: &[u8], + key: &[u8], + nonce: &[u8; 24], +) -> Result, SubmissionError> { + use aes_gcm::{ + aead::{Aead, KeyInit}, + Aes256Gcm, Nonce, + }; + + if key.len() != 32 { + return Err(SubmissionError::InvalidKey); + } + + let cipher = Aes256Gcm::new_from_slice(key).map_err(|_| SubmissionError::DecryptionFailed)?; + + let nonce = Nonce::from_slice(&nonce[..12]); + + cipher + .decrypt(nonce, encrypted) + .map_err(|_| SubmissionError::DecryptionFailed) +} + +/// Generate a random encryption key +pub fn generate_key() -> [u8; 32] { + use rand::RngCore; + let mut key = [0u8; 32]; + rand::thread_rng().fill_bytes(&mut key); + key +} + +/// Generate a random nonce +pub fn generate_nonce() -> [u8; 24] { + use rand::RngCore; + let mut nonce = [0u8; 24]; + rand::thread_rng().fill_bytes(&mut nonce); + nonce +} + +/// Hash a key for commit-reveal +pub fn hash_key(key: &[u8; 32]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(key); + hasher.finalize().into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_encrypt_decrypt() { + let key = generate_key(); + let nonce = generate_nonce(); + let data = b"Hello, World!"; + + let encrypted = encrypt_data(data, &key, &nonce).unwrap(); + let decrypted = decrypt_data(&encrypted, &key, &nonce).unwrap(); + + assert_eq!(data.as_slice(), decrypted.as_slice()); + } + + #[test] + fn test_key_hash() { + let key = generate_key(); + let hash = hash_key(&key); + + assert_eq!(hash, hash_key(&key)); + + let key2 = generate_key(); + assert_ne!(hash, hash_key(&key2)); + } + + #[test] + fn test_submission_hash() { + let key = generate_key(); + let nonce = generate_nonce(); + let key_hash = hash_key(&key); + let data = b"test code"; + let content_hash = EncryptedSubmission::compute_content_hash(data); + let encrypted = encrypt_data(data, &key, &nonce).unwrap(); + + let submission = EncryptedSubmission::new( + "challenge-1".to_string(), + "miner-hotkey".to_string(), + "miner-coldkey".to_string(), + encrypted, + key_hash, + nonce, + content_hash, + vec![], + 1, + ); + + assert!(submission.verify_hash()); + assert_eq!(submission.content_hash, content_hash); + } + + #[test] + fn test_content_hash_verification() { + let data = b"my agent source code"; + let content_hash = EncryptedSubmission::compute_content_hash(data); + + let content_hash2 = EncryptedSubmission::compute_content_hash(data); + assert_eq!(content_hash, content_hash2); + + let different_data = b"different code"; + let different_hash = EncryptedSubmission::compute_content_hash(different_data); + assert_ne!(content_hash, different_hash); + } + + #[test] + fn test_compute_signature_message() { + let content_hash: [u8; 32] = [1; 32]; + let hotkey = "test_hotkey"; + let epoch = 42u64; + + let msg = EncryptedSubmission::compute_signature_message(&content_hash, hotkey, epoch); + + // Should contain all components + assert!(msg.len() > 32); // at least content_hash + something + assert!(msg.starts_with(&content_hash)); + + // Should be deterministic + let msg2 = EncryptedSubmission::compute_signature_message(&content_hash, hotkey, epoch); + assert_eq!(msg, msg2); + + // Different inputs should produce different messages + let msg3 = + EncryptedSubmission::compute_signature_message(&content_hash, "other_hotkey", epoch); + assert_ne!(msg, msg3); + } + + #[test] + fn test_hash_hex() { + let key = generate_key(); + let nonce = generate_nonce(); + let key_hash = hash_key(&key); + let data = b"test"; + let content_hash = EncryptedSubmission::compute_content_hash(data); + let encrypted = encrypt_data(data, &key, &nonce).unwrap(); + + let submission = EncryptedSubmission::new( + "challenge-1".to_string(), + "miner".to_string(), + "coldkey".to_string(), + encrypted, + key_hash, + nonce, + content_hash, + vec![], + 1, + ); + + let hex = submission.hash_hex(); + assert_eq!(hex.len(), 64); // 32 bytes = 64 hex chars + assert!(hex.chars().all(|c| c.is_ascii_hexdigit())); + } + + #[test] + fn test_content_hash_hex() { + let key = generate_key(); + let nonce = generate_nonce(); + let key_hash = hash_key(&key); + let data = b"test"; + let content_hash = EncryptedSubmission::compute_content_hash(data); + let encrypted = encrypt_data(data, &key, &nonce).unwrap(); + + let submission = EncryptedSubmission::new( + "challenge-1".to_string(), + "miner".to_string(), + "coldkey".to_string(), + encrypted, + key_hash, + nonce, + content_hash, + vec![], + 1, + ); + + let hex = submission.content_hash_hex(); + assert_eq!(hex.len(), 64); + assert!(hex.chars().all(|c| c.is_ascii_hexdigit())); + } + + #[test] + fn test_submission_ack_new() { + use platform_core::Hotkey; + + let hash: [u8; 32] = [2; 32]; + let hotkey = Hotkey::from_bytes(&[3; 32]).unwrap(); + let stake = 1000u64; + let signature = vec![4, 5, 6]; + + let ack = SubmissionAck::new(hash, hotkey.clone(), stake, signature.clone()); + + assert_eq!(ack.submission_hash, hash); + assert_eq!(ack.validator_hotkey, hotkey); + assert_eq!(ack.validator_stake, stake); + assert_eq!(ack.signature, signature); + } + + #[test] + fn test_submission_ack_hash_hex() { + use platform_core::Hotkey; + + let hash: [u8; 32] = [7; 32]; + let hotkey = Hotkey::from_bytes(&[8; 32]).unwrap(); + + let ack = SubmissionAck::new(hash, hotkey, 500, vec![]); + let hex = ack.submission_hash_hex(); + + assert_eq!(hex.len(), 64); + assert_eq!( + hex, + "0707070707070707070707070707070707070707070707070707070707070707" + ); + } + + #[test] + fn test_decryption_key_reveal_new() { + let hash: [u8; 32] = [9; 32]; + let key = vec![10, 11, 12]; + let signature = vec![13, 14, 15]; + + let reveal = DecryptionKeyReveal::new(hash, key.clone(), signature.clone()); + + assert_eq!(reveal.submission_hash, hash); + assert_eq!(reveal.decryption_key, key); + assert_eq!(reveal.miner_signature, signature); + } + + #[test] + fn test_decryption_key_reveal_verify() { + let key = generate_key(); + let key_hash = hash_key(&key); + + let reveal = DecryptionKeyReveal::new([0; 32], key.to_vec(), vec![]); + + // Should verify against correct hash + assert!(reveal.verify_key_hash(&key_hash)); + + // Should not verify against wrong hash + let wrong_hash: [u8; 32] = [255; 32]; + assert!(!reveal.verify_key_hash(&wrong_hash)); + } + + #[test] + fn test_decrypt_invalid_key_length() { + let nonce = generate_nonce(); + let data = b"test"; + let key32 = generate_key(); + let encrypted = encrypt_data(data, &key32, &nonce).unwrap(); + + // Try to decrypt with wrong key length + let short_key = vec![1, 2, 3]; // Only 3 bytes + let result = decrypt_data(&encrypted, &short_key, &nonce); + + assert!(result.is_err()); + assert!(matches!(result.unwrap_err(), SubmissionError::InvalidKey)); + } + + #[test] + fn test_submission_error_variants() { + let err = SubmissionError::MinerBanned; + assert_eq!(err.to_string(), "Miner is banned"); + + let err = SubmissionError::QuorumNotReached; + assert_eq!(err.to_string(), "Quorum not reached"); + + let err = SubmissionError::DuplicateContent; + assert!(err.to_string().contains("Duplicate")); + } +} diff --git a/crates/challenge-sdk/src/test_challenge.rs b/crates/challenge-sdk/src/test_challenge.rs new file mode 100644 index 000000000..12115adcd --- /dev/null +++ b/crates/challenge-sdk/src/test_challenge.rs @@ -0,0 +1,262 @@ +//! Simple test challenge for integration testing +//! +//! This module provides a simple challenge implementation using the new +//! ServerChallenge API for testing purposes. + +use crate::{ + error::ChallengeError, + server::{ + EvaluationRequest, EvaluationResponse, ServerChallenge, ValidationRequest, + ValidationResponse, + }, + types::ChallengeId, +}; +use async_trait::async_trait; +use serde_json::{json, Value}; + +/// Simple test challenge that returns scores based on submission data +pub struct SimpleTestChallenge { + id: String, + name: String, + version: String, +} + +impl SimpleTestChallenge { + pub fn new(name: impl Into) -> Self { + Self { + id: "simple-test-challenge".to_string(), + name: name.into(), + version: "0.1.0".to_string(), + } + } + + pub fn with_id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } +} + +impl Default for SimpleTestChallenge { + fn default() -> Self { + Self::new("Simple Test Challenge") + } +} + +#[async_trait] +impl ServerChallenge for SimpleTestChallenge { + fn challenge_id(&self) -> &str { + &self.id + } + + fn name(&self) -> &str { + &self.name + } + + fn version(&self) -> &str { + &self.version + } + + async fn evaluate(&self, req: EvaluationRequest) -> Result { + // Simple scoring based on data content + let base_score = 0.5; + + // Add bonus based on payload + let payload_bonus = if let Some(bonus) = req.data.get("bonus").and_then(|v| v.as_f64()) { + bonus.clamp(0.0, 0.5) + } else { + 0.0 + }; + + let score = (base_score + payload_bonus).clamp(0.0, 1.0); + + Ok(EvaluationResponse::success( + &req.request_id, + score, + json!({ + "base_score": base_score, + "bonus": payload_bonus, + "participant": req.participant_id + }), + )) + } + + async fn validate(&self, req: ValidationRequest) -> Result { + // Simple validation - just check if data is not empty + let is_valid = !req.data.is_null() && req.data != json!({}); + + if is_valid { + Ok(ValidationResponse { + valid: true, + errors: vec![], + warnings: vec![], + }) + } else { + Ok(ValidationResponse { + valid: false, + errors: vec!["Empty or null data".to_string()], + warnings: vec![], + }) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn test_simple_challenge_evaluate() { + let challenge = SimpleTestChallenge::default(); + + let req = EvaluationRequest { + request_id: "test-123".to_string(), + submission_id: "sub-123".to_string(), + participant_id: "participant-1".to_string(), + data: json!({"bonus": 0.2}), + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert!(result.score >= 0.5); + assert!(result.score <= 1.0); + } + + #[tokio::test] + async fn test_simple_challenge_validate() { + let challenge = SimpleTestChallenge::default(); + + // Valid request + let req = ValidationRequest { + data: json!({"some": "data"}), + }; + + let result = challenge.validate(req).await.unwrap(); + assert!(result.valid); + + // Invalid request (empty data) + let req = ValidationRequest { data: json!({}) }; + + let result = challenge.validate(req).await.unwrap(); + assert!(!result.valid); + } + + #[test] + fn test_simple_challenge_with_id() { + let challenge = SimpleTestChallenge::new("Test").with_id("custom-id"); + + assert_eq!(challenge.challenge_id(), "custom-id"); + } + + #[test] + fn test_simple_challenge_challenge_id() { + let challenge = SimpleTestChallenge::default(); + assert_eq!(challenge.challenge_id(), "simple-test-challenge"); + } + + #[test] + fn test_simple_challenge_name() { + let challenge = SimpleTestChallenge::new("My Test Challenge"); + assert_eq!(challenge.name(), "My Test Challenge"); + } + + #[test] + fn test_simple_challenge_version() { + let challenge = SimpleTestChallenge::default(); + assert_eq!(challenge.version(), "0.1.0"); + } + + #[tokio::test] + async fn test_evaluate_with_zero_bonus() { + let challenge = SimpleTestChallenge::default(); + + let req = EvaluationRequest { + request_id: "test-456".to_string(), + submission_id: "sub-456".to_string(), + participant_id: "participant-2".to_string(), + data: json!({"bonus": 0.0}), + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 0.5); // base score only + } + + #[tokio::test] + async fn test_evaluate_with_max_bonus() { + let challenge = SimpleTestChallenge::default(); + + let req = EvaluationRequest { + request_id: "test-789".to_string(), + submission_id: "sub-789".to_string(), + participant_id: "participant-3".to_string(), + data: json!({"bonus": 0.5}), + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 1.0); // base + max bonus + } + + #[tokio::test] + async fn test_evaluate_with_excessive_bonus() { + let challenge = SimpleTestChallenge::default(); + + let req = EvaluationRequest { + request_id: "test-999".to_string(), + submission_id: "sub-999".to_string(), + participant_id: "participant-4".to_string(), + data: json!({"bonus": 1.0}), // More than max 0.5 + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 1.0); // Clamped to 1.0 + } + + #[tokio::test] + async fn test_evaluate_without_bonus() { + let challenge = SimpleTestChallenge::default(); + + let req = EvaluationRequest { + request_id: "test-000".to_string(), + submission_id: "sub-000".to_string(), + participant_id: "participant-5".to_string(), + data: json!({"other_field": "value"}), + metadata: None, + epoch: 1, + deadline: None, + }; + + let result = challenge.evaluate(req).await.unwrap(); + + assert!(result.success); + assert_eq!(result.score, 0.5); // base score only, no bonus field + } + + #[tokio::test] + async fn test_validate_with_null_data() { + let challenge = SimpleTestChallenge::default(); + + let req = ValidationRequest { data: json!(null) }; + + let result = challenge.validate(req).await.unwrap(); + assert!(!result.valid); + assert!(!result.errors.is_empty()); + } +} diff --git a/crates/challenge-sdk/src/types.rs b/crates/challenge-sdk/src/types.rs new file mode 100644 index 000000000..2344beb3c --- /dev/null +++ b/crates/challenge-sdk/src/types.rs @@ -0,0 +1,494 @@ +//! Core types for challenges + +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Unique challenge identifier +#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct ChallengeId(pub uuid::Uuid); + +impl ChallengeId { + pub fn new() -> Self { + Self(uuid::Uuid::new_v4()) + } + + pub fn from_uuid(uuid: uuid::Uuid) -> Self { + Self(uuid) + } + + #[allow(clippy::should_implement_trait)] + pub fn from_str(s: &str) -> Option { + uuid::Uuid::parse_str(s).ok().map(Self) + } +} + +impl Default for ChallengeId { + fn default() -> Self { + Self::new() + } +} + +impl std::fmt::Debug for ChallengeId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Challenge({})", &self.0.to_string()[..8]) + } +} + +impl std::fmt::Display for ChallengeId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +/// Challenge metadata +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeMetadata { + pub id: ChallengeId, + pub name: String, + pub description: String, + pub version: String, + pub owner: Hotkey, + pub emission_weight: f64, // Percentage of total emissions (0.0 - 1.0) + pub config: ChallengeConfig, + pub created_at: chrono::DateTime, + pub updated_at: chrono::DateTime, + pub is_active: bool, +} + +/// Challenge configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeConfig { + /// Mechanism ID on Bittensor (1, 2, 3... - 0 is reserved) + /// Each challenge has its own mechanism for weight setting + pub mechanism_id: u8, + /// Evaluation timeout in seconds + pub evaluation_timeout_secs: u64, + /// Maximum memory per evaluation (MB) + pub max_memory_mb: u64, + /// Minimum validators required for weight consensus + pub min_validators_for_weights: usize, + /// Weight smoothing factor (0.0 = no smoothing, 1.0 = max smoothing) + pub weight_smoothing: f64, + /// Custom parameters as JSON + pub params: String, +} + +impl Default for ChallengeConfig { + fn default() -> Self { + Self { + mechanism_id: 1, + evaluation_timeout_secs: 300, + max_memory_mb: 512, + min_validators_for_weights: 3, + weight_smoothing: 0.3, + params: "{}".to_string(), + } + } +} + +impl ChallengeConfig { + /// Create config with specific mechanism ID + pub fn with_mechanism(mechanism_id: u8) -> Self { + Self { + mechanism_id, + ..Default::default() + } + } +} + +/// Agent information +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentInfo { + pub hash: String, + pub name: Option, + pub owner: Option, + pub version: Option, + pub metadata_json: String, // Stored as JSON string for bincode compatibility + pub submitted_at: chrono::DateTime, +} + +impl AgentInfo { + pub fn new(hash: String) -> Self { + Self { + hash, + name: None, + owner: None, + version: None, + metadata_json: "{}".to_string(), + submitted_at: chrono::Utc::now(), + } + } + + /// Get metadata as JSON Value + pub fn metadata(&self) -> serde_json::Value { + serde_json::from_str(&self.metadata_json).unwrap_or(serde_json::Value::Null) + } + + /// Set metadata from JSON Value + pub fn set_metadata(&mut self, value: serde_json::Value) { + self.metadata_json = serde_json::to_string(&value).unwrap_or_default(); + } +} + +/// Evaluation job +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationJob { + pub id: uuid::Uuid, + pub challenge_id: ChallengeId, + pub agent_hash: String, + pub job_type: String, + pub payload: serde_json::Value, + pub status: JobStatus, + pub result: Option, + pub created_at: chrono::DateTime, + pub started_at: Option>, + pub completed_at: Option>, + pub validator: Option, +} + +impl EvaluationJob { + pub fn new( + challenge_id: ChallengeId, + agent_hash: String, + job_type: String, + payload: serde_json::Value, + ) -> Self { + Self { + id: uuid::Uuid::new_v4(), + challenge_id, + agent_hash, + job_type, + payload, + status: JobStatus::Pending, + result: None, + created_at: chrono::Utc::now(), + started_at: None, + completed_at: None, + validator: None, + } + } +} + +/// Job status +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum JobStatus { + Pending, + Running, + Completed, + Failed, + Timeout, + Cancelled, +} + +/// Evaluation result +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationResult { + pub job_id: uuid::Uuid, + pub agent_hash: String, + pub score: f64, + pub metrics: HashMap, + pub logs: Option, + pub execution_time_ms: u64, + pub timestamp: chrono::DateTime, +} + +impl EvaluationResult { + pub fn new(job_id: uuid::Uuid, agent_hash: String, score: f64) -> Self { + Self { + job_id, + agent_hash, + score: score.clamp(0.0, 1.0), + metrics: HashMap::new(), + logs: None, + execution_time_ms: 0, + timestamp: chrono::Utc::now(), + } + } + + pub fn with_metrics(mut self, metrics: HashMap) -> Self { + self.metrics = metrics; + self + } + + pub fn with_logs(mut self, logs: String) -> Self { + self.logs = Some(logs); + self + } + + pub fn with_execution_time(mut self, ms: u64) -> Self { + self.execution_time_ms = ms; + self + } +} + +/// Weight assignment for a miner +/// +/// The `hotkey` field is the SS58 address of the miner who should receive this weight. +/// This is looked up in the metagraph to find the corresponding UID for Bittensor submission. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightAssignment { + /// Miner hotkey (SS58 address) - used to look up UID in metagraph + pub hotkey: String, + /// Weight for this miner (0.0 - 1.0) + pub weight: f64, +} + +impl WeightAssignment { + /// Create a weight assignment for a miner hotkey + pub fn new(hotkey: String, weight: f64) -> Self { + Self { + hotkey, + weight: weight.clamp(0.0, 1.0), + } + } +} + +/// Weights submission from a validator for an epoch +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightsSubmission { + pub challenge_id: ChallengeId, + pub validator: Hotkey, + pub epoch: u64, + pub weights: Vec, + pub commitment_hash: String, // Hash for commit-reveal + pub timestamp: chrono::DateTime, + pub signature: Vec, +} + +/// Epoch information +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EpochInfo { + pub number: u64, + pub start_block: u64, + pub end_block: u64, + pub phase: EpochPhase, + pub started_at: chrono::DateTime, +} + +/// Epoch phases for commit-reveal weight scheme +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum EpochPhase { + /// Validators are evaluating and preparing weights + Evaluation, + /// Validators commit weight hashes + Commit, + /// Validators reveal actual weights + Reveal, + /// Weights are being aggregated and finalized + Finalization, +} + +/// Aggregated weights after smoothing +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AggregatedWeights { + pub challenge_id: ChallengeId, + pub epoch: u64, + pub weights: Vec, + pub validator_submissions: usize, + pub smoothing_applied: bool, + pub finalized_at: chrono::DateTime, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_challenge_config_default() { + let config = ChallengeConfig::default(); + assert_eq!(config.mechanism_id, 1); + assert_eq!(config.evaluation_timeout_secs, 300); + assert_eq!(config.max_memory_mb, 512); + } + + #[test] + fn test_challenge_config_with_mechanism() { + let config = ChallengeConfig::with_mechanism(5); + assert_eq!(config.mechanism_id, 5); + assert_eq!(config.evaluation_timeout_secs, 300); // other fields should use defaults + } + + #[test] + fn test_challenge_id_default() { + let id = ChallengeId::default(); + let id2 = ChallengeId::default(); + assert_ne!(id, id2); // Each default should create unique ID + } + + #[test] + fn test_challenge_id_debug() { + let id = ChallengeId::new(); + let debug_str = format!("{:?}", id); + assert!(debug_str.starts_with("Challenge(")); + assert!(debug_str.ends_with(")")); + // Length should be "Challenge(" + 8 chars + ")" = variable based on UUID + assert!(debug_str.len() >= 18); + } + + #[test] + fn test_challenge_id_display_fmt() { + let uuid = uuid::Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(); + let id = ChallengeId::from_uuid(uuid); + let display = format!("{}", id); + assert_eq!(display, "550e8400-e29b-41d4-a716-446655440000"); + } + + #[test] + fn test_agent_info_new() { + let agent = AgentInfo::new("hash123".to_string()); + assert_eq!(agent.hash, "hash123"); + assert!(agent.name.is_none()); + assert!(agent.owner.is_none()); + assert!(agent.version.is_none()); + assert_eq!(agent.metadata_json, "{}"); + } + + #[test] + fn test_agent_info_metadata() { + let mut agent = AgentInfo::new("hash".to_string()); + + // Default metadata should be null or empty object + let meta = agent.metadata(); + assert!(meta.is_object() || meta.is_null()); + + // Set metadata + let test_meta = serde_json::json!({"key": "value", "count": 42}); + agent.set_metadata(test_meta.clone()); + + let retrieved = agent.metadata(); + assert_eq!(retrieved, test_meta); + } + + #[test] + fn test_agent_info_set_metadata() { + let mut agent = AgentInfo::new("hash".to_string()); + + let meta = serde_json::json!({ + "author": "test", + "version": "1.0.0", + "tags": ["tag1", "tag2"] + }); + + agent.set_metadata(meta.clone()); + + // Verify it was serialized and stored + assert!(agent.metadata_json.contains("author")); + assert!(agent.metadata_json.contains("test")); + + // Verify we can get it back + let retrieved = agent.metadata(); + assert_eq!(retrieved["author"], "test"); + assert_eq!(retrieved["version"], "1.0.0"); + } + + #[test] + fn test_evaluation_job_creation() { + let id = ChallengeId::new(); + let job = EvaluationJob::new( + id, + "agent1".to_string(), + "eval".to_string(), + serde_json::json!({}), + ); + assert_eq!(job.agent_hash, "agent1"); + assert_eq!(job.job_type, "eval"); + assert_eq!(job.status, JobStatus::Pending); + } + + #[test] + fn test_job_status_variants() { + assert_ne!(JobStatus::Pending, JobStatus::Running); + assert_ne!(JobStatus::Completed, JobStatus::Failed); + } + + #[test] + fn test_evaluation_result() { + let result = EvaluationResult::new(uuid::Uuid::new_v4(), "agent".to_string(), 0.85); + assert_eq!(result.score, 0.85); + assert!(result.logs.is_none()); + } + + #[test] + fn test_evaluation_result_builders() { + let mut metrics = HashMap::new(); + metrics.insert("accuracy".to_string(), 0.95); + + let result = EvaluationResult::new(uuid::Uuid::new_v4(), "agent".to_string(), 0.9) + .with_metrics(metrics) + .with_logs("test logs".to_string()) + .with_execution_time(1000); + + assert_eq!(result.metrics.get("accuracy"), Some(&0.95)); + assert_eq!(result.logs, Some("test logs".to_string())); + assert_eq!(result.execution_time_ms, 1000); + } + + #[test] + fn test_evaluation_result_score_clamping() { + let result1 = EvaluationResult::new(uuid::Uuid::new_v4(), "a".to_string(), 1.5); + assert_eq!(result1.score, 1.0); + + let result2 = EvaluationResult::new(uuid::Uuid::new_v4(), "a".to_string(), -0.5); + assert_eq!(result2.score, 0.0); + } + + #[test] + fn test_weight_assignment() { + let wa = WeightAssignment::new( + "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_string(), + 0.7, + ); + assert_eq!( + wa.hotkey, + "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" + ); + assert_eq!(wa.weight, 0.7); + } + + #[test] + fn test_weight_assignment_clamping() { + let wa1 = WeightAssignment::new("hotkey1".to_string(), 2.0); + assert_eq!(wa1.weight, 1.0); + + let wa2 = WeightAssignment::new("hotkey2".to_string(), -1.0); + assert_eq!(wa2.weight, 0.0); + } + + #[test] + fn test_epoch_phase_variants() { + assert_ne!(EpochPhase::Evaluation, EpochPhase::Commit); + assert_ne!(EpochPhase::Reveal, EpochPhase::Finalization); + } + + #[test] + fn test_challenge_id_new() { + let id1 = ChallengeId::new(); + let id2 = ChallengeId::new(); + assert_ne!(id1, id2); + } + + #[test] + fn test_challenge_id_from_uuid() { + let uuid = uuid::Uuid::new_v4(); + let id1 = ChallengeId::from_uuid(uuid); + let id2 = ChallengeId::from_uuid(uuid); + assert_eq!(id1, id2); + } + + #[test] + fn test_challenge_id_from_str() { + let valid = ChallengeId::from_str("550e8400-e29b-41d4-a716-446655440000"); + assert!(valid.is_some()); + + let invalid = ChallengeId::from_str("not-a-uuid"); + assert!(invalid.is_none()); + } + + #[test] + fn test_challenge_id_display() { + let id = ChallengeId::new(); + let display = format!("{}", id); + assert!(!display.is_empty()); + } +} diff --git a/crates/challenge-sdk/src/weight_types.rs b/crates/challenge-sdk/src/weight_types.rs new file mode 100644 index 000000000..e798b8705 --- /dev/null +++ b/crates/challenge-sdk/src/weight_types.rs @@ -0,0 +1,172 @@ +//! Weight Calculation Types +//! +//! Base types for weight calculation. The actual calculation logic +//! is implemented by each challenge according to their specific rules. + +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; + +/// Evaluation result from a single validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorEvaluation { + /// Validator's hotkey + pub validator_hotkey: Hotkey, + /// Validator's stake in RAO + pub validator_stake: u64, + /// Submission hash being evaluated + pub submission_hash: String, + /// Content hash (for duplicate detection) + pub content_hash: String, + /// Miner's hotkey + pub miner_hotkey: String, + /// Miner's coldkey + pub miner_coldkey: String, + /// Score (0.0 - 1.0) + pub score: f64, + /// Tasks passed / total tasks + pub tasks_passed: u32, + pub tasks_total: u32, + /// When the agent was originally submitted (for priority in case of similar scores) + pub submitted_at: chrono::DateTime, + /// Timestamp of this evaluation + pub timestamp: chrono::DateTime, + /// Epoch + pub epoch: u64, +} + +/// Aggregated score for a submission across all validators +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AggregatedScore { + /// Submission hash + pub submission_hash: String, + /// Content hash (for duplicate detection) + pub content_hash: String, + /// Miner's hotkey + pub miner_hotkey: String, + /// Miner's coldkey + pub miner_coldkey: String, + /// Weighted average score + pub weighted_score: f64, + /// Number of validators who evaluated + pub validator_count: u32, + /// Total stake that evaluated + pub total_stake: u64, + /// Individual evaluations + pub evaluations: Vec, + /// Outlier validators (excluded from calculation) + pub outliers: Vec, + /// Consensus confidence (0.0 - 1.0) + pub confidence: f64, + /// Original submission timestamp (for priority in ties) + pub submitted_at: chrono::DateTime, +} + +/// Weight assignment for a miner +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MinerWeight { + /// Miner's hotkey + pub miner_hotkey: String, + /// Miner's coldkey + pub miner_coldkey: String, + /// Submission hash + pub submission_hash: String, + /// Final weight (0.0 - 1.0, normalized) + pub weight: f64, + /// Raw weighted score before normalization + pub raw_score: f64, + /// Rank (1 = best) + pub rank: u32, +} + +/// Result of weight calculation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightCalculationResult { + /// Epoch + pub epoch: u64, + /// Challenge ID + pub challenge_id: String, + /// Calculated weights + pub weights: Vec, + /// Current best agent + pub best_agent: Option, + /// Previous best agent (from last epoch) + pub previous_best: Option, + /// Whether a new best was found + pub new_best_found: bool, + /// Statistics + pub stats: CalculationStats, +} + +/// Best agent tracking +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BestAgent { + pub submission_hash: String, + pub miner_hotkey: String, + pub score: f64, + pub epoch: u64, + pub timestamp: chrono::DateTime, +} + +/// Statistics from calculation +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +pub struct CalculationStats { + pub total_submissions: u32, + pub valid_submissions: u32, + pub excluded_banned: u32, + pub excluded_low_confidence: u32, + pub outlier_validators: u32, + pub total_evaluations: u32, +} + +/// Configuration for weight calculation (challenge can customize) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightConfig { + /// Minimum validators required to consider a submission + pub min_validators: u32, + /// Minimum stake percentage required (0.0 - 1.0) + pub min_stake_percentage: f64, + /// Z-score threshold for outlier detection + pub outlier_zscore_threshold: f64, + /// Maximum score variance allowed before flagging + pub max_variance_threshold: f64, + /// Improvement threshold for new best agent (e.g., 0.02 = 2%) + pub improvement_threshold: f64, + /// Minimum score to be considered for weights + pub min_score_threshold: f64, +} + +impl Default for WeightConfig { + fn default() -> Self { + Self { + min_validators: 3, + min_stake_percentage: 0.3, + outlier_zscore_threshold: 2.5, + max_variance_threshold: 0.15, + improvement_threshold: 0.02, + min_score_threshold: 0.01, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_weight_config_default() { + let config = WeightConfig::default(); + assert_eq!(config.min_validators, 3); + assert_eq!(config.min_stake_percentage, 0.3); + assert_eq!(config.outlier_zscore_threshold, 2.5); + assert_eq!(config.max_variance_threshold, 0.15); + assert_eq!(config.improvement_threshold, 0.02); + assert_eq!(config.min_score_threshold, 0.01); + } + + #[test] + fn test_weight_config_clone() { + let config = WeightConfig::default(); + let cloned = config.clone(); + assert_eq!(config.min_validators, cloned.min_validators); + } +} diff --git a/crates/challenge-sdk/src/weights.rs b/crates/challenge-sdk/src/weights.rs new file mode 100644 index 000000000..0551b68cf --- /dev/null +++ b/crates/challenge-sdk/src/weights.rs @@ -0,0 +1,216 @@ +//! Weight utilities for challenges +//! +//! Provides functions for: +//! - Normalizing weights to sum to 1.0 +//! - Commit-reveal scheme for weight submission +//! +//! Note: Weight calculation is done by the challenge itself using the shared +//! chain DB. All validators read from the same DB and will get the same result. +//! The challenge uses stake-weighted scoring when calculating final weights. + +use crate::WeightAssignment; +use sha2::{Digest, Sha256}; + +/// Create a commitment hash for weight reveal verification +/// +/// Uses SHA256 hash of sorted weights and secret for commit-reveal scheme. +pub fn create_commitment(weights: &[WeightAssignment], secret: &[u8]) -> String { + let mut hasher = Sha256::new(); + + // Hash weights in deterministic order (by hotkey) + let mut sorted_weights = weights.to_vec(); + sorted_weights.sort_by(|a, b| a.hotkey.cmp(&b.hotkey)); + + for w in &sorted_weights { + hasher.update(w.hotkey.as_bytes()); + hasher.update(w.weight.to_le_bytes()); + } + + // Add secret for privacy + hasher.update(secret); + + hex::encode(hasher.finalize()) +} + +/// Normalize weights to sum to 1.0 +pub fn normalize_weights(mut weights: Vec) -> Vec { + let total: f64 = weights.iter().map(|w| w.weight).sum(); + + if total > 0.0 { + for w in &mut weights { + w.weight /= total; + } + } + + weights +} + +/// Calculate weights from evaluation scores +/// +/// Converts raw scores to normalized weights. +/// The hotkey is the miner's SS58 address. +pub fn scores_to_weights(scores: &[(String, f64)]) -> Vec { + if scores.is_empty() { + return vec![]; + } + + let total: f64 = scores.iter().map(|(_, s)| s).sum(); + + if total <= 0.0 { + return vec![]; + } + + scores + .iter() + .map(|(hotkey, score)| WeightAssignment::new(hotkey.clone(), score / total)) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_normalize_weights() { + // Note: WeightAssignment::new clamps values to 0-1 + // So we use raw values that are already in range + let weights = vec![ + WeightAssignment { + hotkey: "hotkey1".to_string(), + weight: 0.4, + }, + WeightAssignment { + hotkey: "hotkey2".to_string(), + weight: 0.6, + }, + ]; + + let normalized = normalize_weights(weights); + + // Find by hotkey since order may vary + let h1 = normalized.iter().find(|w| w.hotkey == "hotkey1").unwrap(); + let h2 = normalized.iter().find(|w| w.hotkey == "hotkey2").unwrap(); + + assert!((h1.weight - 0.4).abs() < 0.001); + assert!((h2.weight - 0.6).abs() < 0.001); + } + + #[test] + fn test_scores_to_weights() { + let scores = vec![("hotkey1".to_string(), 0.8), ("hotkey2".to_string(), 0.2)]; + + let weights = scores_to_weights(&scores); + + assert_eq!(weights.len(), 2); + assert!((weights[0].weight - 0.8).abs() < 0.001); + assert!((weights[1].weight - 0.2).abs() < 0.001); + } + + #[test] + fn test_empty_scores() { + let scores: Vec<(String, f64)> = vec![]; + let weights = scores_to_weights(&scores); + assert!(weights.is_empty()); + } + + #[test] + fn test_scores_to_weights_zero_total() { + // When total score is 0 or negative, should return empty vec + let scores = vec![("hotkey1".to_string(), 0.0), ("hotkey2".to_string(), 0.0)]; + + let weights = scores_to_weights(&scores); + assert!(weights.is_empty()); + } + + #[test] + fn test_scores_to_weights_negative_total() { + // When total score is negative (shouldn't happen but test edge case) + let scores = vec![("hotkey1".to_string(), -1.0), ("hotkey2".to_string(), -2.0)]; + + let weights = scores_to_weights(&scores); + assert!(weights.is_empty()); + } + + #[test] + fn test_create_commitment() { + let weights = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.6), + WeightAssignment::new("hotkey2".to_string(), 0.4), + ]; + let secret = b"my_secret_key_123"; + + let commitment = create_commitment(&weights, secret); + + // Should be a valid hex string (64 chars for SHA256) + assert_eq!(commitment.len(), 64); + assert!(commitment.chars().all(|c| c.is_ascii_hexdigit())); + + // Same inputs should produce same commitment + let commitment2 = create_commitment(&weights, secret); + assert_eq!(commitment, commitment2); + } + + #[test] + fn test_create_commitment_different_secrets() { + let weights = vec![WeightAssignment::new("hotkey1".to_string(), 0.5)]; + + let commitment1 = create_commitment(&weights, b"secret1"); + let commitment2 = create_commitment(&weights, b"secret2"); + + // Different secrets should produce different commitments + assert_ne!(commitment1, commitment2); + } + + #[test] + fn test_create_commitment_order_independence() { + // Weights should be sorted before hashing, so order doesn't matter + let weights1 = vec![ + WeightAssignment::new("hotkey_a".to_string(), 0.5), + WeightAssignment::new("hotkey_b".to_string(), 0.5), + ]; + + let weights2 = vec![ + WeightAssignment::new("hotkey_b".to_string(), 0.5), + WeightAssignment::new("hotkey_a".to_string(), 0.5), + ]; + + let commitment1 = create_commitment(&weights1, b"secret"); + let commitment2 = create_commitment(&weights2, b"secret"); + + assert_eq!(commitment1, commitment2); + } + + #[test] + fn test_normalize_weights_zero_total() { + // When weights sum to 0, should return them unchanged + let weights = vec![ + WeightAssignment { + hotkey: "hotkey1".to_string(), + weight: 0.0, + }, + WeightAssignment { + hotkey: "hotkey2".to_string(), + weight: 0.0, + }, + ]; + + let normalized = normalize_weights(weights.clone()); + + assert_eq!(normalized.len(), 2); + assert_eq!(normalized[0].weight, 0.0); + assert_eq!(normalized[1].weight, 0.0); + } + + #[test] + fn test_normalize_weights_single() { + let weights = vec![WeightAssignment { + hotkey: "hotkey1".to_string(), + weight: 5.0, + }]; + + let normalized = normalize_weights(weights); + + assert_eq!(normalized.len(), 1); + assert!((normalized[0].weight - 1.0).abs() < 0.001); + } +} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml new file mode 100644 index 000000000..7262e2a3d --- /dev/null +++ b/crates/core/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "platform-core" +version.workspace = true +edition.workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } +sha2 = { workspace = true } +rand = { workspace = true } +hex = { workspace = true } +uuid = { workspace = true } +chrono = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +tracing = { workspace = true } +bs58 = "0.5" +wasm-runtime-interface = { path = "../wasm-runtime-interface" } + +# Sr25519 crypto (Substrate standard) +sp-core = { version = "31.0", default-features = false, features = ["std"] } +schnorrkel = "0.11" + +[dev-dependencies] +tempfile = { workspace = true } \ No newline at end of file diff --git a/crates/core/src/challenge.rs b/crates/core/src/challenge.rs new file mode 100644 index 000000000..9153d8d94 --- /dev/null +++ b/crates/core/src/challenge.rs @@ -0,0 +1,580 @@ +//! Challenge definition and management +use crate::{hash, ChallengeId, Hotkey, Result}; +use serde::{Deserialize, Serialize}; +use wasm_runtime_interface::NetworkPolicy; + +/// Challenge definition +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Challenge { + /// Unique identifier + pub id: ChallengeId, + + /// Challenge name + pub name: String, + + /// Description + pub description: String, + + /// WASM bytecode for evaluation + pub wasm_code: Vec, + + /// Hash of the WASM code + pub code_hash: String, + + /// WASM module metadata + #[serde(default)] + pub wasm_metadata: WasmModuleMetadata, + + /// Challenge owner + pub owner: Hotkey, + + /// Configuration + pub config: ChallengeConfig, + + /// Creation timestamp + pub created_at: chrono::DateTime, + + /// Last update timestamp + pub updated_at: chrono::DateTime, + + /// Is active + pub is_active: bool, +} + +impl Challenge { + /// Create a new challenge + pub fn new( + name: String, + description: String, + wasm_code: Vec, + owner: Hotkey, + config: ChallengeConfig, + ) -> Self { + let code_hash = hex::encode(hash(&wasm_code)); + let now = chrono::Utc::now(); + let wasm_metadata = WasmModuleMetadata::from_code_hash(code_hash.clone()); + + Self { + id: ChallengeId::new(), + name, + description, + wasm_code, + code_hash, + wasm_metadata, + owner, + config, + created_at: now, + updated_at: now, + is_active: true, + } + } + + /// Update the WASM code + pub fn update_code(&mut self, wasm_code: Vec) { + self.code_hash = hex::encode(hash(&wasm_code)); + self.wasm_metadata.code_hash = self.code_hash.clone(); + self.wasm_code = wasm_code; + self.updated_at = chrono::Utc::now(); + } + + /// Verify code hash + pub fn verify_code(&self) -> bool { + let computed_hash = hex::encode(hash(&self.wasm_code)); + computed_hash == self.code_hash + } +} + +/// Challenge configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct ChallengeConfig { + /// Mechanism ID on Bittensor (1, 2, 3... - 0 is reserved for default) + /// Each challenge has its own mechanism for weight setting + pub mechanism_id: u8, + + /// Timeout for evaluation in seconds + pub timeout_secs: u64, + + /// Maximum memory for WASM execution (in MB) + pub max_memory_mb: u64, + + /// Maximum CPU time (in seconds) + pub max_cpu_secs: u64, + + /// Weight in emissions + pub emission_weight: f64, + + /// Required validators for consensus + pub min_validators: usize, + + /// Custom parameters (passed to WASM) - stored as JSON string + pub params_json: String, + + /// WASM module configuration + #[serde(default)] + pub wasm: WasmConfig, +} + +impl Default for ChallengeConfig { + fn default() -> Self { + Self { + mechanism_id: 1, // Default to mechanism 1 + timeout_secs: 300, + max_memory_mb: 512, + max_cpu_secs: 60, + emission_weight: 1.0, + min_validators: 1, + params_json: "{}".to_string(), + wasm: WasmConfig::default(), + } + } +} + +impl ChallengeConfig { + /// Create config with specific mechanism ID + pub fn with_mechanism(mechanism_id: u8) -> Self { + Self { + mechanism_id, + ..Default::default() + } + } +} + +/// WASM module metadata stored alongside the challenge +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +pub struct WasmModuleMetadata { + /// Module path or URL + #[serde(default)] + pub module_path: String, + /// SHA-256 hash of the module + pub code_hash: String, + /// Version string for module + #[serde(default)] + pub version: String, + /// Entrypoint function name + #[serde(default = "default_entrypoint")] + pub entrypoint: String, + /// Network policy for the module + #[serde(default)] + pub network_policy: NetworkPolicy, + /// Resource limits for execution + #[serde(default)] + pub resource_limits: ResourceLimits, +} + +impl WasmModuleMetadata { + pub fn from_code_hash(code_hash: String) -> Self { + Self { + module_path: String::new(), + code_hash, + version: String::new(), + entrypoint: default_entrypoint(), + network_policy: NetworkPolicy::default(), + resource_limits: ResourceLimits::default(), + } + } +} + +/// Resource limits for WASM module execution +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ResourceLimits { + /// Maximum memory in bytes + pub max_memory_bytes: u64, + /// Optional fuel limit for execution + pub max_fuel: Option, + /// Maximum execution time in seconds + pub max_execution_time_secs: u64, +} + +impl Default for ResourceLimits { + fn default() -> Self { + Self { + max_memory_bytes: 268_435_456, + max_fuel: None, + max_execution_time_secs: 300, + } + } +} + +/// WASM execution configuration +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +#[serde(default)] +pub struct WasmConfig { + /// Network policy for WASM host functions + #[serde(default)] + pub network_policy: NetworkPolicy, + /// Restartable configuration identifier + #[serde(default)] + pub restart_id: String, + /// Configuration version for hot-restarts + #[serde(default)] + pub config_version: u64, +} + +/// WASM-only challenge configuration stored in chain state +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmChallengeConfig { + /// Challenge ID + pub challenge_id: ChallengeId, + /// Challenge name + pub name: String, + /// Challenge description + pub description: String, + /// Challenge owner + pub owner: Hotkey, + /// WASM module metadata + pub module: WasmModuleMetadata, + /// Challenge configuration + pub config: ChallengeConfig, + /// Whether challenge is active + pub is_active: bool, +} + +impl Default for WasmChallengeConfig { + fn default() -> Self { + Self { + challenge_id: ChallengeId::new(), + name: String::new(), + description: String::new(), + owner: Hotkey([0u8; 32]), + module: WasmModuleMetadata::from_code_hash(String::new()), + config: ChallengeConfig::default(), + is_active: false, + } + } +} + +impl From<&Challenge> for WasmChallengeConfig { + fn from(challenge: &Challenge) -> Self { + Self { + challenge_id: challenge.id, + name: challenge.name.clone(), + description: challenge.description.clone(), + owner: challenge.owner.clone(), + module: challenge.wasm_metadata.clone(), + config: challenge.config.clone(), + is_active: challenge.is_active, + } + } +} +fn default_entrypoint() -> String { + "evaluate".to_string() +} + +/// Challenge metadata (without WASM code, for listing) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeMeta { + pub id: ChallengeId, + pub name: String, + pub description: String, + pub code_hash: String, + #[serde(default)] + pub wasm_metadata: WasmModuleMetadata, + pub owner: Hotkey, + pub config: ChallengeConfig, + pub created_at: chrono::DateTime, + pub updated_at: chrono::DateTime, + pub is_active: bool, +} + +impl From<&Challenge> for ChallengeMeta { + fn from(c: &Challenge) -> Self { + Self { + id: c.id, + name: c.name.clone(), + description: c.description.clone(), + code_hash: c.code_hash.clone(), + wasm_metadata: c.wasm_metadata.clone(), + owner: c.owner.clone(), + config: c.config.clone(), + created_at: c.created_at, + updated_at: c.updated_at, + is_active: c.is_active, + } + } +} + +/// WASM function interface that challenges must implement +/// +/// The WASM module must export these functions: +/// - `evaluate(agent_ptr: i32, agent_len: i32) -> i64` - Returns score as fixed-point (0-1000000) +/// - `validate(agent_ptr: i32, agent_len: i32) -> i32` - Returns 1 if valid, 0 if not +/// - `get_name() -> i32` - Returns pointer to name string +/// - `get_version() -> i32` - Returns version number +pub trait ChallengeInterface { + fn evaluate(&self, agent_data: &[u8]) -> Result; + fn validate(&self, agent_data: &[u8]) -> Result; + fn name(&self) -> &str; + fn version(&self) -> u32; +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Keypair; + + #[test] + fn test_challenge_creation() { + let owner = Keypair::generate(); + let wasm = vec![0u8; 100]; // Dummy WASM + + let challenge = Challenge::new( + "Test Challenge".into(), + "A test challenge".into(), + wasm.clone(), + owner.hotkey(), + ChallengeConfig::default(), + ); + + assert!(challenge.verify_code()); + assert!(challenge.is_active); + } + + #[test] + fn test_code_update() { + let owner = Keypair::generate(); + let wasm1 = vec![1u8; 100]; + let wasm2 = vec![2u8; 100]; + + let mut challenge = Challenge::new( + "Test".into(), + "Test".into(), + wasm1, + owner.hotkey(), + ChallengeConfig::default(), + ); + + let hash1 = challenge.code_hash.clone(); + challenge.update_code(wasm2); + let hash2 = challenge.code_hash.clone(); + + assert_ne!(hash1, hash2); + assert!(challenge.verify_code()); + } + + #[test] + fn test_challenge_meta() { + let owner = Keypair::generate(); + let challenge = Challenge::new( + "Test".into(), + "Test".into(), + vec![0u8; 50], + owner.hotkey(), + ChallengeConfig::default(), + ); + + let meta: ChallengeMeta = (&challenge).into(); + assert_eq!(meta.name, challenge.name); + assert_eq!(meta.code_hash, challenge.code_hash); + assert_eq!(meta.wasm_metadata.code_hash, challenge.code_hash); + } + + #[test] + fn test_challenge_config_with_mechanism() { + let config = ChallengeConfig::with_mechanism(5); + assert_eq!(config.mechanism_id, 5); + assert_eq!(config.timeout_secs, 300); // Should have other defaults + assert_eq!(config.max_memory_mb, 512); + assert_eq!(config.emission_weight, 1.0); + } + + #[test] + fn test_challenge_config_default() { + let config = ChallengeConfig::default(); + assert_eq!(config.mechanism_id, 1); + assert_eq!(config.timeout_secs, 300); + assert_eq!(config.max_memory_mb, 512); + assert_eq!(config.max_cpu_secs, 60); + assert_eq!(config.emission_weight, 1.0); + assert_eq!(config.min_validators, 1); + assert_eq!(config.params_json, "{}"); + assert!(config.wasm.restart_id.is_empty()); + } + + #[test] + fn test_challenge_verify_code_tampered() { + let owner = Keypair::generate(); + let wasm = vec![0u8; 100]; + + let mut challenge = Challenge::new( + "Test".into(), + "Test".into(), + wasm, + owner.hotkey(), + ChallengeConfig::default(), + ); + + // Tamper with the code without updating the hash + challenge.wasm_code[0] = 255; + + // verify_code should return false since hash doesn't match + assert!(!challenge.verify_code()); + } + + #[test] + fn test_challenge_is_active_default() { + let owner = Keypair::generate(); + let wasm = vec![0u8; 50]; + + let challenge = Challenge::new( + "Test".into(), + "Test".into(), + wasm, + owner.hotkey(), + ChallengeConfig::default(), + ); + + assert!(challenge.is_active); + } + + #[test] + fn test_challenge_id_uniqueness() { + let owner = Keypair::generate(); + let wasm = vec![0u8; 50]; + + let challenge1 = Challenge::new( + "Test 1".into(), + "Test".into(), + wasm.clone(), + owner.hotkey(), + ChallengeConfig::default(), + ); + + let challenge2 = Challenge::new( + "Test 2".into(), + "Test".into(), + wasm, + owner.hotkey(), + ChallengeConfig::default(), + ); + + assert_ne!(challenge1.id, challenge2.id); + } + + #[test] + fn test_challenge_timestamps() { + let owner = Keypair::generate(); + let wasm = vec![0u8; 50]; + + let before = chrono::Utc::now(); + let challenge = Challenge::new( + "Test".into(), + "Test".into(), + wasm, + owner.hotkey(), + ChallengeConfig::default(), + ); + let after = chrono::Utc::now(); + + // created_at and updated_at should be within the time bounds + assert!(challenge.created_at >= before); + assert!(challenge.created_at <= after); + assert!(challenge.updated_at >= before); + assert!(challenge.updated_at <= after); + // For a new challenge, created_at equals updated_at + assert_eq!(challenge.created_at, challenge.updated_at); + } + + #[test] + fn test_challenge_update_code_changes_timestamp() { + let owner = Keypair::generate(); + let wasm1 = vec![1u8; 50]; + let wasm2 = vec![2u8; 50]; + + let mut challenge = Challenge::new( + "Test".into(), + "Test".into(), + wasm1, + owner.hotkey(), + ChallengeConfig::default(), + ); + + let original_updated_at = challenge.updated_at; + let original_created_at = challenge.created_at; + + // Small sleep to ensure timestamp changes + std::thread::sleep(std::time::Duration::from_millis(10)); + + challenge.update_code(wasm2); + + // created_at should not change + assert_eq!(challenge.created_at, original_created_at); + // updated_at should change + assert!(challenge.updated_at > original_updated_at); + } + + #[test] + fn test_challenge_meta_preserves_fields() { + let owner = Keypair::generate(); + let wasm = vec![42u8; 75]; + let config = ChallengeConfig::with_mechanism(3); + + let challenge = Challenge::new( + "Meta Test".into(), + "Description for meta".into(), + wasm, + owner.hotkey(), + config, + ); + + let meta: ChallengeMeta = (&challenge).into(); + + assert_eq!(meta.id, challenge.id); + assert_eq!(meta.name, challenge.name); + assert_eq!(meta.description, challenge.description); + assert_eq!(meta.code_hash, challenge.code_hash); + assert_eq!(meta.owner, challenge.owner); + assert_eq!(meta.config.mechanism_id, challenge.config.mechanism_id); + assert_eq!(meta.config.timeout_secs, challenge.config.timeout_secs); + assert_eq!(meta.created_at, challenge.created_at); + assert_eq!(meta.updated_at, challenge.updated_at); + assert_eq!(meta.is_active, challenge.is_active); + assert_eq!(meta.wasm_metadata.entrypoint, "evaluate"); + } + + #[test] + fn test_challenge_config_params_json() { + let config = ChallengeConfig::default(); + assert_eq!(config.params_json, "{}"); + + let config_mechanism = ChallengeConfig::with_mechanism(2); + assert_eq!(config_mechanism.params_json, "{}"); + } + + #[test] + fn test_challenge_empty_wasm() { + let owner = Keypair::generate(); + let empty_wasm: Vec = vec![]; + + let challenge = Challenge::new( + "Empty WASM".into(), + "Challenge with empty wasm".into(), + empty_wasm, + owner.hotkey(), + ChallengeConfig::default(), + ); + + // Should still create successfully and verify + assert!(challenge.verify_code()); + assert!(challenge.wasm_code.is_empty()); + assert!(!challenge.code_hash.is_empty()); // Hash should still be computed + } + + #[test] + fn test_challenge_large_wasm() { + let owner = Keypair::generate(); + let large_wasm = vec![0xABu8; 10 * 1024]; // 10KB + + let challenge = Challenge::new( + "Large WASM".into(), + "Challenge with 10KB wasm".into(), + large_wasm.clone(), + owner.hotkey(), + ChallengeConfig::default(), + ); + + assert!(challenge.verify_code()); + assert_eq!(challenge.wasm_code.len(), 10 * 1024); + assert_eq!(challenge.wasm_code, large_wasm); + } +} diff --git a/crates/core/src/checkpoint.rs b/crates/core/src/checkpoint.rs new file mode 100644 index 000000000..12e32c153 --- /dev/null +++ b/crates/core/src/checkpoint.rs @@ -0,0 +1,738 @@ +//! Checkpoint system for state persistence +//! +//! Provides mechanisms to save and restore evaluation state, enabling: +//! - Hot-reload without losing progress +//! - Crash recovery +//! - Rolling updates + +use crate::{ChallengeId, Hotkey, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use std::fs::{self, File}; +use std::io::{BufReader, BufWriter, Read, Write}; +use std::path::{Path, PathBuf}; +use std::time::SystemTime; +use tracing::{debug, info, warn}; + +/// Checkpoint version for format compatibility +pub const CHECKPOINT_VERSION: u32 = 1; + +/// Magic bytes for checkpoint file identification +const CHECKPOINT_MAGIC: &[u8; 8] = b"PLATCHKP"; + +/// Checkpoint file header +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CheckpointHeader { + /// Magic bytes (verified on load) + pub magic: [u8; 8], + /// Checkpoint format version + pub version: u32, + /// Creation timestamp (Unix millis) + pub created_at: i64, + /// Checkpoint sequence number + pub sequence: u64, + /// SHA-256 hash of the data section + pub data_hash: [u8; 32], + /// Size of the data section in bytes + pub data_size: u64, +} + +impl CheckpointHeader { + pub fn new(sequence: u64, data_hash: [u8; 32], data_size: u64) -> Self { + Self { + magic: *CHECKPOINT_MAGIC, + version: CHECKPOINT_VERSION, + created_at: chrono::Utc::now().timestamp_millis(), + sequence, + data_hash, + data_size, + } + } + + pub fn verify_magic(&self) -> bool { + self.magic == *CHECKPOINT_MAGIC + } +} + +/// State of a pending evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PendingEvaluationState { + /// Submission ID + pub submission_id: String, + /// Challenge ID + pub challenge_id: ChallengeId, + /// Miner hotkey + pub miner: Hotkey, + /// Submission hash + pub submission_hash: String, + /// Evaluation scores received (validator -> score) + pub scores: HashMap, + /// Creation timestamp + pub created_at: i64, + /// Whether finalization is in progress + pub finalizing: bool, +} + +/// Completed evaluation record +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CompletedEvaluationState { + /// Submission ID + pub submission_id: String, + /// Challenge ID + pub challenge_id: ChallengeId, + /// Final aggregated score + pub final_score: f64, + /// Epoch when completed + pub epoch: u64, + /// Completion timestamp + pub completed_at: i64, +} + +/// Weight vote state +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightVoteState { + /// Epoch for these weights + pub epoch: u64, + /// Netuid + pub netuid: u16, + /// Votes by validator + pub votes: HashMap>, + /// Whether finalized + pub finalized: bool, + /// Final weights if finalized + pub final_weights: Option>, +} + +/// Full checkpoint data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CheckpointData { + /// Current sequence number + pub sequence: u64, + /// Current epoch + pub epoch: u64, + /// Netuid + pub netuid: u16, + /// Pending evaluations + pub pending_evaluations: Vec, + /// Recent completed evaluations (last N epochs) + pub completed_evaluations: Vec, + /// Current weight votes + pub weight_votes: Option, + /// Bittensor block number at checkpoint + pub bittensor_block: u64, + /// Additional metadata + pub metadata: HashMap, +} + +impl CheckpointData { + pub fn new(sequence: u64, epoch: u64, netuid: u16) -> Self { + Self { + sequence, + epoch, + netuid, + pending_evaluations: Vec::new(), + completed_evaluations: Vec::new(), + weight_votes: None, + bittensor_block: 0, + metadata: HashMap::new(), + } + } + + /// Add pending evaluation + pub fn add_pending(&mut self, state: PendingEvaluationState) { + self.pending_evaluations.push(state); + } + + /// Add completed evaluation + pub fn add_completed(&mut self, state: CompletedEvaluationState) { + self.completed_evaluations.push(state); + } + + /// Calculate hash of checkpoint data + pub fn calculate_hash(&self) -> Result<[u8; 32]> { + let bytes = + bincode::serialize(self).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + let mut hasher = Sha256::new(); + hasher.update(&bytes); + Ok(hasher.finalize().into()) + } +} + +/// Checkpoint manager for persisting and restoring state +pub struct CheckpointManager { + /// Directory for checkpoint files + checkpoint_dir: PathBuf, + /// Maximum number of checkpoints to keep + max_checkpoints: usize, + /// Current checkpoint sequence + current_sequence: u64, +} + +impl CheckpointManager { + /// Create a new checkpoint manager + pub fn new>(checkpoint_dir: P, max_checkpoints: usize) -> Result { + let checkpoint_dir = checkpoint_dir.as_ref().to_path_buf(); + + // Create checkpoint directory if it doesn't exist + fs::create_dir_all(&checkpoint_dir).map_err(|e| { + MiniChainError::Storage(format!("Failed to create checkpoint dir: {}", e)) + })?; + + // Find the latest checkpoint sequence + let current_sequence = Self::find_latest_sequence(&checkpoint_dir)?; + + info!( + dir = %checkpoint_dir.display(), + max_checkpoints, + current_sequence, + "Checkpoint manager initialized" + ); + + Ok(Self { + checkpoint_dir, + max_checkpoints, + current_sequence, + }) + } + + /// Find the latest checkpoint sequence number + fn find_latest_sequence(dir: &Path) -> Result { + let mut max_seq = 0u64; + + if let Ok(entries) = fs::read_dir(dir) { + for entry in entries.flatten() { + let path = entry.path(); + if let Some(name) = path.file_name().and_then(|n| n.to_str()) { + if name.starts_with("checkpoint_") && name.ends_with(".bin") { + if let Some(seq_str) = name + .strip_prefix("checkpoint_") + .and_then(|s| s.strip_suffix(".bin")) + { + if let Ok(seq) = seq_str.parse::() { + max_seq = max_seq.max(seq); + } + } + } + } + } + } + + Ok(max_seq) + } + + /// Generate checkpoint filename + fn checkpoint_filename(&self, sequence: u64) -> PathBuf { + self.checkpoint_dir + .join(format!("checkpoint_{:016}.bin", sequence)) + } + + /// Create a new checkpoint + pub fn create_checkpoint(&mut self, data: &CheckpointData) -> Result { + self.current_sequence += 1; + let sequence = self.current_sequence; + let filename = self.checkpoint_filename(sequence); + + // Serialize data + let data_bytes = + bincode::serialize(data).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + // Calculate hash + let mut hasher = Sha256::new(); + hasher.update(&data_bytes); + let data_hash: [u8; 32] = hasher.finalize().into(); + + // Create header + let header = CheckpointHeader::new(sequence, data_hash, data_bytes.len() as u64); + let header_bytes = bincode::serialize(&header) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + // Write to file atomically (write to temp, then rename) + let temp_filename = filename.with_extension("tmp"); + { + let file = File::create(&temp_filename).map_err(|e| { + MiniChainError::Storage(format!("Failed to create checkpoint: {}", e)) + })?; + let mut writer = BufWriter::new(file); + + // Write header length (4 bytes) + let header_len = header_bytes.len() as u32; + writer + .write_all(&header_len.to_le_bytes()) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + // Write header + writer + .write_all(&header_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + // Write data + writer + .write_all(&data_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + writer + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + } + + // Atomic rename + fs::rename(&temp_filename, &filename).map_err(|e| { + MiniChainError::Storage(format!("Failed to finalize checkpoint: {}", e)) + })?; + + info!( + sequence, + path = %filename.display(), + size = data_bytes.len(), + "Checkpoint created" + ); + + // Cleanup old checkpoints + self.cleanup_old_checkpoints()?; + + Ok(filename) + } + + /// Load the latest checkpoint + pub fn load_latest(&self) -> Result> { + if self.current_sequence == 0 { + return Ok(None); + } + + self.load_checkpoint(self.current_sequence) + } + + /// Load a specific checkpoint + pub fn load_checkpoint( + &self, + sequence: u64, + ) -> Result> { + let filename = self.checkpoint_filename(sequence); + + if !filename.exists() { + return Ok(None); + } + + let file = File::open(&filename) + .map_err(|e| MiniChainError::Storage(format!("Failed to open checkpoint: {}", e)))?; + let mut reader = BufReader::new(file); + + // Read header length + let mut header_len_bytes = [0u8; 4]; + reader + .read_exact(&mut header_len_bytes) + .map_err(|e| MiniChainError::Storage(format!("Failed to read header length: {}", e)))?; + let header_len = u32::from_le_bytes(header_len_bytes) as usize; + + // Read header + let mut header_bytes = vec![0u8; header_len]; + reader + .read_exact(&mut header_bytes) + .map_err(|e| MiniChainError::Storage(format!("Failed to read header: {}", e)))?; + + let header: CheckpointHeader = bincode::deserialize(&header_bytes).map_err(|e| { + MiniChainError::Serialization(format!("Failed to deserialize header: {}", e)) + })?; + + // Verify magic + if !header.verify_magic() { + return Err(MiniChainError::Storage( + "Invalid checkpoint magic bytes".into(), + )); + } + + // Verify version compatibility + if header.version > CHECKPOINT_VERSION { + return Err(MiniChainError::Storage(format!( + "Checkpoint version {} is newer than supported version {}", + header.version, CHECKPOINT_VERSION + ))); + } + + // Read data + let mut data_bytes = vec![0u8; header.data_size as usize]; + reader + .read_exact(&mut data_bytes) + .map_err(|e| MiniChainError::Storage(format!("Failed to read data: {}", e)))?; + + // Verify hash + let mut hasher = Sha256::new(); + hasher.update(&data_bytes); + let actual_hash: [u8; 32] = hasher.finalize().into(); + + if actual_hash != header.data_hash { + return Err(MiniChainError::Storage( + "Checkpoint data hash mismatch".into(), + )); + } + + // Deserialize data + let data: CheckpointData = bincode::deserialize(&data_bytes).map_err(|e| { + MiniChainError::Serialization(format!("Failed to deserialize data: {}", e)) + })?; + + info!( + sequence, + epoch = data.epoch, + pending_count = data.pending_evaluations.len(), + "Checkpoint loaded" + ); + + Ok(Some((header, data))) + } + + /// List all available checkpoints + pub fn list_checkpoints(&self) -> Result> { + let mut checkpoints = Vec::new(); + + if let Ok(entries) = fs::read_dir(&self.checkpoint_dir) { + for entry in entries.flatten() { + let path = entry.path(); + if let Some(name) = path.file_name().and_then(|n| n.to_str()) { + if name.starts_with("checkpoint_") && name.ends_with(".bin") { + if let Some(seq_str) = name + .strip_prefix("checkpoint_") + .and_then(|s| s.strip_suffix(".bin")) + { + if let Ok(seq) = seq_str.parse::() { + if let Ok(meta) = entry.metadata() { + if let Ok(modified) = meta.modified() { + checkpoints.push((seq, path, modified)); + } + } + } + } + } + } + } + } + + checkpoints.sort_by_key(|(seq, _, _)| *seq); + Ok(checkpoints) + } + + /// Clean up old checkpoints + fn cleanup_old_checkpoints(&self) -> Result<()> { + let checkpoints = self.list_checkpoints()?; + + if checkpoints.len() <= self.max_checkpoints { + return Ok(()); + } + + let to_remove = checkpoints.len() - self.max_checkpoints; + for (seq, path, _) in checkpoints.into_iter().take(to_remove) { + debug!(sequence = seq, path = %path.display(), "Removing old checkpoint"); + if let Err(e) = fs::remove_file(&path) { + warn!(path = %path.display(), error = %e, "Failed to remove old checkpoint"); + } + } + + Ok(()) + } + + /// Get checkpoint directory + pub fn checkpoint_dir(&self) -> &Path { + &self.checkpoint_dir + } + + /// Get current sequence + pub fn current_sequence(&self) -> u64 { + self.current_sequence + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_checkpoint_header() { + let header = CheckpointHeader::new(1, [0u8; 32], 100); + assert!(header.verify_magic()); + assert_eq!(header.version, CHECKPOINT_VERSION); + } + + #[test] + fn test_checkpoint_header_invalid_magic() { + let mut header = CheckpointHeader::new(1, [0u8; 32], 100); + header.magic = *b"INVALID!"; + assert!(!header.verify_magic()); + } + + #[test] + fn test_checkpoint_data_hash() { + let data = CheckpointData::new(1, 0, 100); + let hash1 = data.calculate_hash().unwrap(); + + let mut data2 = data.clone(); + data2.sequence = 2; + let hash2 = data2.calculate_hash().unwrap(); + + assert_ne!(hash1, hash2); + } + + #[test] + fn test_checkpoint_data_new() { + let data = CheckpointData::new(5, 10, 200); + assert_eq!(data.sequence, 5); + assert_eq!(data.epoch, 10); + assert_eq!(data.netuid, 200); + assert!(data.pending_evaluations.is_empty()); + assert!(data.completed_evaluations.is_empty()); + assert!(data.weight_votes.is_none()); + assert_eq!(data.bittensor_block, 0); + assert!(data.metadata.is_empty()); + } + + #[test] + fn test_checkpoint_data_add_pending() { + let mut data = CheckpointData::new(1, 0, 100); + let pending = PendingEvaluationState { + submission_id: "sub1".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "abc123".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }; + data.add_pending(pending); + assert_eq!(data.pending_evaluations.len(), 1); + } + + #[test] + fn test_checkpoint_data_add_completed() { + let mut data = CheckpointData::new(1, 0, 100); + let completed = CompletedEvaluationState { + submission_id: "sub1".to_string(), + challenge_id: ChallengeId::new(), + final_score: 0.85, + epoch: 5, + completed_at: chrono::Utc::now().timestamp_millis(), + }; + data.add_completed(completed); + assert_eq!(data.completed_evaluations.len(), 1); + } + + #[test] + fn test_checkpoint_manager_roundtrip() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + + let mut data = CheckpointData::new(1, 0, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "sub1".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "abc123".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + + let path = manager.create_checkpoint(&data).unwrap(); + assert!(path.exists()); + + let (header, loaded) = manager.load_latest().unwrap().unwrap(); + assert_eq!(header.sequence, 1); + assert_eq!(loaded.sequence, data.sequence); + assert_eq!(loaded.pending_evaluations.len(), 1); + } + + #[test] + fn test_checkpoint_manager_no_checkpoints() { + let dir = tempdir().unwrap(); + let manager = CheckpointManager::new(dir.path(), 5).unwrap(); + assert!(manager.load_latest().unwrap().is_none()); + assert_eq!(manager.current_sequence(), 0); + } + + #[test] + fn test_checkpoint_cleanup() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 3).unwrap(); + + for i in 0..5 { + let data = CheckpointData::new(i, 0, 100); + manager.create_checkpoint(&data).unwrap(); + } + + let checkpoints = manager.list_checkpoints().unwrap(); + assert_eq!(checkpoints.len(), 3); + } + + #[test] + fn test_checkpoint_list() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 10).unwrap(); + + for i in 0..3 { + let data = CheckpointData::new(i, i, 100); + manager.create_checkpoint(&data).unwrap(); + } + + let checkpoints = manager.list_checkpoints().unwrap(); + assert_eq!(checkpoints.len(), 3); + + // Verify sorted by sequence + assert_eq!(checkpoints[0].0, 1); + assert_eq!(checkpoints[1].0, 2); + assert_eq!(checkpoints[2].0, 3); + } + + #[test] + fn test_checkpoint_load_specific() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 10).unwrap(); + + for i in 0..3 { + let mut data = CheckpointData::new(i, i * 10, 100); + data.metadata + .insert("test_key".to_string(), format!("value_{}", i)); + manager.create_checkpoint(&data).unwrap(); + } + + // Load specific checkpoint + let (header, data) = manager.load_checkpoint(2).unwrap().unwrap(); + assert_eq!(header.sequence, 2); + assert_eq!(data.epoch, 10); + assert_eq!(data.metadata.get("test_key"), Some(&"value_1".to_string())); + } + + #[test] + fn test_checkpoint_load_nonexistent() { + let dir = tempdir().unwrap(); + let manager = CheckpointManager::new(dir.path(), 5).unwrap(); + assert!(manager.load_checkpoint(999).unwrap().is_none()); + } + + #[test] + fn test_checkpoint_resume_sequence() { + let dir = tempdir().unwrap(); + + // First manager creates some checkpoints + { + let mut manager = CheckpointManager::new(dir.path(), 10).unwrap(); + for i in 0..3 { + let data = CheckpointData::new(i, i, 100); + manager.create_checkpoint(&data).unwrap(); + } + assert_eq!(manager.current_sequence(), 3); + } + + // New manager should resume from the latest sequence + { + let manager = CheckpointManager::new(dir.path(), 10).unwrap(); + assert_eq!(manager.current_sequence(), 3); + } + } + + #[test] + fn test_checkpoint_with_scores() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + + let mut scores = HashMap::new(); + scores.insert(Hotkey([1u8; 32]), 0.95); + scores.insert(Hotkey([2u8; 32]), 0.87); + + let mut data = CheckpointData::new(1, 5, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "sub_with_scores".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([3u8; 32]), + submission_hash: "hash123".to_string(), + scores, + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: true, + }); + + manager.create_checkpoint(&data).unwrap(); + + let (_, loaded) = manager.load_latest().unwrap().unwrap(); + let pending = &loaded.pending_evaluations[0]; + assert_eq!(pending.scores.len(), 2); + assert_eq!(pending.scores.get(&Hotkey([1u8; 32])), Some(&0.95)); + assert!(pending.finalizing); + } + + #[test] + fn test_checkpoint_with_weight_votes() { + let dir = tempdir().unwrap(); + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + + let mut votes = HashMap::new(); + votes.insert(Hotkey([1u8; 32]), vec![(0, 100), (1, 200)]); + votes.insert(Hotkey([2u8; 32]), vec![(0, 150), (1, 150)]); + + let mut data = CheckpointData::new(1, 5, 100); + data.weight_votes = Some(WeightVoteState { + epoch: 5, + netuid: 100, + votes, + finalized: true, + final_weights: Some(vec![(0, 125), (1, 175)]), + }); + + manager.create_checkpoint(&data).unwrap(); + + let (_, loaded) = manager.load_latest().unwrap().unwrap(); + let weight_votes = loaded.weight_votes.unwrap(); + assert_eq!(weight_votes.epoch, 5); + assert!(weight_votes.finalized); + assert_eq!(weight_votes.final_weights, Some(vec![(0, 125), (1, 175)])); + } + + #[test] + fn test_checkpoint_dir_accessor() { + let dir = tempdir().unwrap(); + let manager = CheckpointManager::new(dir.path(), 5).unwrap(); + assert_eq!(manager.checkpoint_dir(), dir.path()); + } + + #[test] + fn test_pending_evaluation_state_clone() { + let state = PendingEvaluationState { + submission_id: "test".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([5u8; 32]), + submission_hash: "hash".to_string(), + scores: HashMap::new(), + created_at: 12345, + finalizing: false, + }; + let cloned = state.clone(); + assert_eq!(cloned.submission_id, state.submission_id); + assert_eq!(cloned.miner, state.miner); + } + + #[test] + fn test_completed_evaluation_state_clone() { + let state = CompletedEvaluationState { + submission_id: "test".to_string(), + challenge_id: ChallengeId::new(), + final_score: 0.75, + epoch: 10, + completed_at: 67890, + }; + let cloned = state.clone(); + assert_eq!(cloned.final_score, state.final_score); + assert_eq!(cloned.epoch, state.epoch); + } + + #[test] + fn test_weight_vote_state_clone() { + let state = WeightVoteState { + epoch: 5, + netuid: 100, + votes: HashMap::new(), + finalized: false, + final_weights: None, + }; + let cloned = state.clone(); + assert_eq!(cloned.epoch, state.epoch); + assert_eq!(cloned.finalized, state.finalized); + } +} diff --git a/crates/core/src/constants.rs b/crates/core/src/constants.rs new file mode 100644 index 000000000..49713628a --- /dev/null +++ b/crates/core/src/constants.rs @@ -0,0 +1,123 @@ +//! Production constants for Mini-Chain +//! +//! These values are hardcoded for the production network. + +use crate::Hotkey; + +// ============================================================================ +// PROTOCOL VERSION - Update this when making breaking changes +// ============================================================================ + +/// Protocol major version - increment for breaking changes +pub const PROTOCOL_VERSION_MAJOR: u32 = 0; + +/// Protocol minor version - increment for new features +pub const PROTOCOL_VERSION_MINOR: u32 = 1; + +/// Protocol patch version - increment for bug fixes +pub const PROTOCOL_VERSION_PATCH: u32 = 0; + +/// Full protocol version string +pub const PROTOCOL_VERSION: &str = "0.1.0"; + +/// Minimum compatible protocol version (validators below this are rejected) +pub const MIN_COMPATIBLE_VERSION_MAJOR: u32 = 0; +pub const MIN_COMPATIBLE_VERSION_MINOR: u32 = 1; + +/// Check if a version is compatible with current protocol +pub fn is_version_compatible(major: u32, minor: u32, _patch: u32) -> bool { + if major != PROTOCOL_VERSION_MAJOR { + return false; + } + minor >= MIN_COMPATIBLE_VERSION_MINOR +} + +/// Get protocol version as tuple (major, minor, patch) +pub fn protocol_version() -> (u32, u32, u32) { + ( + PROTOCOL_VERSION_MAJOR, + PROTOCOL_VERSION_MINOR, + PROTOCOL_VERSION_PATCH, + ) +} + +// ============================================================================ +// SUDO KEY (sr25519 - Substrate/Bittensor compatible) +// ============================================================================ + +/// Production Sudo Key (sr25519 public key) +/// SS58 Address: 5GziQCcRpN8NCJktX343brnfuVe3w6gUYieeStXPD1Dag2At +/// All requests signed by this key are treated as root and can update the network. +pub const SUDO_KEY_BYTES: [u8; 32] = [ + 0xda, 0x22, 0x04, 0x09, 0x67, 0x8d, 0xf5, 0xf0, 0x60, 0x74, 0xa6, 0x71, 0xab, 0xdc, 0x1f, 0x19, + 0xbc, 0x2b, 0xa1, 0x51, 0x72, 0x9f, 0xdb, 0x9a, 0x8e, 0x4b, 0xe2, 0x84, 0xe6, 0x0c, 0x94, 0x01, +]; + +/// Production Sudo Key as hex string (sr25519) +pub const SUDO_KEY_HEX: &str = "da220409678df5f06074a671abdc1f19bc2ba151729fdb9a8e4be284e60c9401"; + +/// Production Sudo Key SS58 address +pub const SUDO_KEY_SS58: &str = "5GziQCcRpN8NCJktX343brnfuVe3w6gUYieeStXPD1Dag2At"; + +/// Get the production sudo key +pub fn production_sudo_key() -> Hotkey { + Hotkey(SUDO_KEY_BYTES) +} + +/// Check if a hotkey is the production sudo key +pub fn is_production_sudo(hotkey: &Hotkey) -> bool { + hotkey.0 == SUDO_KEY_BYTES +} + +/// Subnet ID for production +pub const SUBNET_ID: u16 = 100; + +/// Minimum validator stake in RAO (1000 TAO) +/// Note: Actual stake validation should come from Bittensor metagraph +pub const MIN_VALIDATOR_STAKE_RAO: u64 = 1_000_000_000_000; + +/// Minimum validator stake in TAO +pub const MIN_VALIDATOR_STAKE_TAO: u64 = 1000; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sudo_key_hex() { + let key = production_sudo_key(); + assert_eq!(key.to_hex(), SUDO_KEY_HEX); + } + + #[test] + fn test_is_production_sudo() { + let key = production_sudo_key(); + assert!(is_production_sudo(&key)); + + let other = Hotkey([0u8; 32]); + assert!(!is_production_sudo(&other)); + } + + #[test] + fn test_protocol_version() { + let (major, minor, patch) = protocol_version(); + assert_eq!(major, PROTOCOL_VERSION_MAJOR); + assert_eq!(minor, PROTOCOL_VERSION_MINOR); + assert_eq!(patch, PROTOCOL_VERSION_PATCH); + } + + #[test] + fn test_version_compatibility() { + // Same version is compatible + assert!(is_version_compatible(0, 1, 0)); + + // Higher minor version is compatible + assert!(is_version_compatible(0, 2, 0)); + + // Lower minor version is not compatible + assert!(!is_version_compatible(0, 0, 0)); + + // Different major version is not compatible + assert!(!is_version_compatible(1, 1, 0)); + } +} diff --git a/crates/core/src/crypto.rs b/crates/core/src/crypto.rs new file mode 100644 index 000000000..7275d6c5b --- /dev/null +++ b/crates/core/src/crypto.rs @@ -0,0 +1,442 @@ +//! Cryptographic utilities using sr25519 (Substrate standard) +//! +//! This module provides sr25519 keypair management compatible with Substrate/Bittensor. +//! Validators derive their hotkey from BIP39 mnemonics, producing SS58 addresses +//! that can be verified against the Bittensor metagraph for stake lookup. + +use crate::{Hotkey, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sp_core::{sr25519, Pair}; + +/// SS58 address prefix for Bittensor (network ID 42 for generic Substrate) +pub const SS58_PREFIX: u16 = 42; + +/// Keypair for signing using sr25519 (Substrate/Bittensor compatible) +#[derive(Clone)] +pub struct Keypair { + pair: sr25519::Pair, + /// Mini secret seed (32 bytes) - stored for roundtrip capability + mini_seed: Option<[u8; 32]>, +} + +impl Keypair { + /// Generate a new random keypair + pub fn generate() -> Self { + use rand::RngCore; + let mut seed = [0u8; 32]; + rand::thread_rng().fill_bytes(&mut seed); + let pair = sr25519::Pair::from_seed(&seed); + Self { + pair, + mini_seed: Some(seed), + } + } + + /// Create from BIP39 mnemonic phrase (12/24 words) + /// This is the standard way for validators to derive their hotkey + pub fn from_mnemonic(mnemonic: &str) -> Result { + let (pair, seed) = sr25519::Pair::from_phrase(mnemonic, None) + .map_err(|e| MiniChainError::Crypto(format!("Invalid mnemonic: {:?}", e)))?; + + // Convert seed to array + let mut mini_seed = [0u8; 32]; + mini_seed.copy_from_slice(&seed.as_ref()[..32]); + + Ok(Self { + pair, + mini_seed: Some(mini_seed), + }) + } + + /// Create from seed bytes (32 bytes mini secret) + pub fn from_seed(seed: &[u8; 32]) -> Result { + let pair = sr25519::Pair::from_seed(seed); + Ok(Self { + pair, + mini_seed: Some(*seed), + }) + } + + /// Get the public key as Hotkey (32 bytes) + pub fn hotkey(&self) -> Hotkey { + Hotkey(self.pair.public().0) + } + + /// Get the SS58 address (human-readable format like 5GziQCc...) + pub fn ss58_address(&self) -> String { + self.hotkey().to_ss58() + } + + /// Get the seed bytes (32 bytes mini secret for backup/storage) + /// Returns the original seed if available, otherwise derives from pair + pub fn seed(&self) -> [u8; 32] { + if let Some(seed) = self.mini_seed { + seed + } else { + // Fallback: try to extract from raw (not guaranteed to work for all cases) + let raw = self.pair.to_raw_vec(); + let mut seed = [0u8; 32]; + if raw.len() >= 32 { + seed.copy_from_slice(&raw[..32]); + } + seed + } + } + + /// Sign a message + pub fn sign(&self, message: &[u8]) -> SignedMessage { + let signature = self.pair.sign(message); + SignedMessage { + message: message.to_vec(), + signature: signature.0.to_vec(), + signer: self.hotkey(), + } + } + + /// Sign with additional data (for structured messages) + pub fn sign_data(&self, data: &T) -> Result { + let message = + bincode::serialize(data).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(self.sign(&message)) + } + + /// Sign raw bytes and return only the signature bytes (for governance) + pub fn sign_bytes(&self, data: &[u8]) -> Result> { + let signature = self.pair.sign(data); + Ok(signature.0.to_vec()) + } +} + +impl std::fmt::Debug for Keypair { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Keypair({})", self.ss58_address()) + } +} + +/// A signed message using sr25519 +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SignedMessage { + pub message: Vec, + pub signature: Vec, + pub signer: Hotkey, +} + +impl SignedMessage { + /// Verify the sr25519 signature + pub fn verify(&self) -> Result { + use sp_core::crypto::Pair as _; + + if self.signature.len() != 64 { + return Err(MiniChainError::Crypto( + "Invalid signature length (expected 64 bytes)".into(), + )); + } + + let mut sig_bytes = [0u8; 64]; + sig_bytes.copy_from_slice(&self.signature); + let signature = sr25519::Signature::from_raw(sig_bytes); + + let public = sr25519::Public::from_raw(self.signer.0); + + Ok(sr25519::Pair::verify(&signature, &self.message, &public)) + } + + /// Deserialize the message content + pub fn deserialize Deserialize<'de>>(&self) -> Result { + bincode::deserialize(&self.message) + .map_err(|e| MiniChainError::Serialization(e.to_string())) + } +} + +/// Hash data using SHA256 +pub fn hash(data: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(data); + hasher.finalize().into() +} + +/// Hash and return as hex string +pub fn hash_hex(data: &[u8]) -> String { + hex::encode(hash(data)) +} + +/// Hash serializable data +pub fn hash_data(data: &T) -> Result<[u8; 32]> { + let bytes = + bincode::serialize(data).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(hash(&bytes)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_keypair_generation() { + let kp1 = Keypair::generate(); + let kp2 = Keypair::generate(); + assert_ne!(kp1.hotkey(), kp2.hotkey()); + } + + #[test] + fn test_sign_verify() { + let kp = Keypair::generate(); + let message = b"Hello, world!"; + let signed = kp.sign(message); + + assert!(signed.verify().unwrap()); + assert_eq!(signed.signer, kp.hotkey()); + } + + #[test] + fn test_sign_data() { + let kp = Keypair::generate(); + + #[derive(Serialize, Deserialize, PartialEq, Debug)] + struct TestData { + value: u64, + name: String, + } + + let data = TestData { + value: 42, + name: "test".into(), + }; + + let signed = kp.sign_data(&data).unwrap(); + assert!(signed.verify().unwrap()); + + let recovered: TestData = signed.deserialize().unwrap(); + assert_eq!(recovered, data); + } + + #[test] + fn test_mnemonic_derivation() { + // Test with a known mnemonic + let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; + let kp = Keypair::from_mnemonic(mnemonic).unwrap(); + + // The hotkey should be deterministic + let ss58 = kp.ss58_address(); + assert!(ss58.starts_with("5"), "SS58 address should start with 5"); + + // Same mnemonic should produce same keypair + let kp2 = Keypair::from_mnemonic(mnemonic).unwrap(); + assert_eq!(kp.hotkey(), kp2.hotkey()); + } + + #[test] + fn test_ss58_address_format() { + let kp = Keypair::generate(); + let ss58 = kp.ss58_address(); + + // SS58 addresses for Substrate start with 5 and are ~48 chars + assert!(ss58.starts_with("5")); + assert!(ss58.len() >= 46 && ss58.len() <= 50); + } + + #[test] + fn test_hash() { + let data = b"test data"; + let h1 = hash(data); + let h2 = hash(data); + assert_eq!(h1, h2); + + let h3 = hash(b"different data"); + assert_ne!(h1, h3); + } + + #[test] + fn test_seed_roundtrip() { + let kp1 = Keypair::generate(); + let seed = kp1.seed(); + let kp2 = Keypair::from_seed(&seed).unwrap(); + assert_eq!(kp1.hotkey(), kp2.hotkey()); + } + + #[test] + fn test_keypair_debug() { + let kp = Keypair::generate(); + let debug = format!("{:?}", kp); + assert!(debug.contains("Keypair")); + assert!(debug.contains("5")); // SS58 starts with 5 + } + + #[test] + fn test_signed_message_invalid_signature() { + let kp = Keypair::generate(); + let mut signed = kp.sign(b"test message"); + signed.signature[0] ^= 0xff; // Corrupt signature + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_signed_message_wrong_signer() { + let kp1 = Keypair::generate(); + let kp2 = Keypair::generate(); + let mut signed = kp1.sign(b"test"); + signed.signer = kp2.hotkey(); // Wrong signer + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_signed_message_invalid_signature_length() { + let signed = SignedMessage { + message: b"test".to_vec(), + signature: vec![0; 32], // Wrong length (should be 64) + signer: Hotkey([0; 32]), + }; + assert!(signed.verify().is_err()); + } + + #[test] + fn test_hash_hex() { + let data = b"hello"; + let hex = hash_hex(data); + assert_eq!(hex.len(), 64); // SHA256 = 32 bytes = 64 hex chars + } + + #[test] + fn test_hash_data() { + #[derive(Serialize)] + struct Data { + x: u32, + } + + let d1 = Data { x: 42 }; + let d2 = Data { x: 42 }; + let d3 = Data { x: 99 }; + + assert_eq!(hash_data(&d1).unwrap(), hash_data(&d2).unwrap()); + assert_ne!(hash_data(&d1).unwrap(), hash_data(&d3).unwrap()); + } + + #[test] + fn test_hash_empty() { + let h = hash(b""); + assert_eq!(h.len(), 32); + } + + #[test] + fn test_mnemonic_produces_deterministic_hotkey() { + // Test that mnemonic derivation is deterministic using a well-known test vector + // This mnemonic is NOT a production key - it's a standard BIP-39 test vector + let test_mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; + let kp = Keypair::from_mnemonic(test_mnemonic).unwrap(); + + // The hotkey should be deterministic for the same mnemonic + let hotkey_hex = kp.hotkey().to_hex(); + assert_eq!(hotkey_hex.len(), 64); + + // Creating from same mnemonic should produce same hotkey + let kp2 = Keypair::from_mnemonic(test_mnemonic).unwrap(); + assert_eq!(kp.hotkey().to_hex(), kp2.hotkey().to_hex()); + } + + #[test] + fn test_keypair_seed_method() { + let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; + let keypair = Keypair::from_mnemonic(mnemonic).unwrap(); + let seed = keypair.seed(); + assert_eq!(seed.len(), 32); + } + + #[test] + fn test_sign_bytes() { + let keypair = Keypair::generate(); + let message = b"test message"; + let signature = keypair.sign_bytes(message).unwrap(); + assert_eq!(signature.len(), 64); + + // Verify signature works + let signed_msg = SignedMessage { + message: message.to_vec(), + signature, + signer: keypair.hotkey(), + }; + assert!(signed_msg.verify().unwrap()); + } + + #[test] + fn test_signed_message_debug() { + let keypair = Keypair::generate(); + let message = b"test"; + let signature = keypair.sign_bytes(message).unwrap(); + let signed_msg = SignedMessage { + message: message.to_vec(), + signature, + signer: keypair.hotkey(), + }; + let debug_str = format!("{:?}", signed_msg); + assert!(debug_str.contains("SignedMessage")); + } + + #[test] + fn test_keypair_seed_fallback() { + // Force the fallback branch by creating a keypair and removing mini_seed + let mut keypair = Keypair::generate(); + keypair.mini_seed = None; + + // Call seed() which should hit the fallback path + let seed = keypair.seed(); + assert_eq!(seed.len(), 32); + + // Verify the fallback extracts from raw_vec + let raw = keypair.pair.to_raw_vec(); + if raw.len() >= 32 { + let mut expected = [0u8; 32]; + expected.copy_from_slice(&raw[..32]); + assert_eq!(seed, expected); + } + } + + #[test] + fn test_sign_data_serialization_error() { + // Test that sign_data properly handles serialization errors + let keypair = Keypair::generate(); + + // Create a type that always fails to serialize + struct FailSerialize; + impl serde::Serialize for FailSerialize { + fn serialize(&self, _serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + Err(serde::ser::Error::custom( + "intentional serialization failure", + )) + } + } + + let result = keypair.sign_data(&FailSerialize); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + MiniChainError::Serialization(_) + )); + } + + #[test] + fn test_hash_data_serialization_error() { + // Test hash_data with a type that fails to serialize + struct FailSerialize; + impl serde::Serialize for FailSerialize { + fn serialize(&self, _serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + Err(serde::ser::Error::custom( + "intentional serialization failure", + )) + } + } + + let result = hash_data(&FailSerialize); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + MiniChainError::Serialization(_) + )); + } +} diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs new file mode 100644 index 000000000..0ed4e8037 --- /dev/null +++ b/crates/core/src/error.rs @@ -0,0 +1,116 @@ +//! Error types for platform + +use thiserror::Error; + +/// Result type alias +pub type Result = std::result::Result; + +/// Mini-chain error types +#[derive(Error, Debug)] +pub enum MiniChainError { + #[error("Cryptographic error: {0}")] + Crypto(String), + + #[error("Serialization error: {0}")] + Serialization(String), + + #[error("Network error: {0}")] + Network(String), + + #[error("Consensus error: {0}")] + Consensus(String), + + #[error("WASM runtime error: {0}")] + Wasm(String), + + #[error("Storage error: {0}")] + Storage(String), + + #[error("Invalid signature")] + InvalidSignature, + + #[error("Unauthorized: {0}")] + Unauthorized(String), + + #[error("Not found: {0}")] + NotFound(String), + + #[error("Invalid state: {0}")] + InvalidState(String), + + #[error("Timeout: {0}")] + Timeout(String), + + #[error("Internal error: {0}")] + Internal(String), + + #[error("Rate limited: {0}")] + RateLimited(String), + + #[error("Type mismatch: {0}")] + TypeMismatch(String), + + #[error("Validation error: {0}")] + Validation(String), +} + +impl From for MiniChainError { + fn from(err: std::io::Error) -> Self { + MiniChainError::Internal(err.to_string()) + } +} + +impl From for MiniChainError { + fn from(err: bincode::Error) -> Self { + MiniChainError::Serialization(err.to_string()) + } +} + +impl From for MiniChainError { + fn from(err: serde_json::Error) -> Self { + MiniChainError::Serialization(err.to_string()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_from_io_error() { + let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"); + let chain_err: MiniChainError = io_err.into(); + assert!(matches!(chain_err, MiniChainError::Internal(_))); + assert!(chain_err.to_string().contains("file not found")); + } + + #[test] + fn test_from_bincode_error() { + // Create a bincode serialization error by writing to a fixed-size buffer that's too small + let mut buffer = [0u8; 2]; // Fixed small buffer + let large_data = vec![0u8; 1000]; // Data too large for buffer + let result = bincode::serialize_into(&mut buffer[..], &large_data); + let bincode_err = result.unwrap_err(); + let chain_err: MiniChainError = bincode_err.into(); + assert!(matches!(chain_err, MiniChainError::Serialization(_))); + } + + #[test] + fn test_from_serde_json_error() { + let json_err = serde_json::from_str::("{invalid json").unwrap_err(); + let chain_err: MiniChainError = json_err.into(); + assert!(matches!(chain_err, MiniChainError::Serialization(_))); + } + + #[test] + fn test_error_display() { + let err = MiniChainError::Crypto("bad key".to_string()); + assert_eq!(err.to_string(), "Cryptographic error: bad key"); + + let err = MiniChainError::InvalidSignature; + assert_eq!(err.to_string(), "Invalid signature"); + + let err = MiniChainError::NotFound("block 123".to_string()); + assert_eq!(err.to_string(), "Not found: block 123"); + } +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 000000000..c3fec0bce --- /dev/null +++ b/crates/core/src/lib.rs @@ -0,0 +1,32 @@ +//! Mini-Chain Core Types +//! +//! Core types and structures for the P2P validator network. + +pub mod challenge; +pub mod checkpoint; +pub mod constants; +pub mod crypto; +pub mod error; +pub mod message; +pub mod restoration; +pub mod schema_guard; +pub mod state; +pub mod state_versioning; +pub mod types; + +pub use challenge::*; +pub use checkpoint::{ + CheckpointData, CheckpointHeader, CheckpointManager, CompletedEvaluationState, + PendingEvaluationState, WeightVoteState, CHECKPOINT_VERSION, +}; +pub use constants::*; +pub use crypto::*; +pub use error::*; +pub use message::*; +pub use restoration::{ + CheckpointInfo, Restorable, RestorationManager, RestorationOptions, RestorationResult, +}; +pub use schema_guard::{verify_schema_integrity, SchemaError}; +pub use state::*; +pub use state_versioning::*; +pub use types::*; diff --git a/crates/core/src/message.rs b/crates/core/src/message.rs new file mode 100644 index 000000000..f282d15ba --- /dev/null +++ b/crates/core/src/message.rs @@ -0,0 +1,1368 @@ +//! Network messages for P2P communication + +use crate::{ + BlockHeight, ChainState, ChallengeConfig, ChallengeId, Hotkey, Job, NetworkConfig, Result, + Score, SignedMessage, StateSnapshot, ValidatorInfo, +}; +use serde::{Deserialize, Serialize}; + +/// All network message types +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum NetworkMessage { + /// Handshake when connecting (includes version check) + Handshake(HandshakeMessage), + + /// Sudo action from subnet owner + SudoAction(SudoAction), + + /// Proposal for consensus + Proposal(Proposal), + + /// Vote on a proposal + Vote(Vote), + + /// Job assignment + JobAssignment(JobAssignment), + + /// Evaluation result + EvaluationResult(EvaluationResult), + + /// State synchronization + StateSync(StateSyncMessage), + + /// Heartbeat/ping + Heartbeat(HeartbeatMessage), + + /// Weight commitment (commit-reveal phase 1) + WeightCommitment(WeightCommitmentMessage), + + /// Weight reveal (commit-reveal phase 2) + WeightReveal(WeightRevealMessage), + + /// Epoch transition notification + EpochTransition(EpochTransitionMessage), + + /// Agent submission for challenge (P2P propagation) - DEPRECATED + /// Use ChallengeMessage for new submissions + AgentSubmission(AgentSubmissionMessage), + + /// Generic challenge P2P message (routes to challenge handlers) + /// Used for secure submissions, ACKs, evaluations, weights + ChallengeMessage(ChallengeNetworkMessage), + + /// Real-time task progress update (for evaluation tracking) + TaskProgress(TaskProgressMessage), + + /// Agent log proposal for consensus validation + AgentLogProposal(AgentLogProposalMessage), + + /// Version incompatible - disconnect + VersionMismatch { + our_version: String, + required_min_version: String, + }, +} + +/// Real-time task progress message +/// Broadcast when each task in an evaluation completes +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskProgressMessage { + /// Challenge ID + pub challenge_id: String, + /// Agent being evaluated + pub agent_hash: String, + /// Evaluation ID (unique per evaluation run) + pub evaluation_id: String, + /// Task ID that completed + pub task_id: String, + /// Task index (1-based for display) + pub task_index: u32, + /// Total number of tasks + pub total_tasks: u32, + /// Whether this task passed + pub passed: bool, + /// Task score (0.0 - 1.0) + pub score: f64, + /// Execution time in milliseconds + pub execution_time_ms: u64, + /// Cost in USD for this task + pub cost_usd: f64, + /// Error message if task failed + pub error: Option, + /// Validator performing the evaluation + pub validator_hotkey: String, + /// Timestamp + pub timestamp: u64, +} + +impl TaskProgressMessage { + #[allow(clippy::too_many_arguments)] + pub fn new( + challenge_id: String, + agent_hash: String, + evaluation_id: String, + task_id: String, + task_index: u32, + total_tasks: u32, + passed: bool, + score: f64, + execution_time_ms: u64, + cost_usd: f64, + error: Option, + validator_hotkey: String, + ) -> Self { + Self { + challenge_id, + agent_hash, + evaluation_id, + task_id, + task_index, + total_tasks, + passed, + score, + execution_time_ms, + cost_usd, + error, + validator_hotkey, + timestamp: chrono::Utc::now().timestamp() as u64, + } + } +} + +/// Agent log proposal message +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentLogProposalMessage { + pub submission_id: String, + pub challenge_id: String, + pub miner_hotkey: String, + pub logs_hash: [u8; 32], + pub logs_data: Vec, + pub validator_hotkey: String, + pub epoch: u64, +} + +/// Challenge-specific network message +/// Contains serialized challenge P2P message that will be routed to the challenge handler +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeNetworkMessage { + /// Challenge ID (e.g., "term-bench") + pub challenge_id: String, + /// Serialized challenge message (challenge-specific format) + pub payload: Vec, + /// Message type hint (for routing without deserializing) + pub message_type: ChallengeMessageType, +} + +/// Type hints for challenge messages +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub enum ChallengeMessageType { + /// Encrypted submission (commit phase) + EncryptedSubmission, + /// Acknowledgment of submission receipt + SubmissionAck, + /// Decryption key reveal (reveal phase) + KeyReveal, + /// Evaluation result + EvaluationResult, + /// Request evaluations + RequestEvaluations, + /// Evaluations response + EvaluationsResponse, + /// Weight calculation result + WeightResult, + /// Distributed storage: write announcement + StorageWrite, + /// Distributed storage: request entry + StorageRequest, + /// Distributed storage: entry response + StorageResponse, + /// Distributed storage: sync request + StorageSync, + /// Custom challenge-specific message + Custom(String), +} + +/// Agent submission message for P2P propagation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentSubmissionMessage { + /// Challenge ID + pub challenge_id: String, + /// Agent hash (SHA256 of source code) + pub agent_hash: String, + /// Miner hotkey + pub miner_hotkey: String, + /// Source code (may be obfuscated for non-top validators) + pub source_code: Option, + /// Obfuscated code hash (for validators without source) + pub obfuscated_hash: Option, + /// Submission timestamp + pub submitted_at: chrono::DateTime, + /// Submitting validator (who received the original submission) + pub submitting_validator: Hotkey, + /// Signature from miner + pub miner_signature: Vec, + /// Source code size (for stats) + pub source_code_len: usize, +} + +impl AgentSubmissionMessage { + /// Create a new agent submission message + pub fn new( + challenge_id: String, + agent_hash: String, + miner_hotkey: String, + source_code: Option, + submitting_validator: Hotkey, + ) -> Self { + let source_code_len = source_code.as_ref().map(|s| s.len()).unwrap_or(0); + Self { + challenge_id, + agent_hash, + miner_hotkey, + source_code, + obfuscated_hash: None, + submitted_at: chrono::Utc::now(), + submitting_validator, + miner_signature: vec![], + source_code_len, + } + } +} + +/// Handshake message when a node connects +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HandshakeMessage { + pub hotkey: Hotkey, + pub block_height: BlockHeight, + pub state_hash: [u8; 32], + pub version: String, + pub version_major: u32, + pub version_minor: u32, + pub version_patch: u32, + pub timestamp: chrono::DateTime, +} + +impl HandshakeMessage { + pub fn new(hotkey: Hotkey, block_height: BlockHeight, state_hash: [u8; 32]) -> Self { + use crate::constants::{ + PROTOCOL_VERSION, PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR, + PROTOCOL_VERSION_PATCH, + }; + Self { + hotkey, + block_height, + state_hash, + version: PROTOCOL_VERSION.to_string(), + version_major: PROTOCOL_VERSION_MAJOR, + version_minor: PROTOCOL_VERSION_MINOR, + version_patch: PROTOCOL_VERSION_PATCH, + timestamp: chrono::Utc::now(), + } + } + + /// Check if this handshake is from a compatible version + pub fn is_compatible(&self) -> bool { + crate::constants::is_version_compatible( + self.version_major, + self.version_minor, + self.version_patch, + ) + } +} + +/// Sudo actions that only the subnet owner can perform +#[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] +pub enum SudoAction { + // === Network Configuration === + /// Update network configuration + UpdateConfig { config: NetworkConfig }, + + // === Weight Allocation === + /// Set challenge weight ratio on a mechanism (0.0 - 1.0) + /// Remaining weight goes to UID 0 (burn) unless other challenges share the mechanism + SetChallengeWeight { + challenge_id: ChallengeId, + mechanism_id: u8, + /// Weight ratio for this challenge (0.0 - 1.0) + /// If multiple challenges on same mechanism, ratios are normalized + weight_ratio: f64, + }, + + /// Set mechanism burn rate (weight that goes to UID 0) + /// Applied after challenge weights are distributed + SetMechanismBurnRate { + mechanism_id: u8, + /// Burn rate (0.0 - 1.0), e.g., 0.1 = 10% to UID 0 + burn_rate: f64, + }, + + /// Configure mechanism weight distribution + SetMechanismConfig { + mechanism_id: u8, + config: MechanismWeightConfig, + }, + + // === Version Management === + /// Set required validator version (triggers auto-update) + SetRequiredVersion { + min_version: String, + recommended_version: String, + mandatory: bool, + deadline_block: Option, + release_notes: Option, + }, + + // === Validator Management === + /// Add a validator + AddValidator { info: ValidatorInfo }, + + /// Remove a validator + RemoveValidator { hotkey: Hotkey }, + + // === Emergency Controls === + /// Emergency pause + EmergencyPause { reason: String }, + + /// Resume after pause + Resume, + + /// Force state update (for recovery) + ForceStateUpdate { state: ChainState }, + + // === Challenge Management === + /// Add a new challenge with WASM module + AddChallenge { + name: String, + description: String, + wasm_code: Vec, + owner: Hotkey, + config: ChallengeConfig, + weight: u16, + }, + + /// Remove/deactivate a challenge + RemoveChallenge { challenge_id: ChallengeId }, + + /// Edit an existing challenge + EditChallenge { + challenge_id: ChallengeId, + name: Option, + description: Option, + wasm_code: Option>, + config: Option, + weight: Option, + }, + + /// Stop network - burn all emissions to UID 0 + StopNetwork { reason: String }, +} + +/// Configuration for how weights are distributed on a mechanism +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MechanismWeightConfig { + /// Mechanism ID on Bittensor + pub mechanism_id: u8, + /// Base burn rate - percentage of weights that go to UID 0 (0.0 - 1.0) + /// Applied before challenge distribution + pub base_burn_rate: f64, + /// Whether to distribute remaining weight equally among challenges + /// If false, uses per-challenge weight_ratio + pub equal_distribution: bool, + /// Minimum weight per miner (prevents dust weights) + pub min_weight_threshold: f64, + /// Maximum weight cap per miner (DEPRECATED - set to 1.0) + /// NOTE: Weight caps have been removed. Challenges receive pure weights. + pub max_weight_cap: f64, + /// Whether this mechanism is active + pub active: bool, +} + +impl MechanismWeightConfig { + pub fn new(mechanism_id: u8) -> Self { + Self { + mechanism_id, + base_burn_rate: 0.0, + equal_distribution: true, + min_weight_threshold: 0.0001, + max_weight_cap: 1.0, // No cap - pure weights + active: true, + } + } + + pub fn with_burn_rate(mut self, rate: f64) -> Self { + self.base_burn_rate = rate.clamp(0.0, 1.0); + self + } + + pub fn with_max_cap(mut self, cap: f64) -> Self { + self.max_weight_cap = cap.clamp(0.0, 1.0); + self + } +} + +impl Default for MechanismWeightConfig { + fn default() -> Self { + Self::new(0) + } +} + +/// Challenge weight allocation on a mechanism +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeWeightAllocation { + /// Challenge ID + pub challenge_id: ChallengeId, + /// Mechanism ID this challenge is on + pub mechanism_id: u8, + /// Weight ratio for this challenge (0.0 - 1.0) + /// If sum of all challenges on mechanism > 1.0, they are normalized + pub weight_ratio: f64, + /// Whether this allocation is active + pub active: bool, +} + +impl ChallengeWeightAllocation { + pub fn new(challenge_id: ChallengeId, mechanism_id: u8, weight_ratio: f64) -> Self { + Self { + challenge_id, + mechanism_id, + weight_ratio: weight_ratio.clamp(0.0, 1.0), + active: true, + } + } +} + +/// Proposal for consensus +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Proposal { + pub id: uuid::Uuid, + pub block_height: BlockHeight, + pub action: ProposalAction, + pub proposer: Hotkey, + pub timestamp: chrono::DateTime, +} + +impl Proposal { + pub fn new(action: ProposalAction, proposer: Hotkey, block_height: BlockHeight) -> Self { + Self { + id: uuid::Uuid::new_v4(), + block_height, + action, + proposer, + timestamp: chrono::Utc::now(), + } + } +} + +/// Actions that can be proposed for consensus +#[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] +pub enum ProposalAction { + /// Sudo action (only from subnet owner) + Sudo(SudoAction), + + /// New block + NewBlock { state_hash: [u8; 32] }, + + /// Job completion with result + JobCompletion { + job_id: uuid::Uuid, + result: Score, + validator: Hotkey, + }, +} + +/// Vote on a proposal +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Vote { + pub proposal_id: uuid::Uuid, + pub voter: Hotkey, + pub approve: bool, + pub timestamp: chrono::DateTime, +} + +impl Vote { + pub fn approve(proposal_id: uuid::Uuid, voter: Hotkey) -> Self { + Self { + proposal_id, + voter, + approve: true, + timestamp: chrono::Utc::now(), + } + } + + pub fn reject(proposal_id: uuid::Uuid, voter: Hotkey) -> Self { + Self { + proposal_id, + voter, + approve: false, + timestamp: chrono::Utc::now(), + } + } +} + +/// Job assignment message +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobAssignment { + pub job: Job, + pub assigned_to: Hotkey, + pub deadline: chrono::DateTime, +} + +/// Evaluation result from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationResult { + pub job_id: uuid::Uuid, + pub challenge_id: ChallengeId, + pub agent_hash: String, + pub score: Score, + pub execution_time_ms: u64, + pub validator: Hotkey, + pub timestamp: chrono::DateTime, +} + +impl EvaluationResult { + pub fn new( + job_id: uuid::Uuid, + challenge_id: ChallengeId, + agent_hash: String, + score: Score, + execution_time_ms: u64, + validator: Hotkey, + ) -> Self { + Self { + job_id, + challenge_id, + agent_hash, + score, + execution_time_ms, + validator, + timestamp: chrono::Utc::now(), + } + } +} + +/// State synchronization message +#[derive(Clone, Debug, Serialize, Deserialize)] +#[allow(clippy::large_enum_variant)] +pub enum StateSyncMessage { + /// Request state snapshot + RequestSnapshot, + + /// Full state response + FullState(ChainState), + + /// Snapshot response (lightweight) + Snapshot(StateSnapshot), + + /// Request specific data + RequestData { data_type: SyncDataType }, + + /// Data response + DataResponse { + data_type: SyncDataType, + data: Vec, + }, +} + +/// Types of data that can be synced +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum SyncDataType { + Validators, + Challenges, + PendingJobs, + Config, +} + +/// Heartbeat message +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HeartbeatMessage { + pub hotkey: Hotkey, + pub block_height: BlockHeight, + pub state_hash: [u8; 32], + pub timestamp: chrono::DateTime, +} + +impl HeartbeatMessage { + pub fn new(hotkey: Hotkey, block_height: BlockHeight, state_hash: [u8; 32]) -> Self { + Self { + hotkey, + block_height, + state_hash, + timestamp: chrono::Utc::now(), + } + } +} + +/// Weight commitment message (phase 1 of commit-reveal) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightCommitmentMessage { + pub validator: Hotkey, + pub challenge_id: ChallengeId, + pub epoch: u64, + pub commitment_hash: [u8; 32], + pub timestamp: chrono::DateTime, +} + +impl WeightCommitmentMessage { + pub fn new( + validator: Hotkey, + challenge_id: ChallengeId, + epoch: u64, + commitment_hash: [u8; 32], + ) -> Self { + Self { + validator, + challenge_id, + epoch, + commitment_hash, + timestamp: chrono::Utc::now(), + } + } +} + +/// Weight reveal message (phase 2 of commit-reveal) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightRevealMessage { + pub validator: Hotkey, + pub challenge_id: ChallengeId, + pub epoch: u64, + pub weights: Vec, + pub secret: Vec, + pub timestamp: chrono::DateTime, +} + +/// Single weight entry for an agent +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightEntry { + pub agent_hash: String, + pub weight: f64, +} + +impl WeightRevealMessage { + pub fn new( + validator: Hotkey, + challenge_id: ChallengeId, + epoch: u64, + weights: Vec, + secret: Vec, + ) -> Self { + Self { + validator, + challenge_id, + epoch, + weights, + secret, + timestamp: chrono::Utc::now(), + } + } +} + +/// Epoch transition notification +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EpochTransitionMessage { + pub epoch: u64, + pub phase: String, + pub block_height: BlockHeight, + pub timestamp: chrono::DateTime, +} + +impl EpochTransitionMessage { + pub fn new(epoch: u64, phase: &str, block_height: BlockHeight) -> Self { + Self { + epoch, + phase: phase.to_string(), + block_height, + timestamp: chrono::Utc::now(), + } + } +} + +/// Signed network message wrapper +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SignedNetworkMessage { + pub message: NetworkMessage, + pub signature: SignedMessage, +} + +impl SignedNetworkMessage { + /// Create and sign a network message + pub fn new(message: NetworkMessage, keypair: &crate::Keypair) -> Result { + let signed = keypair.sign_data(&message)?; + Ok(Self { + message, + signature: signed, + }) + } + + /// Verify the message signature + pub fn verify(&self) -> Result { + self.signature.verify() + } + + /// Get the signer's hotkey + pub fn signer(&self) -> &Hotkey { + &self.signature.signer + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Keypair; + + #[test] + fn test_signed_message() { + let kp = Keypair::generate(); + let msg = NetworkMessage::Heartbeat(HeartbeatMessage::new(kp.hotkey(), 100, [0u8; 32])); + + let signed = SignedNetworkMessage::new(msg, &kp).unwrap(); + assert!(signed.verify().unwrap()); + assert_eq!(signed.signer(), &kp.hotkey()); + } + + #[test] + fn test_proposal() { + let kp = Keypair::generate(); + let proposal = Proposal::new( + ProposalAction::NewBlock { + state_hash: [1u8; 32], + }, + kp.hotkey(), + 100, + ); + + assert_eq!(proposal.proposer, kp.hotkey()); + assert_eq!(proposal.block_height, 100); + } + + #[test] + fn test_vote() { + let kp = Keypair::generate(); + let vote = Vote::approve(uuid::Uuid::new_v4(), kp.hotkey()); + assert!(vote.approve); + + let vote2 = Vote::reject(uuid::Uuid::new_v4(), kp.hotkey()); + assert!(!vote2.approve); + } + + #[test] + fn test_heartbeat_message() { + let hotkey = Hotkey([1u8; 32]); + let hb = HeartbeatMessage::new(hotkey.clone(), 42, [0xab; 32]); + assert_eq!(hb.hotkey, hotkey); + assert_eq!(hb.block_height, 42); + } + + #[test] + fn test_network_message_variants() { + let hotkey = Hotkey([1u8; 32]); + + // Test Heartbeat + let hb = NetworkMessage::Heartbeat(HeartbeatMessage::new(hotkey.clone(), 1, [0; 32])); + match hb { + NetworkMessage::Heartbeat(_) => (), + _ => panic!("Expected Heartbeat"), + } + + // Test StateSync + let sync_msg = NetworkMessage::StateSync(StateSyncMessage::RequestSnapshot); + match sync_msg { + NetworkMessage::StateSync(StateSyncMessage::RequestSnapshot) => (), + _ => panic!("Expected StateSync::RequestSnapshot"), + } + } + + #[test] + fn test_state_sync_message_variants() { + // Test RequestSnapshot + let msg = StateSyncMessage::RequestSnapshot; + match msg { + StateSyncMessage::RequestSnapshot => (), + _ => panic!("Expected RequestSnapshot"), + } + + // Test RequestData + let msg2 = StateSyncMessage::RequestData { + data_type: SyncDataType::Validators, + }; + match msg2 { + StateSyncMessage::RequestData { + data_type: SyncDataType::Validators, + } => (), + _ => panic!("Expected RequestData::Validators"), + } + } + + #[test] + fn test_sudo_action_variants() { + // Test EmergencyPause + let cmd = SudoAction::EmergencyPause { + reason: "test".to_string(), + }; + match cmd { + SudoAction::EmergencyPause { reason } => assert_eq!(reason, "test"), + _ => panic!("Expected EmergencyPause"), + } + + // Test Resume + let cmd2 = SudoAction::Resume; + match cmd2 { + SudoAction::Resume => (), + _ => panic!("Expected Resume"), + } + } + + #[test] + fn test_proposal_action_new_block() { + let action = ProposalAction::NewBlock { + state_hash: [0xff; 32], + }; + match action { + ProposalAction::NewBlock { state_hash } => { + assert_eq!(state_hash, [0xff; 32]); + } + _ => panic!("Expected NewBlock"), + } + } + + #[test] + fn test_signed_network_message_signer() { + let kp = Keypair::generate(); + let msg = NetworkMessage::Heartbeat(HeartbeatMessage::new(kp.hotkey(), 1, [0; 32])); + let signed = SignedNetworkMessage::new(msg, &kp).unwrap(); + assert_eq!(signed.signer(), &kp.hotkey()); + } + + #[test] + fn test_agent_submission_message() { + let hotkey = Hotkey([1u8; 32]); + let msg = AgentSubmissionMessage::new( + "test-challenge".to_string(), + "abc123".to_string(), + "miner123".to_string(), + Some("print('hello')".to_string()), + hotkey.clone(), + ); + + assert_eq!(msg.challenge_id, "test-challenge"); + assert_eq!(msg.agent_hash, "abc123"); + assert_eq!(msg.miner_hotkey, "miner123"); + assert!(msg.source_code.is_some()); + assert_eq!(msg.source_code_len, 14); + assert_eq!(msg.submitting_validator, hotkey); + } + + #[test] + fn test_handshake_message() { + let hotkey = Hotkey([2u8; 32]); + let hs = HandshakeMessage::new(hotkey.clone(), 100, [0xab; 32]); + + assert_eq!(hs.hotkey, hotkey); + assert_eq!(hs.block_height, 100); + assert_eq!(hs.state_hash, [0xab; 32]); + assert!(!hs.version.is_empty()); + assert!(hs.is_compatible()); + } + + #[test] + fn test_challenge_message_types() { + let types = vec![ + ChallengeMessageType::EncryptedSubmission, + ChallengeMessageType::SubmissionAck, + ChallengeMessageType::KeyReveal, + ChallengeMessageType::EvaluationResult, + ChallengeMessageType::RequestEvaluations, + ChallengeMessageType::EvaluationsResponse, + ChallengeMessageType::WeightResult, + ChallengeMessageType::StorageWrite, + ChallengeMessageType::StorageRequest, + ChallengeMessageType::StorageResponse, + ChallengeMessageType::StorageSync, + ChallengeMessageType::Custom("test".to_string()), + ]; + + for t in types { + let msg = ChallengeNetworkMessage { + challenge_id: "test".to_string(), + payload: vec![1, 2, 3], + message_type: t.clone(), + }; + assert_eq!(msg.challenge_id, "test"); + assert_eq!(msg.payload, vec![1, 2, 3]); + } + } + + #[test] + fn test_sudo_action_set_required_version() { + let action = SudoAction::SetRequiredVersion { + min_version: "0.2.0".to_string(), + recommended_version: "0.3.0".to_string(), + mandatory: true, + deadline_block: Some(1000), + release_notes: Some("Bug fixes".to_string()), + }; + + match action { + SudoAction::SetRequiredVersion { + min_version, + mandatory, + .. + } => { + assert_eq!(min_version, "0.2.0"); + assert!(mandatory); + } + _ => panic!("Expected SetRequiredVersion"), + } + } + + #[test] + fn test_sudo_action_add_validator() { + let hotkey = Hotkey([3u8; 32]); + let info = ValidatorInfo::new(hotkey.clone(), crate::Stake(1000)); + let action = SudoAction::AddValidator { info: info.clone() }; + + match action { + SudoAction::AddValidator { info: i } => { + assert_eq!(i.hotkey, hotkey); + } + _ => panic!("Expected AddValidator"), + } + } + + #[test] + fn test_sudo_action_remove_validator() { + let hotkey = Hotkey([4u8; 32]); + let action = SudoAction::RemoveValidator { + hotkey: hotkey.clone(), + }; + + match action { + SudoAction::RemoveValidator { hotkey: h } => { + assert_eq!(h, hotkey); + } + _ => panic!("Expected RemoveValidator"), + } + } + + #[test] + fn test_evaluation_result() { + let kp = Keypair::generate(); + let job_id = uuid::Uuid::new_v4(); + let challenge_id = ChallengeId::new(); + let score = Score::new(0.85, 1.0); + + let result = EvaluationResult::new( + job_id, + challenge_id, + "agent123".to_string(), + score, + 100, + kp.hotkey(), + ); + + assert_eq!(result.job_id, job_id); + assert_eq!(result.challenge_id, challenge_id); + assert_eq!(result.score.value, score.value); + assert_eq!(result.execution_time_ms, 100); + } + + #[test] + fn test_weight_commitment_message() { + let hotkey = Hotkey([5u8; 32]); + let challenge_id = ChallengeId::new(); + let commitment = WeightCommitmentMessage::new(hotkey.clone(), challenge_id, 10, [0xab; 32]); + + assert_eq!(commitment.validator, hotkey); + assert_eq!(commitment.challenge_id, challenge_id); + assert_eq!(commitment.epoch, 10); + assert_eq!(commitment.commitment_hash, [0xab; 32]); + } + + #[test] + fn test_weight_reveal_message() { + let hotkey = Hotkey([6u8; 32]); + let challenge_id = ChallengeId::new(); + let weights = vec![ + WeightEntry { + agent_hash: "agent1".to_string(), + weight: 0.5, + }, + WeightEntry { + agent_hash: "agent2".to_string(), + weight: 0.3, + }, + ]; + let reveal = + WeightRevealMessage::new(hotkey.clone(), challenge_id, 10, weights, vec![1, 2, 3, 4]); + + assert_eq!(reveal.validator, hotkey); + assert_eq!(reveal.challenge_id, challenge_id); + assert_eq!(reveal.weights.len(), 2); + assert_eq!(reveal.epoch, 10); + } + + #[test] + fn test_epoch_transition_message() { + let transition = EpochTransitionMessage::new(10, "commit", 1000); + + assert_eq!(transition.epoch, 10); + assert_eq!(transition.phase, "commit"); + assert_eq!(transition.block_height, 1000); + } + + #[test] + fn test_job_assignment() { + let job = Job::new(ChallengeId::new(), "abc123".to_string()); + + let hotkey = Hotkey([7u8; 32]); + let assignment = JobAssignment { + job: job.clone(), + assigned_to: hotkey.clone(), + deadline: chrono::Utc::now() + chrono::Duration::hours(1), + }; + + assert_eq!(assignment.job.id, job.id); + assert_eq!(assignment.assigned_to, hotkey); + } + + #[test] + fn test_state_sync_message_all_variants() { + // Test all SyncDataType variants + let data_types = vec![ + SyncDataType::Validators, + SyncDataType::Challenges, + SyncDataType::PendingJobs, + SyncDataType::Config, + ]; + + for dt in data_types { + let msg = StateSyncMessage::RequestData { + data_type: dt.clone(), + }; + match msg { + StateSyncMessage::RequestData { data_type } => { + assert_eq!(data_type, dt); + } + _ => panic!("Expected RequestData"), + } + } + } + + #[test] + fn test_network_message_serialization() { + let hotkey = Hotkey([8u8; 32]); + let msg = NetworkMessage::Heartbeat(HeartbeatMessage::new(hotkey, 100, [0; 32])); + + // Test serialization + let serialized = bincode::serialize(&msg).unwrap(); + assert!(!serialized.is_empty()); + + // Test deserialization + let deserialized: NetworkMessage = bincode::deserialize(&serialized).unwrap(); + match deserialized { + NetworkMessage::Heartbeat(hb) => { + assert_eq!(hb.block_height, 100); + } + _ => panic!("Expected Heartbeat"), + } + } + + #[test] + fn test_challenge_network_message() { + let msg = ChallengeNetworkMessage { + challenge_id: "term-bench".to_string(), + payload: vec![1, 2, 3, 4, 5], + message_type: ChallengeMessageType::EvaluationResult, + }; + + assert_eq!(msg.challenge_id, "term-bench"); + assert_eq!(msg.payload.len(), 5); + assert_eq!(msg.message_type, ChallengeMessageType::EvaluationResult); + } + + #[test] + fn test_proposal_action_variants() { + // Test Sudo action + let sudo = SudoAction::EmergencyPause { + reason: "test".to_string(), + }; + let action = ProposalAction::Sudo(sudo); + match action { + ProposalAction::Sudo(SudoAction::EmergencyPause { reason }) => { + assert_eq!(reason, "test"); + } + _ => panic!("Expected Sudo"), + } + + // Test NewBlock action + let action = ProposalAction::NewBlock { + state_hash: [0xab; 32], + }; + match action { + ProposalAction::NewBlock { state_hash } => { + assert_eq!(state_hash, [0xab; 32]); + } + _ => panic!("Expected NewBlock"), + } + + // Test JobCompletion action + let job_id = uuid::Uuid::new_v4(); + let hotkey = Hotkey([10u8; 32]); + let score = Score::new(0.95, 1.0); + let action = ProposalAction::JobCompletion { + job_id, + result: score, + validator: hotkey.clone(), + }; + match action { + ProposalAction::JobCompletion { + job_id: jid, + result, + validator, + } => { + assert_eq!(jid, job_id); + assert_eq!(result.value, score.value); + assert_eq!(validator, hotkey); + } + _ => panic!("Expected JobCompletion"), + } + } + + #[test] + fn test_sudo_action_update_config() { + let config = NetworkConfig::default(); + let action = SudoAction::UpdateConfig { + config: config.clone(), + }; + match action { + SudoAction::UpdateConfig { config: c } => { + assert_eq!(c.subnet_id, config.subnet_id); + } + _ => panic!("Expected UpdateConfig"), + } + } + + #[test] + fn test_sudo_action_force_state_update() { + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + let action = SudoAction::ForceStateUpdate { + state: state.clone(), + }; + match action { + SudoAction::ForceStateUpdate { state: s } => { + assert_eq!(s.block_height, state.block_height); + } + _ => panic!("Expected ForceStateUpdate"), + } + } + + #[test] + fn test_state_sync_snapshot() { + let snapshot = StateSnapshot { + block_height: 100, + state_hash: [0xab; 32], + validator_count: 5, + challenge_count: 2, + pending_jobs: 10, + timestamp: chrono::Utc::now(), + }; + let msg = StateSyncMessage::Snapshot(snapshot.clone()); + match msg { + StateSyncMessage::Snapshot(s) => { + assert_eq!(s.block_height, 100); + assert_eq!(s.validator_count, 5); + } + _ => panic!("Expected Snapshot"), + } + } + + #[test] + fn test_version_mismatch_message() { + let msg = NetworkMessage::VersionMismatch { + our_version: "0.1.0".to_string(), + required_min_version: "0.2.0".to_string(), + }; + match msg { + NetworkMessage::VersionMismatch { + our_version, + required_min_version, + } => { + assert_eq!(our_version, "0.1.0"); + assert_eq!(required_min_version, "0.2.0"); + } + _ => panic!("Expected VersionMismatch"), + } + } + + #[test] + fn test_all_network_message_variants() { + let hotkey = Hotkey([1u8; 32]); + + // Handshake + let _ = NetworkMessage::Handshake(HandshakeMessage::new(hotkey.clone(), 1, [0; 32])); + + // SudoAction + let _ = NetworkMessage::SudoAction(SudoAction::Resume); + + // Proposal + let _ = NetworkMessage::Proposal(Proposal::new( + ProposalAction::NewBlock { + state_hash: [0; 32], + }, + hotkey.clone(), + 1, + )); + + // Vote + let _ = NetworkMessage::Vote(Vote::approve(uuid::Uuid::new_v4(), hotkey.clone())); + + // JobAssignment + let job = Job::new(ChallengeId::new(), "test".to_string()); + let _ = NetworkMessage::JobAssignment(JobAssignment { + job, + assigned_to: hotkey.clone(), + deadline: chrono::Utc::now(), + }); + + // EvaluationResult + let _ = NetworkMessage::EvaluationResult(EvaluationResult::new( + uuid::Uuid::new_v4(), + ChallengeId::new(), + "hash".to_string(), + crate::Score::new(0.5, 1.0), + 100, + hotkey.clone(), + )); + + // StateSync + let _ = NetworkMessage::StateSync(StateSyncMessage::RequestSnapshot); + + // Heartbeat + let _ = NetworkMessage::Heartbeat(HeartbeatMessage::new(hotkey.clone(), 1, [0; 32])); + + // WeightCommitment + let _ = NetworkMessage::WeightCommitment(WeightCommitmentMessage::new( + hotkey.clone(), + ChallengeId::new(), + 1, + [0; 32], + )); + + // WeightReveal + let _ = NetworkMessage::WeightReveal(WeightRevealMessage::new( + hotkey.clone(), + ChallengeId::new(), + 1, + vec![], + vec![1, 2, 3], + )); + + // EpochTransition + let _ = NetworkMessage::EpochTransition(EpochTransitionMessage::new(1, "commit", 100)); + + // AgentSubmission + let _ = NetworkMessage::AgentSubmission(AgentSubmissionMessage::new( + "test".to_string(), + "hash".to_string(), + "miner".to_string(), + None, + hotkey.clone(), + )); + + // ChallengeMessage + let _ = NetworkMessage::ChallengeMessage(ChallengeNetworkMessage { + challenge_id: "test".to_string(), + payload: vec![], + message_type: ChallengeMessageType::EvaluationResult, + }); + + // TaskProgress (already covered above via TaskProgress variant) + + // AgentLogProposal + let _ = NetworkMessage::AgentLogProposal(AgentLogProposalMessage { + submission_id: "sub-1".to_string(), + challenge_id: "challenge-1".to_string(), + miner_hotkey: "miner-1".to_string(), + logs_hash: [0u8; 32], + logs_data: vec![1, 2, 3], + validator_hotkey: "validator-1".to_string(), + epoch: 1, + }); + + // VersionMismatch + let _ = NetworkMessage::VersionMismatch { + our_version: "0.1.0".to_string(), + required_min_version: "0.2.0".to_string(), + }; + } + + // ========================================================================= + // Docker Image Whitelist Tests + // ========================================================================= + + #[test] + fn test_task_progress_message_new() { + let msg = TaskProgressMessage::new( + "test-challenge".to_string(), + "agent-hash".to_string(), + "eval-123".to_string(), + "task-1".to_string(), + 1, + 10, + true, + 0.95, + 1500, + 0.002, + None, + "validator-key".to_string(), + ); + assert_eq!(msg.challenge_id, "test-challenge"); + assert_eq!(msg.task_index, 1); + assert_eq!(msg.total_tasks, 10); + assert!(msg.passed); + assert_eq!(msg.score, 0.95); + assert!(msg.timestamp > 0); + } + + #[test] + fn test_mechanism_weight_config_new() { + let config = MechanismWeightConfig::new(5); + assert_eq!(config.mechanism_id, 5); + assert_eq!(config.base_burn_rate, 0.0); + assert!(config.equal_distribution); + assert!(config.active); + } + + #[test] + fn test_mechanism_weight_config_with_burn_rate() { + let config = MechanismWeightConfig::new(1).with_burn_rate(0.15); + assert_eq!(config.base_burn_rate, 0.15); + } + + #[test] + fn test_mechanism_weight_config_with_max_cap() { + let config = MechanismWeightConfig::new(1).with_max_cap(0.8); + assert_eq!(config.max_weight_cap, 0.8); + } + + #[test] + fn test_challenge_weight_allocation_new() { + let challenge_id = ChallengeId::new(); + let allocation = ChallengeWeightAllocation::new(challenge_id, 1, 0.7); + assert_eq!(allocation.challenge_id, challenge_id); + assert_eq!(allocation.mechanism_id, 1); + assert_eq!(allocation.weight_ratio, 0.7); + assert!(allocation.active); + } + + #[test] + fn test_mechanism_weight_config_default() { + let config = MechanismWeightConfig::default(); + assert_eq!(config.mechanism_id, 0); + assert_eq!(config.base_burn_rate, 0.0); + assert!(config.equal_distribution); + assert!(config.active); + } +} diff --git a/crates/core/src/restoration.rs b/crates/core/src/restoration.rs new file mode 100644 index 000000000..790d9ef9f --- /dev/null +++ b/crates/core/src/restoration.rs @@ -0,0 +1,618 @@ +//! State restoration system for crash/update recovery +//! +//! Handles restoring validator state from checkpoints, including: +//! - Automatic restoration on startup +//! - State validation and migration +//! - Partial recovery handling + +use crate::checkpoint::{CheckpointData, CheckpointManager}; +use crate::{ChallengeId, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; +use std::path::Path; +use std::time::{Duration, Instant}; +use tracing::{debug, info, warn}; + +/// Result of a restoration operation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RestorationResult { + /// Whether restoration was successful + pub success: bool, + /// Sequence number restored from + pub checkpoint_sequence: u64, + /// Epoch restored to + pub epoch: u64, + /// Number of pending evaluations restored + pub pending_evaluations_count: usize, + /// Number of completed evaluations restored + pub completed_evaluations_count: usize, + /// Whether weight votes were restored + pub weight_votes_restored: bool, + /// Time taken for restoration + pub duration_ms: u64, + /// Any warnings during restoration + pub warnings: Vec, + /// Error message if failed + pub error: Option, +} + +impl RestorationResult { + pub fn success( + checkpoint_sequence: u64, + epoch: u64, + pending_count: usize, + completed_count: usize, + weight_votes: bool, + duration_ms: u64, + ) -> Self { + Self { + success: true, + checkpoint_sequence, + epoch, + pending_evaluations_count: pending_count, + completed_evaluations_count: completed_count, + weight_votes_restored: weight_votes, + duration_ms, + warnings: Vec::new(), + error: None, + } + } + + pub fn failure(error: String) -> Self { + Self { + success: false, + checkpoint_sequence: 0, + epoch: 0, + pending_evaluations_count: 0, + completed_evaluations_count: 0, + weight_votes_restored: false, + duration_ms: 0, + warnings: Vec::new(), + error: Some(error), + } + } + + pub fn add_warning(&mut self, warning: String) { + self.warnings.push(warning); + } +} + +/// Options for restoration +#[derive(Clone, Debug)] +pub struct RestorationOptions { + /// Maximum age of checkpoint to restore from (None = any age) + pub max_age: Option, + /// Whether to validate restored state + pub validate_state: bool, + /// Whether to skip pending evaluations older than threshold + pub skip_stale_evaluations: bool, + /// Threshold for stale evaluations (in epochs) + pub stale_evaluation_threshold: u64, + /// Challenge IDs to restore (None = all) + pub challenge_filter: Option>, +} + +impl Default for RestorationOptions { + fn default() -> Self { + Self { + max_age: Some(Duration::from_secs(24 * 60 * 60)), // 24 hours + validate_state: true, + skip_stale_evaluations: true, + stale_evaluation_threshold: 5, // Skip if > 5 epochs old + challenge_filter: None, + } + } +} + +impl RestorationOptions { + pub fn new() -> Self { + Self::default() + } + + pub fn with_max_age(mut self, age: Duration) -> Self { + self.max_age = Some(age); + self + } + + pub fn without_max_age(mut self) -> Self { + self.max_age = None; + self + } + + pub fn with_validation(mut self, validate: bool) -> Self { + self.validate_state = validate; + self + } + + pub fn with_challenge_filter(mut self, challenges: HashSet) -> Self { + self.challenge_filter = Some(challenges); + self + } +} + +/// State restoration manager +pub struct RestorationManager { + checkpoint_manager: CheckpointManager, + options: RestorationOptions, +} + +impl RestorationManager { + /// Create a new restoration manager + pub fn new>(checkpoint_dir: P, options: RestorationOptions) -> Result { + let checkpoint_manager = CheckpointManager::new(checkpoint_dir, 10)?; + Ok(Self { + checkpoint_manager, + options, + }) + } + + /// Create with default options + pub fn with_defaults>(checkpoint_dir: P) -> Result { + Self::new(checkpoint_dir, RestorationOptions::default()) + } + + /// Attempt to restore from the latest checkpoint + pub fn restore_latest(&self) -> Result> { + let start = Instant::now(); + + // Load latest checkpoint + let checkpoint = match self.checkpoint_manager.load_latest()? { + Some(cp) => cp, + None => { + info!("No checkpoint found, starting fresh"); + return Ok(None); + } + }; + + let (header, data) = checkpoint; + + // Check checkpoint age + if let Some(max_age) = self.options.max_age { + let checkpoint_age = Duration::from_millis( + (chrono::Utc::now().timestamp_millis() - header.created_at).max(0) as u64, + ); + if checkpoint_age > max_age { + warn!( + sequence = header.sequence, + age_secs = checkpoint_age.as_secs(), + max_age_secs = max_age.as_secs(), + "Checkpoint too old, skipping restoration" + ); + return Ok(None); + } + } + + // Filter and validate data + let filtered_data = self.filter_and_validate(data)?; + + let duration_ms = start.elapsed().as_millis() as u64; + + let mut result = RestorationResult::success( + header.sequence, + filtered_data.epoch, + filtered_data.pending_evaluations.len(), + filtered_data.completed_evaluations.len(), + filtered_data.weight_votes.is_some(), + duration_ms, + ); + + info!( + sequence = header.sequence, + epoch = filtered_data.epoch, + pending = filtered_data.pending_evaluations.len(), + duration_ms, + "State restored from checkpoint" + ); + + // Add warnings for filtered items + if self.options.challenge_filter.is_some() { + result.add_warning("Some evaluations filtered by challenge".into()); + } + + Ok(Some((result, filtered_data))) + } + + /// Restore from a specific checkpoint sequence + pub fn restore_from_sequence( + &self, + sequence: u64, + ) -> Result> { + let start = Instant::now(); + + let checkpoint = match self.checkpoint_manager.load_checkpoint(sequence)? { + Some(cp) => cp, + None => { + warn!(sequence, "Checkpoint not found"); + return Ok(None); + } + }; + + let (header, data) = checkpoint; + let filtered_data = self.filter_and_validate(data)?; + let duration_ms = start.elapsed().as_millis() as u64; + + let result = RestorationResult::success( + header.sequence, + filtered_data.epoch, + filtered_data.pending_evaluations.len(), + filtered_data.completed_evaluations.len(), + filtered_data.weight_votes.is_some(), + duration_ms, + ); + + Ok(Some((result, filtered_data))) + } + + /// Filter and validate checkpoint data + fn filter_and_validate(&self, mut data: CheckpointData) -> Result { + // Filter by challenge if specified + if let Some(ref filter) = self.options.challenge_filter { + data.pending_evaluations + .retain(|e| filter.contains(&e.challenge_id)); + data.completed_evaluations + .retain(|e| filter.contains(&e.challenge_id)); + } + + // Skip stale evaluations if enabled + if self.options.skip_stale_evaluations { + let _current_epoch = data.epoch; + let _threshold = self.options.stale_evaluation_threshold; + + let original_count = data.pending_evaluations.len(); + data.pending_evaluations.retain(|_e| { + // Keep if we can't determine staleness or if within threshold + // For now, keep all pending (they don't have epoch info) + true + }); + + let filtered_count = original_count - data.pending_evaluations.len(); + if filtered_count > 0 { + debug!( + filtered = filtered_count, + "Skipped stale pending evaluations" + ); + } + } + + // Validate state if enabled + if self.options.validate_state { + self.validate_data(&data)?; + } + + Ok(data) + } + + /// Validate checkpoint data integrity + fn validate_data(&self, data: &CheckpointData) -> Result<()> { + // Validate epoch is reasonable + if data.epoch > 1_000_000 { + return Err(MiniChainError::Validation( + "Checkpoint epoch seems unreasonably high".into(), + )); + } + + // Validate netuid + if data.netuid == 0 { + warn!("Checkpoint has netuid 0, may need reconfiguration"); + } + + // Validate pending evaluations + for eval in &data.pending_evaluations { + if eval.submission_id.is_empty() { + return Err(MiniChainError::Validation( + "Found pending evaluation with empty submission_id".into(), + )); + } + } + + // Validate weight votes epoch matches + if let Some(ref votes) = data.weight_votes { + if votes.epoch != data.epoch && !votes.finalized { + warn!( + votes_epoch = votes.epoch, + data_epoch = data.epoch, + "Weight votes epoch mismatch (may be stale)" + ); + } + } + + Ok(()) + } + + /// Get list of available checkpoints for restoration + pub fn list_available(&self) -> Result> { + let checkpoints = self.checkpoint_manager.list_checkpoints()?; + + let mut infos = Vec::new(); + for (sequence, _path, _modified) in checkpoints { + if let Some(info) = self.get_checkpoint_info(sequence)? { + infos.push(info); + } + } + + Ok(infos) + } + + /// Get information about a specific checkpoint without full loading + fn get_checkpoint_info(&self, sequence: u64) -> Result> { + match self.checkpoint_manager.load_checkpoint(sequence)? { + Some((header, data)) => Ok(Some(CheckpointInfo { + sequence, + created_at: header.created_at, + epoch: data.epoch, + netuid: data.netuid, + pending_count: data.pending_evaluations.len(), + completed_count: data.completed_evaluations.len(), + has_weight_votes: data.weight_votes.is_some(), + bittensor_block: data.bittensor_block, + })), + None => Ok(None), + } + } + + /// Get the checkpoint manager + pub fn checkpoint_manager(&self) -> &CheckpointManager { + &self.checkpoint_manager + } +} + +/// Information about a checkpoint (lightweight summary) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CheckpointInfo { + pub sequence: u64, + pub created_at: i64, + pub epoch: u64, + pub netuid: u16, + pub pending_count: usize, + pub completed_count: usize, + pub has_weight_votes: bool, + pub bittensor_block: u64, +} + +/// Trait for types that can be restored from checkpoints +pub trait Restorable { + /// Restore state from checkpoint data + fn restore_from(&mut self, data: &CheckpointData) -> Result<()>; + + /// Create checkpoint data from current state + fn create_checkpoint(&self) -> Result; +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::checkpoint::PendingEvaluationState; + use crate::Hotkey; + use std::collections::HashMap; + use tempfile::tempdir; + + fn create_test_checkpoint_data() -> CheckpointData { + let mut data = CheckpointData::new(1, 5, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "sub1".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "hash1".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + data + } + + #[test] + fn test_restoration_result() { + let result = RestorationResult::success(1, 5, 10, 20, true, 100); + assert!(result.success); + assert_eq!(result.checkpoint_sequence, 1); + assert_eq!(result.epoch, 5); + + let failure = RestorationResult::failure("test error".to_string()); + assert!(!failure.success); + assert!(failure.error.is_some()); + } + + #[test] + fn test_restoration_options() { + let opts = RestorationOptions::default(); + assert!(opts.max_age.is_some()); + assert!(opts.validate_state); + + let custom = RestorationOptions::new() + .without_max_age() + .with_validation(false); + assert!(custom.max_age.is_none()); + assert!(!custom.validate_state); + } + + #[test] + fn test_restoration_roundtrip() { + let dir = tempdir().unwrap(); + + // Create checkpoint first + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + let data = create_test_checkpoint_data(); + manager.create_checkpoint(&data).unwrap(); + + // Now restore + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + let result = restoration.restore_latest().unwrap(); + + assert!(result.is_some()); + let (res, restored_data) = result.unwrap(); + assert!(res.success); + assert_eq!(restored_data.epoch, data.epoch); + assert_eq!(restored_data.pending_evaluations.len(), 1); + } + + #[test] + fn test_restoration_no_checkpoint() { + let dir = tempdir().unwrap(); + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + let result = restoration.restore_latest().unwrap(); + assert!(result.is_none()); + } + + #[test] + fn test_checkpoint_info() { + let dir = tempdir().unwrap(); + + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + let data = create_test_checkpoint_data(); + manager.create_checkpoint(&data).unwrap(); + + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + let infos = restoration.list_available().unwrap(); + + assert_eq!(infos.len(), 1); + assert_eq!(infos[0].epoch, 5); + assert_eq!(infos[0].pending_count, 1); + } + + #[test] + fn test_restoration_with_challenge_filter() { + let dir = tempdir().unwrap(); + + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + + let mut data = CheckpointData::new(1, 5, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "sub1".to_string(), + challenge_id: challenge1, + miner: Hotkey([1u8; 32]), + submission_hash: "hash1".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "sub2".to_string(), + challenge_id: challenge2, + miner: Hotkey([2u8; 32]), + submission_hash: "hash2".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + manager.create_checkpoint(&data).unwrap(); + + // Restore with filter for only challenge1 + let mut filter = HashSet::new(); + filter.insert(challenge1); + let options = RestorationOptions::new().with_challenge_filter(filter); + let restoration = RestorationManager::new(dir.path(), options).unwrap(); + let result = restoration.restore_latest().unwrap(); + + assert!(result.is_some()); + let (_res, restored_data) = result.unwrap(); + assert_eq!(restored_data.pending_evaluations.len(), 1); + assert_eq!( + restored_data.pending_evaluations[0].challenge_id, + challenge1 + ); + } + + #[test] + fn test_restoration_add_warning() { + let mut result = RestorationResult::success(1, 5, 10, 20, true, 100); + assert!(result.warnings.is_empty()); + + result.add_warning("Test warning".to_string()); + assert_eq!(result.warnings.len(), 1); + assert_eq!(result.warnings[0], "Test warning"); + } + + #[test] + fn test_restore_from_sequence() { + let dir = tempdir().unwrap(); + + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + + // Create multiple checkpoints + let mut data = create_test_checkpoint_data(); + manager.create_checkpoint(&data).unwrap(); // seq 1 + + data.epoch = 10; + manager.create_checkpoint(&data).unwrap(); // seq 2 + + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + + // Restore from sequence 1 + let result = restoration.restore_from_sequence(1).unwrap(); + assert!(result.is_some()); + let (_res, restored_data) = result.unwrap(); + assert_eq!(restored_data.epoch, 5); + + // Restore from sequence 2 + let result = restoration.restore_from_sequence(2).unwrap(); + assert!(result.is_some()); + let (_res, restored_data) = result.unwrap(); + assert_eq!(restored_data.epoch, 10); + + // Try non-existent sequence + let result = restoration.restore_from_sequence(999).unwrap(); + assert!(result.is_none()); + } + + #[test] + fn test_validation_unreasonable_epoch() { + let dir = tempdir().unwrap(); + + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + let mut data = create_test_checkpoint_data(); + data.epoch = 2_000_000; // Unreasonably high + manager.create_checkpoint(&data).unwrap(); + + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + let result = restoration.restore_latest(); + assert!(result.is_err()); + } + + #[test] + fn test_validation_empty_submission_id() { + let dir = tempdir().unwrap(); + + let mut manager = CheckpointManager::new(dir.path(), 5).unwrap(); + let mut data = CheckpointData::new(1, 5, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "".to_string(), // Empty - invalid + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "hash1".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + manager.create_checkpoint(&data).unwrap(); + + let restoration = RestorationManager::with_defaults(dir.path()).unwrap(); + let result = restoration.restore_latest(); + assert!(result.is_err()); + } + + #[test] + fn test_options_with_max_age() { + let opts = RestorationOptions::new().with_max_age(Duration::from_secs(3600)); + assert_eq!(opts.max_age, Some(Duration::from_secs(3600))); + } + + #[test] + fn test_checkpoint_info_struct() { + let info = CheckpointInfo { + sequence: 1, + created_at: 12345, + epoch: 5, + netuid: 1, + pending_count: 10, + completed_count: 20, + has_weight_votes: true, + bittensor_block: 100, + }; + + assert_eq!(info.sequence, 1); + assert_eq!(info.epoch, 5); + assert!(info.has_weight_votes); + } +} diff --git a/crates/core/src/schema_guard.rs b/crates/core/src/schema_guard.rs new file mode 100644 index 000000000..1f633ba0a --- /dev/null +++ b/crates/core/src/schema_guard.rs @@ -0,0 +1,523 @@ +//! Schema Guard - Compile-time and runtime protection against state corruption +//! +//! This module ensures that ANY change to serializable state structs: +//! 1. Is detected at compile time via schema hash +//! 2. Requires explicit version bump +//! 3. Requires migration code +//! 4. Is tested automatically +//! +//! HOW IT WORKS: +//! - Each serializable struct has a SCHEMA_HASH constant +//! - The hash is computed from field names, types, and order +//! - If you change a struct, the hash changes +//! - Tests will FAIL until you: +//! 1. Bump CURRENT_STATE_VERSION +//! 2. Add migration code +//! 3. Update EXPECTED_SCHEMA_HASHES + +use std::collections::BTreeMap; + +/// Schema hash for a type - computed from field layout +pub trait SchemaHash { + /// Returns a deterministic hash of the struct's schema + fn schema_hash() -> u64; + + /// Returns human-readable schema description for debugging + fn schema_description() -> String; +} + +/// Compute a simple but deterministic hash from a string +const fn const_hash(s: &str) -> u64 { + let bytes = s.as_bytes(); + let mut hash: u64 = 0xcbf29ce484222325; // FNV-1a offset basis + let mut i = 0; + while i < bytes.len() { + hash ^= bytes[i] as u64; + hash = hash.wrapping_mul(0x100000001b3); // FNV-1a prime + i += 1; + } + hash +} + +// ============================================================================ +// Schema Hashes for Core Types +// ============================================================================ + +/// ValidatorInfo schema hash +/// IMPORTANT: Update this if you change ValidatorInfo fields! +impl SchemaHash for crate::ValidatorInfo { + fn schema_hash() -> u64 { + // Hash is computed from: struct_name + field_name:type pairs in order + const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option") + } + + fn schema_description() -> String { + "ValidatorInfo { hotkey: Hotkey, stake: Stake, is_active: bool, last_seen: DateTime, peer_id: Option, x25519_pubkey: Option }".to_string() + } +} + +/// ChainState schema hash +impl SchemaHash for crate::ChainState { + fn schema_hash() -> u64 { + const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,wasm_challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet,network_stopped:bool,network_stop_reason:Option") + } + + fn schema_description() -> String { + "ChainState { block_height, epoch, config, sudo_key, validators, challenges, wasm_challenge_configs, mechanism_configs, challenge_weights, required_version, pending_jobs, state_hash, last_updated, registered_hotkeys, network_stopped, network_stop_reason }".to_string() + } +} + +// ============================================================================ +// Expected Schema Registry +// ============================================================================ + +/// Registry of expected schema hashes for each version +/// +/// WHEN ADDING A NEW VERSION: +/// 1. Add entry for new version with current schema hashes +/// 2. Keep old version entries for migration testing +pub fn expected_schema_hashes() -> BTreeMap { + let mut registry = BTreeMap::new(); + + // Version 1: Original schema (no registered_hotkeys, no x25519_pubkey) + registry.insert(1, SchemaRegistry { + version: 1, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime"), + description: "Original schema without registered_hotkeys or x25519_pubkey", + }); + + // Version 2: Added registered_hotkeys to ChainState + registry.insert(2, SchemaRegistry { + version: 2, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet"), + description: "Added registered_hotkeys to ChainState", + }); + + // Version 3: Added x25519_pubkey to ValidatorInfo + registry.insert(3, SchemaRegistry { + version: 3, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet"), + description: "Added x25519_pubkey to ValidatorInfo", + }); + + // Version 4: Added wasm_challenge_configs to ChainState + registry.insert(4, SchemaRegistry { + version: 4, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,challenge_configs:HashMap,wasm_challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet"), + description: "Added wasm_challenge_configs to ChainState", + }); + + // Version 5: Added WASM restart metadata + registry.insert(5, SchemaRegistry { + version: 5, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,challenge_configs:HashMap,wasm_challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet"), + description: "Added WASM restart metadata", + }); + + // Version 6: Removed docker challenge configs + registry.insert(6, SchemaRegistry { + version: 6, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,wasm_challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet"), + description: "Removed docker challenge configs", + }); + + // Version 7: Added network_stopped, network_stop_reason + registry.insert(7, SchemaRegistry { + version: 7, + validator_info_hash: const_hash("ValidatorInfo:hotkey:Hotkey,stake:Stake,is_active:bool,last_seen:DateTime,peer_id:Option,x25519_pubkey:Option"), + chain_state_hash: const_hash("ChainState:block_height:u64,epoch:u64,config:NetworkConfig,sudo_key:Hotkey,validators:HashMap,challenges:HashMap,wasm_challenge_configs:HashMap,mechanism_configs:HashMap,challenge_weights:HashMap,required_version:Option,pending_jobs:Vec,state_hash:[u8;32],last_updated:DateTime,registered_hotkeys:HashSet,network_stopped:bool,network_stop_reason:Option"), + description: "Added network_stopped and network_stop_reason", + }); + + registry +} + +/// Schema registry entry for a specific version +#[derive(Debug, Clone)] +pub struct SchemaRegistry { + pub version: u32, + pub validator_info_hash: u64, + pub chain_state_hash: u64, + pub description: &'static str, +} + +// ============================================================================ +// Verification Functions +// ============================================================================ + +/// Verify that current schema matches expected for current version +/// +/// This function should be called at startup and in tests. +/// It will panic if schema doesn't match, preventing data corruption. +pub fn verify_schema_integrity() -> Result<(), SchemaError> { + use crate::state_versioning::CURRENT_STATE_VERSION; + + let registry = expected_schema_hashes(); + + // Get expected hashes for current version + let expected = + registry + .get(&CURRENT_STATE_VERSION) + .ok_or_else(|| SchemaError::MissingVersion { + version: CURRENT_STATE_VERSION, + hint: format!( + "Version {} is not registered in schema_guard.rs. \ + Add an entry to expected_schema_hashes() with the current schema hashes.", + CURRENT_STATE_VERSION + ), + })?; + + // Verify ValidatorInfo schema + let actual_validator_hash = ::schema_hash(); + if actual_validator_hash != expected.validator_info_hash { + return Err(SchemaError::SchemaMismatch { + type_name: "ValidatorInfo", + expected_hash: expected.validator_info_hash, + actual_hash: actual_validator_hash, + current_version: CURRENT_STATE_VERSION, + hint: format!( + "ValidatorInfo schema has changed but version is still {}!\n\ + \n\ + TO FIX THIS:\n\ + 1. Bump CURRENT_STATE_VERSION in state_versioning.rs\n\ + 2. Add migration code in migrate_state()\n\ + 3. Add new version entry in expected_schema_hashes()\n\ + 4. Update ValidatorInfoLegacy if needed\n\ + \n\ + Current schema: {}", + CURRENT_STATE_VERSION, + ::schema_description() + ), + }); + } + + // Verify ChainState schema + let actual_state_hash = ::schema_hash(); + if actual_state_hash != expected.chain_state_hash { + return Err(SchemaError::SchemaMismatch { + type_name: "ChainState", + expected_hash: expected.chain_state_hash, + actual_hash: actual_state_hash, + current_version: CURRENT_STATE_VERSION, + hint: format!( + "ChainState schema has changed but version is still {}!\n\ + \n\ + TO FIX THIS:\n\ + 1. Bump CURRENT_STATE_VERSION in state_versioning.rs\n\ + 2. Add migration code in migrate_state()\n\ + 3. Add new version entry in expected_schema_hashes()\n\ + 4. Create ChainStateVX struct for old version\n\ + \n\ + Current schema: {}", + CURRENT_STATE_VERSION, + ::schema_description() + ), + }); + } + + Ok(()) +} + +/// Verify that all migration paths exist and work +pub fn verify_migration_paths() -> Result<(), SchemaError> { + use crate::state_versioning::{CURRENT_STATE_VERSION, MIN_SUPPORTED_VERSION}; + + // Ensure we have registry entries for all supported versions + let registry = expected_schema_hashes(); + + for version in MIN_SUPPORTED_VERSION..=CURRENT_STATE_VERSION { + if !registry.contains_key(&version) { + return Err(SchemaError::MissingVersion { + version, + hint: format!( + "Version {} is between MIN_SUPPORTED_VERSION ({}) and CURRENT_STATE_VERSION ({}) \ + but has no entry in expected_schema_hashes(). Add the missing entry.", + version, MIN_SUPPORTED_VERSION, CURRENT_STATE_VERSION + ), + }); + } + } + + Ok(()) +} + +// ============================================================================ +// Error Types +// ============================================================================ + +#[derive(Debug)] +pub enum SchemaError { + SchemaMismatch { + type_name: &'static str, + expected_hash: u64, + actual_hash: u64, + current_version: u32, + hint: String, + }, + MissingVersion { + version: u32, + hint: String, + }, +} + +impl std::fmt::Display for SchemaError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SchemaError::SchemaMismatch { + type_name, + expected_hash, + actual_hash, + current_version, + hint, + } => { + write!( + f, + "\n\ + โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—\n\ + โ•‘ SCHEMA CHANGE DETECTED! โ•‘\n\ + โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ\n\ + โ•‘ Type: {:<58} โ•‘\n\ + โ•‘ Version: {:<55} โ•‘\n\ + โ•‘ Expected hash: {:<49} โ•‘\n\ + โ•‘ Actual hash: {:<49} โ•‘\n\ + โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ\n\ + โ•‘ {}โ•‘\n\ + โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•", + type_name, + current_version, + expected_hash, + actual_hash, + hint.lines() + .map(|l| format!("{:<64}\nโ•‘ ", l)) + .collect::() + ) + } + SchemaError::MissingVersion { version, hint } => { + write!( + f, + "\n\ + โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—\n\ + โ•‘ MISSING VERSION ENTRY! โ•‘\n\ + โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ\n\ + โ•‘ Version: {:<55} โ•‘\n\ + โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ\n\ + โ•‘ {}โ•‘\n\ + โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•", + version, + hint.lines() + .map(|l| format!("{:<64}\nโ•‘ ", l)) + .collect::() + ) + } + } + } +} + +impl std::error::Error for SchemaError {} + +// ============================================================================ +// Tests - These MUST pass for the build to succeed +// ============================================================================ + +#[cfg(test)] +mod tests { + use super::*; + + /// This test ENFORCES schema integrity. + /// If you change a serializable struct, this test will fail + /// and tell you exactly what to do. + #[test] + fn test_schema_integrity() { + if let Err(e) = verify_schema_integrity() { + panic!("{}", e); + } + } + + /// Verify all migration paths are registered + #[test] + fn test_migration_paths_registered() { + if let Err(e) = verify_migration_paths() { + panic!("{}", e); + } + } + + /// Test that schema hashes are deterministic + #[test] + fn test_schema_hash_deterministic() { + let hash1 = ::schema_hash(); + let hash2 = ::schema_hash(); + assert_eq!(hash1, hash2, "Schema hash must be deterministic"); + } + + /// Verify current version has correct hashes + #[test] + fn test_current_version_hashes() { + use crate::state_versioning::CURRENT_STATE_VERSION; + + let registry = expected_schema_hashes(); + let current = registry + .get(&CURRENT_STATE_VERSION) + .expect("Current version must have registry entry"); + + assert_eq!( + current.validator_info_hash, + ::schema_hash(), + "ValidatorInfo hash mismatch for version {}", + CURRENT_STATE_VERSION + ); + + assert_eq!( + current.chain_state_hash, + ::schema_hash(), + "ChainState hash mismatch for version {}", + CURRENT_STATE_VERSION + ); + } + + /// Test roundtrip serialization for current version + #[test] + fn test_current_version_serialization() { + use crate::crypto::Keypair; + use crate::{ChainState, NetworkConfig, Stake, ValidatorInfo}; + + // Create state with validators + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + for _ in 0..3 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(1_000_000_000)); + state.add_validator(info).unwrap(); + } + + // Serialize and deserialize + let data = crate::state_versioning::serialize_state_versioned(&state).unwrap(); + let loaded = crate::state_versioning::deserialize_state_smart(&data).unwrap(); + + assert_eq!(state.validators.len(), loaded.validators.len()); + assert_eq!(state.block_height, loaded.block_height); + } + + #[test] + fn test_validator_info_schema_description() { + let desc = ::schema_description(); + assert!(desc.contains("ValidatorInfo")); + assert!(desc.contains("hotkey")); + assert!(desc.contains("stake")); + } + + #[test] + fn test_chain_state_schema_description() { + let desc = ::schema_description(); + assert!(desc.contains("ChainState")); + assert!(desc.contains("block_height")); + } + + #[test] + fn test_schema_error_fmt() { + let err = SchemaError::SchemaMismatch { + type_name: "TestType", + expected_hash: 12345, + actual_hash: 67890, + current_version: 1, + hint: "Test hint".to_string(), + }; + let formatted = format!("{}", err); + assert!(formatted.contains("SCHEMA CHANGE DETECTED")); + assert!(formatted.contains("TestType")); + } + + #[test] + fn test_schema_error_missing_version_fmt() { + let err = SchemaError::MissingVersion { + version: 5, + hint: "Add version entry".to_string(), + }; + let formatted = format!("{}", err); + assert!(formatted.contains("MISSING VERSION ENTRY")); + assert!(formatted.contains("5")); + } + + #[test] + fn test_expected_schema_hashes_registry() { + let registry = expected_schema_hashes(); + assert!(registry.contains_key(&1)); + assert!(registry.contains_key(&2)); + assert!(registry.contains_key(&3)); + assert!(registry.contains_key(&4)); + + // Each version should have different descriptions + let v1 = registry.get(&1).unwrap(); + let v4 = registry.get(&4).unwrap(); + assert_ne!(v1.description, v4.description); + } + + #[test] + fn test_const_hash_deterministic() { + let h1 = const_hash("test string"); + let h2 = const_hash("test string"); + assert_eq!(h1, h2); + + // Different strings should have different hashes + let h3 = const_hash("different string"); + assert_ne!(h1, h3); + } + + #[test] + fn test_schema_mismatch_validator_info() { + // This test ensures the error path for ValidatorInfo schema mismatch works + // We can't easily trigger it in practice without modifying the struct, + // but we can verify the error formatting works + let err = SchemaError::SchemaMismatch { + type_name: "ValidatorInfo", + expected_hash: 12345, + actual_hash: 67890, + current_version: 3, + hint: "Test hint for validator".to_string(), + }; + let formatted = format!("{}", err); + assert!(formatted.contains("SCHEMA CHANGE DETECTED")); + assert!(formatted.contains("ValidatorInfo")); + assert!(formatted.contains("12345")); + assert!(formatted.contains("67890")); + } + + #[test] + fn test_schema_mismatch_chain_state() { + // Test the ChainState schema mismatch error path + let err = SchemaError::SchemaMismatch { + type_name: "ChainState", + expected_hash: 11111, + actual_hash: 22222, + current_version: 3, + hint: "Test hint for chain state".to_string(), + }; + let formatted = format!("{}", err); + assert!(formatted.contains("SCHEMA CHANGE DETECTED")); + assert!(formatted.contains("ChainState")); + assert!(formatted.contains("11111")); + assert!(formatted.contains("22222")); + } + + #[test] + fn test_missing_version_registry() { + // This should pass since all versions are registered + let result = verify_migration_paths(); + assert!(result.is_ok()); + + // Test the error formatting for missing version + let err = SchemaError::MissingVersion { + version: 99, + hint: "Version 99 is missing".to_string(), + }; + let formatted = format!("{}", err); + assert!(formatted.contains("MISSING VERSION ENTRY")); + assert!(formatted.contains("99")); + } +} diff --git a/crates/core/src/state.rs b/crates/core/src/state.rs new file mode 100644 index 000000000..5cc67cebd --- /dev/null +++ b/crates/core/src/state.rs @@ -0,0 +1,676 @@ +//! Chain state management + +use crate::{ + hash_data, BlockHeight, Challenge, ChallengeConfig, ChallengeId, ChallengeWeightAllocation, + Hotkey, Job, MechanismWeightConfig, NetworkConfig, Result, Stake, ValidatorInfo, + WasmChallengeConfig, +}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// Required validator version (set by Sudo) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RequiredVersion { + pub min_version: String, + pub recommended_version: String, + pub mandatory: bool, + pub deadline_block: Option, +} + +/// The complete chain state +/// +/// IMPORTANT: When adding new fields, ALWAYS add `#[serde(default)]` to ensure +/// backward compatibility with older serialized states. See state_versioning.rs +/// for migration logic. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct ChainState { + /// Current block height + pub block_height: BlockHeight, + + /// Current epoch + pub epoch: u64, + + /// Network configuration + pub config: NetworkConfig, + + /// Subnet owner (has sudo privileges) + pub sudo_key: Hotkey, + + /// Active validators + #[serde(default)] + pub validators: HashMap, + + /// Active challenges (legacy, for SDK-based challenges) + #[serde(default)] + pub challenges: HashMap, + + /// WASM challenge configurations (metadata only) + #[serde(default)] + pub wasm_challenge_configs: HashMap, + + /// Mechanism weight configurations (mechanism_id -> config) + #[serde(default)] + pub mechanism_configs: HashMap, + + /// Challenge weight allocations (challenge_id -> allocation) + #[serde(default)] + pub challenge_weights: HashMap, + + /// Required validator version + #[serde(default)] + pub required_version: Option, + + /// Pending jobs + #[serde(default)] + pub pending_jobs: Vec, + + /// State hash (for verification) + #[serde(default)] + pub state_hash: [u8; 32], + + /// Last update timestamp + #[serde(default = "default_timestamp")] + pub last_updated: chrono::DateTime, + + /// All registered hotkeys from metagraph (miners + validators) + /// Updated during metagraph sync, used for submission verification + /// Added in V2 + #[serde(default)] + pub registered_hotkeys: std::collections::HashSet, + + /// Whether the network is stopped (all emissions go to UID 0 burn) + #[serde(default)] + pub network_stopped: bool, + + /// Reason for network stop + #[serde(default)] + pub network_stop_reason: Option, +} + +fn default_timestamp() -> chrono::DateTime { + chrono::Utc::now() +} + +impl Default for ChainState { + fn default() -> Self { + Self { + block_height: 0, + epoch: 0, + config: NetworkConfig::default(), + sudo_key: Hotkey([0u8; 32]), + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [0u8; 32], + last_updated: chrono::Utc::now(), + registered_hotkeys: std::collections::HashSet::new(), + network_stopped: false, + network_stop_reason: None, + } + } +} + +impl ChainState { + /// Create a new chain state with a custom sudo key + pub fn new(sudo_key: Hotkey, config: NetworkConfig) -> Self { + let mut state = Self { + block_height: 0, + epoch: 0, + config, + sudo_key, + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [0u8; 32], + last_updated: chrono::Utc::now(), + registered_hotkeys: std::collections::HashSet::new(), + network_stopped: false, + network_stop_reason: None, + }; + state.update_hash(); + state + } + + /// Create a new chain state with the production sudo key + /// (Coldkey: 5GziQCcRpN8NCJktX343brnfuVe3w6gUYieeStXPD1Dag2At) + pub fn new_production(config: NetworkConfig) -> Self { + Self::new(crate::production_sudo_key(), config) + } + + /// Create a new chain state with production defaults + pub fn production_default() -> Self { + Self::new_production(NetworkConfig::production()) + } + + /// Update the state hash + pub fn update_hash(&mut self) { + #[derive(Serialize)] + struct HashInput<'a> { + block_height: BlockHeight, + sudo_key: &'a Hotkey, + validator_count: usize, + challenge_count: usize, + pending_jobs: usize, + } + + let input = HashInput { + block_height: self.block_height, + sudo_key: &self.sudo_key, + validator_count: self.validators.len(), + challenge_count: self.challenges.len(), + pending_jobs: self.pending_jobs.len(), + }; + + self.state_hash = hash_data(&input).unwrap_or([0u8; 32]); + self.last_updated = chrono::Utc::now(); + } + + /// Check if a hotkey is the sudo key + pub fn is_sudo(&self, hotkey: &Hotkey) -> bool { + self.sudo_key == *hotkey + } + + /// Add a validator + pub fn add_validator(&mut self, info: ValidatorInfo) -> Result<()> { + if self.validators.len() >= self.config.max_validators { + return Err(crate::MiniChainError::Consensus( + "Max validators reached".into(), + )); + } + if info.stake < self.config.min_stake { + return Err(crate::MiniChainError::Consensus( + "Insufficient stake".into(), + )); + } + self.validators.insert(info.hotkey.clone(), info); + self.update_hash(); + Ok(()) + } + + /// Remove a validator + pub fn remove_validator(&mut self, hotkey: &Hotkey) -> Option { + let removed = self.validators.remove(hotkey); + if removed.is_some() { + self.update_hash(); + } + removed + } + + /// Get validator by hotkey + pub fn get_validator(&self, hotkey: &Hotkey) -> Option<&ValidatorInfo> { + self.validators.get(hotkey) + } + + /// Get active validators + pub fn active_validators(&self) -> Vec<&ValidatorInfo> { + self.validators.values().filter(|v| v.is_active).collect() + } + + /// Total stake of active validators + pub fn total_stake(&self) -> Stake { + Stake(self.active_validators().iter().map(|v| v.stake.0).sum()) + } + + /// Calculate consensus threshold (number of validators needed) + pub fn consensus_threshold(&self) -> usize { + let active = self.active_validators().len(); + ((active as f64) * self.config.consensus_threshold).ceil() as usize + } + + /// Add a challenge + pub fn add_challenge(&mut self, challenge: Challenge) { + self.challenges.insert(challenge.id, challenge); + self.update_hash(); + } + + /// Remove a challenge + pub fn remove_challenge(&mut self, id: &ChallengeId) -> Option { + let removed = self.challenges.remove(id); + if removed.is_some() { + self.update_hash(); + } + removed + } + + /// Get challenge by ID + pub fn get_challenge(&self, id: &ChallengeId) -> Option<&Challenge> { + self.challenges.get(id) + } + + /// Add a pending job + pub fn add_job(&mut self, job: Job) { + self.pending_jobs.push(job); + self.update_hash(); + } + + /// Get next pending job for a validator + pub fn claim_job(&mut self, validator: &Hotkey) -> Option { + if let Some(pos) = self + .pending_jobs + .iter() + .position(|j| j.assigned_validator.is_none()) + { + let mut job = self.pending_jobs.remove(pos); + job.assigned_validator = Some(validator.clone()); + job.status = crate::JobStatus::Running; + self.update_hash(); + Some(job) + } else { + None + } + } + + /// Increment block height + pub fn increment_block(&mut self) { + self.block_height += 1; + self.update_hash(); + } + + /// Get a WASM challenge configuration by ID + pub fn get_wasm_challenge(&self, id: &ChallengeId) -> Option<&WasmChallengeConfig> { + self.wasm_challenge_configs.get(id) + } + + /// Register a WASM challenge configuration + pub fn register_wasm_challenge(&mut self, config: WasmChallengeConfig) { + self.wasm_challenge_configs + .insert(config.challenge_id, config); + self.update_hash(); + } + + /// List all WASM challenge configurations + pub fn list_wasm_challenges(&self) -> &HashMap { + &self.wasm_challenge_configs + } + + /// Add a challenge from a sudo action + pub fn add_challenge_from_sudo( + &mut self, + name: String, + description: String, + wasm_code: Vec, + owner: Hotkey, + config: ChallengeConfig, + weight: u16, + ) -> ChallengeId { + let challenge = Challenge::new(name, description, wasm_code, owner, config); + let id = challenge.id; + let wasm_config = WasmChallengeConfig::from(&challenge); + self.challenges.insert(id, challenge); + self.wasm_challenge_configs.insert(id, wasm_config); + if weight > 0 { + let allocation = ChallengeWeightAllocation::new(id, 0, weight as f64 / 100.0); + self.challenge_weights.insert(id, allocation); + } + self.update_hash(); + id + } + + /// Remove a challenge from a sudo action + pub fn remove_challenge_from_sudo(&mut self, challenge_id: &ChallengeId) -> bool { + let mut removed = false; + if let Some(challenge) = self.challenges.get_mut(challenge_id) { + challenge.is_active = false; + removed = true; + } + if let Some(wasm_config) = self.wasm_challenge_configs.get_mut(challenge_id) { + wasm_config.is_active = false; + } + if let Some(allocation) = self.challenge_weights.get_mut(challenge_id) { + allocation.active = false; + } + if removed { + self.update_hash(); + } + removed + } + + /// Edit a challenge from a sudo action + pub fn edit_challenge_from_sudo( + &mut self, + challenge_id: &ChallengeId, + name: Option, + description: Option, + wasm_code: Option>, + config: Option, + weight: Option, + ) -> bool { + let Some(challenge) = self.challenges.get_mut(challenge_id) else { + return false; + }; + if let Some(n) = name { + challenge.name = n.clone(); + if let Some(wc) = self.wasm_challenge_configs.get_mut(challenge_id) { + wc.name = n; + } + } + if let Some(d) = description { + challenge.description = d.clone(); + if let Some(wc) = self.wasm_challenge_configs.get_mut(challenge_id) { + wc.description = d; + } + } + if let Some(code) = wasm_code { + challenge.update_code(code); + if let Some(wc) = self.wasm_challenge_configs.get_mut(challenge_id) { + wc.module.code_hash = challenge.code_hash.clone(); + } + } + if let Some(c) = config { + challenge.config = c.clone(); + if let Some(wc) = self.wasm_challenge_configs.get_mut(challenge_id) { + wc.config = c; + } + } + if let Some(w) = weight { + if let Some(allocation) = self.challenge_weights.get_mut(challenge_id) { + allocation.weight_ratio = w as f64 / 100.0; + } else { + let allocation = ChallengeWeightAllocation::new(*challenge_id, 0, w as f64 / 100.0); + self.challenge_weights.insert(*challenge_id, allocation); + } + } + self.update_hash(); + true + } + + /// Stop the network - all emissions go to UID 0 (burn) + pub fn stop_network(&mut self, reason: String) { + self.network_stopped = true; + self.network_stop_reason = Some(reason); + self.update_hash(); + } + + /// Create a snapshot of the state + pub fn snapshot(&self) -> StateSnapshot { + StateSnapshot { + block_height: self.block_height, + state_hash: self.state_hash, + validator_count: self.validators.len(), + challenge_count: self.challenges.len(), + pending_jobs: self.pending_jobs.len(), + timestamp: self.last_updated, + } + } +} + +/// Lightweight state snapshot for sync +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateSnapshot { + pub block_height: BlockHeight, + pub state_hash: [u8; 32], + pub validator_count: usize, + pub challenge_count: usize, + pub pending_jobs: usize, + pub timestamp: chrono::DateTime, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ChallengeConfig, Keypair}; + + fn create_test_state() -> ChainState { + let sudo = Keypair::generate(); + ChainState::new(sudo.hotkey(), NetworkConfig::default()) + } + + #[test] + fn test_new_state() { + let state = create_test_state(); + assert_eq!(state.block_height, 0); + assert!(state.validators.is_empty()); + assert!(state.challenges.is_empty()); + } + + #[test] + fn test_add_validator() { + let mut state = create_test_state(); + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + + state.add_validator(info.clone()).unwrap(); + assert_eq!(state.validators.len(), 1); + assert!(state.get_validator(&kp.hotkey()).is_some()); + } + + #[test] + fn test_insufficient_stake() { + let mut state = create_test_state(); + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(100)); // Too low + + assert!(state.add_validator(info).is_err()); + } + + #[test] + fn test_consensus_threshold() { + let mut state = create_test_state(); + + // Add 8 validators + for _ in 0..8 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + state.add_validator(info).unwrap(); + } + + // 50% of 8 = 4 + assert_eq!(state.consensus_threshold(), 4); + } + + #[test] + fn test_state_hash_changes() { + let mut state = create_test_state(); + let hash1 = state.state_hash; + + state.increment_block(); + let hash2 = state.state_hash; + + assert_ne!(hash1, hash2); + } + + #[test] + fn test_remove_validator() { + let mut state = create_test_state(); + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + + state.add_validator(info).unwrap(); + assert_eq!(state.validators.len(), 1); + + state.remove_validator(&kp.hotkey()); + assert_eq!(state.validators.len(), 0); + } + + #[test] + fn test_add_challenge() { + let mut state = create_test_state(); + let challenge = Challenge::new( + "Test Challenge".to_string(), + "Description".to_string(), + vec![0u8; 100], + Keypair::generate().hotkey(), + ChallengeConfig::default(), + ); + + let id = challenge.id; + state.add_challenge(challenge); + assert!(state.get_challenge(&id).is_some()); + } + + #[test] + fn test_remove_challenge() { + let mut state = create_test_state(); + let challenge = Challenge::new( + "Test".to_string(), + "Desc".to_string(), + vec![0u8; 50], + Keypair::generate().hotkey(), + ChallengeConfig::default(), + ); + + let id = challenge.id; + state.add_challenge(challenge); + + let removed = state.remove_challenge(&id); + assert!(removed.is_some()); + assert!(state.get_challenge(&id).is_none()); + } + + #[test] + fn test_add_job() { + let mut state = create_test_state(); + let job = Job::new(ChallengeId::new(), "agent1".to_string()); + + state.add_job(job); + assert_eq!(state.pending_jobs.len(), 1); + } + + #[test] + fn test_claim_job() { + let mut state = create_test_state(); + let job = Job::new(ChallengeId::new(), "agent1".to_string()); + state.add_job(job); + + let kp = Keypair::generate(); + let claimed = state.claim_job(&kp.hotkey()); + assert!(claimed.is_some()); + assert_eq!(claimed.unwrap().assigned_validator, Some(kp.hotkey())); + assert_eq!(state.pending_jobs.len(), 0); + } + + #[test] + fn test_snapshot() { + let mut state = create_test_state(); + state.increment_block(); + + let snapshot = state.snapshot(); + assert_eq!(snapshot.block_height, 1); + assert_eq!(snapshot.validator_count, 0); + assert_eq!(snapshot.challenge_count, 0); + } + + #[test] + fn test_production_state() { + let state = ChainState::new_production(NetworkConfig::production()); + // Production sudo key should be set + assert!(!state.sudo_key.0.iter().all(|&b| b == 0)); + } + + #[test] + fn test_is_sudo() { + let sudo_kp = Keypair::generate(); + let state = ChainState::new(sudo_kp.hotkey(), NetworkConfig::default()); + + assert!(state.is_sudo(&sudo_kp.hotkey())); + + let other_kp = Keypair::generate(); + assert!(!state.is_sudo(&other_kp.hotkey())); + } + + #[test] + fn test_default_timestamp() { + let ts = default_timestamp(); + let now = chrono::Utc::now(); + // Should be within a few seconds of now + assert!((now.timestamp() - ts.timestamp()).abs() < 5); + } + + #[test] + fn test_production_default() { + let state = ChainState::production_default(); + assert_eq!(state.block_height, 0); + assert_eq!(state.config.subnet_id, 100); + assert!(!state.sudo_key.0.iter().all(|&b| b == 0)); + } + + #[test] + fn test_total_stake() { + let mut state = create_test_state(); + + // Add two validators with known stakes + let kp1 = Keypair::generate(); + let info1 = ValidatorInfo::new(kp1.hotkey(), Stake::new(1_000_000_000)); + state.add_validator(info1).unwrap(); + + let kp2 = Keypair::generate(); + let info2 = ValidatorInfo::new(kp2.hotkey(), Stake::new(2_000_000_000)); + state.add_validator(info2).unwrap(); + + let total = state.total_stake(); + assert_eq!(total.0, 3_000_000_000); + } + + #[test] + fn test_total_stake_only_active() { + let mut state = create_test_state(); + + // Add active validator + let kp1 = Keypair::generate(); + let info1 = ValidatorInfo::new(kp1.hotkey(), Stake::new(1_000_000_000)); + state.add_validator(info1).unwrap(); + + // Add inactive validator + let kp2 = Keypair::generate(); + let mut info2 = ValidatorInfo::new(kp2.hotkey(), Stake::new(2_000_000_000)); + info2.is_active = false; + state.validators.insert(kp2.hotkey(), info2); + + // Total should only include active + let total = state.total_stake(); + assert_eq!(total.0, 1_000_000_000); + } + + #[test] + fn test_add_validator_max_validators_reached() { + let mut state = create_test_state(); + // Set max validators to a small number + state.config.max_validators = 2; + + // Add validators up to the limit + for _ in 0..2 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + state.add_validator(info).unwrap(); + } + + // Try to add one more - should fail + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + let result = state.add_validator(info); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + crate::MiniChainError::Consensus(_) + )); + } + + #[test] + fn test_claim_job_none_available() { + let mut state = create_test_state(); + let kp = Keypair::generate(); + + // Try to claim a job when there are none + let result = state.claim_job(&kp.hotkey()); + assert!(result.is_none()); + + // Add a job and assign it + let job = Job::new(ChallengeId::new(), "agent1".to_string()); + state.add_job(job); + let claimed = state.claim_job(&kp.hotkey()); + assert!(claimed.is_some()); + + // Try to claim again - should return None since all jobs are assigned + let result2 = state.claim_job(&kp.hotkey()); + assert!(result2.is_none()); + } +} diff --git a/crates/core/src/state_versioning.rs b/crates/core/src/state_versioning.rs new file mode 100644 index 000000000..7d93065d7 --- /dev/null +++ b/crates/core/src/state_versioning.rs @@ -0,0 +1,653 @@ +//! State versioning and migration system +//! +//! This module provides backward-compatible state serialization with automatic +//! migration support. When ChainState structure changes between versions, +//! old data can still be loaded and migrated to the current format. +//! +//! # Usage +//! +//! Instead of directly serializing/deserializing ChainState, use: +//! - `VersionedState::from_state()` to wrap a ChainState for serialization +//! - `VersionedState::into_state()` to get the migrated ChainState +//! +//! # Adding a new version +//! +//! 1. Increment `CURRENT_STATE_VERSION` +//! 2. Keep the old `ChainStateVX` struct as-is (rename current to VX) +//! 3. Create new `ChainState` with your changes +//! 4. Implement migration in `migrate_state()` +//! 5. Add `#[serde(default)]` to any new fields + +use crate::{ + BlockHeight, Challenge, ChallengeId, ChallengeWeightAllocation, Hotkey, Job, + MechanismWeightConfig, NetworkConfig, Result, Stake, ValidatorInfo, WasmChallengeConfig, +}; +use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet}; +use tracing::{info, warn}; + +/// Current state version - increment when ChainState structure changes +/// V1: Original format (no registered_hotkeys) +/// V2: Added registered_hotkeys +/// V3: Added x25519_pubkey to ValidatorInfo +/// V4: Added wasm_challenge_configs +/// V5: Added WASM restart metadata +/// V6: Removed docker challenge configs +/// V7: Added network_stopped, network_stop_reason +pub const CURRENT_STATE_VERSION: u32 = 7; + +/// Minimum supported version for migration +pub const MIN_SUPPORTED_VERSION: u32 = 1; + +/// Versioned state wrapper for serialization +/// +/// This wrapper allows us to detect the version of serialized state and +/// migrate it to the current format automatically. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct VersionedState { + /// State format version + pub version: u32, + /// Serialized state data (version-specific format) + pub data: Vec, +} + +impl VersionedState { + /// Create a versioned state from current ChainState + pub fn from_state(state: &crate::ChainState) -> Result { + let data = bincode::serialize(state) + .map_err(|e| crate::MiniChainError::Serialization(e.to_string()))?; + Ok(Self { + version: CURRENT_STATE_VERSION, + data, + }) + } + + /// Deserialize and migrate to current ChainState + pub fn into_state(self) -> Result { + if self.version == CURRENT_STATE_VERSION { + // Current version - deserialize directly + bincode::deserialize(&self.data) + .map_err(|e| crate::MiniChainError::Serialization(e.to_string())) + } else if self.version >= MIN_SUPPORTED_VERSION { + // Old version - migrate + info!( + "Migrating state from version {} to {}", + self.version, CURRENT_STATE_VERSION + ); + migrate_state(self.version, &self.data) + } else { + Err(crate::MiniChainError::Serialization(format!( + "State version {} is too old (minimum supported: {})", + self.version, MIN_SUPPORTED_VERSION + ))) + } + } +} + +// ============================================================================ +// ValidatorInfo versions (for backward compatibility) +// ============================================================================ + +/// ValidatorInfo V1/V2 - without x25519_pubkey field +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorInfoLegacy { + pub hotkey: Hotkey, + pub stake: Stake, + pub is_active: bool, + pub last_seen: chrono::DateTime, + pub peer_id: Option, + // V1/V2 did NOT have x25519_pubkey +} + +impl ValidatorInfoLegacy { + /// Migrate to current ValidatorInfo + pub fn migrate(self) -> ValidatorInfo { + ValidatorInfo { + hotkey: self.hotkey, + stake: self.stake, + is_active: self.is_active, + last_seen: self.last_seen, + peer_id: self.peer_id, + x25519_pubkey: None, // New field in V3 + } + } +} + +// ============================================================================ +// Version 1 State (original format, before registered_hotkeys) +// ============================================================================ + +/// ChainState V1 - original format without registered_hotkeys +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChainStateV1 { + pub block_height: BlockHeight, + pub epoch: u64, + pub config: NetworkConfig, + pub sudo_key: Hotkey, + pub validators: HashMap, + pub challenges: HashMap, + #[serde(default)] + pub wasm_challenge_configs: HashMap, + pub mechanism_configs: HashMap, + pub challenge_weights: HashMap, + pub required_version: Option, + pub pending_jobs: Vec, + pub state_hash: [u8; 32], + pub last_updated: chrono::DateTime, + // V1 did NOT have registered_hotkeys +} + +impl ChainStateV1 { + /// Migrate V1 to current ChainState + pub fn migrate(self) -> crate::ChainState { + crate::ChainState { + block_height: self.block_height, + epoch: self.epoch, + config: self.config, + sudo_key: self.sudo_key, + validators: self + .validators + .into_iter() + .map(|(k, v)| (k, v.migrate())) + .collect(), + challenges: self.challenges, + wasm_challenge_configs: self.wasm_challenge_configs, + mechanism_configs: self.mechanism_configs, + challenge_weights: self.challenge_weights, + required_version: self.required_version, + pending_jobs: self.pending_jobs, + state_hash: self.state_hash, + last_updated: self.last_updated, + registered_hotkeys: HashSet::new(), // New in V2 + network_stopped: false, + network_stop_reason: None, + } + } +} + +// ============================================================================ +// Version 2 State (added registered_hotkeys, but ValidatorInfo without x25519_pubkey) +// ============================================================================ + +/// ChainState V2 - added registered_hotkeys, ValidatorInfo without x25519_pubkey +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChainStateV2 { + pub block_height: BlockHeight, + pub epoch: u64, + pub config: NetworkConfig, + pub sudo_key: Hotkey, + pub validators: HashMap, + pub challenges: HashMap, + #[serde(default)] + pub wasm_challenge_configs: HashMap, + pub mechanism_configs: HashMap, + pub challenge_weights: HashMap, + pub required_version: Option, + pub pending_jobs: Vec, + pub state_hash: [u8; 32], + pub last_updated: chrono::DateTime, + pub registered_hotkeys: HashSet, // Added in V2 +} + +impl ChainStateV2 { + /// Migrate V2 to current ChainState + pub fn migrate(self) -> crate::ChainState { + crate::ChainState { + block_height: self.block_height, + epoch: self.epoch, + config: self.config, + sudo_key: self.sudo_key, + validators: self + .validators + .into_iter() + .map(|(k, v)| (k, v.migrate())) + .collect(), + challenges: self.challenges, + wasm_challenge_configs: self.wasm_challenge_configs, + mechanism_configs: self.mechanism_configs, + challenge_weights: self.challenge_weights, + required_version: self.required_version, + pending_jobs: self.pending_jobs, + state_hash: self.state_hash, + last_updated: self.last_updated, + registered_hotkeys: self.registered_hotkeys, + network_stopped: false, + network_stop_reason: None, + } + } +} + +// ============================================================================ +// Migration Logic +// ============================================================================ + +/// Migrate state from an old version to current +fn migrate_state(version: u32, data: &[u8]) -> Result { + match version { + 1 => { + // V1 -> V7: Add registered_hotkeys, x25519_pubkey, wasm_challenge_configs + let v1: ChainStateV1 = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V1 migration failed: {}", e)) + })?; + info!( + "Migrated state V1->V7: block_height={}, validators={}", + v1.block_height, + v1.validators.len() + ); + Ok(v1.migrate()) + } + 2 => { + // V2 -> V7: Add x25519_pubkey to ValidatorInfo and wasm_challenge_configs + let v2: ChainStateV2 = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V2 migration failed: {}", e)) + })?; + info!( + "Migrated state V2->V7: block_height={}, validators={}", + v2.block_height, + v2.validators.len() + ); + Ok(v2.migrate()) + } + 3 => { + // V3 -> V7: Add wasm_challenge_configs + let mut v3: crate::ChainState = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V3 migration failed: {}", e)) + })?; + v3.wasm_challenge_configs = HashMap::new(); + info!( + "Migrated state V3->V7: block_height={}, validators={}", + v3.block_height, + v3.validators.len() + ); + Ok(v3) + } + 4 => { + // V4 -> V7: Added WASM restart metadata and removed docker configs + let v4: crate::ChainState = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V4 migration failed: {}", e)) + })?; + info!( + "Migrated state V4->V7: block_height={}, validators={}", + v4.block_height, + v4.validators.len() + ); + Ok(v4) + } + 5 => { + // V5 -> V7: Remove docker configs (handled by serde defaults) + let v5: crate::ChainState = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V5 migration failed: {}", e)) + })?; + info!( + "Migrated state V5->V7: block_height={}, validators={}", + v5.block_height, + v5.validators.len() + ); + Ok(v5) + } + 6 => { + // V6 -> V7: Added network_stopped, network_stop_reason (handled by serde defaults) + let v6: crate::ChainState = bincode::deserialize(data).map_err(|e| { + crate::MiniChainError::Serialization(format!("V6 migration failed: {}", e)) + })?; + info!( + "Migrated state V6->V7: block_height={}, validators={}", + v6.block_height, + v6.validators.len() + ); + Ok(v6) + } + _ => Err(crate::MiniChainError::Serialization(format!( + "Unknown state version: {}", + version + ))), + } +} + +// ============================================================================ +// Smart Deserialization (tries versioned first, then raw, then legacy) +// ============================================================================ + +/// Deserialize state with automatic version detection and migration +/// +/// This function tries multiple strategies to load state: +/// 1. Try as VersionedState (new format with version header) +/// 2. Try as current ChainState directly (for states saved without version) +/// 3. Try as ChainStateV2 (legacy format with registered_hotkeys but no x25519_pubkey) +/// 4. Try as ChainStateV1 (oldest format) +/// 5. Return error if all fail +pub fn deserialize_state_smart(data: &[u8]) -> Result { + // Strategy 1: Try as VersionedState (preferred format) + if let Ok(versioned) = bincode::deserialize::(data) { + return versioned.into_state(); + } + + // Strategy 2: Try as current ChainState (unversioned but current format) + if let Ok(state) = bincode::deserialize::(data) { + info!("Loaded unversioned state (current format)"); + return Ok(state); + } + + // Strategy 3: Try as V2 (with registered_hotkeys, without x25519_pubkey) + if let Ok(v2) = bincode::deserialize::(data) { + warn!("Loaded legacy V2 state, migrating..."); + return Ok(v2.migrate()); + } + + // Strategy 4: Try as V1 (oldest format without registered_hotkeys) + if let Ok(v1) = bincode::deserialize::(data) { + warn!("Loaded legacy V1 state, migrating..."); + return Ok(v1.migrate()); + } + + // All strategies failed + Err(crate::MiniChainError::Serialization( + "Failed to deserialize state: incompatible format".to_string(), + )) +} + +/// Serialize state with version header +pub fn serialize_state_versioned(state: &crate::ChainState) -> Result> { + let versioned = VersionedState::from_state(state)?; + bincode::serialize(&versioned).map_err(|e| crate::MiniChainError::Serialization(e.to_string())) +} + +// ============================================================================ +// Tests +// ============================================================================ + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Keypair, NetworkConfig}; + + fn create_test_state() -> crate::ChainState { + let sudo = Keypair::generate(); + crate::ChainState::new(sudo.hotkey(), NetworkConfig::default()) + } + + #[test] + fn test_versioned_roundtrip() { + let original = create_test_state(); + + // Serialize with version + let data = serialize_state_versioned(&original).unwrap(); + + // Deserialize + let loaded = deserialize_state_smart(&data).unwrap(); + + assert_eq!(original.block_height, loaded.block_height); + assert_eq!(original.epoch, loaded.epoch); + } + + #[test] + fn test_validator_info_roundtrip() { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + + // Test bincode roundtrip of just ValidatorInfo + let data = bincode::serialize(&info).unwrap(); + let loaded: ValidatorInfo = bincode::deserialize(&data).unwrap(); + + assert_eq!(info.hotkey, loaded.hotkey); + assert_eq!(info.stake, loaded.stake); + assert_eq!(info.x25519_pubkey, loaded.x25519_pubkey); + } + + #[test] + fn test_versioned_roundtrip_with_validators() { + let mut state = create_test_state(); + + // Add some validators + for _ in 0..4 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + state.add_validator(info).unwrap(); + } + + assert_eq!(state.validators.len(), 4); + + // First test: direct bincode roundtrip (should work) + let direct_data = bincode::serialize(&state).unwrap(); + let direct_loaded: crate::ChainState = bincode::deserialize(&direct_data).unwrap(); + assert_eq!(state.validators.len(), direct_loaded.validators.len()); + + // Second test: versioned roundtrip + let data = serialize_state_versioned(&state).unwrap(); + + // Deserialize + let loaded = deserialize_state_smart(&data).unwrap(); + + assert_eq!(state.block_height, loaded.block_height); + assert_eq!(state.validators.len(), loaded.validators.len()); + } + + #[test] + fn test_v1_migration() { + // Create a V1 state + let sudo = Keypair::generate(); + let v1 = ChainStateV1 { + block_height: 100, + epoch: 5, + config: NetworkConfig::default(), + sudo_key: sudo.hotkey(), + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [0u8; 32], + last_updated: chrono::Utc::now(), + }; + + // Serialize as V1 + let v1_data = bincode::serialize(&v1).unwrap(); + + // Wrap in VersionedState with version 1 + let versioned = VersionedState { + version: 1, + data: v1_data, + }; + let versioned_bytes = bincode::serialize(&versioned).unwrap(); + + // Load and migrate + let migrated = deserialize_state_smart(&versioned_bytes).unwrap(); + + assert_eq!(migrated.block_height, 100); + assert_eq!(migrated.epoch, 5); + assert!(migrated.registered_hotkeys.is_empty()); // New field initialized + } + + #[test] + fn test_legacy_v1_direct() { + // Test loading raw V1 data (no version wrapper) + let sudo = Keypair::generate(); + let v1 = ChainStateV1 { + block_height: 50, + epoch: 2, + config: NetworkConfig::default(), + sudo_key: sudo.hotkey(), + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [0u8; 32], + last_updated: chrono::Utc::now(), + }; + + // Serialize raw V1 (no version wrapper) + let raw_v1 = bincode::serialize(&v1).unwrap(); + + // Smart deserialize should detect and migrate + let migrated = deserialize_state_smart(&raw_v1).unwrap(); + + assert_eq!(migrated.block_height, 50); + } + + #[test] + fn test_version_constants() { + const _: () = assert!(CURRENT_STATE_VERSION >= MIN_SUPPORTED_VERSION); + assert_eq!(CURRENT_STATE_VERSION, 7); + } + + #[test] + fn test_validator_info_legacy_migrate() { + let kp = Keypair::generate(); + let legacy = ValidatorInfoLegacy { + hotkey: kp.hotkey(), + stake: Stake::new(5_000_000_000), + is_active: true, + last_seen: chrono::Utc::now(), + peer_id: Some("peer123".to_string()), + }; + + let migrated = legacy.migrate(); + assert_eq!(migrated.hotkey, kp.hotkey()); + assert_eq!(migrated.stake.0, 5_000_000_000); + assert!(migrated.x25519_pubkey.is_none()); + } + + #[test] + fn test_chainstate_v2_migrate() { + let sudo = Keypair::generate(); + let mut registered = HashSet::new(); + registered.insert(sudo.hotkey()); + + let v2 = ChainStateV2 { + block_height: 200, + epoch: 10, + config: NetworkConfig::default(), + sudo_key: sudo.hotkey(), + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [1u8; 32], + last_updated: chrono::Utc::now(), + registered_hotkeys: registered.clone(), + }; + + let migrated = v2.migrate(); + assert_eq!(migrated.block_height, 200); + assert_eq!(migrated.registered_hotkeys, registered); + } + + #[test] + fn test_deserialize_state_smart_v2() { + // Create V2 state and serialize it + let sudo = Keypair::generate(); + let v2 = ChainStateV2 { + block_height: 150, + epoch: 8, + config: NetworkConfig::default(), + sudo_key: sudo.hotkey(), + validators: HashMap::new(), + challenges: HashMap::new(), + wasm_challenge_configs: HashMap::new(), + mechanism_configs: HashMap::new(), + challenge_weights: HashMap::new(), + required_version: None, + pending_jobs: Vec::new(), + state_hash: [2u8; 32], + last_updated: chrono::Utc::now(), + registered_hotkeys: HashSet::new(), + }; + + let data = bincode::serialize(&v2).unwrap(); + let loaded = deserialize_state_smart(&data).unwrap(); + assert_eq!(loaded.block_height, 150); + } + + #[test] + fn test_deserialize_state_smart_current_format() { + let state = create_test_state(); + // Use versioned serialization (the proper way) + let data = serialize_state_versioned(&state).unwrap(); + let loaded = deserialize_state_smart(&data).unwrap(); + assert_eq!(loaded.block_height, state.block_height); + } + + #[test] + fn test_into_state_version_too_old() { + // Test the error path when version is too old + let versioned = VersionedState { + version: 0, // Version 0 is below MIN_SUPPORTED_VERSION (1) + data: vec![1, 2, 3], + }; + let result = versioned.into_state(); + assert!(result.is_err()); + match result.unwrap_err() { + crate::MiniChainError::Serialization(msg) => { + assert!(msg.contains("too old")); + assert!(msg.contains("minimum supported")); + } + _ => panic!("Expected Serialization error"), + } + } + + #[test] + fn test_migrate_state_v1_deserialization_error() { + // Test that V1 migration handles deserialization errors + let bad_data = vec![0xFF, 0xFF, 0xFF]; // Invalid bincode data + let result = migrate_state(1, &bad_data); + assert!(result.is_err()); + match result.unwrap_err() { + crate::MiniChainError::Serialization(msg) => { + assert!(msg.contains("V1 migration failed")); + } + _ => panic!("Expected Serialization error"), + } + } + + #[test] + fn test_migrate_state_v2_deserialization_error() { + // Test that V2 migration handles deserialization errors + let bad_data = vec![0xFF, 0xFF, 0xFF]; // Invalid bincode data + let result = migrate_state(2, &bad_data); + assert!(result.is_err()); + match result.unwrap_err() { + crate::MiniChainError::Serialization(msg) => { + assert!(msg.contains("V2 migration failed")); + } + _ => panic!("Expected Serialization error"), + } + } + + #[test] + fn test_migrate_state_unknown_version() { + // Test that unknown version returns error + let data = vec![1, 2, 3]; + let result = migrate_state(99, &data); + assert!(result.is_err()); + match result.unwrap_err() { + crate::MiniChainError::Serialization(msg) => { + assert!(msg.contains("Unknown state version")); + assert!(msg.contains("99")); + } + _ => panic!("Expected Serialization error"), + } + } + + #[test] + fn test_deserialize_state_smart_all_strategies_fail() { + // Test that when all deserialization strategies fail, we get proper error + let bad_data = vec![0xFF; 100]; // Completely invalid data + let result = deserialize_state_smart(&bad_data); + assert!(result.is_err()); + match result.unwrap_err() { + crate::MiniChainError::Serialization(msg) => { + assert!(msg.contains("Failed to deserialize state")); + assert!(msg.contains("incompatible format")); + } + _ => panic!("Expected Serialization error"), + } + } +} diff --git a/crates/core/src/types.rs b/crates/core/src/types.rs new file mode 100644 index 000000000..bc7cf4e5b --- /dev/null +++ b/crates/core/src/types.rs @@ -0,0 +1,522 @@ +//! Core types for the platform + +use serde::{Deserialize, Serialize}; +use std::fmt; + +/// Validator hotkey (32 bytes ed25519 public key) +#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Hotkey(pub [u8; 32]); + +impl Hotkey { + pub fn from_bytes(bytes: &[u8]) -> Option { + if bytes.len() == 32 { + let mut arr = [0u8; 32]; + arr.copy_from_slice(bytes); + Some(Self(arr)) + } else { + None + } + } + + pub fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } + + pub fn to_hex(&self) -> String { + hex::encode(self.0) + } + + pub fn from_hex(s: &str) -> Option { + hex::decode(s).ok().and_then(|b| Self::from_bytes(&b)) + } + + /// Convert to SS58 format (Substrate address format) + /// Uses network prefix 42 (generic Substrate) + pub fn to_ss58(&self) -> String { + use sp_core::crypto::Ss58Codec; + + // Use sp_core's SS58 encoding (proper blake2b checksum) + let public = sp_core::sr25519::Public::from_raw(self.0); + public.to_ss58check() + } + + /// Parse from SS58 address + pub fn from_ss58(address: &str) -> Option { + use sp_core::crypto::Ss58Codec; + + sp_core::sr25519::Public::from_ss58check(address) + .ok() + .map(|p| Hotkey(p.0)) + } +} + +impl fmt::Debug for Hotkey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Hotkey({}...)", &self.to_hex()[..8]) + } +} + +impl fmt::Display for Hotkey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}...{}", &self.to_hex()[..8], &self.to_hex()[56..]) + } +} + +/// Challenge ID +#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct ChallengeId(pub uuid::Uuid); + +impl ChallengeId { + pub fn new() -> Self { + Self(uuid::Uuid::new_v4()) + } + + pub fn from_uuid(uuid: uuid::Uuid) -> Self { + Self(uuid) + } + + pub fn from_string(s: &str) -> Self { + match uuid::Uuid::parse_str(s) { + Ok(uuid) => Self(uuid), + Err(_) => { + // If not a valid UUID, create one from hash of string + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + std::hash::Hash::hash(&s, &mut hasher); + let hash = std::hash::Hasher::finish(&hasher); + Self(uuid::Uuid::from_u64_pair(hash, hash.wrapping_mul(31))) + } + } + } +} + +impl Default for ChallengeId { + fn default() -> Self { + Self::new() + } +} + +impl fmt::Debug for ChallengeId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Challenge({})", &self.0.to_string()[..8]) + } +} + +impl fmt::Display for ChallengeId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +/// Block height +pub type BlockHeight = u64; + +/// Stake amount (in TAO units, with 9 decimals) +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)] +pub struct Stake(pub u64); + +impl Stake { + pub fn new(amount: u64) -> Self { + Self(amount) + } + + pub fn as_tao(&self) -> f64 { + self.0 as f64 / 1_000_000_000.0 + } +} + +/// Validator information +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorInfo { + pub hotkey: Hotkey, + pub stake: Stake, + pub is_active: bool, + pub last_seen: chrono::DateTime, + pub peer_id: Option, + /// X25519 public key for API key encryption (hex, 32 bytes) + #[serde(default)] + pub x25519_pubkey: Option, +} + +impl ValidatorInfo { + pub fn new(hotkey: Hotkey, stake: Stake) -> Self { + Self { + hotkey, + stake, + is_active: true, + last_seen: chrono::Utc::now(), + peer_id: None, + x25519_pubkey: None, + } + } +} + +/// Network configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct NetworkConfig { + pub subnet_id: u16, + pub min_stake: Stake, + pub consensus_threshold: f64, // 0.50 for 50%+1 + pub block_time_ms: u64, + pub max_validators: usize, + pub evaluation_timeout_secs: u64, +} + +impl Default for NetworkConfig { + fn default() -> Self { + Self { + subnet_id: 100, + min_stake: Stake::new(1_000_000_000), // 1 TAO minimum + consensus_threshold: 0.50, + block_time_ms: 12_000, // 12 seconds + max_validators: 64, + evaluation_timeout_secs: 300, + } + } +} + +impl NetworkConfig { + /// Production network configuration + pub fn production() -> Self { + Self { + subnet_id: 100, // Terminal Benchmark subnet + min_stake: Stake::new(1_000_000_000_000), // 1000 TAO minimum + consensus_threshold: 0.50, + block_time_ms: 12_000, // 12 seconds (Bittensor block time) + max_validators: 64, + evaluation_timeout_secs: 3600, // 1 hour for long evaluations + } + } +} + +/// Score from challenge evaluation +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +pub struct Score { + pub value: f64, + pub weight: f64, +} + +impl Score { + pub fn new(value: f64, weight: f64) -> Self { + Self { + value: value.clamp(0.0, 1.0), + weight: weight.clamp(0.0, 1.0), + } + } + + pub fn weighted_value(&self) -> f64 { + self.value * self.weight + } +} + +/// Job status +/// +/// IMPORTANT: Never reorder or remove variants! Add new ones at the end only. +/// Use explicit discriminants to ensure stable serialization. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[repr(u8)] +pub enum JobStatus { + Pending = 0, + Running = 1, + Completed = 2, + Failed = 3, + Timeout = 4, + // Add new variants here with next number +} + +/// Evaluation job +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Job { + pub id: uuid::Uuid, + pub challenge_id: ChallengeId, + pub agent_hash: String, + pub status: JobStatus, + pub created_at: chrono::DateTime, + pub assigned_validator: Option, + pub result: Option, +} + +impl Job { + pub fn new(challenge_id: ChallengeId, agent_hash: String) -> Self { + Self { + id: uuid::Uuid::new_v4(), + challenge_id, + agent_hash, + status: JobStatus::Pending, + created_at: chrono::Utc::now(), + assigned_validator: None, + result: None, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hotkey() { + let bytes = [1u8; 32]; + let hotkey = Hotkey(bytes); + let hex = hotkey.to_hex(); + let recovered = Hotkey::from_hex(&hex).unwrap(); + assert_eq!(hotkey, recovered); + } + + #[test] + fn test_hotkey_from_bytes() { + let bytes = [42u8; 32]; + let hotkey = Hotkey::from_bytes(&bytes).unwrap(); + assert_eq!(hotkey.as_bytes(), &bytes); + } + + #[test] + fn test_hotkey_from_bytes_invalid() { + let short = [1u8; 16]; + assert!(Hotkey::from_bytes(&short).is_none()); + } + + #[test] + fn test_hotkey_from_hex_invalid() { + assert!(Hotkey::from_hex("invalid").is_none()); + assert!(Hotkey::from_hex("0102").is_none()); // too short + } + + #[test] + fn test_hotkey_display() { + let hotkey = Hotkey([0xab; 32]); + let display = format!("{}", hotkey); + assert!(display.contains("abababab")); + } + + #[test] + fn test_hotkey_debug() { + let hotkey = Hotkey([0xcd; 32]); + let debug = format!("{:?}", hotkey); + assert!(debug.contains("Hotkey")); + } + + #[test] + fn test_stake() { + let stake = Stake::new(1_500_000_000); + assert_eq!(stake.as_tao(), 1.5); + } + + #[test] + fn test_stake_ordering() { + let s1 = Stake::new(100); + let s2 = Stake::new(200); + assert!(s1 < s2); + assert!(s2 > s1); + assert_eq!(s1, Stake::new(100)); + } + + #[test] + fn test_score() { + let score = Score::new(0.8, 0.5); + assert_eq!(score.weighted_value(), 0.4); + } + + #[test] + fn test_score_clamping() { + let score = Score::new(1.5, 2.0); + assert_eq!(score.value, 1.0); + assert_eq!(score.weight, 1.0); + + let score2 = Score::new(-0.5, -1.0); + assert_eq!(score2.value, 0.0); + assert_eq!(score2.weight, 0.0); + } + + #[test] + fn test_challenge_id() { + let id1 = ChallengeId::new(); + let id2 = ChallengeId::new(); + assert_ne!(id1, id2); + } + + #[test] + fn test_challenge_id_from_string() { + let id = ChallengeId::from_string("test-challenge"); + let id2 = ChallengeId::from_string("test-challenge"); + assert_eq!(id, id2); + } + + #[test] + fn test_challenge_id_from_uuid_string() { + let uuid_str = "550e8400-e29b-41d4-a716-446655440000"; + let id = ChallengeId::from_string(uuid_str); + assert_eq!(format!("{}", id), uuid_str); + } + + #[test] + fn test_challenge_id_display() { + let id = ChallengeId::new(); + let display = format!("{}", id); + assert!(!display.is_empty()); + } + + #[test] + fn test_validator_info() { + let hotkey = Hotkey([1u8; 32]); + let info = ValidatorInfo::new(hotkey.clone(), Stake::new(1000)); + assert_eq!(info.hotkey, hotkey); + assert!(info.is_active); + assert!(info.peer_id.is_none()); + } + + #[test] + fn test_network_config_default() { + let config = NetworkConfig::default(); + assert_eq!(config.subnet_id, 100); + assert_eq!(config.consensus_threshold, 0.50); + } + + #[test] + fn test_network_config_production() { + let config = NetworkConfig::production(); + assert_eq!(config.subnet_id, 100); + assert_eq!(config.min_stake.0, 1_000_000_000_000); + } + + #[test] + fn test_job_creation() { + let challenge_id = ChallengeId::new(); + let job = Job::new(challenge_id, "agent123".to_string()); + assert_eq!(job.status, JobStatus::Pending); + assert!(job.assigned_validator.is_none()); + assert!(job.result.is_none()); + } + + #[test] + fn test_job_status_equality() { + assert_eq!(JobStatus::Pending, JobStatus::Pending); + assert_ne!(JobStatus::Pending, JobStatus::Running); + } + + #[test] + fn test_hotkey_ss58() { + let hotkey = Hotkey([0xab; 32]); + let ss58 = hotkey.to_ss58(); + assert!(!ss58.is_empty()); + assert!(ss58.starts_with('5')); // Substrate generic prefix + } + + #[test] + fn test_challenge_id_debug() { + let id = ChallengeId::new(); + let debug = format!("{:?}", id); + // Debug contains the UUID string + assert!(!debug.is_empty()); + assert!(debug.len() > 10); // UUID is at least 36 chars + } + + #[test] + fn test_stake_conversion() { + // 2.5 TAO = 2_500_000_000 (9 decimals) + let stake = Stake::new(2_500_000_000); + assert_eq!(stake.as_tao(), 2.5); + assert_eq!(stake.0, 2_500_000_000); + } + + #[test] + fn test_stake_zero() { + let stake = Stake::new(0); + assert_eq!(stake.as_tao(), 0.0); + assert!(stake <= Stake::new(1)); + } + + #[test] + fn test_all_job_statuses() { + let statuses = vec![ + JobStatus::Pending, + JobStatus::Running, + JobStatus::Completed, + JobStatus::Failed, + JobStatus::Timeout, + ]; + + for status in statuses { + let debug = format!("{:?}", status); + assert!(!debug.is_empty()); + } + } + + #[test] + fn test_validator_info_peer_id() { + let hotkey = Hotkey([1u8; 32]); + let mut info = ValidatorInfo::new(hotkey.clone(), Stake::new(1000)); + assert!(info.peer_id.is_none()); + + info.peer_id = Some("peer123".to_string()); + assert_eq!(info.peer_id.as_ref().unwrap(), "peer123"); + } + + #[test] + fn test_hotkey_equality() { + let h1 = Hotkey([1u8; 32]); + let h2 = Hotkey([1u8; 32]); + let h3 = Hotkey([2u8; 32]); + + assert_eq!(h1, h2); + assert_ne!(h1, h3); + } + + #[test] + fn test_challenge_id_hash() { + use std::collections::HashMap; + + let id1 = ChallengeId::new(); + let id2 = ChallengeId::new(); + + let mut map: HashMap = HashMap::new(); + map.insert(id1, 1); + map.insert(id2, 2); + + assert_eq!(map.get(&id1), Some(&1)); + assert_eq!(map.get(&id2), Some(&2)); + } + + #[test] + fn test_score_edge_cases() { + let max_score = Score::new(1.0, 1.0); + assert_eq!(max_score.weighted_value(), 1.0); + + let min_score = Score::new(0.0, 0.0); + assert_eq!(min_score.weighted_value(), 0.0); + + let half = Score::new(0.5, 0.5); + assert_eq!(half.weighted_value(), 0.25); + } + + #[test] + fn test_hotkey_from_ss58() { + // Test with a valid SS58 address + let original = Hotkey([0x42; 32]); + let ss58 = original.to_ss58(); + let recovered = Hotkey::from_ss58(&ss58); + assert!(recovered.is_some()); + assert_eq!(recovered.unwrap(), original); + } + + #[test] + fn test_hotkey_from_ss58_invalid() { + assert!(Hotkey::from_ss58("invalid-address").is_none()); + assert!(Hotkey::from_ss58("").is_none()); + } + + #[test] + fn test_challenge_id_from_uuid() { + let uuid = uuid::Uuid::new_v4(); + let challenge_id = ChallengeId::from_uuid(uuid); + assert_eq!(challenge_id.0, uuid); + assert_eq!(format!("{}", challenge_id), format!("{}", uuid)); + } + + #[test] + fn test_challenge_id_default() { + let id1 = ChallengeId::default(); + let id2 = ChallengeId::default(); + // Each default should be unique + assert_ne!(id1, id2); + } +} diff --git a/crates/distributed-storage/Cargo.toml b/crates/distributed-storage/Cargo.toml new file mode 100644 index 000000000..f8b840c03 --- /dev/null +++ b/crates/distributed-storage/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "platform-distributed-storage" +version.workspace = true +edition.workspace = true +description = "Distributed storage layer for Platform Network" + +[dependencies] +platform-core = { path = "../core" } + +# Local storage +sled = { workspace = true } + +# Async +tokio = { workspace = true } +futures = { workspace = true } +async-trait = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Crypto +sha2 = { workspace = true } +hex = { workspace = true } + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +chrono = { workspace = true } +parking_lot = { workspace = true } +uuid = { workspace = true } +base64 = "0.22" + +[dev-dependencies] +tempfile = { workspace = true } +tokio-test = { workspace = true } diff --git a/crates/distributed-storage/src/challenge_store.rs b/crates/distributed-storage/src/challenge_store.rs new file mode 100644 index 000000000..e58436f5d --- /dev/null +++ b/crates/distributed-storage/src/challenge_store.rs @@ -0,0 +1,1247 @@ +//! Challenge-specific namespaced storage +//! +//! This module provides per-challenge dedicated storage with verifiable merkle proofs. +//! Each challenge gets its own namespace, ensuring data isolation and providing +//! cryptographic verification of all stored data. +//! +//! # Features +//! +//! - **Namespace Isolation**: Each challenge's data is stored in its own namespace, +//! preventing cross-challenge data access. +//! - **Merkle Proofs**: All write operations return a merkle proof for verification. +//! - **State Root**: Compute a deterministic state root hash for the entire challenge. +//! +//! # Usage +//! +//! ```text +//! use platform_distributed_storage::{ChallengeStore, LocalStorageBuilder}; +//! +//! let storage = LocalStorageBuilder::new("node-1") +//! .in_memory() +//! .build()?; +//! +//! let challenge_store = ChallengeStore::new(storage, "challenge-abc123"); +//! +//! // Store a submission +//! let proof = challenge_store.store_submission("hash123", &submission).await?; +//! +//! // Verify the submission is in the store +//! assert!(challenge_store.verify_submission("hash123", &proof)); +//! ``` + +use crate::error::{StorageError, StorageResult}; +use crate::store::{DistributedStore, GetOptions, PutOptions, StorageKey}; +use crate::submission::{StoredEvaluation, StoredSubmission}; +use crate::weights::StoredWeights; +use async_trait::async_trait; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::BTreeMap; +use std::sync::Arc; +use tokio::sync::RwLock; + +/// Merkle proof for verifying data inclusion in the challenge store. +/// +/// This proof allows verification that a specific piece of data is part +/// of the challenge's state without needing access to the entire state. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MerkleProof { + /// Root hash of the merkle tree + pub root: [u8; 32], + /// Path from leaf to root + pub path: Vec, + /// Index of the leaf in the tree + pub leaf_index: usize, + /// Hash of the leaf data + pub leaf_hash: [u8; 32], +} + +impl MerkleProof { + /// Verify that this proof is valid for the given data + pub fn verify(&self, data: &[u8]) -> bool { + // Compute leaf hash + let computed_leaf = hash_bytes(data); + if computed_leaf != self.leaf_hash { + return false; + } + + // Walk up the tree + let mut current = self.leaf_hash; + for node in &self.path { + current = if node.is_left { + hash_pair(&node.sibling_hash, ¤t) + } else { + hash_pair(¤t, &node.sibling_hash) + }; + } + + current == self.root + } +} + +/// A node in the merkle proof path +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MerkleNode { + /// Hash of the sibling node + pub sibling_hash: [u8; 32], + /// Whether the sibling is on the left side + pub is_left: bool, +} + +/// Challenge-specific storage wrapper that provides namespace isolation +/// and merkle proof generation for all stored data. +/// +/// This wrapper ensures: +/// - All data is stored in challenge-specific namespaces +/// - All write operations return merkle proofs +/// - State can be verified cryptographically +pub struct ChallengeStore { + /// The underlying distributed store + inner: Arc, + /// Challenge identifier for namespace isolation + challenge_id: String, + /// Cache for merkle tree leaves (key hash -> data hash) + /// Used for computing state root and generating proofs + leaf_cache: Arc>, +} + +/// Internal cache for tracking merkle tree leaves +struct LeafCache { + /// Submissions: key -> data hash + submissions: BTreeMap, + /// Evaluations: key -> data hash + evaluations: BTreeMap, + /// Weights: epoch -> data hash + weights: BTreeMap, +} + +impl LeafCache { + fn new() -> Self { + Self { + submissions: BTreeMap::new(), + evaluations: BTreeMap::new(), + weights: BTreeMap::new(), + } + } + + /// Get all leaves in deterministic order + fn all_leaves(&self) -> Vec<[u8; 32]> { + let mut leaves = Vec::new(); + + // Add submissions (sorted by key) + for hash in self.submissions.values() { + leaves.push(*hash); + } + + // Add evaluations (sorted by key) + for hash in self.evaluations.values() { + leaves.push(*hash); + } + + // Add weights (sorted by epoch) + for hash in self.weights.values() { + leaves.push(*hash); + } + + leaves + } + + /// Find the index of a submission leaf + fn submission_index(&self, key: &str) -> Option { + self.submissions.keys().position(|k| k == key) + } + + /// Find the index of an evaluation leaf + fn evaluation_index(&self, key: &str) -> Option { + let submissions_count = self.submissions.len(); + self.evaluations + .keys() + .position(|k| k == key) + .map(|idx| submissions_count + idx) + } + + /// Find the index of a weights leaf + fn weights_index(&self, epoch: u64) -> Option { + let base = self.submissions.len() + self.evaluations.len(); + self.weights + .keys() + .position(|e| *e == epoch) + .map(|idx| base + idx) + } +} + +impl ChallengeStore { + /// Create a new challenge store wrapping the given distributed store. + /// + /// # Arguments + /// + /// * `store` - The underlying distributed store + /// * `challenge_id` - Unique identifier for this challenge + /// + /// # Returns + /// + /// A new `ChallengeStore` instance with namespace isolation for the challenge. + pub fn new(store: S, challenge_id: &str) -> Self { + Self { + inner: Arc::new(store), + challenge_id: challenge_id.to_string(), + leaf_cache: Arc::new(RwLock::new(LeafCache::new())), + } + } + + /// Create a challenge store with an Arc-wrapped store (for sharing) + pub fn with_arc(store: Arc, challenge_id: &str) -> Self { + Self { + inner: store, + challenge_id: challenge_id.to_string(), + leaf_cache: Arc::new(RwLock::new(LeafCache::new())), + } + } + + /// Get the challenge ID + pub fn challenge_id(&self) -> &str { + &self.challenge_id + } + + /// Get the inner store reference + pub fn inner(&self) -> &S { + &self.inner + } + + // ======================================================================== + // Namespace helpers + // ======================================================================== + + /// Create a namespaced namespace string + fn namespace(&self, category: &str) -> String { + format!("{}:{}", category, self.challenge_id) + } + + /// Create a storage key for a submission + fn submission_key(&self, hash: &str) -> StorageKey { + StorageKey::new(&self.namespace("submissions"), hash) + } + + /// Create a storage key for evaluations of a submission + fn evaluation_key(&self, submission_hash: &str, validator: &str) -> StorageKey { + StorageKey::new( + &self.namespace("evaluations"), + format!("{}:{}", submission_hash, validator), + ) + } + + /// Create a storage key for evaluations list prefix + fn evaluations_prefix(&self, submission_hash: &str) -> String { + format!("{}:", submission_hash) + } + + /// Create a storage key for weights at an epoch + fn weights_key(&self, epoch: u64) -> StorageKey { + StorageKey::new(&self.namespace("weights"), epoch.to_string()) + } + + // ======================================================================== + // Submission operations + // ======================================================================== + + /// Store a submission and return a merkle proof. + /// + /// # Arguments + /// + /// * `hash` - Unique hash identifier for the submission + /// * `data` - The submission data to store + /// + /// # Returns + /// + /// A merkle proof that can be used to verify the submission's inclusion. + pub async fn store_submission( + &self, + hash: &str, + data: &StoredSubmission, + ) -> StorageResult { + let key = self.submission_key(hash); + let bytes = data + .to_bytes() + .map_err(|e| StorageError::Serialization(e.to_string()))?; + let data_hash = hash_bytes(&bytes); + + // Store the data + self.inner + .put(key, bytes.clone(), PutOptions::default()) + .await?; + + // Update the leaf cache + { + let mut cache = self.leaf_cache.write().await; + cache.submissions.insert(hash.to_string(), data_hash); + } + + // Generate and return the merkle proof + self.generate_proof_for_submission(hash, &bytes).await + } + + /// Get a submission by hash. + /// + /// # Arguments + /// + /// * `hash` - The submission hash to look up + /// + /// # Returns + /// + /// The stored submission if found, None otherwise. + pub async fn get_submission(&self, hash: &str) -> StorageResult> { + let key = self.submission_key(hash); + let result = self.inner.get(&key, GetOptions::default()).await?; + + match result { + Some(stored) => { + let submission = StoredSubmission::from_bytes(&stored.data) + .map_err(|e| StorageError::Serialization(e.to_string()))?; + Ok(Some(submission)) + } + None => Ok(None), + } + } + + /// List submissions in this challenge. + /// + /// # Arguments + /// + /// * `limit` - Maximum number of submissions to return + /// + /// # Returns + /// + /// A vector of stored submissions. + pub async fn list_submissions(&self, limit: usize) -> StorageResult> { + let namespace = self.namespace("submissions"); + let result = self + .inner + .list_prefix(&namespace, None, limit, None) + .await?; + + let mut submissions = Vec::with_capacity(result.items.len()); + for (_, stored) in result.items { + if let Ok(submission) = StoredSubmission::from_bytes(&stored.data) { + submissions.push(submission); + } + } + + Ok(submissions) + } + + // ======================================================================== + // Evaluation operations + // ======================================================================== + + /// Store an evaluation result and return a merkle proof. + /// + /// # Arguments + /// + /// * `submission_hash` - Hash of the submission being evaluated + /// * `validator` - Validator hotkey who performed the evaluation + /// * `eval` - The evaluation data to store + /// + /// # Returns + /// + /// A merkle proof for the evaluation. + pub async fn store_evaluation( + &self, + submission_hash: &str, + validator: &str, + eval: &StoredEvaluation, + ) -> StorageResult { + let key = self.evaluation_key(submission_hash, validator); + let bytes = eval + .to_bytes() + .map_err(|e| StorageError::Serialization(e.to_string()))?; + let data_hash = hash_bytes(&bytes); + + // Store the data + self.inner + .put(key, bytes.clone(), PutOptions::default()) + .await?; + + // Update the leaf cache + let cache_key = format!("{}:{}", submission_hash, validator); + { + let mut cache = self.leaf_cache.write().await; + cache.evaluations.insert(cache_key.clone(), data_hash); + } + + // Generate and return the merkle proof + self.generate_proof_for_evaluation(&cache_key, &bytes).await + } + + /// Get all evaluations for a submission. + /// + /// # Arguments + /// + /// * `submission_hash` - Hash of the submission + /// + /// # Returns + /// + /// A vector of evaluations for the submission. + pub async fn get_evaluations( + &self, + submission_hash: &str, + ) -> StorageResult> { + let namespace = self.namespace("evaluations"); + let prefix = self.evaluations_prefix(submission_hash); + let result = self + .inner + .list_prefix(&namespace, Some(prefix.as_bytes()), 1000, None) + .await?; + + let mut evaluations = Vec::with_capacity(result.items.len()); + for (_, stored) in result.items { + if let Ok(eval) = StoredEvaluation::from_bytes(&stored.data) { + evaluations.push(eval); + } + } + + Ok(evaluations) + } + + // ======================================================================== + // Weights operations + // ======================================================================== + + /// Store weights for an epoch and return a merkle proof. + /// + /// # Arguments + /// + /// * `epoch` - The epoch number + /// * `weights` - The weights data to store + /// + /// # Returns + /// + /// A merkle proof for the weights. + pub async fn store_weights( + &self, + epoch: u64, + weights: &StoredWeights, + ) -> StorageResult { + let key = self.weights_key(epoch); + let bytes = weights + .to_bytes() + .map_err(|e| StorageError::Serialization(e.to_string()))?; + let data_hash = hash_bytes(&bytes); + + // Store the data + self.inner + .put(key, bytes.clone(), PutOptions::default()) + .await?; + + // Update the leaf cache + { + let mut cache = self.leaf_cache.write().await; + cache.weights.insert(epoch, data_hash); + } + + // Generate and return the merkle proof + self.generate_proof_for_weights(epoch, &bytes).await + } + + /// Get weights for a specific epoch. + /// + /// # Arguments + /// + /// * `epoch` - The epoch number + /// + /// # Returns + /// + /// The stored weights if found, None otherwise. + pub async fn get_weights(&self, epoch: u64) -> StorageResult> { + let key = self.weights_key(epoch); + let result = self.inner.get(&key, GetOptions::default()).await?; + + match result { + Some(stored) => { + let weights = StoredWeights::from_bytes(&stored.data) + .map_err(|e| StorageError::Serialization(e.to_string()))?; + Ok(Some(weights)) + } + None => Ok(None), + } + } + + // ======================================================================== + // Verification and state root + // ======================================================================== + + /// Verify that a submission is included in the challenge state. + /// + /// # Arguments + /// + /// * `hash` - The submission hash + /// * `proof` - The merkle proof to verify + /// + /// # Returns + /// + /// `true` if the proof is valid for the current state root. + pub async fn verify_submission(&self, hash: &str, proof: &MerkleProof) -> bool { + // Get the submission data + let key = self.submission_key(hash); + let result = self.inner.get(&key, GetOptions::default()).await; + + match result { + Ok(Some(stored)) => proof.verify(&stored.data), + _ => false, + } + } + + /// Verify that data matches a merkle proof against a specific root. + /// + /// This is a static verification that doesn't require the current state. + pub fn verify_proof(data: &[u8], proof: &MerkleProof) -> bool { + proof.verify(data) + } + + /// Compute the current state root hash for this challenge. + /// + /// The state root is computed as a merkle root of all data in the challenge, + /// ordered deterministically (submissions, then evaluations, then weights, + /// each sorted by their keys). + /// + /// # Returns + /// + /// A 32-byte hash representing the current state. + pub async fn compute_state_root(&self) -> [u8; 32] { + let cache = self.leaf_cache.read().await; + let leaves = cache.all_leaves(); + + if leaves.is_empty() { + return [0u8; 32]; + } + + compute_merkle_root(&leaves) + } + + /// Rebuild the leaf cache from storage. + /// + /// This should be called on startup to restore the merkle tree state + /// from the persisted data. + pub async fn rebuild_cache(&self) -> StorageResult<()> { + let mut cache = self.leaf_cache.write().await; + cache.submissions.clear(); + cache.evaluations.clear(); + cache.weights.clear(); + + // Load submissions + let submissions_ns = self.namespace("submissions"); + let submissions = self + .inner + .list_prefix(&submissions_ns, None, 10000, None) + .await?; + for (key, stored) in submissions.items { + if let Some(hash) = key.key_string() { + cache.submissions.insert(hash, hash_bytes(&stored.data)); + } + } + + // Load evaluations + let evaluations_ns = self.namespace("evaluations"); + let evaluations = self + .inner + .list_prefix(&evaluations_ns, None, 100000, None) + .await?; + for (key, stored) in evaluations.items { + if let Some(k) = key.key_string() { + cache.evaluations.insert(k, hash_bytes(&stored.data)); + } + } + + // Load weights + let weights_ns = self.namespace("weights"); + let weights = self + .inner + .list_prefix(&weights_ns, None, 10000, None) + .await?; + for (key, stored) in weights.items { + if let Some(epoch_str) = key.key_string() { + if let Ok(epoch) = epoch_str.parse::() { + cache.weights.insert(epoch, hash_bytes(&stored.data)); + } + } + } + + Ok(()) + } + + // ======================================================================== + // Internal merkle proof generation + // ======================================================================== + + async fn generate_proof_for_submission( + &self, + hash: &str, + data: &[u8], + ) -> StorageResult { + let cache = self.leaf_cache.read().await; + let leaves = cache.all_leaves(); + let leaf_index = cache + .submission_index(hash) + .ok_or_else(|| StorageError::Internal("Submission not in cache".to_string()))?; + + Ok(build_merkle_proof(&leaves, leaf_index, data)) + } + + async fn generate_proof_for_evaluation( + &self, + cache_key: &str, + data: &[u8], + ) -> StorageResult { + let cache = self.leaf_cache.read().await; + let leaves = cache.all_leaves(); + let leaf_index = cache + .evaluation_index(cache_key) + .ok_or_else(|| StorageError::Internal("Evaluation not in cache".to_string()))?; + + Ok(build_merkle_proof(&leaves, leaf_index, data)) + } + + async fn generate_proof_for_weights( + &self, + epoch: u64, + data: &[u8], + ) -> StorageResult { + let cache = self.leaf_cache.read().await; + let leaves = cache.all_leaves(); + let leaf_index = cache + .weights_index(epoch) + .ok_or_else(|| StorageError::Internal("Weights not in cache".to_string()))?; + + Ok(build_merkle_proof(&leaves, leaf_index, data)) + } +} + +// ============================================================================ +// Multi-challenge store management +// ============================================================================ + +/// Registry for managing multiple challenge stores. +/// +/// Provides a unified way to access challenge-specific stores and +/// list all challenges with data. +pub struct ChallengeStoreRegistry { + /// Underlying store shared by all challenges + inner: Arc, + /// Active challenge stores + stores: Arc>>>>, +} + +impl ChallengeStoreRegistry { + /// Create a new registry wrapping the given store. + pub fn new(store: S) -> Self { + Self { + inner: Arc::new(store), + stores: Arc::new(RwLock::new(BTreeMap::new())), + } + } + + /// Get or create a challenge store for the given challenge ID. + pub async fn get_or_create(&self, challenge_id: &str) -> Arc> { + { + let stores = self.stores.read().await; + if let Some(store) = stores.get(challenge_id) { + return Arc::clone(store); + } + } + + // Create a new store + let store = Arc::new(ChallengeStore::with_arc( + Arc::clone(&self.inner), + challenge_id, + )); + + { + let mut stores = self.stores.write().await; + stores.insert(challenge_id.to_string(), Arc::clone(&store)); + } + + store + } + + /// List all challenge IDs that have data in the store. + pub async fn list_challenges(&self) -> StorageResult> { + let stores = self.stores.read().await; + Ok(stores.keys().cloned().collect()) + } + + /// Get the inner distributed store. + pub fn inner(&self) -> &Arc { + &self.inner + } +} + +// ============================================================================ +// Merkle tree utilities +// ============================================================================ + +/// Hash bytes using SHA-256 +fn hash_bytes(data: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(data); + hasher.finalize().into() +} + +/// Hash two nodes together +fn hash_pair(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(left); + hasher.update(right); + hasher.finalize().into() +} + +/// Compute merkle root from leaves +fn compute_merkle_root(leaves: &[[u8; 32]]) -> [u8; 32] { + if leaves.is_empty() { + return [0u8; 32]; + } + if leaves.len() == 1 { + return leaves[0]; + } + + let mut level: Vec<[u8; 32]> = leaves.to_vec(); + + while level.len() > 1 { + let mut next_level = Vec::new(); + + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + // Odd number of nodes - duplicate the last one + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + } + + level[0] +} + +/// Build a merkle proof for a leaf at the given index +fn build_merkle_proof(leaves: &[[u8; 32]], leaf_index: usize, data: &[u8]) -> MerkleProof { + if leaves.is_empty() || leaf_index >= leaves.len() { + return MerkleProof { + root: [0u8; 32], + path: Vec::new(), + leaf_index, + leaf_hash: hash_bytes(data), + }; + } + + let leaf_hash = leaves[leaf_index]; + let root = compute_merkle_root(leaves); + let mut path = Vec::new(); + let mut level: Vec<[u8; 32]> = leaves.to_vec(); + let mut index = leaf_index; + + while level.len() > 1 { + let sibling_index = if index.is_multiple_of(2) { + if index + 1 < level.len() { + index + 1 + } else { + index + } + } else { + index - 1 + }; + + path.push(MerkleNode { + sibling_hash: level[sibling_index], + is_left: sibling_index < index, + }); + + // Build next level + let mut next_level = Vec::new(); + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + index /= 2; + } + + MerkleProof { + root, + path, + leaf_index, + leaf_hash, + } +} + +// ============================================================================ +// Trait for async operations +// ============================================================================ + +/// Async trait for challenge-specific storage operations. +/// +/// This trait can be implemented by different storage backends +/// while providing the same challenge-namespaced interface. +#[async_trait] +pub trait ChallengeStorage: Send + Sync { + /// Get the challenge ID + fn challenge_id(&self) -> &str; + + /// Store a submission + async fn store_submission( + &self, + hash: &str, + data: &StoredSubmission, + ) -> StorageResult; + + /// Get a submission + async fn get_submission(&self, hash: &str) -> StorageResult>; + + /// List submissions + async fn list_submissions(&self, limit: usize) -> StorageResult>; + + /// Store an evaluation + async fn store_evaluation( + &self, + submission_hash: &str, + validator: &str, + eval: &StoredEvaluation, + ) -> StorageResult; + + /// Get evaluations for a submission + async fn get_evaluations(&self, submission_hash: &str) -> StorageResult>; + + /// Store weights + async fn store_weights( + &self, + epoch: u64, + weights: &StoredWeights, + ) -> StorageResult; + + /// Get weights for an epoch + async fn get_weights(&self, epoch: u64) -> StorageResult>; + + /// Compute the state root + async fn compute_state_root(&self) -> [u8; 32]; +} + +#[async_trait] +impl ChallengeStorage for ChallengeStore { + fn challenge_id(&self) -> &str { + &self.challenge_id + } + + async fn store_submission( + &self, + hash: &str, + data: &StoredSubmission, + ) -> StorageResult { + ChallengeStore::store_submission(self, hash, data).await + } + + async fn get_submission(&self, hash: &str) -> StorageResult> { + ChallengeStore::get_submission(self, hash).await + } + + async fn list_submissions(&self, limit: usize) -> StorageResult> { + ChallengeStore::list_submissions(self, limit).await + } + + async fn store_evaluation( + &self, + submission_hash: &str, + validator: &str, + eval: &StoredEvaluation, + ) -> StorageResult { + ChallengeStore::store_evaluation(self, submission_hash, validator, eval).await + } + + async fn get_evaluations(&self, submission_hash: &str) -> StorageResult> { + ChallengeStore::get_evaluations(self, submission_hash).await + } + + async fn store_weights( + &self, + epoch: u64, + weights: &StoredWeights, + ) -> StorageResult { + ChallengeStore::store_weights(self, epoch, weights).await + } + + async fn get_weights(&self, epoch: u64) -> StorageResult> { + ChallengeStore::get_weights(self, epoch).await + } + + async fn compute_state_root(&self) -> [u8; 32] { + ChallengeStore::compute_state_root(self).await + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::local::LocalStorageBuilder; + + fn create_test_submission(challenge_id: &str, miner: &str) -> StoredSubmission { + StoredSubmission::new( + challenge_id, + miner, + Some("def solve(): pass".to_string()), + serde_json::json!({"language": "python"}), + ) + } + + fn create_test_evaluation( + challenge_id: &str, + submission_hash: &str, + validator: &str, + score: f64, + ) -> StoredEvaluation { + StoredEvaluation::new( + challenge_id, + submission_hash, + validator, + score, + 1000, + serde_json::json!({"tasks": 10}), + vec![1, 2, 3, 4], + ) + } + + fn create_test_weights(challenge_id: &str, epoch: u64) -> StoredWeights { + StoredWeights::new( + challenge_id, + epoch, + vec![("miner1".to_string(), 0.6), ("miner2".to_string(), 0.4)], + vec![], + ) + } + + #[tokio::test] + async fn test_store_and_retrieve_submission() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + let submission = create_test_submission("challenge-1", "miner-abc"); + let hash = submission.submission_hash.clone(); + + // Store + let proof = store + .store_submission(&hash, &submission) + .await + .expect("Failed to store submission"); + + // Verify proof structure + assert_ne!(proof.root, [0u8; 32]); + + // Retrieve + let retrieved = store + .get_submission(&hash) + .await + .expect("Failed to get submission") + .expect("Submission not found"); + + assert_eq!(retrieved.challenge_id, submission.challenge_id); + assert_eq!(retrieved.miner_hotkey, submission.miner_hotkey); + } + + #[tokio::test] + async fn test_list_submissions() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + // Store multiple submissions + for i in 0..5 { + let submission = create_test_submission("challenge-1", &format!("miner-{}", i)); + store + .store_submission(&submission.submission_hash, &submission) + .await + .expect("Failed to store"); + } + + // List + let submissions = store.list_submissions(10).await.expect("Failed to list"); + + assert_eq!(submissions.len(), 5); + } + + #[tokio::test] + async fn test_store_and_retrieve_evaluations() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + let submission_hash = "submission-abc123"; + + // Store evaluations from multiple validators + for i in 0..3 { + let eval = create_test_evaluation( + "challenge-1", + submission_hash, + &format!("validator-{}", i), + 0.5 + (i as f64 * 0.1), + ); + store + .store_evaluation(submission_hash, &format!("validator-{}", i), &eval) + .await + .expect("Failed to store evaluation"); + } + + // Retrieve all evaluations for the submission + let evals = store + .get_evaluations(submission_hash) + .await + .expect("Failed to get evaluations"); + + assert_eq!(evals.len(), 3); + } + + #[tokio::test] + async fn test_store_and_retrieve_weights() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + let weights = create_test_weights("challenge-1", 42); + + // Store + let proof = store + .store_weights(42, &weights) + .await + .expect("Failed to store weights"); + + assert_ne!(proof.root, [0u8; 32]); + + // Retrieve + let retrieved = store + .get_weights(42) + .await + .expect("Failed to get weights") + .expect("Weights not found"); + + assert_eq!(retrieved.epoch, 42); + assert_eq!(retrieved.weights.len(), 2); + } + + #[tokio::test] + async fn test_namespace_isolation() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = Arc::new(storage); + + let store1 = ChallengeStore::with_arc(Arc::clone(&store), "challenge-1"); + let store2 = ChallengeStore::with_arc(Arc::clone(&store), "challenge-2"); + + // Store submissions in different challenges + let sub1 = create_test_submission("challenge-1", "miner-a"); + let sub2 = create_test_submission("challenge-2", "miner-b"); + + store1 + .store_submission(&sub1.submission_hash, &sub1) + .await + .expect("Failed to store"); + store2 + .store_submission(&sub2.submission_hash, &sub2) + .await + .expect("Failed to store"); + + // Each store should only see its own submissions + let list1 = store1.list_submissions(10).await.expect("Failed to list"); + let list2 = store2.list_submissions(10).await.expect("Failed to list"); + + assert_eq!(list1.len(), 1); + assert_eq!(list2.len(), 1); + assert_eq!(list1[0].challenge_id, "challenge-1"); + assert_eq!(list2[0].challenge_id, "challenge-2"); + } + + #[tokio::test] + async fn test_state_root_computation() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + // Empty state should have zero root + let root1 = store.compute_state_root().await; + assert_eq!(root1, [0u8; 32]); + + // Add some data + let sub = create_test_submission("challenge-1", "miner-a"); + store + .store_submission(&sub.submission_hash, &sub) + .await + .expect("Failed to store"); + + // Non-zero root after adding data + let root2 = store.compute_state_root().await; + assert_ne!(root2, [0u8; 32]); + + // Adding more data changes the root + let sub2 = create_test_submission("challenge-1", "miner-b"); + store + .store_submission(&sub2.submission_hash, &sub2) + .await + .expect("Failed to store"); + + let root3 = store.compute_state_root().await; + assert_ne!(root3, root2); + } + + #[tokio::test] + async fn test_merkle_proof_verification() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + let submission = create_test_submission("challenge-1", "miner-abc"); + let hash = submission.submission_hash.clone(); + + // Store and get proof + let proof = store + .store_submission(&hash, &submission) + .await + .expect("Failed to store"); + + // Verify the proof + let is_valid = store.verify_submission(&hash, &proof).await; + assert!(is_valid); + + // Tampered proof should fail + let mut bad_proof = proof.clone(); + bad_proof.root[0] ^= 1; + let is_valid = store.verify_submission(&hash, &bad_proof).await; + assert!(!is_valid); + } + + #[tokio::test] + async fn test_merkle_proof_static_verification() { + let data = b"test data"; + let hash = hash_bytes(data); + + let leaves = vec![hash, [1u8; 32], [2u8; 32], [3u8; 32]]; + let proof = build_merkle_proof(&leaves, 0, data); + + // Valid proof should pass + assert!(proof.verify(data)); + + // Wrong data should fail + assert!(!proof.verify(b"wrong data")); + } + + #[tokio::test] + async fn test_challenge_store_registry() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let registry = ChallengeStoreRegistry::new(storage); + + // Get or create stores + let store1 = registry.get_or_create("challenge-1").await; + let store2 = registry.get_or_create("challenge-2").await; + let store1_again = registry.get_or_create("challenge-1").await; + + // Same store should be returned for same challenge + assert_eq!(store1.challenge_id(), store1_again.challenge_id()); + + // Different challenges should have different stores + assert_ne!(store1.challenge_id(), store2.challenge_id()); + + // List challenges + let challenges = registry.list_challenges().await.expect("Failed to list"); + assert_eq!(challenges.len(), 2); + assert!(challenges.contains(&"challenge-1".to_string())); + assert!(challenges.contains(&"challenge-2".to_string())); + } + + #[tokio::test] + async fn test_rebuild_cache() { + // Create and populate a store + let storage = LocalStorageBuilder::new("test-node") + .path("/tmp/test-rebuild-cache") + .build() + .expect("Failed to create storage"); + + let store = ChallengeStore::new(storage, "challenge-1"); + + let sub = create_test_submission("challenge-1", "miner-a"); + store + .store_submission(&sub.submission_hash, &sub) + .await + .expect("Failed to store"); + + let root_before = store.compute_state_root().await; + + // Rebuild cache (simulating restart) + store + .rebuild_cache() + .await + .expect("Failed to rebuild cache"); + + let root_after = store.compute_state_root().await; + + // State root should be the same after rebuild + assert_eq!(root_before, root_after); + + // Cleanup + let _ = std::fs::remove_dir_all("/tmp/test-rebuild-cache"); + } + + #[test] + fn test_merkle_utilities() { + // Test hash_bytes + let hash1 = hash_bytes(b"test"); + let hash2 = hash_bytes(b"test"); + let hash3 = hash_bytes(b"different"); + + assert_eq!(hash1, hash2); + assert_ne!(hash1, hash3); + + // Test compute_merkle_root with various sizes + let empty: Vec<[u8; 32]> = vec![]; + assert_eq!(compute_merkle_root(&empty), [0u8; 32]); + + let single = vec![[1u8; 32]]; + assert_eq!(compute_merkle_root(&single), [1u8; 32]); + + let two = vec![[1u8; 32], [2u8; 32]]; + let root2 = compute_merkle_root(&two); + assert_ne!(root2, [0u8; 32]); + assert_ne!(root2, [1u8; 32]); + assert_ne!(root2, [2u8; 32]); + + // Three leaves (odd number) + let three = vec![[1u8; 32], [2u8; 32], [3u8; 32]]; + let root3 = compute_merkle_root(&three); + assert_ne!(root3, root2); + } +} diff --git a/crates/distributed-storage/src/dht.rs b/crates/distributed-storage/src/dht.rs new file mode 100644 index 000000000..7422293f7 --- /dev/null +++ b/crates/distributed-storage/src/dht.rs @@ -0,0 +1,981 @@ +//! DHT integration for distributed queries +//! +//! This module provides a DHT-backed storage implementation that uses +//! a Kademlia-style distributed hash table for finding and storing data +//! across the network. +//! +//! **Note:** This is a stub implementation that will be connected to +//! the p2p-consensus crate for actual network operations. + +use async_trait::async_trait; +use parking_lot::RwLock; +use sha2::{Digest, Sha256}; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; +use tracing::{debug, trace, warn}; + +use crate::error::{StorageError, StorageResult}; +use crate::local::LocalStorage; +use crate::replication::ReplicationConfig; +use crate::store::{ + DistributedStore, GetOptions, ListResult, PutOptions, StorageKey, StorageStats, StoredValue, + ValueMetadata, +}; + +/// DHT node information +#[derive(Clone, Debug)] +pub struct DhtNode { + /// Node ID (typically derived from the hotkey) + pub id: String, + /// Network address + pub address: String, + /// Node's distance from a given key (for routing) + pub distance: Option<[u8; 32]>, + /// Last seen timestamp + pub last_seen: i64, + /// Whether the node is currently reachable + pub is_online: bool, +} + +impl DhtNode { + /// Create a new DHT node + pub fn new(id: impl Into, address: impl Into) -> Self { + Self { + id: id.into(), + address: address.into(), + distance: None, + last_seen: chrono::Utc::now().timestamp_millis(), + is_online: true, + } + } +} + +/// Calculate XOR distance between two 32-byte arrays +/// +/// In Kademlia, the distance between two identifiers is the XOR of those identifiers, +/// interpreted as an unsigned integer. This is a valid metric: +/// - d(x, x) = 0 +/// - d(x, y) = d(y, x) +/// - d(x, z) <= d(x, y) + d(y, z) +fn xor_distance(a: &[u8; 32], b: &[u8; 32]) -> [u8; 32] { + let mut result = [0u8; 32]; + for i in 0..32 { + result[i] = a[i] ^ b[i]; + } + result +} + +/// Compare two XOR distances using lexicographic ordering +/// +/// Since XOR distances are represented as 256-bit numbers (32 bytes), +/// lexicographic comparison provides correct numerical ordering. +fn distance_cmp(d1: &[u8; 32], d2: &[u8; 32]) -> std::cmp::Ordering { + d1.cmp(d2) +} + +/// Hash a node ID to a 32-byte array using SHA256 +/// +/// This converts the string node ID to a fixed-size identifier +/// suitable for XOR distance calculations. +fn hash_node_id(node_id: &str) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(node_id.as_bytes()); + let result = hasher.finalize(); + let mut hash = [0u8; 32]; + hash.copy_from_slice(&result); + hash +} + +/// DHT routing table +#[derive(Debug, Default)] +pub struct RoutingTable { + /// Known nodes indexed by ID + nodes: HashMap, + /// Nodes organized by k-bucket (distance prefix) + buckets: Vec>, +} + +impl RoutingTable { + /// Create a new routing table + pub fn new() -> Self { + Self { + nodes: HashMap::new(), + // 256 k-buckets for 256-bit key space + buckets: vec![HashSet::new(); 256], + } + } + + /// Add or update a node + pub fn add_node(&mut self, node: DhtNode) { + self.nodes.insert(node.id.clone(), node); + } + + /// Remove a node + pub fn remove_node(&mut self, node_id: &str) { + self.nodes.remove(node_id); + for bucket in &mut self.buckets { + bucket.remove(node_id); + } + } + + /// Get a node by ID + pub fn get_node(&self, node_id: &str) -> Option<&DhtNode> { + self.nodes.get(node_id) + } + + /// Get all known nodes + pub fn all_nodes(&self) -> impl Iterator { + self.nodes.values() + } + + /// Get the number of known nodes + pub fn node_count(&self) -> usize { + self.nodes.len() + } + + /// Find the k closest nodes to a key using XOR distance metric + /// + /// This implements the core Kademlia algorithm for finding nodes closest + /// to a given key. Nodes are sorted by their XOR distance to the key hash, + /// and the k closest are returned. + pub fn find_closest(&self, key_hash: &[u8; 32], k: usize) -> Vec<&DhtNode> { + if self.nodes.is_empty() { + return Vec::new(); + } + + // Calculate distances for all nodes and sort + let mut nodes_with_distance: Vec<(&DhtNode, [u8; 32])> = self + .nodes + .values() + .map(|node| { + let node_hash = hash_node_id(&node.id); + let distance = xor_distance(&node_hash, key_hash); + (node, distance) + }) + .collect(); + + // Sort by XOR distance (ascending - closest first) + nodes_with_distance.sort_by(|(_, d1), (_, d2)| distance_cmp(d1, d2)); + + // Return the k closest nodes + nodes_with_distance + .into_iter() + .take(k) + .map(|(node, _)| node) + .collect() + } + + /// Mark a node as offline + pub fn mark_offline(&mut self, node_id: &str) { + if let Some(node) = self.nodes.get_mut(node_id) { + node.is_online = false; + } + } + + /// Mark a node as online + pub fn mark_online(&mut self, node_id: &str) { + if let Some(node) = self.nodes.get_mut(node_id) { + node.is_online = true; + node.last_seen = chrono::Utc::now().timestamp_millis(); + } + } + + /// Get online nodes + pub fn online_nodes(&self) -> impl Iterator { + self.nodes.values().filter(|n| n.is_online) + } +} + +/// Handler for DHT network operations +/// +/// This trait will be implemented by the p2p-consensus layer to provide +/// actual network communication. +#[async_trait] +pub trait DhtNetworkHandler: Send + Sync { + /// Find nodes close to a key + async fn find_nodes(&self, key_hash: &[u8; 32]) -> StorageResult>; + + /// Get a value from a remote node + async fn get_value( + &self, + node: &DhtNode, + key: &StorageKey, + ) -> StorageResult>; + + /// Put a value to a remote node + async fn put_value( + &self, + node: &DhtNode, + key: &StorageKey, + value: &StoredValue, + ) -> StorageResult<()>; + + /// Delete a value from a remote node + async fn delete_value(&self, node: &DhtNode, key: &StorageKey) -> StorageResult; + + /// Ping a node to check if it's alive + async fn ping(&self, node: &DhtNode) -> StorageResult; +} + +/// No-op network handler for local-only mode +pub struct LocalOnlyHandler; + +#[async_trait] +impl DhtNetworkHandler for LocalOnlyHandler { + async fn find_nodes(&self, _key_hash: &[u8; 32]) -> StorageResult> { + Ok(Vec::new()) + } + + async fn get_value( + &self, + _node: &DhtNode, + _key: &StorageKey, + ) -> StorageResult> { + Ok(None) + } + + async fn put_value( + &self, + _node: &DhtNode, + _key: &StorageKey, + _value: &StoredValue, + ) -> StorageResult<()> { + Ok(()) + } + + async fn delete_value(&self, _node: &DhtNode, _key: &StorageKey) -> StorageResult { + Ok(false) + } + + async fn ping(&self, _node: &DhtNode) -> StorageResult { + Ok(false) + } +} + +/// DHT-backed distributed storage +/// +/// This combines local storage with DHT operations for distributed +/// key-value storage across the network. +pub struct DhtStorage { + /// Local storage backend + local: Arc, + /// DHT routing table + routing_table: RwLock, + /// Network handler for DHT operations + network: Arc, + /// Replication configuration + replication_config: ReplicationConfig, + /// Whether DHT is enabled (false = local-only mode) + dht_enabled: bool, +} + +impl DhtStorage { + /// Create a new DHT storage in local-only mode + pub fn local_only(local: LocalStorage) -> Self { + Self { + local: Arc::new(local), + routing_table: RwLock::new(RoutingTable::new()), + network: Arc::new(LocalOnlyHandler), + replication_config: ReplicationConfig::default(), + dht_enabled: false, + } + } +} + +impl DhtStorage { + /// Create a new DHT storage with a custom network handler + pub fn with_network( + local: LocalStorage, + network: H, + replication_config: ReplicationConfig, + ) -> Self { + Self { + local: Arc::new(local), + routing_table: RwLock::new(RoutingTable::new()), + network: Arc::new(network), + replication_config, + dht_enabled: true, + } + } + + /// Get the local storage + pub fn local(&self) -> &LocalStorage { + &self.local + } + + /// Check if DHT mode is enabled + pub fn is_dht_enabled(&self) -> bool { + self.dht_enabled + } + + /// Add a node to the routing table + pub fn add_node(&self, node: DhtNode) { + self.routing_table.write().add_node(node); + } + + /// Remove a node from the routing table + pub fn remove_node(&self, node_id: &str) { + self.routing_table.write().remove_node(node_id); + } + + /// Get the number of known nodes + pub fn node_count(&self) -> usize { + self.routing_table.read().node_count() + } + + /// Get nodes closest to a key + fn find_closest_nodes(&self, key: &StorageKey, count: usize) -> Vec { + let key_hash = key.hash(); + self.routing_table + .read() + .find_closest(&key_hash, count) + .into_iter() + .cloned() + .collect() + } + + /// Perform a quorum read across multiple nodes + async fn quorum_read( + &self, + key: &StorageKey, + quorum_size: usize, + ) -> StorageResult> { + // First check local + let local_result = self + .local + .get( + key, + GetOptions { + local_only: true, + ..Default::default() + }, + ) + .await?; + + if !self.dht_enabled { + return Ok(local_result); + } + + // Find closest nodes + let nodes = self.find_closest_nodes(key, self.replication_config.replication_factor); + + if nodes.is_empty() { + return Ok(local_result); + } + + // Query remote nodes + let mut values: Vec = Vec::new(); + if let Some(v) = local_result { + values.push(v); + } + + for node in &nodes { + match self.network.get_value(node, key).await { + Ok(Some(v)) => values.push(v), + Ok(None) => {} + Err(e) => { + warn!("Failed to get value from node {}: {}", node.id, e); + self.routing_table.write().mark_offline(&node.id); + } + } + + if values.len() >= quorum_size { + break; + } + } + + if values.is_empty() { + return Ok(None); + } + + // Return the newest value + let newest = values.into_iter().max_by(|a, b| { + if a.is_newer_than(b) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Less + } + }); + + Ok(newest) + } + + /// Perform a quorum write across multiple nodes + async fn quorum_write( + &self, + key: &StorageKey, + value: &StoredValue, + quorum_size: usize, + ) -> StorageResult { + if !self.dht_enabled { + return Ok(1); // Just local + } + + // Find closest nodes + let nodes = self.find_closest_nodes(key, self.replication_config.replication_factor); + + let mut success_count = 1; // Local write already done + + for node in &nodes { + match self.network.put_value(node, key, value).await { + Ok(()) => { + success_count += 1; + self.local.mark_replicated(key, &node.id).await?; + } + Err(e) => { + warn!("Failed to put value to node {}: {}", node.id, e); + self.routing_table.write().mark_offline(&node.id); + } + } + } + + if success_count >= quorum_size { + Ok(success_count) + } else { + Err(StorageError::QuorumNotReached { + required: quorum_size, + received: success_count, + }) + } + } +} + +#[async_trait] +impl DistributedStore for DhtStorage { + async fn get( + &self, + key: &StorageKey, + options: GetOptions, + ) -> StorageResult> { + trace!( + "DhtStorage::get key={} local_only={}", + key, + options.local_only + ); + + if options.local_only || !self.dht_enabled { + return self.local.get(key, options).await; + } + + if options.quorum_read { + let quorum_size = options + .quorum_size + .unwrap_or(self.replication_config.read_quorum); + self.quorum_read(key, quorum_size).await + } else { + // Try local first, then DHT if not found + let local_result = self.local.get(key, GetOptions::default()).await?; + if local_result.is_some() { + return Ok(local_result); + } + + // Not found locally, try DHT + self.quorum_read(key, 1).await + } + } + + async fn put( + &self, + key: StorageKey, + value: Vec, + options: PutOptions, + ) -> StorageResult { + trace!( + "DhtStorage::put key={} local_only={} quorum={}", + key, + options.local_only, + options.quorum_write + ); + + // Always write locally first + let local_options = PutOptions { + local_only: true, + ..options.clone() + }; + let metadata = self + .local + .put(key.clone(), value.clone(), local_options) + .await?; + + if options.local_only || !self.dht_enabled { + return Ok(metadata); + } + + // Create stored value for replication + let stored_value = StoredValue { + data: value, + metadata: metadata.clone(), + }; + + if options.quorum_write { + let quorum_size = options + .quorum_size + .unwrap_or(self.replication_config.write_quorum); + + let replicated = self.quorum_write(&key, &stored_value, quorum_size).await?; + debug!( + "DhtStorage::put key={} replicated to {} nodes", + key, replicated + ); + } else { + // Background replication (fire and forget) + let _replicated = self.quorum_write(&key, &stored_value, 1).await; + } + + Ok(metadata) + } + + async fn delete(&self, key: &StorageKey) -> StorageResult { + trace!("DhtStorage::delete key={}", key); + + // Delete locally + let deleted = self.local.delete(key).await?; + + if !self.dht_enabled { + return Ok(deleted); + } + + // Delete from remote nodes + let nodes = self.find_closest_nodes(key, self.replication_config.replication_factor); + + for node in &nodes { + match self.network.delete_value(node, key).await { + Ok(_) => {} + Err(e) => { + warn!("Failed to delete value from node {}: {}", node.id, e); + } + } + } + + Ok(deleted) + } + + async fn exists(&self, key: &StorageKey) -> StorageResult { + self.local.exists(key).await + } + + async fn list_prefix( + &self, + namespace: &str, + prefix: Option<&[u8]>, + limit: usize, + continuation_token: Option<&[u8]>, + ) -> StorageResult { + // List operations are always local + // DHT doesn't support efficient range queries + self.local + .list_prefix(namespace, prefix, limit, continuation_token) + .await + } + + async fn stats(&self) -> StorageResult { + let mut stats = self.local.stats().await?; + stats.remote_peers = self.routing_table.read().node_count() as u64; + Ok(stats) + } + + async fn list_before_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult { + // Block-indexed queries are always local (DHT doesn't support range queries) + self.local + .list_before_block(namespace, block_id, limit) + .await + } + + async fn list_after_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult { + // Block-indexed queries are always local + self.local + .list_after_block(namespace, block_id, limit) + .await + } + + async fn list_range( + &self, + namespace: &str, + start_block: u64, + end_block: u64, + limit: usize, + ) -> StorageResult { + // Block-indexed queries are always local + self.local + .list_range(namespace, start_block, end_block, limit) + .await + } + + async fn count_by_namespace(&self, namespace: &str) -> StorageResult { + self.local.count_by_namespace(namespace).await + } + + async fn query( + &self, + query: crate::query::QueryBuilder, + ) -> StorageResult { + // Complex queries are always local + self.local.query(query).await + } + + async fn put_with_block( + &self, + key: StorageKey, + value: Vec, + block_id: u64, + options: PutOptions, + ) -> StorageResult { + trace!( + "DhtStorage::put_with_block key={} block_id={} local_only={}", + key, + block_id, + options.local_only + ); + + // Always write locally first (with block_id for indexing) + let local_options = PutOptions { + local_only: true, + ..options.clone() + }; + let metadata = self + .local + .put_with_block(key.clone(), value.clone(), block_id, local_options) + .await?; + + if options.local_only || !self.dht_enabled { + return Ok(metadata); + } + + // Create stored value for replication + let stored_value = StoredValue { + data: value, + metadata: metadata.clone(), + }; + + if options.quorum_write { + let quorum_size = options + .quorum_size + .unwrap_or(self.replication_config.write_quorum); + + let replicated = self.quorum_write(&key, &stored_value, quorum_size).await?; + debug!( + "DhtStorage::put_with_block key={} block_id={} replicated to {} nodes", + key, block_id, replicated + ); + } else { + // Background replication (fire and forget) + let _replicated = self.quorum_write(&key, &stored_value, 1).await; + } + + Ok(metadata) + } +} + +/// Builder for DhtStorage +pub struct DhtStorageBuilder { + local: Option, + network: Option, + replication_config: ReplicationConfig, +} + +impl DhtStorageBuilder { + /// Create a new builder + pub fn new() -> Self { + Self { + local: None, + network: None, + replication_config: ReplicationConfig::default(), + } + } +} + +impl Default for DhtStorageBuilder { + fn default() -> Self { + Self::new() + } +} + +impl DhtStorageBuilder { + /// Set the local storage + pub fn local_storage(mut self, local: LocalStorage) -> Self { + self.local = Some(local); + self + } + + /// Set the replication configuration + pub fn replication_config(mut self, config: ReplicationConfig) -> Self { + self.replication_config = config; + self + } + + /// Build a local-only storage + pub fn build_local_only(self) -> StorageResult> { + let local = self + .local + .ok_or_else(|| StorageError::InvalidData("Local storage is required".to_string()))?; + + Ok(DhtStorage::local_only(local)) + } +} + +impl DhtStorageBuilder { + /// Set the network handler + pub fn network_handler(self, network: H) -> DhtStorageBuilder { + DhtStorageBuilder { + local: self.local, + network: Some(network), + replication_config: self.replication_config, + } + } + + /// Build the DHT storage + pub fn build(self) -> StorageResult> { + let local = self + .local + .ok_or_else(|| StorageError::InvalidData("Local storage is required".to_string()))?; + + let network = self.network.ok_or_else(|| { + StorageError::InvalidData("Network handler is required for DHT mode".to_string()) + })?; + + Ok(DhtStorage::with_network( + local, + network, + self.replication_config, + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::local::LocalStorageBuilder; + + fn create_local_storage() -> LocalStorage { + LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage") + } + + #[tokio::test] + async fn test_local_only_mode() { + let local = create_local_storage(); + let dht = DhtStorage::local_only(local); + + assert!(!dht.is_dht_enabled()); + + let key = StorageKey::new("test", "key1"); + let value = b"hello".to_vec(); + + dht.put(key.clone(), value.clone(), PutOptions::default()) + .await + .expect("put failed"); + + let result = dht + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + assert!(result.is_some()); + assert_eq!(result.unwrap().data, value); + } + + #[test] + fn test_routing_table() { + let mut rt = RoutingTable::new(); + + let node1 = DhtNode::new("node1", "127.0.0.1:8000"); + let node2 = DhtNode::new("node2", "127.0.0.1:8001"); + + rt.add_node(node1); + rt.add_node(node2); + + assert_eq!(rt.node_count(), 2); + assert!(rt.get_node("node1").is_some()); + assert!(rt.get_node("node3").is_none()); + + rt.remove_node("node1"); + assert_eq!(rt.node_count(), 1); + } + + #[test] + fn test_routing_table_online_status() { + let mut rt = RoutingTable::new(); + rt.add_node(DhtNode::new("node1", "127.0.0.1:8000")); + + assert!(rt.get_node("node1").unwrap().is_online); + + rt.mark_offline("node1"); + assert!(!rt.get_node("node1").unwrap().is_online); + + rt.mark_online("node1"); + assert!(rt.get_node("node1").unwrap().is_online); + } + + #[tokio::test] + async fn test_builder() { + let local = create_local_storage(); + + let dht = DhtStorageBuilder::new() + .local_storage(local) + .build_local_only() + .expect("build failed"); + + assert!(!dht.is_dht_enabled()); + } + + #[tokio::test] + async fn test_add_remove_nodes() { + let local = create_local_storage(); + let dht = DhtStorage::local_only(local); + + assert_eq!(dht.node_count(), 0); + + dht.add_node(DhtNode::new("node1", "127.0.0.1:8000")); + assert_eq!(dht.node_count(), 1); + + dht.remove_node("node1"); + assert_eq!(dht.node_count(), 0); + } + + #[tokio::test] + async fn test_stats_includes_remote_peers() { + let local = create_local_storage(); + let dht = DhtStorage::local_only(local); + + dht.add_node(DhtNode::new("node1", "127.0.0.1:8000")); + dht.add_node(DhtNode::new("node2", "127.0.0.1:8001")); + + let stats = dht.stats().await.expect("stats failed"); + assert_eq!(stats.remote_peers, 2); + } + + #[test] + fn test_xor_distance() { + // Test basic XOR properties + let a = [0u8; 32]; + let b = [0xffu8; 32]; + + // Distance to self is zero + let d_aa = xor_distance(&a, &a); + assert_eq!(d_aa, [0u8; 32]); + + // Distance is symmetric + let d_ab = xor_distance(&a, &b); + let d_ba = xor_distance(&b, &a); + assert_eq!(d_ab, d_ba); + + // XOR of zeros and all-ones gives all-ones + assert_eq!(d_ab, [0xffu8; 32]); + + // Test with specific values + let mut c = [0u8; 32]; + c[0] = 0x10; + let d_ac = xor_distance(&a, &c); + let mut expected = [0u8; 32]; + expected[0] = 0x10; + assert_eq!(d_ac, expected); + } + + #[test] + fn test_distance_cmp() { + let zero = [0u8; 32]; + let mut small = [0u8; 32]; + small[31] = 1; // Small distance (only LSB set) + let mut large = [0u8; 32]; + large[0] = 1; // Large distance (MSB set) + + // Zero is smallest + assert_eq!(distance_cmp(&zero, &small), std::cmp::Ordering::Less); + assert_eq!(distance_cmp(&zero, &large), std::cmp::Ordering::Less); + + // Small < Large (since MSB comparison dominates) + assert_eq!(distance_cmp(&small, &large), std::cmp::Ordering::Less); + + // Equal distances + assert_eq!(distance_cmp(&small, &small), std::cmp::Ordering::Equal); + } + + #[test] + fn test_hash_node_id() { + // Hash should be deterministic + let h1 = hash_node_id("test-node"); + let h2 = hash_node_id("test-node"); + assert_eq!(h1, h2); + + // Different IDs should produce different hashes + let h3 = hash_node_id("other-node"); + assert_ne!(h1, h3); + } + + #[test] + fn test_find_closest_with_xor_distance() { + let mut rt = RoutingTable::new(); + + // Add multiple nodes + rt.add_node(DhtNode::new("node-a", "127.0.0.1:8000")); + rt.add_node(DhtNode::new("node-b", "127.0.0.1:8001")); + rt.add_node(DhtNode::new("node-c", "127.0.0.1:8002")); + rt.add_node(DhtNode::new("node-d", "127.0.0.1:8003")); + rt.add_node(DhtNode::new("node-e", "127.0.0.1:8004")); + + // Create a key hash (using hash of "test-key") + let key_hash = hash_node_id("test-key"); + + // Find closest 3 nodes + let closest = rt.find_closest(&key_hash, 3); + assert_eq!(closest.len(), 3); + + // Verify that returned nodes are actually the closest + // by checking that all non-returned nodes are at least as far + let closest_ids: HashSet = closest.iter().map(|n| n.id.clone()).collect(); + + // Calculate distances for all returned nodes + let returned_distances: Vec<[u8; 32]> = closest + .iter() + .map(|n| xor_distance(&hash_node_id(&n.id), &key_hash)) + .collect(); + + // Get max distance among returned nodes + let max_returned = returned_distances.iter().max().expect("should have max"); + + // Check that non-returned nodes are not closer than any returned node + for node in rt.all_nodes() { + if !closest_ids.contains(&node.id) { + let dist = xor_distance(&hash_node_id(&node.id), &key_hash); + assert!( + distance_cmp(&dist, max_returned) != std::cmp::Ordering::Less, + "Node {} should not be closer than returned nodes", + node.id + ); + } + } + } + + #[test] + fn test_find_closest_fewer_than_k_nodes() { + let mut rt = RoutingTable::new(); + rt.add_node(DhtNode::new("node1", "127.0.0.1:8000")); + rt.add_node(DhtNode::new("node2", "127.0.0.1:8001")); + + let key_hash = [0u8; 32]; + let closest = rt.find_closest(&key_hash, 5); + + // Should return all available nodes when k > node count + assert_eq!(closest.len(), 2); + } + + #[test] + fn test_find_closest_empty_table() { + let rt = RoutingTable::new(); + let key_hash = [0u8; 32]; + let closest = rt.find_closest(&key_hash, 5); + + assert!(closest.is_empty()); + } +} diff --git a/crates/distributed-storage/src/error.rs b/crates/distributed-storage/src/error.rs new file mode 100644 index 000000000..2a6201430 --- /dev/null +++ b/crates/distributed-storage/src/error.rs @@ -0,0 +1,281 @@ +//! Error types for distributed storage + +use thiserror::Error; + +/// Errors that can occur in distributed storage operations +#[derive(Debug, Error)] +pub enum StorageError { + /// Error from the underlying sled database + #[error("Database error: {0}")] + Database(String), + + /// Serialization/deserialization error + #[error("Serialization error: {0}")] + Serialization(String), + + /// Key not found + #[error("Key not found: {namespace}:{key}")] + NotFound { namespace: String, key: String }, + + /// Namespace not found + #[error("Namespace not found: {0}")] + NamespaceNotFound(String), + + /// DHT operation error + #[error("DHT error: {0}")] + Dht(String), + + /// Replication error + #[error("Replication error: {0}")] + Replication(String), + + /// Quorum not reached for operation + #[error("Quorum not reached: got {received} of {required} confirmations")] + QuorumNotReached { required: usize, received: usize }, + + /// Conflict detected during write + #[error("Write conflict: {0}")] + Conflict(String), + + /// Invalid data format + #[error("Invalid data: {0}")] + InvalidData(String), + + /// Operation timeout + #[error("Operation timed out: {0}")] + Timeout(String), + + /// Storage is not initialized + #[error("Storage not initialized")] + NotInitialized, + + /// Generic internal error + #[error("Internal error: {0}")] + Internal(String), +} + +impl From for StorageError { + fn from(err: sled::Error) -> Self { + StorageError::Database(err.to_string()) + } +} + +impl From for StorageError { + fn from(err: bincode::Error) -> Self { + StorageError::Serialization(err.to_string()) + } +} + +impl From for StorageError { + fn from(err: serde_json::Error) -> Self { + StorageError::Serialization(err.to_string()) + } +} + +/// Result type for storage operations +pub type StorageResult = Result; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_storage_error_display_database() { + let err = StorageError::Database("connection failed".to_string()); + assert_eq!(err.to_string(), "Database error: connection failed"); + } + + #[test] + fn test_storage_error_display_not_found() { + let err = StorageError::NotFound { + namespace: "users".to_string(), + key: "user_123".to_string(), + }; + assert_eq!(err.to_string(), "Key not found: users:user_123"); + } + + #[test] + fn test_storage_error_display_quorum() { + let err = StorageError::QuorumNotReached { + required: 3, + received: 1, + }; + assert_eq!( + err.to_string(), + "Quorum not reached: got 1 of 3 confirmations" + ); + } + + #[test] + fn test_storage_error_display_all_variants() { + // Test Database variant + let database_err = StorageError::Database("db failure".to_string()); + assert_eq!(database_err.to_string(), "Database error: db failure"); + + // Test Serialization variant + let serialization_err = StorageError::Serialization("invalid format".to_string()); + assert_eq!( + serialization_err.to_string(), + "Serialization error: invalid format" + ); + + // Test NotFound variant + let not_found_err = StorageError::NotFound { + namespace: "config".to_string(), + key: "setting_1".to_string(), + }; + assert_eq!(not_found_err.to_string(), "Key not found: config:setting_1"); + + // Test NamespaceNotFound variant + let namespace_err = StorageError::NamespaceNotFound("missing_ns".to_string()); + assert_eq!(namespace_err.to_string(), "Namespace not found: missing_ns"); + + // Test Dht variant + let dht_err = StorageError::Dht("peer unreachable".to_string()); + assert_eq!(dht_err.to_string(), "DHT error: peer unreachable"); + + // Test Replication variant + let replication_err = StorageError::Replication("sync failed".to_string()); + assert_eq!( + replication_err.to_string(), + "Replication error: sync failed" + ); + + // Test QuorumNotReached variant + let quorum_err = StorageError::QuorumNotReached { + required: 5, + received: 2, + }; + assert_eq!( + quorum_err.to_string(), + "Quorum not reached: got 2 of 5 confirmations" + ); + + // Test Conflict variant + let conflict_err = StorageError::Conflict("concurrent write detected".to_string()); + assert_eq!( + conflict_err.to_string(), + "Write conflict: concurrent write detected" + ); + + // Test InvalidData variant + let invalid_data_err = StorageError::InvalidData("corrupted checksum".to_string()); + assert_eq!( + invalid_data_err.to_string(), + "Invalid data: corrupted checksum" + ); + + // Test Timeout variant + let timeout_err = StorageError::Timeout("operation exceeded 30s".to_string()); + assert_eq!( + timeout_err.to_string(), + "Operation timed out: operation exceeded 30s" + ); + + // Test NotInitialized variant + let not_initialized_err = StorageError::NotInitialized; + assert_eq!(not_initialized_err.to_string(), "Storage not initialized"); + + // Test Internal variant + let internal_err = StorageError::Internal("unexpected state".to_string()); + assert_eq!(internal_err.to_string(), "Internal error: unexpected state"); + } + + #[test] + fn test_from_sled_error() { + // Create a sled error by opening an invalid path scenario + // sled::Error doesn't have public constructors, so we trigger a real error + let sled_result = sled::open("/\0invalid"); + if let Err(sled_err) = sled_result { + let storage_err: StorageError = sled_err.into(); + let display = storage_err.to_string(); + assert!( + display.starts_with("Database error:"), + "Expected 'Database error:' prefix, got: {}", + display + ); + } + } + + #[test] + fn test_from_bincode_error() { + // Create a bincode error by attempting to deserialize invalid data + let invalid_data: &[u8] = &[0xff, 0xff, 0xff, 0xff]; + let bincode_result: Result = bincode::deserialize(invalid_data); + if let Err(bincode_err) = bincode_result { + let storage_err: StorageError = bincode_err.into(); + let display = storage_err.to_string(); + assert!( + display.starts_with("Serialization error:"), + "Expected 'Serialization error:' prefix, got: {}", + display + ); + } + } + + #[test] + fn test_from_serde_json_error() { + // Create a serde_json error by parsing invalid JSON + let invalid_json = "{invalid json}"; + let json_result: Result = + serde_json::from_str(invalid_json); + if let Err(json_err) = json_result { + let storage_err: StorageError = json_err.into(); + let display = storage_err.to_string(); + assert!( + display.starts_with("Serialization error:"), + "Expected 'Serialization error:' prefix, got: {}", + display + ); + } + } + + #[test] + fn test_storage_result_type() { + // Test that StorageResult works as expected + fn returns_ok() -> StorageResult { + Ok(42) + } + + fn returns_err() -> StorageResult { + Err(StorageError::NotInitialized) + } + + // Test Ok case + let ok_result = returns_ok(); + assert!(ok_result.is_ok()); + assert_eq!(ok_result.unwrap(), 42); + + // Test Err case + let err_result = returns_err(); + assert!(err_result.is_err()); + assert_eq!( + err_result.unwrap_err().to_string(), + "Storage not initialized" + ); + } + + #[test] + fn test_storage_error_is_send_sync() { + // Verify StorageError can be sent across threads + fn assert_send() {} + fn assert_sync() {} + assert_send::(); + assert_sync::(); + } + + #[test] + fn test_storage_error_debug_format() { + // Verify Debug trait is implemented correctly + let err = StorageError::Database("test".to_string()); + let debug_str = format!("{:?}", err); + assert!( + debug_str.contains("Database"), + "Debug format should contain variant name" + ); + assert!( + debug_str.contains("test"), + "Debug format should contain error message" + ); + } +} diff --git a/crates/distributed-storage/src/lib.rs b/crates/distributed-storage/src/lib.rs new file mode 100644 index 000000000..9e96d62b7 --- /dev/null +++ b/crates/distributed-storage/src/lib.rs @@ -0,0 +1,373 @@ +//! Distributed Storage Layer for Platform Network +//! +//! This crate provides a decentralized storage layer using DHT (Distributed Hash Table) +//! combined with local sled database for persistence. It replaces centralized PostgreSQL +//! with a distributed, eventually-consistent storage system. +//! +//! # Features +//! +//! - **Local Storage**: Persistent key-value storage using sled +//! - **DHT Integration**: Kademlia-style distributed hash table for network-wide storage +//! - **Replication**: Configurable replication policies with quorum reads/writes +//! - **Conflict Resolution**: Last-write-wins or version-based conflict resolution +//! - **Typed Storage**: Specialized types for submissions, evaluations, and weights +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ Application Layer โ”‚ +//! โ”‚ (submissions, evaluations, weights, etc.) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ DistributedStore Trait โ”‚ +//! โ”‚ (get, put, delete, list_prefix) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ–ผ โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ LocalStorage โ”‚ โ”‚ DhtStorage โ”‚ +//! โ”‚ (sled backend) โ”‚ โ”‚ (local + DHT network) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` +//! +//! # Usage +//! +//! ## Local-only mode +//! +//! ```text +//! use platform_distributed_storage::{LocalStorage, LocalStorageBuilder, DistributedStore, StorageKey, PutOptions, GetOptions}; +//! +//! // Create local storage +//! let storage = LocalStorageBuilder::new("node-1") +//! .path("/var/lib/platform/data") +//! .build()?; +//! +//! // Store a value +//! let key = StorageKey::submission("challenge-1", "abc123"); +//! storage.put(key.clone(), b"submission data".to_vec(), PutOptions::default()).await?; +//! +//! // Retrieve a value +//! let value = storage.get(&key, GetOptions::default()).await?; +//! ``` +//! +//! ## DHT-backed mode +//! +//! ```text +//! use platform_distributed_storage::{DhtStorage, DhtStorageBuilder, LocalStorageBuilder, ReplicationConfig}; +//! +//! // Create local storage first +//! let local = LocalStorageBuilder::new("node-1") +//! .path("/var/lib/platform/data") +//! .build()?; +//! +//! // Create DHT storage with custom network handler +//! let dht = DhtStorage::with_network( +//! local, +//! my_network_handler, +//! ReplicationConfig::default(), +//! ); +//! +//! // Add known peers +//! dht.add_node(DhtNode::new("peer-1", "192.168.1.10:8080")); +//! ``` +//! +//! # Storage Keys +//! +//! Keys are organized by namespace for efficient querying: +//! +//! - `submissions:{challenge_id}:{hash}` - Miner submissions +//! - `evaluations:{challenge_id}:{submission_hash}:{validator}` - Evaluation results +//! - `weights:{challenge_id}:{epoch}` - Weight aggregations +//! - `challenges:{challenge_id}` - Challenge metadata + +pub mod challenge_store; +pub mod dht; +pub mod error; +pub mod local; +pub mod query; +pub mod replication; +pub mod state_consensus; +pub mod store; +pub mod submission; +pub mod validated_storage; +pub mod weights; + +// Re-export main types for convenience +pub use dht::{ + DhtNetworkHandler, DhtNode, DhtStorage, DhtStorageBuilder, LocalOnlyHandler, RoutingTable, +}; +pub use error::{StorageError, StorageResult}; +pub use local::{LocalStorage, LocalStorageBuilder, ReplicationInfo}; +pub use query::{ + block_index_key, block_range_end, block_range_start, parse_block_index_key, QueryBuilder, + QueryCursor, QueryFilter, QueryResult, +}; +pub use replication::{ + ConflictResolution, ConflictResolver, QuorumCalculator, ReplicationConfig, ReplicationManager, + ReplicationPolicy, ReplicationState, +}; +pub use store::{ + DistributedStore, DistributedStoreExt, GetOptions, ListResult, PutOptions, StorageKey, + StorageStats, StoredValue, ValueMetadata, +}; +pub use submission::{ + AggregatedEvaluations, EvaluationStatus, StoredEvaluation, StoredSubmission, SubmissionStatus, +}; +pub use weights::{StoredWeights, ValidatorWeightVote, WeightAggregator, WeightHistory}; + +// Challenge-specific storage +pub use challenge_store::{ + ChallengeStorage, ChallengeStore, ChallengeStoreRegistry, MerkleNode, MerkleProof, +}; + +// State consensus protocol +pub use state_consensus::{ + ConsensusResult as StateConsensusResult, FraudProof, GlobalStateLinker, InclusionProof, + StateRootConsensus, StateRootConsensusError, StateRootProposal, StateRootVote, +}; + +// Validated storage with WASM consensus +pub use validated_storage::{ + ConsensusResult as ValidatedConsensusResult, DefaultWasmValidator, StorageWriteProposal, + StorageWriteVote, ValidatedStorage, ValidatedStorageConfig, ValidatedStorageError, + WasmStorageValidator, WasmValidationResult, +}; + +#[cfg(test)] +mod integration_tests { + use super::*; + + #[tokio::test] + async fn test_full_submission_workflow() { + // Create local storage + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + // Create a submission + let submission = StoredSubmission::new( + "challenge-1", + "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty", + Some("def solve(task): return task.upper()".to_string()), + serde_json::json!({"language": "python", "version": "3.10"}), + ); + + // Store the submission + let key = StorageKey::submission(&submission.challenge_id, &submission.submission_hash); + let bytes = submission.to_bytes().expect("serialization failed"); + + storage + .put(key.clone(), bytes, PutOptions::default()) + .await + .expect("put failed"); + + // Retrieve and verify + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + let stored = result.unwrap(); + let decoded = StoredSubmission::from_bytes(&stored.data).expect("deserialization failed"); + + assert_eq!(decoded.challenge_id, "challenge-1"); + assert_eq!( + decoded.miner_hotkey, + "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" + ); + } + + #[tokio::test] + async fn test_evaluation_storage() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + // Create an evaluation + let eval = StoredEvaluation::new( + "challenge-1", + "submission-hash", + "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", + 0.85, + 1500, + serde_json::json!({"tasks_completed": 17, "total_tasks": 20}), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ); + + let key = StorageKey::evaluation( + &eval.challenge_id, + &eval.submission_hash, + &eval.validator_hotkey, + ); + let bytes = eval.to_bytes().expect("serialization failed"); + + storage + .put(key.clone(), bytes, PutOptions::default()) + .await + .expect("put failed"); + + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + } + + #[tokio::test] + async fn test_weights_workflow() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + // Simulate weight aggregation + let mut aggregator = WeightAggregator::new("challenge-1", 42); + + // Add validator votes + aggregator.add_vote(ValidatorWeightVote::new( + "validator-1", + "challenge-1", + 42, + vec![("miner-1".to_string(), 0.6), ("miner-2".to_string(), 0.4)], + vec![], + 1_000_000_000, + )); + + aggregator.add_vote(ValidatorWeightVote::new( + "validator-2", + "challenge-1", + 42, + vec![("miner-1".to_string(), 0.8), ("miner-2".to_string(), 0.2)], + vec![], + 2_000_000_000, + )); + + // Aggregate + let weights = aggregator.aggregate_stake_weighted(); + + // Store + let key = StorageKey::weights(&weights.challenge_id, weights.epoch); + let bytes = weights.to_bytes().expect("serialization failed"); + + storage + .put(key.clone(), bytes, PutOptions::default()) + .await + .expect("put failed"); + + // Verify + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + let stored = result.unwrap(); + let decoded = StoredWeights::from_bytes(&stored.data).expect("deserialization failed"); + + assert_eq!(decoded.epoch, 42); + assert!(decoded.verify_hash()); + } + + #[tokio::test] + async fn test_dht_local_only() { + let local = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let dht = DhtStorage::local_only(local); + + let key = StorageKey::new("test", "key1"); + let value = b"hello world".to_vec(); + + dht.put(key.clone(), value.clone(), PutOptions::default()) + .await + .expect("put failed"); + + let result = dht + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + assert_eq!(result.unwrap().data, value); + } + + #[tokio::test] + async fn test_list_by_namespace() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + // Add submissions for different challenges + for i in 0..5 { + let key = StorageKey::submission("challenge-1", &format!("hash-{}", i)); + storage + .put( + key, + format!("data-{}", i).into_bytes(), + PutOptions::default(), + ) + .await + .expect("put failed"); + } + + for i in 0..3 { + let key = StorageKey::submission("challenge-2", &format!("hash-{}", i)); + storage + .put( + key, + format!("data-{}", i).into_bytes(), + PutOptions::default(), + ) + .await + .expect("put failed"); + } + + // List all submissions + let result = storage + .list_prefix("submissions", None, 100, None) + .await + .expect("list failed"); + + assert_eq!(result.items.len(), 8); + } + + #[tokio::test] + async fn test_extension_methods() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)] + struct TestData { + name: String, + value: i32, + } + + let key = StorageKey::new("test", "json-key"); + let data = TestData { + name: "test".to_string(), + value: 42, + }; + + // Use extension methods + storage + .put_json(key.clone(), &data) + .await + .expect("put_json failed"); + + let result: Option = storage.get_json(&key).await.expect("get_json failed"); + assert_eq!(result, Some(data)); + } +} diff --git a/crates/distributed-storage/src/local.rs b/crates/distributed-storage/src/local.rs new file mode 100644 index 000000000..298cec340 --- /dev/null +++ b/crates/distributed-storage/src/local.rs @@ -0,0 +1,1582 @@ +//! Local sled-based storage with replication markers +//! +//! This module provides a local storage implementation using sled as the backend. +//! It stores data with replication metadata to track which remote nodes have copies. + +use async_trait::async_trait; +use bincode::Options; +use parking_lot::RwLock; +use serde::{Deserialize, Serialize}; +use sled::{Db, Tree}; +use std::collections::{HashMap, HashSet}; +use std::path::Path; +use std::sync::Arc; +use tracing::{debug, trace}; + +/// Maximum size for deserializing storage entries (100MB). +/// This limit prevents DoS attacks from malformed data causing excessive memory allocation. +const MAX_ENTRY_SIZE: u64 = 100 * 1024 * 1024; + +use crate::error::{StorageError, StorageResult}; +use crate::query::{ + block_index_key, block_range_end, block_range_start, parse_block_index_key, QueryBuilder, + QueryCursor, QueryFilter, QueryResult, +}; +use crate::store::{ + DistributedStore, GetOptions, ListResult, PutOptions, StorageKey, StorageStats, StoredValue, + ValueMetadata, +}; + +/// Replication metadata for tracking which nodes have copies of data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReplicationInfo { + /// Set of node IDs that have confirmed receipt of this value + pub replicated_to: HashSet, + /// When the last replication attempt occurred + pub last_replication_at: i64, + /// Number of replication attempts + pub replication_attempts: u32, + /// Whether this value needs replication + pub needs_replication: bool, +} + +impl Default for ReplicationInfo { + fn default() -> Self { + Self { + replicated_to: HashSet::new(), + last_replication_at: 0, + replication_attempts: 0, + needs_replication: true, + } + } +} + +impl ReplicationInfo { + /// Mark that a node has received this value + pub fn mark_replicated(&mut self, node_id: &str) { + self.replicated_to.insert(node_id.to_string()); + self.last_replication_at = chrono::Utc::now().timestamp_millis(); + } + + /// Check if value is replicated to enough nodes + pub fn meets_replication_factor(&self, factor: usize) -> bool { + self.replicated_to.len() >= factor + } +} + +/// Entry stored in the local database +#[derive(Clone, Debug, Serialize, Deserialize)] +struct LocalEntry { + /// The stored value + value: StoredValue, + /// Replication tracking info + replication: ReplicationInfo, + /// Optional block_id for block-indexed queries + #[serde(default)] + block_id: Option, +} + +/// Local storage implementation using sled +pub struct LocalStorage { + /// The underlying sled database (wrapped in Arc for spawn_blocking) + db: Arc, + /// Tree for storing key-value data + data_tree: Tree, + /// Tree for storing namespace indexes + index_tree: Tree, + /// Tree for block-based secondary index (namespace:block_id:key_hash -> primary_key) + block_index_tree: Tree, + /// Our node ID for tracking origin + node_id: String, + /// Cache for namespace entry counts + namespace_counts: RwLock>, +} + +impl LocalStorage { + /// Open or create a local storage at the given path + pub fn open>(path: P, node_id: String) -> StorageResult { + let db = sled::open(path)?; + let data_tree = db.open_tree("data")?; + let index_tree = db.open_tree("index")?; + let block_index_tree = db.open_tree("block_index")?; + + let storage = Self { + db: Arc::new(db), + data_tree, + index_tree, + block_index_tree, + node_id, + namespace_counts: RwLock::new(HashMap::new()), + }; + + // Initialize namespace counts + storage.rebuild_namespace_counts()?; + + Ok(storage) + } + + /// Create an in-memory local storage (for testing) + pub fn in_memory(node_id: String) -> StorageResult { + let db = sled::Config::new().temporary(true).open()?; + let data_tree = db.open_tree("data")?; + let index_tree = db.open_tree("index")?; + let block_index_tree = db.open_tree("block_index")?; + + Ok(Self { + db: Arc::new(db), + data_tree, + index_tree, + block_index_tree, + node_id, + namespace_counts: RwLock::new(HashMap::new()), + }) + } + + /// Get the node ID + pub fn node_id(&self) -> &str { + &self.node_id + } + + /// Rebuild namespace counts from the index + fn rebuild_namespace_counts(&self) -> StorageResult<()> { + let mut counts = HashMap::new(); + + for result in self.index_tree.iter() { + let (key, _) = result?; + if let Ok(key_str) = std::str::from_utf8(&key) { + if let Some(ns) = key_str.split(':').next() { + *counts.entry(ns.to_string()).or_insert(0) += 1; + } + } + } + + *self.namespace_counts.write() = counts; + Ok(()) + } + + /// Convert a StorageKey to a database key + fn db_key(key: &StorageKey) -> Vec { + key.to_bytes() + } + + /// Get a raw entry from the database + fn get_entry(&self, key: &StorageKey) -> StorageResult> { + let db_key = Self::db_key(key); + + match self.data_tree.get(&db_key)? { + Some(bytes) => { + // Use options compatible with bincode::serialize (legacy format with fixint encoding) + let entry: LocalEntry = bincode::options() + .with_limit(MAX_ENTRY_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&bytes)?; + Ok(Some(entry)) + } + None => Ok(None), + } + } + + /// Put a raw entry to the database + fn put_entry(&self, key: &StorageKey, entry: &LocalEntry) -> StorageResult<()> { + let db_key = Self::db_key(key); + let bytes = bincode::serialize(entry)?; + + // Check if this is an update (entry already exists) + let existing = self.get_entry(key)?; + let is_new = existing.is_none(); + + // Remove old block index if updating with different block_id + if let Some(ref old_entry) = existing { + if old_entry.block_id != entry.block_id { + if let Some(old_block_id) = old_entry.block_id { + let old_block_key = block_index_key(&key.namespace, old_block_id, key); + self.block_index_tree.remove(&old_block_key)?; + } + } + } + + self.data_tree.insert(&db_key, bytes)?; + + // Update index + let index_key = format!("{}:{}", key.namespace, hex::encode(&key.key)); + self.index_tree + .insert(index_key.as_bytes(), db_key.as_slice())?; + + // Update block index if block_id is present + if let Some(block_id) = entry.block_id { + let block_key = block_index_key(&key.namespace, block_id, key); + self.block_index_tree + .insert(&block_key, db_key.as_slice())?; + } + + // Update namespace count only for new entries + if is_new { + let mut counts = self.namespace_counts.write(); + *counts.entry(key.namespace.clone()).or_insert(0) += 1; + } + + Ok(()) + } + + /// Delete an entry from the database + fn delete_entry(&self, key: &StorageKey) -> StorageResult { + let db_key = Self::db_key(key); + + // Get the entry first to find its block_id for index cleanup + let entry = self.get_entry(key)?; + + let existed = self.data_tree.remove(&db_key)?.is_some(); + + if existed { + // Update index + let index_key = format!("{}:{}", key.namespace, hex::encode(&key.key)); + self.index_tree.remove(index_key.as_bytes())?; + + // Remove from block index if block_id was present + if let Some(ref entry) = entry { + if let Some(block_id) = entry.block_id { + let block_key = block_index_key(&key.namespace, block_id, key); + self.block_index_tree.remove(&block_key)?; + } + } + + // Update namespace count + { + let mut counts = self.namespace_counts.write(); + if let Some(count) = counts.get_mut(&key.namespace) { + *count = count.saturating_sub(1); + } + } + } + + Ok(existed) + } + + /// Mark that this value has been replicated to a node + pub async fn mark_replicated(&self, key: &StorageKey, node_id: &str) -> StorageResult<()> { + if let Some(mut entry) = self.get_entry(key)? { + entry.replication.mark_replicated(node_id); + self.put_entry(key, &entry)?; + self.flush_async().await?; + } + Ok(()) + } + + /// Get all keys that need replication + pub fn keys_needing_replication(&self, limit: usize) -> StorageResult> { + let mut keys = Vec::new(); + + for result in self.data_tree.iter() { + let (key_bytes, value_bytes) = result?; + + // Use options compatible with bincode::serialize (legacy format with fixint encoding) + let entry: LocalEntry = bincode::options() + .with_limit(MAX_ENTRY_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&value_bytes)?; + + if entry.replication.needs_replication { + // Parse the key back into a StorageKey + if let Ok(key_str) = std::str::from_utf8(&key_bytes) { + if let Some((namespace, rest)) = key_str.split_once(':') { + keys.push(StorageKey::new(namespace, rest)); + + if keys.len() >= limit { + break; + } + } + } + } + } + + Ok(keys) + } + + /// Get replication info for a key + pub fn get_replication_info(&self, key: &StorageKey) -> StorageResult> { + Ok(self.get_entry(key)?.map(|e| e.replication)) + } + + /// Flush all changes to disk synchronously (blocking) + pub fn flush(&self) -> StorageResult<()> { + self.db.flush()?; + Ok(()) + } + + /// Flush all changes to disk asynchronously using spawn_blocking + pub async fn flush_async(&self) -> StorageResult<()> { + let db = Arc::clone(&self.db); + tokio::task::spawn_blocking(move || db.flush()) + .await + .map_err(|e| StorageError::Database(format!("flush task panicked: {}", e)))? + .map_err(StorageError::from)?; + Ok(()) + } + + /// Get the underlying database (for advanced operations) + pub fn db(&self) -> &Db { + &self.db + } + + /// Get the block_id associated with an entry + pub fn get_block_id(&self, key: &StorageKey) -> StorageResult> { + Ok(self.get_entry(key)?.and_then(|e| e.block_id)) + } + + /// Internal method to put a value with an associated block_id + fn put_entry_with_block_internal( + &self, + key: &StorageKey, + value: Vec, + block_id: u64, + options: &PutOptions, + ) -> StorageResult { + trace!( + "LocalStorage::put_with_block key={} block_id={} size={}", + key, + block_id, + value.len() + ); + + // Check for optimistic concurrency + let existing = self.get_entry(key)?; + + if let Some(expected_version) = options.expected_version { + if let Some(ref existing_entry) = existing { + if existing_entry.value.metadata.version != expected_version { + return Err(StorageError::Conflict(format!( + "Version mismatch: expected {}, found {}", + expected_version, existing_entry.value.metadata.version + ))); + } + } else if expected_version != 0 { + return Err(StorageError::Conflict(format!( + "Key does not exist but expected version {}", + expected_version + ))); + } + } + + // Create the stored value + let mut stored_value = match existing { + Some(existing_entry) => { + let new_metadata = existing_entry + .value + .metadata + .update(&value, Some(self.node_id.clone())); + StoredValue { + data: value, + metadata: new_metadata, + } + } + None => StoredValue::new(value, Some(self.node_id.clone())), + }; + + // Apply TTL if specified + if options.ttl_seconds > 0 { + stored_value.metadata.ttl_seconds = options.ttl_seconds; + } + + // Create entry with replication info and block_id + let entry = LocalEntry { + value: stored_value.clone(), + replication: ReplicationInfo { + needs_replication: !options.local_only, + ..Default::default() + }, + block_id: Some(block_id), + }; + + self.put_entry(key, &entry)?; + + debug!( + "LocalStorage::put_with_block completed key={} block_id={} version={}", + key, block_id, stored_value.metadata.version + ); + + Ok(stored_value.metadata) + } + + /// Determine the optimal scan range based on query filters + fn determine_scan_range_from_query(&self, query: &QueryBuilder) -> (Vec, Option>) { + let namespace = query.namespace(); + let mut min_block: Option = None; + let mut max_block: Option = None; + + // Extract block range constraints from filters + for filter in query.filters() { + match filter { + QueryFilter::BlockBefore(block) => { + max_block = Some(max_block.map_or(*block, |m| m.min(*block))); + } + QueryFilter::BlockAfter(block) => { + // For "after", we want block_id > block, so start at block + 1 + let start = block.saturating_add(1); + min_block = Some(min_block.map_or(start, |m| m.max(start))); + } + QueryFilter::BlockRange { start, end } => { + min_block = Some(min_block.map_or(*start, |m| m.max(*start))); + max_block = Some(max_block.map_or(*end + 1, |m| m.min(*end + 1))); + } + QueryFilter::And(filters) => { + for f in filters { + match f { + QueryFilter::BlockBefore(block) => { + max_block = Some(max_block.map_or(*block, |m| m.min(*block))); + } + QueryFilter::BlockAfter(block) => { + let start = block.saturating_add(1); + min_block = Some(min_block.map_or(start, |m| m.max(start))); + } + QueryFilter::BlockRange { start, end } => { + min_block = Some(min_block.map_or(*start, |m| m.max(*start))); + max_block = Some(max_block.map_or(*end + 1, |m| m.min(*end + 1))); + } + _ => {} + } + } + } + _ => {} + } + } + + let start_key = block_range_start(namespace, min_block.unwrap_or(0)); + let end_key = max_block.map(|b| block_range_start(namespace, b)); + + (start_key, end_key) + } + + /// Iterate over block index in a range and collect results + fn query_block_index_range( + &self, + namespace: &str, + start_key: Vec, + end_key: Option>, + limit: usize, + filter: Option<&QueryFilter>, + ) -> StorageResult> { + let mut results = Vec::new(); + + let range = match end_key { + Some(ref end) => self.block_index_tree.range(start_key..end.clone()), + None => self.block_index_tree.range(start_key..), + }; + + for result in range { + let (block_index_key_bytes, data_key_bytes) = result?; + + // Parse the block index key to get namespace and block_id + let parsed = match parse_block_index_key(&block_index_key_bytes) { + Some(p) => p, + None => continue, + }; + let (parsed_namespace, block_id, _key_hash) = parsed; + + // Verify namespace matches + if parsed_namespace != namespace { + break; + } + + // Get the actual data + if let Some(value_bytes) = self.data_tree.get(&data_key_bytes)? { + // Use options compatible with bincode::serialize (legacy format with fixint encoding) + let entry: LocalEntry = bincode::options() + .with_limit(MAX_ENTRY_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&value_bytes)?; + + // Skip expired entries + if entry.value.metadata.is_expired() { + continue; + } + + // Apply filter if present + if let Some(f) = filter { + // Parse the data key to get the actual key for filtering + if let Ok(key_str) = std::str::from_utf8(&data_key_bytes) { + if let Some((_ns, rest)) = key_str.split_once(':') { + if !f.matches( + entry.block_id, + entry.value.metadata.created_at, + rest.as_bytes(), + ) { + continue; + } + } + } + } + + // Parse the data key back to StorageKey + if let Ok(key_str) = std::str::from_utf8(&data_key_bytes) { + if let Some((ns, rest)) = key_str.split_once(':') { + let storage_key = StorageKey::new(ns, rest); + results.push((storage_key, entry.value, block_id)); + + if results.len() >= limit { + break; + } + } + } + } + } + + Ok(results) + } +} + +#[async_trait] +impl DistributedStore for LocalStorage { + async fn get( + &self, + key: &StorageKey, + _options: GetOptions, + ) -> StorageResult> { + trace!("LocalStorage::get key={}", key); + + match self.get_entry(key)? { + Some(entry) => { + // Check if expired + if entry.value.metadata.is_expired() { + debug!("Key {} has expired, deleting", key); + self.delete_entry(key)?; + return Ok(None); + } + Ok(Some(entry.value)) + } + None => Ok(None), + } + } + + async fn put( + &self, + key: StorageKey, + value: Vec, + options: PutOptions, + ) -> StorageResult { + trace!("LocalStorage::put key={} size={}", key, value.len()); + + // Check for optimistic concurrency + let existing = self.get_entry(&key)?; + + if let Some(expected_version) = options.expected_version { + if let Some(ref existing_entry) = existing { + if existing_entry.value.metadata.version != expected_version { + return Err(StorageError::Conflict(format!( + "Version mismatch: expected {}, found {}", + expected_version, existing_entry.value.metadata.version + ))); + } + } else if expected_version != 0 { + return Err(StorageError::Conflict(format!( + "Key does not exist but expected version {}", + expected_version + ))); + } + } + + // Create the stored value + let mut stored_value = match existing { + Some(existing_entry) => { + let new_metadata = existing_entry + .value + .metadata + .update(&value, Some(self.node_id.clone())); + StoredValue { + data: value, + metadata: new_metadata, + } + } + None => StoredValue::new(value, Some(self.node_id.clone())), + }; + + // Apply TTL if specified + if options.ttl_seconds > 0 { + stored_value.metadata.ttl_seconds = options.ttl_seconds; + } + + // Create entry with replication info (no block_id for regular put) + let entry = LocalEntry { + value: stored_value.clone(), + replication: ReplicationInfo { + needs_replication: !options.local_only, + ..Default::default() + }, + block_id: None, + }; + + self.put_entry(&key, &entry)?; + self.flush_async().await?; + + debug!( + "LocalStorage::put completed key={} version={}", + key, stored_value.metadata.version + ); + + Ok(stored_value.metadata) + } + + async fn delete(&self, key: &StorageKey) -> StorageResult { + trace!("LocalStorage::delete key={}", key); + let deleted = self.delete_entry(key)?; + self.flush_async().await?; + Ok(deleted) + } + + async fn exists(&self, key: &StorageKey) -> StorageResult { + let db_key = Self::db_key(key); + Ok(self.data_tree.contains_key(&db_key)?) + } + + async fn list_prefix( + &self, + namespace: &str, + prefix: Option<&[u8]>, + limit: usize, + continuation_token: Option<&[u8]>, + ) -> StorageResult { + trace!( + "LocalStorage::list_prefix namespace={} prefix={:?}", + namespace, + prefix + ); + + let mut items = Vec::new(); + let mut last_key: Option> = None; + + // Build the scan prefix + let scan_prefix = match prefix { + Some(p) => format!("{}:{}", namespace, hex::encode(p)), + None => format!("{}:", namespace), + }; + + // Determine where to start scanning + let start = match continuation_token { + Some(token) => token.to_vec(), + None => scan_prefix.as_bytes().to_vec(), + }; + + for result in self.index_tree.range(start..) { + let (index_key, data_key) = result?; + + // Check if we're still in the right namespace + if !index_key.starts_with(scan_prefix.as_bytes()) { + break; + } + + // Get the actual data + if let Some(value_bytes) = self.data_tree.get(&data_key)? { + // Use options compatible with bincode::serialize (legacy format with fixint encoding) + let entry: LocalEntry = bincode::options() + .with_limit(MAX_ENTRY_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&value_bytes)?; + + // Skip expired entries + if entry.value.metadata.is_expired() { + continue; + } + + // Parse the key + if let Ok(key_str) = std::str::from_utf8(&data_key) { + if let Some((ns, rest)) = key_str.split_once(':') { + let key = StorageKey::new(ns, rest); + items.push((key, entry.value)); + last_key = Some(index_key.to_vec()); + + if items.len() >= limit { + break; + } + } + } + } + } + + let has_more = items.len() >= limit; + let continuation_token = if has_more { + last_key.map(|mut k| { + // Increment the last byte to get the next key + if let Some(last) = k.last_mut() { + *last = last.saturating_add(1); + } + k + }) + } else { + None + }; + + Ok(ListResult { + items, + has_more, + continuation_token, + }) + } + + async fn stats(&self) -> StorageResult { + let counts = self.namespace_counts.read().clone(); + + let total_keys: u64 = counts.values().sum(); + + // Calculate total bytes + let mut total_bytes: u64 = 0; + for result in self.data_tree.iter() { + let (_, value) = result?; + total_bytes += value.len() as u64; + } + + Ok(StorageStats { + total_keys, + total_bytes, + keys_per_namespace: counts, + local_replicas: total_keys, + remote_peers: 0, // Local storage doesn't know about remote peers + }) + } + + async fn list_before_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult { + trace!( + "LocalStorage::list_before_block namespace={} block_id={} limit={}", + namespace, + block_id, + limit + ); + + // Start from the beginning of the namespace, end before the target block + let start_key = block_range_start(namespace, 0); + let end_key = block_range_start(namespace, block_id); + + let results = + self.query_block_index_range(namespace, start_key, Some(end_key), limit, None)?; + + let items: Vec<(StorageKey, StoredValue)> = + results.into_iter().map(|(k, v, _)| (k, v)).collect(); + + let has_more = items.len() >= limit; + let result = QueryResult::new(items, 0, limit, has_more); + + Ok(result) + } + + async fn list_after_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult { + trace!( + "LocalStorage::list_after_block namespace={} block_id={} limit={}", + namespace, + block_id, + limit + ); + + // Start from the block after block_id + let start_key = block_range_end(namespace, block_id); + + let results = self.query_block_index_range(namespace, start_key, None, limit, None)?; + + let items: Vec<(StorageKey, StoredValue)> = + results.into_iter().map(|(k, v, _)| (k, v)).collect(); + + let has_more = items.len() >= limit; + let result = QueryResult::new(items, 0, limit, has_more); + + Ok(result) + } + + async fn list_range( + &self, + namespace: &str, + start_block: u64, + end_block: u64, + limit: usize, + ) -> StorageResult { + trace!( + "LocalStorage::list_range namespace={} start={} end={} limit={}", + namespace, + start_block, + end_block, + limit + ); + + let start_key = block_range_start(namespace, start_block); + let end_key = block_range_end(namespace, end_block); + + let results = + self.query_block_index_range(namespace, start_key, Some(end_key), limit, None)?; + + let items: Vec<(StorageKey, StoredValue)> = + results.into_iter().map(|(k, v, _)| (k, v)).collect(); + + let has_more = items.len() >= limit; + let result = QueryResult::new(items, 0, limit, has_more); + + Ok(result) + } + + async fn count_by_namespace(&self, namespace: &str) -> StorageResult { + let counts = self.namespace_counts.read(); + Ok(*counts.get(namespace).unwrap_or(&0)) + } + + async fn query(&self, query: QueryBuilder) -> StorageResult { + trace!( + "LocalStorage::query namespace={} limit={}", + query.namespace(), + query.get_limit() + ); + + let namespace = query.namespace(); + let limit = query.get_limit(); + let filter = query.build_filter(); + + // Determine scan range based on filters + let (start_key, end_key) = self.determine_scan_range_from_query(&query); + + // Handle cursor-based pagination + let effective_start = if let Some(cursor) = query.get_cursor() { + if let (Some(last_block), Some(last_hash)) = + (cursor.last_block_id, cursor.last_key_hash.as_ref()) + { + // Create a key that's just after the cursor position + let mut cursor_key = Vec::new(); + cursor_key.extend_from_slice(namespace.as_bytes()); + cursor_key.push(b':'); + cursor_key.extend_from_slice(&last_block.to_be_bytes()); + cursor_key.push(b':'); + cursor_key.extend_from_slice(last_hash); + // Increment to get next item + if let Some(last) = cursor_key.last_mut() { + *last = last.saturating_add(1); + } + cursor_key + } else { + start_key + } + } else { + start_key + }; + + let results = self.query_block_index_range( + namespace, + effective_start, + end_key, + limit + 1, // Fetch one extra to check has_more + filter.as_ref(), + )?; + + let has_more = results.len() > limit; + let results: Vec<_> = results.into_iter().take(limit).collect(); + + // Build cursor for next page + let next_cursor = if has_more && !results.is_empty() { + let (last_key, _, last_block) = results.last().expect("results not empty"); + Some(QueryCursor::from_last_item( + namespace, + *last_block, + last_key, + )) + } else { + None + }; + + let items: Vec<(StorageKey, StoredValue)> = + results.into_iter().map(|(k, v, _)| (k, v)).collect(); + + let mut result = QueryResult::new(items, query.get_offset(), limit, has_more); + if let Some(cursor) = next_cursor { + result = result.with_cursor(cursor); + } + + // Include count if requested + if query.should_include_count() { + let count = self.count_by_namespace(namespace).await?; + result = result.with_total_count(count); + } + + Ok(result) + } + + async fn put_with_block( + &self, + key: StorageKey, + value: Vec, + block_id: u64, + options: PutOptions, + ) -> StorageResult { + let metadata = self.put_entry_with_block_internal(&key, value, block_id, &options)?; + self.flush_async().await?; + Ok(metadata) + } +} + +/// Builder for LocalStorage with configuration options +pub struct LocalStorageBuilder { + path: Option, + node_id: String, + in_memory: bool, +} + +impl LocalStorageBuilder { + /// Create a new builder + pub fn new(node_id: impl Into) -> Self { + Self { + path: None, + node_id: node_id.into(), + in_memory: false, + } + } + + /// Set the storage path + pub fn path(mut self, path: impl Into) -> Self { + self.path = Some(path.into()); + self.in_memory = false; + self + } + + /// Use in-memory storage + pub fn in_memory(mut self) -> Self { + self.in_memory = true; + self.path = None; + self + } + + /// Build the storage + pub fn build(self) -> StorageResult { + if self.in_memory { + LocalStorage::in_memory(self.node_id) + } else if let Some(path) = self.path { + LocalStorage::open(path, self.node_id) + } else { + Err(StorageError::InvalidData( + "Must specify either path or in_memory".to_string(), + )) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test_storage() -> LocalStorage { + LocalStorage::in_memory("test-node".to_string()).expect("Failed to create storage") + } + + #[tokio::test] + async fn test_put_and_get() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + let value = b"hello world".to_vec(); + + let metadata = storage + .put(key.clone(), value.clone(), PutOptions::default()) + .await + .expect("put failed"); + + assert_eq!(metadata.version, 1); + assert_eq!(metadata.size, value.len()); + + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + let stored = result.unwrap(); + assert_eq!(stored.data, value); + } + + #[tokio::test] + async fn test_update_increments_version() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + + storage + .put(key.clone(), b"v1".to_vec(), PutOptions::default()) + .await + .expect("put 1 failed"); + + let metadata = storage + .put(key.clone(), b"v2".to_vec(), PutOptions::default()) + .await + .expect("put 2 failed"); + + assert_eq!(metadata.version, 2); + } + + #[tokio::test] + async fn test_optimistic_concurrency() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + + storage + .put(key.clone(), b"v1".to_vec(), PutOptions::default()) + .await + .expect("put 1 failed"); + + // Should succeed with correct version + let options = PutOptions { + expected_version: Some(1), + ..Default::default() + }; + + let result = storage.put(key.clone(), b"v2".to_vec(), options).await; + assert!(result.is_ok()); + + // Should fail with wrong version + let options = PutOptions { + expected_version: Some(1), + ..Default::default() + }; // Still expecting 1, but it's now 2 + + let result = storage.put(key.clone(), b"v3".to_vec(), options).await; + assert!(matches!(result, Err(StorageError::Conflict(_)))); + } + + #[tokio::test] + async fn test_delete() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + + storage + .put(key.clone(), b"value".to_vec(), PutOptions::default()) + .await + .expect("put failed"); + + assert!(storage.delete(&key).await.expect("delete failed")); + + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + assert!(result.is_none()); + } + + #[tokio::test] + async fn test_exists() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + + assert!(!storage.exists(&key).await.expect("exists check failed")); + + storage + .put(key.clone(), b"value".to_vec(), PutOptions::default()) + .await + .expect("put failed"); + + assert!(storage.exists(&key).await.expect("exists check failed")); + } + + #[tokio::test] + async fn test_list_prefix() { + let storage = create_test_storage(); + + // Add some keys + for i in 0..5 { + let key = StorageKey::new("test", format!("key{}", i)); + storage + .put( + key, + format!("value{}", i).into_bytes(), + PutOptions::default(), + ) + .await + .expect("put failed"); + } + + // Also add a key in a different namespace + storage + .put( + StorageKey::new("other", "key"), + b"other".to_vec(), + PutOptions::default(), + ) + .await + .expect("put failed"); + + let result = storage + .list_prefix("test", None, 10, None) + .await + .expect("list failed"); + + assert_eq!(result.items.len(), 5); + assert!(!result.has_more); + } + + #[tokio::test] + async fn test_list_prefix_pagination() { + let storage = create_test_storage(); + + // Add 10 keys + for i in 0..10 { + let key = StorageKey::new("test", format!("key{:02}", i)); + storage + .put( + key, + format!("value{}", i).into_bytes(), + PutOptions::default(), + ) + .await + .expect("put failed"); + } + + // Get first page + let result1 = storage + .list_prefix("test", None, 5, None) + .await + .expect("list failed"); + + assert_eq!(result1.items.len(), 5); + assert!(result1.has_more); + assert!(result1.continuation_token.is_some()); + + // Get second page + let result2 = storage + .list_prefix("test", None, 5, result1.continuation_token.as_deref()) + .await + .expect("list failed"); + + assert_eq!(result2.items.len(), 5); + } + + #[tokio::test] + async fn test_stats() { + let storage = create_test_storage(); + + storage + .put( + StorageKey::new("ns1", "key1"), + b"value1".to_vec(), + PutOptions::default(), + ) + .await + .expect("put failed"); + + storage + .put( + StorageKey::new("ns1", "key2"), + b"value2".to_vec(), + PutOptions::default(), + ) + .await + .expect("put failed"); + + storage + .put( + StorageKey::new("ns2", "key1"), + b"value3".to_vec(), + PutOptions::default(), + ) + .await + .expect("put failed"); + + let stats = storage.stats().await.expect("stats failed"); + + assert_eq!(stats.total_keys, 3); + assert_eq!(stats.keys_per_namespace.get("ns1"), Some(&2)); + assert_eq!(stats.keys_per_namespace.get("ns2"), Some(&1)); + } + + #[tokio::test] + async fn test_replication_tracking() { + let storage = create_test_storage(); + + let key = StorageKey::new("test", "key1"); + + storage + .put(key.clone(), b"value".to_vec(), PutOptions::default()) + .await + .expect("put failed"); + + // Check initial replication info + let info = storage + .get_replication_info(&key) + .expect("get replication info failed") + .expect("should have replication info"); + + assert!(info.needs_replication); + assert!(info.replicated_to.is_empty()); + + // Mark as replicated + storage + .mark_replicated(&key, "node2") + .await + .expect("mark replicated failed"); + + let info = storage + .get_replication_info(&key) + .expect("get replication info failed") + .expect("should have replication info"); + + assert!(info.replicated_to.contains("node2")); + } + + #[test] + fn test_builder_in_memory() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("build failed"); + + assert_eq!(storage.node_id(), "test-node"); + } + + #[test] + fn test_builder_requires_path_or_in_memory() { + let result = LocalStorageBuilder::new("test-node").build(); + assert!(matches!(result, Err(StorageError::InvalidData(_)))); + } + + #[tokio::test] + async fn test_put_with_block() { + let storage = create_test_storage(); + + let key = StorageKey::new("submissions", "test-hash-1"); + let value = b"submission data".to_vec(); + + let metadata = storage + .put_with_block(key.clone(), value.clone(), 100, PutOptions::default()) + .await + .expect("put_with_block failed"); + + assert_eq!(metadata.version, 1); + assert_eq!(metadata.size, value.len()); + + // Verify we can retrieve it + let result = storage + .get(&key, GetOptions::default()) + .await + .expect("get failed"); + + assert!(result.is_some()); + assert_eq!(result.unwrap().data, value); + + // Verify block_id is stored + let block_id = storage.get_block_id(&key).expect("get_block_id failed"); + assert_eq!(block_id, Some(100)); + } + + #[tokio::test] + async fn test_list_before_block() { + let storage = create_test_storage(); + + // Add entries at different blocks + for block in [50, 100, 150, 200, 250] { + let key = StorageKey::new("submissions", format!("hash-block-{}", block)); + storage + .put_with_block( + key, + format!("data-{}", block).into_bytes(), + block, + PutOptions::default(), + ) + .await + .expect("put_with_block failed"); + } + + // Query entries before block 150 + let result = storage + .list_before_block("submissions", 150, 100) + .await + .expect("list_before_block failed"); + + assert_eq!(result.items.len(), 2); // blocks 50 and 100 + assert!(!result.has_more); + } + + #[tokio::test] + async fn test_list_after_block() { + let storage = create_test_storage(); + + // Add entries at different blocks + for block in [50, 100, 150, 200, 250] { + let key = StorageKey::new("submissions", format!("hash-block-{}", block)); + storage + .put_with_block( + key, + format!("data-{}", block).into_bytes(), + block, + PutOptions::default(), + ) + .await + .expect("put_with_block failed"); + } + + // Query entries after block 150 + let result = storage + .list_after_block("submissions", 150, 100) + .await + .expect("list_after_block failed"); + + assert_eq!(result.items.len(), 2); // blocks 200 and 250 + assert!(!result.has_more); + } + + #[tokio::test] + async fn test_list_range() { + let storage = create_test_storage(); + + // Add entries at different blocks + for block in [50, 100, 150, 200, 250] { + let key = StorageKey::new("submissions", format!("hash-block-{}", block)); + storage + .put_with_block( + key, + format!("data-{}", block).into_bytes(), + block, + PutOptions::default(), + ) + .await + .expect("put_with_block failed"); + } + + // Query entries in range [100, 200] + let result = storage + .list_range("submissions", 100, 200, 100) + .await + .expect("list_range failed"); + + assert_eq!(result.items.len(), 3); // blocks 100, 150, 200 + assert!(!result.has_more); + } + + #[tokio::test] + async fn test_count_by_namespace() { + let storage = create_test_storage(); + + // Add entries to different namespaces + for i in 0..5 { + let key = StorageKey::new("submissions", format!("hash-{}", i)); + storage + .put_with_block(key, b"data".to_vec(), i * 10, PutOptions::default()) + .await + .expect("put_with_block failed"); + } + + for i in 0..3 { + let key = StorageKey::new("evaluations", format!("hash-{}", i)); + storage + .put_with_block(key, b"data".to_vec(), i * 10, PutOptions::default()) + .await + .expect("put_with_block failed"); + } + + let count = storage + .count_by_namespace("submissions") + .await + .expect("count_by_namespace failed"); + assert_eq!(count, 5); + + let count = storage + .count_by_namespace("evaluations") + .await + .expect("count_by_namespace failed"); + assert_eq!(count, 3); + + let count = storage + .count_by_namespace("nonexistent") + .await + .expect("count_by_namespace failed"); + assert_eq!(count, 0); + } + + #[tokio::test] + async fn test_query_builder_with_filters() { + let storage = create_test_storage(); + + // Add entries at different blocks + for block in [50, 100, 150, 200, 250, 300] { + let key = StorageKey::new("submissions", format!("hash-block-{}", block)); + storage + .put_with_block( + key, + format!("data-{}", block).into_bytes(), + block, + PutOptions::default(), + ) + .await + .expect("put_with_block failed"); + } + + // Query with range filter using QueryBuilder + let query = QueryBuilder::new("submissions") + .after_block(100) + .before_block(300) + .limit(10); + + let result = storage.query(query).await.expect("query failed"); + + // Should match blocks 150, 200, 250 (after 100 AND before 300) + assert_eq!(result.items.len(), 3); + } + + #[tokio::test] + async fn test_query_with_pagination() { + let storage = create_test_storage(); + + // Add 10 entries + for block in 0..10 { + let key = StorageKey::new("submissions", format!("hash-block-{:02}", block)); + storage + .put_with_block( + key, + format!("data-{}", block).into_bytes(), + block * 10, + PutOptions::default(), + ) + .await + .expect("put_with_block failed"); + } + + // First page + let query = QueryBuilder::new("submissions").limit(3); + let result1 = storage.query(query).await.expect("query failed"); + + assert_eq!(result1.items.len(), 3); + assert!(result1.has_more); + assert!(result1.next_cursor.is_some()); + + // Second page using cursor + let query = QueryBuilder::new("submissions") + .limit(3) + .cursor(result1.next_cursor.unwrap()); + let result2 = storage.query(query).await.expect("query failed"); + + assert_eq!(result2.items.len(), 3); + assert!(result2.has_more); + + // Verify no overlap between pages + let keys1: Vec<_> = result1.items.iter().map(|(k, _)| k.to_string()).collect(); + let keys2: Vec<_> = result2.items.iter().map(|(k, _)| k.to_string()).collect(); + for k in &keys1 { + assert!(!keys2.contains(k), "Key {} found in both pages", k); + } + } + + #[tokio::test] + async fn test_query_with_count() { + let storage = create_test_storage(); + + // Add 5 entries + for block in 0..5 { + let key = StorageKey::new("submissions", format!("hash-{}", block)); + storage + .put_with_block(key, b"data".to_vec(), block * 10, PutOptions::default()) + .await + .expect("put_with_block failed"); + } + + let query = QueryBuilder::new("submissions").limit(2).with_count(); + let result = storage.query(query).await.expect("query failed"); + + assert_eq!(result.items.len(), 2); + assert_eq!(result.total_count, Some(5)); + } + + #[tokio::test] + async fn test_block_index_update_on_reput() { + let storage = create_test_storage(); + + let key = StorageKey::new("submissions", "test-hash"); + + // Put at block 100 + storage + .put_with_block(key.clone(), b"v1".to_vec(), 100, PutOptions::default()) + .await + .expect("put_with_block failed"); + + // Verify it's at block 100 + let result = storage + .list_range("submissions", 100, 100, 10) + .await + .expect("list_range failed"); + assert_eq!(result.items.len(), 1); + + // Re-put at block 200 + storage + .put_with_block(key.clone(), b"v2".to_vec(), 200, PutOptions::default()) + .await + .expect("put_with_block failed"); + + // Should not be at block 100 anymore + let result = storage + .list_range("submissions", 100, 100, 10) + .await + .expect("list_range failed"); + assert_eq!(result.items.len(), 0); + + // Should be at block 200 + let result = storage + .list_range("submissions", 200, 200, 10) + .await + .expect("list_range failed"); + assert_eq!(result.items.len(), 1); + } + + #[tokio::test] + async fn test_delete_removes_block_index() { + let storage = create_test_storage(); + + let key = StorageKey::new("submissions", "test-hash"); + + storage + .put_with_block(key.clone(), b"data".to_vec(), 100, PutOptions::default()) + .await + .expect("put_with_block failed"); + + // Verify it exists in block index + let result = storage + .list_range("submissions", 100, 100, 10) + .await + .expect("list_range failed"); + assert_eq!(result.items.len(), 1); + + // Delete + storage.delete(&key).await.expect("delete failed"); + + // Should no longer be in block index + let result = storage + .list_range("submissions", 100, 100, 10) + .await + .expect("list_range failed"); + assert_eq!(result.items.len(), 0); + } + + #[tokio::test] + async fn test_empty_namespace_queries() { + let storage = create_test_storage(); + + // Query empty namespace + let result = storage + .list_before_block("empty", 1000, 100) + .await + .expect("list_before_block failed"); + assert!(result.is_empty()); + + let result = storage + .list_after_block("empty", 0, 100) + .await + .expect("list_after_block failed"); + assert!(result.is_empty()); + + let result = storage + .list_range("empty", 0, 1000, 100) + .await + .expect("list_range failed"); + assert!(result.is_empty()); + } +} diff --git a/crates/distributed-storage/src/query.rs b/crates/distributed-storage/src/query.rs new file mode 100644 index 000000000..56cf0cf83 --- /dev/null +++ b/crates/distributed-storage/src/query.rs @@ -0,0 +1,619 @@ +//! Query capabilities for distributed storage +//! +//! This module provides SQL-like query capabilities using sled's range iterators. +//! It supports block-based filtering, pagination, and fluent query construction. + +use bincode::Options; +use serde::{Deserialize, Serialize}; + +use crate::store::{StorageKey, StoredValue}; + +/// Maximum size for deserializing query cursor data (1MB). +/// This limit prevents DoS attacks from malformed data causing excessive memory allocation. +/// Cursors are small structures, so 1MB is more than sufficient. +const MAX_CURSOR_SIZE: u64 = 1024 * 1024; + +/// Create bincode options with size limit for safe deserialization. +/// Uses fixint encoding and allows trailing bytes for compatibility with `bincode::serialize()`. +fn bincode_options() -> impl Options { + bincode::options() + .with_limit(MAX_CURSOR_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() +} + +/// Result of a query operation with pagination support +#[derive(Clone, Debug)] +pub struct QueryResult { + /// Matching items as key-value pairs + pub items: Vec<(StorageKey, StoredValue)>, + /// Total count of items matching the filter (before limit) + pub total_count: Option, + /// Current page offset + pub offset: usize, + /// Requested limit + pub limit: usize, + /// Whether there are more results beyond the current page + pub has_more: bool, + /// Continuation token for fetching the next page + pub next_cursor: Option, +} + +impl QueryResult { + /// Create a new empty query result + pub fn empty(limit: usize) -> Self { + Self { + items: Vec::new(), + total_count: None, + offset: 0, + limit, + has_more: false, + next_cursor: None, + } + } + + /// Create a query result from items + pub fn new( + items: Vec<(StorageKey, StoredValue)>, + offset: usize, + limit: usize, + has_more: bool, + ) -> Self { + Self { + items, + total_count: None, + offset, + limit, + has_more, + next_cursor: None, + } + } + + /// Set the total count + pub fn with_total_count(mut self, count: u64) -> Self { + self.total_count = Some(count); + self + } + + /// Set the continuation cursor + pub fn with_cursor(mut self, cursor: QueryCursor) -> Self { + self.next_cursor = Some(cursor); + self + } + + /// Get the number of items in this result + pub fn len(&self) -> usize { + self.items.len() + } + + /// Check if the result is empty + pub fn is_empty(&self) -> bool { + self.items.is_empty() + } +} + +/// Cursor for pagination in queries +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct QueryCursor { + /// Last block_id seen + pub last_block_id: Option, + /// Last key hash seen (for tie-breaking within same block) + pub last_key_hash: Option>, + /// Namespace being queried + pub namespace: String, +} + +impl QueryCursor { + /// Create a new cursor + pub fn new(namespace: &str) -> Self { + Self { + last_block_id: None, + last_key_hash: None, + namespace: namespace.to_string(), + } + } + + /// Create a cursor from the last item in a result + pub fn from_last_item(namespace: &str, block_id: u64, key: &StorageKey) -> Self { + Self { + last_block_id: Some(block_id), + last_key_hash: Some(key.hash().to_vec()), + namespace: namespace.to_string(), + } + } + + /// Encode cursor to bytes for transport + pub fn to_bytes(&self) -> Vec { + bincode::serialize(self).unwrap_or_default() + } + + /// Decode cursor from bytes with size limit protection. + /// Limits deserialization to MAX_CURSOR_SIZE bytes to prevent DoS via memory exhaustion. + pub fn from_bytes(bytes: &[u8]) -> Option { + bincode_options().deserialize(bytes).ok() + } + + /// Encode cursor to base64 string + pub fn to_base64(&self) -> String { + use base64::Engine; + base64::engine::general_purpose::STANDARD.encode(self.to_bytes()) + } + + /// Decode cursor from base64 string + pub fn from_base64(s: &str) -> Option { + use base64::Engine; + let bytes = base64::engine::general_purpose::STANDARD.decode(s).ok()?; + Self::from_bytes(&bytes) + } +} + +/// Filter types for queries +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum QueryFilter { + /// Match entries where block_id is less than the specified value + BlockBefore(u64), + /// Match entries where block_id is greater than the specified value + BlockAfter(u64), + /// Match entries where block_id is within a range (inclusive) + BlockRange { start: u64, end: u64 }, + /// Match entries where created_at timestamp is before the specified value + CreatedBefore(i64), + /// Match entries where created_at timestamp is after the specified value + CreatedAfter(i64), + /// Match entries with a specific key prefix within the namespace + KeyPrefix(Vec), + /// Combine multiple filters with AND logic + And(Vec), + /// Combine multiple filters with OR logic + Or(Vec), +} + +impl QueryFilter { + /// Check if an entry matches this filter + pub fn matches(&self, block_id: Option, created_at: i64, key: &[u8]) -> bool { + match self { + QueryFilter::BlockBefore(max_block) => block_id.is_none_or(|b| b < *max_block), + QueryFilter::BlockAfter(min_block) => block_id.is_some_and(|b| b > *min_block), + QueryFilter::BlockRange { start, end } => { + block_id.is_some_and(|b| b >= *start && b <= *end) + } + QueryFilter::CreatedBefore(timestamp) => created_at < *timestamp, + QueryFilter::CreatedAfter(timestamp) => created_at > *timestamp, + QueryFilter::KeyPrefix(prefix) => key.starts_with(prefix), + QueryFilter::And(filters) => { + filters.iter().all(|f| f.matches(block_id, created_at, key)) + } + QueryFilter::Or(filters) => { + filters.iter().any(|f| f.matches(block_id, created_at, key)) + } + } + } +} + +/// Builder for constructing queries with a fluent API +#[derive(Clone, Debug)] +pub struct QueryBuilder { + namespace: String, + filters: Vec, + limit: usize, + offset: usize, + cursor: Option, + include_count: bool, + order_ascending: bool, +} + +impl QueryBuilder { + /// Create a new query builder for a namespace + pub fn new(namespace: &str) -> Self { + Self { + namespace: namespace.to_string(), + filters: Vec::new(), + limit: 100, + offset: 0, + cursor: None, + include_count: false, + order_ascending: true, + } + } + + /// Add a filter for entries before a specific block + pub fn before_block(mut self, block_id: u64) -> Self { + self.filters.push(QueryFilter::BlockBefore(block_id)); + self + } + + /// Add a filter for entries after a specific block + pub fn after_block(mut self, block_id: u64) -> Self { + self.filters.push(QueryFilter::BlockAfter(block_id)); + self + } + + /// Add a filter for entries within a block range (inclusive) + pub fn block_range(mut self, start: u64, end: u64) -> Self { + self.filters.push(QueryFilter::BlockRange { start, end }); + self + } + + /// Add a filter for entries created before a timestamp + pub fn created_before(mut self, timestamp: i64) -> Self { + self.filters.push(QueryFilter::CreatedBefore(timestamp)); + self + } + + /// Add a filter for entries created after a timestamp + pub fn created_after(mut self, timestamp: i64) -> Self { + self.filters.push(QueryFilter::CreatedAfter(timestamp)); + self + } + + /// Add a filter for entries with a key prefix + pub fn key_prefix(mut self, prefix: impl Into>) -> Self { + self.filters.push(QueryFilter::KeyPrefix(prefix.into())); + self + } + + /// Set the maximum number of results to return + pub fn limit(mut self, limit: usize) -> Self { + self.limit = limit; + self + } + + /// Set the offset for pagination (alternative to cursor) + pub fn offset(mut self, offset: usize) -> Self { + self.offset = offset; + self + } + + /// Set a cursor for pagination + pub fn cursor(mut self, cursor: QueryCursor) -> Self { + self.cursor = Some(cursor); + self + } + + /// Include the total count in results (may be slower) + pub fn with_count(mut self) -> Self { + self.include_count = true; + self + } + + /// Order results in ascending order (default) + pub fn ascending(mut self) -> Self { + self.order_ascending = true; + self + } + + /// Order results in descending order + pub fn descending(mut self) -> Self { + self.order_ascending = false; + self + } + + /// Get the namespace + pub fn namespace(&self) -> &str { + &self.namespace + } + + /// Get all filters + pub fn filters(&self) -> &[QueryFilter] { + &self.filters + } + + /// Get the limit + pub fn get_limit(&self) -> usize { + self.limit + } + + /// Get the offset + pub fn get_offset(&self) -> usize { + self.offset + } + + /// Get the cursor + pub fn get_cursor(&self) -> Option<&QueryCursor> { + self.cursor.as_ref() + } + + /// Check if count should be included + pub fn should_include_count(&self) -> bool { + self.include_count + } + + /// Check if order is ascending + pub fn is_ascending(&self) -> bool { + self.order_ascending + } + + /// Build a combined filter from all added filters + pub fn build_filter(&self) -> Option { + if self.filters.is_empty() { + None + } else if self.filters.len() == 1 { + Some(self.filters[0].clone()) + } else { + Some(QueryFilter::And(self.filters.clone())) + } + } +} + +/// Generate a block index key for efficient range queries +/// +/// Format: `namespace:block_id(8 bytes BE):key_hash(32 bytes)` +/// Using big-endian ensures proper lexicographic ordering of block IDs. +pub fn block_index_key(namespace: &str, block_id: u64, key: &StorageKey) -> Vec { + let mut result = Vec::with_capacity(namespace.len() + 1 + 8 + 1 + 32); + result.extend_from_slice(namespace.as_bytes()); + result.push(b':'); + result.extend_from_slice(&block_id.to_be_bytes()); + result.push(b':'); + result.extend_from_slice(&key.hash()); + result +} + +/// Parse a block index key back to its components +/// +/// Returns (namespace, block_id, key_hash) if successful +pub fn parse_block_index_key(key: &[u8]) -> Option<(String, u64, [u8; 32])> { + // Find the first colon to separate namespace + let first_colon = key.iter().position(|&b| b == b':')?; + let namespace = std::str::from_utf8(&key[..first_colon]).ok()?.to_string(); + + // After namespace:, we have 8 bytes for block_id, then :, then 32 bytes for hash + let rest = &key[first_colon + 1..]; + if rest.len() < 8 + 1 + 32 { + return None; + } + + let block_id = u64::from_be_bytes(rest[..8].try_into().ok()?); + + // Verify the separator + if rest[8] != b':' { + return None; + } + + let mut key_hash = [0u8; 32]; + key_hash.copy_from_slice(&rest[9..41]); + + Some((namespace, block_id, key_hash)) +} + +/// Generate the start key for a block range query +pub fn block_range_start(namespace: &str, start_block: u64) -> Vec { + let mut result = Vec::with_capacity(namespace.len() + 1 + 8); + result.extend_from_slice(namespace.as_bytes()); + result.push(b':'); + result.extend_from_slice(&start_block.to_be_bytes()); + result +} + +/// Generate the end key for a block range query (exclusive) +pub fn block_range_end(namespace: &str, end_block: u64) -> Vec { + let mut result = Vec::with_capacity(namespace.len() + 1 + 8); + result.extend_from_slice(namespace.as_bytes()); + result.push(b':'); + // Add 1 to make it exclusive, handle overflow + let end = end_block.saturating_add(1); + result.extend_from_slice(&end.to_be_bytes()); + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_query_result_empty() { + let result = QueryResult::empty(10); + assert!(result.is_empty()); + assert_eq!(result.len(), 0); + assert_eq!(result.limit, 10); + assert!(!result.has_more); + } + + #[test] + fn test_query_cursor_serialization() { + let cursor = QueryCursor::new("submissions"); + let bytes = cursor.to_bytes(); + let decoded = QueryCursor::from_bytes(&bytes).expect("should decode"); + assert_eq!(decoded.namespace, "submissions"); + } + + #[test] + fn test_query_cursor_base64() { + let cursor = QueryCursor::from_last_item( + "submissions", + 1000, + &StorageKey::new("submissions", "test-key"), + ); + let encoded = cursor.to_base64(); + let decoded = QueryCursor::from_base64(&encoded).expect("should decode"); + assert_eq!(decoded.namespace, "submissions"); + assert_eq!(decoded.last_block_id, Some(1000)); + assert!(decoded.last_key_hash.is_some()); + } + + #[test] + fn test_query_filter_block_before() { + let filter = QueryFilter::BlockBefore(100); + assert!(filter.matches(Some(50), 0, b"key")); + assert!(filter.matches(Some(99), 0, b"key")); + assert!(!filter.matches(Some(100), 0, b"key")); + assert!(!filter.matches(Some(101), 0, b"key")); + // None block_id matches (no block info available) + assert!(filter.matches(None, 0, b"key")); + } + + #[test] + fn test_query_filter_block_after() { + let filter = QueryFilter::BlockAfter(100); + assert!(!filter.matches(Some(50), 0, b"key")); + assert!(!filter.matches(Some(100), 0, b"key")); + assert!(filter.matches(Some(101), 0, b"key")); + // None block_id doesn't match + assert!(!filter.matches(None, 0, b"key")); + } + + #[test] + fn test_query_filter_block_range() { + let filter = QueryFilter::BlockRange { + start: 50, + end: 150, + }; + assert!(!filter.matches(Some(49), 0, b"key")); + assert!(filter.matches(Some(50), 0, b"key")); + assert!(filter.matches(Some(100), 0, b"key")); + assert!(filter.matches(Some(150), 0, b"key")); + assert!(!filter.matches(Some(151), 0, b"key")); + } + + #[test] + fn test_query_filter_created_before() { + let filter = QueryFilter::CreatedBefore(1000); + assert!(filter.matches(None, 500, b"key")); + assert!(filter.matches(None, 999, b"key")); + assert!(!filter.matches(None, 1000, b"key")); + assert!(!filter.matches(None, 1001, b"key")); + } + + #[test] + fn test_query_filter_key_prefix() { + let filter = QueryFilter::KeyPrefix(b"prefix".to_vec()); + assert!(filter.matches(None, 0, b"prefix-key")); + assert!(filter.matches(None, 0, b"prefix")); + assert!(!filter.matches(None, 0, b"other-key")); + } + + #[test] + fn test_query_filter_and() { + let filter = QueryFilter::And(vec![ + QueryFilter::BlockAfter(50), + QueryFilter::BlockBefore(150), + ]); + assert!(!filter.matches(Some(50), 0, b"key")); + assert!(filter.matches(Some(100), 0, b"key")); + assert!(!filter.matches(Some(150), 0, b"key")); + } + + #[test] + fn test_query_filter_or() { + let filter = QueryFilter::Or(vec![ + QueryFilter::BlockBefore(50), + QueryFilter::BlockAfter(150), + ]); + assert!(filter.matches(Some(25), 0, b"key")); + assert!(!filter.matches(Some(100), 0, b"key")); + assert!(filter.matches(Some(200), 0, b"key")); + } + + #[test] + fn test_query_builder_basic() { + let builder = QueryBuilder::new("submissions") + .before_block(1000) + .limit(50); + + assert_eq!(builder.namespace(), "submissions"); + assert_eq!(builder.get_limit(), 50); + assert!(builder.is_ascending()); + } + + #[test] + fn test_query_builder_chaining() { + let builder = QueryBuilder::new("submissions") + .after_block(100) + .before_block(1000) + .limit(25) + .descending() + .with_count(); + + assert!(!builder.is_ascending()); + assert!(builder.should_include_count()); + assert_eq!(builder.filters().len(), 2); + } + + #[test] + fn test_query_builder_build_filter() { + let builder = QueryBuilder::new("test"); + assert!(builder.build_filter().is_none()); + + let builder = QueryBuilder::new("test").before_block(100); + let filter = builder.build_filter().expect("should have filter"); + assert!(matches!(filter, QueryFilter::BlockBefore(100))); + + let builder = QueryBuilder::new("test").after_block(50).before_block(150); + let filter = builder.build_filter().expect("should have filter"); + assert!(matches!(filter, QueryFilter::And(_))); + } + + #[test] + fn test_block_index_key_generation() { + let key = StorageKey::new("submissions", "test-key"); + let index_key = block_index_key("submissions", 1000, &key); + + // Should start with namespace + assert!(index_key.starts_with(b"submissions:")); + + // Parse it back + let (namespace, block_id, key_hash) = + parse_block_index_key(&index_key).expect("should parse"); + assert_eq!(namespace, "submissions"); + assert_eq!(block_id, 1000); + assert_eq!(key_hash, key.hash()); + } + + #[test] + fn test_block_index_key_ordering() { + let key1 = StorageKey::new("submissions", "key1"); + let key2 = StorageKey::new("submissions", "key2"); + + let idx1 = block_index_key("submissions", 100, &key1); + let idx2 = block_index_key("submissions", 200, &key2); + let idx3 = block_index_key("submissions", 100, &key2); + + // Block 100 should come before block 200 + assert!(idx1 < idx2); + assert!(idx3 < idx2); + } + + #[test] + fn test_block_range_keys() { + let start = block_range_start("submissions", 100); + let end = block_range_end("submissions", 200); + + assert!(start < end); + + // Keys within range should be between start and end + let key = StorageKey::new("submissions", "test"); + let idx_150 = block_index_key("submissions", 150, &key); + assert!(idx_150 > start); + assert!(idx_150 < end); + + // Keys outside range + let idx_50 = block_index_key("submissions", 50, &key); + assert!(idx_50 < start); + + let idx_250 = block_index_key("submissions", 250, &key); + assert!(idx_250 > end); + } + + #[test] + fn test_block_range_end_overflow() { + let end = block_range_end("test", u64::MAX); + // Should not panic, uses saturating_add + assert!(!end.is_empty()); + } + + #[test] + fn test_parse_block_index_key_invalid() { + // Too short + assert!(parse_block_index_key(b"short").is_none()); + + // No colon + assert!(parse_block_index_key(b"nonamespace").is_none()); + + // Invalid structure + let mut bad_key = b"ns:".to_vec(); + bad_key.extend_from_slice(&[0u8; 8]); // block_id + bad_key.push(b'X'); // wrong separator + bad_key.extend_from_slice(&[0u8; 32]); // hash + assert!(parse_block_index_key(&bad_key).is_none()); + } +} diff --git a/crates/distributed-storage/src/replication.rs b/crates/distributed-storage/src/replication.rs new file mode 100644 index 000000000..7f43856d4 --- /dev/null +++ b/crates/distributed-storage/src/replication.rs @@ -0,0 +1,595 @@ +//! Replication policy and consistency +//! +//! This module defines replication strategies for distributed storage, +//! including quorum reads/writes, eventual consistency, and conflict resolution. + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use tracing::debug; + +use crate::store::StoredValue; + +/// Configuration for replication behavior +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReplicationConfig { + /// Number of nodes that should store each piece of data + pub replication_factor: usize, + /// Minimum number of nodes that must confirm a write + pub write_quorum: usize, + /// Minimum number of nodes that must respond to a read + pub read_quorum: usize, + /// Time before retrying failed replication (in seconds) + pub retry_interval_secs: u64, + /// Maximum number of retry attempts + pub max_retries: u32, + /// Time before considering a node as failed (in seconds) + pub node_timeout_secs: u64, +} + +impl Default for ReplicationConfig { + fn default() -> Self { + Self { + replication_factor: 3, + write_quorum: 2, + read_quorum: 2, + retry_interval_secs: 30, + max_retries: 3, + node_timeout_secs: 10, + } + } +} + +impl ReplicationConfig { + /// Create a configuration optimized for consistency + pub fn strong_consistency() -> Self { + Self { + replication_factor: 3, + write_quorum: 3, // All nodes must confirm + read_quorum: 2, + retry_interval_secs: 10, + max_retries: 5, + node_timeout_secs: 5, + } + } + + /// Create a configuration optimized for availability + pub fn high_availability() -> Self { + Self { + replication_factor: 3, + write_quorum: 1, // Only one node needed + read_quorum: 1, + retry_interval_secs: 5, + max_retries: 10, + node_timeout_secs: 30, + } + } + + /// Create a configuration for single-node operation + pub fn single_node() -> Self { + Self { + replication_factor: 1, + write_quorum: 1, + read_quorum: 1, + retry_interval_secs: 0, + max_retries: 0, + node_timeout_secs: 0, + } + } + + /// Validate the configuration + pub fn validate(&self) -> Result<(), String> { + if self.write_quorum > self.replication_factor { + return Err(format!( + "Write quorum ({}) cannot exceed replication factor ({})", + self.write_quorum, self.replication_factor + )); + } + if self.read_quorum > self.replication_factor { + return Err(format!( + "Read quorum ({}) cannot exceed replication factor ({})", + self.read_quorum, self.replication_factor + )); + } + if self.write_quorum == 0 { + return Err("Write quorum must be at least 1".to_string()); + } + if self.read_quorum == 0 { + return Err("Read quorum must be at least 1".to_string()); + } + Ok(()) + } +} + +/// Strategy for resolving conflicts between divergent values +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] +pub enum ConflictResolution { + /// Last write wins (based on timestamp) + #[default] + LastWriteWins, + /// Highest version number wins + HighestVersion, + /// Custom merge function (caller provides) + Custom, +} + +/// Replication policy for a namespace +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReplicationPolicy { + /// The configuration + pub config: ReplicationConfig, + /// How to resolve conflicts + pub conflict_resolution: ConflictResolution, + /// Whether to enable anti-entropy repair + pub enable_anti_entropy: bool, + /// Interval for anti-entropy sync (in seconds) + pub anti_entropy_interval_secs: u64, +} + +impl Default for ReplicationPolicy { + fn default() -> Self { + Self { + config: ReplicationConfig::default(), + conflict_resolution: ConflictResolution::LastWriteWins, + enable_anti_entropy: true, + anti_entropy_interval_secs: 300, // 5 minutes + } + } +} + +impl ReplicationPolicy { + /// Create a new policy with default settings + pub fn new() -> Self { + Self::default() + } + + /// Set the replication configuration + pub fn with_config(mut self, config: ReplicationConfig) -> Self { + self.config = config; + self + } + + /// Set the conflict resolution strategy + pub fn with_conflict_resolution(mut self, strategy: ConflictResolution) -> Self { + self.conflict_resolution = strategy; + self + } + + /// Enable or disable anti-entropy + pub fn with_anti_entropy(mut self, enabled: bool) -> Self { + self.enable_anti_entropy = enabled; + self + } +} + +/// Conflict resolver for handling divergent values +pub struct ConflictResolver { + /// Resolution strategy + strategy: ConflictResolution, +} + +impl ConflictResolver { + /// Create a new resolver + pub fn new(strategy: ConflictResolution) -> Self { + Self { strategy } + } + + /// Resolve a conflict between multiple values + pub fn resolve(&self, values: Vec) -> Option { + if values.is_empty() { + return None; + } + + if values.len() == 1 { + return Some(values.into_iter().next().unwrap()); + } + + match self.strategy { + ConflictResolution::LastWriteWins => self.resolve_lww(values), + ConflictResolution::HighestVersion => self.resolve_version(values), + ConflictResolution::Custom => { + // For custom, just return the latest by default + self.resolve_lww(values) + } + } + } + + /// Resolve using last-write-wins + fn resolve_lww(&self, values: Vec) -> Option { + values.into_iter().max_by_key(|v| v.metadata.updated_at) + } + + /// Resolve using highest version + fn resolve_version(&self, values: Vec) -> Option { + values.into_iter().max_by_key(|v| v.metadata.version) + } +} + +/// Tracks replication state for a key +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReplicationState { + /// Key being tracked + pub key: String, + /// Nodes that have acknowledged the value + pub acknowledged_by: Vec, + /// Nodes that are pending acknowledgment + pub pending: Vec, + /// Last replication attempt + pub last_attempt: Option>, + /// Number of attempts made + pub attempt_count: u32, + /// Whether replication is complete + pub is_complete: bool, +} + +impl ReplicationState { + /// Create a new replication state + pub fn new(key: impl Into, target_nodes: Vec) -> Self { + Self { + key: key.into(), + acknowledged_by: Vec::new(), + pending: target_nodes, + last_attempt: None, + attempt_count: 0, + is_complete: false, + } + } + + /// Mark a node as having acknowledged + pub fn acknowledge(&mut self, node_id: &str) { + if let Some(pos) = self.pending.iter().position(|n| n == node_id) { + self.pending.remove(pos); + self.acknowledged_by.push(node_id.to_string()); + } + } + + /// Mark a replication attempt + pub fn mark_attempt(&mut self) { + self.last_attempt = Some(Utc::now()); + self.attempt_count += 1; + } + + /// Check if quorum is reached + pub fn has_quorum(&self, quorum_size: usize) -> bool { + self.acknowledged_by.len() >= quorum_size + } + + /// Check if replication should be retried + pub fn should_retry(&self, config: &ReplicationConfig) -> bool { + if self.is_complete { + return false; + } + + if self.attempt_count >= config.max_retries { + return false; + } + + if self.pending.is_empty() { + return false; + } + + if let Some(last) = self.last_attempt { + let elapsed = (Utc::now() - last).num_seconds(); + elapsed >= config.retry_interval_secs as i64 + } else { + true + } + } + + /// Mark replication as complete + pub fn complete(&mut self) { + self.is_complete = true; + } +} + +/// Manager for tracking replication across multiple keys +pub struct ReplicationManager { + /// Policy to use + policy: ReplicationPolicy, + /// Replication state per key + states: HashMap, + /// Conflict resolver + resolver: ConflictResolver, +} + +impl ReplicationManager { + /// Create a new replication manager + pub fn new(policy: ReplicationPolicy) -> Self { + let resolver = ConflictResolver::new(policy.conflict_resolution.clone()); + Self { + policy, + states: HashMap::new(), + resolver, + } + } + + /// Start tracking replication for a key + pub fn track(&mut self, key: String, target_nodes: Vec) { + let state = ReplicationState::new(key.clone(), target_nodes); + self.states.insert(key, state); + } + + /// Stop tracking a key + pub fn untrack(&mut self, key: &str) { + self.states.remove(key); + } + + /// Record an acknowledgment + pub fn acknowledge(&mut self, key: &str, node_id: &str) { + if let Some(state) = self.states.get_mut(key) { + state.acknowledge(node_id); + + // Check if quorum is reached + if state.has_quorum(self.policy.config.write_quorum) { + debug!("Quorum reached for key {}", key); + state.complete(); + } + } + } + + /// Get keys that need replication retries + pub fn get_pending_retries(&self) -> Vec { + self.states + .iter() + .filter(|(_, state)| state.should_retry(&self.policy.config)) + .map(|(key, _)| key.clone()) + .collect() + } + + /// Get the state for a key + pub fn get_state(&self, key: &str) -> Option<&ReplicationState> { + self.states.get(key) + } + + /// Resolve conflicts between multiple values + pub fn resolve_conflict(&self, values: Vec) -> Option { + self.resolver.resolve(values) + } + + /// Get the number of tracked keys + pub fn tracked_count(&self) -> usize { + self.states.len() + } + + /// Get the number of complete replications + pub fn completed_count(&self) -> usize { + self.states.values().filter(|s| s.is_complete).count() + } + + /// Get the number of pending replications + pub fn pending_count(&self) -> usize { + self.states.values().filter(|s| !s.is_complete).count() + } + + /// Clean up completed replications older than a duration + pub fn cleanup_completed(&mut self, max_age_secs: i64) { + let now = Utc::now(); + self.states.retain(|_, state| { + if !state.is_complete { + return true; + } + if let Some(last) = state.last_attempt { + (now - last).num_seconds() < max_age_secs + } else { + true + } + }); + } +} + +/// Quorum calculator for dynamic quorum based on available nodes +pub struct QuorumCalculator { + /// Minimum acceptable quorum + min_quorum: usize, + /// Target quorum (ideal case) + target_quorum: usize, + /// Total nodes in the cluster + total_nodes: usize, +} + +impl QuorumCalculator { + /// Create a new quorum calculator + pub fn new(min_quorum: usize, target_quorum: usize, total_nodes: usize) -> Self { + Self { + min_quorum, + target_quorum, + total_nodes, + } + } + + /// Calculate the quorum size based on available nodes + pub fn calculate(&self, available_nodes: usize) -> usize { + if available_nodes == 0 { + return self.min_quorum; + } + + // Use majority of available nodes, but at least min_quorum + let majority = (available_nodes / 2) + 1; + let effective = majority.min(self.target_quorum); + + effective.max(self.min_quorum) + } + + /// Check if we have enough nodes for any quorum + pub fn can_achieve_quorum(&self, available_nodes: usize) -> bool { + available_nodes >= self.min_quorum + } + + /// Get the minimum number of nodes needed + pub fn min_nodes_required(&self) -> usize { + self.min_quorum + } + + /// Get the total nodes in the cluster + pub fn total_nodes(&self) -> usize { + self.total_nodes + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::store::ValueMetadata; + + fn create_test_value(data: &[u8], version: u64, timestamp: i64) -> StoredValue { + let mut metadata = ValueMetadata::new(data, None); + metadata.version = version; + metadata.updated_at = timestamp; + StoredValue { + data: data.to_vec(), + metadata, + } + } + + #[test] + fn test_replication_config_default() { + let config = ReplicationConfig::default(); + assert!(config.validate().is_ok()); + assert_eq!(config.replication_factor, 3); + assert_eq!(config.write_quorum, 2); + assert_eq!(config.read_quorum, 2); + } + + #[test] + fn test_replication_config_validation() { + let mut config = ReplicationConfig { + write_quorum: 10, + ..Default::default() + }; // Exceeds replication factor + assert!(config.validate().is_err()); + + config.write_quorum = 0; + assert!(config.validate().is_err()); + } + + #[test] + fn test_conflict_resolver_lww() { + let resolver = ConflictResolver::new(ConflictResolution::LastWriteWins); + + let v1 = create_test_value(b"old", 1, 1000); + let v2 = create_test_value(b"new", 1, 2000); + + let result = resolver.resolve(vec![v1, v2]).unwrap(); + assert_eq!(result.data, b"new"); + } + + #[test] + fn test_conflict_resolver_version() { + let resolver = ConflictResolver::new(ConflictResolution::HighestVersion); + + let v1 = create_test_value(b"v1", 1, 2000); + let v2 = create_test_value(b"v2", 3, 1000); + + let result = resolver.resolve(vec![v1, v2]).unwrap(); + assert_eq!(result.data, b"v2"); // Higher version wins + } + + #[test] + fn test_replication_state() { + let mut state = ReplicationState::new( + "key1", + vec![ + "node1".to_string(), + "node2".to_string(), + "node3".to_string(), + ], + ); + + assert!(!state.has_quorum(2)); + + state.acknowledge("node1"); + assert!(!state.has_quorum(2)); + + state.acknowledge("node2"); + assert!(state.has_quorum(2)); + + assert_eq!(state.pending.len(), 1); + assert_eq!(state.acknowledged_by.len(), 2); + } + + #[test] + fn test_replication_state_retry() { + let config = ReplicationConfig { + retry_interval_secs: 1, + max_retries: 3, + ..Default::default() + }; + + let mut state = ReplicationState::new("key1", vec!["node1".to_string()]); + + // Should retry immediately + assert!(state.should_retry(&config)); + + state.mark_attempt(); + // Should not retry immediately after attempt + assert!(!state.should_retry(&config)); + + // After interval passes... + state.last_attempt = Some(Utc::now() - chrono::Duration::seconds(2)); + assert!(state.should_retry(&config)); + + // After max retries + state.attempt_count = 3; + assert!(!state.should_retry(&config)); + } + + #[test] + fn test_replication_manager() { + let policy = ReplicationPolicy::default(); + let mut manager = ReplicationManager::new(policy); + + manager.track( + "key1".to_string(), + vec!["node1".to_string(), "node2".to_string()], + ); + + assert_eq!(manager.tracked_count(), 1); + assert_eq!(manager.pending_count(), 1); + + manager.acknowledge("key1", "node1"); + manager.acknowledge("key1", "node2"); + + assert_eq!(manager.completed_count(), 1); + } + + #[test] + fn test_quorum_calculator() { + let calc = QuorumCalculator::new(1, 3, 5); + + assert_eq!(calc.calculate(5), 3); // Use target + assert_eq!(calc.calculate(3), 2); // Majority of 3 + assert_eq!(calc.calculate(1), 1); // Min quorum + + assert!(calc.can_achieve_quorum(1)); + assert!(!calc.can_achieve_quorum(0)); + } + + #[test] + fn test_replication_policy_builder() { + let policy = ReplicationPolicy::new() + .with_config(ReplicationConfig::strong_consistency()) + .with_conflict_resolution(ConflictResolution::HighestVersion) + .with_anti_entropy(false); + + assert_eq!(policy.config.write_quorum, 3); + assert_eq!( + policy.conflict_resolution, + ConflictResolution::HighestVersion + ); + assert!(!policy.enable_anti_entropy); + } + + #[test] + fn test_replication_manager_cleanup() { + let policy = ReplicationPolicy::default(); + let mut manager = ReplicationManager::new(policy); + + manager.track("key1".to_string(), vec![]); + manager.states.get_mut("key1").unwrap().complete(); + manager.states.get_mut("key1").unwrap().last_attempt = + Some(Utc::now() - chrono::Duration::seconds(100)); + + manager.cleanup_completed(50); + assert_eq!(manager.tracked_count(), 0); + } +} diff --git a/crates/distributed-storage/src/state_consensus.rs b/crates/distributed-storage/src/state_consensus.rs new file mode 100644 index 000000000..b4ddf5eb9 --- /dev/null +++ b/crates/distributed-storage/src/state_consensus.rs @@ -0,0 +1,1559 @@ +//! State Root Consensus Protocol +//! +//! This module provides cross-validator state verification with fraud proofs. +//! Validators coordinate to agree on global state roots using 2f+1 consensus, +//! enabling detection and proof of Byzantine behavior. +//! +//! # Overview +//! +//! The state consensus protocol allows validators to: +//! - Propose state roots for specific block numbers +//! - Vote on proposals by comparing against locally computed state +//! - Reach consensus when 2f+1 validators agree +//! - Generate fraud proofs when conflicting roots are detected +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ GlobalStateLinker โ”‚ +//! โ”‚ (aggregates per-challenge roots into global root) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ StateRootConsensus โ”‚ +//! โ”‚ (manages proposals, votes, and consensus achievement) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ–ผ โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ StateRootProposal โ”‚ โ”‚ StateRootVote โ”‚ +//! โ”‚ (proposer submits) โ”‚ โ”‚ (validators vote yes/no) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ FraudProof โ”‚ +//! โ”‚ (evidence of misbehavior) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` +//! +//! # Usage +//! +//! ```text +//! use platform_distributed_storage::state_consensus::{ +//! StateRootConsensus, GlobalStateLinker, StateRootProposal, +//! }; +//! use platform_core::Hotkey; +//! +//! // Create a consensus manager +//! let my_hotkey = Hotkey([0u8; 32]); +//! let mut consensus = StateRootConsensus::new(my_hotkey, 3); // quorum of 3 +//! +//! // Create a global state linker +//! let mut linker = GlobalStateLinker::new(); +//! linker.add_challenge_root("challenge-1", [1u8; 32]); +//! linker.add_challenge_root("challenge-2", [2u8; 32]); +//! +//! // Compute global root +//! let global_root = linker.compute_global_root(); +//! +//! // Propose a state root +//! let proposal = consensus.propose_state_root( +//! 100, // block number +//! global_root, +//! linker.get_challenge_roots().clone(), +//! ); +//! ``` + +#![allow(dead_code, unused_variables, unused_imports)] + +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use thiserror::Error; +use tracing::{debug, info, warn}; + +// ============================================================================ +// Error Types +// ============================================================================ + +/// Errors that can occur during state root consensus. +#[derive(Error, Debug, Clone)] +pub enum StateRootConsensusError { + /// Not enough votes to reach consensus. + #[error("Not enough votes: need {needed}, have {have}")] + NotEnoughVotes { + /// Number of votes needed for consensus + needed: usize, + /// Number of votes currently received + have: usize, + }, + + /// Conflicting state roots detected. + #[error("Conflicting roots: expected {expected}, got {got}")] + ConflictingRoots { + /// Expected root (hex encoded) + expected: String, + /// Actual root received (hex encoded) + got: String, + }, + + /// Invalid signature on message. + #[error("Invalid signature: {0}")] + InvalidSignature(String), + + /// Proposal timed out before reaching consensus. + #[error("Proposal timeout")] + ProposalTimeout, + + /// Fraud was detected during consensus. + #[error("Fraud detected: {0}")] + FraudDetected(String), + + /// Internal error occurred. + #[error("Internal error: {0}")] + InternalError(String), +} + +// ============================================================================ +// Core Data Structures +// ============================================================================ + +/// A proposal for a state root at a specific block number. +/// +/// The proposer computes the global state root from all challenge roots +/// and broadcasts this to other validators for verification. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateRootProposal { + /// Block number this proposal is for + pub block_number: u64, + + /// Hotkey of the validator proposing this root + pub proposer: Hotkey, + + /// The global state root (hash of all challenge roots) + pub global_state_root: [u8; 32], + + /// Individual challenge roots that make up the global root + /// Maps challenge_id -> merkle root of that challenge's data + pub challenge_roots: HashMap, + + /// Unix timestamp (milliseconds) when proposal was created + pub timestamp: i64, + + /// Cryptographic signature over the proposal content + pub signature: Vec, +} + +impl StateRootProposal { + /// Compute the hash of the proposal for signing/verification. + pub fn compute_hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.block_number.to_le_bytes()); + hasher.update(self.proposer.as_bytes()); + hasher.update(self.global_state_root); + + // Sort challenge roots for deterministic hashing + let mut sorted_roots: Vec<_> = self.challenge_roots.iter().collect(); + sorted_roots.sort_by_key(|(k, _)| *k); + for (challenge_id, root) in sorted_roots { + hasher.update(challenge_id.as_bytes()); + hasher.update(root); + } + + hasher.update(self.timestamp.to_le_bytes()); + hasher.finalize().into() + } + + /// Verify the global root matches the challenge roots. + pub fn verify_global_root(&self) -> bool { + let computed = compute_global_root_from_challenges(&self.challenge_roots); + computed == self.global_state_root + } +} + +/// A vote on a state root proposal. +/// +/// Validators compare the proposed root against their locally computed state +/// and vote accordingly. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateRootVote { + /// Block number this vote is for + pub block_number: u64, + + /// Hotkey of the voting validator + pub voter: Hotkey, + + /// The state root the voter computed locally + pub state_root: [u8; 32], + + /// Whether this voter agrees with the proposal + pub agrees_with_proposal: bool, + + /// Unix timestamp (milliseconds) when vote was cast + pub timestamp: i64, + + /// Cryptographic signature over the vote content + pub signature: Vec, +} + +impl StateRootVote { + /// Compute the hash of the vote for signing/verification. + pub fn compute_hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.block_number.to_le_bytes()); + hasher.update(self.voter.as_bytes()); + hasher.update(self.state_root); + hasher.update([self.agrees_with_proposal as u8]); + hasher.update(self.timestamp.to_le_bytes()); + hasher.finalize().into() + } +} + +/// Proof of fraudulent behavior by a validator. +/// +/// Generated when a validator is caught submitting conflicting state roots +/// or when their claimed root doesn't match the actual computed state. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FraudProof { + /// Hotkey of the validator creating this proof + pub accuser: Hotkey, + + /// Hotkey of the validator being accused + pub accused: Hotkey, + + /// Block number where fraud occurred + pub block_number: u64, + + /// The root the accused validator claimed + pub claimed_root: [u8; 32], + + /// The actual root as computed from the data + pub actual_root: [u8; 32], + + /// Optional merkle proof showing the incorrect data + pub merkle_proof: Option>, + + /// Unix timestamp (milliseconds) when proof was created + pub timestamp: i64, + + /// Cryptographic signature over the proof content + pub signature: Vec, +} + +impl FraudProof { + /// Compute the hash of the fraud proof for signing/verification. + pub fn compute_hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.accuser.as_bytes()); + hasher.update(self.accused.as_bytes()); + hasher.update(self.block_number.to_le_bytes()); + hasher.update(self.claimed_root); + hasher.update(self.actual_root); + hasher.update(self.timestamp.to_le_bytes()); + hasher.finalize().into() + } + + /// Check if the claimed and actual roots differ. + pub fn roots_differ(&self) -> bool { + self.claimed_root != self.actual_root + } +} + +/// Result of successful consensus. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ConsensusResult { + /// Block number consensus was achieved for + pub block_number: u64, + + /// The agreed-upon state root + pub agreed_root: [u8; 32], + + /// All votes that contributed to consensus + pub votes: Vec, + + /// Unix timestamp (milliseconds) when consensus was achieved + pub timestamp: i64, +} + +impl ConsensusResult { + /// Get the number of agreeing votes. + pub fn agreeing_votes(&self) -> usize { + self.votes.iter().filter(|v| v.agrees_with_proposal).count() + } + + /// Get the number of disagreeing votes. + pub fn disagreeing_votes(&self) -> usize { + self.votes + .iter() + .filter(|v| !v.agrees_with_proposal) + .count() + } +} + +// ============================================================================ +// Inclusion Proof +// ============================================================================ + +/// A step in the inclusion proof path. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ProofStep { + /// Hash of the sibling node + pub sibling_hash: [u8; 32], + /// Whether the current node is on the left (true) or right (false) + pub is_left: bool, +} + +/// Proof that a challenge's state is included in the global state root. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct InclusionProof { + /// The challenge this proof is for + pub challenge_id: String, + + /// The challenge's state root + pub challenge_root: [u8; 32], + + /// The global state root containing this challenge + pub global_root: [u8; 32], + + /// Merkle path from challenge leaf to global root + pub proof_path: Vec, +} + +impl InclusionProof { + /// Verify this inclusion proof is valid. + pub fn verify(&self) -> bool { + // Start with the leaf hash (challenge_id + challenge_root) + let mut hasher = Sha256::new(); + hasher.update(self.challenge_id.as_bytes()); + hasher.update(self.challenge_root); + let mut current: [u8; 32] = hasher.finalize().into(); + + // Walk up the proof path + for step in &self.proof_path { + // Combine based on position + current = if step.is_left { + // We are left child, sibling is on right + hash_pair(¤t, &step.sibling_hash) + } else { + // We are right child, sibling is on left + hash_pair(&step.sibling_hash, ¤t) + }; + } + + // Check if we reached the global root + current == self.global_root + } +} + +// ============================================================================ +// Global State Linker +// ============================================================================ + +/// Links per-challenge storage roots into a global state root. +/// +/// This struct maintains the mapping between individual challenge state roots +/// and computes the aggregate global root that validators agree upon. +#[derive(Clone, Debug, Default)] +pub struct GlobalStateLinker { + /// Maps challenge_id -> state root for that challenge + challenge_roots: HashMap, + + /// Cached global root (invalidated on changes) + cached_global_root: Option<[u8; 32]>, +} + +impl GlobalStateLinker { + /// Create a new empty state linker. + pub fn new() -> Self { + Self { + challenge_roots: HashMap::new(), + cached_global_root: None, + } + } + + /// Add or update a challenge root. + pub fn add_challenge_root(&mut self, challenge_id: &str, root: [u8; 32]) { + self.challenge_roots.insert(challenge_id.to_string(), root); + self.cached_global_root = None; // Invalidate cache + debug!( + challenge_id, + root = hex::encode(root), + "Added challenge root" + ); + } + + /// Remove a challenge root. + pub fn remove_challenge_root(&mut self, challenge_id: &str) { + self.challenge_roots.remove(challenge_id); + self.cached_global_root = None; // Invalidate cache + debug!(challenge_id, "Removed challenge root"); + } + + /// Compute the global state root from all challenge roots. + /// + /// The global root is computed as a merkle tree of all challenge roots, + /// sorted by challenge ID for determinism. + pub fn compute_global_root(&self) -> [u8; 32] { + if let Some(cached) = self.cached_global_root { + return cached; + } + + compute_global_root_from_challenges(&self.challenge_roots) + } + + /// Get a reference to all challenge roots. + pub fn get_challenge_roots(&self) -> &HashMap { + &self.challenge_roots + } + + /// Verify that a specific challenge root is included in the global state. + pub fn verify_inclusion(&self, challenge_id: &str, claimed_root: [u8; 32]) -> bool { + match self.challenge_roots.get(challenge_id) { + Some(root) => *root == claimed_root, + None => false, + } + } + + /// Build an inclusion proof for a challenge. + pub fn build_inclusion_proof(&self, challenge_id: &str) -> Option { + let challenge_root = *self.challenge_roots.get(challenge_id)?; + let global_root = self.compute_global_root(); + + // Build merkle proof path + let proof_path = build_merkle_proof_path(&self.challenge_roots, challenge_id); + + Some(InclusionProof { + challenge_id: challenge_id.to_string(), + challenge_root, + global_root, + proof_path, + }) + } + + /// Get the number of challenges tracked. + pub fn challenge_count(&self) -> usize { + self.challenge_roots.len() + } + + /// Check if empty. + pub fn is_empty(&self) -> bool { + self.challenge_roots.is_empty() + } +} + +// ============================================================================ +// State Root Consensus Manager +// ============================================================================ + +/// Manages the state root consensus protocol. +/// +/// This struct coordinates proposals, votes, and consensus detection, +/// maintaining the state needed to achieve 2f+1 agreement. +pub struct StateRootConsensus { + /// Our local hotkey for signing + local_hotkey: Hotkey, + + /// Number of votes required for consensus (2f+1) + quorum_size: usize, + + /// Current proposal being voted on + current_proposal: Option, + + /// Votes received for the current proposal + votes: HashMap, + + /// Detected fraud proofs + fraud_proofs: Vec, + + /// Completed consensus results (block_number -> result) + completed: HashMap, +} + +impl StateRootConsensus { + /// Create a new consensus manager. + /// + /// # Arguments + /// + /// * `local_hotkey` - Our hotkey for signing proposals and votes + /// * `quorum_size` - Number of votes needed for consensus (typically 2f+1) + pub fn new(local_hotkey: Hotkey, quorum_size: usize) -> Self { + info!( + hotkey = local_hotkey.to_hex(), + quorum_size, "Created state root consensus manager" + ); + + Self { + local_hotkey, + quorum_size, + current_proposal: None, + votes: HashMap::new(), + fraud_proofs: Vec::new(), + completed: HashMap::new(), + } + } + + /// Propose a new state root for consensus. + /// + /// Creates a proposal that other validators will vote on. + pub fn propose_state_root( + &mut self, + block_number: u64, + global_root: [u8; 32], + challenge_roots: HashMap, + ) -> StateRootProposal { + let timestamp = chrono::Utc::now().timestamp_millis(); + + let proposal = StateRootProposal { + block_number, + proposer: self.local_hotkey.clone(), + global_state_root: global_root, + challenge_roots, + timestamp, + signature: Vec::new(), // Signature would be added by caller with keypair + }; + + info!( + block_number, + root = hex::encode(global_root), + "Created state root proposal" + ); + + // Clear previous state and set new proposal + self.current_proposal = Some(proposal.clone()); + self.votes.clear(); + + proposal + } + + /// Receive and process an incoming proposal. + /// + /// Validates the proposal structure and stores it for voting. + pub fn receive_proposal( + &mut self, + proposal: StateRootProposal, + ) -> Result<(), StateRootConsensusError> { + // Verify the proposal's internal consistency + if !proposal.verify_global_root() { + return Err(StateRootConsensusError::ConflictingRoots { + expected: hex::encode(compute_global_root_from_challenges( + &proposal.challenge_roots, + )), + got: hex::encode(proposal.global_state_root), + }); + } + + debug!( + block_number = proposal.block_number, + proposer = proposal.proposer.to_hex(), + "Received state root proposal" + ); + + // Clear any previous proposal and votes + self.current_proposal = Some(proposal); + self.votes.clear(); + + Ok(()) + } + + /// Vote on the current proposal. + /// + /// Compares the proposal against the locally computed state root. + pub fn vote_on_proposal( + &mut self, + proposal: &StateRootProposal, + local_root: [u8; 32], + ) -> StateRootVote { + let agrees = local_root == proposal.global_state_root; + let timestamp = chrono::Utc::now().timestamp_millis(); + + let vote = StateRootVote { + block_number: proposal.block_number, + voter: self.local_hotkey.clone(), + state_root: local_root, + agrees_with_proposal: agrees, + timestamp, + signature: Vec::new(), // Signature would be added by caller with keypair + }; + + if !agrees { + warn!( + block_number = proposal.block_number, + expected = hex::encode(proposal.global_state_root), + local = hex::encode(local_root), + "Local state differs from proposal" + ); + } else { + debug!( + block_number = proposal.block_number, + "Voting in agreement with proposal" + ); + } + + // Record our own vote + self.votes.insert(self.local_hotkey.clone(), vote.clone()); + + vote + } + + /// Receive and process an incoming vote. + /// + /// Returns `Some(ConsensusResult)` if consensus is reached with this vote. + pub fn receive_vote( + &mut self, + vote: StateRootVote, + ) -> Result, StateRootConsensusError> { + let proposal = self.current_proposal.as_ref().ok_or_else(|| { + StateRootConsensusError::InternalError("No active proposal".to_string()) + })?; + + // Verify vote is for current proposal + if vote.block_number != proposal.block_number { + return Err(StateRootConsensusError::InternalError(format!( + "Vote block {} doesn't match proposal block {}", + vote.block_number, proposal.block_number + ))); + } + + // Check for conflicting votes from same voter + if let Some(existing) = self.votes.get(&vote.voter) { + if existing.state_root != vote.state_root { + // This is potential fraud - voter sending different roots + warn!( + voter = vote.voter.to_hex(), + first_root = hex::encode(existing.state_root), + second_root = hex::encode(vote.state_root), + "Detected conflicting votes from same validator" + ); + return Err(StateRootConsensusError::FraudDetected(format!( + "Validator {} sent conflicting votes", + vote.voter.to_hex() + ))); + } + } + + debug!( + voter = vote.voter.to_hex(), + agrees = vote.agrees_with_proposal, + "Received vote" + ); + + self.votes.insert(vote.voter.clone(), vote); + + // Check if we've reached consensus + Ok(self.check_consensus()) + } + + /// Check if consensus has been reached. + /// + /// Returns `Some(ConsensusResult)` if 2f+1 validators agree on the state root. + pub fn check_consensus(&self) -> Option { + let proposal = self.current_proposal.as_ref()?; + + // Count agreeing votes + let agreeing_votes: Vec<_> = self + .votes + .values() + .filter(|v| v.agrees_with_proposal) + .cloned() + .collect(); + + if agreeing_votes.len() >= self.quorum_size { + info!( + block_number = proposal.block_number, + votes = agreeing_votes.len(), + quorum = self.quorum_size, + "Consensus reached!" + ); + + Some(ConsensusResult { + block_number: proposal.block_number, + agreed_root: proposal.global_state_root, + votes: agreeing_votes, + timestamp: chrono::Utc::now().timestamp_millis(), + }) + } else { + None + } + } + + /// Create a fraud proof against a validator. + pub fn create_fraud_proof( + &self, + accused: &Hotkey, + claimed: [u8; 32], + actual: [u8; 32], + ) -> FraudProof { + let current_block = self + .current_proposal + .as_ref() + .map(|p| p.block_number) + .unwrap_or(0); + + let proof = FraudProof { + accuser: self.local_hotkey.clone(), + accused: accused.clone(), + block_number: current_block, + claimed_root: claimed, + actual_root: actual, + merkle_proof: None, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), // Signature would be added by caller with keypair + }; + + warn!( + accused = accused.to_hex(), + block_number = current_block, + claimed = hex::encode(claimed), + actual = hex::encode(actual), + "Created fraud proof" + ); + + proof + } + + /// Verify a fraud proof. + pub fn verify_fraud_proof(&self, proof: &FraudProof) -> bool { + // Basic validation: roots must actually differ + if !proof.roots_differ() { + debug!("Fraud proof invalid: roots are identical"); + return false; + } + + // If merkle proof is provided, verify it + if let Some(ref merkle_path) = proof.merkle_proof { + // Verify the merkle path leads to actual_root + let mut current = proof.claimed_root; + for sibling in merkle_path { + current = if current <= *sibling { + hash_pair(¤t, sibling) + } else { + hash_pair(sibling, ¤t) + }; + } + + // The merkle path should NOT lead to actual_root if fraud is genuine + // (the accused claimed a wrong root) + if current == proof.actual_root { + debug!("Fraud proof invalid: merkle path verifies to actual root"); + return false; + } + } + + debug!(accused = proof.accused.to_hex(), "Fraud proof verified"); + + true + } + + /// Get the current proposal if any. + pub fn current_proposal(&self) -> Option<&StateRootProposal> { + self.current_proposal.as_ref() + } + + /// Get all votes for the current proposal. + pub fn current_votes(&self) -> &HashMap { + &self.votes + } + + /// Get the number of votes received. + pub fn vote_count(&self) -> usize { + self.votes.len() + } + + /// Get completed consensus results. + pub fn get_completed(&self, block_number: u64) -> Option<&ConsensusResult> { + self.completed.get(&block_number) + } + + /// Store a completed consensus result. + pub fn store_completed(&mut self, result: ConsensusResult) { + let block = result.block_number; + self.completed.insert(block, result); + } +} + +// ============================================================================ +// Helper Functions +// ============================================================================ + +/// Compute global state root from challenge roots. +fn compute_global_root_from_challenges(challenge_roots: &HashMap) -> [u8; 32] { + if challenge_roots.is_empty() { + return [0u8; 32]; + } + + // Sort by challenge ID for determinism + let mut sorted_entries: Vec<_> = challenge_roots.iter().collect(); + sorted_entries.sort_by_key(|(k, _)| *k); + + // Build leaf hashes (challenge_id + root) + let leaves: Vec<[u8; 32]> = sorted_entries + .iter() + .map(|(id, root)| { + let mut hasher = Sha256::new(); + hasher.update(id.as_bytes()); + hasher.update(*root); + hasher.finalize().into() + }) + .collect(); + + // Compute merkle root of leaves + compute_merkle_root(&leaves) +} + +/// Compute merkle root from a list of leaf hashes. +fn compute_merkle_root(leaves: &[[u8; 32]]) -> [u8; 32] { + if leaves.is_empty() { + return [0u8; 32]; + } + + if leaves.len() == 1 { + return leaves[0]; + } + + let mut level = leaves.to_vec(); + + while level.len() > 1 { + let mut next_level = Vec::new(); + + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + // Odd number - duplicate last element + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + } + + level[0] +} + +/// Hash two 32-byte values together. +fn hash_pair(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(left); + hasher.update(right); + hasher.finalize().into() +} + +/// Build merkle proof path for a specific challenge. +fn build_merkle_proof_path( + challenge_roots: &HashMap, + target_challenge: &str, +) -> Vec { + if challenge_roots.is_empty() { + return Vec::new(); + } + + // Sort by challenge ID for determinism + let mut sorted_entries: Vec<_> = challenge_roots.iter().collect(); + sorted_entries.sort_by_key(|(k, _)| *k); + + // Find target index + let target_index = sorted_entries + .iter() + .position(|(k, _)| *k == target_challenge); + + let target_index = match target_index { + Some(idx) => idx, + None => return Vec::new(), + }; + + // Build leaf hashes + let leaves: Vec<[u8; 32]> = sorted_entries + .iter() + .map(|(id, root)| { + let mut hasher = Sha256::new(); + hasher.update(id.as_bytes()); + hasher.update(*root); + hasher.finalize().into() + }) + .collect(); + + // Build proof path + let mut proof_path = Vec::new(); + let mut level = leaves; + let mut index = target_index; + + while level.len() > 1 { + // Determine if we are left (even index) or right (odd index) child + let is_left = index % 2 == 0; + + // Get sibling index + let sibling_index = if is_left { + if index + 1 < level.len() { + index + 1 + } else { + index // duplicate self for odd case + } + } else { + index - 1 + }; + + proof_path.push(ProofStep { + sibling_hash: level[sibling_index], + is_left, + }); + + // Build next level + let mut next_level = Vec::new(); + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + index /= 2; + } + + proof_path +} + +// ============================================================================ +// Unit Tests +// ============================================================================ + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test_hotkey(seed: u8) -> Hotkey { + Hotkey([seed; 32]) + } + + #[test] + fn test_global_state_linker_basic() { + let mut linker = GlobalStateLinker::new(); + + assert!(linker.is_empty()); + assert_eq!(linker.challenge_count(), 0); + + // Add some challenge roots + linker.add_challenge_root("challenge-1", [1u8; 32]); + linker.add_challenge_root("challenge-2", [2u8; 32]); + + assert!(!linker.is_empty()); + assert_eq!(linker.challenge_count(), 2); + + // Compute global root + let root = linker.compute_global_root(); + assert_ne!(root, [0u8; 32]); + + // Verify inclusion + assert!(linker.verify_inclusion("challenge-1", [1u8; 32])); + assert!(!linker.verify_inclusion("challenge-1", [2u8; 32])); + assert!(!linker.verify_inclusion("challenge-3", [1u8; 32])); + } + + #[test] + fn test_global_state_linker_remove() { + let mut linker = GlobalStateLinker::new(); + + linker.add_challenge_root("challenge-1", [1u8; 32]); + linker.add_challenge_root("challenge-2", [2u8; 32]); + + let root_before = linker.compute_global_root(); + + linker.remove_challenge_root("challenge-1"); + + let root_after = linker.compute_global_root(); + assert_ne!(root_before, root_after); + assert_eq!(linker.challenge_count(), 1); + } + + #[test] + fn test_global_state_linker_deterministic() { + let mut linker1 = GlobalStateLinker::new(); + let mut linker2 = GlobalStateLinker::new(); + + // Add in different orders + linker1.add_challenge_root("b-challenge", [2u8; 32]); + linker1.add_challenge_root("a-challenge", [1u8; 32]); + + linker2.add_challenge_root("a-challenge", [1u8; 32]); + linker2.add_challenge_root("b-challenge", [2u8; 32]); + + // Should produce same root regardless of insertion order + assert_eq!(linker1.compute_global_root(), linker2.compute_global_root()); + } + + #[test] + fn test_inclusion_proof() { + let mut linker = GlobalStateLinker::new(); + + linker.add_challenge_root("challenge-1", [1u8; 32]); + linker.add_challenge_root("challenge-2", [2u8; 32]); + linker.add_challenge_root("challenge-3", [3u8; 32]); + + // Build and verify inclusion proof + let proof = linker + .build_inclusion_proof("challenge-2") + .expect("Should build proof"); + + assert_eq!(proof.challenge_id, "challenge-2"); + assert_eq!(proof.challenge_root, [2u8; 32]); + assert_eq!(proof.global_root, linker.compute_global_root()); + assert!(proof.verify()); + } + + #[test] + fn test_inclusion_proof_nonexistent() { + let mut linker = GlobalStateLinker::new(); + linker.add_challenge_root("challenge-1", [1u8; 32]); + + let proof = linker.build_inclusion_proof("nonexistent"); + assert!(proof.is_none()); + } + + #[test] + fn test_state_root_proposal() { + let hotkey = create_test_hotkey(1); + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + challenge_roots.insert("challenge-2".to_string(), [2u8; 32]); + + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let proposal = StateRootProposal { + block_number: 100, + proposer: hotkey, + global_state_root: global_root, + challenge_roots, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + // Verify global root consistency + assert!(proposal.verify_global_root()); + + // Compute hash + let hash = proposal.compute_hash(); + assert_ne!(hash, [0u8; 32]); + } + + #[test] + fn test_state_root_proposal_invalid_global_root() { + let hotkey = create_test_hotkey(1); + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + + let proposal = StateRootProposal { + block_number: 100, + proposer: hotkey, + global_state_root: [0u8; 32], // Wrong root + challenge_roots, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + // Should fail verification + assert!(!proposal.verify_global_root()); + } + + #[test] + fn test_state_root_vote() { + let hotkey = create_test_hotkey(1); + let state_root = [42u8; 32]; + + let vote = StateRootVote { + block_number: 100, + voter: hotkey, + state_root, + agrees_with_proposal: true, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let hash = vote.compute_hash(); + assert_ne!(hash, [0u8; 32]); + } + + #[test] + fn test_fraud_proof() { + let accuser = create_test_hotkey(1); + let accused = create_test_hotkey(2); + + let proof = FraudProof { + accuser, + accused, + block_number: 100, + claimed_root: [1u8; 32], + actual_root: [2u8; 32], + merkle_proof: None, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + assert!(proof.roots_differ()); + + let hash = proof.compute_hash(); + assert_ne!(hash, [0u8; 32]); + } + + #[test] + fn test_fraud_proof_same_roots() { + let accuser = create_test_hotkey(1); + let accused = create_test_hotkey(2); + + let proof = FraudProof { + accuser, + accused, + block_number: 100, + claimed_root: [1u8; 32], + actual_root: [1u8; 32], // Same as claimed + merkle_proof: None, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + assert!(!proof.roots_differ()); + } + + #[test] + fn test_state_root_consensus_creation() { + let hotkey = create_test_hotkey(1); + let consensus = StateRootConsensus::new(hotkey, 3); + + assert_eq!(consensus.quorum_size, 3); + assert!(consensus.current_proposal().is_none()); + assert_eq!(consensus.vote_count(), 0); + } + + #[test] + fn test_state_root_consensus_propose() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + + let global_root = compute_global_root_from_challenges(&challenge_roots); + let proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + assert_eq!(proposal.block_number, 100); + assert!(consensus.current_proposal().is_some()); + } + + #[test] + fn test_state_root_consensus_receive_proposal() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let proposal = StateRootProposal { + block_number: 100, + proposer: create_test_hotkey(2), + global_state_root: global_root, + challenge_roots, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_proposal(proposal); + assert!(result.is_ok()); + assert!(consensus.current_proposal().is_some()); + } + + #[test] + fn test_state_root_consensus_receive_invalid_proposal() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + + let proposal = StateRootProposal { + block_number: 100, + proposer: create_test_hotkey(2), + global_state_root: [0u8; 32], // Invalid root + challenge_roots, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_proposal(proposal); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + StateRootConsensusError::ConflictingRoots { .. } + )); + } + + #[test] + fn test_state_root_consensus_voting() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey.clone(), 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + // Vote in agreement + let vote = consensus.vote_on_proposal(&proposal, global_root); + assert!(vote.agrees_with_proposal); + assert_eq!(vote.state_root, global_root); + } + + #[test] + fn test_state_root_consensus_voting_disagreement() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + // Vote with different local state + let different_root = [99u8; 32]; + let vote = consensus.vote_on_proposal(&proposal, different_root); + assert!(!vote.agrees_with_proposal); + } + + #[test] + fn test_state_root_consensus_quorum() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey.clone(), 2); // Quorum of 2 + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + // First vote (our own) + let vote1 = consensus.vote_on_proposal(&proposal, global_root); + assert!(consensus.check_consensus().is_none()); // Not enough yet + + // Second vote from another validator + let vote2 = StateRootVote { + block_number: 100, + voter: create_test_hotkey(2), + state_root: global_root, + agrees_with_proposal: true, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_vote(vote2).expect("Should accept vote"); + assert!(result.is_some()); // Should have consensus now + + let consensus_result = result.unwrap(); + assert_eq!(consensus_result.block_number, 100); + assert_eq!(consensus_result.agreed_root, global_root); + assert_eq!(consensus_result.agreeing_votes(), 2); + } + + #[test] + fn test_state_root_consensus_conflicting_votes() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let _proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + // First vote from validator 2 + let vote1 = StateRootVote { + block_number: 100, + voter: create_test_hotkey(2), + state_root: global_root, + agrees_with_proposal: true, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + consensus.receive_vote(vote1).expect("Should accept vote"); + + // Conflicting vote from same validator + let vote2 = StateRootVote { + block_number: 100, + voter: create_test_hotkey(2), + state_root: [99u8; 32], // Different root! + agrees_with_proposal: false, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_vote(vote2); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + StateRootConsensusError::FraudDetected(_) + )); + } + + #[test] + fn test_create_and_verify_fraud_proof() { + let hotkey = create_test_hotkey(1); + let consensus = StateRootConsensus::new(hotkey, 3); + + let accused = create_test_hotkey(2); + let claimed = [1u8; 32]; + let actual = [2u8; 32]; + + let proof = consensus.create_fraud_proof(&accused, claimed, actual); + + assert!(proof.roots_differ()); + assert!(consensus.verify_fraud_proof(&proof)); + } + + #[test] + fn test_verify_invalid_fraud_proof() { + let hotkey = create_test_hotkey(1); + let consensus = StateRootConsensus::new(hotkey, 3); + + // Proof with same roots (not fraud) + let proof = FraudProof { + accuser: create_test_hotkey(1), + accused: create_test_hotkey(2), + block_number: 100, + claimed_root: [1u8; 32], + actual_root: [1u8; 32], // Same! + merkle_proof: None, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + assert!(!consensus.verify_fraud_proof(&proof)); + } + + #[test] + fn test_consensus_result_methods() { + let result = ConsensusResult { + block_number: 100, + agreed_root: [42u8; 32], + votes: vec![ + StateRootVote { + block_number: 100, + voter: create_test_hotkey(1), + state_root: [42u8; 32], + agrees_with_proposal: true, + timestamp: 0, + signature: Vec::new(), + }, + StateRootVote { + block_number: 100, + voter: create_test_hotkey(2), + state_root: [42u8; 32], + agrees_with_proposal: true, + timestamp: 0, + signature: Vec::new(), + }, + StateRootVote { + block_number: 100, + voter: create_test_hotkey(3), + state_root: [99u8; 32], + agrees_with_proposal: false, + timestamp: 0, + signature: Vec::new(), + }, + ], + timestamp: 0, + }; + + assert_eq!(result.agreeing_votes(), 2); + assert_eq!(result.disagreeing_votes(), 1); + } + + #[test] + fn test_store_and_get_completed() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let result = ConsensusResult { + block_number: 100, + agreed_root: [42u8; 32], + votes: Vec::new(), + timestamp: chrono::Utc::now().timestamp_millis(), + }; + + consensus.store_completed(result.clone()); + + let retrieved = consensus.get_completed(100); + assert!(retrieved.is_some()); + assert_eq!(retrieved.unwrap().block_number, 100); + + assert!(consensus.get_completed(101).is_none()); + } + + #[test] + fn test_merkle_root_computation() { + // Empty case + let empty: Vec<[u8; 32]> = Vec::new(); + assert_eq!(compute_merkle_root(&empty), [0u8; 32]); + + // Single leaf + let single = vec![[1u8; 32]]; + assert_eq!(compute_merkle_root(&single), [1u8; 32]); + + // Two leaves + let two = vec![[1u8; 32], [2u8; 32]]; + let root_two = compute_merkle_root(&two); + assert_ne!(root_two, [0u8; 32]); + assert_ne!(root_two, [1u8; 32]); + assert_ne!(root_two, [2u8; 32]); + + // Three leaves (odd number) + let three = vec![[1u8; 32], [2u8; 32], [3u8; 32]]; + let root_three = compute_merkle_root(&three); + assert_ne!(root_three, root_two); + } + + #[test] + fn test_hash_pair() { + let a = [1u8; 32]; + let b = [2u8; 32]; + + let hash1 = hash_pair(&a, &b); + let hash2 = hash_pair(&b, &a); + + // Order matters + assert_ne!(hash1, hash2); + + // Deterministic + assert_eq!(hash_pair(&a, &b), hash_pair(&a, &b)); + } + + #[test] + fn test_empty_global_state_linker() { + let linker = GlobalStateLinker::new(); + + assert!(linker.is_empty()); + assert_eq!(linker.compute_global_root(), [0u8; 32]); + assert!(linker.build_inclusion_proof("anything").is_none()); + } + + #[test] + fn test_single_challenge_inclusion_proof() { + let mut linker = GlobalStateLinker::new(); + linker.add_challenge_root("challenge-1", [42u8; 32]); + + let proof = linker + .build_inclusion_proof("challenge-1") + .expect("Should build proof"); + assert!(proof.verify()); + } + + #[test] + fn test_receive_vote_no_proposal() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let vote = StateRootVote { + block_number: 100, + voter: create_test_hotkey(2), + state_root: [42u8; 32], + agrees_with_proposal: true, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_vote(vote); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + StateRootConsensusError::InternalError(_) + )); + } + + #[test] + fn test_receive_vote_wrong_block() { + let hotkey = create_test_hotkey(1); + let mut consensus = StateRootConsensus::new(hotkey, 3); + + let mut challenge_roots = HashMap::new(); + challenge_roots.insert("challenge-1".to_string(), [1u8; 32]); + let global_root = compute_global_root_from_challenges(&challenge_roots); + + let _proposal = consensus.propose_state_root(100, global_root, challenge_roots); + + let vote = StateRootVote { + block_number: 999, // Wrong block! + voter: create_test_hotkey(2), + state_root: global_root, + agrees_with_proposal: true, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + }; + + let result = consensus.receive_vote(vote); + assert!(result.is_err()); + } + + #[test] + fn test_error_display() { + let err1 = StateRootConsensusError::NotEnoughVotes { needed: 5, have: 2 }; + assert!(err1.to_string().contains("5")); + assert!(err1.to_string().contains("2")); + + let err2 = StateRootConsensusError::ConflictingRoots { + expected: "abc".to_string(), + got: "def".to_string(), + }; + assert!(err2.to_string().contains("abc")); + assert!(err2.to_string().contains("def")); + + let err3 = StateRootConsensusError::InvalidSignature("bad sig".to_string()); + assert!(err3.to_string().contains("bad sig")); + + let err4 = StateRootConsensusError::ProposalTimeout; + assert!(err4.to_string().contains("timeout")); + + let err5 = StateRootConsensusError::FraudDetected("fraud!".to_string()); + assert!(err5.to_string().contains("fraud")); + + let err6 = StateRootConsensusError::InternalError("internal".to_string()); + assert!(err6.to_string().contains("internal")); + } + + #[test] + fn test_global_root_update_invalidates_cache() { + let mut linker = GlobalStateLinker::new(); + + linker.add_challenge_root("challenge-1", [1u8; 32]); + let root1 = linker.compute_global_root(); + + linker.add_challenge_root("challenge-1", [2u8; 32]); // Update + let root2 = linker.compute_global_root(); + + assert_ne!(root1, root2); + } + + #[test] + fn test_many_challenges_inclusion_proof() { + let mut linker = GlobalStateLinker::new(); + + // Add many challenges + for i in 0..10 { + linker.add_challenge_root(&format!("challenge-{}", i), [i as u8; 32]); + } + + // Build and verify proofs for each + for i in 0..10 { + let proof = linker + .build_inclusion_proof(&format!("challenge-{}", i)) + .expect("Should build proof"); + assert!(proof.verify(), "Proof for challenge-{} failed", i); + } + } +} diff --git a/crates/distributed-storage/src/store.rs b/crates/distributed-storage/src/store.rs new file mode 100644 index 000000000..51f0e9e7a --- /dev/null +++ b/crates/distributed-storage/src/store.rs @@ -0,0 +1,564 @@ +//! Distributed storage abstraction +//! +//! This module defines the core traits and types for distributed key-value storage. + +use async_trait::async_trait; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::fmt; + +use crate::error::StorageResult; +use crate::query::{QueryBuilder, QueryResult}; + +/// Key for distributed storage +/// +/// Keys are organized by namespace (e.g., "submissions", "evaluations", "weights") +/// and an arbitrary key within that namespace. +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct StorageKey { + /// Namespace for the key (e.g., "submissions", "evaluations") + pub namespace: String, + /// Key data within the namespace + pub key: Vec, +} + +impl StorageKey { + /// Create a new storage key + pub fn new(namespace: &str, key: impl AsRef<[u8]>) -> Self { + Self { + namespace: namespace.to_string(), + key: key.as_ref().to_vec(), + } + } + + /// Create a key for a submission + pub fn submission(challenge_id: &str, hash: &str) -> Self { + Self::new("submissions", format!("{}:{}", challenge_id, hash)) + } + + /// Create a key for an evaluation + pub fn evaluation(challenge_id: &str, submission_hash: &str, validator: &str) -> Self { + Self::new( + "evaluations", + format!("{}:{}:{}", challenge_id, submission_hash, validator), + ) + } + + /// Create a key for weights + pub fn weights(challenge_id: &str, epoch: u64) -> Self { + Self::new("weights", format!("{}:{}", challenge_id, epoch)) + } + + /// Create a key for a challenge + pub fn challenge(challenge_id: &str) -> Self { + Self::new("challenges", challenge_id) + } + + /// Convert key to bytes for storage + pub fn to_bytes(&self) -> Vec { + let mut bytes = Vec::with_capacity(self.namespace.len() + 1 + self.key.len()); + bytes.extend_from_slice(self.namespace.as_bytes()); + bytes.push(b':'); + bytes.extend_from_slice(&self.key); + bytes + } + + /// Compute SHA256 hash of the key (for DHT routing) + pub fn hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.to_bytes()); + hasher.finalize().into() + } + + /// Get the key as a string if it's valid UTF-8 + pub fn key_string(&self) -> Option { + String::from_utf8(self.key.clone()).ok() + } +} + +impl fmt::Display for StorageKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(key_str) = self.key_string() { + write!(f, "{}:{}", self.namespace, key_str) + } else { + write!(f, "{}:{}", self.namespace, hex::encode(&self.key)) + } + } +} + +/// Metadata associated with stored values +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValueMetadata { + /// When the value was created (Unix timestamp in milliseconds) + pub created_at: i64, + /// When the value was last updated (Unix timestamp in milliseconds) + pub updated_at: i64, + /// Version number for optimistic concurrency + pub version: u64, + /// Node that originally wrote this value + pub origin_node: Option, + /// SHA256 hash of the value + pub value_hash: [u8; 32], + /// Size of the value in bytes + pub size: usize, + /// Time-to-live in seconds (0 = never expires) + pub ttl_seconds: u64, +} + +impl ValueMetadata { + /// Create new metadata for a value + pub fn new(value: &[u8], origin_node: Option) -> Self { + let now = chrono::Utc::now().timestamp_millis(); + let mut hasher = Sha256::new(); + hasher.update(value); + + Self { + created_at: now, + updated_at: now, + version: 1, + origin_node, + value_hash: hasher.finalize().into(), + size: value.len(), + ttl_seconds: 0, + } + } + + /// Create metadata for an update + pub fn update(&self, value: &[u8], origin_node: Option) -> Self { + let now = chrono::Utc::now().timestamp_millis(); + let mut hasher = Sha256::new(); + hasher.update(value); + + Self { + created_at: self.created_at, + updated_at: now, + version: self.version + 1, + origin_node, + value_hash: hasher.finalize().into(), + size: value.len(), + ttl_seconds: self.ttl_seconds, + } + } + + /// Check if the value has expired + pub fn is_expired(&self) -> bool { + if self.ttl_seconds == 0 { + return false; + } + let now = chrono::Utc::now().timestamp_millis(); + let expires_at = self.created_at + (self.ttl_seconds as i64 * 1000); + now > expires_at + } +} + +/// Stored value with metadata +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StoredValue { + /// The actual value data + pub data: Vec, + /// Metadata about the value + pub metadata: ValueMetadata, +} + +impl StoredValue { + /// Create a new stored value + pub fn new(data: Vec, origin_node: Option) -> Self { + let metadata = ValueMetadata::new(&data, origin_node); + Self { data, metadata } + } + + /// Check if this value is newer than another + pub fn is_newer_than(&self, other: &Self) -> bool { + // First compare versions + if self.metadata.version != other.metadata.version { + return self.metadata.version > other.metadata.version; + } + // Fall back to timestamp comparison + self.metadata.updated_at > other.metadata.updated_at + } +} + +/// Options for get operations +#[derive(Clone, Debug, Default)] +pub struct GetOptions { + /// If true, only check local storage + pub local_only: bool, + /// If true, require quorum read + pub quorum_read: bool, + /// Custom quorum size (defaults to replication policy) + pub quorum_size: Option, +} + +/// Options for put operations +#[derive(Clone, Debug, Default)] +pub struct PutOptions { + /// If true, only write to local storage + pub local_only: bool, + /// If true, require quorum write + pub quorum_write: bool, + /// Custom quorum size (defaults to replication policy) + pub quorum_size: Option, + /// Time-to-live in seconds (0 = never expires) + pub ttl_seconds: u64, + /// Expected version for optimistic concurrency (None = ignore) + pub expected_version: Option, +} + +/// Result of a list operation +#[derive(Clone, Debug)] +pub struct ListResult { + /// Key-value pairs + pub items: Vec<(StorageKey, StoredValue)>, + /// Whether there are more results + pub has_more: bool, + /// Continuation token for pagination + pub continuation_token: Option>, +} + +/// Trait for distributed key-value storage +/// +/// This trait defines the interface for a distributed storage system that can +/// operate in both local-only and DHT-backed modes. +#[async_trait] +pub trait DistributedStore: Send + Sync { + /// Get a value by key + /// + /// # Arguments + /// * `key` - The storage key + /// * `options` - Options for the get operation + /// + /// # Returns + /// The stored value if found, None otherwise + async fn get( + &self, + key: &StorageKey, + options: GetOptions, + ) -> StorageResult>; + + /// Put a value + /// + /// # Arguments + /// * `key` - The storage key + /// * `value` - The value to store + /// * `options` - Options for the put operation + /// + /// # Returns + /// The metadata of the stored value + async fn put( + &self, + key: StorageKey, + value: Vec, + options: PutOptions, + ) -> StorageResult; + + /// Delete a value + /// + /// # Arguments + /// * `key` - The storage key + /// + /// # Returns + /// true if the value was deleted, false if it didn't exist + async fn delete(&self, key: &StorageKey) -> StorageResult; + + /// Check if a key exists + /// + /// # Arguments + /// * `key` - The storage key + /// + /// # Returns + /// true if the key exists + async fn exists(&self, key: &StorageKey) -> StorageResult; + + /// List all key-value pairs with a given namespace prefix + /// + /// # Arguments + /// * `namespace` - The namespace to list + /// * `prefix` - Optional prefix within the namespace + /// * `limit` - Maximum number of results + /// * `continuation_token` - Token for pagination + /// + /// # Returns + /// List of key-value pairs + async fn list_prefix( + &self, + namespace: &str, + prefix: Option<&[u8]>, + limit: usize, + continuation_token: Option<&[u8]>, + ) -> StorageResult; + + /// Get statistics about the storage + async fn stats(&self) -> StorageResult; + + /// List entries created before a specific block + /// + /// This uses a block-indexed secondary index for efficient range queries. + /// + /// # Arguments + /// * `namespace` - The namespace to query + /// * `block_id` - Return entries with block_id < this value + /// * `limit` - Maximum number of results to return + /// + /// # Returns + /// List of key-value pairs ordered by block_id (ascending) + async fn list_before_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult; + + /// List entries created after a specific block + /// + /// This uses a block-indexed secondary index for efficient range queries. + /// + /// # Arguments + /// * `namespace` - The namespace to query + /// * `block_id` - Return entries with block_id > this value + /// * `limit` - Maximum number of results to return + /// + /// # Returns + /// List of key-value pairs ordered by block_id (ascending) + async fn list_after_block( + &self, + namespace: &str, + block_id: u64, + limit: usize, + ) -> StorageResult; + + /// List entries within a block range + /// + /// Equivalent to: SELECT * FROM namespace WHERE block_id >= start AND block_id <= end + /// + /// # Arguments + /// * `namespace` - The namespace to query + /// * `start_block` - Minimum block_id (inclusive) + /// * `end_block` - Maximum block_id (inclusive) + /// * `limit` - Maximum number of results to return + /// + /// # Returns + /// List of key-value pairs ordered by block_id (ascending) + async fn list_range( + &self, + namespace: &str, + start_block: u64, + end_block: u64, + limit: usize, + ) -> StorageResult; + + /// Count entries in a namespace + /// + /// # Arguments + /// * `namespace` - The namespace to count + /// + /// # Returns + /// Number of entries in the namespace + async fn count_by_namespace(&self, namespace: &str) -> StorageResult; + + /// Execute a query built with QueryBuilder + /// + /// This is the most flexible query method, supporting complex filters + /// and pagination via cursors. + /// + /// # Arguments + /// * `query` - The query builder with filters configured + /// + /// # Returns + /// Query result with matching items and pagination info + async fn query(&self, query: QueryBuilder) -> StorageResult; + + /// Store a value with an associated block_id for indexing + /// + /// This creates both the primary entry and a secondary index entry + /// for efficient block-based queries. + /// + /// # Arguments + /// * `key` - The storage key + /// * `value` - The value to store + /// * `block_id` - The block number to associate with this entry + /// * `options` - Options for the put operation + /// + /// # Returns + /// The metadata of the stored value + async fn put_with_block( + &self, + key: StorageKey, + value: Vec, + block_id: u64, + options: PutOptions, + ) -> StorageResult; +} + +/// Storage statistics +#[derive(Clone, Debug, Default)] +pub struct StorageStats { + /// Total number of keys + pub total_keys: u64, + /// Total size in bytes + pub total_bytes: u64, + /// Number of keys per namespace + pub keys_per_namespace: std::collections::HashMap, + /// Number of local replicas + pub local_replicas: u64, + /// Number of remote peers (for DHT mode) + pub remote_peers: u64, +} + +/// Convenience methods for DistributedStore +#[async_trait] +pub trait DistributedStoreExt: DistributedStore { + /// Get a value with default options + async fn get_simple(&self, key: &StorageKey) -> StorageResult>> { + let result = self.get(key, GetOptions::default()).await?; + Ok(result.map(|v| v.data)) + } + + /// Put a value with default options + async fn put_simple(&self, key: StorageKey, value: Vec) -> StorageResult { + self.put(key, value, PutOptions::default()).await + } + + /// Get and deserialize a value + async fn get_json( + &self, + key: &StorageKey, + ) -> StorageResult> { + let result = self.get(key, GetOptions::default()).await?; + match result { + Some(stored) => { + let value: T = serde_json::from_slice(&stored.data)?; + Ok(Some(value)) + } + None => Ok(None), + } + } + + /// Serialize and put a value + async fn put_json( + &self, + key: StorageKey, + value: &T, + ) -> StorageResult { + let data = serde_json::to_vec(value)?; + self.put(key, data, PutOptions::default()).await + } +} + +// Blanket implementation for all DistributedStore implementors +impl DistributedStoreExt for T {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_storage_key_new() { + let key = StorageKey::new("test", "mykey"); + assert_eq!(key.namespace, "test"); + assert_eq!(key.key, b"mykey"); + } + + #[test] + fn test_storage_key_submission() { + let key = StorageKey::submission("challenge1", "abc123"); + assert_eq!(key.namespace, "submissions"); + assert_eq!(key.key_string(), Some("challenge1:abc123".to_string())); + } + + #[test] + fn test_storage_key_evaluation() { + let key = StorageKey::evaluation("challenge1", "sub123", "validator1"); + assert_eq!(key.namespace, "evaluations"); + assert_eq!( + key.key_string(), + Some("challenge1:sub123:validator1".to_string()) + ); + } + + #[test] + fn test_storage_key_weights() { + let key = StorageKey::weights("challenge1", 42); + assert_eq!(key.namespace, "weights"); + assert_eq!(key.key_string(), Some("challenge1:42".to_string())); + } + + #[test] + fn test_storage_key_to_bytes() { + let key = StorageKey::new("ns", "key"); + let bytes = key.to_bytes(); + assert_eq!(bytes, b"ns:key"); + } + + #[test] + fn test_storage_key_hash() { + let key1 = StorageKey::new("test", "key1"); + let key2 = StorageKey::new("test", "key1"); + let key3 = StorageKey::new("test", "key2"); + + assert_eq!(key1.hash(), key2.hash()); + assert_ne!(key1.hash(), key3.hash()); + } + + #[test] + fn test_storage_key_display() { + let key = StorageKey::new("test", "mykey"); + assert_eq!(format!("{}", key), "test:mykey"); + } + + #[test] + fn test_value_metadata_new() { + let value = b"test value"; + let metadata = ValueMetadata::new(value, Some("node1".to_string())); + + assert_eq!(metadata.version, 1); + assert_eq!(metadata.size, value.len()); + assert_eq!(metadata.origin_node, Some("node1".to_string())); + assert_eq!(metadata.ttl_seconds, 0); + } + + #[test] + fn test_value_metadata_update() { + let value1 = b"test value"; + let value2 = b"updated value"; + let metadata1 = ValueMetadata::new(value1, Some("node1".to_string())); + let metadata2 = metadata1.update(value2, Some("node2".to_string())); + + assert_eq!(metadata2.version, 2); + assert_eq!(metadata2.size, value2.len()); + assert_eq!(metadata2.created_at, metadata1.created_at); + assert!(metadata2.updated_at >= metadata1.updated_at); + } + + #[test] + fn test_value_metadata_expiry() { + let value = b"test"; + let mut metadata = ValueMetadata::new(value, None); + + // No TTL - never expires + assert!(!metadata.is_expired()); + + // Set TTL in the past + metadata.ttl_seconds = 1; + metadata.created_at = chrono::Utc::now().timestamp_millis() - 10000; + assert!(metadata.is_expired()); + } + + #[test] + fn test_stored_value_is_newer_than() { + let value1 = StoredValue::new(b"v1".to_vec(), None); + + // Simulate time passing + std::thread::sleep(std::time::Duration::from_millis(10)); + + let value2 = StoredValue::new(b"v2".to_vec(), None); + + // value2 should be newer due to higher version + let mut v1_modified = value1.clone(); + v1_modified.metadata.version = 1; + let mut v2_modified = value2.clone(); + v2_modified.metadata.version = 2; + + assert!(v2_modified.is_newer_than(&v1_modified)); + assert!(!v1_modified.is_newer_than(&v2_modified)); + } +} diff --git a/crates/distributed-storage/src/submission.rs b/crates/distributed-storage/src/submission.rs new file mode 100644 index 000000000..4cdbe80cb --- /dev/null +++ b/crates/distributed-storage/src/submission.rs @@ -0,0 +1,688 @@ +//! Submission storage types +//! +//! Types for storing and managing miner submissions and validator evaluations. + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +/// Canonicalizes a JSON value to a deterministic string representation. +/// +/// This ensures that JSON objects with the same key-value pairs but different +/// insertion orders produce identical strings. Keys in objects are sorted +/// lexicographically, and the process is applied recursively to nested values. +/// +/// # Examples +/// +/// ``` +/// use serde_json::json; +/// +/// // These two values have the same content but were created with different key orders +/// let v1 = json!({"b": 2, "a": 1}); +/// let v2 = json!({"a": 1, "b": 2}); +/// +/// // canonicalize_json produces the same output for both +/// // (keys are sorted: "a" comes before "b") +/// ``` +fn canonicalize_json(value: &serde_json::Value) -> String { + match value { + serde_json::Value::Object(map) => { + let mut pairs: Vec<_> = map.iter().collect(); + pairs.sort_by(|(k1, _), (k2, _)| k1.cmp(k2)); + let inner: Vec = pairs + .iter() + .map(|(k, v)| { + format!( + "{}:{}", + serde_json::to_string(k).unwrap_or_else(|_| format!("\"{}\"", k)), + canonicalize_json(v) + ) + }) + .collect(); + format!("{{{}}}", inner.join(",")) + } + serde_json::Value::Array(arr) => { + let inner: Vec = arr.iter().map(canonicalize_json).collect(); + format!("[{}]", inner.join(",")) + } + _ => serde_json::to_string(value).unwrap_or_else(|_| "null".to_string()), + } +} + +/// A stored submission from a miner +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StoredSubmission { + /// Challenge ID this submission is for + pub challenge_id: String, + /// Hash of the submission (for deduplication and lookup) + pub submission_hash: String, + /// Miner's hotkey (SS58 address) + pub miner_hotkey: String, + /// The source code of the submission (may be None if encrypted) + pub source_code: Option, + /// Additional metadata about the submission + pub metadata: serde_json::Value, + /// When the submission was received + pub submitted_at: DateTime, + /// List of validator hotkeys that have received this submission + pub received_by: Vec, + /// Status of the submission + pub status: SubmissionStatus, +} + +/// Status of a submission +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum SubmissionStatus { + /// Submission received but not yet validated + Pending, + /// Submission is being evaluated + Evaluating, + /// Submission has been fully evaluated + Evaluated, + /// Submission was rejected (invalid format, etc.) + Rejected(String), + /// Submission expired before evaluation + Expired, +} + +impl StoredSubmission { + /// Create a new submission + pub fn new( + challenge_id: impl Into, + miner_hotkey: impl Into, + source_code: Option, + metadata: serde_json::Value, + ) -> Self { + let challenge_id = challenge_id.into(); + let miner_hotkey = miner_hotkey.into(); + + // Compute submission hash using canonicalized JSON for deterministic hashing + let mut hasher = Sha256::new(); + hasher.update(challenge_id.as_bytes()); + hasher.update(miner_hotkey.as_bytes()); + if let Some(ref code) = source_code { + hasher.update(code.as_bytes()); + } + hasher.update(canonicalize_json(&metadata).as_bytes()); + let submission_hash = hex::encode(hasher.finalize()); + + Self { + challenge_id, + submission_hash, + miner_hotkey, + source_code, + metadata, + submitted_at: Utc::now(), + received_by: Vec::new(), + status: SubmissionStatus::Pending, + } + } + + /// Mark that a validator has received this submission + pub fn mark_received_by(&mut self, validator_hotkey: &str) { + if !self.received_by.contains(&validator_hotkey.to_string()) { + self.received_by.push(validator_hotkey.to_string()); + } + } + + /// Check if enough validators have received this submission + pub fn has_quorum(&self, required: usize) -> bool { + self.received_by.len() >= required + } + + /// Serialize to JSON bytes for storage + /// + /// Uses JSON instead of bincode because the metadata field contains + /// serde_json::Value which is not supported by bincode. + pub fn to_bytes(&self) -> Result, serde_json::Error> { + serde_json::to_vec(self) + } + + /// Deserialize from JSON bytes + pub fn from_bytes(bytes: &[u8]) -> Result { + serde_json::from_slice(bytes) + } +} + +/// A stored evaluation result from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StoredEvaluation { + /// Challenge ID + pub challenge_id: String, + /// Hash of the submission being evaluated + pub submission_hash: String, + /// Validator's hotkey (SS58 address) + pub validator_hotkey: String, + /// The computed score (0.0 to 1.0) + pub score: f64, + /// Time taken to execute the submission in milliseconds + pub execution_time_ms: u64, + /// Additional result data (challenge-specific) + pub result_data: serde_json::Value, + /// When the evaluation completed + pub evaluated_at: DateTime, + /// Signature from the validator over the evaluation + pub signature: Vec, + /// Evaluation status + pub status: EvaluationStatus, +} + +/// Status of an evaluation +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum EvaluationStatus { + /// Evaluation completed successfully + Completed, + /// Evaluation failed due to an error + Failed(String), + /// Evaluation timed out + TimedOut, + /// Evaluation skipped (validator chose not to evaluate) + Skipped, +} + +impl StoredEvaluation { + /// Create a new successful evaluation + pub fn new( + challenge_id: impl Into, + submission_hash: impl Into, + validator_hotkey: impl Into, + score: f64, + execution_time_ms: u64, + result_data: serde_json::Value, + signature: Vec, + ) -> Self { + Self { + challenge_id: challenge_id.into(), + submission_hash: submission_hash.into(), + validator_hotkey: validator_hotkey.into(), + score: score.clamp(0.0, 1.0), + execution_time_ms, + result_data, + evaluated_at: Utc::now(), + signature, + status: EvaluationStatus::Completed, + } + } + + /// Create a failed evaluation + pub fn failed( + challenge_id: impl Into, + submission_hash: impl Into, + validator_hotkey: impl Into, + error: impl Into, + signature: Vec, + ) -> Self { + Self { + challenge_id: challenge_id.into(), + submission_hash: submission_hash.into(), + validator_hotkey: validator_hotkey.into(), + score: 0.0, + execution_time_ms: 0, + result_data: serde_json::Value::Null, + evaluated_at: Utc::now(), + signature, + status: EvaluationStatus::Failed(error.into()), + } + } + + /// Create a timed-out evaluation + pub fn timed_out( + challenge_id: impl Into, + submission_hash: impl Into, + validator_hotkey: impl Into, + execution_time_ms: u64, + signature: Vec, + ) -> Self { + Self { + challenge_id: challenge_id.into(), + submission_hash: submission_hash.into(), + validator_hotkey: validator_hotkey.into(), + score: 0.0, + execution_time_ms, + result_data: serde_json::Value::Null, + evaluated_at: Utc::now(), + signature, + status: EvaluationStatus::TimedOut, + } + } + + /// Check if this evaluation was successful + pub fn is_successful(&self) -> bool { + matches!(self.status, EvaluationStatus::Completed) + } + + /// Get the unique key for this evaluation + pub fn key(&self) -> String { + format!( + "{}:{}:{}", + self.challenge_id, self.submission_hash, self.validator_hotkey + ) + } + + /// Serialize to JSON bytes for storage + /// + /// Uses JSON instead of bincode because the result_data field contains + /// serde_json::Value which is not supported by bincode. + pub fn to_bytes(&self) -> Result, serde_json::Error> { + serde_json::to_vec(self) + } + + /// Deserialize from JSON bytes + pub fn from_bytes(bytes: &[u8]) -> Result { + serde_json::from_slice(bytes) + } +} + +/// Aggregated evaluation results for a submission +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AggregatedEvaluations { + /// Challenge ID + pub challenge_id: String, + /// Submission hash + pub submission_hash: String, + /// Miner hotkey + pub miner_hotkey: String, + /// Individual evaluations from validators + pub evaluations: Vec, + /// Final aggregated score + pub final_score: Option, + /// Confidence in the score (based on validator agreement) + pub confidence: f64, + /// When the aggregation was computed + pub aggregated_at: DateTime, +} + +impl AggregatedEvaluations { + /// Create a new aggregation from individual evaluations + pub fn new( + challenge_id: impl Into, + submission_hash: impl Into, + miner_hotkey: impl Into, + evaluations: Vec, + ) -> Self { + Self { + challenge_id: challenge_id.into(), + submission_hash: submission_hash.into(), + miner_hotkey: miner_hotkey.into(), + evaluations, + final_score: None, + confidence: 0.0, + aggregated_at: Utc::now(), + } + } + + /// Compute the final score using median aggregation + pub fn compute_median_score(&mut self) { + let mut scores: Vec = self + .evaluations + .iter() + .filter(|e| e.is_successful()) + .map(|e| e.score) + .collect(); + + if scores.is_empty() { + self.final_score = None; + self.confidence = 0.0; + return; + } + + scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); + + let len = scores.len(); + let median = if len.is_multiple_of(2) { + (scores[len / 2 - 1] + scores[len / 2]) / 2.0 + } else { + scores[len / 2] + }; + + self.final_score = Some(median); + + // Compute confidence based on variance + let mean: f64 = scores.iter().sum::() / len as f64; + let variance: f64 = scores.iter().map(|s| (s - mean).powi(2)).sum::() / len as f64; + let std_dev = variance.sqrt(); + + // Higher confidence with lower variance + self.confidence = (1.0 - std_dev).clamp(0.0, 1.0); + self.aggregated_at = Utc::now(); + } + + /// Compute the final score using stake-weighted average + pub fn compute_weighted_score(&mut self, stakes: &[(String, u64)]) { + let stake_map: std::collections::HashMap<&str, u64> = + stakes.iter().map(|(k, v)| (k.as_str(), *v)).collect(); + + let successful: Vec<_> = self + .evaluations + .iter() + .filter(|e| e.is_successful()) + .collect(); + + if successful.is_empty() { + self.final_score = None; + self.confidence = 0.0; + return; + } + + let total_stake: u64 = successful + .iter() + .filter_map(|e| stake_map.get(e.validator_hotkey.as_str())) + .sum(); + + if total_stake == 0 { + self.compute_median_score(); + return; + } + + let weighted_sum: f64 = successful + .iter() + .map(|e| { + let stake = stake_map.get(e.validator_hotkey.as_str()).unwrap_or(&0); + e.score * (*stake as f64) + }) + .sum(); + + self.final_score = Some(weighted_sum / total_stake as f64); + self.confidence = (successful.len() as f64 / self.evaluations.len() as f64).clamp(0.0, 1.0); + self.aggregated_at = Utc::now(); + } + + /// Check if we have enough evaluations for consensus + pub fn has_quorum(&self, required: usize) -> bool { + let successful_count = self + .evaluations + .iter() + .filter(|e| e.is_successful()) + .count(); + successful_count >= required + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_submission_creation() { + let submission = StoredSubmission::new( + "challenge1", + "5FHneW46...", + Some("print('hello')".to_string()), + serde_json::json!({"language": "python"}), + ); + + assert_eq!(submission.challenge_id, "challenge1"); + assert_eq!(submission.miner_hotkey, "5FHneW46..."); + assert!(!submission.submission_hash.is_empty()); + assert!(submission.received_by.is_empty()); + assert_eq!(submission.status, SubmissionStatus::Pending); + } + + #[test] + fn test_submission_received_by() { + let mut submission = StoredSubmission::new( + "challenge1", + "5FHneW46...", + Some("code".to_string()), + serde_json::Value::Null, + ); + + submission.mark_received_by("validator1"); + submission.mark_received_by("validator2"); + submission.mark_received_by("validator1"); // Duplicate + + assert_eq!(submission.received_by.len(), 2); + assert!(submission.has_quorum(2)); + assert!(!submission.has_quorum(3)); + } + + #[test] + fn test_submission_serialization() { + let submission = StoredSubmission::new( + "challenge1", + "5FHneW46...", + Some("code".to_string()), + serde_json::json!({"key": "value"}), + ); + + let bytes = submission.to_bytes().expect("serialization failed"); + let decoded = StoredSubmission::from_bytes(&bytes).expect("deserialization failed"); + + assert_eq!(decoded.challenge_id, submission.challenge_id); + assert_eq!(decoded.submission_hash, submission.submission_hash); + } + + #[test] + fn test_evaluation_creation() { + let eval = StoredEvaluation::new( + "challenge1", + "hash123", + "validator1", + 0.85, + 1500, + serde_json::json!({"tasks_completed": 17}), + vec![1, 2, 3, 4], + ); + + assert_eq!(eval.challenge_id, "challenge1"); + assert_eq!(eval.score, 0.85); + assert!(eval.is_successful()); + } + + #[test] + fn test_evaluation_score_clamping() { + let eval1 = StoredEvaluation::new("c", "h", "v", 1.5, 0, serde_json::Value::Null, vec![]); + assert_eq!(eval1.score, 1.0); + + let eval2 = StoredEvaluation::new("c", "h", "v", -0.5, 0, serde_json::Value::Null, vec![]); + assert_eq!(eval2.score, 0.0); + } + + #[test] + fn test_evaluation_failed() { + let eval = StoredEvaluation::failed( + "challenge1", + "hash123", + "validator1", + "Out of memory", + vec![], + ); + + assert!(!eval.is_successful()); + assert!(matches!(eval.status, EvaluationStatus::Failed(_))); + } + + #[test] + fn test_evaluation_timed_out() { + let eval = + StoredEvaluation::timed_out("challenge1", "hash123", "validator1", 30000, vec![]); + + assert!(!eval.is_successful()); + assert_eq!(eval.status, EvaluationStatus::TimedOut); + assert_eq!(eval.execution_time_ms, 30000); + } + + #[test] + fn test_evaluation_key() { + let eval = StoredEvaluation::new( + "challenge1", + "hash123", + "validator1", + 0.5, + 0, + serde_json::Value::Null, + vec![], + ); + + assert_eq!(eval.key(), "challenge1:hash123:validator1"); + } + + #[test] + fn test_evaluation_serialization() { + let eval = StoredEvaluation::new( + "challenge1", + "hash123", + "validator1", + 0.75, + 2000, + serde_json::json!({}), + vec![1, 2, 3], + ); + + let bytes = eval.to_bytes().expect("serialization failed"); + let decoded = StoredEvaluation::from_bytes(&bytes).expect("deserialization failed"); + + assert_eq!(decoded.score, eval.score); + assert_eq!(decoded.signature, eval.signature); + } + + #[test] + fn test_aggregated_median_score() { + let evaluations = vec![ + StoredEvaluation::new("c", "h", "v1", 0.8, 100, serde_json::Value::Null, vec![]), + StoredEvaluation::new("c", "h", "v2", 0.6, 100, serde_json::Value::Null, vec![]), + StoredEvaluation::new("c", "h", "v3", 0.9, 100, serde_json::Value::Null, vec![]), + ]; + + let mut agg = AggregatedEvaluations::new("c", "h", "miner1", evaluations); + agg.compute_median_score(); + + assert!(agg.final_score.is_some()); + assert!((agg.final_score.unwrap() - 0.8).abs() < 0.001); + } + + #[test] + fn test_aggregated_weighted_score() { + let evaluations = vec![ + StoredEvaluation::new("c", "h", "v1", 0.5, 100, serde_json::Value::Null, vec![]), + StoredEvaluation::new("c", "h", "v2", 1.0, 100, serde_json::Value::Null, vec![]), + ]; + + let stakes = vec![("v1".to_string(), 100), ("v2".to_string(), 300)]; + + let mut agg = AggregatedEvaluations::new("c", "h", "miner1", evaluations); + agg.compute_weighted_score(&stakes); + + // Weighted: (0.5 * 100 + 1.0 * 300) / 400 = 0.875 + assert!(agg.final_score.is_some()); + assert!((agg.final_score.unwrap() - 0.875).abs() < 0.001); + } + + #[test] + fn test_aggregated_no_successful_evaluations() { + let evaluations = vec![ + StoredEvaluation::failed("c", "h", "v1", "error", vec![]), + StoredEvaluation::timed_out("c", "h", "v2", 1000, vec![]), + ]; + + let mut agg = AggregatedEvaluations::new("c", "h", "miner1", evaluations); + agg.compute_median_score(); + + assert!(agg.final_score.is_none()); + assert_eq!(agg.confidence, 0.0); + } + + #[test] + fn test_aggregated_quorum() { + let evaluations = vec![ + StoredEvaluation::new("c", "h", "v1", 0.8, 100, serde_json::Value::Null, vec![]), + StoredEvaluation::failed("c", "h", "v2", "error", vec![]), + StoredEvaluation::new("c", "h", "v3", 0.9, 100, serde_json::Value::Null, vec![]), + ]; + + let agg = AggregatedEvaluations::new("c", "h", "miner1", evaluations); + + assert!(agg.has_quorum(2)); + assert!(!agg.has_quorum(3)); + } + + #[test] + fn test_canonicalize_json_simple() { + use crate::submission::canonicalize_json; + + // Test that object key order doesn't affect output + let json1 = serde_json::json!({"a": 1, "b": 2}); + let json2 = serde_json::json!({"b": 2, "a": 1}); + + assert_eq!(canonicalize_json(&json1), canonicalize_json(&json2)); + assert_eq!(canonicalize_json(&json1), r#"{"a":1,"b":2}"#); + } + + #[test] + fn test_canonicalize_json_nested() { + use crate::submission::canonicalize_json; + + // Test nested objects with different key orders + let json1 = serde_json::json!({ + "outer_b": {"inner_z": 1, "inner_a": 2}, + "outer_a": [3, 4] + }); + let json2 = serde_json::json!({ + "outer_a": [3, 4], + "outer_b": {"inner_a": 2, "inner_z": 1} + }); + + assert_eq!(canonicalize_json(&json1), canonicalize_json(&json2)); + } + + #[test] + fn test_canonicalize_json_all_types() { + use crate::submission::canonicalize_json; + + // Test all JSON value types + assert_eq!(canonicalize_json(&serde_json::Value::Null), "null"); + assert_eq!(canonicalize_json(&serde_json::json!(true)), "true"); + assert_eq!(canonicalize_json(&serde_json::json!(false)), "false"); + assert_eq!(canonicalize_json(&serde_json::json!(42)), "42"); + assert_eq!(canonicalize_json(&serde_json::json!(3.125)), "3.125"); + assert_eq!(canonicalize_json(&serde_json::json!("hello")), r#""hello""#); + assert_eq!(canonicalize_json(&serde_json::json!([1, 2, 3])), "[1,2,3]"); + } + + #[test] + fn test_submission_hash_deterministic() { + // Create two submissions with the same data but different JSON key insertion order + // This tests that the submission hash is deterministic regardless of key order + + // Create metadata with keys in one order + let mut map1 = serde_json::Map::new(); + map1.insert("zebra".to_string(), serde_json::json!("value_z")); + map1.insert("alpha".to_string(), serde_json::json!("value_a")); + map1.insert( + "middle".to_string(), + serde_json::json!({"nested_b": 2, "nested_a": 1}), + ); + let metadata1 = serde_json::Value::Object(map1); + + // Create metadata with keys in different order + let mut map2 = serde_json::Map::new(); + map2.insert("alpha".to_string(), serde_json::json!("value_a")); + map2.insert( + "middle".to_string(), + serde_json::json!({"nested_a": 1, "nested_b": 2}), + ); + map2.insert("zebra".to_string(), serde_json::json!("value_z")); + let metadata2 = serde_json::Value::Object(map2); + + // Create submissions with the same logical metadata but different insertion orders + let submission1 = StoredSubmission::new( + "challenge_test", + "miner_hotkey_test", + Some("print('test')".to_string()), + metadata1, + ); + + let submission2 = StoredSubmission::new( + "challenge_test", + "miner_hotkey_test", + Some("print('test')".to_string()), + metadata2, + ); + + // Both submissions should have the same hash + assert_eq!( + submission1.submission_hash, submission2.submission_hash, + "Submission hashes should be identical for semantically equivalent metadata" + ); + } +} diff --git a/crates/distributed-storage/src/validated_storage.rs b/crates/distributed-storage/src/validated_storage.rs new file mode 100644 index 000000000..3c00a28aa --- /dev/null +++ b/crates/distributed-storage/src/validated_storage.rs @@ -0,0 +1,1105 @@ +//! Validated Storage with WASM-based Consensus +//! +//! This module provides per-challenge storage where validators must reach consensus +//! before data is accepted. WASM code defines validation rules that each validator +//! runs locally, and writes only succeed when a quorum of validators agree. +//! +//! # Overview +//! +//! The validated storage system prevents abuse by requiring: +//! 1. WASM-defined validation logic for each write operation +//! 2. Validator consensus (configurable quorum) before accepting writes +//! 3. Cryptographic signatures on all proposals and votes +//! +//! # Architecture +//! +//! ```text +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ ValidatedStorage โ”‚ +//! โ”‚ (per-challenge storage with consensus) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ–ผ โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ StorageWriteProposal โ”‚ โ”‚ StorageWriteVote โ”‚ +//! โ”‚ (proposer submits) โ”‚ โ”‚ (validators vote yes/no) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ”‚ โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! โ–ผ +//! โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +//! โ”‚ WASM Validation โ”‚ +//! โ”‚ (challenge-defined rules) โ”‚ +//! โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +//! ``` +//! +//! # Usage +//! +//! ```text +//! use platform_distributed_storage::validated_storage::{ +//! ValidatedStorage, ValidatedStorageConfig, StorageWriteProposal, +//! }; +//! use platform_core::Hotkey; +//! +//! // Create validated storage for a challenge +//! let config = ValidatedStorageConfig::new("challenge-abc", 3); +//! let storage = ValidatedStorage::new(inner_store, config); +//! +//! // Propose a write +//! let proposal = storage.propose_write( +//! my_hotkey, +//! "data-key", +//! data_bytes, +//! ); +//! +//! // Validators vote after running WASM validation +//! let vote = storage.vote_on_proposal(&proposal, true); +//! +//! // Check if consensus is reached +//! if let Some(result) = storage.check_consensus(&proposal.proposal_id) { +//! // Write is committed +//! } +//! ``` + +#![allow(dead_code, unused_variables, unused_imports)] + +use crate::error::{StorageError, StorageResult}; +use crate::store::{DistributedStore, GetOptions, PutOptions, StorageKey, StoredValue}; +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use std::sync::Arc; +use thiserror::Error; +use tokio::sync::RwLock; +use tracing::{debug, info, warn}; + +#[derive(Error, Debug, Clone)] +pub enum ValidatedStorageError { + #[error("not enough votes: need {needed}, have {have}")] + NotEnoughVotes { needed: usize, have: usize }, + + #[error("validation failed: {0}")] + ValidationFailed(String), + + #[error("proposal not found: {0}")] + ProposalNotFound(String), + + #[error("proposal expired: {0}")] + ProposalExpired(String), + + #[error("duplicate vote from validator: {0}")] + DuplicateVote(String), + + #[error("conflicting votes detected from validator: {0}")] + ConflictingVotes(String), + + #[error("invalid signature: {0}")] + InvalidSignature(String), + + #[error("storage error: {0}")] + Storage(String), + + #[error("wasm validation error: {0}")] + WasmValidation(String), + + #[error("consensus timeout")] + ConsensusTimeout, +} + +impl From for ValidatedStorageError { + fn from(err: StorageError) -> Self { + ValidatedStorageError::Storage(err.to_string()) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatedStorageConfig { + pub challenge_id: String, + pub quorum_size: usize, + pub proposal_timeout_ms: u64, + pub namespace_prefix: String, + pub require_wasm_validation: bool, +} + +impl ValidatedStorageConfig { + pub fn new(challenge_id: &str, quorum_size: usize) -> Self { + Self { + challenge_id: challenge_id.to_string(), + quorum_size, + proposal_timeout_ms: 30_000, + namespace_prefix: format!("validated:{}", challenge_id), + require_wasm_validation: true, + } + } + + pub fn with_timeout(mut self, timeout_ms: u64) -> Self { + self.proposal_timeout_ms = timeout_ms; + self + } + + pub fn without_wasm_validation(mut self) -> Self { + self.require_wasm_validation = false; + self + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageWriteProposal { + pub proposal_id: [u8; 32], + pub challenge_id: String, + pub proposer: Hotkey, + pub key: Vec, + pub value: Vec, + pub value_hash: [u8; 32], + pub timestamp: i64, + pub signature: Vec, +} + +impl StorageWriteProposal { + pub fn new(challenge_id: &str, proposer: Hotkey, key: &[u8], value: &[u8]) -> Self { + let timestamp = chrono::Utc::now().timestamp_millis(); + let value_hash = hash_bytes(value); + let proposal_id = + Self::compute_proposal_id(challenge_id, &proposer, key, &value_hash, timestamp); + + Self { + proposal_id, + challenge_id: challenge_id.to_string(), + proposer, + key: key.to_vec(), + value: value.to_vec(), + value_hash, + timestamp, + signature: Vec::new(), + } + } + + fn compute_proposal_id( + challenge_id: &str, + proposer: &Hotkey, + key: &[u8], + value_hash: &[u8; 32], + timestamp: i64, + ) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(challenge_id.as_bytes()); + hasher.update(proposer.as_bytes()); + hasher.update(key); + hasher.update(value_hash); + hasher.update(timestamp.to_le_bytes()); + hasher.finalize().into() + } + + pub fn compute_hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.proposal_id); + hasher.update(self.challenge_id.as_bytes()); + hasher.update(self.proposer.as_bytes()); + hasher.update(&self.key); + hasher.update(self.value_hash); + hasher.update(self.timestamp.to_le_bytes()); + hasher.finalize().into() + } + + pub fn verify_value_hash(&self) -> bool { + hash_bytes(&self.value) == self.value_hash + } + + pub fn is_expired(&self, timeout_ms: u64) -> bool { + let now = chrono::Utc::now().timestamp_millis(); + now - self.timestamp > timeout_ms as i64 + } + + pub fn proposal_id_hex(&self) -> String { + hex::encode(self.proposal_id) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageWriteVote { + pub proposal_id: [u8; 32], + pub voter: Hotkey, + pub approved: bool, + pub validation_result: Option, + pub timestamp: i64, + pub signature: Vec, +} + +impl StorageWriteVote { + pub fn new( + proposal_id: [u8; 32], + voter: Hotkey, + approved: bool, + validation_result: Option, + ) -> Self { + Self { + proposal_id, + voter, + approved, + validation_result, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: Vec::new(), + } + } + + pub fn compute_hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.proposal_id); + hasher.update(self.voter.as_bytes()); + hasher.update([self.approved as u8]); + hasher.update(self.timestamp.to_le_bytes()); + hasher.finalize().into() + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WasmValidationResult { + pub valid: bool, + pub error_message: Option, + pub gas_used: u64, + pub execution_time_ms: u64, +} + +impl WasmValidationResult { + pub fn success(gas_used: u64, execution_time_ms: u64) -> Self { + Self { + valid: true, + error_message: None, + gas_used, + execution_time_ms, + } + } + + pub fn failure(error: &str, gas_used: u64, execution_time_ms: u64) -> Self { + Self { + valid: false, + error_message: Some(error.to_string()), + gas_used, + execution_time_ms, + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ConsensusResult { + pub proposal_id: [u8; 32], + pub key: Vec, + pub value_hash: [u8; 32], + pub approving_votes: Vec, + pub rejecting_votes: Vec, + pub consensus_reached: bool, + pub committed: bool, + pub timestamp: i64, +} + +impl ConsensusResult { + pub fn approving_count(&self) -> usize { + self.approving_votes.len() + } + + pub fn rejecting_count(&self) -> usize { + self.rejecting_votes.len() + } + + pub fn total_votes(&self) -> usize { + self.approving_votes.len() + self.rejecting_votes.len() + } +} + +struct ProposalState { + proposal: StorageWriteProposal, + votes: HashMap, + consensus_result: Option, +} + +impl ProposalState { + fn new(proposal: StorageWriteProposal) -> Self { + Self { + proposal, + votes: HashMap::new(), + consensus_result: None, + } + } +} + +pub struct ValidatedStorage { + inner: Arc, + config: ValidatedStorageConfig, + local_hotkey: Hotkey, + proposals: Arc>>, + committed: Arc>>, +} + +impl ValidatedStorage { + pub fn new(store: S, config: ValidatedStorageConfig, local_hotkey: Hotkey) -> Self { + info!( + challenge_id = %config.challenge_id, + quorum_size = config.quorum_size, + hotkey = local_hotkey.to_hex(), + "Created validated storage" + ); + + Self { + inner: Arc::new(store), + config, + local_hotkey, + proposals: Arc::new(RwLock::new(HashMap::new())), + committed: Arc::new(RwLock::new(HashMap::new())), + } + } + + pub fn with_arc(store: Arc, config: ValidatedStorageConfig, local_hotkey: Hotkey) -> Self { + Self { + inner: store, + config, + local_hotkey, + proposals: Arc::new(RwLock::new(HashMap::new())), + committed: Arc::new(RwLock::new(HashMap::new())), + } + } + + pub fn config(&self) -> &ValidatedStorageConfig { + &self.config + } + + pub fn challenge_id(&self) -> &str { + &self.config.challenge_id + } + + pub fn inner(&self) -> &S { + &self.inner + } + + fn storage_key(&self, key: &[u8]) -> StorageKey { + StorageKey::new(&self.config.namespace_prefix, key) + } + + pub async fn propose_write(&self, key: &[u8], value: &[u8]) -> StorageWriteProposal { + let proposal = StorageWriteProposal::new( + &self.config.challenge_id, + self.local_hotkey.clone(), + key, + value, + ); + + info!( + proposal_id = proposal.proposal_id_hex(), + challenge_id = %self.config.challenge_id, + key_len = key.len(), + value_len = value.len(), + "Created storage write proposal" + ); + + let state = ProposalState::new(proposal.clone()); + + { + let mut proposals = self.proposals.write().await; + proposals.insert(proposal.proposal_id, state); + } + + proposal + } + + pub async fn receive_proposal( + &self, + proposal: StorageWriteProposal, + ) -> Result<(), ValidatedStorageError> { + if proposal.challenge_id != self.config.challenge_id { + return Err(ValidatedStorageError::ValidationFailed(format!( + "Proposal challenge {} doesn't match storage challenge {}", + proposal.challenge_id, self.config.challenge_id + ))); + } + + if !proposal.verify_value_hash() { + return Err(ValidatedStorageError::ValidationFailed( + "Value hash mismatch".to_string(), + )); + } + + if proposal.is_expired(self.config.proposal_timeout_ms) { + return Err(ValidatedStorageError::ProposalExpired( + proposal.proposal_id_hex(), + )); + } + + debug!( + proposal_id = proposal.proposal_id_hex(), + proposer = proposal.proposer.to_hex(), + "Received storage write proposal" + ); + + let state = ProposalState::new(proposal.clone()); + + { + let mut proposals = self.proposals.write().await; + proposals.insert(proposal.proposal_id, state); + } + + Ok(()) + } + + pub async fn vote_on_proposal( + &self, + proposal_id: &[u8; 32], + approved: bool, + validation_result: Option, + ) -> Result { + let vote = StorageWriteVote::new( + *proposal_id, + self.local_hotkey.clone(), + approved, + validation_result, + ); + + debug!( + proposal_id = hex::encode(proposal_id), + voter = self.local_hotkey.to_hex(), + approved, + "Casting vote on storage write proposal" + ); + + { + let mut proposals = self.proposals.write().await; + let state = proposals + .get_mut(proposal_id) + .ok_or_else(|| ValidatedStorageError::ProposalNotFound(hex::encode(proposal_id)))?; + + if state.votes.contains_key(&self.local_hotkey) { + return Err(ValidatedStorageError::DuplicateVote( + self.local_hotkey.to_hex(), + )); + } + + state.votes.insert(self.local_hotkey.clone(), vote.clone()); + } + + Ok(vote) + } + + pub async fn receive_vote( + &self, + vote: StorageWriteVote, + ) -> Result, ValidatedStorageError> { + let proposal_id = vote.proposal_id; + + { + let mut proposals = self.proposals.write().await; + let state = proposals.get_mut(&vote.proposal_id).ok_or_else(|| { + ValidatedStorageError::ProposalNotFound(hex::encode(vote.proposal_id)) + })?; + + if let Some(existing) = state.votes.get(&vote.voter) { + if existing.approved != vote.approved { + warn!( + voter = vote.voter.to_hex(), + proposal_id = hex::encode(vote.proposal_id), + "Detected conflicting votes from same validator" + ); + return Err(ValidatedStorageError::ConflictingVotes(vote.voter.to_hex())); + } + return Err(ValidatedStorageError::DuplicateVote(vote.voter.to_hex())); + } + + debug!( + voter = vote.voter.to_hex(), + approved = vote.approved, + proposal_id = hex::encode(vote.proposal_id), + "Received vote on storage write proposal" + ); + + state.votes.insert(vote.voter.clone(), vote); + } + + self.check_consensus(&proposal_id).await + } + + pub async fn check_consensus( + &self, + proposal_id: &[u8; 32], + ) -> Result, ValidatedStorageError> { + let mut proposals = self.proposals.write().await; + let state = proposals + .get_mut(proposal_id) + .ok_or_else(|| ValidatedStorageError::ProposalNotFound(hex::encode(proposal_id)))?; + + if state.consensus_result.is_some() { + return Ok(state.consensus_result.clone()); + } + + let approving: Vec<_> = state + .votes + .values() + .filter(|v| v.approved) + .cloned() + .collect(); + + let rejecting: Vec<_> = state + .votes + .values() + .filter(|v| !v.approved) + .cloned() + .collect(); + + let consensus_reached = approving.len() >= self.config.quorum_size; + + if consensus_reached { + info!( + proposal_id = hex::encode(proposal_id), + approving = approving.len(), + rejecting = rejecting.len(), + quorum = self.config.quorum_size, + "Consensus reached for storage write" + ); + + let result = ConsensusResult { + proposal_id: *proposal_id, + key: state.proposal.key.clone(), + value_hash: state.proposal.value_hash, + approving_votes: approving, + rejecting_votes: rejecting, + consensus_reached: true, + committed: false, + timestamp: chrono::Utc::now().timestamp_millis(), + }; + + state.consensus_result = Some(result.clone()); + return Ok(Some(result)); + } + + debug!( + proposal_id = hex::encode(proposal_id), + approving = approving.len(), + rejecting = rejecting.len(), + quorum = self.config.quorum_size, + "Consensus not yet reached" + ); + + Ok(None) + } + + pub async fn commit_write( + &self, + proposal_id: &[u8; 32], + ) -> Result { + let (proposal, mut result) = + { + let proposals = self.proposals.read().await; + let state = proposals.get(proposal_id).ok_or_else(|| { + ValidatedStorageError::ProposalNotFound(hex::encode(proposal_id)) + })?; + + let result = state.consensus_result.clone().ok_or( + ValidatedStorageError::NotEnoughVotes { + needed: self.config.quorum_size, + have: state.votes.len(), + }, + )?; + + if !result.consensus_reached { + return Err(ValidatedStorageError::NotEnoughVotes { + needed: self.config.quorum_size, + have: result.approving_count(), + }); + } + + (state.proposal.clone(), result) + }; + + let storage_key = self.storage_key(&proposal.key); + self.inner + .put(storage_key, proposal.value.clone(), PutOptions::default()) + .await?; + + result.committed = true; + + info!( + proposal_id = hex::encode(proposal_id), + key_len = proposal.key.len(), + value_len = proposal.value.len(), + "Committed validated storage write" + ); + + { + let mut committed = self.committed.write().await; + committed.insert(*proposal_id, result.clone()); + } + + { + let mut proposals = self.proposals.write().await; + if let Some(state) = proposals.get_mut(proposal_id) { + state.consensus_result = Some(result.clone()); + } + } + + Ok(result) + } + + pub async fn get(&self, key: &[u8]) -> StorageResult> { + let storage_key = self.storage_key(key); + self.inner.get(&storage_key, GetOptions::default()).await + } + + pub async fn get_if_committed( + &self, + key: &[u8], + proposal_id: &[u8; 32], + ) -> Result>, ValidatedStorageError> { + let committed = self.committed.read().await; + if let Some(result) = committed.get(proposal_id) { + if result.committed && result.key == key { + let storage_key = self.storage_key(key); + let value = self + .inner + .get(&storage_key, GetOptions::default()) + .await? + .map(|v| v.data); + return Ok(value); + } + } + Ok(None) + } + + pub async fn get_proposal(&self, proposal_id: &[u8; 32]) -> Option { + let proposals = self.proposals.read().await; + proposals.get(proposal_id).map(|s| s.proposal.clone()) + } + + pub async fn get_votes(&self, proposal_id: &[u8; 32]) -> Option> { + let proposals = self.proposals.read().await; + proposals + .get(proposal_id) + .map(|s| s.votes.values().cloned().collect()) + } + + pub async fn pending_proposals_count(&self) -> usize { + let proposals = self.proposals.read().await; + proposals + .values() + .filter(|s| s.consensus_result.is_none()) + .count() + } + + pub async fn cleanup_expired(&self) -> usize { + let mut proposals = self.proposals.write().await; + let timeout = self.config.proposal_timeout_ms; + let before = proposals.len(); + + proposals.retain(|_, state| { + !state.proposal.is_expired(timeout) || state.consensus_result.is_some() + }); + + let removed = before - proposals.len(); + if removed > 0 { + debug!(removed, "Cleaned up expired proposals"); + } + removed + } +} + +pub trait WasmStorageValidator: Send + Sync { + fn validate_write( + &self, + challenge_id: &str, + key: &[u8], + value: &[u8], + ) -> Result; +} + +pub struct DefaultWasmValidator; + +impl WasmStorageValidator for DefaultWasmValidator { + fn validate_write( + &self, + _challenge_id: &str, + _key: &[u8], + _value: &[u8], + ) -> Result { + Ok(WasmValidationResult::success(0, 0)) + } +} + +fn hash_bytes(data: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(data); + hasher.finalize().into() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::local::LocalStorageBuilder; + + fn create_test_hotkey(seed: u8) -> Hotkey { + Hotkey([seed; 32]) + } + + #[tokio::test] + async fn test_validated_storage_creation() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 3); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + assert_eq!(validated.challenge_id(), "challenge-1"); + assert_eq!(validated.config().quorum_size, 3); + } + + #[tokio::test] + async fn test_propose_write() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey.clone()); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + assert_eq!(proposal.challenge_id, "challenge-1"); + assert_eq!(proposal.proposer, hotkey); + assert_eq!(proposal.key, b"test-key"); + assert_eq!(proposal.value, b"test-value"); + assert!(proposal.verify_value_hash()); + } + + #[tokio::test] + async fn test_vote_on_proposal() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey.clone()); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + let vote = validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await + .expect("Failed to vote"); + + assert!(vote.approved); + assert_eq!(vote.voter, hotkey); + assert_eq!(vote.proposal_id, proposal.proposal_id); + } + + #[tokio::test] + async fn test_duplicate_vote_rejected() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await + .expect("First vote should succeed"); + + let result = validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await; + + assert!(matches!( + result, + Err(ValidatedStorageError::DuplicateVote(_)) + )); + } + + #[tokio::test] + async fn test_consensus_reached() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey1 = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey1.clone()); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await + .expect("Vote should succeed"); + + let result = validated + .check_consensus(&proposal.proposal_id) + .await + .expect("Check should succeed"); + assert!(result.is_none()); + + let vote2 = StorageWriteVote::new(proposal.proposal_id, create_test_hotkey(2), true, None); + let result = validated + .receive_vote(vote2) + .await + .expect("Receive vote should succeed"); + + assert!(result.is_some()); + let consensus = result.unwrap(); + assert!(consensus.consensus_reached); + assert_eq!(consensus.approving_count(), 2); + } + + #[tokio::test] + async fn test_commit_write() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey1 = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey1); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await + .expect("Vote should succeed"); + + let vote2 = StorageWriteVote::new(proposal.proposal_id, create_test_hotkey(2), true, None); + validated + .receive_vote(vote2) + .await + .expect("Receive vote should succeed"); + + let result = validated + .commit_write(&proposal.proposal_id) + .await + .expect("Commit should succeed"); + + assert!(result.committed); + + let stored = validated + .get(b"test-key") + .await + .expect("Get should succeed") + .expect("Value should exist"); + + assert_eq!(stored.data, b"test-value"); + } + + #[tokio::test] + async fn test_commit_without_consensus_fails() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 3); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + validated + .vote_on_proposal(&proposal.proposal_id, true, None) + .await + .expect("Vote should succeed"); + + let result = validated.commit_write(&proposal.proposal_id).await; + + assert!(matches!( + result, + Err(ValidatedStorageError::NotEnoughVotes { .. }) + )); + } + + #[tokio::test] + async fn test_receive_proposal() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + let proposal = StorageWriteProposal::new( + "challenge-1", + create_test_hotkey(2), + b"external-key", + b"external-value", + ); + + validated + .receive_proposal(proposal.clone()) + .await + .expect("Should accept proposal"); + + let stored = validated + .get_proposal(&proposal.proposal_id) + .await + .expect("Proposal should exist"); + + assert_eq!(stored.key, b"external-key"); + } + + #[tokio::test] + async fn test_receive_proposal_wrong_challenge() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + let proposal = + StorageWriteProposal::new("challenge-2", create_test_hotkey(2), b"key", b"value"); + + let result = validated.receive_proposal(proposal).await; + + assert!(matches!( + result, + Err(ValidatedStorageError::ValidationFailed(_)) + )); + } + + #[tokio::test] + async fn test_conflicting_votes_detected() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let config = ValidatedStorageConfig::new("challenge-1", 2); + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + let proposal = validated.propose_write(b"test-key", b"test-value").await; + + let voter = create_test_hotkey(2); + let vote1 = StorageWriteVote::new(proposal.proposal_id, voter.clone(), true, None); + validated + .receive_vote(vote1) + .await + .expect("First vote should succeed"); + + let vote2 = StorageWriteVote::new(proposal.proposal_id, voter, false, None); + let result = validated.receive_vote(vote2).await; + + assert!(matches!( + result, + Err(ValidatedStorageError::ConflictingVotes(_)) + )); + } + + #[tokio::test] + async fn test_cleanup_expired() { + let storage = LocalStorageBuilder::new("test-node") + .in_memory() + .build() + .expect("Failed to create storage"); + + let mut config = ValidatedStorageConfig::new("challenge-1", 2); + config.proposal_timeout_ms = 1; + let hotkey = create_test_hotkey(1); + let validated = ValidatedStorage::new(storage, config, hotkey); + + validated.propose_write(b"test-key", b"test-value").await; + + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + + let removed = validated.cleanup_expired().await; + assert_eq!(removed, 1); + assert_eq!(validated.pending_proposals_count().await, 0); + } + + #[tokio::test] + async fn test_wasm_validation_result() { + let success = WasmValidationResult::success(100, 5); + assert!(success.valid); + assert!(success.error_message.is_none()); + assert_eq!(success.gas_used, 100); + + let failure = WasmValidationResult::failure("invalid format", 50, 3); + assert!(!failure.valid); + assert_eq!(failure.error_message, Some("invalid format".to_string())); + } + + #[test] + fn test_proposal_hash_verification() { + let proposal = + StorageWriteProposal::new("challenge-1", create_test_hotkey(1), b"key", b"value"); + + assert!(proposal.verify_value_hash()); + + let mut tampered = proposal.clone(); + tampered.value = b"tampered".to_vec(); + assert!(!tampered.verify_value_hash()); + } + + #[test] + fn test_proposal_expiry() { + let mut proposal = + StorageWriteProposal::new("challenge-1", create_test_hotkey(1), b"key", b"value"); + + assert!(!proposal.is_expired(30_000)); + + proposal.timestamp = chrono::Utc::now().timestamp_millis() - 60_000; + assert!(proposal.is_expired(30_000)); + } + + #[test] + fn test_config_builder() { + let config = ValidatedStorageConfig::new("challenge-1", 5) + .with_timeout(60_000) + .without_wasm_validation(); + + assert_eq!(config.challenge_id, "challenge-1"); + assert_eq!(config.quorum_size, 5); + assert_eq!(config.proposal_timeout_ms, 60_000); + assert!(!config.require_wasm_validation); + } + + #[test] + fn test_error_display() { + let err1 = ValidatedStorageError::NotEnoughVotes { needed: 5, have: 2 }; + assert!(err1.to_string().contains("5")); + assert!(err1.to_string().contains("2")); + + let err2 = ValidatedStorageError::ValidationFailed("bad data".to_string()); + assert!(err2.to_string().contains("bad data")); + + let err3 = ValidatedStorageError::ProposalNotFound("abc123".to_string()); + assert!(err3.to_string().contains("abc123")); + + let err4 = ValidatedStorageError::DuplicateVote("voter1".to_string()); + assert!(err4.to_string().contains("voter1")); + + let err5 = ValidatedStorageError::ConflictingVotes("voter2".to_string()); + assert!(err5.to_string().contains("voter2")); + } + + #[test] + fn test_default_wasm_validator() { + let validator = DefaultWasmValidator; + let result = validator + .validate_write("challenge-1", b"key", b"value") + .expect("Validation should succeed"); + + assert!(result.valid); + } +} diff --git a/crates/distributed-storage/src/weights.rs b/crates/distributed-storage/src/weights.rs new file mode 100644 index 000000000..f093c2ead --- /dev/null +++ b/crates/distributed-storage/src/weights.rs @@ -0,0 +1,690 @@ +//! Weight aggregation storage +//! +//! Types for storing and managing weight calculations and validator votes. + +use bincode::Options; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +/// Maximum size for deserializing weight data (10MB). +/// This limit prevents DoS attacks from malformed data causing excessive memory allocation. +const MAX_DESERIALIZE_SIZE: u64 = 10 * 1024 * 1024; + +/// Create bincode options with size limit for safe deserialization. +/// Uses fixint encoding and allows trailing bytes for compatibility with `bincode::serialize()`. +fn bincode_options() -> impl Options { + bincode::options() + .with_limit(MAX_DESERIALIZE_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() +} + +/// Stored weights for an epoch +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StoredWeights { + /// Challenge ID + pub challenge_id: String, + /// Epoch number + pub epoch: u64, + /// Final aggregated weights: (hotkey, weight) + pub weights: Vec<(String, f64)>, + /// Individual validator votes + pub validator_votes: Vec, + /// When the weights were aggregated + pub aggregated_at: DateTime, + /// Hash of the weights (for verification) + pub weights_hash: [u8; 32], + /// Whether these weights have been submitted to Bittensor + pub submitted_to_chain: bool, + /// Block number when submitted (if applicable) + pub submission_block: Option, +} + +impl StoredWeights { + /// Create new stored weights + pub fn new( + challenge_id: impl Into, + epoch: u64, + weights: Vec<(String, f64)>, + validator_votes: Vec, + ) -> Self { + let weights_hash = Self::compute_hash(&weights); + + Self { + challenge_id: challenge_id.into(), + epoch, + weights, + validator_votes, + aggregated_at: Utc::now(), + weights_hash, + submitted_to_chain: false, + submission_block: None, + } + } + + /// Compute hash of weights for verification + fn compute_hash(weights: &[(String, f64)]) -> [u8; 32] { + let mut hasher = Sha256::new(); + for (hotkey, weight) in weights { + hasher.update(hotkey.as_bytes()); + hasher.update(weight.to_le_bytes()); + } + hasher.finalize().into() + } + + /// Mark weights as submitted to chain + pub fn mark_submitted(&mut self, block: u64) { + self.submitted_to_chain = true; + self.submission_block = Some(block); + } + + /// Verify the weights hash + pub fn verify_hash(&self) -> bool { + Self::compute_hash(&self.weights) == self.weights_hash + } + + /// Get the weight for a specific hotkey + pub fn get_weight(&self, hotkey: &str) -> Option { + self.weights + .iter() + .find(|(h, _)| h == hotkey) + .map(|(_, w)| *w) + } + + /// Get top N weights + pub fn top_n(&self, n: usize) -> Vec<(String, f64)> { + let mut sorted = self.weights.clone(); + sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); + sorted.truncate(n); + sorted + } + + /// Normalize weights to sum to 1.0 + pub fn normalize(&mut self) { + let total: f64 = self.weights.iter().map(|(_, w)| *w).sum(); + if total > 0.0 { + for (_, weight) in &mut self.weights { + *weight /= total; + } + } + self.weights_hash = Self::compute_hash(&self.weights); + } + + /// Apply softmax normalization + pub fn apply_softmax(&mut self, temperature: f64) { + if self.weights.is_empty() { + return; + } + + // Find max for numerical stability + let max_weight = self + .weights + .iter() + .map(|(_, w)| *w) + .fold(f64::NEG_INFINITY, f64::max); + + // Compute exp(w/T) for each weight + let exp_weights: Vec = self + .weights + .iter() + .map(|(_, w)| ((w - max_weight) / temperature).exp()) + .collect(); + + let sum_exp: f64 = exp_weights.iter().sum(); + + // Normalize + for (i, (_, weight)) in self.weights.iter_mut().enumerate() { + *weight = exp_weights[i] / sum_exp; + } + + self.weights_hash = Self::compute_hash(&self.weights); + } + + /// Serialize to bincode for storage + pub fn to_bytes(&self) -> Result, bincode::Error> { + bincode::serialize(self) + } + + /// Deserialize from bincode with size limit protection. + /// Limits deserialization to MAX_DESERIALIZE_SIZE bytes to prevent DoS via memory exhaustion. + pub fn from_bytes(bytes: &[u8]) -> Result { + bincode_options().deserialize(bytes) + } +} + +/// A validator's vote on weights for an epoch +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorWeightVote { + /// Validator's hotkey (SS58 address) + pub validator_hotkey: String, + /// The weights this validator computed + pub weights: Vec<(String, f64)>, + /// Signature over the weights + pub signature: Vec, + /// When the vote was cast + pub timestamp: DateTime, + /// Epoch this vote is for + pub epoch: u64, + /// Challenge ID + pub challenge_id: String, + /// Validator's stake at time of voting (for weighted aggregation) + pub stake: u64, +} + +impl ValidatorWeightVote { + /// Create a new weight vote + pub fn new( + validator_hotkey: impl Into, + challenge_id: impl Into, + epoch: u64, + weights: Vec<(String, f64)>, + signature: Vec, + stake: u64, + ) -> Self { + Self { + validator_hotkey: validator_hotkey.into(), + challenge_id: challenge_id.into(), + epoch, + weights, + signature, + timestamp: Utc::now(), + stake, + } + } + + /// Get the message that should be signed + pub fn signing_message(&self) -> Vec { + let mut message = Vec::new(); + message.extend_from_slice(self.challenge_id.as_bytes()); + message.extend_from_slice(&self.epoch.to_le_bytes()); + for (hotkey, weight) in &self.weights { + message.extend_from_slice(hotkey.as_bytes()); + message.extend_from_slice(&weight.to_le_bytes()); + } + message + } + + /// Compute hash of the vote (for deduplication) + pub fn hash(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(self.signing_message()); + hasher.update(self.timestamp.timestamp_millis().to_le_bytes()); + hasher.finalize().into() + } + + /// Get the weight for a specific hotkey + pub fn get_weight(&self, hotkey: &str) -> Option { + self.weights + .iter() + .find(|(h, _)| h == hotkey) + .map(|(_, w)| *w) + } +} + +/// Aggregator for combining multiple validator votes into final weights +#[derive(Debug)] +pub struct WeightAggregator { + /// Challenge ID + challenge_id: String, + /// Epoch + epoch: u64, + /// Collected votes + votes: Vec, +} + +impl WeightAggregator { + /// Create a new aggregator + pub fn new(challenge_id: impl Into, epoch: u64) -> Self { + Self { + challenge_id: challenge_id.into(), + epoch, + votes: Vec::new(), + } + } + + /// Add a vote + pub fn add_vote(&mut self, vote: ValidatorWeightVote) { + // Only add if for the correct challenge and epoch + if vote.challenge_id == self.challenge_id && vote.epoch == self.epoch { + // Check for duplicate + let exists = self + .votes + .iter() + .any(|v| v.validator_hotkey == vote.validator_hotkey); + if !exists { + self.votes.push(vote); + } + } + } + + /// Get the number of votes + pub fn vote_count(&self) -> usize { + self.votes.len() + } + + /// Check if we have enough votes for quorum + pub fn has_quorum(&self, required: usize) -> bool { + self.votes.len() >= required + } + + /// Aggregate votes using stake-weighted averaging + pub fn aggregate_stake_weighted(&self) -> StoredWeights { + if self.votes.is_empty() { + return StoredWeights::new( + self.challenge_id.clone(), + self.epoch, + Vec::new(), + Vec::new(), + ); + } + + // Collect all unique hotkeys + let mut all_hotkeys: std::collections::HashSet = std::collections::HashSet::new(); + for vote in &self.votes { + for (hotkey, _) in &vote.weights { + all_hotkeys.insert(hotkey.clone()); + } + } + + // Compute stake-weighted average for each hotkey + let total_stake: u64 = self.votes.iter().map(|v| v.stake).sum(); + + let mut final_weights: Vec<(String, f64)> = Vec::new(); + + for hotkey in all_hotkeys { + let mut weighted_sum = 0.0; + + for vote in &self.votes { + if let Some(weight) = vote.get_weight(&hotkey) { + weighted_sum += weight * (vote.stake as f64); + } + } + + if total_stake > 0 { + final_weights.push((hotkey, weighted_sum / total_stake as f64)); + } else { + // Equal weighting if no stakes + let count = self.votes.len() as f64; + let avg: f64 = self + .votes + .iter() + .filter_map(|v| v.get_weight(&hotkey)) + .sum::() + / count; + final_weights.push((hotkey, avg)); + } + } + + // Sort by weight descending + final_weights.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); + + StoredWeights::new( + self.challenge_id.clone(), + self.epoch, + final_weights, + self.votes.clone(), + ) + } + + /// Aggregate votes using median (more outlier-resistant) + pub fn aggregate_median(&self) -> StoredWeights { + if self.votes.is_empty() { + return StoredWeights::new( + self.challenge_id.clone(), + self.epoch, + Vec::new(), + Vec::new(), + ); + } + + // Collect all unique hotkeys + let mut all_hotkeys: std::collections::HashSet = std::collections::HashSet::new(); + for vote in &self.votes { + for (hotkey, _) in &vote.weights { + all_hotkeys.insert(hotkey.clone()); + } + } + + let mut final_weights: Vec<(String, f64)> = Vec::new(); + + for hotkey in all_hotkeys { + let mut weights: Vec = self + .votes + .iter() + .filter_map(|v| v.get_weight(&hotkey)) + .collect(); + + if weights.is_empty() { + continue; + } + + weights.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); + + let len = weights.len(); + let median = if len.is_multiple_of(2) { + (weights[len / 2 - 1] + weights[len / 2]) / 2.0 + } else { + weights[len / 2] + }; + + final_weights.push((hotkey, median)); + } + + // Sort by weight descending + final_weights.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); + + StoredWeights::new( + self.challenge_id.clone(), + self.epoch, + final_weights, + self.votes.clone(), + ) + } +} + +/// Historical weight record for trend analysis +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightHistory { + /// Challenge ID + pub challenge_id: String, + /// Miner hotkey + pub hotkey: String, + /// Weight values per epoch + pub weights_by_epoch: Vec<(u64, f64)>, +} + +impl WeightHistory { + /// Create a new weight history + pub fn new(challenge_id: impl Into, hotkey: impl Into) -> Self { + Self { + challenge_id: challenge_id.into(), + hotkey: hotkey.into(), + weights_by_epoch: Vec::new(), + } + } + + /// Add a weight for an epoch + pub fn add_weight(&mut self, epoch: u64, weight: f64) { + // Keep sorted by epoch + let pos = self + .weights_by_epoch + .binary_search_by_key(&epoch, |(e, _)| *e) + .unwrap_or_else(|p| p); + + if pos < self.weights_by_epoch.len() && self.weights_by_epoch[pos].0 == epoch { + self.weights_by_epoch[pos].1 = weight; + } else { + self.weights_by_epoch.insert(pos, (epoch, weight)); + } + } + + /// Get the latest weight + pub fn latest_weight(&self) -> Option<(u64, f64)> { + self.weights_by_epoch.last().copied() + } + + /// Get the weight at a specific epoch + pub fn weight_at_epoch(&self, epoch: u64) -> Option { + self.weights_by_epoch + .iter() + .find(|(e, _)| *e == epoch) + .map(|(_, w)| *w) + } + + /// Calculate the moving average over the last N epochs + pub fn moving_average(&self, n: usize) -> Option { + if self.weights_by_epoch.is_empty() { + return None; + } + + let start = self.weights_by_epoch.len().saturating_sub(n); + let weights: Vec = self.weights_by_epoch[start..] + .iter() + .map(|(_, w)| *w) + .collect(); + + if weights.is_empty() { + return None; + } + + Some(weights.iter().sum::() / weights.len() as f64) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_stored_weights_creation() { + let weights = vec![ + ("miner1".to_string(), 0.5), + ("miner2".to_string(), 0.3), + ("miner3".to_string(), 0.2), + ]; + + let stored = StoredWeights::new("challenge1", 10, weights.clone(), vec![]); + + assert_eq!(stored.challenge_id, "challenge1"); + assert_eq!(stored.epoch, 10); + assert_eq!(stored.weights.len(), 3); + assert!(!stored.submitted_to_chain); + assert!(stored.verify_hash()); + } + + #[test] + fn test_stored_weights_normalize() { + let weights = vec![ + ("m1".to_string(), 1.0), + ("m2".to_string(), 2.0), + ("m3".to_string(), 2.0), + ]; + + let mut stored = StoredWeights::new("c", 1, weights, vec![]); + stored.normalize(); + + let total: f64 = stored.weights.iter().map(|(_, w)| *w).sum(); + assert!((total - 1.0).abs() < 0.001); + } + + #[test] + fn test_stored_weights_softmax() { + let weights = vec![ + ("m1".to_string(), 1.0), + ("m2".to_string(), 2.0), + ("m3".to_string(), 3.0), + ]; + + let mut stored = StoredWeights::new("c", 1, weights, vec![]); + stored.apply_softmax(1.0); + + let total: f64 = stored.weights.iter().map(|(_, w)| *w).sum(); + assert!((total - 1.0).abs() < 0.001); + + // Higher scores should have higher weights + assert!(stored.get_weight("m3").unwrap() > stored.get_weight("m2").unwrap()); + assert!(stored.get_weight("m2").unwrap() > stored.get_weight("m1").unwrap()); + } + + #[test] + fn test_stored_weights_top_n() { + let weights = vec![ + ("m1".to_string(), 0.1), + ("m2".to_string(), 0.5), + ("m3".to_string(), 0.3), + ("m4".to_string(), 0.1), + ]; + + let stored = StoredWeights::new("c", 1, weights, vec![]); + let top2 = stored.top_n(2); + + assert_eq!(top2.len(), 2); + assert_eq!(top2[0].0, "m2"); + assert_eq!(top2[1].0, "m3"); + } + + #[test] + fn test_stored_weights_serialization() { + let weights = vec![("m1".to_string(), 0.5)]; + let stored = StoredWeights::new("c", 1, weights, vec![]); + + let bytes = stored.to_bytes().expect("serialization failed"); + let decoded = StoredWeights::from_bytes(&bytes).expect("deserialization failed"); + + assert_eq!(decoded.epoch, stored.epoch); + assert!(decoded.verify_hash()); + } + + #[test] + fn test_validator_vote_creation() { + let vote = ValidatorWeightVote::new( + "validator1", + "challenge1", + 5, + vec![("m1".to_string(), 0.7), ("m2".to_string(), 0.3)], + vec![1, 2, 3, 4], + 1000, + ); + + assert_eq!(vote.validator_hotkey, "validator1"); + assert_eq!(vote.epoch, 5); + assert_eq!(vote.stake, 1000); + assert!(vote.get_weight("m1").is_some()); + } + + #[test] + fn test_weight_aggregator_stake_weighted() { + let mut aggregator = WeightAggregator::new("challenge1", 1); + + // Vote 1: 100 stake, gives m1=0.6, m2=0.4 + aggregator.add_vote(ValidatorWeightVote::new( + "v1", + "challenge1", + 1, + vec![("m1".to_string(), 0.6), ("m2".to_string(), 0.4)], + vec![], + 100, + )); + + // Vote 2: 300 stake, gives m1=0.8, m2=0.2 + aggregator.add_vote(ValidatorWeightVote::new( + "v2", + "challenge1", + 1, + vec![("m1".to_string(), 0.8), ("m2".to_string(), 0.2)], + vec![], + 300, + )); + + let result = aggregator.aggregate_stake_weighted(); + + // m1: (0.6*100 + 0.8*300) / 400 = 0.75 + // m2: (0.4*100 + 0.2*300) / 400 = 0.25 + let m1_weight = result.get_weight("m1").unwrap(); + let m2_weight = result.get_weight("m2").unwrap(); + + assert!((m1_weight - 0.75).abs() < 0.001); + assert!((m2_weight - 0.25).abs() < 0.001); + } + + #[test] + fn test_weight_aggregator_median() { + let mut aggregator = WeightAggregator::new("challenge1", 1); + + aggregator.add_vote(ValidatorWeightVote::new( + "v1", + "challenge1", + 1, + vec![("m1".to_string(), 0.1)], + vec![], + 100, + )); + + aggregator.add_vote(ValidatorWeightVote::new( + "v2", + "challenge1", + 1, + vec![("m1".to_string(), 0.5)], + vec![], + 100, + )); + + aggregator.add_vote(ValidatorWeightVote::new( + "v3", + "challenge1", + 1, + vec![("m1".to_string(), 0.9)], + vec![], + 100, + )); + + let result = aggregator.aggregate_median(); + let m1_weight = result.get_weight("m1").unwrap(); + + // Median of 0.1, 0.5, 0.9 = 0.5 + assert!((m1_weight - 0.5).abs() < 0.001); + } + + #[test] + fn test_weight_aggregator_deduplicates() { + let mut aggregator = WeightAggregator::new("challenge1", 1); + + let vote = ValidatorWeightVote::new( + "v1", + "challenge1", + 1, + vec![("m1".to_string(), 0.5)], + vec![], + 100, + ); + + aggregator.add_vote(vote.clone()); + aggregator.add_vote(vote); + + assert_eq!(aggregator.vote_count(), 1); + } + + #[test] + fn test_weight_aggregator_ignores_wrong_epoch() { + let mut aggregator = WeightAggregator::new("challenge1", 1); + + aggregator.add_vote(ValidatorWeightVote::new( + "v1", + "challenge1", + 2, // Wrong epoch + vec![("m1".to_string(), 0.5)], + vec![], + 100, + )); + + assert_eq!(aggregator.vote_count(), 0); + } + + #[test] + fn test_weight_history() { + let mut history = WeightHistory::new("challenge1", "miner1"); + + history.add_weight(1, 0.1); + history.add_weight(2, 0.2); + history.add_weight(3, 0.3); + + assert_eq!(history.latest_weight(), Some((3, 0.3))); + assert_eq!(history.weight_at_epoch(2), Some(0.2)); + + // Moving average of last 2 + let ma = history.moving_average(2).unwrap(); + assert!((ma - 0.25).abs() < 0.001); + } + + #[test] + fn test_weight_history_updates() { + let mut history = WeightHistory::new("c", "m"); + + history.add_weight(1, 0.1); + history.add_weight(1, 0.5); // Update + + assert_eq!(history.weights_by_epoch.len(), 1); + assert_eq!(history.weight_at_epoch(1), Some(0.5)); + } +} diff --git a/crates/epoch/Cargo.toml b/crates/epoch/Cargo.toml new file mode 100644 index 000000000..57d909d6a --- /dev/null +++ b/crates/epoch/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "platform-epoch" +version.workspace = true +edition.workspace = true +description = "Epoch management and weight commit-reveal for Mini-Chain" + +[dependencies] +platform-core = { path = "../core" } +platform-challenge-sdk = { path = "../challenge-sdk" } + +tokio = { workspace = true } +async-trait = { workspace = true } + +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +sha2 = { workspace = true } +hex = { workspace = true } + +chrono = { workspace = true } +uuid = { workspace = true } +tracing = { workspace = true } +thiserror = { workspace = true } +parking_lot = { workspace = true } diff --git a/crates/epoch/src/aggregator.rs b/crates/epoch/src/aggregator.rs new file mode 100644 index 000000000..4c9a622be --- /dev/null +++ b/crates/epoch/src/aggregator.rs @@ -0,0 +1,568 @@ +//! Weight aggregator +//! +//! Aggregates weights from multiple validators and challenges. + +use crate::{AgentEmission, EmissionDistribution, EpochConfig, FinalizedWeights}; +use platform_challenge_sdk::{ChallengeId, ChallengeMetadata}; +use platform_core::Hotkey; +use std::collections::HashMap; +use tracing::{info, warn}; + +/// Aggregates weights across all challenges for emission distribution +/// +/// Finalized weights already represent validator consensus output from the +/// commit-reveal flow. Challenge evaluation scores incorporate validator stake +/// and outlier filtering before reaching this aggregator. +pub struct WeightAggregator { + config: EpochConfig, +} + +impl WeightAggregator { + pub fn new(config: EpochConfig) -> Self { + Self { config } + } + + /// Calculate emissions for an epoch + /// + /// Takes finalized weights from all challenges and distributes emissions + /// according to each challenge's emission weight. + pub fn calculate_emissions( + &self, + epoch: u64, + total_emission: u64, + challenges: &[ChallengeMetadata], + finalized_weights: &HashMap, + ) -> EmissionDistribution { + let mut distributions = Vec::new(); + + // Normalize challenge emission weights + let total_challenge_weight: f64 = challenges + .iter() + .filter(|c| c.is_active) + .map(|c| c.emission_weight) + .sum(); + + if total_challenge_weight == 0.0 { + warn!("No active challenges with emission weight"); + return EmissionDistribution { + epoch, + total_emission, + distributions: vec![], + timestamp: chrono::Utc::now(), + }; + } + + for challenge in challenges { + if !challenge.is_active { + continue; + } + + // Get finalized weights for this challenge + let weights = match finalized_weights.get(&challenge.id) { + Some(fw) => &fw.weights, + None => { + warn!("No finalized weights for challenge {:?}", challenge.id); + continue; + } + }; + + // Calculate challenge's share of total emission + let challenge_share = challenge.emission_weight / total_challenge_weight; + let challenge_emission = (total_emission as f64 * challenge_share) as u64; + + info!( + "Challenge {} gets {}% ({} units) of emission", + challenge.name, + challenge_share * 100.0, + challenge_emission + ); + + // Distribute to miners based on weights + for weight in weights { + let miner_emission = (challenge_emission as f64 * weight.weight) as u64; + + distributions.push(AgentEmission { + hotkey: weight.hotkey.clone(), + weight: weight.weight, + emission: miner_emission, + challenge_id: challenge.id, + }); + } + } + + // Merge emissions for same agent across challenges + let merged = self.merge_agent_emissions(distributions); + + info!( + "Epoch {} emission distribution: {} agents, {} total", + epoch, + merged.len(), + total_emission + ); + + EmissionDistribution { + epoch, + total_emission, + distributions: merged, + timestamp: chrono::Utc::now(), + } + } + + /// Merge emissions for same miner across multiple challenges + fn merge_agent_emissions(&self, distributions: Vec) -> Vec { + let mut by_miner: HashMap> = HashMap::new(); + + for dist in distributions { + by_miner.entry(dist.hotkey.clone()).or_default().push(dist); + } + + by_miner + .into_iter() + .map(|(hotkey, emissions)| { + let total_emission: u64 = emissions.iter().map(|e| e.emission).sum(); + let total_weight: f64 = emissions.iter().map(|e| e.weight).sum(); + + // Use the first challenge_id (or could aggregate differently) + let challenge_id = emissions + .first() + .map(|e| e.challenge_id) + .unwrap_or_else(ChallengeId::new); + + AgentEmission { + hotkey, + weight: total_weight / emissions.len() as f64, + emission: total_emission, + challenge_id, + } + }) + .collect() + } + + /// Detect validators with suspicious weight patterns + pub fn detect_suspicious_validators( + &self, + finalized_weights: &[FinalizedWeights], + ) -> Vec { + let mut suspicious = Vec::new(); + + for fw in finalized_weights { + // Check for validators who were excluded + for validator in &fw.excluded_validators { + suspicious.push(SuspiciousValidator { + hotkey: validator.clone(), + reason: SuspicionReason::ExcludedFromConsensus, + challenge_id: fw.challenge_id, + epoch: fw.epoch, + }); + } + } + + suspicious + } + + /// Calculate validator performance metrics + pub fn validator_metrics( + &self, + validator: &Hotkey, + history: &[FinalizedWeights], + ) -> ValidatorMetrics { + let mut participated = 0; + let mut excluded = 0; + + for fw in history { + if fw.participating_validators.contains(validator) { + participated += 1; + } else if fw.excluded_validators.contains(validator) { + excluded += 1; + } + } + + let total = participated + excluded; + let participation_rate = if total > 0 { + participated as f64 / total as f64 + } else { + 0.0 + }; + + ValidatorMetrics { + hotkey: validator.clone(), + epochs_participated: participated, + epochs_excluded: excluded, + participation_rate, + } + } +} + +/// Suspicious validator report +#[derive(Clone, Debug)] +pub struct SuspiciousValidator { + pub hotkey: Hotkey, + pub reason: SuspicionReason, + pub challenge_id: ChallengeId, + pub epoch: u64, +} + +/// Reason for suspicion +#[derive(Clone, Debug)] +pub enum SuspicionReason { + /// Validator was excluded from consensus + ExcludedFromConsensus, + /// Validator's weights deviated significantly + WeightDeviation { deviation: f64 }, + /// Validator didn't participate + NoParticipation, +} + +/// Validator performance metrics +#[derive(Clone, Debug)] +pub struct ValidatorMetrics { + pub hotkey: Hotkey, + pub epochs_participated: usize, + pub epochs_excluded: usize, + pub participation_rate: f64, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::WeightAssignment; + use platform_core::Keypair; + + fn create_test_challenge(name: &str, weight: f64) -> ChallengeMetadata { + ChallengeMetadata { + id: ChallengeId::new(), + name: name.to_string(), + description: "Test".to_string(), + version: "1.0".to_string(), + owner: Keypair::generate().hotkey(), + emission_weight: weight, + config: platform_challenge_sdk::ChallengeConfig::default(), + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + is_active: true, + } + } + + #[test] + fn test_emission_distribution() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge1 = create_test_challenge("Challenge1", 0.6); + let challenge2 = create_test_challenge("Challenge2", 0.4); + + let mut finalized = HashMap::new(); + + finalized.insert( + challenge1.id, + FinalizedWeights { + challenge_id: challenge1.id, + epoch: 0, + weights: vec![ + WeightAssignment::new("agent1".to_string(), 0.7), + WeightAssignment::new("agent2".to_string(), 0.3), + ], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.3, + finalized_at: chrono::Utc::now(), + }, + ); + + finalized.insert( + challenge2.id, + FinalizedWeights { + challenge_id: challenge2.id, + epoch: 0, + weights: vec![ + WeightAssignment::new("agent1".to_string(), 0.5), + WeightAssignment::new("agent3".to_string(), 0.5), + ], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.3, + finalized_at: chrono::Utc::now(), + }, + ); + + let distribution = + aggregator.calculate_emissions(0, 1000, &[challenge1, challenge2], &finalized); + + assert_eq!(distribution.epoch, 0); + assert!(!distribution.distributions.is_empty()); + + // Total emissions should approximately equal total_emission + let total: u64 = distribution.distributions.iter().map(|d| d.emission).sum(); + assert!(total <= 1000); + } + + #[test] + fn test_no_active_challenges() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let mut challenge = create_test_challenge("Challenge", 0.5); + challenge.is_active = false; + + let distribution = aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new()); + + assert_eq!(distribution.distributions.len(), 0); + } + + #[test] + fn test_zero_emission_weight() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge = create_test_challenge("Challenge", 0.0); + + let distribution = aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new()); + + assert_eq!(distribution.distributions.len(), 0); + } + + #[test] + fn test_missing_finalized_weights() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge = create_test_challenge("Challenge", 0.5); + + // No finalized weights for this challenge + let distribution = aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new()); + + assert_eq!(distribution.distributions.len(), 0); + } + + #[test] + fn test_merge_agent_emissions() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge1 = create_test_challenge("Challenge1", 0.5); + let challenge2 = create_test_challenge("Challenge2", 0.5); + + let mut finalized = HashMap::new(); + + // Same agent in both challenges + finalized.insert( + challenge1.id, + FinalizedWeights { + challenge_id: challenge1.id, + epoch: 0, + weights: vec![WeightAssignment::new("agent1".to_string(), 1.0)], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + ); + + finalized.insert( + challenge2.id, + FinalizedWeights { + challenge_id: challenge2.id, + epoch: 0, + weights: vec![WeightAssignment::new("agent1".to_string(), 1.0)], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + ); + + let distribution = + aggregator.calculate_emissions(0, 1000, &[challenge1, challenge2], &finalized); + + // agent1 should have merged emissions from both challenges + assert_eq!(distribution.distributions.len(), 1); + assert_eq!(distribution.distributions[0].hotkey, "agent1"); + assert!(distribution.distributions[0].emission > 0); + } + + #[test] + fn test_detect_suspicious_validators() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let validator1 = Keypair::generate().hotkey(); + let validator2 = Keypair::generate().hotkey(); + + let finalized = vec![FinalizedWeights { + challenge_id: ChallengeId::new(), + epoch: 0, + weights: vec![], + participating_validators: vec![], + excluded_validators: vec![validator1.clone(), validator2.clone()], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }]; + + let suspicious = aggregator.detect_suspicious_validators(&finalized); + assert_eq!(suspicious.len(), 2); + assert!(suspicious.iter().any(|s| s.hotkey == validator1)); + assert!(suspicious.iter().any(|s| s.hotkey == validator2)); + } + + #[test] + fn test_validator_metrics_full_participation() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let validator = Keypair::generate().hotkey(); + + let history = vec![ + FinalizedWeights { + challenge_id: ChallengeId::new(), + epoch: 0, + weights: vec![], + participating_validators: vec![validator.clone()], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + FinalizedWeights { + challenge_id: ChallengeId::new(), + epoch: 1, + weights: vec![], + participating_validators: vec![validator.clone()], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + ]; + + let metrics = aggregator.validator_metrics(&validator, &history); + assert_eq!(metrics.epochs_participated, 2); + assert_eq!(metrics.epochs_excluded, 0); + assert_eq!(metrics.participation_rate, 1.0); + } + + #[test] + fn test_validator_metrics_partial_participation() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let validator = Keypair::generate().hotkey(); + + let history = vec![ + FinalizedWeights { + challenge_id: ChallengeId::new(), + epoch: 0, + weights: vec![], + participating_validators: vec![validator.clone()], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + FinalizedWeights { + challenge_id: ChallengeId::new(), + epoch: 1, + weights: vec![], + participating_validators: vec![], + excluded_validators: vec![validator.clone()], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + ]; + + let metrics = aggregator.validator_metrics(&validator, &history); + assert_eq!(metrics.epochs_participated, 1); + assert_eq!(metrics.epochs_excluded, 1); + assert_eq!(metrics.participation_rate, 0.5); + } + + #[test] + fn test_validator_metrics_no_history() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let validator = Keypair::generate().hotkey(); + let metrics = aggregator.validator_metrics(&validator, &[]); + + assert_eq!(metrics.epochs_participated, 0); + assert_eq!(metrics.epochs_excluded, 0); + assert_eq!(metrics.participation_rate, 0.0); + } + + #[test] + fn test_suspicion_reason_variants() { + let reason1 = SuspicionReason::ExcludedFromConsensus; + let reason2 = SuspicionReason::WeightDeviation { deviation: 0.5 }; + let reason3 = SuspicionReason::NoParticipation; + + // Just verify we can create all variants + assert!(matches!(reason1, SuspicionReason::ExcludedFromConsensus)); + assert!(matches!(reason2, SuspicionReason::WeightDeviation { .. })); + assert!(matches!(reason3, SuspicionReason::NoParticipation)); + } + + #[test] + fn test_emission_with_multiple_weights() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge1 = create_test_challenge("Challenge1", 0.3); + let challenge2 = create_test_challenge("Challenge2", 0.7); + + let mut finalized = HashMap::new(); + + finalized.insert( + challenge1.id, + FinalizedWeights { + challenge_id: challenge1.id, + epoch: 0, + weights: vec![ + WeightAssignment::new("agent1".to_string(), 0.8), + WeightAssignment::new("agent2".to_string(), 0.2), + ], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.3, + finalized_at: chrono::Utc::now(), + }, + ); + + finalized.insert( + challenge2.id, + FinalizedWeights { + challenge_id: challenge2.id, + epoch: 0, + weights: vec![ + WeightAssignment::new("agent3".to_string(), 0.4), + WeightAssignment::new("agent4".to_string(), 0.6), + ], + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.3, + finalized_at: chrono::Utc::now(), + }, + ); + + let distribution = + aggregator.calculate_emissions(0, 10000, &[challenge1, challenge2], &finalized); + + assert_eq!(distribution.epoch, 0); + assert!(!distribution.distributions.is_empty()); + + // Verify distribution proportions + let total: u64 = distribution.distributions.iter().map(|d| d.emission).sum(); + assert!(total <= 10000); + } + + #[test] + fn test_empty_finalized_weights() { + let aggregator = WeightAggregator::new(EpochConfig::default()); + + let challenge = create_test_challenge("Challenge", 0.5); + + let mut finalized = HashMap::new(); + finalized.insert( + challenge.id, + FinalizedWeights { + challenge_id: challenge.id, + epoch: 0, + weights: vec![], // Empty weights + participating_validators: vec![], + excluded_validators: vec![], + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }, + ); + + let distribution = aggregator.calculate_emissions(0, 1000, &[challenge], &finalized); + + // Should handle empty weights gracefully + assert_eq!(distribution.epoch, 0); + } +} diff --git a/crates/epoch/src/commit_reveal.rs b/crates/epoch/src/commit_reveal.rs new file mode 100644 index 000000000..4339b4fa2 --- /dev/null +++ b/crates/epoch/src/commit_reveal.rs @@ -0,0 +1,816 @@ +//! Commit-Reveal scheme for weight submissions +//! +//! Prevents validators from seeing others' weights before committing their own. + +use crate::{FinalizedWeights, WeightCommitment, WeightReveal}; +use parking_lot::RwLock; +use platform_challenge_sdk::{weights, ChallengeId, WeightAssignment}; +use platform_core::Hotkey; +use std::collections::{HashMap, HashSet}; +use tracing::{debug, error, info, warn}; + +/// Commit-reveal state for a single epoch +pub struct CommitRevealState { + epoch: u64, + challenge_id: ChallengeId, + + /// Commitments by validator + commitments: HashMap, + + /// Reveals by validator (after reveal phase) + reveals: HashMap, + + /// Validators who committed but didn't reveal (penalized) + missing_reveals: Vec, + + /// Validators whose reveal didn't match commitment + mismatched_reveals: Vec, +} + +impl CommitRevealState { + pub fn new(epoch: u64, challenge_id: ChallengeId) -> Self { + Self { + epoch, + challenge_id, + commitments: HashMap::new(), + reveals: HashMap::new(), + missing_reveals: Vec::new(), + mismatched_reveals: Vec::new(), + } + } + + /// Submit a commitment + pub fn submit_commitment( + &mut self, + commitment: WeightCommitment, + ) -> Result<(), CommitRevealError> { + if commitment.epoch != self.epoch { + return Err(CommitRevealError::WrongEpoch { + expected: self.epoch, + got: commitment.epoch, + }); + } + + if commitment.challenge_id != self.challenge_id { + return Err(CommitRevealError::WrongChallenge); + } + + if self.commitments.contains_key(&commitment.validator) { + return Err(CommitRevealError::AlreadyCommitted); + } + + debug!( + "Validator {:?} committed weights for epoch {}", + commitment.validator, self.epoch + ); + + self.commitments + .insert(commitment.validator.clone(), commitment); + Ok(()) + } + + /// Submit a reveal + pub fn submit_reveal(&mut self, reveal: WeightReveal) -> Result<(), CommitRevealError> { + if reveal.epoch != self.epoch { + return Err(CommitRevealError::WrongEpoch { + expected: self.epoch, + got: reveal.epoch, + }); + } + + // Check that validator committed + let commitment = self + .commitments + .get(&reveal.validator) + .ok_or(CommitRevealError::NoCommitment)?; + + // Verify reveal matches commitment + let computed_hash = weights::create_commitment(&reveal.weights, &reveal.secret); + if computed_hash != commitment.commitment_hash { + warn!( + "Validator {:?} reveal doesn't match commitment", + reveal.validator + ); + self.mismatched_reveals.push(reveal.validator.clone()); + return Err(CommitRevealError::CommitmentMismatch); + } + + if self.reveals.contains_key(&reveal.validator) { + return Err(CommitRevealError::AlreadyRevealed); + } + + debug!( + "Validator {:?} revealed weights for epoch {}", + reveal.validator, self.epoch + ); + + self.reveals.insert(reveal.validator.clone(), reveal); + Ok(()) + } + + /// Finalize weights after reveal phase + /// + /// The commit-reveal step verifies that validators used the same challenge + /// evaluation output. The resulting weight vector is the consensus payload + /// that later drives stake-weighted PBFT and on-chain weight voting. + pub fn finalize( + &mut self, + smoothing: f64, + min_validators: usize, + ) -> Result { + // Find validators who committed but didn't reveal + for validator in self.commitments.keys() { + if !self.reveals.contains_key(validator) && !self.mismatched_reveals.contains(validator) + { + self.missing_reveals.push(validator.clone()); + } + } + + if !self.missing_reveals.is_empty() { + warn!( + "Epoch {}: {} validators committed but didn't reveal", + self.epoch, + self.missing_reveals.len() + ); + } + + // Collect valid submissions + let submissions: Vec> = + self.reveals.values().map(|r| r.weights.clone()).collect(); + + if submissions.len() < min_validators { + return Err(CommitRevealError::InsufficientValidators { + required: min_validators, + got: submissions.len(), + }); + } + + // Validate that all submissions are consistent + // All validators read from shared chain DB, so submissions should be identical + let first = &submissions[0]; + let divergence_detected = self.check_submission_divergence(&submissions); + + if divergence_detected { + error!( + "Epoch {}: Weight submissions diverged across {} validators! Using first submission.", + self.epoch, + submissions.len() + ); + } + + let aggregated = weights::normalize_weights(first.clone()); + + let participating: Vec = self.reveals.keys().cloned().collect(); + let mut excluded = self.missing_reveals.clone(); + excluded.extend(self.mismatched_reveals.clone()); + + info!( + "Epoch {} finalized: {} validators, {} excluded, {} agents", + self.epoch, + participating.len(), + excluded.len(), + aggregated.len() + ); + + Ok(FinalizedWeights { + challenge_id: self.challenge_id, + epoch: self.epoch, + weights: aggregated, + participating_validators: participating, + excluded_validators: excluded, + smoothing_applied: 0.0, + finalized_at: chrono::Utc::now(), + }) + } + + /// Get number of commitments + pub fn commitment_count(&self) -> usize { + self.commitments.len() + } + + /// Get number of reveals + pub fn reveal_count(&self) -> usize { + self.reveals.len() + } + + /// Check if validator has committed + pub fn has_committed(&self, validator: &Hotkey) -> bool { + self.commitments.contains_key(validator) + } + + /// Check if validator has revealed + pub fn has_revealed(&self, validator: &Hotkey) -> bool { + self.reveals.contains_key(validator) + } + + /// Check if submissions from different validators have diverged. + /// Returns true if divergence is detected. + fn check_submission_divergence(&self, submissions: &[Vec]) -> bool { + if submissions.len() <= 1 { + return false; + } + + let first = &submissions[0]; + + // Build a map of hotkey -> weight for the first submission + let first_weights: HashMap<&str, f64> = first + .iter() + .map(|w| (w.hotkey.as_str(), w.weight)) + .collect(); + + // Tolerance for floating-point comparison (0.1% difference allowed) + const WEIGHT_TOLERANCE: f64 = 0.001; + + for (idx, submission) in submissions.iter().enumerate().skip(1) { + // Check if same number of weight assignments + if submission.len() != first.len() { + warn!( + "Epoch {}: Submission {} has {} weights, first has {}", + self.epoch, + idx, + submission.len(), + first.len() + ); + return true; + } + + // Check if same hotkeys with similar weights + for weight in submission { + match first_weights.get(weight.hotkey.as_str()) { + None => { + warn!( + "Epoch {}: Submission {} has hotkey {} not in first submission", + self.epoch, + idx, + &weight.hotkey[..16.min(weight.hotkey.len())] + ); + return true; + } + Some(&first_weight) => { + let diff = (weight.weight - first_weight).abs(); + if diff > WEIGHT_TOLERANCE { + warn!( + "Epoch {}: Weight divergence for hotkey {}: {} vs {} (diff: {:.4})", + self.epoch, + &weight.hotkey[..16.min(weight.hotkey.len())], + first_weight, + weight.weight, + diff + ); + return true; + } + } + } + } + } + + false + } +} + +/// Errors for commit-reveal +#[derive(Debug, thiserror::Error)] +pub enum CommitRevealError { + #[error("Wrong epoch: expected {expected}, got {got}")] + WrongEpoch { expected: u64, got: u64 }, + + #[error("Wrong challenge")] + WrongChallenge, + + #[error("Already committed")] + AlreadyCommitted, + + #[error("Already revealed")] + AlreadyRevealed, + + #[error("No commitment found")] + NoCommitment, + + #[error("Reveal doesn't match commitment")] + CommitmentMismatch, + + #[error("Insufficient validators: required {required}, got {got}")] + InsufficientValidators { required: usize, got: usize }, + + #[error("Aggregation failed: {0}")] + AggregationFailed(String), +} + +/// Manager for multiple challenges' commit-reveal states +pub struct CommitRevealManager { + states: RwLock>, +} + +impl CommitRevealManager { + pub fn new() -> Self { + Self { + states: RwLock::new(HashMap::new()), + } + } + + /// Get or create state for an epoch/challenge + pub fn get_or_create( + &self, + epoch: u64, + challenge_id: ChallengeId, + ) -> parking_lot::RwLockWriteGuard<'_, HashMap<(u64, ChallengeId), CommitRevealState>> { + let mut states = self.states.write(); + let key = (epoch, challenge_id); + + states + .entry(key) + .or_insert_with(|| CommitRevealState::new(epoch, challenge_id)); + + states + } + + /// Submit commitment + pub fn commit( + &self, + epoch: u64, + challenge_id: ChallengeId, + commitment: WeightCommitment, + ) -> Result<(), CommitRevealError> { + let mut states = self.states.write(); + let key = (epoch, challenge_id); + + let state = states + .entry(key) + .or_insert_with(|| CommitRevealState::new(epoch, challenge_id)); + + state.submit_commitment(commitment) + } + + /// Submit reveal + pub fn reveal( + &self, + epoch: u64, + challenge_id: ChallengeId, + reveal: WeightReveal, + ) -> Result<(), CommitRevealError> { + let mut states = self.states.write(); + let key = (epoch, challenge_id); + + let state = states + .get_mut(&key) + .ok_or(CommitRevealError::NoCommitment)?; + + state.submit_reveal(reveal) + } + + /// Finalize epoch + pub fn finalize( + &self, + epoch: u64, + challenge_id: ChallengeId, + smoothing: f64, + min_validators: usize, + ) -> Result { + let mut states = self.states.write(); + let key = (epoch, challenge_id); + + let state = states + .get_mut(&key) + .ok_or(CommitRevealError::InsufficientValidators { + required: min_validators, + got: 0, + })?; + + state.finalize(smoothing, min_validators) + } + + /// Clean up old epochs + pub fn cleanup_old_epochs(&self, current_epoch: u64, keep_epochs: u64) { + let mut states = self.states.write(); + let cutoff = current_epoch.saturating_sub(keep_epochs); + + states.retain(|(epoch, _), _| *epoch >= cutoff); + } +} + +impl Default for CommitRevealManager { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use platform_core::Keypair; + + fn create_test_commitment( + validator: &Keypair, + epoch: u64, + challenge_id: ChallengeId, + ) -> (WeightCommitment, WeightReveal) { + let weights = vec![ + WeightAssignment::new("agent1".to_string(), 0.6), + WeightAssignment::new("agent2".to_string(), 0.4), + ]; + let secret = b"test_secret".to_vec(); + let hash = weights::create_commitment(&weights, &secret); + + let commitment = WeightCommitment { + validator: validator.hotkey(), + challenge_id, + epoch, + commitment_hash: hash, + timestamp: chrono::Utc::now(), + }; + + let reveal = WeightReveal { + validator: validator.hotkey(), + challenge_id, + epoch, + weights, + secret, + timestamp: chrono::Utc::now(), + }; + + (commitment, reveal) + } + + #[test] + fn test_commit_reveal_flow() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, reveal) = create_test_commitment(&validator, 0, challenge_id); + + // Submit commitment + state.submit_commitment(commitment).unwrap(); + assert!(state.has_committed(&validator.hotkey())); + + // Submit reveal + state.submit_reveal(reveal).unwrap(); + assert!(state.has_revealed(&validator.hotkey())); + } + + #[test] + fn test_commitment_mismatch() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, mut reveal) = create_test_commitment(&validator, 0, challenge_id); + + state.submit_commitment(commitment).unwrap(); + + // Modify reveal to not match commitment + reveal.secret = b"wrong_secret".to_vec(); + + let result = state.submit_reveal(reveal); + assert!(matches!(result, Err(CommitRevealError::CommitmentMismatch))); + } + + #[test] + fn test_wrong_epoch_commitment() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (mut commitment, _) = create_test_commitment(&validator, 1, challenge_id); + commitment.epoch = 1; // Wrong epoch + + let result = state.submit_commitment(commitment); + assert!(matches!( + result, + Err(CommitRevealError::WrongEpoch { + expected: 0, + got: 1 + }) + )); + } + + #[test] + fn test_wrong_challenge() { + let challenge_id = ChallengeId::new(); + let different_challenge = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, _) = create_test_commitment(&validator, 0, different_challenge); + + let result = state.submit_commitment(commitment); + assert!(matches!(result, Err(CommitRevealError::WrongChallenge))); + } + + #[test] + fn test_already_committed() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, _) = create_test_commitment(&validator, 0, challenge_id); + + state.submit_commitment(commitment.clone()).unwrap(); + let result = state.submit_commitment(commitment); + assert!(matches!(result, Err(CommitRevealError::AlreadyCommitted))); + } + + #[test] + fn test_already_revealed() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, reveal) = create_test_commitment(&validator, 0, challenge_id); + + state.submit_commitment(commitment).unwrap(); + state.submit_reveal(reveal.clone()).unwrap(); + + let result = state.submit_reveal(reveal); + assert!(matches!(result, Err(CommitRevealError::AlreadyRevealed))); + } + + #[test] + fn test_reveal_no_commitment() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (_, reveal) = create_test_commitment(&validator, 0, challenge_id); + + let result = state.submit_reveal(reveal); + assert!(matches!(result, Err(CommitRevealError::NoCommitment))); + } + + #[test] + fn test_reveal_wrong_epoch() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, mut reveal) = create_test_commitment(&validator, 0, challenge_id); + + state.submit_commitment(commitment).unwrap(); + reveal.epoch = 1; // Wrong epoch + + let result = state.submit_reveal(reveal); + assert!(matches!( + result, + Err(CommitRevealError::WrongEpoch { + expected: 0, + got: 1 + }) + )); + } + + #[test] + fn test_finalize_insufficient_validators() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator = Keypair::generate(); + let (commitment, reveal) = create_test_commitment(&validator, 0, challenge_id); + + state.submit_commitment(commitment).unwrap(); + state.submit_reveal(reveal).unwrap(); + + // Require more validators than we have + let result = state.finalize(0.3, 5); + assert!(matches!( + result, + Err(CommitRevealError::InsufficientValidators { + required: 5, + got: 1 + }) + )); + } + + #[test] + fn test_finalize_success() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator1 = Keypair::generate(); + let validator2 = Keypair::generate(); + let validator3 = Keypair::generate(); + + let (c1, r1) = create_test_commitment(&validator1, 0, challenge_id); + let (c2, r2) = create_test_commitment(&validator2, 0, challenge_id); + let (c3, r3) = create_test_commitment(&validator3, 0, challenge_id); + + state.submit_commitment(c1).unwrap(); + state.submit_commitment(c2).unwrap(); + state.submit_commitment(c3).unwrap(); + + state.submit_reveal(r1).unwrap(); + state.submit_reveal(r2).unwrap(); + state.submit_reveal(r3).unwrap(); + + let finalized = state.finalize(0.3, 3).unwrap(); + assert_eq!(finalized.epoch, 0); + assert_eq!(finalized.challenge_id, challenge_id); + assert_eq!(finalized.participating_validators.len(), 3); + assert_eq!(finalized.excluded_validators.len(), 0); + } + + #[test] + fn test_finalize_missing_reveals() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + let validator1 = Keypair::generate(); + let validator2 = Keypair::generate(); + let validator3 = Keypair::generate(); + + let (c1, r1) = create_test_commitment(&validator1, 0, challenge_id); + let (c2, _r2) = create_test_commitment(&validator2, 0, challenge_id); + let (c3, r3) = create_test_commitment(&validator3, 0, challenge_id); + + state.submit_commitment(c1).unwrap(); + state.submit_commitment(c2).unwrap(); + state.submit_commitment(c3).unwrap(); + + state.submit_reveal(r1).unwrap(); + // validator2 doesn't reveal + state.submit_reveal(r3).unwrap(); + + let finalized = state.finalize(0.3, 2).unwrap(); + assert_eq!(finalized.participating_validators.len(), 2); + assert_eq!(finalized.excluded_validators.len(), 1); + assert!(finalized.excluded_validators.contains(&validator2.hotkey())); + } + + #[test] + fn test_commitment_count() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + assert_eq!(state.commitment_count(), 0); + + let validator = Keypair::generate(); + let (commitment, _) = create_test_commitment(&validator, 0, challenge_id); + state.submit_commitment(commitment).unwrap(); + + assert_eq!(state.commitment_count(), 1); + } + + #[test] + fn test_reveal_count() { + let challenge_id = ChallengeId::new(); + let mut state = CommitRevealState::new(0, challenge_id); + + assert_eq!(state.reveal_count(), 0); + + let validator = Keypair::generate(); + let (commitment, reveal) = create_test_commitment(&validator, 0, challenge_id); + state.submit_commitment(commitment).unwrap(); + state.submit_reveal(reveal).unwrap(); + + assert_eq!(state.reveal_count(), 1); + } + + #[test] + fn test_commit_reveal_manager() { + let manager = CommitRevealManager::new(); + let challenge_id = ChallengeId::new(); + let epoch = 1; + + let validator = Keypair::generate(); + let (commitment, reveal) = create_test_commitment(&validator, epoch, challenge_id); + + manager.commit(epoch, challenge_id, commitment).unwrap(); + manager.reveal(epoch, challenge_id, reveal).unwrap(); + + let finalized = manager.finalize(epoch, challenge_id, 0.3, 1).unwrap(); + assert_eq!(finalized.epoch, epoch); + } + + #[test] + fn test_commit_reveal_manager_default() { + let manager = CommitRevealManager::default(); + // Verify initial state + let result = manager.finalize(0, ChallengeId::new(), 0.3, 1); + assert!(result.is_err()); // No commits exist + } + + #[test] + fn test_cleanup_old_epochs() { + let manager = CommitRevealManager::new(); + let challenge_id = ChallengeId::new(); + + let validator = Keypair::generate(); + + // Create states for epochs 0, 1, 2 + for epoch in 0..3 { + let (commitment, _) = create_test_commitment(&validator, epoch, challenge_id); + manager.commit(epoch, challenge_id, commitment).unwrap(); + } + + // Cleanup, keeping only last 1 epoch + manager.cleanup_old_epochs(2, 1); + + // Should only have epoch 2 remaining (current 2 - keep 1 = cutoff 1) + // Verify old epochs were removed by checking that get_or_create returns empty for epoch 0 + { + let states_map = manager.get_or_create(0, challenge_id); + let state = states_map.get(&(0, challenge_id)).unwrap(); + assert_eq!(state.commitment_count(), 0); + } + + // Verify epoch 2 still exists with commitment + { + let states_map = manager.get_or_create(2, challenge_id); + let state = states_map.get(&(2, challenge_id)).unwrap(); + assert_eq!(state.commitment_count(), 1); + } + } + + #[test] + fn test_manager_get_or_create() { + let manager = CommitRevealManager::new(); + let challenge_id = ChallengeId::new(); + let epoch = 0; + + // First call creates the state + { + let states = manager.get_or_create(epoch, challenge_id); + assert!(states.contains_key(&(epoch, challenge_id))); + } + + // Second call retrieves existing - verify by checking it exists + { + let states = manager.get_or_create(epoch, challenge_id); + let state = states.get(&(epoch, challenge_id)).unwrap(); + assert_eq!(state.epoch, epoch); + assert_eq!(state.challenge_id, challenge_id); + } + } + + #[test] + fn test_finalize_manager_no_state() { + let manager = CommitRevealManager::new(); + let challenge_id = ChallengeId::new(); + + // Try to finalize without any commits + let result = manager.finalize(0, challenge_id, 0.3, 1); + assert!(matches!( + result, + Err(CommitRevealError::InsufficientValidators { .. }) + )); + } + + #[test] + fn test_multiple_challenges_same_epoch() { + let manager = CommitRevealManager::new(); + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + let epoch = 0; + + let validator1 = Keypair::generate(); + let validator2 = Keypair::generate(); + + let (c1_1, r1_1) = create_test_commitment(&validator1, epoch, challenge1); + let (c2_1, r2_1) = create_test_commitment(&validator2, epoch, challenge1); + let (c1_2, r1_2) = create_test_commitment(&validator1, epoch, challenge2); + + // Submit to challenge1 + manager.commit(epoch, challenge1, c1_1).unwrap(); + manager.commit(epoch, challenge1, c2_1).unwrap(); + manager.reveal(epoch, challenge1, r1_1).unwrap(); + manager.reveal(epoch, challenge1, r2_1).unwrap(); + + // Submit to challenge2 + manager.commit(epoch, challenge2, c1_2).unwrap(); + manager.reveal(epoch, challenge2, r1_2).unwrap(); + + // Finalize both + let finalized1 = manager.finalize(epoch, challenge1, 0.3, 2).unwrap(); + let finalized2 = manager.finalize(epoch, challenge2, 0.3, 1).unwrap(); + + assert_eq!(finalized1.challenge_id, challenge1); + assert_eq!(finalized2.challenge_id, challenge2); + } + + #[test] + fn test_commit_reveal_error_display() { + let err1 = CommitRevealError::WrongEpoch { + expected: 1, + got: 2, + }; + let err2 = CommitRevealError::WrongChallenge; + let err3 = CommitRevealError::AlreadyCommitted; + let err4 = CommitRevealError::AlreadyRevealed; + let err5 = CommitRevealError::NoCommitment; + let err6 = CommitRevealError::CommitmentMismatch; + let err7 = CommitRevealError::InsufficientValidators { + required: 3, + got: 1, + }; + let err8 = CommitRevealError::AggregationFailed("test".to_string()); + + // Verify error messages can be formatted + assert!(!format!("{}", err1).is_empty()); + assert!(!format!("{}", err2).is_empty()); + assert!(!format!("{}", err3).is_empty()); + assert!(!format!("{}", err4).is_empty()); + assert!(!format!("{}", err5).is_empty()); + assert!(!format!("{}", err6).is_empty()); + assert!(!format!("{}", err7).is_empty()); + assert!(!format!("{}", err8).is_empty()); + } +} diff --git a/crates/epoch/src/lib.rs b/crates/epoch/src/lib.rs new file mode 100644 index 000000000..09f657350 --- /dev/null +++ b/crates/epoch/src/lib.rs @@ -0,0 +1,198 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Epoch Management for Mini-Chain +//! +//! Handles: +//! - Epoch transitions +//! - Weight commit-reveal scheme (per mechanism) +//! - Weight aggregation and smoothing +//! - Emission distribution +//! - Mechanism-based weight grouping + +mod aggregator; +mod commit_reveal; +mod manager; +mod mechanism_weights; + +pub use aggregator::*; +pub use commit_reveal::*; +pub use manager::*; +pub use mechanism_weights::*; + +use platform_challenge_sdk::{ChallengeId, WeightAssignment}; +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; + +/// Epoch configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EpochConfig { + /// Blocks per epoch + pub blocks_per_epoch: u64, + /// Blocks for evaluation phase + pub evaluation_blocks: u64, + /// Blocks for commit phase + pub commit_blocks: u64, + /// Blocks for reveal phase + pub reveal_blocks: u64, + /// Minimum validators for weight consensus + pub min_validators_for_consensus: usize, + /// Weight smoothing factor + pub weight_smoothing: f64, +} + +impl Default for EpochConfig { + fn default() -> Self { + Self { + blocks_per_epoch: 360, // ~1 hour at 10s blocks + evaluation_blocks: 270, // 75% for evaluation + commit_blocks: 45, // 12.5% for commit + reveal_blocks: 45, // 12.5% for reveal + min_validators_for_consensus: 3, + weight_smoothing: 0.3, + } + } +} + +/// Epoch phase +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum EpochPhase { + /// Validators are evaluating agents + Evaluation, + /// Validators commit weight hashes + Commit, + /// Validators reveal actual weights + Reveal, + /// Weights are being finalized + Finalization, +} + +impl std::fmt::Display for EpochPhase { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + EpochPhase::Evaluation => write!(f, "evaluation"), + EpochPhase::Commit => write!(f, "commit"), + EpochPhase::Reveal => write!(f, "reveal"), + EpochPhase::Finalization => write!(f, "finalization"), + } + } +} + +/// Current epoch state +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EpochState { + /// Current epoch number + pub epoch: u64, + /// Current phase + pub phase: EpochPhase, + /// Start block of this epoch + pub start_block: u64, + /// Current block + pub current_block: u64, + /// Blocks remaining in current phase + pub blocks_remaining: u64, +} + +impl EpochState { + pub fn new(epoch: u64, start_block: u64, config: &EpochConfig) -> Self { + Self { + epoch, + phase: EpochPhase::Evaluation, + start_block, + current_block: start_block, + blocks_remaining: config.evaluation_blocks, + } + } +} + +/// Weight commitment from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightCommitment { + pub validator: Hotkey, + pub challenge_id: ChallengeId, + pub epoch: u64, + pub commitment_hash: String, + pub timestamp: chrono::DateTime, +} + +/// Weight reveal from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightReveal { + pub validator: Hotkey, + pub challenge_id: ChallengeId, + pub epoch: u64, + pub weights: Vec, + pub secret: Vec, + pub timestamp: chrono::DateTime, +} + +/// Finalized weights for an epoch +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct FinalizedWeights { + pub challenge_id: ChallengeId, + pub epoch: u64, + pub weights: Vec, + pub participating_validators: Vec, + pub excluded_validators: Vec, // Malicious or non-participating + pub smoothing_applied: f64, + pub finalized_at: chrono::DateTime, +} + +/// Emission distribution for an epoch +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EmissionDistribution { + pub epoch: u64, + pub total_emission: u64, + pub distributions: Vec, + pub timestamp: chrono::DateTime, +} + +/// Emission for a single miner +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentEmission { + /// Miner hotkey (SS58 address) + pub hotkey: String, + pub weight: f64, + pub emission: u64, + pub challenge_id: ChallengeId, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_epoch_config_default() { + let config = EpochConfig::default(); + assert_eq!(config.blocks_per_epoch, 360); + assert_eq!(config.evaluation_blocks, 270); + assert_eq!(config.commit_blocks, 45); + assert_eq!(config.reveal_blocks, 45); + assert_eq!(config.min_validators_for_consensus, 3); + assert_eq!(config.weight_smoothing, 0.3); + } + + #[test] + fn test_epoch_phase_display() { + assert_eq!(EpochPhase::Evaluation.to_string(), "evaluation"); + assert_eq!(EpochPhase::Commit.to_string(), "commit"); + assert_eq!(EpochPhase::Reveal.to_string(), "reveal"); + assert_eq!(EpochPhase::Finalization.to_string(), "finalization"); + } + + #[test] + fn test_epoch_phase_equality() { + assert_eq!(EpochPhase::Evaluation, EpochPhase::Evaluation); + assert_ne!(EpochPhase::Evaluation, EpochPhase::Commit); + } + + #[test] + fn test_epoch_state_new() { + let config = EpochConfig::default(); + let state = EpochState::new(5, 1800, &config); + + assert_eq!(state.epoch, 5); + assert_eq!(state.phase, EpochPhase::Evaluation); + assert_eq!(state.start_block, 1800); + assert_eq!(state.current_block, 1800); + assert_eq!(state.blocks_remaining, config.evaluation_blocks); + } +} diff --git a/crates/epoch/src/manager.rs b/crates/epoch/src/manager.rs new file mode 100644 index 000000000..6265b96c7 --- /dev/null +++ b/crates/epoch/src/manager.rs @@ -0,0 +1,483 @@ +//! Epoch Manager +//! +//! Manages epoch transitions and phase changes. + +use crate::{EpochConfig, EpochPhase, EpochState}; +use parking_lot::RwLock; +use std::sync::Arc; +use tracing::info; + +/// Epoch manager +pub struct EpochManager { + config: RwLock, + state: Arc>, +} + +impl EpochManager { + /// Create a new epoch manager + pub fn new(config: EpochConfig, current_block: u64) -> Self { + let epoch = current_block / config.blocks_per_epoch; + let start_block = epoch * config.blocks_per_epoch; + + let state = EpochState::new(epoch, start_block, &config); + + Self { + config: RwLock::new(config), + state: Arc::new(RwLock::new(state)), + } + } + + /// Update configuration (e.g., when syncing with Bittensor tempo) + pub fn update_config(&self, new_config: EpochConfig) { + info!( + "Updating epoch config: blocks_per_epoch={}, eval={}, commit={}, reveal={}", + new_config.blocks_per_epoch, + new_config.evaluation_blocks, + new_config.commit_blocks, + new_config.reveal_blocks + ); + *self.config.write() = new_config; + } + + /// Get current epoch state + pub fn state(&self) -> EpochState { + self.state.read().clone() + } + + /// Get current epoch number + pub fn current_epoch(&self) -> u64 { + self.state.read().epoch + } + + /// Get current phase + pub fn current_phase(&self) -> EpochPhase { + self.state.read().phase + } + + /// Update with new block + pub fn on_new_block(&self, block_number: u64) -> Option { + let config = self.config.read(); + let mut state = self.state.write(); + state.current_block = block_number; + + // Check if we've moved to a new epoch + let new_epoch = block_number / config.blocks_per_epoch; + if new_epoch > state.epoch { + let old_epoch = state.epoch; + state.epoch = new_epoch; + state.start_block = new_epoch * config.blocks_per_epoch; + state.phase = EpochPhase::Evaluation; + state.blocks_remaining = config.evaluation_blocks; + + info!("New epoch started: {} -> {}", old_epoch, new_epoch); + + return Some(EpochTransition::NewEpoch { + old_epoch, + new_epoch, + }); + } + + // Calculate position within epoch + let blocks_into_epoch = block_number - state.start_block; + + // Determine phase (use saturating_sub to prevent overflow) + let (new_phase, blocks_remaining) = if blocks_into_epoch < config.evaluation_blocks { + ( + EpochPhase::Evaluation, + config.evaluation_blocks.saturating_sub(blocks_into_epoch), + ) + } else if blocks_into_epoch < config.evaluation_blocks + config.commit_blocks { + ( + EpochPhase::Commit, + (config.evaluation_blocks + config.commit_blocks).saturating_sub(blocks_into_epoch), + ) + } else if blocks_into_epoch + < config.evaluation_blocks + config.commit_blocks + config.reveal_blocks + { + ( + EpochPhase::Reveal, + (config.evaluation_blocks + config.commit_blocks + config.reveal_blocks) + .saturating_sub(blocks_into_epoch), + ) + } else { + ( + EpochPhase::Finalization, + config.blocks_per_epoch.saturating_sub(blocks_into_epoch), + ) + }; + + // Check for phase transition + if new_phase != state.phase { + let old_phase = state.phase; + state.phase = new_phase; + state.blocks_remaining = blocks_remaining; + + info!( + "Epoch {} phase transition: {} -> {}", + state.epoch, old_phase, new_phase + ); + + return Some(EpochTransition::PhaseChange { + epoch: state.epoch, + old_phase, + new_phase, + }); + } + + state.blocks_remaining = blocks_remaining; + None + } + + /// Check if we can submit commitments + pub fn can_commit(&self) -> bool { + self.state.read().phase == EpochPhase::Commit + } + + /// Check if we can reveal weights + pub fn can_reveal(&self) -> bool { + self.state.read().phase == EpochPhase::Reveal + } + + /// Check if weights are being finalized + pub fn is_finalizing(&self) -> bool { + self.state.read().phase == EpochPhase::Finalization + } + + /// Get blocks until next phase + pub fn blocks_until_next_phase(&self) -> u64 { + self.state.read().blocks_remaining + } + + /// Get epoch for a given block + pub fn epoch_for_block(&self, block: u64) -> u64 { + block / self.config.read().blocks_per_epoch + } + + /// Get start block for an epoch + pub fn start_block_for_epoch(&self, epoch: u64) -> u64 { + epoch * self.config.read().blocks_per_epoch + } + + /// Get phase for a given block + pub fn phase_for_block(&self, block: u64) -> EpochPhase { + let config = self.config.read(); + let epoch_start = self.start_block_for_epoch(self.epoch_for_block(block)); + let blocks_into_epoch = block - epoch_start; + + if blocks_into_epoch < config.evaluation_blocks { + EpochPhase::Evaluation + } else if blocks_into_epoch < config.evaluation_blocks + config.commit_blocks { + EpochPhase::Commit + } else if blocks_into_epoch + < config.evaluation_blocks + config.commit_blocks + config.reveal_blocks + { + EpochPhase::Reveal + } else { + EpochPhase::Finalization + } + } +} + +/// Epoch transition event +#[derive(Clone, Debug)] +pub enum EpochTransition { + /// New epoch started + NewEpoch { old_epoch: u64, new_epoch: u64 }, + /// Phase changed within epoch + PhaseChange { + epoch: u64, + old_phase: EpochPhase, + new_phase: EpochPhase, + }, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_epoch_manager() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + assert_eq!(manager.current_epoch(), 0); + assert_eq!(manager.current_phase(), EpochPhase::Evaluation); + } + + #[test] + fn test_phase_transitions() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + // Should be in evaluation at block 0 + assert_eq!(manager.current_phase(), EpochPhase::Evaluation); + + // Move to commit phase + let transition = manager.on_new_block(70); + assert!(matches!( + transition, + Some(EpochTransition::PhaseChange { + new_phase: EpochPhase::Commit, + .. + }) + )); + + // Move to reveal phase + let transition = manager.on_new_block(85); + assert!(matches!( + transition, + Some(EpochTransition::PhaseChange { + new_phase: EpochPhase::Reveal, + .. + }) + )); + + // Move to new epoch + let transition = manager.on_new_block(100); + assert!(matches!( + transition, + Some(EpochTransition::NewEpoch { new_epoch: 1, .. }) + )); + } + + #[test] + fn test_can_commit() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config.clone(), 0); + // Start at block 70 (start of commit phase) + manager.on_new_block(70); + assert!(manager.can_commit()); + assert!(!manager.can_reveal()); + assert!(!manager.is_finalizing()); + } + + #[test] + fn test_can_reveal() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config.clone(), 0); + // Start at block 85 (start of reveal phase) + manager.on_new_block(85); + assert!(!manager.can_commit()); + assert!(manager.can_reveal()); + assert!(!manager.is_finalizing()); + } + + #[test] + fn test_is_finalizing() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 10, + ..Default::default() + }; + + let manager = EpochManager::new(config.clone(), 0); + // Start at block 95 (start of finalization phase) + manager.on_new_block(95); + assert!(!manager.can_commit()); + assert!(!manager.can_reveal()); + assert!(manager.is_finalizing()); + } + + #[test] + fn test_blocks_until_next_phase() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + assert_eq!(manager.blocks_until_next_phase(), 70); + + manager.on_new_block(50); + assert_eq!(manager.blocks_until_next_phase(), 20); + } + + #[test] + fn test_epoch_for_block() { + let config = EpochConfig { + blocks_per_epoch: 100, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + assert_eq!(manager.epoch_for_block(0), 0); + assert_eq!(manager.epoch_for_block(99), 0); + assert_eq!(manager.epoch_for_block(100), 1); + assert_eq!(manager.epoch_for_block(250), 2); + } + + #[test] + fn test_start_block_for_epoch() { + let config = EpochConfig { + blocks_per_epoch: 100, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + assert_eq!(manager.start_block_for_epoch(0), 0); + assert_eq!(manager.start_block_for_epoch(1), 100); + assert_eq!(manager.start_block_for_epoch(5), 500); + } + + #[test] + fn test_phase_for_block() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 10, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + assert_eq!(manager.phase_for_block(0), EpochPhase::Evaluation); + assert_eq!(manager.phase_for_block(50), EpochPhase::Evaluation); + assert_eq!(manager.phase_for_block(70), EpochPhase::Commit); + assert_eq!(manager.phase_for_block(85), EpochPhase::Reveal); + assert_eq!(manager.phase_for_block(95), EpochPhase::Finalization); + } + + #[test] + fn test_update_config() { + let config = EpochConfig::default(); + let manager = EpochManager::new(config, 0); + + let new_config = EpochConfig { + blocks_per_epoch: 200, + evaluation_blocks: 150, + commit_blocks: 25, + reveal_blocks: 25, + ..Default::default() + }; + + manager.update_config(new_config); + // Config should be updated and used for subsequent calculations + } + + #[test] + fn test_state_clone() { + let config = EpochConfig::default(); + let manager = EpochManager::new(config, 0); + + let state1 = manager.state(); + let state2 = manager.state(); + + assert_eq!(state1.epoch, state2.epoch); + assert_eq!(state1.phase, state2.phase); + } + + #[test] + fn test_no_transition_same_phase() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + // Move within same phase + let transition = manager.on_new_block(10); + assert!(transition.is_none()); + + let transition = manager.on_new_block(20); + assert!(transition.is_none()); + } + + #[test] + fn test_finalization_phase() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 10, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + // Move to finalization + manager.on_new_block(95); + assert_eq!(manager.current_phase(), EpochPhase::Finalization); + } + + #[test] + fn test_epoch_transition_across_multiple_epochs() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + // Jump multiple epochs + let transition = manager.on_new_block(250); + assert!(matches!( + transition, + Some(EpochTransition::NewEpoch { + old_epoch: 0, + new_epoch: 2, + .. + }) + )); + + assert_eq!(manager.current_epoch(), 2); + } + + #[test] + fn test_blocks_remaining_decreases() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_blocks: 70, + commit_blocks: 15, + reveal_blocks: 15, + ..Default::default() + }; + + let manager = EpochManager::new(config, 0); + + let initial_remaining = manager.blocks_until_next_phase(); + manager.on_new_block(10); + let new_remaining = manager.blocks_until_next_phase(); + + assert!(new_remaining < initial_remaining); + } +} diff --git a/crates/epoch/src/mechanism_weights.rs b/crates/epoch/src/mechanism_weights.rs new file mode 100644 index 000000000..4d8f475a4 --- /dev/null +++ b/crates/epoch/src/mechanism_weights.rs @@ -0,0 +1,1137 @@ +//! Mechanism Weight Manager +//! +//! Groups weights from multiple challenges by mechanism_id for batch submission to Bittensor. +//! Each challenge is mapped to a mechanism (1:1 relationship). +//! +//! Weight Distribution: +//! - Each challenge has an emission_weight (0.0 - 1.0) defining its share of total emissions +//! - Challenge scores are passed through as-is (no normalization/manipulation) +//! - Remaining weight (1.0 - emission_weight) goes to UID 0 (burn address) + +use parking_lot::RwLock; +use platform_challenge_sdk::{ChallengeId, WeightAssignment}; +use std::collections::HashMap; +use tracing::{debug, info, warn}; + +/// UID 0 is the burn address - receives unused emission weight +pub const BURN_UID: u16 = 0; + +/// Maximum weight value for Bittensor +pub const MAX_WEIGHT: u16 = 65535; + +/// Weight data for a single mechanism +#[derive(Clone, Debug)] +pub struct MechanismWeights { + /// Mechanism ID (u8 as per Bittensor) + pub mechanism_id: u8, + /// Challenge that produced these weights + pub challenge_id: ChallengeId, + /// Agent UIDs (converted from hashes) + pub uids: Vec, + /// Normalized weights (u16 format for Bittensor) + pub weights: Vec, + /// Original float weights for reference + pub raw_weights: Vec, + /// Emission weight for this challenge (0.0 - 1.0) + pub emission_weight: f64, +} + +/// Hotkey to UID mapping from metagraph +pub type HotkeyUidMap = HashMap; + +impl MechanismWeights { + /// Create new MechanismWeights with emission-based distribution + /// + /// WARNING: This method creates weights without hotkey->UID mapping. + /// All weights will go to UID 0 (burn) since no hotkeys can be resolved. + /// For production use, prefer `with_hotkey_mapping()` with metagraph data. + /// + /// - emission_weight: fraction of total emissions this challenge controls (0.0 - 1.0) + /// - assignments: raw scores from challenge (will be scaled by emission_weight) + /// - Remaining weight goes to UID 0 (burn) + pub fn new( + mechanism_id: u8, + challenge_id: ChallengeId, + assignments: Vec, + emission_weight: f64, + ) -> Self { + warn!( + "MechanismWeights::new() called without hotkey mapping - \ + all weights will go to UID 0 (burn). Use with_hotkey_mapping() in production." + ); + Self::with_hotkey_mapping( + mechanism_id, + challenge_id, + assignments, + emission_weight, + &HashMap::new(), + ) + } + + /// Create with hotkey to UID mapping from metagraph + pub fn with_hotkey_mapping( + mechanism_id: u8, + challenge_id: ChallengeId, + assignments: Vec, + emission_weight: f64, + hotkey_to_uid: &HotkeyUidMap, + ) -> Self { + let emission_weight = emission_weight.clamp(0.0, 1.0); + let (uids, weights) = + Self::convert_to_bittensor_format(&assignments, emission_weight, hotkey_to_uid); + Self { + mechanism_id, + challenge_id, + uids, + weights, + raw_weights: assignments, + emission_weight, + } + } + + /// Convert WeightAssignment to Bittensor format with emission scaling + /// + /// Example: emission_weight = 0.1 (10%) + /// - Challenge returns [Agent A (hotkey1): 0.6, Agent B (hotkey2): 0.4] + /// - Look up UIDs from metagraph: hotkey1 -> UID 5, hotkey2 -> UID 12 + /// - After scaling: UID 5: 6%, UID 12: 4%, UID 0: 90% + fn convert_to_bittensor_format( + assignments: &[WeightAssignment], + emission_weight: f64, + hotkey_to_uid: &HotkeyUidMap, + ) -> (Vec, Vec) { + if assignments.is_empty() || emission_weight <= 0.0 { + // No challenge weights - all to UID 0 + return (vec![BURN_UID], vec![MAX_WEIGHT]); + } + + // Normalize challenge scores to sum to 1.0 + let total: f64 = assignments.iter().map(|a| a.weight).sum(); + if total <= 0.0 { + return (vec![BURN_UID], vec![MAX_WEIGHT]); + } + + let mut uids = Vec::with_capacity(assignments.len() + 1); + let mut weights = Vec::with_capacity(assignments.len() + 1); + let mut used_weight: u64 = 0; + let mut skipped_no_uid = 0; + + // Add challenge agent weights (scaled by emission_weight) + for assignment in assignments.iter() { + // Look up UID from hotkey via metagraph + let uid = if let Some(&uid) = hotkey_to_uid.get(&assignment.hotkey) { + uid + } else { + // Hotkey not found in metagraph - skip this assignment + debug!( + "Hotkey {} not found in metagraph, skipping weight assignment", + assignment.hotkey + ); + skipped_no_uid += 1; + continue; + }; + + // Skip UID 0 as it's reserved for burn + if uid == BURN_UID { + debug!("Skipping UID 0 assignment (reserved for burn)"); + continue; + } + + // Scale: (score / total) * emission_weight * MAX_WEIGHT + let normalized_score = assignment.weight / total; + let scaled_weight = + (normalized_score * emission_weight * MAX_WEIGHT as f64).round() as u16; + + if scaled_weight > 0 { + uids.push(uid); + weights.push(scaled_weight); + used_weight += scaled_weight as u64; + } + } + + // Remaining weight goes to UID 0 (burn) + let burn_weight = MAX_WEIGHT.saturating_sub(used_weight as u16); + if burn_weight > 0 { + uids.insert(0, BURN_UID); + weights.insert(0, burn_weight); + } + + info!( + "Weight distribution: {}% to {} agents, {}% to UID 0 (burn){}", + (emission_weight * 100.0).round(), + uids.len().saturating_sub(1), // Exclude UID 0 from count + ((burn_weight as f64 / MAX_WEIGHT as f64) * 100.0).round(), + if skipped_no_uid > 0 { + format!(", {} skipped (no UID)", skipped_no_uid) + } else { + String::new() + } + ); + + (uids, weights) + } + + /// Get weights as tuple for batch submission + pub fn as_batch_tuple(&self) -> (u8, Vec, Vec) { + (self.mechanism_id, self.uids.clone(), self.weights.clone()) + } +} + +/// Manages weights grouped by mechanism for an epoch +pub struct MechanismWeightManager { + /// Epoch number + epoch: u64, + /// Weights per mechanism (mechanism_id -> MechanismWeights) + weights: RwLock>, + /// Challenge to mechanism mapping + challenge_mechanism_map: RwLock>, +} + +impl MechanismWeightManager { + pub fn new(epoch: u64) -> Self { + Self { + epoch, + weights: RwLock::new(HashMap::new()), + challenge_mechanism_map: RwLock::new(HashMap::new()), + } + } + + /// Register a challenge with its mechanism + pub fn register_challenge(&self, challenge_id: ChallengeId, mechanism_id: u8) { + self.challenge_mechanism_map + .write() + .insert(challenge_id, mechanism_id); + debug!( + "Registered challenge {:?} with mechanism {}", + challenge_id, mechanism_id + ); + } + + /// Submit weights from a challenge + /// + /// - emission_weight: fraction of total emissions this challenge controls (0.0 - 1.0) + /// - hotkey_to_uid: mapping from hotkey (SS58) to UID from metagraph + /// - Remaining weight (1.0 - emission_weight) automatically goes to UID 0 (burn) + pub fn submit_weights( + &self, + challenge_id: ChallengeId, + mechanism_id: u8, + weights: Vec, + emission_weight: f64, + ) { + // No hotkey mapping - use fallback UIDs + self.submit_weights_with_metagraph( + challenge_id, + mechanism_id, + weights, + emission_weight, + &HashMap::new(), + ) + } + + /// Submit weights with hotkey to UID mapping from metagraph + pub fn submit_weights_with_metagraph( + &self, + challenge_id: ChallengeId, + mechanism_id: u8, + weights: Vec, + emission_weight: f64, + hotkey_to_uid: &HotkeyUidMap, + ) { + let mech_weights = MechanismWeights::with_hotkey_mapping( + mechanism_id, + challenge_id, + weights, + emission_weight, + hotkey_to_uid, + ); + self.weights.write().insert(mechanism_id, mech_weights); + + info!( + "Submitted weights for mechanism {} from challenge {:?}: {} UIDs, {}% emission", + mechanism_id, + challenge_id, + self.weights + .read() + .get(&mechanism_id) + .map(|w| w.uids.len().saturating_sub(1)) // Exclude UID 0 + .unwrap_or(0), + (emission_weight * 100.0).round() + ); + } + + /// Get all mechanism weights for batch submission + pub fn get_all_mechanism_weights(&self) -> Vec<(u8, Vec, Vec)> { + self.weights + .read() + .values() + .map(|w| w.as_batch_tuple()) + .collect() + } + + /// Get weights for a specific mechanism + pub fn get_mechanism_weights(&self, mechanism_id: u8) -> Option { + self.weights.read().get(&mechanism_id).cloned() + } + + /// Get mechanism ID for a challenge + pub fn get_mechanism_for_challenge(&self, challenge_id: &ChallengeId) -> Option { + self.challenge_mechanism_map + .read() + .get(challenge_id) + .copied() + } + + /// Get all registered mechanisms + pub fn list_mechanisms(&self) -> Vec { + self.weights.read().keys().copied().collect() + } + + /// Clear all weights (for new epoch) + pub fn clear(&self) { + self.weights.write().clear(); + } + + /// Get epoch + pub fn epoch(&self) -> u64 { + self.epoch + } + + /// Get number of mechanisms with weights + pub fn mechanism_count(&self) -> usize { + self.weights.read().len() + } +} + +/// Commit data for a mechanism +#[derive(Clone, Debug)] +pub struct MechanismCommitment { + pub mechanism_id: u8, + pub epoch: u64, + pub commit_hash: [u8; 32], + pub salt: Vec, +} + +impl MechanismCommitment { + pub fn new(mechanism_id: u8, epoch: u64, weights: &MechanismWeights, salt: &[u8]) -> Self { + let commit_hash = Self::compute_hash(&weights.uids, &weights.weights, salt); + Self { + mechanism_id, + epoch, + commit_hash, + salt: salt.to_vec(), + } + } + + /// Compute commit hash for commit-reveal + fn compute_hash(uids: &[u16], weights: &[u16], salt: &[u8]) -> [u8; 32] { + use sha2::{Digest, Sha256}; + + let mut hasher = Sha256::new(); + + // Hash UIDs + for uid in uids { + hasher.update(uid.to_le_bytes()); + } + + // Hash weights + for w in weights { + hasher.update(w.to_le_bytes()); + } + + // Hash salt + hasher.update(salt); + + let result = hasher.finalize(); + let mut hash = [0u8; 32]; + hash.copy_from_slice(&result); + hash + } + + /// Get commit hash as hex string + pub fn hash_hex(&self) -> String { + hex::encode(self.commit_hash) + } +} + +/// Manages commit-reveal per mechanism +pub struct MechanismCommitRevealManager { + /// Current epoch + epoch: RwLock, + /// Commitments per mechanism + commitments: RwLock>, + /// Revealed weights per mechanism + revealed: RwLock>, +} + +impl MechanismCommitRevealManager { + pub fn new() -> Self { + Self { + epoch: RwLock::new(0), + commitments: RwLock::new(HashMap::new()), + revealed: RwLock::new(HashMap::new()), + } + } + + /// Start new epoch + pub fn new_epoch(&self, epoch: u64) { + *self.epoch.write() = epoch; + self.commitments.write().clear(); + self.revealed.write().clear(); + info!("MechanismCommitReveal: new epoch {}", epoch); + } + + /// Commit weights for a mechanism + pub fn commit(&self, commitment: MechanismCommitment) { + debug!( + "Committing weights for mechanism {} epoch {}: hash={}", + commitment.mechanism_id, + commitment.epoch, + commitment.hash_hex() + ); + self.commitments + .write() + .insert(commitment.mechanism_id, commitment); + } + + /// Reveal weights for a mechanism + pub fn reveal(&self, mechanism_id: u8, weights: MechanismWeights) -> Result<(), String> { + let commitment = self + .commitments + .read() + .get(&mechanism_id) + .cloned() + .ok_or_else(|| format!("No commitment for mechanism {}", mechanism_id))?; + + // Verify hash matches + let expected_hash = + MechanismCommitment::compute_hash(&weights.uids, &weights.weights, &commitment.salt); + + if expected_hash != commitment.commit_hash { + return Err(format!( + "Commitment mismatch for mechanism {}: expected {}, got {}", + mechanism_id, + hex::encode(commitment.commit_hash), + hex::encode(expected_hash) + )); + } + + debug!( + "Revealed weights for mechanism {}: {} weights", + mechanism_id, + weights.weights.len() + ); + self.revealed.write().insert(mechanism_id, weights); + Ok(()) + } + + /// Get all revealed weights for batch submission + pub fn get_revealed_weights(&self) -> Vec<(u8, Vec, Vec)> { + self.revealed + .read() + .values() + .map(|w| w.as_batch_tuple()) + .collect() + } + + /// Check if all committed mechanisms have been revealed + pub fn all_revealed(&self) -> bool { + let commitments = self.commitments.read(); + let revealed = self.revealed.read(); + commitments.keys().all(|m| revealed.contains_key(m)) + } + + /// Get commitment for a mechanism + pub fn get_commitment(&self, mechanism_id: u8) -> Option { + self.commitments.read().get(&mechanism_id).cloned() + } + + /// Get all commitments + pub fn get_all_commitments(&self) -> Vec { + self.commitments.read().values().cloned().collect() + } +} + +impl Default for MechanismCommitRevealManager { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mechanism_weights_with_emission() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.6), + WeightAssignment::new("hotkey2".to_string(), 0.4), + ]; + + // Create hotkey -> UID mapping + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + hotkey_to_uid.insert("hotkey2".to_string(), 2); + + // 10% emission weight - challenge controls 10% of total emissions + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.1, + &hotkey_to_uid, + ); + + // Should have 3 UIDs: UID 0 (burn) + 2 miners + assert_eq!(mech_weights.uids.len(), 3); + assert_eq!(mech_weights.weights.len(), 3); + + // UID 0 should be first (burn address) + assert_eq!(mech_weights.uids[0], BURN_UID); + + // Weights should sum to MAX_WEIGHT (65535) + let sum: u32 = mech_weights.weights.iter().map(|w| *w as u32).sum(); + assert!( + (65530..=65540).contains(&sum), + "Sum should be ~65535, got {}", + sum + ); + + // UID 0 should get ~90% (since emission_weight is 10%) + let burn_weight = mech_weights.weights[0] as f64 / MAX_WEIGHT as f64; + assert!( + burn_weight > 0.89 && burn_weight < 0.91, + "Burn should be ~90%, got {}", + burn_weight * 100.0 + ); + } + + #[test] + fn test_mechanism_weights_full_emission() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.6), + WeightAssignment::new("hotkey2".to_string(), 0.4), + ]; + + // Create hotkey -> UID mapping + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + hotkey_to_uid.insert("hotkey2".to_string(), 2); + + // 100% emission weight - challenge controls all emissions + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 1.0, + &hotkey_to_uid, + ); + + // Should have 2 UIDs (no burn needed when emission is 100%) + assert!(mech_weights.uids.len() >= 2); + + // Weights should sum to MAX_WEIGHT + let sum: u32 = mech_weights.weights.iter().map(|w| *w as u32).sum(); + assert!( + (65530..=65540).contains(&sum), + "Sum should be ~65535, got {}", + sum + ); + } + + #[test] + fn test_mechanism_weight_manager() { + let manager = MechanismWeightManager::new(1); + + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + + manager.register_challenge(challenge1, 1); + manager.register_challenge(challenge2, 2); + + let weights1 = vec![WeightAssignment::new("a".to_string(), 0.5)]; + let weights2 = vec![WeightAssignment::new("b".to_string(), 0.5)]; + + // Each challenge gets 50% emission + manager.submit_weights(challenge1, 1, weights1, 0.5); + manager.submit_weights(challenge2, 2, weights2, 0.5); + + let all_weights = manager.get_all_mechanism_weights(); + assert_eq!(all_weights.len(), 2); + } + + #[test] + fn test_commit_reveal() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let weights = MechanismWeights::new( + 1, + ChallengeId::new(), + vec![WeightAssignment::new("agent".to_string(), 1.0)], + 1.0, // 100% emission + ); + + let salt = b"test_salt".to_vec(); + let commitment = MechanismCommitment::new(1, 1, &weights, &salt); + + manager.commit(commitment); + assert!(manager.reveal(1, weights).is_ok()); + assert!(manager.all_revealed()); + } + + #[test] + fn test_empty_weights_go_to_burn() { + let mech_weights = MechanismWeights::new(1, ChallengeId::new(), vec![], 0.5); + + // All weight should go to UID 0 + assert_eq!(mech_weights.uids.len(), 1); + assert_eq!(mech_weights.uids[0], BURN_UID); + assert_eq!(mech_weights.weights[0], MAX_WEIGHT); + } + + #[test] + fn test_zero_emission_weight_goes_to_burn() { + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.0, + &hotkey_to_uid, + ); + + // All weight should go to UID 0 when emission_weight is 0 + assert_eq!(mech_weights.uids.len(), 1); + assert_eq!(mech_weights.uids[0], BURN_UID); + assert_eq!(mech_weights.weights[0], MAX_WEIGHT); + } + + #[test] + fn test_negative_total_weight() { + // Can't really have negative weights, but test zero/invalid case + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 0.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.5, + &hotkey_to_uid, + ); + + // Should go to burn when weights are zero + assert_eq!(mech_weights.uids[0], BURN_UID); + } + + #[test] + fn test_hotkey_not_in_mapping() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.6), + WeightAssignment::new("hotkey_unknown".to_string(), 0.4), + ]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.5, + &hotkey_to_uid, + ); + + // Should only have weights for hotkey1 + burn + // hotkey_unknown is skipped + assert!(mech_weights.uids.len() <= 2); + } + + #[test] + fn test_uid_zero_skipped() { + let assignments = vec![ + WeightAssignment::new("hotkey_zero".to_string(), 0.5), + WeightAssignment::new("hotkey1".to_string(), 0.5), + ]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey_zero".to_string(), BURN_UID); // Maps to UID 0 + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.5, + &hotkey_to_uid, + ); + + // hotkey_zero assignment should be skipped, UID 0 gets burn weight + assert!(mech_weights.uids.contains(&BURN_UID)); + assert!(mech_weights.uids.contains(&1)); + } + + #[test] + fn test_as_batch_tuple() { + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 5, + ChallengeId::new(), + assignments, + 0.3, + &hotkey_to_uid, + ); + + let (mech_id, uids, weights) = mech_weights.as_batch_tuple(); + assert_eq!(mech_id, 5); + assert_eq!(uids.len(), weights.len()); + } + + #[test] + fn test_mechanism_weight_manager_operations() { + let manager = MechanismWeightManager::new(5); + + assert_eq!(manager.epoch(), 5); + assert_eq!(manager.mechanism_count(), 0); + + let challenge1 = ChallengeId::new(); + manager.register_challenge(challenge1, 1); + + assert_eq!(manager.get_mechanism_for_challenge(&challenge1), Some(1)); + + let weights = vec![WeightAssignment::new("agent".to_string(), 1.0)]; + manager.submit_weights(challenge1, 1, weights, 0.5); + + assert_eq!(manager.mechanism_count(), 1); + assert!(manager.list_mechanisms().contains(&1)); + + let mech_weights = manager.get_mechanism_weights(1); + assert!(mech_weights.is_some()); + + let all_weights = manager.get_all_mechanism_weights(); + assert_eq!(all_weights.len(), 1); + + manager.clear(); + assert_eq!(manager.mechanism_count(), 0); + } + + #[test] + fn test_mechanism_weight_manager_metagraph() { + let manager = MechanismWeightManager::new(1); + let challenge = ChallengeId::new(); + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + let weights = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + manager.submit_weights_with_metagraph(challenge, 1, weights, 0.5, &hotkey_to_uid); + + assert_eq!(manager.mechanism_count(), 1); + } + + #[test] + fn test_mechanism_commitment_hash() { + let weights = MechanismWeights::new( + 1, + ChallengeId::new(), + vec![WeightAssignment::new("agent".to_string(), 1.0)], + 1.0, + ); + + let salt = b"test_salt"; + let commitment = MechanismCommitment::new(1, 1, &weights, salt); + + assert_eq!(commitment.mechanism_id, 1); + assert_eq!(commitment.epoch, 1); + assert_eq!(commitment.salt, salt); + assert!(!commitment.hash_hex().is_empty()); + } + + #[test] + fn test_mechanism_commitment_different_salts() { + let weights = MechanismWeights::new( + 1, + ChallengeId::new(), + vec![WeightAssignment::new("agent".to_string(), 1.0)], + 1.0, + ); + + let commitment1 = MechanismCommitment::new(1, 1, &weights, b"salt1"); + let commitment2 = MechanismCommitment::new(1, 1, &weights, b"salt2"); + + // Different salts should produce different hashes + assert_ne!(commitment1.commit_hash, commitment2.commit_hash); + } + + #[test] + fn test_mechanism_commit_reveal_manager() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let weights = MechanismWeights::new( + 1, + ChallengeId::new(), + vec![WeightAssignment::new("agent".to_string(), 1.0)], + 1.0, + ); + + let salt = b"test_salt".to_vec(); + let commitment = MechanismCommitment::new(1, 1, &weights, &salt); + + manager.commit(commitment.clone()); + + let retrieved = manager.get_commitment(1); + assert!(retrieved.is_some()); + assert_eq!(retrieved.unwrap().mechanism_id, commitment.mechanism_id); + + assert!(manager.reveal(1, weights).is_ok()); + assert!(manager.all_revealed()); + + let revealed_weights = manager.get_revealed_weights(); + assert_eq!(revealed_weights.len(), 1); + } + + #[test] + fn test_mechanism_commit_reveal_mismatch() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let assignments1 = vec![WeightAssignment::new("agent1".to_string(), 1.0)]; + let assignments2 = vec![WeightAssignment::new("agent2".to_string(), 1.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("agent1".to_string(), 1); + hotkey_to_uid.insert("agent2".to_string(), 2); + + let weights1 = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments1, + 1.0, + &hotkey_to_uid, + ); + + let weights2 = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments2, + 1.0, + &hotkey_to_uid, + ); + + let salt = b"test_salt".to_vec(); + let commitment = MechanismCommitment::new(1, 1, &weights1, &salt); + + manager.commit(commitment); + + // Try to reveal with different weights + let result = manager.reveal(1, weights2); + assert!(result.is_err()); + } + + #[test] + fn test_mechanism_commit_reveal_no_commitment() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let weights = MechanismWeights::new( + 1, + ChallengeId::new(), + vec![WeightAssignment::new("agent".to_string(), 1.0)], + 1.0, + ); + + // Try to reveal without committing + let result = manager.reveal(1, weights); + assert!(result.is_err()); + } + + #[test] + fn test_mechanism_commit_reveal_manager_default() { + let manager = MechanismCommitRevealManager::default(); + // Verify initial state - with no commitments, all_revealed() returns true (vacuously) + assert!(manager.all_revealed()); + assert!(manager.get_all_commitments().is_empty()); + } + + #[test] + fn test_get_all_commitments() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let weights1 = MechanismWeights::new(1, ChallengeId::new(), vec![], 1.0); + let weights2 = MechanismWeights::new(2, ChallengeId::new(), vec![], 1.0); + + let commitment1 = MechanismCommitment::new(1, 1, &weights1, b"salt1"); + let commitment2 = MechanismCommitment::new(2, 1, &weights2, b"salt2"); + + manager.commit(commitment1); + manager.commit(commitment2); + + let all = manager.get_all_commitments(); + assert_eq!(all.len(), 2); + } + + #[test] + fn test_partial_reveals() { + let manager = MechanismCommitRevealManager::new(); + manager.new_epoch(1); + + let weights1 = MechanismWeights::new(1, ChallengeId::new(), vec![], 1.0); + let weights2 = MechanismWeights::new(2, ChallengeId::new(), vec![], 1.0); + + let commitment1 = MechanismCommitment::new(1, 1, &weights1, b"salt1"); + let commitment2 = MechanismCommitment::new(2, 1, &weights2, b"salt2"); + + manager.commit(commitment1); + manager.commit(commitment2); + + // Only reveal mechanism 1 + manager.reveal(1, weights1).unwrap(); + + assert!(!manager.all_revealed()); + } + + #[test] + fn test_emission_weight_clamping() { + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + // Test clamping to [0, 1] range + let mech_weights_over = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments.clone(), + 1.5, // Over 1.0 + &hotkey_to_uid, + ); + assert_eq!(mech_weights_over.emission_weight, 1.0); + + let mech_weights_under = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + -0.5, // Under 0.0 + &hotkey_to_uid, + ); + assert_eq!(mech_weights_under.emission_weight, 0.0); + } + + #[test] + fn test_mechanism_weights_new_warning() { + // Test that MechanismWeights::new() without hotkey mapping works + // (even though it logs a warning) + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + let mech_weights = MechanismWeights::new(1, ChallengeId::new(), assignments, 0.5); + + // Should have UID 0 since no hotkeys can be resolved + assert!(mech_weights.uids.contains(&BURN_UID)); + } + + #[test] + fn test_multiple_mechanisms() { + let manager = MechanismWeightManager::new(1); + + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + let challenge3 = ChallengeId::new(); + + manager.register_challenge(challenge1, 1); + manager.register_challenge(challenge2, 2); + manager.register_challenge(challenge3, 3); + + let weights1 = vec![WeightAssignment::new("a".to_string(), 1.0)]; + let weights2 = vec![WeightAssignment::new("b".to_string(), 1.0)]; + let weights3 = vec![WeightAssignment::new("c".to_string(), 1.0)]; + + manager.submit_weights(challenge1, 1, weights1, 0.3); + manager.submit_weights(challenge2, 2, weights2, 0.3); + manager.submit_weights(challenge3, 3, weights3, 0.4); + + let mechanisms = manager.list_mechanisms(); + assert_eq!(mechanisms.len(), 3); + + let all_weights = manager.get_all_mechanism_weights(); + assert_eq!(all_weights.len(), 3); + } + + #[test] + fn test_mechanism_weights_raw_storage() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.6), + WeightAssignment::new("hotkey2".to_string(), 0.4), + ]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + hotkey_to_uid.insert("hotkey2".to_string(), 2); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments.clone(), + 0.5, + &hotkey_to_uid, + ); + + // Verify raw weights are preserved + assert_eq!(mech_weights.raw_weights.len(), 2); + assert_eq!(mech_weights.raw_weights[0].weight, 0.6); + assert_eq!(mech_weights.raw_weights[1].weight, 0.4); + } + + #[test] + fn test_very_small_weights() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.001), + WeightAssignment::new("hotkey2".to_string(), 0.002), + ]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + hotkey_to_uid.insert("hotkey2".to_string(), 2); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 0.01, // Very small emission weight + &hotkey_to_uid, + ); + + // Should still handle small weights + assert!(!mech_weights.uids.is_empty()); + let sum: u32 = mech_weights.weights.iter().map(|w| *w as u32).sum(); + assert_eq!(sum, MAX_WEIGHT as u32); + } + + #[test] + fn test_rounding_weights() { + let assignments = vec![ + WeightAssignment::new("hotkey1".to_string(), 0.333), + WeightAssignment::new("hotkey2".to_string(), 0.333), + WeightAssignment::new("hotkey3".to_string(), 0.334), + ]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + hotkey_to_uid.insert("hotkey2".to_string(), 2); + hotkey_to_uid.insert("hotkey3".to_string(), 3); + + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 1.0, + &hotkey_to_uid, + ); + + // Verify weights sum correctly despite rounding + let sum: u32 = mech_weights.weights.iter().map(|w| *w as u32).sum(); + assert!((65530..=65540).contains(&sum)); + } + + #[test] + fn test_saturating_sub_in_burn_weight() { + let assignments = vec![WeightAssignment::new("hotkey1".to_string(), 1.0)]; + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("hotkey1".to_string(), 1); + + // With 100% emission and rounding, burn weight should be 0 or very small + let mech_weights = MechanismWeights::with_hotkey_mapping( + 1, + ChallengeId::new(), + assignments, + 1.0, + &hotkey_to_uid, + ); + + // Verify proper handling of saturating_sub + let sum: u32 = mech_weights.weights.iter().map(|w| *w as u32).sum(); + assert!((65530..=65540).contains(&sum)); + } + + #[test] + fn test_mechanism_id_storage() { + let mech_weights = MechanismWeights::new( + 255, // Max u8 value + ChallengeId::new(), + vec![], + 0.5, + ); + + assert_eq!(mech_weights.mechanism_id, 255); + + let (mech_id, _, _) = mech_weights.as_batch_tuple(); + assert_eq!(mech_id, 255); + } + + #[test] + fn test_complete_workflow() { + // Complete workflow test + let manager = MechanismWeightManager::new(10); + + let challenge = ChallengeId::new(); + manager.register_challenge(challenge, 5); + + let mut hotkey_to_uid: HotkeyUidMap = HashMap::new(); + hotkey_to_uid.insert("validator1".to_string(), 10); + hotkey_to_uid.insert("validator2".to_string(), 20); + + let weights = vec![ + WeightAssignment::new("validator1".to_string(), 0.7), + WeightAssignment::new("validator2".to_string(), 0.3), + ]; + + manager.submit_weights_with_metagraph(challenge, 5, weights, 0.4, &hotkey_to_uid); + + let retrieved = manager.get_mechanism_weights(5); + assert!(retrieved.is_some()); + + let mech_weights = retrieved.unwrap(); + assert_eq!(mech_weights.mechanism_id, 5); + assert_eq!(mech_weights.challenge_id, challenge); + + let all = manager.get_all_mechanism_weights(); + assert_eq!(all.len(), 1); + + let (mech_id, uids, weights) = &all[0]; + assert_eq!(*mech_id, 5); + assert!(!uids.is_empty()); + assert_eq!(uids.len(), weights.len()); + } + + #[test] + fn test_unknown_challenge_mechanism() { + let manager = MechanismWeightManager::new(1); + let unknown_challenge = ChallengeId::new(); + + assert_eq!( + manager.get_mechanism_for_challenge(&unknown_challenge), + None + ); + } + + #[test] + fn test_get_nonexistent_mechanism() { + let manager = MechanismWeightManager::new(1); + assert!(manager.get_mechanism_weights(99).is_none()); + } +} diff --git a/crates/p2p-consensus/Cargo.toml b/crates/p2p-consensus/Cargo.toml new file mode 100644 index 000000000..8d9dfdf5e --- /dev/null +++ b/crates/p2p-consensus/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "platform-p2p-consensus" +version.workspace = true +edition.workspace = true +description = "Decentralized P2P consensus for Platform Network validators" + +[dependencies] +platform-core = { path = "../core" } + +# Networking +libp2p = { version = "0.54", features = ["tokio", "gossipsub", "kad", "identify", "noise", "yamux", "tcp", "dns", "macros", "serde"] } + +# Async +tokio = { workspace = true } +futures = { workspace = true } +async-trait = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Crypto +sha2 = { workspace = true } +rand = { workspace = true } +hex = { workspace = true } + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +chrono = { workspace = true } +parking_lot = { workspace = true } + +[dev-dependencies] +tokio-test = { workspace = true } +tempfile = { workspace = true } diff --git a/crates/p2p-consensus/src/config.rs b/crates/p2p-consensus/src/config.rs new file mode 100644 index 000000000..34c40264f --- /dev/null +++ b/crates/p2p-consensus/src/config.rs @@ -0,0 +1,163 @@ +//! P2P network configuration +//! +//! Provides configuration for the decentralized P2P network including +//! listen addresses, bootstrap peers, and consensus parameters. + +use serde::{Deserialize, Serialize}; + +/// Bootstrap nodes for the Platform P2P network. +/// +/// Each entry should be a multiaddr in the format: +/// `/ip4//tcp//p2p/` +/// +/// Configure your bootstrap peers via the BOOTSTRAP_PEERS environment variable +/// or add them here for your deployment. +pub const DEFAULT_BOOTSTRAP_NODES: &[&str] = &[ + // Add your bootstrap peers here +]; + +/// P2P network configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct P2PConfig { + /// Listen addresses (multiaddr format) + pub listen_addrs: Vec, + /// Bootstrap peers (multiaddr format) + pub bootstrap_peers: Vec, + /// Gossipsub topic for consensus messages + pub consensus_topic: String, + /// Gossipsub topic for challenge messages + pub challenge_topic: String, + /// Netuid for the subnet + pub netuid: u16, + /// Minimum validator stake in RAO + pub min_stake: u64, + /// Heartbeat interval in seconds + pub heartbeat_interval_secs: u64, + /// Maximum message size in bytes + pub max_message_size: usize, + /// Connection timeout in seconds + pub connection_timeout_secs: u64, + /// Maximum number of peers to maintain + pub max_peers: usize, +} + +impl Default for P2PConfig { + fn default() -> Self { + Self { + listen_addrs: vec!["/ip4/0.0.0.0/tcp/9000".to_string()], + bootstrap_peers: vec![], + consensus_topic: "platform/consensus/1.0.0".to_string(), + challenge_topic: "platform/challenge/1.0.0".to_string(), + netuid: 100, + min_stake: 1_000_000_000_000, // 1000 TAO + heartbeat_interval_secs: 30, + max_message_size: 16 * 1024 * 1024, // 16 MB + connection_timeout_secs: 30, + max_peers: 64, + } + } +} + +impl P2PConfig { + /// Create a new P2P config with custom listen address + pub fn with_listen_addr(mut self, addr: &str) -> Self { + self.listen_addrs = vec![addr.to_string()]; + self + } + + /// Add bootstrap peers + pub fn with_bootstrap_peers(mut self, peers: Vec) -> Self { + self.bootstrap_peers = peers; + self + } + + /// Set the netuid + pub fn with_netuid(mut self, netuid: u16) -> Self { + self.netuid = netuid; + self + } + + /// Set minimum stake requirement + pub fn with_min_stake(mut self, min_stake: u64) -> Self { + self.min_stake = min_stake; + self + } + + /// Create a development config with relaxed settings + pub fn development() -> Self { + Self { + listen_addrs: vec!["/ip4/127.0.0.1/tcp/0".to_string()], + bootstrap_peers: vec![], + consensus_topic: "platform/consensus/dev".to_string(), + challenge_topic: "platform/challenge/dev".to_string(), + netuid: 100, + min_stake: 0, // No stake requirement in dev + heartbeat_interval_secs: 5, + max_message_size: 16 * 1024 * 1024, + connection_timeout_secs: 10, + max_peers: 32, + } + } + + /// Create a production config with default bootstrap nodes + pub fn production() -> Self { + Self { + listen_addrs: vec![ + "/ip4/0.0.0.0/tcp/9000".to_string(), + "/ip6/::/tcp/9000".to_string(), + ], + bootstrap_peers: DEFAULT_BOOTSTRAP_NODES + .iter() + .map(|s| s.to_string()) + .collect(), + consensus_topic: "platform/consensus/1.0.0".to_string(), + challenge_topic: "platform/challenge/1.0.0".to_string(), + netuid: 100, + min_stake: 1_000_000_000_000, // 1000 TAO + heartbeat_interval_secs: 30, + max_message_size: 16 * 1024 * 1024, + connection_timeout_secs: 30, + max_peers: 64, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_default_config() { + let config = P2PConfig::default(); + assert_eq!(config.netuid, 100); + assert_eq!(config.min_stake, 1_000_000_000_000); + assert!(!config.listen_addrs.is_empty()); + } + + #[test] + fn test_with_listen_addr() { + let config = P2PConfig::default().with_listen_addr("/ip4/127.0.0.1/tcp/8000"); + assert_eq!(config.listen_addrs, vec!["/ip4/127.0.0.1/tcp/8000"]); + } + + #[test] + fn test_with_bootstrap_peers() { + let peers = vec!["/ip4/1.2.3.4/tcp/9000".to_string()]; + let config = P2PConfig::default().with_bootstrap_peers(peers.clone()); + assert_eq!(config.bootstrap_peers, peers); + } + + #[test] + fn test_development_config() { + let config = P2PConfig::development(); + assert_eq!(config.min_stake, 0); + assert_eq!(config.heartbeat_interval_secs, 5); + } + + #[test] + fn test_production_config() { + let config = P2PConfig::production(); + assert_eq!(config.min_stake, 1_000_000_000_000); + assert_eq!(config.listen_addrs.len(), 2); + } +} diff --git a/crates/p2p-consensus/src/consensus.rs b/crates/p2p-consensus/src/consensus.rs new file mode 100644 index 000000000..1438e22d2 --- /dev/null +++ b/crates/p2p-consensus/src/consensus.rs @@ -0,0 +1,1259 @@ +//! PBFT-style consensus implementation +//! +//! Implements a practical Byzantine Fault Tolerant consensus protocol +//! with view changes and leader election. + +use crate::messages::{ + CommitMessage, ConsensusProposal, NewViewMessage, PrePrepare, PrepareMessage, PreparedProof, + ProposalContent, SequenceNumber, StateChangeType, ViewChangeMessage, ViewNumber, +}; +use crate::state::StateManager; +use crate::validator::{LeaderElection, StakeWeightedVoting, ValidatorSet}; +use parking_lot::RwLock; +use platform_core::{Hotkey, Keypair, SignedMessage}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; +use thiserror::Error; +use tracing::{info, warn}; + +/// Consensus errors +#[derive(Error, Debug)] +pub enum ConsensusError { + #[error("Not the leader for view {0}")] + NotLeader(ViewNumber), + #[error("Invalid proposal: {0}")] + InvalidProposal(String), + #[error("Invalid signature from {0}")] + InvalidSignature(String), + #[error("Sequence number mismatch: expected {expected}, got {actual}")] + SequenceMismatch { expected: u64, actual: u64 }, + #[error("View mismatch: expected {expected}, got {actual}")] + ViewMismatch { expected: u64, actual: u64 }, + #[error("Not enough votes: need {needed}, have {have}")] + NotEnoughVotes { needed: usize, have: usize }, + #[error("Consensus timeout")] + Timeout, + #[error("View change in progress")] + ViewChangeInProgress, + #[error("Already voted in this round")] + AlreadyVoted, +} + +/// Consensus phase +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum ConsensusPhase { + /// Idle, waiting for proposal + Idle, + /// Pre-prepare received, collecting prepares + PrePrepare, + /// Prepare quorum reached, collecting commits + Prepared, + /// Commit quorum reached, executing + Committed, + /// View change in progress + ViewChange, +} + +/// State of a consensus round +#[derive(Clone, Debug)] +pub struct ConsensusRound { + /// View number + pub view: ViewNumber, + /// Sequence number + pub sequence: SequenceNumber, + /// Current phase + pub phase: ConsensusPhase, + /// The proposal (if received) + pub proposal: Option, + /// Pre-prepare message + pub pre_prepare: Option, + /// Prepare messages received (validator -> message) + pub prepares: HashMap, + /// Commit messages received (validator -> message) + pub commits: HashMap, + /// Proposal hash for this round + pub proposal_hash: [u8; 32], + /// Whether we have prepared + pub local_prepared: bool, + /// Whether we have committed + pub local_committed: bool, + /// Round start time (unix millis) + pub started_at: i64, +} + +impl ConsensusRound { + fn new(view: ViewNumber, sequence: SequenceNumber) -> Self { + Self { + view, + sequence, + phase: ConsensusPhase::Idle, + proposal: None, + pre_prepare: None, + prepares: HashMap::new(), + commits: HashMap::new(), + proposal_hash: [0u8; 32], + local_prepared: false, + local_committed: false, + started_at: chrono::Utc::now().timestamp_millis(), + } + } +} + +/// View change state +#[derive(Clone, Debug)] +pub struct ViewChangeState { + /// New view being proposed + pub new_view: ViewNumber, + /// View change messages received + pub view_changes: HashMap, + /// When view change started + pub started_at: i64, +} + +/// Result of consensus decision +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ConsensusDecision { + /// View number + pub view: ViewNumber, + /// Sequence number + pub sequence: SequenceNumber, + /// The decided proposal content + pub content: ProposalContent, + /// Commit signatures (proof of consensus) + pub commit_signatures: Vec<(Hotkey, Vec)>, +} + +/// PBFT Consensus engine +pub struct ConsensusEngine { + /// Our keypair + keypair: Keypair, + /// Validator set + validator_set: Arc, + /// Leader election + leader_election: LeaderElection, + /// Stake-weighted voting helper + stake_voting: StakeWeightedVoting, + /// State manager (for applying decisions and sudo checks) + state_manager: Arc, + /// Current view number + current_view: RwLock, + /// Next sequence number + next_sequence: RwLock, + /// Current consensus round + current_round: RwLock>, + /// View change state + view_change_state: RwLock>, + /// Completed decisions (sequence -> decision) + decisions: RwLock>, + /// Round timeout in milliseconds + round_timeout_ms: i64, + /// View change timeout in milliseconds (for extended view change operations) + view_change_timeout_ms: i64, +} + +impl ConsensusEngine { + /// Create new consensus engine + pub fn new( + keypair: Keypair, + validator_set: Arc, + state_manager: Arc, + ) -> Self { + let leader_election = LeaderElection::new(validator_set.clone()); + let stake_voting = StakeWeightedVoting::new(validator_set.clone()); + + Self { + keypair, + validator_set, + leader_election, + stake_voting, + state_manager, + current_view: RwLock::new(0), + next_sequence: RwLock::new(1), + current_round: RwLock::new(None), + view_change_state: RwLock::new(None), + decisions: RwLock::new(HashMap::new()), + round_timeout_ms: 30_000, // 30 seconds + view_change_timeout_ms: 60_000, // 60 seconds + } + } + + /// Get current view number + pub fn current_view(&self) -> ViewNumber { + *self.current_view.read() + } + + /// Get next sequence number + pub fn next_sequence(&self) -> SequenceNumber { + *self.next_sequence.read() + } + + /// Check if we are the current leader + pub fn am_i_leader(&self) -> bool { + self.leader_election.am_i_leader(*self.current_view.read()) + } + + /// Get the current leader's hotkey + pub fn current_leader(&self) -> Option { + self.leader_election + .leader_for_view(*self.current_view.read()) + } + + /// Get required quorum size (2f+1) + pub fn quorum_size(&self) -> usize { + self.validator_set.quorum_size() + } + + /// Create a new proposal (called by leader) + pub fn create_proposal( + &self, + change_type: StateChangeType, + data: Vec, + ) -> Result { + let view = *self.current_view.read(); + + if !self.leader_election.am_i_leader(view) { + return Err(ConsensusError::NotLeader(view)); + } + + // Verify sudo authorization for ConfigUpdate proposals + if change_type == StateChangeType::ConfigUpdate { + let is_sudo = self + .state_manager + .read(|s| s.is_sudo(&self.keypair.hotkey())); + if !is_sudo { + return Err(ConsensusError::InvalidProposal( + "ConfigUpdate requires sudo authorization".to_string(), + )); + } + } + + let sequence = *self.next_sequence.read(); + + // Hash the data + let mut hasher = Sha256::new(); + hasher.update(&data); + let data_hash: [u8; 32] = hasher.finalize().into(); + + let content = ProposalContent { + change_type, + data, + data_hash, + }; + + // Create signature + #[derive(Serialize)] + struct SigningData { + view: ViewNumber, + sequence: SequenceNumber, + data_hash: [u8; 32], + } + + let signing_data = SigningData { + view, + sequence, + data_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let proposal = ConsensusProposal { + view, + sequence, + proposal: content, + proposer: self.keypair.hotkey(), + signature, + timestamp: chrono::Utc::now().timestamp_millis(), + }; + + // Start the round + let mut round = ConsensusRound::new(view, sequence); + round.proposal = Some(proposal.clone()); + round.proposal_hash = data_hash; + round.phase = ConsensusPhase::PrePrepare; + *self.current_round.write() = Some(round); + + info!(view, sequence, "Created consensus proposal"); + Ok(proposal) + } + + /// Handle incoming proposal (for non-leaders) + pub fn handle_proposal( + &self, + proposal: ConsensusProposal, + ) -> Result { + let view = *self.current_view.read(); + let sequence = *self.next_sequence.read(); + + // Validate view and sequence + if proposal.view != view { + return Err(ConsensusError::ViewMismatch { + expected: view, + actual: proposal.view, + }); + } + + if proposal.sequence != sequence { + return Err(ConsensusError::SequenceMismatch { + expected: sequence, + actual: proposal.sequence, + }); + } + + // Verify proposer is the leader + let expected_leader = self.leader_election.leader_for_view(view); + if expected_leader.as_ref() != Some(&proposal.proposer) { + return Err(ConsensusError::InvalidProposal(format!( + "Proposer {:?} is not the leader", + proposal.proposer + ))); + } + + // Verify proposal hash + let mut hasher = Sha256::new(); + hasher.update(&proposal.proposal.data); + let computed_hash: [u8; 32] = hasher.finalize().into(); + + if computed_hash != proposal.proposal.data_hash { + return Err(ConsensusError::InvalidProposal( + "Data hash mismatch".to_string(), + )); + } + + // Verify cryptographic signature on the proposal + #[derive(Serialize)] + struct ProposalSigningData { + view: ViewNumber, + sequence: SequenceNumber, + data_hash: [u8; 32], + } + + let signing_data = ProposalSigningData { + view: proposal.view, + sequence: proposal.sequence, + data_hash: proposal.proposal.data_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signed_msg = SignedMessage { + message: signing_bytes, + signature: proposal.signature.clone(), + signer: proposal.proposer.clone(), + }; + + let is_valid_sig = signed_msg + .verify() + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature(proposal.proposer.to_hex())); + } + + // Verify sudo authorization for ConfigUpdate proposals + if proposal.proposal.change_type == StateChangeType::ConfigUpdate { + let is_sudo = self.state_manager.read(|s| s.is_sudo(&proposal.proposer)); + if !is_sudo { + return Err(ConsensusError::InvalidProposal( + "ConfigUpdate requires sudo authorization".to_string(), + )); + } + } + + // Start round + let mut round = ConsensusRound::new(view, sequence); + round.proposal = Some(proposal); + round.proposal_hash = computed_hash; + round.phase = ConsensusPhase::PrePrepare; + *self.current_round.write() = Some(round); + + // Create prepare message + let prepare = self.create_prepare(view, sequence, computed_hash)?; + + info!(view, sequence, "Handling proposal, sending prepare"); + Ok(prepare) + } + + /// Create pre-prepare message (leader sends after receiving proposal) + pub fn create_pre_prepare( + &self, + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + ) -> Result { + if !self.leader_election.am_i_leader(view) { + return Err(ConsensusError::NotLeader(view)); + } + + #[derive(Serialize)] + struct SigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let signing_data = SigningData { + view, + sequence, + proposal_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let pre_prepare = PrePrepare { + view, + sequence, + proposal_hash, + leader: self.keypair.hotkey(), + signature, + }; + + // Store in current round + if let Some(round) = self.current_round.write().as_mut() { + round.pre_prepare = Some(pre_prepare.clone()); + } + + Ok(pre_prepare) + } + + /// Create prepare message + fn create_prepare( + &self, + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + ) -> Result { + #[derive(Serialize)] + struct SigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let signing_data = SigningData { + view, + sequence, + proposal_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let prepare = PrepareMessage { + view, + sequence, + proposal_hash, + validator: self.keypair.hotkey(), + signature, + }; + + // Mark as locally prepared + if let Some(round) = self.current_round.write().as_mut() { + round.local_prepared = true; + round + .prepares + .insert(self.keypair.hotkey(), prepare.clone()); + } + + Ok(prepare) + } + + /// Handle incoming prepare message + pub fn handle_prepare( + &self, + prepare: PrepareMessage, + ) -> Result, ConsensusError> { + use std::collections::hash_map::Entry; + + let mut round_guard = self.current_round.write(); + let round = round_guard + .as_mut() + .ok_or_else(|| ConsensusError::InvalidProposal("No active round".to_string()))?; + + // Validate view and sequence + if prepare.view != round.view { + return Err(ConsensusError::ViewMismatch { + expected: round.view, + actual: prepare.view, + }); + } + + if prepare.sequence != round.sequence { + return Err(ConsensusError::SequenceMismatch { + expected: round.sequence, + actual: prepare.sequence, + }); + } + + // Validate proposal hash + if prepare.proposal_hash != round.proposal_hash { + return Err(ConsensusError::InvalidProposal( + "Proposal hash mismatch".to_string(), + )); + } + + // Verify cryptographic signature on the prepare message + #[derive(Serialize)] + struct PrepareSigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let signing_data = PrepareSigningData { + view: prepare.view, + sequence: prepare.sequence, + proposal_hash: prepare.proposal_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let is_valid_sig = self + .validator_set + .verify_signature(&prepare.validator, &signing_bytes, &prepare.signature) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature(prepare.validator.to_hex())); + } + + // Atomic check and insert using Entry API to prevent TOCTOU race + match round.prepares.entry(prepare.validator.clone()) { + Entry::Occupied(_) => return Err(ConsensusError::AlreadyVoted), + Entry::Vacant(entry) => entry.insert(prepare), + }; + + // Check if we have quorum and sufficient stake weight + let quorum = self.quorum_size(); + let voters: Vec = round.prepares.keys().cloned().collect(); + let meets_weight_threshold = self.stake_voting.meets_threshold(&voters, 2.0 / 3.0); + let voting_power = self.stake_voting.total_voting_power(&voters); + if round.prepares.len() >= quorum + && meets_weight_threshold + && round.phase == ConsensusPhase::PrePrepare + { + round.phase = ConsensusPhase::Prepared; + info!( + view = round.view, + sequence = round.sequence, + prepares = round.prepares.len(), + voting_power, + "Reached prepare quorum" + ); + // Extract data needed for commit while still holding lock to avoid race condition + let view = round.view; + let sequence = round.sequence; + let proposal_hash = round.proposal_hash; + + // Mark as locally committed while holding lock + round.local_committed = true; + + // Create commit message while still holding lock + let commit = self.create_commit_internal(view, sequence, proposal_hash)?; + round.commits.insert(self.keypair.hotkey(), commit.clone()); + + drop(round_guard); + return Ok(Some(commit)); + } + + Ok(None) + } + + /// Internal commit message creation - does not acquire current_round lock + /// Use this when the caller already holds the lock to avoid race conditions + fn create_commit_internal( + &self, + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + ) -> Result { + #[derive(Serialize)] + struct SigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let signing_data = SigningData { + view, + sequence, + proposal_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + Ok(CommitMessage { + view, + sequence, + proposal_hash, + validator: self.keypair.hotkey(), + signature, + }) + } + + /// Handle incoming commit message + pub fn handle_commit( + &self, + commit: CommitMessage, + ) -> Result, ConsensusError> { + use std::collections::hash_map::Entry; + + let mut round_guard = self.current_round.write(); + let round = round_guard + .as_mut() + .ok_or_else(|| ConsensusError::InvalidProposal("No active round".to_string()))?; + + // Validate view and sequence + if commit.view != round.view { + return Err(ConsensusError::ViewMismatch { + expected: round.view, + actual: commit.view, + }); + } + + if commit.sequence != round.sequence { + return Err(ConsensusError::SequenceMismatch { + expected: round.sequence, + actual: commit.sequence, + }); + } + + // Validate proposal hash + if commit.proposal_hash != round.proposal_hash { + return Err(ConsensusError::InvalidProposal( + "Proposal hash mismatch".to_string(), + )); + } + + // Verify cryptographic signature on the commit message + #[derive(Serialize)] + struct CommitSigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let signing_data = CommitSigningData { + view: commit.view, + sequence: commit.sequence, + proposal_hash: commit.proposal_hash, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let is_valid_sig = self + .validator_set + .verify_signature(&commit.validator, &signing_bytes, &commit.signature) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature(commit.validator.to_hex())); + } + + // Atomic check and insert using Entry API to prevent TOCTOU race + match round.commits.entry(commit.validator.clone()) { + Entry::Occupied(_) => return Err(ConsensusError::AlreadyVoted), + Entry::Vacant(entry) => entry.insert(commit), + }; + + // Check if we have quorum and sufficient stake weight + let quorum = self.quorum_size(); + let voters: Vec = round.commits.keys().cloned().collect(); + let meets_weight_threshold = self.stake_voting.meets_threshold(&voters, 2.0 / 3.0); + let voting_power = self.stake_voting.total_voting_power(&voters); + if round.commits.len() >= quorum + && meets_weight_threshold + && round.phase == ConsensusPhase::Prepared + { + round.phase = ConsensusPhase::Committed; + info!( + view = round.view, + sequence = round.sequence, + commits = round.commits.len(), + voting_power, + "Reached commit quorum - consensus achieved!" + ); + // Create decision + let proposal = round.proposal.as_ref().ok_or_else(|| { + ConsensusError::InvalidProposal("No proposal in committed round".to_string()) + })?; + let decision = ConsensusDecision { + view: round.view, + sequence: round.sequence, + content: proposal.proposal.clone(), + commit_signatures: round + .commits + .iter() + .map(|(h, c)| (h.clone(), c.signature.clone())) + .collect(), + }; + + // Store decision + let seq = round.sequence; + drop(round_guard); + self.decisions.write().insert(seq, decision.clone()); + + // Increment sequence + *self.next_sequence.write() += 1; + + // Clear current round + *self.current_round.write() = None; + + return Ok(Some(decision)); + } + + Ok(None) + } + + /// Initiate view change + pub fn initiate_view_change( + &self, + new_view: ViewNumber, + ) -> Result { + let current_view = *self.current_view.read(); + if new_view <= current_view { + return Err(ConsensusError::ViewMismatch { + expected: current_view + 1, + actual: new_view, + }); + } + + // Get last prepared info + let (last_prepared_sequence, prepared_proof) = { + let round = self.current_round.read(); + if let Some(r) = round.as_ref() { + if let Some(pre_prepare) = r.pre_prepare.clone() { + if r.phase >= ConsensusPhase::Prepared { + let proof = PreparedProof { + pre_prepare, + prepares: r.prepares.values().cloned().collect(), + }; + (Some(r.sequence), Some(proof)) + } else { + (None, None) + } + } else { + (None, None) + } + } else { + (None, None) + } + }; + + #[derive(Serialize)] + struct SigningData { + new_view: ViewNumber, + last_prepared_sequence: Option, + } + + let signing_data = SigningData { + new_view, + last_prepared_sequence, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let view_change = ViewChangeMessage { + new_view, + last_prepared_sequence, + prepared_proof, + validator: self.keypair.hotkey(), + signature, + }; + + // Start view change state + let mut state = ViewChangeState { + new_view, + view_changes: HashMap::new(), + started_at: chrono::Utc::now().timestamp_millis(), + }; + state + .view_changes + .insert(self.keypair.hotkey(), view_change.clone()); + *self.view_change_state.write() = Some(state); + + info!(new_view, "Initiating view change"); + Ok(view_change) + } + + /// Handle incoming view change message + pub fn handle_view_change( + &self, + view_change: ViewChangeMessage, + ) -> Result, ConsensusError> { + // Verify cryptographic signature on the view change message + #[derive(Serialize)] + struct ViewChangeSigningData { + new_view: ViewNumber, + last_prepared_sequence: Option, + } + + let signing_data = ViewChangeSigningData { + new_view: view_change.new_view, + last_prepared_sequence: view_change.last_prepared_sequence, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let is_valid_sig = self + .validator_set + .verify_signature( + &view_change.validator, + &signing_bytes, + &view_change.signature, + ) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature( + view_change.validator.to_hex(), + )); + } + + // Verify prepared_proof signatures if present + if let Some(ref proof) = view_change.prepared_proof { + // Verify PrePrepare signature + #[derive(Serialize)] + struct PrePrepareSigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let pre_prepare_signing_data = PrePrepareSigningData { + view: proof.pre_prepare.view, + sequence: proof.pre_prepare.sequence, + proposal_hash: proof.pre_prepare.proposal_hash, + }; + + let pre_prepare_signing_bytes = bincode::serialize(&pre_prepare_signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let pre_prepare_valid = self + .validator_set + .verify_signature( + &proof.pre_prepare.leader, + &pre_prepare_signing_bytes, + &proof.pre_prepare.signature, + ) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !pre_prepare_valid { + return Err(ConsensusError::InvalidSignature(format!( + "Invalid PrePrepare signature from {}", + proof.pre_prepare.leader.to_hex() + ))); + } + + // Verify each Prepare message signature + #[derive(Serialize)] + struct PrepareSigningData { + view: ViewNumber, + sequence: SequenceNumber, + proposal_hash: [u8; 32], + } + + let mut valid_prepare_count = 0; + let mut valid_prepare_hotkeys = HashSet::new(); + for prepare in &proof.prepares { + let prepare_signing_data = PrepareSigningData { + view: prepare.view, + sequence: prepare.sequence, + proposal_hash: prepare.proposal_hash, + }; + + let prepare_signing_bytes = bincode::serialize(&prepare_signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let prepare_valid = self + .validator_set + .verify_signature( + &prepare.validator, + &prepare_signing_bytes, + &prepare.signature, + ) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if prepare_valid { + valid_prepare_count += 1; + valid_prepare_hotkeys.insert(prepare.validator.clone()); + } else { + warn!( + validator = %prepare.validator.to_hex(), + "Invalid Prepare signature in prepared_proof, skipping" + ); + } + } + + // Verify we have 2f+1 valid prepares + let quorum = self.quorum_size(); + if valid_prepare_count < quorum { + return Err(ConsensusError::NotEnoughVotes { + needed: quorum, + have: valid_prepare_count, + }); + } + + let valid_prepare_voters: Vec = valid_prepare_hotkeys.into_iter().collect(); + if !self + .stake_voting + .meets_threshold(&valid_prepare_voters, 2.0 / 3.0) + { + return Err(ConsensusError::InvalidProposal( + "Prepared proof lacks weighted quorum".to_string(), + )); + } + } + + let mut state_guard = self.view_change_state.write(); + + let state = state_guard.get_or_insert_with(|| ViewChangeState { + new_view: view_change.new_view, + view_changes: HashMap::new(), + started_at: chrono::Utc::now().timestamp_millis(), + }); + + // Handle view number mismatch + if view_change.new_view != state.new_view { + if view_change.new_view > state.new_view { + // Higher view - switch to it + info!( + old_view = state.new_view, + new_view = view_change.new_view, + "Switching to higher view change" + ); + state.new_view = view_change.new_view; + state.view_changes.clear(); + state.started_at = chrono::Utc::now().timestamp_millis(); + } else { + // Lower view - ignore stale view change + warn!( + received_view = view_change.new_view, + current_view = state.new_view, + "Ignoring stale view change for lower view" + ); + return Ok(None); + } + } + + state + .view_changes + .insert(view_change.validator.clone(), view_change); + + // Check if we have quorum and are the new leader + let quorum = self.quorum_size(); + let new_view = state.new_view; + + let view_change_voters: Vec = state.view_changes.keys().cloned().collect(); + let meets_weight_threshold = self + .stake_voting + .meets_threshold(&view_change_voters, 2.0 / 3.0); + if state.view_changes.len() >= quorum + && meets_weight_threshold + && self + .leader_election + .is_leader(&self.keypair.hotkey(), new_view) + { + info!(new_view, "View change quorum reached, becoming new leader"); + + // Create new view message + #[derive(Serialize)] + struct SigningData { + view: ViewNumber, + } + + let signing_bytes = bincode::serialize(&SigningData { view: new_view }) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let new_view_msg = NewViewMessage { + view: new_view, + view_changes: state.view_changes.values().cloned().collect(), + leader: self.keypair.hotkey(), + signature, + }; + + // Update view + drop(state_guard); + *self.current_view.write() = new_view; + *self.view_change_state.write() = None; + *self.current_round.write() = None; + + return Ok(Some(new_view_msg)); + } + + Ok(None) + } + + /// Handle new view message (from new leader) + pub fn handle_new_view(&self, new_view: NewViewMessage) -> Result<(), ConsensusError> { + // Verify sender is the leader for this view + if !self + .leader_election + .is_leader(&new_view.leader, new_view.view) + { + return Err(ConsensusError::InvalidProposal(format!( + "{:?} is not the leader for view {}", + new_view.leader, new_view.view + ))); + } + + // Verify cryptographic signature on the new view message + #[derive(Serialize)] + struct NewViewSigningData { + view: ViewNumber, + } + + let signing_data = NewViewSigningData { + view: new_view.view, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let is_valid_sig = self + .validator_set + .verify_signature(&new_view.leader, &signing_bytes, &new_view.signature) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature(new_view.leader.to_hex())); + } + + // Verify quorum of view changes + let quorum = self.quorum_size(); + if new_view.view_changes.len() < quorum { + return Err(ConsensusError::NotEnoughVotes { + needed: quorum, + have: new_view.view_changes.len(), + }); + } + + let view_change_voters: Vec = new_view + .view_changes + .iter() + .map(|vc| vc.validator.clone()) + .collect(); + if !self + .stake_voting + .meets_threshold(&view_change_voters, 2.0 / 3.0) + { + return Err(ConsensusError::InvalidProposal( + "View change lacks weighted quorum".to_string(), + )); + } + + // Verify each ViewChangeMessage signature AND that they're all for the announced view + for vc in &new_view.view_changes { + // CRITICAL: Verify ViewChange is for the announced view + if vc.new_view != new_view.view { + return Err(ConsensusError::ViewMismatch { + expected: new_view.view, + actual: vc.new_view, + }); + } + + #[derive(Serialize)] + struct ViewChangeSigningData { + new_view: ViewNumber, + last_prepared_sequence: Option, + } + + let signing_data = ViewChangeSigningData { + new_view: vc.new_view, + last_prepared_sequence: vc.last_prepared_sequence, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| ConsensusError::InvalidProposal(e.to_string()))?; + + let is_valid_sig = self + .validator_set + .verify_signature(&vc.validator, &signing_bytes, &vc.signature) + .map_err(|e| ConsensusError::InvalidSignature(e.to_string()))?; + + if !is_valid_sig { + return Err(ConsensusError::InvalidSignature(vc.validator.to_hex())); + } + } + + // Transition to new view + info!(view = new_view.view, "Transitioning to new view"); + *self.current_view.write() = new_view.view; + *self.view_change_state.write() = None; + *self.current_round.write() = None; + + Ok(()) + } + + /// Check for round timeout + pub fn check_timeout(&self) -> bool { + let now = chrono::Utc::now().timestamp_millis(); + + if let Some(round) = self.current_round.read().as_ref() { + if now - round.started_at > self.round_timeout_ms { + warn!( + view = round.view, + sequence = round.sequence, + "Consensus round timed out" + ); + return true; + } + } + + false + } + + /// Check if view change state has timed out + pub fn check_view_change_timeout(&self) -> bool { + let now = chrono::Utc::now().timestamp_millis(); + + if let Some(view_change_state) = self.view_change_state.read().as_ref() { + if now - view_change_state.started_at > self.view_change_timeout_ms { + warn!( + new_view = view_change_state.new_view, + "View change operation timed out" + ); + return true; + } + } + + false + } + + /// Determine if we should initiate a view change based on timeouts + /// + /// Returns true if: + /// - The current consensus round has timed out, or + /// - There is an ongoing view change that has timed out + pub fn should_initiate_view_change(&self) -> bool { + // Check if current round has timed out + if self.check_timeout() { + return true; + } + + // Check if there's a stalled view change operation + if self.check_view_change_timeout() { + return true; + } + + false + } + + /// Get a past decision + pub fn get_decision(&self, sequence: SequenceNumber) -> Option { + self.decisions.read().get(&sequence).cloned() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test_engine() -> ConsensusEngine { + let keypair = Keypair::generate(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let state_manager = Arc::new(StateManager::for_netuid(100)); + + // Register ourselves as a validator + let record = crate::validator::ValidatorRecord::new(keypair.hotkey(), 10_000); + validator_set.register_validator(record).unwrap(); + + ConsensusEngine::new(keypair, validator_set, state_manager) + } + + #[test] + fn test_engine_creation() { + let engine = create_test_engine(); + assert_eq!(engine.current_view(), 0); + assert_eq!(engine.next_sequence(), 1); + } + + #[test] + fn test_create_proposal_as_leader() { + let engine = create_test_engine(); + + // With only one validator, we're always the leader + let proposal = engine + .create_proposal(StateChangeType::ChallengeSubmission, vec![1, 2, 3]) + .unwrap(); + + assert_eq!(proposal.view, 0); + assert_eq!(proposal.sequence, 1); + assert_eq!( + proposal.proposal.change_type, + StateChangeType::ChallengeSubmission + ); + } + + #[test] + fn test_consensus_phases() { + let phase = ConsensusPhase::Idle; + assert!(phase < ConsensusPhase::Committed); + } + + #[test] + fn test_quorum_calculation() { + let keypair = Keypair::generate(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let state_manager = Arc::new(StateManager::for_netuid(100)); + + // Add 4 validators (n=4, f=1, quorum=3) + for i in 0..4 { + let mut bytes = [0u8; 32]; + bytes[0] = i; + let record = crate::validator::ValidatorRecord::new(Hotkey(bytes), 10_000); + validator_set.register_validator(record).unwrap(); + } + + let engine = ConsensusEngine::new(keypair, validator_set, state_manager); + assert_eq!(engine.quorum_size(), 3); + } + + #[test] + fn test_view_change_initiation() { + let engine = create_test_engine(); + + let view_change = engine.initiate_view_change(1).unwrap(); + assert_eq!(view_change.new_view, 1); + assert_eq!(view_change.validator, engine.keypair.hotkey()); + } +} diff --git a/crates/p2p-consensus/src/lib.rs b/crates/p2p-consensus/src/lib.rs new file mode 100644 index 000000000..069d3ca33 --- /dev/null +++ b/crates/p2p-consensus/src/lib.rs @@ -0,0 +1,97 @@ +//! Decentralized P2P consensus for Platform Network validators +//! +//! This crate provides the networking and consensus layer for validators +//! to communicate directly without a centralized server. +//! +//! # Architecture +//! +//! - **Network Layer** (`network.rs`): libp2p-based P2P networking with gossipsub +//! for message broadcasting and Kademlia DHT for peer discovery. +//! +//! - **Consensus** (`consensus.rs`): PBFT-style Byzantine fault tolerant consensus +//! with view changes and leader election. +//! +//! - **State Management** (`state.rs`): Decentralized state synchronization with +//! merkle proofs for verification. +//! +//! - **Validator Management** (`validator.rs`): Tracks active validators, their +//! stakes, and handles leader election. +//! +//! # Usage +//! +//! ```text +//! use platform_p2p_consensus::{P2PConfig, P2PNetwork, ConsensusEngine, StateManager}; +//! +//! // Create configuration +//! let config = P2PConfig::production(); +//! +//! // Create network and consensus +//! let network = P2PNetwork::new(keypair, config, validator_set, event_tx)?; +//! let consensus = ConsensusEngine::new(keypair, validator_set, state_manager); +//! ``` +//! +//! # SudoOwner +//! +//! The network sudo key is hardcoded to: `5GziQCcRpN8NCJktX343brnfuVe3w6gUYieeStXPD1Dag2At` + +pub mod config; +pub mod consensus; +pub mod messages; +pub mod network; +pub mod state; +pub mod validator; + +// Re-export main types +pub use config::{P2PConfig, DEFAULT_BOOTSTRAP_NODES}; +pub use consensus::{ConsensusDecision, ConsensusEngine, ConsensusError, ConsensusPhase}; +pub use messages::{ + ChallengeUpdateMessage, CommitMessage, ConsensusProposal, DataRequestMessage, + DataResponseMessage, EvaluationMessage, EvaluationMetrics, HeartbeatMessage, + JobAssignmentMessage, JobClaimMessage, LeaderboardRequestMessage, LeaderboardResponseMessage, + MerkleNode, MerkleProof, NewViewMessage, P2PMessage, PeerAnnounceMessage, PrePrepare, + PrepareMessage, PreparedProof, ProposalContent, RoundId, SequenceNumber, SignedP2PMessage, + StateChangeType, StateRequest, StateResponse, StorageProposalMessage, StorageVoteMessage, + SubmissionMessage, SudoActionMessage, TaskProgressMessage, TaskResultMessage, + ViewChangeMessage, ViewNumber, WeightVoteMessage, +}; +pub use network::{ + NetworkBehaviour, NetworkError, NetworkEvent, NetworkRunner, P2PCommand, P2PEvent, P2PNetwork, + PeerMapping, +}; +pub use state::{ + build_merkle_proof, compute_merkle_root, verify_merkle_proof, ChainState, ChallengeConfig, + EvaluationRecord, JobRecord, JobStatus, LeaderboardEntry, StateError, StateManager, + TaskProgressRecord, ValidatorEvaluation, WeightVotes, +}; +pub use validator::{ + LeaderElection, StakeWeightedVoting, ValidatorError, ValidatorRecord, ValidatorSet, +}; + +/// Protocol version string +pub const PROTOCOL_VERSION: &str = "1.0.0"; + +/// Hardcoded SudoOwner hotkey (SS58 format) +/// This is the only key allowed to perform sudo operations +pub const SUDO_HOTKEY: &str = "5GziQCcRpN8NCJktX343brnfuVe3w6gUYieeStXPD1Dag2At"; + +/// Default consensus topic +pub const CONSENSUS_TOPIC: &str = "platform/consensus/1.0.0"; + +/// Default challenge topic +pub const CHALLENGE_TOPIC: &str = "platform/challenge/1.0.0"; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_protocol_version() { + assert_eq!(PROTOCOL_VERSION, "1.0.0"); + } + + #[test] + fn test_default_topics() { + assert!(CONSENSUS_TOPIC.contains("consensus")); + assert!(CHALLENGE_TOPIC.contains("challenge")); + } +} diff --git a/crates/p2p-consensus/src/messages.rs b/crates/p2p-consensus/src/messages.rs new file mode 100644 index 000000000..b51017882 --- /dev/null +++ b/crates/p2p-consensus/src/messages.rs @@ -0,0 +1,867 @@ +//! P2P message types for consensus and state synchronization +//! +//! Defines all message types used for inter-validator communication +//! over the libp2p gossipsub network. + +use bincode::Options; +use platform_core::{ChallengeId, Hotkey}; +use serde::{Deserialize, Serialize}; + +pub const MAX_P2P_MESSAGE_SIZE: u64 = 16 * 1024 * 1024; + +/// Unique identifier for a consensus round +pub type RoundId = u64; + +/// View number for PBFT consensus +pub type ViewNumber = u64; + +/// Sequence number for ordering +pub type SequenceNumber = u64; + +/// All P2P message types +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum P2PMessage { + // Consensus messages + Proposal(ConsensusProposal), + PrePrepare(PrePrepare), + Prepare(PrepareMessage), + Commit(CommitMessage), + ViewChange(ViewChangeMessage), + NewView(NewViewMessage), + + // State sync + StateRequest(StateRequest), + StateResponse(StateResponse), + + // Challenge evaluation + Submission(SubmissionMessage), + Evaluation(EvaluationMessage), + WeightVote(WeightVoteMessage), + + // Network maintenance + Heartbeat(HeartbeatMessage), + PeerAnnounce(PeerAnnounceMessage), + + // Challenge lifecycle + JobClaim(JobClaimMessage), + JobAssignment(JobAssignmentMessage), + DataRequest(DataRequestMessage), + DataResponse(DataResponseMessage), + TaskProgress(TaskProgressMessage), + TaskResult(TaskResultMessage), + LeaderboardRequest(LeaderboardRequestMessage), + LeaderboardResponse(LeaderboardResponseMessage), + ChallengeUpdate(ChallengeUpdateMessage), + StorageProposal(StorageProposalMessage), + StorageVote(StorageVoteMessage), + + // Review assignment + ReviewAssignment(ReviewAssignmentMessage), + ReviewDecline(ReviewDeclineMessage), + ReviewResult(ReviewResultMessage), + + /// Agent log proposal for consensus + AgentLogProposal(AgentLogProposalMessage), + + /// Sudo action from subnet owner + SudoAction(SudoActionMessage), +} + +impl P2PMessage { + /// Serialize message to bytes + pub fn to_bytes(&self) -> Result, bincode::Error> { + bincode::serialize(self) + } + + /// Deserialize message from bytes + pub fn from_bytes(bytes: &[u8]) -> Result { + if bytes.len() as u64 > MAX_P2P_MESSAGE_SIZE { + return Err(Box::new(bincode::ErrorKind::Custom(format!( + "message exceeds maximum size: {} > {}", + bytes.len(), + MAX_P2P_MESSAGE_SIZE + )))); + } + bincode::DefaultOptions::new() + .with_limit(MAX_P2P_MESSAGE_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(bytes) + } + + /// Get the message type name for logging + pub fn type_name(&self) -> &'static str { + match self { + P2PMessage::Proposal(_) => "Proposal", + P2PMessage::PrePrepare(_) => "PrePrepare", + P2PMessage::Prepare(_) => "Prepare", + P2PMessage::Commit(_) => "Commit", + P2PMessage::ViewChange(_) => "ViewChange", + P2PMessage::NewView(_) => "NewView", + P2PMessage::StateRequest(_) => "StateRequest", + P2PMessage::StateResponse(_) => "StateResponse", + P2PMessage::Submission(_) => "Submission", + P2PMessage::Evaluation(_) => "Evaluation", + P2PMessage::WeightVote(_) => "WeightVote", + P2PMessage::Heartbeat(_) => "Heartbeat", + P2PMessage::PeerAnnounce(_) => "PeerAnnounce", + P2PMessage::JobClaim(_) => "JobClaim", + P2PMessage::JobAssignment(_) => "JobAssignment", + P2PMessage::DataRequest(_) => "DataRequest", + P2PMessage::DataResponse(_) => "DataResponse", + P2PMessage::TaskProgress(_) => "TaskProgress", + P2PMessage::TaskResult(_) => "TaskResult", + P2PMessage::LeaderboardRequest(_) => "LeaderboardRequest", + P2PMessage::LeaderboardResponse(_) => "LeaderboardResponse", + P2PMessage::ChallengeUpdate(_) => "ChallengeUpdate", + P2PMessage::StorageProposal(_) => "StorageProposal", + P2PMessage::StorageVote(_) => "StorageVote", + P2PMessage::ReviewAssignment(_) => "ReviewAssignment", + P2PMessage::ReviewDecline(_) => "ReviewDecline", + P2PMessage::ReviewResult(_) => "ReviewResult", + P2PMessage::AgentLogProposal(_) => "AgentLogProposal", + P2PMessage::SudoAction(_) => "SudoAction", + } + } +} + +// ============================================================================ +// Consensus Messages (PBFT-style) +// ============================================================================ + +/// Proposal from the leader to initiate consensus +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ConsensusProposal { + /// View number (leader term) + pub view: ViewNumber, + /// Sequence number for this proposal + pub sequence: SequenceNumber, + /// The proposed state transition + pub proposal: ProposalContent, + /// Proposer's hotkey + pub proposer: Hotkey, + /// Proposer's signature over (view, sequence, proposal_hash) + pub signature: Vec, + /// Timestamp when proposal was created + pub timestamp: i64, +} + +/// Content of a consensus proposal +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ProposalContent { + /// Type of state change + pub change_type: StateChangeType, + /// Serialized change data + pub data: Vec, + /// Hash of the change for verification + pub data_hash: [u8; 32], +} + +/// Types of state changes that can be proposed +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum StateChangeType { + /// New challenge submission + ChallengeSubmission, + /// Evaluation result + EvaluationResult, + /// Weight update + WeightUpdate, + /// Validator set change + ValidatorChange, + /// Configuration update (sudo only) + ConfigUpdate, + /// Epoch transition + EpochTransition, +} + +/// Pre-prepare message (leader broadcasts after receiving proposal) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PrePrepare { + /// View number + pub view: ViewNumber, + /// Sequence number + pub sequence: SequenceNumber, + /// Hash of the proposal + pub proposal_hash: [u8; 32], + /// Leader's hotkey + pub leader: Hotkey, + /// Leader's signature + pub signature: Vec, +} + +/// Prepare message (validators acknowledge pre-prepare) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PrepareMessage { + /// View number + pub view: ViewNumber, + /// Sequence number + pub sequence: SequenceNumber, + /// Hash of the proposal + pub proposal_hash: [u8; 32], + /// Validator's hotkey + pub validator: Hotkey, + /// Validator's signature + pub signature: Vec, +} + +/// Commit message (validators commit to the proposal) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CommitMessage { + /// View number + pub view: ViewNumber, + /// Sequence number + pub sequence: SequenceNumber, + /// Hash of the proposal + pub proposal_hash: [u8; 32], + /// Validator's hotkey + pub validator: Hotkey, + /// Validator's signature + pub signature: Vec, +} + +/// View change message (request new leader) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ViewChangeMessage { + /// New view number being proposed + pub new_view: ViewNumber, + /// Last prepared sequence number + pub last_prepared_sequence: Option, + /// Proof of last prepared (signatures) + pub prepared_proof: Option, + /// Validator requesting change + pub validator: Hotkey, + /// Validator's signature + pub signature: Vec, +} + +/// Proof that a proposal was prepared +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PreparedProof { + /// The pre-prepare message + pub pre_prepare: PrePrepare, + /// Prepare messages (2f+1 required) + pub prepares: Vec, +} + +/// New view message (new leader announces) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct NewViewMessage { + /// The new view number + pub view: ViewNumber, + /// View change messages collected (2f+1) + pub view_changes: Vec, + /// New leader's hotkey + pub leader: Hotkey, + /// New leader's signature + pub signature: Vec, +} + +// ============================================================================ +// State Sync Messages +// ============================================================================ + +/// Request for state synchronization +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateRequest { + /// Requesting validator + pub requester: Hotkey, + /// Current state hash of requester + pub current_hash: [u8; 32], + /// Current sequence number + pub current_sequence: SequenceNumber, + /// Request timestamp + pub timestamp: i64, +} + +/// Response with state data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StateResponse { + /// Responding validator + pub responder: Hotkey, + /// State hash being sent + pub state_hash: [u8; 32], + /// Sequence number of this state + pub sequence: SequenceNumber, + /// Serialized state data + pub state_data: Vec, + /// Merkle proof for verification + pub merkle_proof: Option, + /// Responder's signature + pub signature: Vec, +} + +/// Merkle proof for state verification +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MerkleProof { + /// Root hash + pub root: [u8; 32], + /// Path from leaf to root + pub path: Vec, +} + +/// Node in merkle proof path +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MerkleNode { + /// Hash of sibling + pub sibling_hash: [u8; 32], + /// Whether sibling is on the left + pub is_left: bool, +} + +// ============================================================================ +// Challenge Messages +// ============================================================================ + +/// Submission of agent code for evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SubmissionMessage { + /// Unique submission ID + pub submission_id: String, + /// Challenge being submitted to + pub challenge_id: ChallengeId, + /// Miner's hotkey + pub miner: Hotkey, + /// Hash of the agent code + pub agent_hash: String, + /// Signature from miner + pub signature: Vec, + /// Submission timestamp + pub timestamp: i64, +} + +/// Evaluation result from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationMessage { + /// Submission being evaluated + pub submission_id: String, + /// Challenge ID + pub challenge_id: ChallengeId, + /// Evaluating validator + pub validator: Hotkey, + /// Evaluation score (0.0 to 1.0) + pub score: f64, + /// Evaluation metrics + pub metrics: EvaluationMetrics, + /// Validator's signature + pub signature: Vec, + /// Evaluation timestamp + pub timestamp: i64, +} + +/// Detailed evaluation metrics +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationMetrics { + /// Primary score + pub primary_score: f64, + /// Secondary metrics (challenge-specific) + pub secondary_metrics: Vec<(String, f64)>, + /// Execution time in milliseconds + pub execution_time_ms: u64, + /// Memory usage in bytes + pub memory_usage_bytes: Option, + /// Whether evaluation timed out + pub timed_out: bool, + /// Error message if failed + pub error: Option, +} + +/// Weight vote for epoch finalization +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightVoteMessage { + /// Epoch number + pub epoch: u64, + /// Netuid + pub netuid: u16, + /// Validator casting the vote + pub validator: Hotkey, + /// Weight vector (uid -> weight) + pub weights: Vec<(u16, u16)>, + /// Hash of the weight vector + pub weights_hash: [u8; 32], + /// Validator's signature + pub signature: Vec, + /// Vote timestamp + pub timestamp: i64, +} + +// ============================================================================ +// Network Maintenance Messages +// ============================================================================ + +/// Heartbeat message to maintain presence +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HeartbeatMessage { + /// Validator's hotkey + pub validator: Hotkey, + /// Current state hash + pub state_hash: [u8; 32], + /// Current sequence number + pub sequence: SequenceNumber, + /// Validator's stake (self-reported, verify against chain) + pub stake: u64, + /// Timestamp + pub timestamp: i64, + /// Signature + pub signature: Vec, +} + +/// Peer announcement for discovery +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PeerAnnounceMessage { + /// Validator's hotkey + pub validator: Hotkey, + /// Multiaddresses where this peer can be reached + pub addresses: Vec, + /// Peer ID (libp2p) + pub peer_id: String, + /// Protocol version + pub protocol_version: String, + /// Timestamp + pub timestamp: i64, + /// Signature + pub signature: Vec, +} + +// ============================================================================ +// Challenge Lifecycle Messages +// ============================================================================ + +/// Job claim from a validator for challenge evaluation work +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobClaimMessage { + /// Validator claiming the job + pub validator: Hotkey, + /// Challenge to claim work for + pub challenge_id: ChallengeId, + /// Maximum number of jobs the validator can handle + pub max_jobs: u32, + /// Claim timestamp + pub timestamp: i64, + /// Validator's signature + pub signature: Vec, +} + +/// Assignment of a submission evaluation job to a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobAssignmentMessage { + /// Submission being assigned + pub submission_id: String, + /// Challenge the submission belongs to + pub challenge_id: ChallengeId, + /// Validator assigned to evaluate + pub assigned_validator: Hotkey, + /// Validator that made the assignment + pub assigner: Hotkey, + /// Hash of the agent code to evaluate + pub agent_hash: String, + /// Assignment timestamp + pub timestamp: i64, + /// Assigner's signature + pub signature: Vec, +} + +/// Request for challenge-related data from peers +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct DataRequestMessage { + /// Unique request identifier + pub request_id: String, + /// Validator making the request + pub requester: Hotkey, + /// Challenge the data belongs to + pub challenge_id: ChallengeId, + /// Type of data being requested + pub data_type: String, + /// Key identifying the specific data + pub data_key: String, + /// Request timestamp + pub timestamp: i64, + /// Requester's signature + pub signature: Vec, +} + +/// Response containing requested challenge data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct DataResponseMessage { + /// Request identifier this responds to + pub request_id: String, + /// Validator providing the data + pub responder: Hotkey, + /// Challenge the data belongs to + pub challenge_id: ChallengeId, + /// Type of data being returned + pub data_type: String, + /// Serialized data payload + pub data: Vec, + /// Response timestamp + pub timestamp: i64, + /// Responder's signature + pub signature: Vec, +} + +/// Progress update for a task within a submission evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskProgressMessage { + /// Submission being evaluated + pub submission_id: String, + /// Challenge the submission belongs to + pub challenge_id: ChallengeId, + /// Validator performing the evaluation + pub validator: Hotkey, + /// Index of the current task + pub task_index: u32, + /// Total number of tasks + pub total_tasks: u32, + /// Current status description + pub status: String, + /// Progress percentage (0.0 to 100.0) + pub progress_pct: f64, + /// Progress timestamp + pub timestamp: i64, + /// Validator's signature + pub signature: Vec, +} + +/// Result of a single task within a submission evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskResultMessage { + /// Submission being evaluated + pub submission_id: String, + /// Challenge the submission belongs to + pub challenge_id: ChallengeId, + /// Validator that performed the evaluation + pub validator: Hotkey, + /// Unique task identifier + pub task_id: String, + /// Whether the task passed + pub passed: bool, + /// Task score + pub score: f64, + /// Serialized task output + pub output: Vec, + /// Execution time in milliseconds + pub execution_time_ms: u64, + /// Result timestamp + pub timestamp: i64, + /// Validator's signature + pub signature: Vec, +} + +/// Request for challenge leaderboard data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LeaderboardRequestMessage { + /// Validator making the request + pub requester: Hotkey, + /// Challenge to get leaderboard for + pub challenge_id: ChallengeId, + /// Maximum number of entries to return + pub limit: u32, + /// Offset for pagination + pub offset: u32, + /// Request timestamp + pub timestamp: i64, + /// Requester's signature + pub signature: Vec, +} + +/// Response containing leaderboard data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LeaderboardResponseMessage { + /// Validator providing the data + pub responder: Hotkey, + /// Challenge the leaderboard belongs to + pub challenge_id: ChallengeId, + /// Serialized leaderboard entries + pub entries: Vec, + /// Total number of entries in the leaderboard + pub total_count: u32, + /// Response timestamp + pub timestamp: i64, + /// Responder's signature + pub signature: Vec, +} + +/// Update notification for a challenge +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeUpdateMessage { + /// Challenge being updated + pub challenge_id: ChallengeId, + /// Validator publishing the update + pub updater: Hotkey, + /// Type of update + pub update_type: String, + /// Serialized update data + pub data: Vec, + /// Update timestamp + pub timestamp: i64, + /// Updater's signature + pub signature: Vec, +} + +/// Proposal to store a key-value pair in consensus storage +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageProposalMessage { + /// Unique proposal identifier + pub proposal_id: [u8; 32], + /// Challenge the storage belongs to + pub challenge_id: ChallengeId, + /// Validator proposing the storage + pub proposer: Hotkey, + /// Storage key + pub key: Vec, + /// Storage value + pub value: Vec, + /// Proposal timestamp + pub timestamp: i64, + /// Proposer's signature + pub signature: Vec, +} + +/// Vote on a storage proposal +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageVoteMessage { + /// Proposal being voted on + pub proposal_id: [u8; 32], + /// Validator casting the vote + pub voter: Hotkey, + /// Whether the voter approves + pub approve: bool, + /// Vote timestamp + pub timestamp: i64, + /// Voter's signature + pub signature: Vec, +} + +// ============================================================================ +// Review Assignment Messages +// ============================================================================ + +/// Type of review to be performed +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum ReviewType { + /// LLM-based code review + Llm, + /// AST-based structural review + Ast, +} + +/// Assignment of review validators for a submission +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReviewAssignmentMessage { + /// Submission being reviewed + pub submission_id: String, + /// Type of review + pub review_type: ReviewType, + /// Validators assigned to perform the review + pub assigned_validators: Vec, + /// Deterministic seed used for selection + pub seed: [u8; 32], + /// Assignment timestamp + pub timestamp: i64, + /// Validator that made the assignment + pub assigner: Hotkey, + /// Assigner's signature + pub signature: Vec, +} + +/// Decline message when a validator cannot perform a review +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReviewDeclineMessage { + /// Submission being reviewed + pub submission_id: String, + /// Validator declining the review + pub validator: Hotkey, + /// Reason for declining + pub reason: String, + /// Decline timestamp + pub timestamp: i64, + /// Validator's signature + pub signature: Vec, +} + +/// Result of a review from a validator +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReviewResultMessage { + /// Submission being reviewed + pub submission_id: String, + /// Validator that performed the review + pub validator: Hotkey, + /// Type of review performed + pub review_type: ReviewType, + /// Review score (0.0 to 1.0) + pub score: f64, + /// Detailed review output + pub details: String, + /// Result timestamp + pub timestamp: i64, + /// Validator's signature + pub signature: Vec, +} + +// ============================================================================ +// Agent Log Messages +// ============================================================================ + +/// Sudo action message from the subnet owner +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SudoActionMessage { + /// The sudo action to perform + pub action: platform_core::SudoAction, + /// Signer's hotkey (must match sudo key) + pub signer: Hotkey, + /// Signature over the serialized action + pub signature: Vec, + /// Timestamp + pub timestamp: i64, +} + +/// Agent log proposal message for P2P consensus +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentLogProposalMessage { + /// Submission ID this log belongs to + pub submission_id: String, + /// Challenge ID + pub challenge_id: String, + /// Miner hotkey + pub miner_hotkey: String, + /// SHA256 hash of the logs data + pub logs_hash: [u8; 32], + /// Serialized agent logs (max 256KB) + pub logs_data: Vec, + /// Validator proposing these logs + pub validator_hotkey: Hotkey, + /// Epoch when evaluation occurred + pub epoch: u64, + /// Timestamp + pub timestamp: i64, +} + +// ============================================================================ +// Signed Message Wrapper +// ============================================================================ + +/// Wrapper for signed P2P messages with validation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SignedP2PMessage { + /// The inner message + pub message: P2PMessage, + /// Signer's hotkey + pub signer: Hotkey, + /// Signature over the serialized message + pub signature: Vec, + /// Message nonce for replay protection + pub nonce: u64, +} + +impl SignedP2PMessage { + /// Get the bytes that were signed + pub fn signing_bytes(&self) -> Result, bincode::Error> { + #[derive(Serialize)] + struct SigningData<'a> { + message: &'a P2PMessage, + nonce: u64, + } + bincode::serialize(&SigningData { + message: &self.message, + nonce: self.nonce, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_p2p_message_serialization() { + let msg = P2PMessage::Heartbeat(HeartbeatMessage { + validator: Hotkey([1u8; 32]), + state_hash: [2u8; 32], + sequence: 100, + stake: 1_000_000_000_000, + timestamp: 1234567890, + signature: vec![0u8; 64], + }); + + let bytes = msg.to_bytes().expect("serialization should work"); + let recovered = P2PMessage::from_bytes(&bytes).expect("deserialization should work"); + + assert_eq!(msg.type_name(), recovered.type_name()); + } + + #[test] + fn test_message_type_names() { + let proposal = P2PMessage::Proposal(ConsensusProposal { + view: 1, + sequence: 1, + proposal: ProposalContent { + change_type: StateChangeType::ChallengeSubmission, + data: vec![], + data_hash: [0u8; 32], + }, + proposer: Hotkey([0u8; 32]), + signature: vec![], + timestamp: 0, + }); + assert_eq!(proposal.type_name(), "Proposal"); + + let heartbeat = P2PMessage::Heartbeat(HeartbeatMessage { + validator: Hotkey([0u8; 32]), + state_hash: [0u8; 32], + sequence: 0, + stake: 0, + timestamp: 0, + signature: vec![], + }); + assert_eq!(heartbeat.type_name(), "Heartbeat"); + } + + #[test] + fn test_state_change_types() { + assert_eq!( + StateChangeType::ChallengeSubmission, + StateChangeType::ChallengeSubmission + ); + assert_ne!( + StateChangeType::ChallengeSubmission, + StateChangeType::WeightUpdate + ); + } + + #[test] + fn test_evaluation_metrics() { + let metrics = EvaluationMetrics { + primary_score: 0.95, + secondary_metrics: vec![("accuracy".to_string(), 0.98)], + execution_time_ms: 5000, + memory_usage_bytes: Some(1024 * 1024), + timed_out: false, + error: None, + }; + + let serialized = bincode::serialize(&metrics).expect("serialize"); + let deserialized: EvaluationMetrics = + bincode::deserialize(&serialized).expect("deserialize"); + + assert!((deserialized.primary_score - 0.95).abs() < f64::EPSILON); + } + + #[test] + fn test_signed_message_signing_bytes() { + let msg = SignedP2PMessage { + message: P2PMessage::Heartbeat(HeartbeatMessage { + validator: Hotkey([1u8; 32]), + state_hash: [0u8; 32], + sequence: 1, + stake: 0, + timestamp: 0, + signature: vec![], + }), + signer: Hotkey([1u8; 32]), + signature: vec![], + nonce: 42, + }; + + let bytes = msg.signing_bytes().expect("should get signing bytes"); + assert!(!bytes.is_empty()); + } +} diff --git a/crates/p2p-consensus/src/network.rs b/crates/p2p-consensus/src/network.rs new file mode 100644 index 000000000..ce06841d3 --- /dev/null +++ b/crates/p2p-consensus/src/network.rs @@ -0,0 +1,1433 @@ +//! P2P network layer using libp2p +//! +//! Implements gossipsub for message broadcasting and Kademlia DHT for peer discovery. +//! Provides the networking foundation for PBFT consensus. + +use crate::config::P2PConfig; +use crate::messages::{P2PMessage, SignedP2PMessage, WeightVoteMessage, MAX_P2P_MESSAGE_SIZE}; +use crate::validator::ValidatorSet; +use bincode::Options; +use libp2p::{ + gossipsub::{self, IdentTopic, MessageAuthenticity, MessageId, ValidationMode}, + identify, + kad::{self, store::MemoryStore}, + noise, tcp, yamux, Multiaddr, PeerId, Swarm, SwarmBuilder, +}; +use parking_lot::RwLock; +use platform_core::{hash_data, Hotkey, Keypair}; +use std::collections::{HashMap, VecDeque}; +use std::sync::Arc; +use std::time::Duration; +use thiserror::Error; +use tokio::sync::mpsc; +use tracing::{debug, error, info, warn}; + +/// Network errors +#[derive(Error, Debug)] +pub enum NetworkError { + #[error("Transport error: {0}")] + Transport(String), + #[error("Gossipsub error: {0}")] + Gossipsub(String), + #[error("DHT error: {0}")] + Dht(String), + #[error("Serialization error: {0}")] + Serialization(String), + #[error("Not connected to any peers")] + NoPeers, + #[error("Channel error: {0}")] + Channel(String), + #[error("Replay attack detected: nonce {nonce} already seen for {signer}")] + ReplayAttack { signer: String, nonce: u64 }, + #[error("Rate limit exceeded for {signer}: {count} messages in current window")] + RateLimitExceeded { signer: String, count: u32 }, +} + +/// Combined network behavior using manual composition +pub struct NetworkBehaviour { + /// Gossipsub for pub/sub messaging + pub gossipsub: gossipsub::Behaviour, + /// Kademlia DHT for peer discovery + pub kademlia: kad::Behaviour, + /// Identify protocol for peer identification + pub identify: identify::Behaviour, +} + +/// Events from the network layer +#[derive(Debug)] +pub enum NetworkEvent { + /// Received a P2P message + Message { source: PeerId, message: P2PMessage }, + /// New peer connected + PeerConnected(PeerId), + /// Peer disconnected + PeerDisconnected(PeerId), + /// Peer identified with hotkey + PeerIdentified { + peer_id: PeerId, + hotkey: Option, + addresses: Vec, + }, +} + +/// Commands for controlling the P2P network +#[derive(Debug, Clone)] +pub enum P2PCommand { + /// Broadcast message to all peers + Broadcast(P2PMessage), + /// Dial a specific peer by multiaddr + Dial(String), + /// Disconnect from peer by peer ID string + Disconnect(String), + /// Shutdown the network + Shutdown, +} + +/// Events emitted from the P2P network +#[derive(Debug, Clone)] +#[allow(clippy::large_enum_variant)] +pub enum P2PEvent { + /// Message received from a peer + Message { from: PeerId, message: P2PMessage }, + /// A peer has connected + PeerConnected(PeerId), + /// A peer has disconnected + PeerDisconnected(PeerId), +} + +/// Mapping between peer IDs and validator hotkeys +pub struct PeerMapping { + /// PeerId -> Hotkey + peer_to_hotkey: RwLock>, + /// Hotkey -> PeerId + hotkey_to_peer: RwLock>, +} + +impl PeerMapping { + pub fn new() -> Self { + Self { + peer_to_hotkey: RwLock::new(HashMap::new()), + hotkey_to_peer: RwLock::new(HashMap::new()), + } + } + + pub fn insert(&self, peer_id: PeerId, hotkey: Hotkey) { + self.peer_to_hotkey.write().insert(peer_id, hotkey.clone()); + self.hotkey_to_peer.write().insert(hotkey, peer_id); + } + + pub fn get_hotkey(&self, peer_id: &PeerId) -> Option { + self.peer_to_hotkey.read().get(peer_id).cloned() + } + + pub fn get_peer(&self, hotkey: &Hotkey) -> Option { + self.hotkey_to_peer.read().get(hotkey).copied() + } + + pub fn remove_peer(&self, peer_id: &PeerId) { + if let Some(hotkey) = self.peer_to_hotkey.write().remove(peer_id) { + self.hotkey_to_peer.write().remove(&hotkey); + } + } + + /// Get the number of mapped peers (peers that have been identified with a hotkey) + pub fn len(&self) -> usize { + self.peer_to_hotkey.read().len() + } + + /// Check if there are no mapped peers + pub fn is_empty(&self) -> bool { + self.peer_to_hotkey.read().is_empty() + } +} + +impl Default for PeerMapping { + fn default() -> Self { + Self::new() + } +} + +/// Default rate limit: maximum messages per second per signer +const DEFAULT_RATE_LIMIT: u32 = 100; + +/// Rate limit sliding window in milliseconds (1 second) +const RATE_LIMIT_WINDOW_MS: i64 = 1000; + +/// Nonce expiry time in milliseconds (5 minutes) +const NONCE_EXPIRY_MS: i64 = 5 * 60 * 1000; + +/// P2P network node +pub struct P2PNetwork { + /// Local keypair + keypair: Keypair, + /// libp2p peer ID + local_peer_id: PeerId, + /// Network configuration + config: P2PConfig, + /// Gossipsub topics + consensus_topic: IdentTopic, + challenge_topic: IdentTopic, + /// Peer mapping + peer_mapping: Arc, + /// Reference to validator set + validator_set: Arc, + /// Event sender + #[allow(dead_code)] + event_tx: mpsc::Sender, + /// Message nonce counter + nonce: RwLock, + /// Seen nonces for replay protection with timestamps (hotkey -> (nonce -> timestamp_ms)) + /// Timestamps allow automatic expiry of old nonces + seen_nonces: RwLock>>, + /// Message timestamps for sliding window rate limiting (hotkey -> recent message timestamps in ms) + message_timestamps: RwLock>>, +} + +impl P2PNetwork { + /// Create a new P2P network + pub fn new( + keypair: Keypair, + config: P2PConfig, + validator_set: Arc, + event_tx: mpsc::Sender, + ) -> Result { + // Generate libp2p keypair from our keypair seed + let seed = keypair.seed(); + let libp2p_keypair = libp2p::identity::Keypair::ed25519_from_bytes(seed).map_err(|e| { + NetworkError::Transport(format!("Failed to create libp2p keypair: {}", e)) + })?; + let local_peer_id = PeerId::from(libp2p_keypair.public()); + + let consensus_topic = IdentTopic::new(&config.consensus_topic); + let challenge_topic = IdentTopic::new(&config.challenge_topic); + + Ok(Self { + keypair, + local_peer_id, + config, + consensus_topic, + challenge_topic, + peer_mapping: Arc::new(PeerMapping::new()), + validator_set, + event_tx, + nonce: RwLock::new(0), + seen_nonces: RwLock::new(HashMap::new()), + message_timestamps: RwLock::new(HashMap::new()), + }) + } + + /// Get local peer ID + pub fn local_peer_id(&self) -> PeerId { + self.local_peer_id + } + + /// Get local hotkey + pub fn local_hotkey(&self) -> Hotkey { + self.keypair.hotkey() + } + + /// Get peer mapping + pub fn peer_mapping(&self) -> Arc { + self.peer_mapping.clone() + } + + /// Get the count of connected peers that have been identified with a hotkey + /// + /// This returns the number of peers in the peer mapping, which includes + /// peers that have sent at least one verified message. + pub fn connected_peer_count(&self) -> usize { + self.peer_mapping.len() + } + + /// Check if we have the minimum required peers for consensus + /// + /// This is useful for determining if the network has enough participants + /// to achieve consensus on proposals. + pub fn has_min_peers(&self, min_required: usize) -> bool { + self.connected_peer_count() >= min_required + } + + /// Create gossipsub behaviour + fn create_gossipsub( + &self, + libp2p_keypair: &libp2p::identity::Keypair, + ) -> Result { + let gossipsub_config = gossipsub::ConfigBuilder::default() + .heartbeat_interval(Duration::from_secs(1)) + .validation_mode(ValidationMode::Strict) + .message_id_fn(|msg: &gossipsub::Message| { + use sha2::Digest; + let hash = sha2::Sha256::digest(&msg.data); + MessageId::from(hash.to_vec()) + }) + .max_transmit_size(self.config.max_message_size) + .build() + .map_err(|e| NetworkError::Gossipsub(e.to_string()))?; + + gossipsub::Behaviour::new( + MessageAuthenticity::Signed(libp2p_keypair.clone()), + gossipsub_config, + ) + .map_err(|e| NetworkError::Gossipsub(e.to_string())) + } + + /// Create behaviour components + pub fn create_behaviour( + &self, + libp2p_keypair: &libp2p::identity::Keypair, + ) -> Result { + let gossipsub = self.create_gossipsub(libp2p_keypair)?; + let store = MemoryStore::new(self.local_peer_id); + let kademlia = kad::Behaviour::new(self.local_peer_id, store); + let identify_config = + identify::Config::new("/platform/1.0.0".to_string(), libp2p_keypair.public()); + let identify = identify::Behaviour::new(identify_config); + + Ok(NetworkBehaviour { + gossipsub, + kademlia, + identify, + }) + } + + /// Subscribe to gossipsub topics + pub fn subscribe(&self, behaviour: &mut NetworkBehaviour) -> Result<(), NetworkError> { + behaviour + .gossipsub + .subscribe(&self.consensus_topic) + .map_err(|e| { + NetworkError::Gossipsub(format!("Failed to subscribe to consensus: {}", e)) + })?; + + behaviour + .gossipsub + .subscribe(&self.challenge_topic) + .map_err(|e| { + NetworkError::Gossipsub(format!("Failed to subscribe to challenge: {}", e)) + })?; + + info!( + consensus_topic = %self.config.consensus_topic, + challenge_topic = %self.config.challenge_topic, + "Subscribed to gossipsub topics" + ); + + Ok(()) + } + + /// Connect to bootstrap peers + pub async fn connect_bootstrap( + &self, + swarm: &mut Swarm, + behaviour: &mut NetworkBehaviour, + ) -> Result + where + TBehaviour: libp2p::swarm::NetworkBehaviour, + { + let mut connected = 0; + + for addr_str in &self.config.bootstrap_peers { + match addr_str.parse::() { + Ok(addr) => { + info!(addr = %addr, "Connecting to bootstrap peer"); + match swarm.dial(addr.clone()) { + Ok(_) => { + if let Some(peer_id) = extract_peer_id(&addr) { + behaviour.kademlia.add_address(&peer_id, addr); + connected += 1; + } + } + Err(e) => { + warn!(addr = %addr_str, error = %e, "Failed to dial bootstrap peer"); + } + } + } + Err(e) => { + warn!(addr = %addr_str, error = %e, "Invalid bootstrap address"); + } + } + } + + Ok(connected) + } + + /// Broadcast a message to the consensus topic + pub fn broadcast_consensus( + &self, + behaviour: &mut NetworkBehaviour, + message: P2PMessage, + ) -> Result<(), NetworkError> { + let signed = self.sign_message(message)?; + let bytes = + bincode::serialize(&signed).map_err(|e| NetworkError::Serialization(e.to_string()))?; + + behaviour + .gossipsub + .publish(self.consensus_topic.clone(), bytes) + .map_err(|e| NetworkError::Gossipsub(e.to_string()))?; + + debug!(msg_type = %signed.message.type_name(), "Broadcast consensus message"); + Ok(()) + } + + /// Broadcast a message to the challenge topic + pub fn broadcast_challenge( + &self, + behaviour: &mut NetworkBehaviour, + message: P2PMessage, + ) -> Result<(), NetworkError> { + let signed = self.sign_message(message)?; + let bytes = + bincode::serialize(&signed).map_err(|e| NetworkError::Serialization(e.to_string()))?; + + behaviour + .gossipsub + .publish(self.challenge_topic.clone(), bytes) + .map_err(|e| NetworkError::Gossipsub(e.to_string()))?; + + debug!(msg_type = %signed.message.type_name(), "Broadcast challenge message"); + Ok(()) + } + + /// Sign a P2P message + fn sign_message(&self, message: P2PMessage) -> Result { + let nonce = { + let mut n = self.nonce.write(); + *n += 1; + *n + }; + + let mut signed = SignedP2PMessage { + message, + signer: self.keypair.hotkey(), + signature: vec![], + nonce, + }; + + let signing_bytes = signed + .signing_bytes() + .map_err(|e| NetworkError::Serialization(e.to_string()))?; + + signed.signature = self + .keypair + .sign_bytes(&signing_bytes) + .map_err(|e| NetworkError::Serialization(e.to_string()))?; + + Ok(signed) + } + + /// Verify a signed message + pub fn verify_message(&self, signed: &SignedP2PMessage) -> bool { + let signing_bytes = match signed.signing_bytes() { + Ok(bytes) => bytes, + Err(_) => return false, + }; + + let signed_msg = platform_core::SignedMessage { + message: signing_bytes, + signature: signed.signature.clone(), + signer: signed.signer.clone(), + }; + + signed_msg.verify().unwrap_or_default() + } + + /// Handle incoming gossipsub message + /// + /// Performs the following security checks: + /// 1. Signature verification + /// 2. Replay protection (nonce tracking) + /// 3. Rate limiting (messages per second) + pub fn handle_gossipsub_message( + &self, + source: PeerId, + data: &[u8], + ) -> Result { + let signed: SignedP2PMessage = bincode::DefaultOptions::new() + .with_limit(MAX_P2P_MESSAGE_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(data) + .map_err(|e| NetworkError::Serialization(e.to_string()))?; + + // Verify signature first + if !self.verify_message(&signed) { + return Err(NetworkError::Gossipsub( + "Invalid message signature".to_string(), + )); + } + + // Ensure the signed hotkey matches the message identity + if let Some(expected) = expected_signer(&signed.message) { + if expected != &signed.signer { + return Err(NetworkError::Gossipsub( + "Signed hotkey does not match message sender".to_string(), + )); + } + } + + // Enforce validator-only messages for consensus traffic + if requires_validator(&signed.message) && !self.validator_set.is_validator(&signed.signer) { + return Err(NetworkError::Gossipsub( + "Signer is not a registered validator".to_string(), + )); + } + + // Validate weight vote payload integrity when present + if let P2PMessage::WeightVote(weight_vote) = &signed.message { + validate_weight_vote_hash(weight_vote)?; + } + + // Check rate limit before processing + self.check_rate_limit(&signed.signer)?; + + // Check for replay attack (after signature verification to avoid DoS) + self.check_replay(&signed.signer, signed.nonce)?; + + // Update peer mapping + if self.peer_mapping.get_hotkey(&source).is_none() { + self.peer_mapping.insert(source, signed.signer.clone()); + } + + Ok(signed.message) + } + + /// Check if a nonce has been seen before (replay attack detection) + /// + /// Uses timestamp-based expiry to automatically clean old nonces and bound memory usage. + /// Nonces older than NONCE_EXPIRY_MS (5 minutes) are automatically removed. + fn check_replay(&self, signer: &Hotkey, nonce: u64) -> Result<(), NetworkError> { + let now_ms = chrono::Utc::now().timestamp_millis(); + let mut seen_nonces = self.seen_nonces.write(); + let nonces = seen_nonces.entry(signer.clone()).or_default(); + + // Auto-expire old nonces to bound memory usage + nonces.retain(|_, timestamp| now_ms - *timestamp < NONCE_EXPIRY_MS); + + // Check if this nonce was already seen (and not expired) + if nonces.contains_key(&nonce) { + return Err(NetworkError::ReplayAttack { + signer: signer.to_hex(), + nonce, + }); + } + + // Record this nonce with current timestamp + nonces.insert(nonce, now_ms); + Ok(()) + } + + /// Check and update rate limit for a signer using sliding window + /// + /// Uses a sliding window approach to prevent burst attacks at window boundaries. + /// Tracks individual message timestamps and counts messages within the window. + fn check_rate_limit(&self, signer: &Hotkey) -> Result<(), NetworkError> { + let now_ms = chrono::Utc::now().timestamp_millis(); + let mut timestamps = self.message_timestamps.write(); + let queue = timestamps.entry(signer.clone()).or_default(); + + // Remove timestamps older than the sliding window + while let Some(&front) = queue.front() { + if now_ms - front > RATE_LIMIT_WINDOW_MS { + queue.pop_front(); + } else { + break; + } + } + + // Check if over limit (>= because we're about to add one more) + if queue.len() >= DEFAULT_RATE_LIMIT as usize { + return Err(NetworkError::RateLimitExceeded { + signer: signer.to_hex(), + count: queue.len() as u32, + }); + } + + // Add current timestamp + queue.push_back(now_ms); + Ok(()) + } + + /// Clean old nonces to prevent memory growth + /// + /// This should be called periodically (e.g., every minute) to remove + /// old nonces that are no longer relevant for replay protection. + /// The `max_age_secs` parameter determines how long to keep nonces. + /// + /// Note: Nonces are also automatically cleaned during `check_replay()` calls, + /// but this method provides bulk cleanup for signers who have stopped sending messages. + pub fn clean_old_nonces(&self, max_age_secs: u64) { + let now_ms = chrono::Utc::now().timestamp_millis(); + let max_age_ms = (max_age_secs * 1000) as i64; + let mut seen_nonces = self.seen_nonces.write(); + + // Clean expired nonces for each signer + for nonces in seen_nonces.values_mut() { + nonces.retain(|_, timestamp| now_ms - *timestamp < max_age_ms); + } + + // Remove signers with no remaining nonces + seen_nonces.retain(|_, nonces| !nonces.is_empty()); + + debug!( + "Cleaned old nonces, current signer count: {}", + seen_nonces.len() + ); + } + + /// Clean stale rate limit entries + /// + /// Should be called periodically to remove old rate limit tracking entries. + /// Removes signers who haven't sent messages within the rate limit window. + pub fn clean_rate_limit_entries(&self) { + let now_ms = chrono::Utc::now().timestamp_millis(); + let mut timestamps = self.message_timestamps.write(); + + // Clean old timestamps for each signer + for queue in timestamps.values_mut() { + while let Some(&front) = queue.front() { + if now_ms - front > RATE_LIMIT_WINDOW_MS { + queue.pop_front(); + } else { + break; + } + } + } + + // Remove signers with no recent messages + timestamps.retain(|_, queue| !queue.is_empty()); + } + + /// Start listening on configured addresses + pub fn start_listening( + &self, + swarm: &mut Swarm, + ) -> Result, NetworkError> + where + TBehaviour: libp2p::swarm::NetworkBehaviour, + { + let mut listening_addrs = Vec::new(); + + for addr_str in &self.config.listen_addrs { + match addr_str.parse::() { + Ok(addr) => match swarm.listen_on(addr.clone()) { + Ok(_) => { + info!(addr = %addr, "Listening on address"); + listening_addrs.push(addr); + } + Err(e) => { + error!(addr = %addr_str, error = %e, "Failed to listen on address"); + } + }, + Err(e) => { + error!(addr = %addr_str, error = %e, "Invalid listen address"); + } + } + } + + if listening_addrs.is_empty() { + return Err(NetworkError::Transport( + "No valid listen addresses".to_string(), + )); + } + + Ok(listening_addrs) + } + + /// Bootstrap Kademlia DHT + pub fn bootstrap_dht(&self, behaviour: &mut NetworkBehaviour) { + match behaviour.kademlia.bootstrap() { + Ok(_) => info!("Started Kademlia bootstrap"), + Err(e) => warn!(error = ?e, "Failed to bootstrap Kademlia (no peers?)"), + } + } + + /// Get connected peer count + pub fn peer_count(&self, swarm: &Swarm) -> usize + where + TBehaviour: libp2p::swarm::NetworkBehaviour, + { + swarm.connected_peers().count() + } + + /// Start the P2P network and return event/command channels + /// + /// Returns a tuple of (event_receiver, command_sender) that can be used to + /// interact with the network. The network runs in the background and processes + /// incoming events, broadcasting them through the event channel. + pub async fn start( + &self, + ) -> Result<(mpsc::Receiver, mpsc::Sender), NetworkError> { + let (event_tx, event_rx) = mpsc::channel::(1000); + let (cmd_tx, _cmd_rx) = mpsc::channel::(1000); + + // Get libp2p keypair + let seed = self.keypair.seed(); + let libp2p_keypair = libp2p::identity::Keypair::ed25519_from_bytes(seed).map_err(|e| { + NetworkError::Transport(format!("Failed to create libp2p keypair: {}", e)) + })?; + + // Create behaviour + let mut behaviour = self.create_behaviour(&libp2p_keypair)?; + + // Subscribe to topics + self.subscribe(&mut behaviour)?; + + info!( + peer_id = %self.local_peer_id, + "P2P network started, returning event/command channels" + ); + + // Store event_tx for forwarding events + let _event_tx_clone = event_tx.clone(); + + // The actual event loop would be spawned here in a full implementation + // For now, we return the channels and let the caller handle the swarm event loop + // This allows for more flexible integration with different runtime patterns + + Ok((event_rx, cmd_tx)) + } +} + +fn expected_signer(message: &P2PMessage) -> Option<&Hotkey> { + match message { + P2PMessage::Proposal(msg) => Some(&msg.proposer), + P2PMessage::PrePrepare(msg) => Some(&msg.leader), + P2PMessage::Prepare(msg) => Some(&msg.validator), + P2PMessage::Commit(msg) => Some(&msg.validator), + P2PMessage::ViewChange(msg) => Some(&msg.validator), + P2PMessage::NewView(msg) => Some(&msg.leader), + P2PMessage::StateRequest(msg) => Some(&msg.requester), + P2PMessage::StateResponse(msg) => Some(&msg.responder), + P2PMessage::Submission(msg) => Some(&msg.miner), + P2PMessage::Evaluation(msg) => Some(&msg.validator), + P2PMessage::WeightVote(msg) => Some(&msg.validator), + P2PMessage::Heartbeat(msg) => Some(&msg.validator), + P2PMessage::PeerAnnounce(msg) => Some(&msg.validator), + P2PMessage::JobClaim(msg) => Some(&msg.validator), + P2PMessage::JobAssignment(msg) => Some(&msg.assigner), + P2PMessage::DataRequest(msg) => Some(&msg.requester), + P2PMessage::DataResponse(msg) => Some(&msg.responder), + P2PMessage::TaskProgress(msg) => Some(&msg.validator), + P2PMessage::TaskResult(msg) => Some(&msg.validator), + P2PMessage::LeaderboardRequest(msg) => Some(&msg.requester), + P2PMessage::LeaderboardResponse(msg) => Some(&msg.responder), + P2PMessage::ChallengeUpdate(msg) => Some(&msg.updater), + P2PMessage::StorageProposal(msg) => Some(&msg.proposer), + P2PMessage::StorageVote(msg) => Some(&msg.voter), + P2PMessage::ReviewAssignment(msg) => Some(&msg.assigner), + P2PMessage::ReviewDecline(msg) => Some(&msg.validator), + P2PMessage::ReviewResult(msg) => Some(&msg.validator), + P2PMessage::AgentLogProposal(msg) => Some(&msg.validator_hotkey), + P2PMessage::SudoAction(msg) => Some(&msg.signer), + } +} + +fn requires_validator(message: &P2PMessage) -> bool { + !matches!(message, P2PMessage::Submission(_)) +} + +fn validate_weight_vote_hash(message: &WeightVoteMessage) -> Result<(), NetworkError> { + let computed = + hash_data(&message.weights).map_err(|e| NetworkError::Serialization(e.to_string()))?; + if computed != message.weights_hash { + return Err(NetworkError::Gossipsub( + "Weight vote hash mismatch".to_string(), + )); + } + Ok(()) +} + +/// Extract peer ID from multiaddr if present +fn extract_peer_id(addr: &Multiaddr) -> Option { + addr.iter().find_map(|p| { + if let libp2p::multiaddr::Protocol::P2p(peer_id) = p { + Some(peer_id) + } else { + None + } + }) +} + +/// Network runner that processes swarm events +pub struct NetworkRunner { + network: Arc, + event_tx: mpsc::Sender, +} + +impl NetworkRunner { + pub fn new(network: Arc, event_tx: mpsc::Sender) -> Self { + Self { network, event_tx } + } + + /// Handle gossipsub event + pub async fn handle_gossipsub_event( + &self, + event: gossipsub::Event, + ) -> Result<(), NetworkError> { + if let gossipsub::Event::Message { + propagation_source, + message, + .. + } = event + { + match self + .network + .handle_gossipsub_message(propagation_source, &message.data) + { + Ok(msg) => { + debug!( + source = %propagation_source, + msg_type = %msg.type_name(), + "Received gossipsub message" + ); + if let Err(e) = self + .event_tx + .send(NetworkEvent::Message { + source: propagation_source, + message: msg, + }) + .await + { + error!(error = %e, "Failed to send message event"); + } + } + Err(e) => { + warn!( + source = %propagation_source, + error = %e, + "Failed to process gossipsub message" + ); + } + } + } + Ok(()) + } + + /// Handle kademlia event + pub async fn handle_kademlia_event(&self, event: kad::Event) -> Result<(), NetworkError> { + match event { + kad::Event::RoutingUpdated { peer, .. } => { + debug!(peer = %peer, "Kademlia routing updated"); + } + kad::Event::OutboundQueryProgressed { + result: kad::QueryResult::Bootstrap(Ok(_)), + .. + } => { + info!("Kademlia bootstrap completed"); + } + kad::Event::OutboundQueryProgressed { .. } => {} + _ => {} + } + Ok(()) + } + + /// Handle identify event + pub async fn handle_identify_event( + &self, + event: identify::Event, + behaviour: &mut NetworkBehaviour, + ) -> Result<(), NetworkError> { + if let identify::Event::Received { peer_id, info, .. } = event { + debug!( + peer = %peer_id, + protocol = %info.protocol_version, + "Received identify info" + ); + + for addr in &info.listen_addrs { + behaviour.kademlia.add_address(&peer_id, addr.clone()); + } + + if let Err(e) = self + .event_tx + .send(NetworkEvent::PeerIdentified { + peer_id, + hotkey: self.network.peer_mapping.get_hotkey(&peer_id), + addresses: info.listen_addrs, + }) + .await + { + error!(error = %e, "Failed to send peer identified event"); + } + } + Ok(()) + } + + /// Handle connection established + pub async fn handle_connection_established(&self, peer_id: PeerId) -> Result<(), NetworkError> { + info!(peer = %peer_id, "Connection established"); + if let Err(e) = self + .event_tx + .send(NetworkEvent::PeerConnected(peer_id)) + .await + { + error!(error = %e, "Failed to send peer connected event"); + } + Ok(()) + } + + /// Handle connection closed + pub async fn handle_connection_closed(&self, peer_id: PeerId) -> Result<(), NetworkError> { + info!(peer = %peer_id, "Connection closed"); + self.network.peer_mapping.remove_peer(&peer_id); + if let Err(e) = self + .event_tx + .send(NetworkEvent::PeerDisconnected(peer_id)) + .await + { + error!(error = %e, "Failed to send peer disconnected event"); + } + Ok(()) + } +} + +/// Helper to build a complete swarm with all behaviours +pub async fn build_swarm( + keypair: &Keypair, + config: &P2PConfig, +) -> Result<(Swarm, NetworkBehaviour), NetworkError> { + let seed = keypair.seed(); + let libp2p_keypair = libp2p::identity::Keypair::ed25519_from_bytes(seed) + .map_err(|e| NetworkError::Transport(format!("Failed to create keypair: {}", e)))?; + + let local_peer_id = PeerId::from(libp2p_keypair.public()); + + // Create gossipsub + let gossipsub_config = gossipsub::ConfigBuilder::default() + .heartbeat_interval(Duration::from_secs(1)) + .validation_mode(ValidationMode::Strict) + .message_id_fn(|msg: &gossipsub::Message| { + use sha2::Digest; + let hash = sha2::Sha256::digest(&msg.data); + MessageId::from(hash.to_vec()) + }) + .max_transmit_size(config.max_message_size) + .build() + .map_err(|e| NetworkError::Gossipsub(e.to_string()))?; + + let gossipsub = gossipsub::Behaviour::new( + MessageAuthenticity::Signed(libp2p_keypair.clone()), + gossipsub_config, + ) + .map_err(|e| NetworkError::Gossipsub(e.to_string()))?; + + // Create kademlia + let store = MemoryStore::new(local_peer_id); + let kademlia = kad::Behaviour::new(local_peer_id, store); + + // Create identify + let identify_config = + identify::Config::new("/platform/1.0.0".to_string(), libp2p_keypair.public()); + let identify = identify::Behaviour::new(identify_config); + + let behaviour = NetworkBehaviour { + gossipsub, + kademlia, + identify, + }; + + // Build a minimal swarm for structure (actual swarm creation would need the behaviour) + let swarm = SwarmBuilder::with_existing_identity(libp2p_keypair) + .with_tokio() + .with_tcp( + tcp::Config::default(), + noise::Config::new, + yamux::Config::default, + ) + .map_err(|e| NetworkError::Transport(e.to_string()))? + .with_dns() + .map_err(|e| NetworkError::Transport(e.to_string()))? + .with_behaviour(|_| libp2p::swarm::dummy::Behaviour) + .map_err(|e| NetworkError::Transport(e.to_string()))? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) + .build(); + + Ok((swarm, behaviour)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_peer_mapping() { + let mapping = PeerMapping::new(); + let peer_id = PeerId::random(); + let hotkey = Hotkey([1u8; 32]); + + mapping.insert(peer_id, hotkey.clone()); + + assert_eq!(mapping.get_hotkey(&peer_id), Some(hotkey.clone())); + assert_eq!(mapping.get_peer(&hotkey), Some(peer_id)); + + mapping.remove_peer(&peer_id); + + assert!(mapping.get_hotkey(&peer_id).is_none()); + assert!(mapping.get_peer(&hotkey).is_none()); + } + + #[test] + fn test_extract_peer_id() { + let peer_id = PeerId::random(); + let addr: Multiaddr = format!("/ip4/127.0.0.1/tcp/9000/p2p/{}", peer_id) + .parse() + .unwrap(); + + let extracted = extract_peer_id(&addr); + assert_eq!(extracted, Some(peer_id)); + + let addr_no_peer: Multiaddr = "/ip4/127.0.0.1/tcp/9000".parse().unwrap(); + assert!(extract_peer_id(&addr_no_peer).is_none()); + } + + #[tokio::test] + async fn test_network_creation() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = P2PNetwork::new(keypair, config, validator_set, tx); + assert!(network.is_ok()); + } + + #[test] + fn test_peer_mapping_default() { + let mapping = PeerMapping::default(); + let peer_id = PeerId::random(); + assert!(mapping.get_hotkey(&peer_id).is_none()); + } + + #[test] + fn test_peer_mapping_len_and_is_empty() { + let mapping = PeerMapping::new(); + + // Initially empty + assert!(mapping.is_empty()); + assert_eq!(mapping.len(), 0); + + // Add first peer + let peer_id1 = PeerId::random(); + let hotkey1 = Hotkey([1u8; 32]); + mapping.insert(peer_id1, hotkey1); + + assert!(!mapping.is_empty()); + assert_eq!(mapping.len(), 1); + + // Add second peer + let peer_id2 = PeerId::random(); + let hotkey2 = Hotkey([2u8; 32]); + mapping.insert(peer_id2, hotkey2); + + assert!(!mapping.is_empty()); + assert_eq!(mapping.len(), 2); + + // Remove one peer + mapping.remove_peer(&peer_id1); + assert_eq!(mapping.len(), 1); + + // Remove the other peer + mapping.remove_peer(&peer_id2); + assert!(mapping.is_empty()); + assert_eq!(mapping.len(), 0); + } + + #[test] + fn test_peer_mapping_overwrite() { + let mapping = PeerMapping::new(); + let peer_id = PeerId::random(); + let hotkey1 = Hotkey([1u8; 32]); + let hotkey2 = Hotkey([2u8; 32]); + + // Insert with first hotkey + mapping.insert(peer_id, hotkey1.clone()); + assert_eq!(mapping.get_hotkey(&peer_id), Some(hotkey1.clone())); + assert_eq!(mapping.get_peer(&hotkey1), Some(peer_id)); + + // Overwrite with second hotkey + mapping.insert(peer_id, hotkey2.clone()); + assert_eq!(mapping.get_hotkey(&peer_id), Some(hotkey2.clone())); + assert_eq!(mapping.get_peer(&hotkey2), Some(peer_id)); + + // Old hotkey should still point to the peer (due to current impl not cleaning old entry) + // This tests the actual behavior - hotkey_to_peer is not cleaned on overwrite + assert_eq!(mapping.get_peer(&hotkey1), Some(peer_id)); + } + + #[test] + fn test_peer_mapping_multiple_peers() { + let mapping = PeerMapping::new(); + + // Create multiple peers with unique hotkeys + let peers: Vec<(PeerId, Hotkey)> = (0..5) + .map(|i| { + let peer_id = PeerId::random(); + let mut hotkey_bytes = [0u8; 32]; + hotkey_bytes[0] = i as u8; + (peer_id, Hotkey(hotkey_bytes)) + }) + .collect(); + + // Insert all peers + for (peer_id, hotkey) in &peers { + mapping.insert(*peer_id, hotkey.clone()); + } + + assert_eq!(mapping.len(), 5); + + // Verify all mappings are correct + for (peer_id, hotkey) in &peers { + assert_eq!(mapping.get_hotkey(peer_id), Some(hotkey.clone())); + assert_eq!(mapping.get_peer(hotkey), Some(*peer_id)); + } + + // Remove a middle peer and verify others still work + let (removed_peer, removed_hotkey) = &peers[2]; + mapping.remove_peer(removed_peer); + + assert_eq!(mapping.len(), 4); + assert!(mapping.get_hotkey(removed_peer).is_none()); + assert!(mapping.get_peer(removed_hotkey).is_none()); + + // Other peers should still be intact + assert_eq!(mapping.get_hotkey(&peers[0].0), Some(peers[0].1.clone())); + assert_eq!(mapping.get_hotkey(&peers[4].0), Some(peers[4].1.clone())); + } + + #[test] + fn test_network_error_display() { + // Test Transport error display + let transport_err = NetworkError::Transport("connection refused".to_string()); + assert_eq!( + format!("{}", transport_err), + "Transport error: connection refused" + ); + + // Test Gossipsub error display + let gossipsub_err = NetworkError::Gossipsub("subscription failed".to_string()); + assert_eq!( + format!("{}", gossipsub_err), + "Gossipsub error: subscription failed" + ); + + // Test DHT error display + let dht_err = NetworkError::Dht("bootstrap failed".to_string()); + assert_eq!(format!("{}", dht_err), "DHT error: bootstrap failed"); + + // Test Serialization error display + let serial_err = NetworkError::Serialization("invalid data".to_string()); + assert_eq!( + format!("{}", serial_err), + "Serialization error: invalid data" + ); + + // Test NoPeers error display + let no_peers_err = NetworkError::NoPeers; + assert_eq!(format!("{}", no_peers_err), "Not connected to any peers"); + + // Test Channel error display + let channel_err = NetworkError::Channel("channel closed".to_string()); + assert_eq!(format!("{}", channel_err), "Channel error: channel closed"); + + // Test ReplayAttack error display + let replay_err = NetworkError::ReplayAttack { + signer: "abc123".to_string(), + nonce: 42, + }; + assert_eq!( + format!("{}", replay_err), + "Replay attack detected: nonce 42 already seen for abc123" + ); + + // Test RateLimitExceeded error display + let rate_limit_err = NetworkError::RateLimitExceeded { + signer: "def456".to_string(), + count: 150, + }; + assert_eq!( + format!("{}", rate_limit_err), + "Rate limit exceeded for def456: 150 messages in current window" + ); + } + + #[tokio::test] + async fn test_replay_attack_detection() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + let signer = Hotkey([5u8; 32]); + let nonce = 12345u64; + + // First use of nonce should succeed + let result1 = network.check_replay(&signer, nonce); + assert!(result1.is_ok(), "First nonce use should succeed"); + + // Second use of same nonce from same signer should fail + let result2 = network.check_replay(&signer, nonce); + assert!(result2.is_err(), "Replay should be detected"); + + match result2 { + Err(NetworkError::ReplayAttack { + signer: err_signer, + nonce: err_nonce, + }) => { + assert_eq!(err_signer, signer.to_hex()); + assert_eq!(err_nonce, nonce); + } + _ => panic!("Expected ReplayAttack error"), + } + + // Different nonce from same signer should succeed + let result3 = network.check_replay(&signer, nonce + 1); + assert!(result3.is_ok(), "Different nonce should succeed"); + + // Same nonce from different signer should succeed + let signer2 = Hotkey([6u8; 32]); + let result4 = network.check_replay(&signer2, nonce); + assert!( + result4.is_ok(), + "Same nonce from different signer should succeed" + ); + } + + #[tokio::test] + async fn test_rate_limit_enforcement() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + let signer = Hotkey([7u8; 32]); + + // Send DEFAULT_RATE_LIMIT (100) messages - should all succeed + for i in 0..DEFAULT_RATE_LIMIT { + let result = network.check_rate_limit(&signer); + assert!( + result.is_ok(), + "Message {} should be within rate limit", + i + 1 + ); + } + + // The next message should exceed the limit + let result = network.check_rate_limit(&signer); + assert!( + result.is_err(), + "Should exceed rate limit after 100 messages" + ); + + match result { + Err(NetworkError::RateLimitExceeded { + signer: err_signer, + count, + }) => { + assert_eq!(err_signer, signer.to_hex()); + assert_eq!(count, DEFAULT_RATE_LIMIT); + } + _ => panic!("Expected RateLimitExceeded error"), + } + + // Different signer should have separate rate limit + let signer2 = Hotkey([8u8; 32]); + let result2 = network.check_rate_limit(&signer2); + assert!( + result2.is_ok(), + "Different signer should have separate rate limit" + ); + } + + #[tokio::test] + async fn test_clean_old_nonces() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + let signer = Hotkey([9u8; 32]); + + // Add some nonces + network + .check_replay(&signer, 1) + .expect("Nonce 1 should succeed"); + network + .check_replay(&signer, 2) + .expect("Nonce 2 should succeed"); + network + .check_replay(&signer, 3) + .expect("Nonce 3 should succeed"); + + // Verify nonces are tracked + { + let seen_nonces = network.seen_nonces.read(); + let signer_nonces = seen_nonces.get(&signer); + assert!(signer_nonces.is_some()); + assert_eq!(signer_nonces.unwrap().len(), 3); + } + + // Clean with 0 max_age_secs - all nonces should be considered old and removed + network.clean_old_nonces(0); + + // After cleaning with 0 age, all nonces should be gone + { + let seen_nonces = network.seen_nonces.read(); + // Signer entry should be removed since all its nonces expired + assert!( + seen_nonces.get(&signer).is_none() || seen_nonces.get(&signer).unwrap().is_empty(), + "Nonces should be cleaned" + ); + } + + // Now the same nonces should be usable again + let result = network.check_replay(&signer, 1); + assert!(result.is_ok(), "Nonce 1 should be usable after cleaning"); + } + + #[tokio::test] + async fn test_clean_rate_limit_entries() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + let signer1 = Hotkey([10u8; 32]); + let signer2 = Hotkey([11u8; 32]); + + // Add rate limit entries for both signers + network + .check_rate_limit(&signer1) + .expect("Rate limit check should succeed"); + network + .check_rate_limit(&signer2) + .expect("Rate limit check should succeed"); + + // Verify entries exist + { + let timestamps = network.message_timestamps.read(); + assert!(timestamps.contains_key(&signer1)); + assert!(timestamps.contains_key(&signer2)); + } + + // Clean entries (this removes entries older than RATE_LIMIT_WINDOW_MS) + // Since entries were just added, they shouldn't be removed yet + network.clean_rate_limit_entries(); + + { + let timestamps = network.message_timestamps.read(); + // Entries should still exist since they're recent + assert!(timestamps.contains_key(&signer1)); + assert!(timestamps.contains_key(&signer2)); + } + + // Manually manipulate timestamps to simulate old entries for testing + // by replacing with empty queues (simulating all old entries removed) + { + let mut timestamps = network.message_timestamps.write(); + timestamps.clear(); + } + + // After clearing, clean should not find anything + network.clean_rate_limit_entries(); + + { + let timestamps = network.message_timestamps.read(); + assert!(timestamps.is_empty()); + } + } + + #[tokio::test] + async fn test_network_connected_peer_count() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + // Initially no connected peers + assert_eq!(network.connected_peer_count(), 0); + + // Add peers to the peer mapping + let peer_id1 = PeerId::random(); + let hotkey1 = Hotkey([20u8; 32]); + network.peer_mapping.insert(peer_id1, hotkey1); + + assert_eq!(network.connected_peer_count(), 1); + + let peer_id2 = PeerId::random(); + let hotkey2 = Hotkey([21u8; 32]); + network.peer_mapping.insert(peer_id2, hotkey2); + + assert_eq!(network.connected_peer_count(), 2); + + let peer_id3 = PeerId::random(); + let hotkey3 = Hotkey([22u8; 32]); + network.peer_mapping.insert(peer_id3, hotkey3); + + assert_eq!(network.connected_peer_count(), 3); + + // Remove a peer + network.peer_mapping.remove_peer(&peer_id2); + assert_eq!(network.connected_peer_count(), 2); + } + + #[tokio::test] + async fn test_network_has_min_peers() { + let keypair = Keypair::generate(); + let config = P2PConfig::development(); + let validator_set = Arc::new(ValidatorSet::new(keypair.clone(), 0)); + let (tx, _rx) = mpsc::channel(100); + + let network = + P2PNetwork::new(keypair, config, validator_set, tx).expect("Failed to create network"); + + // Initially no peers + assert!(!network.has_min_peers(1)); + assert!(!network.has_min_peers(3)); + assert!(network.has_min_peers(0)); // 0 is always satisfied + + // Add one peer + let peer_id1 = PeerId::random(); + let hotkey1 = Hotkey([30u8; 32]); + network.peer_mapping.insert(peer_id1, hotkey1); + + assert!(network.has_min_peers(0)); + assert!(network.has_min_peers(1)); + assert!(!network.has_min_peers(2)); + + // Add two more peers + let peer_id2 = PeerId::random(); + let hotkey2 = Hotkey([31u8; 32]); + network.peer_mapping.insert(peer_id2, hotkey2); + + let peer_id3 = PeerId::random(); + let hotkey3 = Hotkey([32u8; 32]); + network.peer_mapping.insert(peer_id3, hotkey3); + + assert!(network.has_min_peers(0)); + assert!(network.has_min_peers(1)); + assert!(network.has_min_peers(2)); + assert!(network.has_min_peers(3)); + assert!(!network.has_min_peers(4)); + + // Remove one peer + network.peer_mapping.remove_peer(&peer_id2); + assert!(network.has_min_peers(2)); + assert!(!network.has_min_peers(3)); + } +} diff --git a/crates/p2p-consensus/src/state.rs b/crates/p2p-consensus/src/state.rs new file mode 100644 index 000000000..1545e9c80 --- /dev/null +++ b/crates/p2p-consensus/src/state.rs @@ -0,0 +1,1375 @@ +//! Decentralized state management for P2P consensus +//! +//! Manages the shared state across validators including challenges, +//! evaluations, weights, and validator information. + +use crate::messages::{MerkleNode, MerkleProof, SequenceNumber}; +use bincode::Options; +use parking_lot::RwLock; +use platform_core::{hash_data, ChallengeId, Hotkey, SignedMessage}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use thiserror::Error; +use tracing::{debug, info, warn}; + +const MAX_STATE_DESERIALIZATION_SIZE: u64 = 256 * 1024 * 1024; + +/// Maximum size of agent log data in bytes (256 KB) +const MAX_AGENT_LOG_SIZE: usize = 256 * 1024; + +/// Maximum number of pending agent log proposals (per-submission entries) +const MAX_AGENT_LOG_PROPOSALS: usize = 10_000; + +/// Maximum number of validated agent log entries +const MAX_VALIDATED_AGENT_LOGS: usize = 50_000; + +/// Errors related to state operations +#[derive(Error, Debug)] +pub enum StateError { + #[error("State hash mismatch: expected {expected}, got {actual}")] + HashMismatch { expected: String, actual: String }, + #[error("Invalid merkle proof")] + InvalidMerkleProof, + #[error("Sequence number too old: current {current}, received {received}")] + SequenceTooOld { current: u64, received: u64 }, + #[error("State serialization error: {0}")] + Serialization(String), + #[error("Challenge not found: {0}")] + ChallengeNotFound(String), + #[error("Invalid signature: {0}")] + InvalidSignature(String), +} + +/// Evaluation record for a submission +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EvaluationRecord { + /// Submission ID + pub submission_id: String, + /// Challenge ID + pub challenge_id: ChallengeId, + /// Miner who submitted + pub miner: Hotkey, + /// Agent code hash + pub agent_hash: String, + /// Evaluations from validators (validator hotkey -> score) + pub evaluations: HashMap, + /// Aggregated score (computed when finalized) + pub aggregated_score: Option, + /// Whether evaluation is finalized + pub finalized: bool, + /// Creation timestamp + pub created_at: i64, + /// Finalization timestamp + pub finalized_at: Option, +} + +/// Single validator's evaluation +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorEvaluation { + /// Score (0.0 to 1.0) + pub score: f64, + /// Validator's stake at evaluation time + pub stake: u64, + /// Evaluation timestamp + pub timestamp: i64, + /// Signature over (submission_id, score) + pub signature: Vec, +} + +/// Challenge configuration stored in state +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeConfig { + /// Challenge ID + pub id: ChallengeId, + /// Challenge name + pub name: String, + /// Weight allocation (0-100) + pub weight: u16, + /// Whether challenge is active + pub is_active: bool, + /// Creator hotkey + pub creator: Hotkey, + /// Creation timestamp + pub created_at: i64, +} + +/// Weight votes for epoch finalization +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightVotes { + /// Epoch number + pub epoch: u64, + /// Netuid + pub netuid: u16, + /// Votes from validators (hotkey -> weight vector) + pub votes: HashMap>, + /// Whether weights have been finalized + pub finalized: bool, + /// Final aggregated weights (if finalized) + pub final_weights: Option>, +} + +/// Leaderboard entry for a challenge +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct LeaderboardEntry { + pub miner: Hotkey, + pub score: f64, + pub submission_count: u32, + pub last_submission_at: i64, + pub rank: u32, +} + +/// Record of an active evaluation job +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobRecord { + pub submission_id: String, + pub challenge_id: ChallengeId, + pub assigned_validator: Hotkey, + pub assigned_at: i64, + pub timeout_at: i64, + pub status: JobStatus, +} + +/// Status of an evaluation job +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum JobStatus { + Pending, + InProgress, + Completed, + TimedOut, +} + +/// Record of real-time task progress +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct TaskProgressRecord { + pub submission_id: String, + pub challenge_id: ChallengeId, + pub validator: Hotkey, + pub task_index: u32, + pub total_tasks: u32, + pub status: String, + pub progress_pct: f64, + pub updated_at: i64, +} + +/// The shared chain state for P2P consensus +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChainState { + /// Current sequence number (increments with each state change) + pub sequence: SequenceNumber, + /// Current epoch + pub epoch: u64, + /// State hash + pub state_hash: [u8; 32], + /// Active validators (hotkey -> stake) + pub validators: HashMap, + /// Active challenges + pub challenges: HashMap, + /// Pending evaluations (submission_id -> record) + pub pending_evaluations: HashMap, + /// Completed evaluations (by epoch) + pub completed_evaluations: HashMap>, + /// Current epoch's weight votes + pub weight_votes: Option, + /// Historical weights (epoch -> final weights) + pub historical_weights: HashMap>, + /// Sudo key + pub sudo_key: Hotkey, + /// Netuid + pub netuid: u16, + /// Last update timestamp + pub last_updated: i64, + /// Linked Bittensor/Subtensor block number + pub bittensor_block: u64, + /// Hash of the linked Bittensor/Subtensor block + pub bittensor_block_hash: [u8; 32], + /// Leaderboards per challenge + #[serde(default)] + pub leaderboard: HashMap>, + /// Active evaluation jobs + #[serde(default)] + pub active_jobs: HashMap, + /// Real-time task progress records + #[serde(default)] + pub task_progress: HashMap, + /// Storage roots per challenge + #[serde(default)] + pub challenge_storage_roots: HashMap, + /// Review assignments per submission + #[serde(default)] + pub review_assignments: HashMap>, + /// Agent logs awaiting consensus (submission_id -> validator_hotkey -> serialized logs) + #[serde(default)] + pub agent_log_proposals: HashMap>>, + /// Consensus-validated agent logs (submission_id -> validated logs) + #[serde(default)] + pub validated_agent_logs: HashMap>, + /// Stored agent code registry (miner_hotkey -> latest agent code entry) + #[serde(default)] + pub agent_code_registry: HashMap, + + /// Whether the network is stopped (all emissions go to UID 0 burn) + #[serde(default)] + pub network_stopped: bool, + + /// Reason for network stop + #[serde(default)] + pub network_stop_reason: Option, +} + +/// Record of a review assignment +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReviewRecord { + pub submission_id: String, + pub review_type: crate::messages::ReviewType, + pub assigned_validators: Vec, + pub results: HashMap, + pub created_at: i64, +} + +/// Single review result entry +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ReviewResultEntry { + pub score: f64, + pub details: String, + pub timestamp: i64, +} + +/// Registry entry for stored agent code +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AgentCodeEntry { + pub agent_hash: String, + pub code_size: u64, + pub epoch: u64, + pub stored_at: i64, +} + +impl Default for ChainState { + fn default() -> Self { + Self { + sequence: 0, + epoch: 0, + state_hash: [0u8; 32], + validators: HashMap::new(), + challenges: HashMap::new(), + pending_evaluations: HashMap::new(), + completed_evaluations: HashMap::new(), + weight_votes: None, + historical_weights: HashMap::new(), + sudo_key: Hotkey(platform_core::SUDO_KEY_BYTES), + netuid: 100, + last_updated: chrono::Utc::now().timestamp_millis(), + bittensor_block: 0, + bittensor_block_hash: [0u8; 32], + leaderboard: HashMap::new(), + active_jobs: HashMap::new(), + task_progress: HashMap::new(), + challenge_storage_roots: HashMap::new(), + review_assignments: HashMap::new(), + agent_log_proposals: HashMap::new(), + validated_agent_logs: HashMap::new(), + agent_code_registry: HashMap::new(), + network_stopped: false, + network_stop_reason: None, + } + } +} + +impl ChainState { + /// Create new chain state with production sudo key + pub fn new(netuid: u16) -> Self { + let mut state = Self { + netuid, + sudo_key: Hotkey(platform_core::SUDO_KEY_BYTES), + ..Default::default() + }; + state.update_hash(); + state + } + + /// Create with custom sudo key (for testing) + pub fn with_sudo(sudo_key: Hotkey, netuid: u16) -> Self { + let mut state = Self { + netuid, + sudo_key, + ..Default::default() + }; + state.update_hash(); + state + } + + /// Update the state hash after modifications + pub fn update_hash(&mut self) { + self.last_updated = chrono::Utc::now().timestamp_millis(); + + // Create a deterministic hash input + #[derive(Serialize)] + struct HashInput { + sequence: SequenceNumber, + epoch: u64, + validator_count: usize, + challenge_count: usize, + pending_count: usize, + netuid: u16, + } + + let input = HashInput { + sequence: self.sequence, + epoch: self.epoch, + validator_count: self.validators.len(), + challenge_count: self.challenges.len(), + pending_count: self.pending_evaluations.len(), + netuid: self.netuid, + }; + + self.state_hash = hash_data(&input).unwrap_or([0u8; 32]); + } + + /// Increment sequence and update hash + pub fn increment_sequence(&mut self) { + self.sequence += 1; + self.update_hash(); + } + + /// Link state to a Bittensor block + /// + /// Updates the linked Bittensor block number and hash, and increments + /// the sequence number to track this state change. + pub fn link_to_bittensor_block(&mut self, block_number: u64, block_hash: [u8; 32]) { + self.bittensor_block = block_number; + self.bittensor_block_hash = block_hash; + self.increment_sequence(); + } + + /// Get linked Bittensor block number + pub fn linked_block(&self) -> u64 { + self.bittensor_block + } + + /// Check if a hotkey is the sudo key + pub fn is_sudo(&self, hotkey: &Hotkey) -> bool { + self.sudo_key == *hotkey + } + + /// Get state hash as hex string + pub fn hash_hex(&self) -> String { + hex::encode(self.state_hash) + } + + /// Serialize state to bytes + pub fn to_bytes(&self) -> Result, StateError> { + bincode::serialize(self).map_err(|e| StateError::Serialization(e.to_string())) + } + + /// Deserialize state from bytes + pub fn from_bytes(bytes: &[u8]) -> Result { + if bytes.len() as u64 > MAX_STATE_DESERIALIZATION_SIZE { + return Err(StateError::Serialization(format!( + "state data exceeds maximum size: {} > {}", + bytes.len(), + MAX_STATE_DESERIALIZATION_SIZE + ))); + } + bincode::DefaultOptions::new() + .with_limit(MAX_STATE_DESERIALIZATION_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(bytes) + .map_err(|e| StateError::Serialization(e.to_string())) + } + + /// Add or update a validator + pub fn update_validator(&mut self, hotkey: Hotkey, stake: u64) { + self.validators.insert(hotkey, stake); + self.increment_sequence(); + } + + /// Remove a validator + pub fn remove_validator(&mut self, hotkey: &Hotkey) -> bool { + let removed = self.validators.remove(hotkey).is_some(); + if removed { + self.increment_sequence(); + } + removed + } + + /// Add a challenge + pub fn add_challenge(&mut self, config: ChallengeConfig) { + info!(challenge_id = %config.id, name = %config.name, "Adding challenge to state"); + self.challenges.insert(config.id, config); + self.increment_sequence(); + } + + /// Remove a challenge + pub fn remove_challenge(&mut self, id: &ChallengeId) -> Option { + let removed = self.challenges.remove(id); + if removed.is_some() { + self.increment_sequence(); + } + removed + } + + /// Add an evaluation record + pub fn add_evaluation(&mut self, record: EvaluationRecord) { + debug!(submission_id = %record.submission_id, "Adding evaluation record"); + self.pending_evaluations + .insert(record.submission_id.clone(), record); + self.increment_sequence(); + } + + /// Add validator evaluation to existing record + /// + /// Verifies the provided signature before accepting the evaluation. + /// The signing data is (submission_id, score) serialized via bincode. + pub fn add_validator_evaluation( + &mut self, + submission_id: &str, + validator: Hotkey, + evaluation: ValidatorEvaluation, + signature: &[u8], + ) -> Result<(), StateError> { + // Verify signature over (submission_id, score) + #[derive(Serialize)] + struct EvaluationSigningData<'a> { + submission_id: &'a str, + score: f64, + } + + let signing_data = EvaluationSigningData { + submission_id, + score: evaluation.score, + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| StateError::Serialization(e.to_string()))?; + + let signed_msg = SignedMessage { + message: signing_bytes, + signature: signature.to_vec(), + signer: validator.clone(), + }; + + let is_valid = signed_msg + .verify() + .map_err(|e| StateError::InvalidSignature(e.to_string()))?; + + if !is_valid { + return Err(StateError::InvalidSignature(validator.to_hex())); + } + + if let Some(record) = self.pending_evaluations.get_mut(submission_id) { + record.evaluations.insert(validator, evaluation); + self.update_hash(); + Ok(()) + } else { + Err(StateError::ChallengeNotFound(submission_id.to_string())) + } + } + + /// Finalize an evaluation (compute aggregated score) + /// + /// Only uses verified stakes from the validators map. Evaluations from + /// unknown validators are skipped entirely to prevent stake inflation attacks. + pub fn finalize_evaluation(&mut self, submission_id: &str) -> Result { + if let Some(record) = self.pending_evaluations.get_mut(submission_id) { + // Stake-weighted average using ONLY verified stakes + let mut total_stake: u64 = 0; + let mut weighted_sum: f64 = 0.0; + + for (validator_hotkey, eval) in &record.evaluations { + // Only use verified stake from validators map - skip unknown validators + if let Some(stake) = self.validators.get(validator_hotkey).copied() { + total_stake += stake; + weighted_sum += eval.score * (stake as f64); + } else { + // Skip evaluations from unknown validators to prevent stake inflation + warn!( + validator = %validator_hotkey.to_hex(), + self_reported_stake = eval.stake, + "Skipping evaluation from unknown validator - not in verified validators map" + ); + } + } + + if total_stake == 0 { + return Ok(0.0); + } + + let aggregated = weighted_sum / (total_stake as f64); + record.aggregated_score = Some(aggregated); + record.finalized = true; + record.finalized_at = Some(chrono::Utc::now().timestamp_millis()); + + // Move to completed + let completed = self + .pending_evaluations + .remove(submission_id) + .ok_or_else(|| { + StateError::ChallengeNotFound(format!( + "Submission '{}' was removed during finalization", + submission_id + )) + })?; + self.completed_evaluations + .entry(self.epoch) + .or_default() + .push(completed); + + self.increment_sequence(); + Ok(aggregated) + } else { + Err(StateError::ChallengeNotFound(submission_id.to_string())) + } + } + + /// Add weight vote from validator with signature verification + /// + /// Verifies the provided signature before accepting the weight vote. + /// The signing data is (epoch, netuid, weights) serialized via bincode. + pub fn add_weight_vote_verified( + &mut self, + validator: Hotkey, + weights: Vec<(u16, u16)>, + epoch: u64, + signature: &[u8], + ) -> Result<(), StateError> { + // Verify signature over (epoch, netuid, weights) + #[derive(Serialize)] + struct WeightVoteSigningData { + epoch: u64, + netuid: u16, + weights: Vec<(u16, u16)>, + } + + let signing_data = WeightVoteSigningData { + epoch, + netuid: self.netuid, + weights: weights.clone(), + }; + + let signing_bytes = bincode::serialize(&signing_data) + .map_err(|e| StateError::Serialization(e.to_string()))?; + + let signed_msg = SignedMessage { + message: signing_bytes, + signature: signature.to_vec(), + signer: validator.clone(), + }; + + let is_valid = signed_msg + .verify() + .map_err(|e| StateError::InvalidSignature(e.to_string()))?; + + if !is_valid { + return Err(StateError::InvalidSignature(validator.to_hex())); + } + + let votes = self.weight_votes.get_or_insert_with(|| WeightVotes { + epoch, + netuid: self.netuid, + votes: HashMap::new(), + finalized: false, + final_weights: None, + }); + + if votes.epoch == epoch && !votes.finalized { + votes.votes.insert(validator, weights); + self.update_hash(); + Ok(()) + } else { + warn!( + epoch, + votes_epoch = votes.epoch, + finalized = votes.finalized, + "Weight vote rejected: epoch mismatch or already finalized" + ); + Ok(()) // Not an error, just a stale vote + } + } + + /// Finalize epoch weights (stake-weighted aggregation) + pub fn finalize_weights(&mut self) -> Option> { + let votes = self.weight_votes.as_mut()?; + if votes.finalized { + return votes.final_weights.clone(); + } + + // Collect all UIDs that received votes + let mut uid_weights: HashMap = HashMap::new(); + let mut total_stake: u64 = 0; + + for (validator, weights) in &votes.votes { + let stake = self.validators.get(validator).copied().unwrap_or(0); + if stake == 0 { + continue; + } + total_stake += stake; + + for (uid, weight) in weights { + let weighted = (*weight as f64) * (stake as f64); + *uid_weights.entry(*uid).or_default() += weighted; + } + } + + if total_stake == 0 { + return None; + } + + // Normalize and convert to u16 + let max_weight = uid_weights.values().copied().fold(0.0f64, f64::max); + + let final_weights: Vec<(u16, u16)> = if max_weight > 0.0 { + uid_weights + .into_iter() + .map(|(uid, w)| { + let normalized = ((w / max_weight) * u16::MAX as f64) as u16; + (uid, normalized) + }) + .collect() + } else { + vec![] + }; + + votes.final_weights = Some(final_weights.clone()); + votes.finalized = true; + + // Store in history + self.historical_weights + .insert(votes.epoch, final_weights.clone()); + + self.increment_sequence(); + Some(final_weights) + } + + /// Maximum epochs to keep in historical_weights + const MAX_HISTORICAL_EPOCHS: usize = 100; + + /// Transition to next epoch + pub fn next_epoch(&mut self) { + self.epoch += 1; + self.weight_votes = None; + + // Prune old historical weights to prevent unbounded growth + if self.historical_weights.len() > Self::MAX_HISTORICAL_EPOCHS { + let cutoff_epoch = self + .epoch + .saturating_sub(Self::MAX_HISTORICAL_EPOCHS as u64); + self.historical_weights + .retain(|epoch, _| *epoch > cutoff_epoch); + debug!( + retained_epochs = self.historical_weights.len(), + cutoff = cutoff_epoch, + "Pruned historical weights" + ); + } + + self.increment_sequence(); + info!(epoch = self.epoch, "Transitioned to new epoch"); + } + + /// Get the current block height (sequence number) + /// + /// In this P2P consensus system, the sequence number serves as the + /// logical block height, incrementing with each state change. + pub fn block_height(&self) -> u64 { + self.sequence + } + + /// Get the current state hash as a 32-byte array + pub fn get_state_hash(&self) -> [u8; 32] { + self.state_hash + } + + /// Get aggregated weights for a specific epoch + /// + /// Returns a vector of (hotkey_string, weight_as_f64) pairs, where weight + /// is normalized to a 0.0-1.0 range. Returns empty vector if no weights + /// exist for the given epoch. + pub fn get_aggregated_weights(&self, epoch: u64) -> Vec<(String, f64)> { + self.historical_weights + .get(&epoch) + .map(|weights| { + weights + .iter() + .map(|(uid, weight)| { + // Convert u16 UID to string and normalize weight + let normalized_weight = (*weight as f64) / (u16::MAX as f64); + (uid.to_string(), normalized_weight) + }) + .collect() + }) + .unwrap_or_default() + } + + pub fn assign_job(&mut self, job: JobRecord) { + info!(submission_id = %job.submission_id, validator = %job.assigned_validator.to_hex(), "Job assigned"); + self.active_jobs.insert(job.submission_id.clone(), job); + self.increment_sequence(); + } + + pub fn complete_job(&mut self, submission_id: &str) -> Option { + let mut job = self.active_jobs.remove(submission_id)?; + job.status = JobStatus::Completed; + self.increment_sequence(); + Some(job) + } + + pub fn update_leaderboard( + &mut self, + challenge_id: ChallengeId, + entries: Vec, + ) { + self.leaderboard.insert(challenge_id, entries); + self.increment_sequence(); + } + + pub fn get_leaderboard(&self, challenge_id: &ChallengeId) -> Vec { + self.leaderboard + .get(challenge_id) + .cloned() + .unwrap_or_default() + } + + pub fn update_task_progress(&mut self, record: TaskProgressRecord) { + let key = format!("{}:{}", record.submission_id, record.validator.to_hex()); + self.task_progress.insert(key, record); + self.update_hash(); + } + + pub fn update_challenge_storage_root(&mut self, challenge_id: ChallengeId, root: [u8; 32]) { + self.challenge_storage_roots.insert(challenge_id, root); + self.increment_sequence(); + } + + pub fn cleanup_stale_jobs(&mut self, now: i64) -> Vec { + let stale: Vec = self + .active_jobs + .iter() + .filter(|(_, job)| job.timeout_at < now && job.status != JobStatus::Completed) + .map(|(id, _)| id.clone()) + .collect(); + let mut removed = Vec::new(); + for id in stale { + if let Some(mut job) = self.active_jobs.remove(&id) { + job.status = JobStatus::TimedOut; + removed.push(job); + } + } + if !removed.is_empty() { + self.increment_sequence(); + } + removed + } + + pub fn assign_review(&mut self, record: ReviewRecord) { + self.review_assignments + .entry(record.submission_id.clone()) + .or_default() + .push(record); + self.increment_sequence(); + } + + pub fn add_review_result( + &mut self, + submission_id: &str, + validator: &Hotkey, + score: f64, + details: String, + ) -> bool { + if !score.is_finite() || !(0.0..=1.0).contains(&score) { + warn!( + score, + submission_id, + "Rejecting review result with invalid score (must be finite and in 0.0..=1.0)" + ); + return false; + } + if let Some(reviews) = self.review_assignments.get_mut(submission_id) { + for review in reviews.iter_mut() { + if review.assigned_validators.contains(validator) { + review.results.insert( + validator.clone(), + ReviewResultEntry { + score, + details, + timestamp: chrono::Utc::now().timestamp_millis(), + }, + ); + self.update_hash(); + return true; + } + } + } + false + } + + pub fn get_review_status(&self, submission_id: &str) -> Option<&Vec> { + self.review_assignments.get(submission_id) + } + + /// Propose agent logs from a validator. + /// + /// Returns `false` and discards the data if: + /// - `logs_data` exceeds `MAX_AGENT_LOG_SIZE` (256 KB) + /// - the total number of proposal entries exceeds `MAX_AGENT_LOG_PROPOSALS` + pub fn propose_agent_logs( + &mut self, + submission_id: &str, + validator: Hotkey, + logs_data: Vec, + ) -> bool { + if logs_data.len() > MAX_AGENT_LOG_SIZE { + warn!( + submission_id = %submission_id, + size = logs_data.len(), + max = MAX_AGENT_LOG_SIZE, + "Rejecting agent log proposal: data exceeds maximum size" + ); + return false; + } + + if self.agent_log_proposals.len() >= MAX_AGENT_LOG_PROPOSALS + && !self.agent_log_proposals.contains_key(submission_id) + { + warn!( + submission_id = %submission_id, + count = self.agent_log_proposals.len(), + max = MAX_AGENT_LOG_PROPOSALS, + "Rejecting agent log proposal: too many pending proposals" + ); + return false; + } + + self.agent_log_proposals + .entry(submission_id.to_string()) + .or_default() + .insert(validator, logs_data); + self.update_hash(); + true + } + + /// Finalize agent logs by consensus (>50% agreement by hash) + pub fn finalize_agent_logs(&mut self, submission_id: &str) -> bool { + let proposals = match self.agent_log_proposals.get(submission_id) { + Some(p) if !p.is_empty() => p, + _ => return false, + }; + + let total_proposals = proposals.len(); + + let mut hash_counts: HashMap<[u8; 32], usize> = HashMap::new(); + let mut hash_to_data: HashMap<[u8; 32], &Vec> = HashMap::new(); + + for logs_data in proposals.values() { + let mut hasher = Sha256::new(); + hasher.update(logs_data); + let hash: [u8; 32] = hasher.finalize().into(); + + *hash_counts.entry(hash).or_default() += 1; + hash_to_data.entry(hash).or_insert(logs_data); + } + + let (best_hash, best_count) = hash_counts + .iter() + .max_by_key(|(_, count)| *count) + .map(|(h, c)| (*h, *c)) + .unwrap_or(([0u8; 32], 0)); + + if best_count > total_proposals / 2 { + if let Some(data) = hash_to_data.get(&best_hash) { + if self.validated_agent_logs.len() >= MAX_VALIDATED_AGENT_LOGS { + warn!( + count = self.validated_agent_logs.len(), + max = MAX_VALIDATED_AGENT_LOGS, + "Validated agent logs at capacity; pruning oldest entries" + ); + let keys_to_remove: Vec = self + .validated_agent_logs + .keys() + .take(self.validated_agent_logs.len() / 10) + .cloned() + .collect(); + for key in keys_to_remove { + self.validated_agent_logs.remove(&key); + } + } + self.validated_agent_logs + .insert(submission_id.to_string(), (*data).clone()); + } + self.agent_log_proposals.remove(submission_id); + self.increment_sequence(); + true + } else { + false + } + } + + /// Register agent code entry + pub fn register_agent_code(&mut self, miner: Hotkey, entry: AgentCodeEntry) { + self.agent_code_registry.insert(miner, entry); + self.increment_sequence(); + } + + /// Get agent code entry for a miner + pub fn get_agent_code_entry(&self, miner: &Hotkey) -> Option<&AgentCodeEntry> { + self.agent_code_registry.get(miner) + } + + /// Add a challenge from a sudo action + pub fn add_challenge_from_sudo( + &mut self, + id: ChallengeId, + name: String, + weight: u16, + creator: Hotkey, + ) { + let config = ChallengeConfig { + id, + name, + weight, + is_active: true, + creator, + created_at: chrono::Utc::now().timestamp_millis(), + }; + self.challenges.insert(id, config); + self.increment_sequence(); + } + + /// Remove a challenge from a sudo action + pub fn remove_challenge_from_sudo(&mut self, challenge_id: &ChallengeId) -> bool { + if let Some(config) = self.challenges.get_mut(challenge_id) { + config.is_active = false; + self.increment_sequence(); + true + } else { + false + } + } + + /// Edit a challenge from a sudo action + pub fn edit_challenge_from_sudo( + &mut self, + challenge_id: &ChallengeId, + name: Option, + weight: Option, + ) -> bool { + if let Some(config) = self.challenges.get_mut(challenge_id) { + if let Some(n) = name { + config.name = n; + } + if let Some(w) = weight { + config.weight = w; + } + self.increment_sequence(); + true + } else { + false + } + } + + /// Stop the network - all emissions go to UID 0 (burn) + pub fn stop_network(&mut self, reason: String) { + self.network_stopped = true; + self.network_stop_reason = Some(reason); + self.increment_sequence(); + } +} + +/// Thread-safe state manager +pub struct StateManager { + state: RwLock, +} + +impl StateManager { + /// Create new state manager + pub fn new(initial_state: ChainState) -> Self { + Self { + state: RwLock::new(initial_state), + } + } + + /// Create with default state for netuid + pub fn for_netuid(netuid: u16) -> Self { + Self::new(ChainState::new(netuid)) + } + + /// Get current sequence number + pub fn sequence(&self) -> SequenceNumber { + self.state.read().sequence + } + + /// Get current state hash + pub fn state_hash(&self) -> [u8; 32] { + self.state.read().state_hash + } + + /// Get current epoch + pub fn epoch(&self) -> u64 { + self.state.read().epoch + } + + /// Get a snapshot of the state + pub fn snapshot(&self) -> ChainState { + self.state.read().clone() + } + + /// Apply a state update + pub fn apply(&self, f: F) -> R + where + F: FnOnce(&mut ChainState) -> R, + { + let mut state = self.state.write(); + f(&mut state) + } + + /// Read-only access to state + pub fn read(&self, f: F) -> R + where + F: FnOnce(&ChainState) -> R, + { + let state = self.state.read(); + f(&state) + } + + /// Verify and apply state from sync + pub fn apply_sync_state(&self, new_state: ChainState) -> Result<(), StateError> { + let mut state = self.state.write(); + + // Only accept state with higher sequence + if new_state.sequence <= state.sequence { + return Err(StateError::SequenceTooOld { + current: state.sequence, + received: new_state.sequence, + }); + } + + // Verify hash matches + let mut verification_state = new_state.clone(); + verification_state.update_hash(); + if verification_state.state_hash != new_state.state_hash { + return Err(StateError::HashMismatch { + expected: hex::encode(new_state.state_hash), + actual: hex::encode(verification_state.state_hash), + }); + } + + info!( + old_seq = state.sequence, + new_seq = new_state.sequence, + "Applying synced state" + ); + *state = new_state; + Ok(()) + } +} + +/// Compute merkle root from leaves +pub fn compute_merkle_root(leaves: &[[u8; 32]]) -> [u8; 32] { + if leaves.is_empty() { + return [0u8; 32]; + } + if leaves.len() == 1 { + return leaves[0]; + } + + let mut level: Vec<[u8; 32]> = leaves.to_vec(); + + while level.len() > 1 { + let mut next_level = Vec::new(); + + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + } + + level[0] +} + +/// Hash two nodes together +fn hash_pair(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(left); + hasher.update(right); + hasher.finalize().into() +} + +/// Verify a merkle proof +pub fn verify_merkle_proof(leaf: &[u8; 32], proof: &MerkleProof) -> bool { + let mut current = *leaf; + + for node in &proof.path { + current = if node.is_left { + hash_pair(&node.sibling_hash, ¤t) + } else { + hash_pair(¤t, &node.sibling_hash) + }; + } + + current == proof.root +} + +/// Build merkle proof for a leaf +pub fn build_merkle_proof(leaves: &[[u8; 32]], leaf_index: usize) -> Option { + if leaf_index >= leaves.len() || leaves.is_empty() { + return None; + } + + let root = compute_merkle_root(leaves); + let mut path = Vec::new(); + let mut level: Vec<[u8; 32]> = leaves.to_vec(); + let mut index = leaf_index; + + while level.len() > 1 { + let sibling_index = if index.is_multiple_of(2) { + if index + 1 < level.len() { + index + 1 + } else { + index + } + } else { + index - 1 + }; + + path.push(MerkleNode { + sibling_hash: level[sibling_index], + is_left: sibling_index < index, + }); + + // Build next level + let mut next_level = Vec::new(); + for chunk in level.chunks(2) { + let combined = if chunk.len() == 2 { + hash_pair(&chunk[0], &chunk[1]) + } else { + hash_pair(&chunk[0], &chunk[0]) + }; + next_level.push(combined); + } + + level = next_level; + index /= 2; + } + + Some(MerkleProof { root, path }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_chain_state_creation() { + let state = ChainState::new(100); + assert_eq!(state.netuid, 100); + assert_eq!(state.sequence, 0); + assert_eq!(state.sudo_key, Hotkey(platform_core::SUDO_KEY_BYTES)); + } + + #[test] + fn test_state_hash_changes() { + let mut state = ChainState::new(100); + let hash1 = state.state_hash; + + state.increment_sequence(); + let hash2 = state.state_hash; + + assert_ne!(hash1, hash2); + } + + #[test] + fn test_validator_updates() { + let mut state = ChainState::new(100); + let hotkey = Hotkey([1u8; 32]); + + state.update_validator(hotkey.clone(), 1_000_000); + assert!(state.validators.contains_key(&hotkey)); + assert_eq!(state.validators.get(&hotkey), Some(&1_000_000)); + + state.remove_validator(&hotkey); + assert!(!state.validators.contains_key(&hotkey)); + } + + #[test] + fn test_challenge_management() { + let mut state = ChainState::new(100); + let config = ChallengeConfig { + id: ChallengeId::new(), + name: "Test Challenge".to_string(), + weight: 50, + is_active: true, + creator: Hotkey([0u8; 32]), + created_at: chrono::Utc::now().timestamp_millis(), + }; + + let id = config.id; + state.add_challenge(config); + assert!(state.challenges.contains_key(&id)); + + state.remove_challenge(&id); + assert!(!state.challenges.contains_key(&id)); + } + + #[test] + fn test_evaluation_flow() { + use platform_core::Keypair; + + let mut state = ChainState::new(100); + + // Add evaluation record + let record = EvaluationRecord { + submission_id: "sub1".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + agent_hash: "abc123".to_string(), + evaluations: HashMap::new(), + aggregated_score: None, + finalized: false, + created_at: chrono::Utc::now().timestamp_millis(), + finalized_at: None, + }; + state.add_evaluation(record); + + // Create a keypair for the validator + let validator_keypair = Keypair::generate(); + let validator = validator_keypair.hotkey(); + state.validators.insert(validator.clone(), 1000); + + // Create signing data for the evaluation + #[derive(serde::Serialize)] + struct EvaluationSigningData<'a> { + submission_id: &'a str, + score: f64, + } + let signing_data = EvaluationSigningData { + submission_id: "sub1", + score: 0.85, + }; + let signing_bytes = bincode::serialize(&signing_data).unwrap(); + let signature = validator_keypair.sign_bytes(&signing_bytes).unwrap(); + + let eval = ValidatorEvaluation { + score: 0.85, + stake: 1000, + timestamp: chrono::Utc::now().timestamp_millis(), + signature: signature.clone(), + }; + state + .add_validator_evaluation("sub1", validator, eval, &signature) + .unwrap(); + + // Finalize + let score = state.finalize_evaluation("sub1").unwrap(); + assert!((score - 0.85).abs() < 0.01); + } + + #[test] + fn test_merkle_proof() { + let leaves: Vec<[u8; 32]> = (0..4).map(|i| [i as u8; 32]).collect(); + + let root = compute_merkle_root(&leaves); + assert_ne!(root, [0u8; 32]); + + // Build and verify proof for each leaf + for i in 0..leaves.len() { + let proof = build_merkle_proof(&leaves, i).unwrap(); + assert!(verify_merkle_proof(&leaves[i], &proof)); + } + } + + #[test] + fn test_state_manager() { + let manager = StateManager::for_netuid(100); + + assert_eq!(manager.sequence(), 0); + + manager.apply(|state| { + state.update_validator(Hotkey([1u8; 32]), 1000); + }); + + assert_eq!(manager.sequence(), 1); + } + + #[test] + fn test_state_serialization() { + let state = ChainState::new(100); + let bytes = state.to_bytes().unwrap(); + let recovered = ChainState::from_bytes(&bytes).unwrap(); + + assert_eq!(state.sequence, recovered.sequence); + assert_eq!(state.netuid, recovered.netuid); + } + + #[test] + fn test_weight_voting() { + use platform_core::Keypair; + + let mut state = ChainState::new(100); + + // Create keypairs for validators + let validator1_keypair = Keypair::generate(); + let validator2_keypair = Keypair::generate(); + let validator1 = validator1_keypair.hotkey(); + let validator2 = validator2_keypair.hotkey(); + + // Add validators with stakes + state.validators.insert(validator1.clone(), 1000); + state.validators.insert(validator2.clone(), 2000); + + // Create signing data structure for weight votes + #[derive(serde::Serialize)] + struct WeightVoteSigningData { + epoch: u64, + netuid: u16, + weights: Vec<(u16, u16)>, + } + + // Create and sign weight vote for validator1 + let weights1 = vec![(0, 100), (1, 200)]; + let signing_data1 = WeightVoteSigningData { + epoch: 1, + netuid: state.netuid, + weights: weights1.clone(), + }; + let signing_bytes1 = bincode::serialize(&signing_data1).unwrap(); + let signature1 = validator1_keypair.sign_bytes(&signing_bytes1).unwrap(); + + // Create and sign weight vote for validator2 + let weights2 = vec![(0, 150), (1, 100)]; + let signing_data2 = WeightVoteSigningData { + epoch: 1, + netuid: state.netuid, + weights: weights2.clone(), + }; + let signing_bytes2 = bincode::serialize(&signing_data2).unwrap(); + let signature2 = validator2_keypair.sign_bytes(&signing_bytes2).unwrap(); + + // Add weight votes with signature verification + state + .add_weight_vote_verified(validator1, weights1, 1, &signature1) + .expect("Failed to add weight vote for validator1"); + state + .add_weight_vote_verified(validator2, weights2, 1, &signature2) + .expect("Failed to add weight vote for validator2"); + + // Finalize + let weights = state.finalize_weights().unwrap(); + assert!(!weights.is_empty()); + assert!(state.weight_votes.as_ref().unwrap().finalized); + } + + #[test] + fn test_epoch_transition() { + let mut state = ChainState::new(100); + assert_eq!(state.epoch, 0); + + state.next_epoch(); + assert_eq!(state.epoch, 1); + } +} diff --git a/crates/p2p-consensus/src/validator.rs b/crates/p2p-consensus/src/validator.rs new file mode 100644 index 000000000..e56992caf --- /dev/null +++ b/crates/p2p-consensus/src/validator.rs @@ -0,0 +1,622 @@ +//! Validator management for P2P consensus +//! +//! Tracks active validators, their stakes, and handles leader election +//! using round-robin based on stake-weighted ordering. + +use crate::messages::{SequenceNumber, ViewNumber}; +use parking_lot::RwLock; +use platform_core::{Hotkey, Keypair, SignedMessage}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::sync::Arc; +use thiserror::Error; +use tracing::{debug, info, warn}; + +/// Errors related to validator operations +#[derive(Error, Debug)] +pub enum ValidatorError { + #[error("Validator not found: {0}")] + NotFound(String), + #[error("Insufficient stake: required {required}, has {actual}")] + InsufficientStake { required: u64, actual: u64 }, + #[error("Invalid signature from validator")] + InvalidSignature, + #[error("Validator already registered")] + AlreadyRegistered, + #[error("Not authorized: {0}")] + NotAuthorized(String), +} + +/// Information about a validator in the P2P network +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorRecord { + /// Validator's hotkey + pub hotkey: Hotkey, + /// Current stake in RAO + pub stake: u64, + /// libp2p peer ID + pub peer_id: Option, + /// Multiaddresses where validator can be reached + pub addresses: Vec, + /// Last heartbeat timestamp (unix millis) + pub last_seen: i64, + /// Current state hash reported by validator + pub state_hash: [u8; 32], + /// Last sequence number seen + pub last_sequence: SequenceNumber, + /// Whether validator is currently active (responding to heartbeats) + pub is_active: bool, + /// Protocol version + pub protocol_version: String, +} + +impl ValidatorRecord { + /// Create a new validator record + pub fn new(hotkey: Hotkey, stake: u64) -> Self { + Self { + hotkey, + stake, + peer_id: None, + addresses: vec![], + last_seen: chrono::Utc::now().timestamp_millis(), + state_hash: [0u8; 32], + last_sequence: 0, + is_active: true, + protocol_version: String::new(), + } + } + + /// Update from heartbeat + pub fn update_from_heartbeat( + &mut self, + state_hash: [u8; 32], + sequence: SequenceNumber, + stake: u64, + ) { + self.state_hash = state_hash; + self.last_sequence = sequence; + self.stake = stake; + self.last_seen = chrono::Utc::now().timestamp_millis(); + self.is_active = true; + } + + /// Check if validator is stale (no heartbeat in threshold) + pub fn is_stale(&self, threshold_ms: i64) -> bool { + let now = chrono::Utc::now().timestamp_millis(); + (now - self.last_seen) > threshold_ms + } +} + +/// Manages the validator set for consensus +pub struct ValidatorSet { + /// Map of hotkey to validator record + validators: RwLock>, + /// Minimum stake required (in RAO) + min_stake: u64, + /// Our own keypair + local_keypair: Keypair, + /// Stale threshold in milliseconds + stale_threshold_ms: i64, + /// Verified stakes from on-chain data (set by caller who queries chain) + /// Key: validator hotkey, Value: verified stake amount in RAO + /// The caller is responsible for periodically querying on-chain data + /// (e.g., via Bittensor metagraph) and updating these values. + verified_stakes: RwLock>, +} + +impl ValidatorSet { + /// Create a new validator set + pub fn new(local_keypair: Keypair, min_stake: u64) -> Self { + Self { + validators: RwLock::new(HashMap::new()), + min_stake, + local_keypair, + stale_threshold_ms: 90_000, // 90 seconds (3x heartbeat interval) + verified_stakes: RwLock::new(HashMap::new()), + } + } + + /// Set verified stake for a validator from on-chain data + /// + /// This should be called by the caller (e.g., validator node) after + /// querying on-chain data (Bittensor metagraph) to establish trusted + /// stake values. Self-reported stakes from heartbeats are only accepted + /// if they match the verified stake or if no verified stake exists yet. + pub fn set_verified_stake(&self, hotkey: &Hotkey, stake: u64) { + self.verified_stakes.write().insert(hotkey.clone(), stake); + debug!(hotkey = %hotkey.to_hex(), stake, "Set verified stake from on-chain data"); + } + + /// Get verified stake for a validator + pub fn get_verified_stake(&self, hotkey: &Hotkey) -> Option { + self.verified_stakes.read().get(hotkey).copied() + } + + /// Clear all verified stakes (useful when re-syncing from chain) + pub fn clear_verified_stakes(&self) { + self.verified_stakes.write().clear(); + } + + /// Get our local hotkey + pub fn local_hotkey(&self) -> Hotkey { + self.local_keypair.hotkey() + } + + /// Sign a message with our local keypair + pub fn sign(&self, message: &[u8]) -> SignedMessage { + self.local_keypair.sign(message) + } + + /// Sign arbitrary bytes and return signature + pub fn sign_bytes(&self, data: &[u8]) -> Result, platform_core::MiniChainError> { + self.local_keypair.sign_bytes(data) + } + + /// Register or update a validator + pub fn register_validator(&self, record: ValidatorRecord) -> Result<(), ValidatorError> { + if record.stake < self.min_stake { + return Err(ValidatorError::InsufficientStake { + required: self.min_stake, + actual: record.stake, + }); + } + + let mut validators = self.validators.write(); + let hotkey_str = record.hotkey.to_hex(); + + if let Some(existing) = validators.get_mut(&record.hotkey) { + debug!(hotkey = %hotkey_str, "Updating existing validator"); + existing.stake = record.stake; + existing.peer_id = record.peer_id.or(existing.peer_id.clone()); + if !record.addresses.is_empty() { + existing.addresses = record.addresses; + } + existing.last_seen = record.last_seen; + existing.is_active = true; + } else { + info!(hotkey = %hotkey_str, stake = record.stake, "Registering new validator"); + validators.insert(record.hotkey.clone(), record); + } + + Ok(()) + } + + /// Remove a validator + pub fn remove_validator(&self, hotkey: &Hotkey) { + let mut validators = self.validators.write(); + if validators.remove(hotkey).is_some() { + info!(hotkey = %hotkey.to_hex(), "Removed validator"); + } + } + + /// Get a validator by hotkey + pub fn get_validator(&self, hotkey: &Hotkey) -> Option { + self.validators.read().get(hotkey).cloned() + } + + /// Check if a hotkey is a registered validator + pub fn is_validator(&self, hotkey: &Hotkey) -> bool { + self.validators.read().contains_key(hotkey) + } + + /// Get all active validators + pub fn active_validators(&self) -> Vec { + self.validators + .read() + .values() + .filter(|v| v.is_active && !v.is_stale(self.stale_threshold_ms)) + .cloned() + .collect() + } + + /// Get count of active validators + pub fn active_count(&self) -> usize { + self.validators + .read() + .values() + .filter(|v| v.is_active && !v.is_stale(self.stale_threshold_ms)) + .count() + } + + /// Get total stake of active validators + pub fn total_active_stake(&self) -> u64 { + self.validators + .read() + .values() + .filter(|v| v.is_active && !v.is_stale(self.stale_threshold_ms)) + .map(|v| v.stake) + .sum() + } + + /// Get stake for a validator (0 if unknown or inactive) + pub fn stake_for(&self, hotkey: &Hotkey) -> u64 { + self.validators + .read() + .get(hotkey) + .filter(|v| v.is_active && !v.is_stale(self.stale_threshold_ms)) + .map(|v| v.stake) + .unwrap_or(0) + } + + /// Calculate stake-weighted quorum threshold (2/3 of total stake + 1) + pub fn stake_quorum_threshold(&self) -> u64 { + let total = self.total_active_stake(); + if total == 0 { + 0 + } else { + (total.saturating_mul(2) / 3).saturating_add(1) + } + } + + /// Update validator from heartbeat + /// + /// Self-reported stake is only accepted if it matches the verified stake + /// (set via `set_verified_stake`) or if no verified stake exists yet. + /// This prevents validators from falsely inflating their stake in heartbeats. + pub fn update_from_heartbeat( + &self, + hotkey: &Hotkey, + state_hash: [u8; 32], + sequence: SequenceNumber, + reported_stake: u64, + ) -> Result<(), ValidatorError> { + // Determine the stake to use: prefer verified stake over self-reported + let stake_to_use = { + let verified = self.verified_stakes.read(); + if let Some(&verified_stake) = verified.get(hotkey) { + // Only accept self-reported stake if it matches verified stake + if reported_stake != verified_stake { + warn!( + hotkey = %hotkey.to_hex(), + reported = reported_stake, + verified = verified_stake, + "Heartbeat stake mismatch, using verified stake" + ); + } + verified_stake + } else { + // No verified stake yet, accept self-reported for now + // Caller should eventually verify this against on-chain data + reported_stake + } + }; + + let mut validators = self.validators.write(); + if let Some(validator) = validators.get_mut(hotkey) { + validator.update_from_heartbeat(state_hash, sequence, stake_to_use); + Ok(()) + } else { + // Auto-register if stake meets minimum + if stake_to_use >= self.min_stake { + let mut record = ValidatorRecord::new(hotkey.clone(), stake_to_use); + record.update_from_heartbeat(state_hash, sequence, stake_to_use); + drop(validators); + return self.register_validator(record); + } + Err(ValidatorError::NotFound(hotkey.to_hex())) + } + } + + /// Mark stale validators as inactive + pub fn mark_stale_validators(&self) { + let mut validators = self.validators.write(); + for validator in validators.values_mut() { + if validator.is_stale(self.stale_threshold_ms) && validator.is_active { + warn!( + hotkey = %validator.hotkey.to_hex(), + last_seen = validator.last_seen, + "Marking validator as inactive (stale)" + ); + validator.is_active = false; + } + } + } + + /// Get sorted validators by stake (descending) for leader election + pub fn validators_by_stake(&self) -> Vec { + let mut validators: Vec<_> = self + .validators + .read() + .values() + .filter(|v| v.is_active && !v.is_stale(self.stale_threshold_ms)) + .cloned() + .collect(); + validators.sort_by(|a, b| { + b.stake + .cmp(&a.stake) + .then_with(|| a.hotkey.0.cmp(&b.hotkey.0)) + }); + validators + } + + /// Calculate PBFT fault tolerance threshold (f) + /// Byzantine fault tolerance: n = 3f + 1, so f = (n - 1) / 3 + pub fn fault_tolerance(&self) -> usize { + let n = self.active_count(); + if n == 0 { + return 0; + } + (n - 1) / 3 + } + + /// Calculate quorum size for consensus (2f + 1) + pub fn quorum_size(&self) -> usize { + let f = self.fault_tolerance(); + 2 * f + 1 + } + + /// Verify a signature from a validator + pub fn verify_signature( + &self, + hotkey: &Hotkey, + message: &[u8], + signature: &[u8], + ) -> Result { + // Verify the validator is registered + if !self.is_validator(hotkey) { + return Err(ValidatorError::NotFound(hotkey.to_hex())); + } + + let signed_msg = SignedMessage { + message: message.to_vec(), + signature: signature.to_vec(), + signer: hotkey.clone(), + }; + + match signed_msg.verify() { + Ok(valid) => Ok(valid), + Err(_) => Err(ValidatorError::InvalidSignature), + } + } +} + +/// Leader election based on round-robin with stake ordering +pub struct LeaderElection { + /// Reference to validator set + validator_set: Arc, +} + +impl LeaderElection { + /// Create new leader election + pub fn new(validator_set: Arc) -> Self { + Self { validator_set } + } + + /// Get the leader for a given view number + pub fn leader_for_view(&self, view: ViewNumber) -> Option { + let validators = self.validator_set.validators_by_stake(); + if validators.is_empty() { + return None; + } + let index = (view as usize) % validators.len(); + Some(validators[index].hotkey.clone()) + } + + /// Check if a hotkey is the leader for a given view + pub fn is_leader(&self, hotkey: &Hotkey, view: ViewNumber) -> bool { + self.leader_for_view(view) + .map(|leader| leader == *hotkey) + .unwrap_or(false) + } + + /// Check if we are the leader for a given view + pub fn am_i_leader(&self, view: ViewNumber) -> bool { + self.is_leader(&self.validator_set.local_hotkey(), view) + } + + /// Get the next view number where we would be leader + pub fn next_leader_view(&self, current_view: ViewNumber) -> Option { + let validators = self.validator_set.validators_by_stake(); + let local_hotkey = self.validator_set.local_hotkey(); + + let our_position = validators.iter().position(|v| v.hotkey == local_hotkey)?; + + let validator_count = validators.len(); + let current_position = (current_view as usize) % validator_count; + + if our_position >= current_position { + Some(current_view + (our_position - current_position) as u64) + } else { + Some(current_view + (validator_count - current_position + our_position) as u64) + } + } +} + +/// Stake-weighted voting for consensus +pub struct StakeWeightedVoting { + /// Reference to validator set + validator_set: Arc, +} + +impl StakeWeightedVoting { + /// Create new stake-weighted voting + pub fn new(validator_set: Arc) -> Self { + Self { validator_set } + } + + /// Calculate the voting power of a validator (normalized 0-1) + pub fn voting_power(&self, hotkey: &Hotkey) -> f64 { + let total_stake = self.validator_set.total_active_stake(); + if total_stake == 0 { + return 0.0; + } + + self.validator_set + .get_validator(hotkey) + .filter(|v| v.is_active) + .map(|v| v.stake as f64 / total_stake as f64) + .unwrap_or(0.0) + } + + /// Calculate total voting power for a set of validators + pub fn total_voting_power(&self, hotkeys: &[Hotkey]) -> f64 { + let total_stake = self.validator_set.total_active_stake(); + if total_stake == 0 { + return 0.0; + } + + let voters_stake: u64 = hotkeys + .iter() + .filter_map(|h| self.validator_set.get_validator(h)) + .filter(|v| v.is_active) + .map(|v| v.stake) + .sum(); + + voters_stake as f64 / total_stake as f64 + } + + /// Check if votes meet the required threshold + pub fn meets_threshold(&self, voters: &[Hotkey], threshold: f64) -> bool { + self.total_voting_power(voters) >= threshold + } + + /// Check if votes meet 2f+1 quorum (by count, not stake) + pub fn meets_quorum(&self, voter_count: usize) -> bool { + voter_count >= self.validator_set.quorum_size() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test_keypair() -> Keypair { + Keypair::generate() + } + + fn create_validator_set() -> ValidatorSet { + let keypair = create_test_keypair(); + ValidatorSet::new(keypair, 1000) + } + + #[test] + fn test_validator_record_creation() { + let hotkey = Hotkey([1u8; 32]); + let record = ValidatorRecord::new(hotkey.clone(), 1_000_000); + assert_eq!(record.hotkey, hotkey); + assert_eq!(record.stake, 1_000_000); + assert!(record.is_active); + } + + #[test] + fn test_validator_set_registration() { + let set = create_validator_set(); + let record = ValidatorRecord::new(Hotkey([1u8; 32]), 10_000); + + set.register_validator(record.clone()).unwrap(); + assert!(set.is_validator(&record.hotkey)); + } + + #[test] + fn test_insufficient_stake() { + let set = create_validator_set(); + let record = ValidatorRecord::new(Hotkey([1u8; 32]), 500); // Below min_stake + + let result = set.register_validator(record); + assert!(matches!( + result, + Err(ValidatorError::InsufficientStake { .. }) + )); + } + + #[test] + fn test_fault_tolerance() { + let set = create_validator_set(); + + // Add 4 validators (n=4, f=1) + for i in 0..4 { + let mut bytes = [0u8; 32]; + bytes[0] = i; + let record = ValidatorRecord::new(Hotkey(bytes), 10_000); + set.register_validator(record).unwrap(); + } + + assert_eq!(set.fault_tolerance(), 1); + assert_eq!(set.quorum_size(), 3); + } + + #[test] + fn test_leader_election() { + let keypair = create_test_keypair(); + let set = Arc::new(ValidatorSet::new(keypair, 1000)); + + // Add validators with different stakes + for i in 0..3 { + let mut bytes = [0u8; 32]; + bytes[0] = i; + let record = ValidatorRecord::new(Hotkey(bytes), 10_000 - i as u64 * 1000); + set.register_validator(record).unwrap(); + } + + let election = LeaderElection::new(set); + + // Leader should cycle through validators + let leader0 = election.leader_for_view(0).unwrap(); + let leader1 = election.leader_for_view(1).unwrap(); + let leader3 = election.leader_for_view(3).unwrap(); + + // View 0 and View 3 should have same leader (3 validators) + assert_eq!(leader0, leader3); + assert_ne!(leader0, leader1); + } + + #[test] + fn test_stake_weighted_voting() { + let keypair = create_test_keypair(); + let set = Arc::new(ValidatorSet::new(keypair, 1000)); + + // Add validators: one with 7000 stake, one with 3000 + let mut bytes1 = [0u8; 32]; + bytes1[0] = 1; + let record1 = ValidatorRecord::new(Hotkey(bytes1), 7000); + set.register_validator(record1.clone()).unwrap(); + + let mut bytes2 = [0u8; 32]; + bytes2[0] = 2; + let record2 = ValidatorRecord::new(Hotkey(bytes2), 3000); + set.register_validator(record2.clone()).unwrap(); + + let voting = StakeWeightedVoting::new(set); + + // Validator 1 should have 70% voting power + let power1 = voting.voting_power(&record1.hotkey); + assert!((power1 - 0.7).abs() < 0.01); + + // Validator 2 should have 30% voting power + let power2 = voting.voting_power(&record2.hotkey); + assert!((power2 - 0.3).abs() < 0.01); + + // Together they have 100% + assert!(voting.meets_threshold(&[record1.hotkey, record2.hotkey], 0.99)); + } + + #[test] + fn test_validator_staleness() { + let record = ValidatorRecord::new(Hotkey([1u8; 32]), 10_000); + + // New record should not be stale + assert!(!record.is_stale(90_000)); + + // Record with old timestamp should be stale + let mut old_record = record; + old_record.last_seen = chrono::Utc::now().timestamp_millis() - 100_000; + assert!(old_record.is_stale(90_000)); + } + + #[test] + fn test_update_from_heartbeat() { + let set = create_validator_set(); + let hotkey = Hotkey([1u8; 32]); + let record = ValidatorRecord::new(hotkey.clone(), 10_000); + set.register_validator(record).unwrap(); + + let new_hash = [42u8; 32]; + set.update_from_heartbeat(&hotkey, new_hash, 100, 15_000) + .unwrap(); + + let updated = set.get_validator(&hotkey).unwrap(); + assert_eq!(updated.state_hash, new_hash); + assert_eq!(updated.last_sequence, 100); + assert_eq!(updated.stake, 15_000); + } +} diff --git a/crates/rpc-server/Cargo.toml b/crates/rpc-server/Cargo.toml new file mode 100644 index 000000000..8e3e66344 --- /dev/null +++ b/crates/rpc-server/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "platform-rpc" +version.workspace = true +edition.workspace = true +description = "HTTP RPC server for Mini-Chain validators" + +[dependencies] +platform-core = { path = "../core" } +platform-challenge-sdk = { path = "../challenge-sdk" } +platform-subnet-manager = { path = "../subnet-manager" } + +# HTTP Server +axum = { version = "0.7", features = ["json", "tokio"] } +tower = "0.4" +tower-http = { version = "0.5", features = ["cors", "trace"] } + +# Async +tokio = { workspace = true } +async-trait = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Crypto +sp-core = { version = "31.0", default-features = false, features = ["std"] } +hex = { workspace = true } + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +parking_lot = { workspace = true } +uuid = { workspace = true } +chrono = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } +reqwest = { version = "0.12", features = ["json"] } diff --git a/crates/rpc-server/src/auth.rs b/crates/rpc-server/src/auth.rs new file mode 100644 index 000000000..5674e762a --- /dev/null +++ b/crates/rpc-server/src/auth.rs @@ -0,0 +1,175 @@ +//! Authentication for RPC requests +//! +//! Validators authenticate using their hotkey signature (sr25519). + +use platform_core::Hotkey; +use sp_core::{crypto::Pair as _, sr25519}; +use tracing::warn; + +/// Verify a signed message from a validator (sr25519) +pub fn verify_validator_signature( + hotkey_hex: &str, + message: &str, + signature_hex: &str, +) -> Result { + // Parse hotkey + let hotkey = Hotkey::from_hex(hotkey_hex).ok_or(AuthError::InvalidHotkey)?; + + // Parse signature + let signature_bytes = hex::decode(signature_hex).map_err(|_| AuthError::InvalidSignature)?; + + if signature_bytes.len() != 64 { + return Err(AuthError::InvalidSignature); + } + + // Verify using sr25519 + let mut sig_bytes = [0u8; 64]; + sig_bytes.copy_from_slice(&signature_bytes); + let signature = sr25519::Signature::from_raw(sig_bytes); + + let public = sr25519::Public::from_raw(hotkey.0); + let is_valid = sr25519::Pair::verify(&signature, message.as_bytes(), &public); + + if !is_valid { + warn!("Invalid signature for hotkey: {}", &hotkey_hex[..16]); + } + + Ok(is_valid) +} + +/// Create a message for signing +pub fn create_auth_message(action: &str, timestamp: i64, nonce: &str) -> String { + format!("{}:{}:{}", action, timestamp, nonce) +} + +/// Verify message is recent (within 5 minutes) +pub fn verify_timestamp(timestamp: i64) -> bool { + let now = chrono::Utc::now().timestamp(); + let diff = (now - timestamp).abs(); + diff < 300 // 5 minutes +} + +#[derive(Debug, thiserror::Error)] +pub enum AuthError { + #[error("Invalid hotkey format")] + InvalidHotkey, + + #[error("Invalid signature format")] + InvalidSignature, + + #[error("Signature verification failed")] + VerificationFailed, + + #[error("Message expired")] + MessageExpired, +} + +#[cfg(test)] +mod tests { + use super::*; + use platform_core::Keypair; + + #[test] + fn test_create_auth_message() { + let msg = create_auth_message("register", 1234567890, "abc123"); + assert_eq!(msg, "register:1234567890:abc123"); + } + + #[test] + fn test_verify_timestamp() { + let now = chrono::Utc::now().timestamp(); + assert!(verify_timestamp(now)); + assert!(verify_timestamp(now - 60)); // 1 minute ago + assert!(!verify_timestamp(now - 600)); // 10 minutes ago + } + + #[test] + fn test_signature_verification() { + let kp = Keypair::generate(); + let message = "test:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + + let hotkey_hex = kp.hotkey().to_hex(); + let sig_hex = hex::encode(&signed.signature); + + let result = verify_validator_signature(&hotkey_hex, message, &sig_hex); + assert!(result.is_ok()); + assert!(result.unwrap()); + } + + #[test] + fn test_signature_verification_invalid_hotkey() { + let result = verify_validator_signature("invalid_hotkey", "message", "signature"); + assert!(result.is_err()); + } + + #[test] + fn test_signature_verification_invalid_signature_hex() { + let kp = Keypair::generate(); + let result = verify_validator_signature(&kp.hotkey().to_hex(), "message", "not_hex"); + assert!(result.is_err()); + } + + #[test] + fn test_signature_verification_wrong_signature() { + let kp1 = Keypair::generate(); + let kp2 = Keypair::generate(); + let message = "test:1234567890:nonce"; + let signed = kp1.sign(message.as_bytes()); + + // Use kp2's hotkey but kp1's signature - should fail + let hotkey_hex = kp2.hotkey().to_hex(); + let sig_hex = hex::encode(&signed.signature); + + let result = verify_validator_signature(&hotkey_hex, message, &sig_hex); + assert!(result.is_ok()); + assert!(!result.unwrap()); // Signature doesn't match + } + + #[test] + fn test_signature_verification_wrong_message() { + let kp = Keypair::generate(); + let message1 = "test:1234567890:nonce1"; + let message2 = "test:1234567890:nonce2"; + let signed = kp.sign(message1.as_bytes()); + + let hotkey_hex = kp.hotkey().to_hex(); + let sig_hex = hex::encode(&signed.signature); + + // Try to verify with different message - should fail + let result = verify_validator_signature(&hotkey_hex, message2, &sig_hex); + assert!(result.is_ok()); + assert!(!result.unwrap()); + } + + #[test] + fn test_verify_timestamp_edge_case() { + let now = chrono::Utc::now().timestamp(); + // Test exactly at 5 minute boundary + assert!(!verify_timestamp(now - 301)); // 5 minutes 1 second ago + assert!(verify_timestamp(now - 299)); // 4 minutes 59 seconds ago + } + + #[test] + fn test_verify_timestamp_future() { + let now = chrono::Utc::now().timestamp(); + assert!(verify_timestamp(now + 10)); // Future timestamp within 5 min should be valid + assert!(verify_timestamp(now + 299)); // Just under 5 minutes in future + } + + #[test] + fn test_signature_verification_invalid_length() { + let kp = Keypair::generate(); + let message = "test:1234567890:nonce"; + + // Test with signature that's too short (not 64 bytes) + let short_sig = hex::encode([0u8; 32]); // Only 32 bytes + let result = verify_validator_signature(&kp.hotkey().to_hex(), message, &short_sig); + assert!(result.is_err()); + + // Test with signature that's too long + let long_sig = hex::encode([0u8; 128]); // 128 bytes + let result = verify_validator_signature(&kp.hotkey().to_hex(), message, &long_sig); + assert!(result.is_err()); + } +} diff --git a/crates/rpc-server/src/handlers.rs b/crates/rpc-server/src/handlers.rs new file mode 100644 index 000000000..d8d39ffae --- /dev/null +++ b/crates/rpc-server/src/handlers.rs @@ -0,0 +1,1199 @@ +//! RPC request handlers + +use crate::auth::*; +use crate::types::*; +use axum::{ + extract::{Path, Query, State}, + http::StatusCode, + Json, +}; +use parking_lot::RwLock; +use platform_core::{ChainState, Hotkey, JobStatus, Stake, ValidatorInfo}; +use platform_subnet_manager::BanList; +use std::sync::Arc; +use std::time::Instant; +use tracing::info; + +/// Shared state for RPC handlers +pub struct RpcState { + pub chain_state: Arc>, + pub bans: Arc>, + pub start_time: Instant, + pub version: String, + pub netuid: u16, + pub name: String, + pub min_stake: u64, +} + +impl RpcState { + pub fn new( + chain_state: Arc>, + bans: Arc>, + netuid: u16, + name: String, + min_stake: u64, + ) -> Self { + Self { + chain_state, + bans, + start_time: Instant::now(), + version: env!("CARGO_PKG_VERSION").to_string(), + netuid, + name, + min_stake, + } + } +} + +/// GET /health +pub async fn health_handler( + State(state): State>, +) -> Json> { + Json(RpcResponse::ok(HealthResponse { + status: "healthy".to_string(), + version: state.version.clone(), + uptime_secs: state.start_time.elapsed().as_secs(), + })) +} + +/// GET /status +pub async fn status_handler( + State(state): State>, +) -> Json> { + let chain = state.chain_state.read(); + + Json(RpcResponse::ok(StatusResponse { + netuid: state.netuid, + name: state.name.clone(), + version: state.version.clone(), + block_height: chain.block_height, + epoch: chain.epoch, + validators_count: chain.validators.len(), + challenges_count: chain.challenges.len(), + pending_jobs: chain.pending_jobs.len(), + is_paused: false, + })) +} + +/// GET /validators +pub async fn validators_handler( + State(state): State>, + Query(params): Query, +) -> Json>> { + let chain = state.chain_state.read(); + + let offset = params.offset.unwrap_or(0); + let limit = params.limit.unwrap_or(100).min(1000); + + let validators: Vec = chain + .validators + .values() + .skip(offset) + .take(limit) + .map(|v| ValidatorResponse { + hotkey: v.hotkey.to_hex(), + stake: v.stake.0, + stake_tao: v.stake.as_tao(), + is_active: v.is_active, + last_seen: v.last_seen, + peer_id: v.peer_id.clone(), + x25519_pubkey: v.x25519_pubkey.clone(), + }) + .collect(); + + Json(RpcResponse::ok(validators)) +} + +/// GET /challenges +pub async fn challenges_handler( + State(state): State>, +) -> Json>> { + let chain = state.chain_state.read(); + + let challenges: Vec = chain + .challenges + .values() + .map(|c| ChallengeResponse { + id: c.id.to_string(), + name: c.name.clone(), + description: c.description.clone(), + code_hash: c.code_hash.clone(), + is_active: c.is_active, + emission_weight: c.config.emission_weight, + timeout_secs: c.config.timeout_secs, + }) + .collect(); + + Json(RpcResponse::ok(challenges)) +} + +/// GET /challenge/:id +pub async fn challenge_handler( + State(state): State>, + Path(id): Path, +) -> Result>, StatusCode> { + let chain = state.chain_state.read(); + + // Find challenge by ID string + let challenge = chain + .challenges + .values() + .find(|c| c.id.to_string() == id || c.name == id); + + match challenge { + Some(c) => Ok(Json(RpcResponse::ok(ChallengeResponse { + id: c.id.to_string(), + name: c.name.clone(), + description: c.description.clone(), + code_hash: c.code_hash.clone(), + is_active: c.is_active, + emission_weight: c.config.emission_weight, + timeout_secs: c.config.timeout_secs, + }))), + None => Err(StatusCode::NOT_FOUND), + } +} + +/// POST /register +pub async fn register_handler( + State(state): State>, + Json(req): Json, +) -> Json> { + // Verify signature + match verify_validator_signature(&req.hotkey, &req.message, &req.signature) { + Ok(true) => {} + Ok(false) => { + return Json(RpcResponse::ok(RegisterResponse { + accepted: false, + uid: None, + reason: Some("Invalid signature".to_string()), + })); + } + Err(e) => { + return Json(RpcResponse::ok(RegisterResponse { + accepted: false, + uid: None, + reason: Some(format!("Auth error: {}", e)), + })); + } + } + + // Parse hotkey + let hotkey = match Hotkey::from_hex(&req.hotkey) { + Some(h) => h, + None => { + return Json(RpcResponse::ok(RegisterResponse { + accepted: false, + uid: None, + reason: Some("Invalid hotkey format".to_string()), + })); + } + }; + + // Check if banned + let bans = state.bans.read(); + if bans.is_validator_banned(&hotkey) { + return Json(RpcResponse::ok(RegisterResponse { + accepted: false, + uid: None, + reason: Some("Validator is banned".to_string()), + })); + } + drop(bans); + + // Check if already registered + let chain = state.chain_state.read(); + if chain.validators.contains_key(&hotkey) { + return Json(RpcResponse::ok(RegisterResponse { + accepted: true, + uid: Some(0), + reason: Some("Already registered".to_string()), + })); + } + drop(chain); + + // Register with minimum stake (actual stake will be synced from Bittensor) + let info = ValidatorInfo::new(hotkey.clone(), Stake::new(state.min_stake)); + + let mut chain = state.chain_state.write(); + match chain.add_validator(info) { + Ok(_) => { + info!("Validator registered via RPC: {}", req.hotkey); + Json(RpcResponse::ok(RegisterResponse { + accepted: true, + uid: Some(0), + reason: None, + })) + } + Err(e) => Json(RpcResponse::ok(RegisterResponse { + accepted: false, + uid: None, + reason: Some(format!("Registration failed: {}", e)), + })), + } +} + +/// POST /heartbeat +pub async fn heartbeat_handler( + State(state): State>, + Json(req): Json, +) -> Json> { + // Parse hotkey + let hotkey = match Hotkey::from_hex(&req.hotkey) { + Some(h) => h, + None => { + return Json(RpcResponse::error("Invalid hotkey")); + } + }; + + // Update last_seen + let mut chain = state.chain_state.write(); + if let Some(validator) = chain.validators.get_mut(&hotkey) { + validator.last_seen = chrono::Utc::now(); + if let Some(peer_id) = req.peer_id { + validator.peer_id = Some(peer_id); + } + + Json(RpcResponse::ok(HeartbeatResponse { + accepted: true, + current_block: chain.block_height, + current_epoch: chain.epoch, + next_sync_block: None, + })) + } else { + Json(RpcResponse::ok(HeartbeatResponse { + accepted: false, + current_block: chain.block_height, + current_epoch: chain.epoch, + next_sync_block: None, + })) + } +} + +/// GET /jobs +pub async fn jobs_handler( + State(state): State>, + Query(params): Query, +) -> Json>> { + let chain = state.chain_state.read(); + + let offset = params.offset.unwrap_or(0); + let limit = params.limit.unwrap_or(100).min(1000); + + let jobs: Vec = chain + .pending_jobs + .iter() + .skip(offset) + .take(limit) + .map(|j| JobResponse { + id: j.id.to_string(), + challenge_id: j.challenge_id.to_string(), + agent_hash: j.agent_hash.clone(), + status: format!("{:?}", j.status), + created_at: j.created_at, + assigned_validator: j.assigned_validator.as_ref().map(|h| h.to_hex()), + }) + .collect(); + + Json(RpcResponse::ok(jobs)) +} + +/// POST /jobs/:id/result +pub async fn job_result_handler( + State(state): State>, + Path(job_id): Path, + Json(req): Json, +) -> Json> { + // Verify signature + let msg = format!("result:{}:{}", job_id, req.score); + match verify_validator_signature(&req.hotkey, &msg, &req.signature) { + Ok(true) => {} + _ => { + return Json(RpcResponse::ok(JobResultResponse { + accepted: false, + job_id: job_id.clone(), + })); + } + } + + // Parse job ID + let job_uuid = match uuid::Uuid::parse_str(&job_id) { + Ok(u) => u, + Err(_) => { + return Json(RpcResponse::error("Invalid job ID")); + } + }; + + // Find and update job + let mut chain = state.chain_state.write(); + if let Some(job) = chain.pending_jobs.iter_mut().find(|j| j.id == job_uuid) { + job.status = JobStatus::Completed; + job.result = Some(platform_core::Score::new(req.score, 1.0)); + + info!("Job result submitted: {} score={}", job_id, req.score); + Json(RpcResponse::ok(JobResultResponse { + accepted: true, + job_id, + })) + } else { + Json(RpcResponse::ok(JobResultResponse { + accepted: false, + job_id, + })) + } +} + +/// GET /epoch +pub async fn epoch_handler(State(state): State>) -> Json> { + let chain = state.chain_state.read(); + + // Epoch config from state + let blocks_per_epoch = 100u64; + let block_in_epoch = chain.block_height % blocks_per_epoch; + + let (phase, phase_progress) = if block_in_epoch < 75 { + ("evaluation", block_in_epoch as f64 / 75.0) + } else if block_in_epoch < 88 { + ("commit", (block_in_epoch - 75) as f64 / 13.0) + } else { + ("reveal", (block_in_epoch - 88) as f64 / 12.0) + }; + + let blocks_until_next = match phase { + "evaluation" => 75 - block_in_epoch, + "commit" => 88 - block_in_epoch, + "reveal" => blocks_per_epoch - block_in_epoch, + _ => 0, + }; + + Json(RpcResponse::ok(EpochResponse { + current_epoch: chain.epoch, + current_block: chain.block_height, + blocks_per_epoch, + phase: phase.to_string(), + phase_progress, + blocks_until_next_phase: blocks_until_next, + })) +} + +/// GET /sync +pub async fn sync_handler(State(state): State>) -> Json> { + let chain = state.chain_state.read(); + + let validators: Vec = chain + .validators + .values() + .map(|v| ValidatorResponse { + hotkey: v.hotkey.to_hex(), + stake: v.stake.0, + stake_tao: v.stake.as_tao(), + is_active: v.is_active, + last_seen: v.last_seen, + peer_id: v.peer_id.clone(), + x25519_pubkey: v.x25519_pubkey.clone(), + }) + .collect(); + + let challenges: Vec = chain + .challenges + .values() + .map(|c| ChallengeResponse { + id: c.id.to_string(), + name: c.name.clone(), + description: c.description.clone(), + code_hash: c.code_hash.clone(), + is_active: c.is_active, + emission_weight: c.config.emission_weight, + timeout_secs: c.config.timeout_secs, + }) + .collect(); + + Json(RpcResponse::ok(SyncResponse { + block_height: chain.block_height, + epoch: chain.epoch, + state_hash: hex::encode(chain.state_hash), + validators, + challenges, + })) +} + +/// POST /weights/commit +pub async fn weight_commit_handler( + State(state): State>, + Json(req): Json, +) -> Json> { + // Verify signature + let msg = format!( + "commit:{}:{}:{}", + req.challenge_id, req.epoch, req.commitment_hash + ); + match verify_validator_signature(&req.hotkey, &msg, &req.signature) { + Ok(true) => {} + _ => { + return Json(RpcResponse::error("Invalid signature")); + } + } + + info!( + "Weight commitment received: validator={} challenge={} epoch={}", + &req.hotkey[..16], + req.challenge_id, + req.epoch + ); + + // Commitment storage + Json(RpcResponse::ok(true)) +} + +/// POST /weights/reveal +pub async fn weight_reveal_handler( + State(state): State>, + Json(req): Json, +) -> Json> { + // Verify signature + let weights_str: String = req + .weights + .iter() + .map(|w| format!("{}:{}", w.hotkey, w.weight)) + .collect::>() + .join(","); + let msg = format!( + "reveal:{}:{}:{}:{}", + req.challenge_id, req.epoch, weights_str, req.salt + ); + + match verify_validator_signature(&req.hotkey, &msg, &req.signature) { + Ok(true) => {} + _ => { + return Json(RpcResponse::error("Invalid signature")); + } + } + + info!( + "Weight reveal received: validator={} challenge={} epoch={} weights={}", + &req.hotkey[..16], + req.challenge_id, + req.epoch, + req.weights.len() + ); + + // Commitment verification and weight storage + Json(RpcResponse::ok(true)) +} +#[cfg(test)] +mod tests { + use super::*; + use platform_core::{ + ChainState, Challenge, ChallengeConfig, ChallengeId, Hotkey, Job, Keypair, NetworkConfig, + Score, Stake, ValidatorInfo, WasmConfig, WasmModuleMetadata, + }; + + fn create_test_state() -> Arc { + let kp = Keypair::generate(); + let chain_state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + Arc::new(RpcState::new( + chain_state, + bans, + 1, + "Test-Chain".to_string(), + 1_000_000_000_000, + )) + } + + #[tokio::test] + async fn test_health_handler() { + let state = create_test_state(); + let response = health_handler(State(state)).await; + assert_eq!(response.0.data.as_ref().unwrap().status, "healthy"); + } + + #[tokio::test] + async fn test_status_handler() { + let state = create_test_state(); + let response = status_handler(State(state)).await; + let data = response.0.data.unwrap(); + assert_eq!(data.netuid, 1); + assert_eq!(data.name, "Test-Chain"); + } + + #[tokio::test] + async fn test_validators_handler_empty() { + let state = create_test_state(); + let params = PaginationParams::default(); + let response = validators_handler(State(state), Query(params)).await; + let validators = response.0.data.unwrap(); + assert!(validators.is_empty()); + } + + #[tokio::test] + async fn test_validators_handler_with_validators() { + let state = create_test_state(); + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(5_000_000_000_000)); + + state + .chain_state + .write() + .validators + .insert(kp.hotkey(), info); + + let params = PaginationParams::default(); + let response = validators_handler(State(state), Query(params)).await; + let validators = response.0.data.unwrap(); + assert_eq!(validators.len(), 1); + assert_eq!(validators[0].hotkey, kp.hotkey().to_hex()); + } + + #[tokio::test] + async fn test_challenges_handler_empty() { + let state = create_test_state(); + let response = challenges_handler(State(state)).await; + let challenges = response.0.data.unwrap(); + assert!(challenges.is_empty()); + } + + #[tokio::test] + async fn test_challenges_handler_with_challenges() { + let state = create_test_state(); + let kp = Keypair::generate(); + let challenge_id = ChallengeId::new(); + let config = ChallengeConfig { + mechanism_id: 1, + emission_weight: 1.0, + timeout_secs: 300, + max_memory_mb: 2048, + max_cpu_secs: 60, + min_validators: 1, + params_json: "{}".to_string(), + wasm: WasmConfig::default(), + }; + let challenge = Challenge { + id: challenge_id, + name: "Test Challenge".to_string(), + description: "Test description".to_string(), + code_hash: "abc123".to_string(), + wasm_code: vec![], + wasm_metadata: WasmModuleMetadata::from_code_hash("abc123".to_string()), + is_active: true, + owner: kp.hotkey(), + config, + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + }; + + state + .chain_state + .write() + .challenges + .insert(challenge_id, challenge); + + let response = challenges_handler(State(state)).await; + let challenges = response.0.data.unwrap(); + assert_eq!(challenges.len(), 1); + assert_eq!(challenges[0].name, "Test Challenge"); + } + + #[tokio::test] + async fn test_challenge_handler_found() { + let state = create_test_state(); + let kp = Keypair::generate(); + let challenge_id = ChallengeId::new(); + let config = ChallengeConfig { + mechanism_id: 1, + emission_weight: 1.0, + timeout_secs: 300, + max_memory_mb: 2048, + max_cpu_secs: 60, + min_validators: 1, + params_json: "{}".to_string(), + wasm: WasmConfig::default(), + }; + let challenge = Challenge { + id: challenge_id, + name: "Test Challenge".to_string(), + description: "Test description".to_string(), + code_hash: "abc123".to_string(), + wasm_code: vec![], + wasm_metadata: WasmModuleMetadata::from_code_hash("abc123".to_string()), + is_active: true, + owner: kp.hotkey(), + config, + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + }; + + state + .chain_state + .write() + .challenges + .insert(challenge_id, challenge); + + let response = challenge_handler(State(state), Path(challenge_id.to_string())).await; + assert!(response.is_ok()); + let json_response = response.unwrap(); + let challenge_data = json_response.0.data.unwrap(); + assert_eq!(challenge_data.name, "Test Challenge"); + } + + #[tokio::test] + async fn test_challenge_handler_not_found() { + let state = create_test_state(); + let response = challenge_handler(State(state), Path("nonexistent".to_string())).await; + assert!(response.is_err()); + assert_eq!(response.unwrap_err(), StatusCode::NOT_FOUND); + } + + #[tokio::test] + async fn test_challenge_handler_find_by_name() { + let state = create_test_state(); + let kp = Keypair::generate(); + let challenge_id = ChallengeId::new(); + let config = ChallengeConfig { + mechanism_id: 1, + emission_weight: 1.0, + timeout_secs: 300, + max_memory_mb: 2048, + max_cpu_secs: 60, + min_validators: 1, + params_json: "{}".to_string(), + wasm: WasmConfig::default(), + }; + let challenge = Challenge { + id: challenge_id, + name: "test-challenge".to_string(), + description: "Test description".to_string(), + code_hash: "abc123".to_string(), + wasm_code: vec![], + wasm_metadata: WasmModuleMetadata::from_code_hash("abc123".to_string()), + is_active: true, + owner: kp.hotkey(), + config, + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + }; + + state + .chain_state + .write() + .challenges + .insert(challenge_id, challenge); + + let response = challenge_handler(State(state), Path("test-challenge".to_string())).await; + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_register_handler_invalid_signature() { + let state = create_test_state(); + let req = RegisterRequest { + hotkey: "0000000000000000000000000000000000000000000000000000000000000000".to_string(), + signature: "invalid".to_string(), + message: "register:1234567890:nonce".to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + assert!(!response.0.data.unwrap().accepted); + } + + #[tokio::test] + async fn test_register_handler_banned_validator() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Ban the validator + state + .bans + .write() + .ban_validator(&kp.hotkey(), "Test ban", "test"); + + let message = "register:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + let req = RegisterRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + message: message.to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(!register_resp.accepted); + assert!(register_resp.reason.unwrap().contains("banned")); + } + + #[tokio::test] + async fn test_register_handler_already_registered() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Pre-register the validator + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(5_000_000_000_000)); + state + .chain_state + .write() + .validators + .insert(kp.hotkey(), info); + + let message = "register:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + let req = RegisterRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + message: message.to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(register_resp.accepted); + assert!(register_resp.reason.unwrap().contains("Already registered")); + } + + #[tokio::test] + async fn test_register_handler_success() { + let state = create_test_state(); + let kp = Keypair::generate(); + + let message = "register:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + let req = RegisterRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + message: message.to_string(), + peer_id: None, + }; + + let response = register_handler(State(state.clone()), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(register_resp.accepted); + + // Verify validator was added + let chain = state.chain_state.read(); + assert!(chain.validators.contains_key(&kp.hotkey())); + } + + #[tokio::test] + async fn test_heartbeat_handler_invalid_hotkey() { + let state = create_test_state(); + let req = HeartbeatRequest { + hotkey: "invalid".to_string(), + signature: "sig".to_string(), + block_height: 100, + peer_id: None, + }; + + let response = heartbeat_handler(State(state), Json(req)).await; + assert!(response.0.error.is_some()); + } + + #[tokio::test] + async fn test_heartbeat_handler_not_registered() { + let state = create_test_state(); + let kp = Keypair::generate(); + let req = HeartbeatRequest { + hotkey: kp.hotkey().to_hex(), + signature: "sig".to_string(), + block_height: 100, + peer_id: Some("peer1".to_string()), + }; + + let response = heartbeat_handler(State(state), Json(req)).await; + let heartbeat_resp = response.0.data.unwrap(); + assert!(!heartbeat_resp.accepted); + } + + #[tokio::test] + async fn test_heartbeat_handler_success() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Register the validator + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(5_000_000_000_000)); + state + .chain_state + .write() + .validators + .insert(kp.hotkey(), info); + + let req = HeartbeatRequest { + hotkey: kp.hotkey().to_hex(), + signature: "sig".to_string(), + block_height: 100, + peer_id: Some("peer1".to_string()), + }; + + let response = heartbeat_handler(State(state.clone()), Json(req)).await; + let heartbeat_resp = response.0.data.unwrap(); + assert!(heartbeat_resp.accepted); + assert_eq!(heartbeat_resp.current_block, 0); + + // Verify peer_id was updated + let chain = state.chain_state.read(); + let validator = chain.validators.get(&kp.hotkey()).unwrap(); + assert_eq!(validator.peer_id, Some("peer1".to_string())); + } + + #[tokio::test] + async fn test_jobs_handler_empty() { + let state = create_test_state(); + let params = PaginationParams::default(); + let response = jobs_handler(State(state), Query(params)).await; + let jobs = response.0.data.unwrap(); + assert!(jobs.is_empty()); + } + + #[tokio::test] + async fn test_jobs_handler_with_pagination() { + let state = create_test_state(); + + // Add some jobs + { + let mut chain = state.chain_state.write(); + for i in 0..5 { + let job = platform_core::Job { + id: uuid::Uuid::new_v4(), + challenge_id: ChallengeId::new(), + agent_hash: format!("hash{}", i), + status: platform_core::JobStatus::Pending, + created_at: chrono::Utc::now(), + assigned_validator: None, + result: None, + }; + chain.pending_jobs.push(job); + } + } + + let params = PaginationParams { + offset: Some(1), + limit: Some(2), + }; + let response = jobs_handler(State(state), Query(params)).await; + let jobs = response.0.data.unwrap(); + assert_eq!(jobs.len(), 2); + } + + #[tokio::test] + async fn test_job_result_handler_invalid_job_id() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Sign the message correctly first + let job_id = "not-a-uuid"; + let message = format!("result:{}:0.9", job_id); + let signed = kp.sign(message.as_bytes()); + + let req = JobResultRequest { + job_id: job_id.to_string(), + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + score: 0.9, + metadata: None, + }; + + let response = job_result_handler(State(state), Path(job_id.to_string()), Json(req)).await; + // Invalid job ID (not a UUID) returns error through RpcResponse::error + assert!(!response.0.success); + assert!(response.0.error.is_some()); + } + + #[tokio::test] + async fn test_job_result_handler_invalid_signature() { + let state = create_test_state(); + let job_id = uuid::Uuid::new_v4(); + let req = JobResultRequest { + job_id: job_id.to_string(), + hotkey: "0000000000000000000000000000000000000000000000000000000000000000".to_string(), + signature: "invalid".to_string(), + score: 0.9, + metadata: None, + }; + + let response = job_result_handler(State(state), Path(job_id.to_string()), Json(req)).await; + let result_resp = response.0.data.unwrap(); + assert!(!result_resp.accepted); + } + + #[tokio::test] + async fn test_epoch_handler() { + let state = create_test_state(); + let response = epoch_handler(State(state)).await; + let epoch = response.0.data.unwrap(); + assert_eq!(epoch.current_epoch, 0); + assert_eq!(epoch.blocks_per_epoch, 100); + assert_eq!(epoch.phase, "evaluation"); + } + + #[tokio::test] + async fn test_epoch_handler_commit_phase() { + let state = create_test_state(); + + // Set block height to commit phase (75-87) + state.chain_state.write().block_height = 80; + + let response = epoch_handler(State(state)).await; + let epoch = response.0.data.unwrap(); + assert_eq!(epoch.phase, "commit"); + } + + #[tokio::test] + async fn test_epoch_handler_reveal_phase() { + let state = create_test_state(); + + // Set block height to reveal phase (88-99) + state.chain_state.write().block_height = 90; + + let response = epoch_handler(State(state)).await; + let epoch = response.0.data.unwrap(); + assert_eq!(epoch.phase, "reveal"); + } + + #[tokio::test] + async fn test_sync_handler() { + let state = create_test_state(); + let response = sync_handler(State(state)).await; + let sync = response.0.data.unwrap(); + assert_eq!(sync.block_height, 0); + assert_eq!(sync.epoch, 0); + assert!(sync.validators.is_empty()); + assert!(sync.challenges.is_empty()); + } + + #[tokio::test] + async fn test_weight_commit_handler_invalid_signature() { + let state = create_test_state(); + let req = WeightCommitRequest { + hotkey: "0000000000000000000000000000000000000000000000000000000000000000".to_string(), + signature: "invalid".to_string(), + challenge_id: "challenge1".to_string(), + commitment_hash: "hash123".to_string(), + epoch: 1, + }; + + let response = weight_commit_handler(State(state), Json(req)).await; + assert!(response.0.error.is_some()); + } + + #[tokio::test] + async fn test_weight_reveal_handler_invalid_signature() { + let state = create_test_state(); + let req = WeightRevealRequest { + hotkey: "0000000000000000000000000000000000000000000000000000000000000000".to_string(), + signature: "invalid".to_string(), + challenge_id: "challenge1".to_string(), + weights: vec![], + salt: "salt123".to_string(), + epoch: 1, + }; + + let response = weight_reveal_handler(State(state), Json(req)).await; + assert!(response.0.error.is_some()); + } + + #[tokio::test] + async fn test_register_handler_invalid_hotkey_format() { + let state = create_test_state(); + let kp = Keypair::generate(); + let message = "register:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + + let req = RegisterRequest { + hotkey: "invalid-hotkey-format".to_string(), + signature: hex::encode(&signed.signature), + message: message.to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(!register_resp.accepted); + assert!(register_resp + .reason + .unwrap() + .contains("Invalid hotkey format")); + } + + #[tokio::test] + async fn test_register_handler_signature_error() { + let state = create_test_state(); + let req = RegisterRequest { + hotkey: "invalid".to_string(), + signature: "sig".to_string(), + message: "register:1234567890:nonce".to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(!register_resp.accepted); + assert!(register_resp.reason.unwrap().contains("Auth error")); + } + + #[tokio::test] + async fn test_register_handler_add_validator_error() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Fill validators to max capacity to trigger error + { + let mut chain = state.chain_state.write(); + for _ in 0..chain.config.max_validators { + let temp_kp = Keypair::generate(); + let info = ValidatorInfo::new(temp_kp.hotkey(), Stake::new(5_000_000_000_000)); + chain.validators.insert(temp_kp.hotkey(), info); + } + } + + let message = "register:1234567890:nonce"; + let signed = kp.sign(message.as_bytes()); + let req = RegisterRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + message: message.to_string(), + peer_id: None, + }; + + let response = register_handler(State(state), Json(req)).await; + let register_resp = response.0.data.unwrap(); + assert!(!register_resp.accepted); + assert!(register_resp + .reason + .unwrap() + .contains("Registration failed")); + } + + #[tokio::test] + async fn test_job_result_handler_job_not_found() { + let state = create_test_state(); + let kp = Keypair::generate(); + let job_id = uuid::Uuid::new_v4(); + + let message = format!("result:{}:0.9", job_id); + let signed = kp.sign(message.as_bytes()); + + let req = JobResultRequest { + job_id: job_id.to_string(), + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + score: 0.9, + metadata: None, + }; + + let response = job_result_handler(State(state), Path(job_id.to_string()), Json(req)).await; + let result_resp = response.0.data.unwrap(); + assert!(!result_resp.accepted); + } + + #[tokio::test] + async fn test_job_result_handler_success() { + let state = create_test_state(); + let kp = Keypair::generate(); + + // Add a job first + let job_id = uuid::Uuid::new_v4(); + let job = platform_core::Job { + id: job_id, + challenge_id: ChallengeId::new(), + agent_hash: "hash123".to_string(), + status: platform_core::JobStatus::Pending, + created_at: chrono::Utc::now(), + assigned_validator: None, + result: None, + }; + state.chain_state.write().pending_jobs.push(job); + + let message = format!("result:{}:0.95", job_id); + let signed = kp.sign(message.as_bytes()); + + let req = JobResultRequest { + job_id: job_id.to_string(), + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + score: 0.95, + metadata: Some(serde_json::json!({"test": "data"})), + }; + + let response = + job_result_handler(State(state.clone()), Path(job_id.to_string()), Json(req)).await; + let result_resp = response.0.data.unwrap(); + assert!(result_resp.accepted); + + // Verify job was updated + let chain = state.chain_state.read(); + let updated_job = chain.pending_jobs.iter().find(|j| j.id == job_id).unwrap(); + assert!(matches!( + updated_job.status, + platform_core::JobStatus::Completed + )); + assert!(updated_job.result.is_some()); + } + + #[tokio::test] + async fn test_weight_commit_handler_success() { + let state = create_test_state(); + let kp = Keypair::generate(); + + let challenge_id = "challenge1"; + let epoch = 5; + let commitment_hash = "abc123"; + let message = format!("commit:{}:{}:{}", challenge_id, epoch, commitment_hash); + let signed = kp.sign(message.as_bytes()); + + let req = WeightCommitRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + challenge_id: challenge_id.to_string(), + commitment_hash: commitment_hash.to_string(), + epoch, + }; + + let response = weight_commit_handler(State(state), Json(req)).await; + assert!(response.0.success); + assert_eq!(response.0.data, Some(true)); + } + + #[tokio::test] + async fn test_weight_reveal_handler_success() { + let state = create_test_state(); + let kp = Keypair::generate(); + + let challenge_id = "challenge1"; + let epoch = 5; + let salt = "salt123"; + let weights = vec![ + WeightEntry { + hotkey: "hk1".to_string(), + weight: 0.6, + }, + WeightEntry { + hotkey: "hk2".to_string(), + weight: 0.4, + }, + ]; + + let weights_str: String = weights + .iter() + .map(|w| format!("{}:{}", w.hotkey, w.weight)) + .collect::>() + .join(","); + let message = format!("reveal:{}:{}:{}:{}", challenge_id, epoch, weights_str, salt); + let signed = kp.sign(message.as_bytes()); + + let req = WeightRevealRequest { + hotkey: kp.hotkey().to_hex(), + signature: hex::encode(&signed.signature), + challenge_id: challenge_id.to_string(), + weights, + salt: salt.to_string(), + epoch, + }; + + let response = weight_reveal_handler(State(state), Json(req)).await; + assert!(response.0.success); + assert_eq!(response.0.data, Some(true)); + } +} diff --git a/crates/rpc-server/src/health.rs b/crates/rpc-server/src/health.rs new file mode 100644 index 000000000..5ae0dd35d --- /dev/null +++ b/crates/rpc-server/src/health.rs @@ -0,0 +1,380 @@ +//! Health check endpoints for validator coordination +//! +//! Provides: +//! - `/health` - Basic liveness check +//! - `/ready` - Readiness check (can accept traffic) +//! - `/live` - Kubernetes-style liveness probe +//! +//! These enable coordinated rolling updates across the validator network. + +use serde::{Deserialize, Serialize}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::Arc; +use std::time::{Instant, SystemTime, UNIX_EPOCH}; +use tracing::{info, warn}; + +/// Health status of a component +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] +#[serde(rename_all = "lowercase")] +pub enum HealthStatus { + /// Component is healthy + Healthy, + /// Component is degraded but operational + Degraded, + /// Component is unhealthy + Unhealthy, + /// Component status is unknown + #[default] + Unknown, +} + +/// Readiness status for traffic handling +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] +#[serde(rename_all = "lowercase")] +pub enum ReadinessStatus { + /// Ready to accept traffic + Ready, + /// Not ready (initializing, draining, etc.) + #[default] + NotReady, + /// Draining - finishing current work, not accepting new + Draining, +} + +/// Health check response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HealthResponse { + /// Overall health status + pub status: HealthStatus, + /// Readiness for traffic + pub ready: ReadinessStatus, + /// Version string + pub version: String, + /// Uptime in seconds + pub uptime_secs: u64, + /// Current epoch + pub epoch: u64, + /// P2P connection count + pub peer_count: u64, + /// Active challenges count + pub active_challenges: u64, + /// Pending evaluations count + pub pending_evaluations: u64, + /// Last checkpoint sequence + pub checkpoint_sequence: u64, + /// Timestamp (Unix millis) + pub timestamp: i64, + /// Component statuses + pub components: ComponentStatus, +} + +/// Status of individual components +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct ComponentStatus { + /// P2P network status + pub p2p: HealthStatus, + /// Storage status + pub storage: HealthStatus, + /// Consensus status + pub consensus: HealthStatus, + /// Bittensor connection status + pub bittensor: HealthStatus, + /// Challenge containers status + pub challenges: HealthStatus, +} + +/// Health check manager +pub struct HealthCheck { + /// Start time + start_time: Instant, + /// Version string + version: String, + /// Whether ready for traffic + ready: AtomicBool, + /// Whether draining + draining: AtomicBool, + /// Current epoch + epoch: AtomicU64, + /// Peer count + peer_count: AtomicU64, + /// Active challenges + active_challenges: AtomicU64, + /// Pending evaluations + pending_evaluations: AtomicU64, + /// Last checkpoint sequence + checkpoint_sequence: AtomicU64, + /// Component status (using interior mutability) + components: parking_lot::RwLock, +} + +impl HealthCheck { + /// Create a new health check manager + pub fn new(version: impl Into) -> Self { + Self { + start_time: Instant::now(), + version: version.into(), + ready: AtomicBool::new(false), + draining: AtomicBool::new(false), + epoch: AtomicU64::new(0), + peer_count: AtomicU64::new(0), + active_challenges: AtomicU64::new(0), + pending_evaluations: AtomicU64::new(0), + checkpoint_sequence: AtomicU64::new(0), + components: parking_lot::RwLock::new(ComponentStatus::default()), + } + } + + /// Mark as ready for traffic + pub fn set_ready(&self, ready: bool) { + self.ready.store(ready, Ordering::SeqCst); + if ready { + info!("Validator marked as ready for traffic"); + } + } + + /// Start draining (preparing for shutdown) + pub fn start_draining(&self) { + self.draining.store(true, Ordering::SeqCst); + self.ready.store(false, Ordering::SeqCst); + info!("Validator entering drain mode"); + } + + /// Check if draining + pub fn is_draining(&self) -> bool { + self.draining.load(Ordering::SeqCst) + } + + /// Update epoch + pub fn set_epoch(&self, epoch: u64) { + self.epoch.store(epoch, Ordering::SeqCst); + } + + /// Update peer count + pub fn set_peer_count(&self, count: u64) { + self.peer_count.store(count, Ordering::SeqCst); + } + + /// Update active challenges + pub fn set_active_challenges(&self, count: u64) { + self.active_challenges.store(count, Ordering::SeqCst); + } + + /// Update pending evaluations + pub fn set_pending_evaluations(&self, count: u64) { + self.pending_evaluations.store(count, Ordering::SeqCst); + } + + /// Update checkpoint sequence + pub fn set_checkpoint_sequence(&self, seq: u64) { + self.checkpoint_sequence.store(seq, Ordering::SeqCst); + } + + /// Update component status + pub fn set_component_status(&self, component: &str, status: HealthStatus) { + let mut components = self.components.write(); + match component { + "p2p" => components.p2p = status, + "storage" => components.storage = status, + "consensus" => components.consensus = status, + "bittensor" => components.bittensor = status, + "challenges" => components.challenges = status, + _ => warn!("Unknown component: {}", component), + } + } + + /// Get overall health status + fn get_overall_status(&self) -> HealthStatus { + let components = self.components.read(); + + // If any component is unhealthy, overall is unhealthy + if components.p2p == HealthStatus::Unhealthy + || components.storage == HealthStatus::Unhealthy + || components.consensus == HealthStatus::Unhealthy + { + return HealthStatus::Unhealthy; + } + + // If any critical component is degraded, overall is degraded + if components.p2p == HealthStatus::Degraded + || components.storage == HealthStatus::Degraded + || components.consensus == HealthStatus::Degraded + { + return HealthStatus::Degraded; + } + + // If Bittensor is down but others are fine, degraded + if components.bittensor == HealthStatus::Unhealthy { + return HealthStatus::Degraded; + } + + HealthStatus::Healthy + } + + /// Get readiness status + fn get_readiness(&self) -> ReadinessStatus { + if self.draining.load(Ordering::SeqCst) { + return ReadinessStatus::Draining; + } + if self.ready.load(Ordering::SeqCst) { + return ReadinessStatus::Ready; + } + ReadinessStatus::NotReady + } + + /// Get full health response + pub fn get_health(&self) -> HealthResponse { + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_millis() as i64; + + HealthResponse { + status: self.get_overall_status(), + ready: self.get_readiness(), + version: self.version.clone(), + uptime_secs: self.start_time.elapsed().as_secs(), + epoch: self.epoch.load(Ordering::SeqCst), + peer_count: self.peer_count.load(Ordering::SeqCst), + active_challenges: self.active_challenges.load(Ordering::SeqCst), + pending_evaluations: self.pending_evaluations.load(Ordering::SeqCst), + checkpoint_sequence: self.checkpoint_sequence.load(Ordering::SeqCst), + timestamp, + components: self.components.read().clone(), + } + } + + /// Basic liveness check (is the process running) + pub fn is_live(&self) -> bool { + // If we can respond, we're live + true + } + + /// Readiness check (can accept traffic) + pub fn is_ready(&self) -> bool { + self.ready.load(Ordering::SeqCst) && !self.draining.load(Ordering::SeqCst) + } +} + +impl Default for HealthCheck { + fn default() -> Self { + Self::new("unknown") + } +} + +/// Create a shared health check instance +pub fn create_health_check(version: &str) -> Arc { + Arc::new(HealthCheck::new(version)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_health_check_creation() { + let health = HealthCheck::new("1.0.0"); + assert_eq!(health.version, "1.0.0"); + assert!(!health.is_ready()); + assert!(!health.is_draining()); + } + + #[test] + fn test_ready_state() { + let health = HealthCheck::new("1.0.0"); + + assert!(!health.is_ready()); + health.set_ready(true); + assert!(health.is_ready()); + + let response = health.get_health(); + assert_eq!(response.ready, ReadinessStatus::Ready); + } + + #[test] + fn test_draining_state() { + let health = HealthCheck::new("1.0.0"); + health.set_ready(true); + + health.start_draining(); + assert!(health.is_draining()); + assert!(!health.is_ready()); + + let response = health.get_health(); + assert_eq!(response.ready, ReadinessStatus::Draining); + } + + #[test] + fn test_component_status() { + let health = HealthCheck::new("1.0.0"); + + health.set_component_status("p2p", HealthStatus::Healthy); + health.set_component_status("storage", HealthStatus::Healthy); + health.set_component_status("consensus", HealthStatus::Healthy); + health.set_component_status("bittensor", HealthStatus::Healthy); + + let response = health.get_health(); + assert_eq!(response.status, HealthStatus::Healthy); + } + + #[test] + fn test_unhealthy_component() { + let health = HealthCheck::new("1.0.0"); + + health.set_component_status("p2p", HealthStatus::Unhealthy); + + let response = health.get_health(); + assert_eq!(response.status, HealthStatus::Unhealthy); + } + + #[test] + fn test_degraded_component() { + let health = HealthCheck::new("1.0.0"); + + health.set_component_status("p2p", HealthStatus::Healthy); + health.set_component_status("storage", HealthStatus::Degraded); + + let response = health.get_health(); + assert_eq!(response.status, HealthStatus::Degraded); + } + + #[test] + fn test_metrics_update() { + let health = HealthCheck::new("1.0.0"); + + health.set_epoch(42); + health.set_peer_count(10); + health.set_active_challenges(3); + health.set_pending_evaluations(5); + health.set_checkpoint_sequence(100); + + let response = health.get_health(); + assert_eq!(response.epoch, 42); + assert_eq!(response.peer_count, 10); + assert_eq!(response.active_challenges, 3); + assert_eq!(response.pending_evaluations, 5); + assert_eq!(response.checkpoint_sequence, 100); + } + + #[test] + fn test_uptime() { + let health = HealthCheck::new("1.0.0"); + + // Just check uptime is a reasonable value (not negative, not huge) + let response = health.get_health(); + assert!(response.uptime_secs < 10); // Should be very small in a test + } + + #[test] + fn test_bittensor_degraded() { + let health = HealthCheck::new("1.0.0"); + + health.set_component_status("p2p", HealthStatus::Healthy); + health.set_component_status("storage", HealthStatus::Healthy); + health.set_component_status("consensus", HealthStatus::Healthy); + health.set_component_status("bittensor", HealthStatus::Unhealthy); + + // Bittensor unhealthy = degraded, not fully unhealthy + let response = health.get_health(); + assert_eq!(response.status, HealthStatus::Degraded); + } +} diff --git a/crates/rpc-server/src/jsonrpc.rs b/crates/rpc-server/src/jsonrpc.rs new file mode 100644 index 000000000..c1094080c --- /dev/null +++ b/crates/rpc-server/src/jsonrpc.rs @@ -0,0 +1,2614 @@ +//! Substrate-style JSON-RPC 2.0 Server +//! +//! Complete JSON-RPC API for Mini-Chain. +//! +//! # Namespaces +//! +//! ## system_* - System information +//! - system_health - Health check +//! - system_version - Get version info +//! - system_name - Get chain name +//! - system_properties - Get chain properties +//! - system_peers - Get connected peers +//! - system_networkState - Get network state +//! +//! ## chain_* - Chain data +//! - chain_getHead - Get latest block header +//! - chain_getBlock - Get block by number +//! - chain_getBlockHash - Get block hash by number +//! - chain_getFinalizedHead - Get finalized block +//! +//! ## state_* - State queries +//! - state_getStorage - Get storage by key +//! - state_getKeys - Get storage keys with prefix +//! - state_getMetadata - Get runtime metadata +//! - state_getRuntimeVersion - Get runtime version +//! +//! ## author_* - Authoring (transactions) +//! - author_submitExtrinsic - Submit a transaction +//! - author_pendingExtrinsics - Get pending transactions +//! +//! ## validator_* - Validator queries +//! - validator_list - List all validators +//! - validator_get - Get validator by hotkey +//! - validator_count - Get validator count +//! +//! ## challenge_* - Challenge management +//! - challenge_list - List all challenges +//! - challenge_get - Get challenge by ID/name +//! - challenge_getRoutes - Get routes for a challenge +//! - challenge_listAllRoutes - List all challenge routes +//! - challenge_call - Call a challenge route +//! +//! ## job_* - Job management +//! - job_list - List pending jobs +//! - job_get - Get job by ID +//! - job_submit - Submit a job +//! +//! ## epoch_* - Epoch information +//! - epoch_current - Get current epoch info +//! - epoch_getPhase - Get current phase + +use parking_lot::RwLock; +use platform_challenge_sdk::{ + ChallengeRoute, RouteRequest, RouteResponse as ChallengeRouteResponse, +}; +use platform_core::ChainState; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use std::collections::HashMap; +use std::sync::Arc; +use std::time::Instant; +use tracing::{debug, info, warn}; + +/// Handler for challenge routes +pub type ChallengeRouteHandler = Arc< + dyn Fn( + String, + RouteRequest, + ) + -> std::pin::Pin + Send>> + + Send + + Sync, +>; + +/// JSON-RPC 2.0 Request +#[derive(Debug, Clone, Deserialize)] +pub struct JsonRpcRequest { + pub jsonrpc: String, + pub method: String, + #[serde(default)] + pub params: Value, + pub id: Value, +} + +/// JSON-RPC 2.0 Response +#[derive(Debug, Clone, Serialize)] +pub struct JsonRpcResponse { + pub jsonrpc: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + pub id: Value, +} + +/// JSON-RPC 2.0 Error +#[derive(Debug, Clone, Serialize)] +pub struct JsonRpcError { + pub code: i32, + pub message: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub data: Option, +} + +impl JsonRpcResponse { + pub fn result(id: Value, result: Value) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: Some(result), + error: None, + id, + } + } + + pub fn error(id: Value, code: i32, message: impl Into) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: None, + error: Some(JsonRpcError { + code, + message: message.into(), + data: None, + }), + id, + } + } + + pub fn error_with_data(id: Value, code: i32, message: impl Into, data: Value) -> Self { + Self { + jsonrpc: "2.0".to_string(), + result: None, + error: Some(JsonRpcError { + code, + message: message.into(), + data: Some(data), + }), + id, + } + } +} + +// Standard JSON-RPC error codes +pub const PARSE_ERROR: i32 = -32700; +pub const INVALID_REQUEST: i32 = -32600; +pub const METHOD_NOT_FOUND: i32 = -32601; +pub const INVALID_PARAMS: i32 = -32602; +pub const INTERNAL_ERROR: i32 = -32603; + +// Custom error codes +pub const CHALLENGE_NOT_FOUND: i32 = -32000; +pub const VALIDATOR_NOT_FOUND: i32 = -32001; +pub const JOB_NOT_FOUND: i32 = -32002; +pub const ROUTE_NOT_FOUND: i32 = -32003; +pub const INSUFFICIENT_STAKE: i32 = -32004; +pub const UNAUTHORIZED: i32 = -32005; + +/// Registered challenge route info +#[derive(Clone, Debug, Serialize)] +pub struct RegisteredChallengeRoute { + pub challenge_id: String, + pub challenge_name: String, + pub route: ChallengeRouteInfo, +} + +/// Simplified route info for serialization +#[derive(Clone, Debug, Serialize)] +pub struct ChallengeRouteInfo { + pub method: String, + pub path: String, + pub full_path: String, + pub description: String, + pub requires_auth: bool, + pub rate_limit: u32, +} + +/// RPC Handler State +pub struct RpcHandler { + pub chain_state: Arc>, + pub start_time: Instant, + pub version: String, + pub netuid: u16, + pub chain_name: String, + pub peers: Arc>>, + /// Registered challenge routes: challenge_id -> routes + pub challenge_routes: Arc>>>, + /// Challenge route handler callback + pub route_handler: Arc>>, + /// Channel to send signed messages for P2P broadcast + pub broadcast_tx: Arc>>>>, + /// Keypair for signing P2P messages (optional, set by validator) + pub keypair: Arc>>, +} + +impl RpcHandler { + pub fn new(chain_state: Arc>, netuid: u16) -> Self { + Self { + chain_state, + start_time: Instant::now(), + version: env!("CARGO_PKG_VERSION").to_string(), + netuid, + chain_name: format!("MiniChain-{}", netuid), + peers: Arc::new(RwLock::new(Vec::new())), + challenge_routes: Arc::new(RwLock::new(HashMap::new())), + route_handler: Arc::new(RwLock::new(None)), + broadcast_tx: Arc::new(RwLock::new(None)), + keypair: Arc::new(RwLock::new(None)), + } + } + + /// Set the keypair for signing P2P messages + pub fn set_keypair(&self, keypair: platform_core::Keypair) { + *self.keypair.write() = Some(keypair); + } + + /// Set the broadcast channel for P2P message sending + pub fn set_broadcast_tx(&self, tx: tokio::sync::mpsc::UnboundedSender>) { + *self.broadcast_tx.write() = Some(tx); + } + + /// Normalize challenge name: lowercase, replace spaces with dashes, remove special chars + pub fn normalize_challenge_name(name: &str) -> String { + name.trim() + .to_lowercase() + .replace([' ', '_'], "-") + .chars() + .filter(|c| c.is_alphanumeric() || *c == '-') + .collect::() + .trim_matches('-') + .to_string() + } + + /// Register routes for a challenge + pub fn register_challenge_routes(&self, challenge_id: &str, routes: Vec) { + if routes.is_empty() { + return; + } + info!( + "Registering {} routes for challenge {}", + routes.len(), + challenge_id + ); + for route in &routes { + debug!( + " {} {}: {}", + route.method.as_str(), + route.path, + route.description + ); + } + self.challenge_routes + .write() + .insert(challenge_id.to_string(), routes); + } + + /// Unregister routes for a challenge + pub fn unregister_challenge_routes(&self, challenge_id: &str) { + self.challenge_routes.write().remove(challenge_id); + } + + /// Set the route handler callback + pub fn set_route_handler(&self, handler: ChallengeRouteHandler) { + *self.route_handler.write() = Some(handler); + } + + /// Get all registered challenge routes + pub fn get_all_challenge_routes(&self) -> Vec { + let routes = self.challenge_routes.read(); + let chain = self.chain_state.read(); + + let mut result = Vec::new(); + for (challenge_id, challenge_routes) in routes.iter() { + let challenge_name = chain + .challenges + .values() + .find(|c| c.id.to_string() == *challenge_id) + .map(|c| c.name.clone()) + .unwrap_or_else(|| challenge_id.clone()); + + for route in challenge_routes { + result.push(RegisteredChallengeRoute { + challenge_id: challenge_id.clone(), + challenge_name: challenge_name.clone(), + route: ChallengeRouteInfo { + method: route.method.as_str().to_string(), + path: route.path.clone(), + full_path: format!("/challenge/{}{}", challenge_id, route.path), + description: route.description.clone(), + requires_auth: route.requires_auth, + rate_limit: route.rate_limit, + }, + }); + } + } + result + } + + /// Handle a JSON-RPC request + pub fn handle(&self, req: JsonRpcRequest) -> JsonRpcResponse { + debug!("RPC: {}", req.method); + + // Route to appropriate handler based on namespace + let parts: Vec<&str> = req.method.splitn(2, '_').collect(); + + match parts.as_slice() { + // System namespace + ["system", "health"] => self.system_health(req.id), + ["system", "version"] => self.system_version(req.id), + ["system", "name"] => self.system_name(req.id), + ["system", "properties"] => self.system_properties(req.id), + ["system", "peers"] => self.system_peers(req.id), + ["system", "networkState"] => self.system_network_state(req.id), + + // Chain namespace + ["chain", "getHead"] => self.chain_get_head(req.id), + ["chain", "getBlock"] => self.chain_get_block(req.id, req.params), + ["chain", "getBlockHash"] => self.chain_get_block_hash(req.id, req.params), + ["chain", "getFinalizedHead"] => self.chain_get_finalized_head(req.id), + ["chain", "getState"] => self.chain_get_state(req.id), + + // State namespace + ["state", "getStorage"] => self.state_get_storage(req.id, req.params), + ["state", "getKeys"] => self.state_get_keys(req.id, req.params), + ["state", "getMetadata"] => self.state_get_metadata(req.id), + ["state", "getRuntimeVersion"] => self.state_get_runtime_version(req.id), + + // Validator namespace + ["validator", "list"] => self.validator_list(req.id, req.params), + ["validator", "get"] => self.validator_get(req.id, req.params), + ["validator", "count"] => self.validator_count(req.id), + + // Metagraph namespace + ["metagraph", "hotkeys"] => self.metagraph_hotkeys(req.id), + ["metagraph", "isRegistered"] => self.metagraph_is_registered(req.id, req.params), + + // Challenge namespace + ["challenge", "list"] => self.challenge_list(req.id, req.params), + ["challenge", "get"] => self.challenge_get(req.id, req.params), + ["challenge", "getRoutes"] => self.challenge_get_routes(req.id, req.params), + ["challenge", "listAllRoutes"] => self.challenge_list_all_routes(req.id), + // challenge_call is handled asynchronously via handle_async() + ["challenge", "call"] => JsonRpcResponse::error( + req.id, + INTERNAL_ERROR, + "challenge_call must be invoked via handle_async()", + ), + + // Job namespace + ["job", "list"] => self.job_list(req.id, req.params), + ["job", "get"] => self.job_get(req.id, req.params), + + // Epoch namespace + ["epoch", "current"] => self.epoch_current(req.id), + ["epoch", "getPhase"] => self.epoch_get_phase(req.id), + + // Leaderboard namespace + ["leaderboard", "get"] => self.leaderboard_get(req.id, req.params), + + // Evaluation namespace + ["evaluation", "getProgress"] => self.evaluation_get_progress(req.id, req.params), + ["evaluation", "getLogs"] => self.evaluation_get_logs(req.id, req.params), + + // Agent namespace + ["agent", "getCode"] => self.agent_get_code(req.id, req.params), + ["agent", "getLogs"] => self.agent_get_logs(req.id, req.params), + + // RPC info + ["rpc", "methods"] => self.rpc_methods(req.id), + + // Sudo namespace (for subnet owner actions) + ["sudo", "submit"] => self.sudo_submit(req.id, req.params), + + _ => { + warn!("Unknown RPC method: {}", req.method); + JsonRpcResponse::error( + req.id, + METHOD_NOT_FOUND, + format!( + "Method not found: {}. Use rpc_methods to list available methods.", + req.method + ), + ) + } + } + } + + // ==================== RPC Info ==================== + + fn rpc_methods(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result( + id, + json!({ + "version": 1, + "methods": [ + // System + "system_health", "system_version", "system_name", + "system_properties", "system_peers", "system_networkState", + // Chain + "chain_getHead", "chain_getBlock", "chain_getBlockHash", + "chain_getFinalizedHead", "chain_getState", + // State + "state_getStorage", "state_getKeys", "state_getMetadata", + "state_getRuntimeVersion", + // Validator + "validator_list", "validator_get", "validator_count", + // Challenge + "challenge_list", "challenge_get", "challenge_getRoutes", + "challenge_listAllRoutes", "challenge_call", + // Job + "job_list", "job_get", + // Epoch + "epoch_current", "epoch_getPhase", + // Leaderboard + "leaderboard_get", + // Evaluation + "evaluation_getProgress", "evaluation_getLogs", + // Agent + "agent_getCode", "agent_getLogs", + // RPC + "rpc_methods", + // Monitor + ] + }), + ) + } + + // ==================== System Namespace ==================== + + fn system_health(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let peers_count = self.peers.read().len(); + + JsonRpcResponse::result( + id, + json!({ + "isSyncing": false, + "peers": peers_count, + "shouldHavePeers": true, + "health": if peers_count > 0 || !chain.validators.is_empty() { "healthy" } else { "degraded" } + }), + ) + } + + fn system_version(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(self.version)) + } + + fn system_name(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result(id, json!(self.chain_name)) + } + + fn system_properties(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + JsonRpcResponse::result( + id, + json!({ + "netuid": self.netuid, + "tokenSymbol": "TAO", + "tokenDecimals": 9, + "ss58Format": 42, + "minStake": chain.config.min_stake.0, + "minStakeTao": chain.config.min_stake.as_tao(), + "consensusThreshold": chain.config.consensus_threshold, + "blockTimeMs": chain.config.block_time_ms, + }), + ) + } + + fn system_peers(&self, id: Value) -> JsonRpcResponse { + let peers = self.peers.read(); + JsonRpcResponse::result(id, json!(peers.clone())) + } + + fn system_network_state(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let peers = self.peers.read(); + + JsonRpcResponse::result( + id, + json!({ + "peerId": null, + "listenedAddresses": [], + "connectedPeers": peers.len(), + "notConnectedPeers": [], + "averagePing": null, + "validators": chain.validators.len(), + }), + ) + } + + // ==================== Chain Namespace ==================== + + fn chain_get_head(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + JsonRpcResponse::result( + id, + json!({ + "number": chain.block_height, + "hash": format!("0x{}", hex::encode(&chain.state_hash)), + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "stateRoot": format!("0x{}", hex::encode(&chain.state_hash)), + "extrinsicsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + }), + ) + } + + fn chain_get_block(&self, id: Value, params: Value) -> JsonRpcResponse { + let block_num = self.get_param_u64(¶ms, 0, "number"); + let chain = self.chain_state.read(); + + // For now, only current block is available + if block_num.map(|n| n == chain.block_height).unwrap_or(true) { + JsonRpcResponse::result( + id, + json!({ + "block": { + "header": { + "number": chain.block_height, + "hash": format!("0x{}", hex::encode(&chain.state_hash)), + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "stateRoot": format!("0x{}", hex::encode(&chain.state_hash)), + }, + "extrinsics": [] + }, + "justifications": null + }), + ) + } else { + JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Block not found (only current block available)", + ) + } + } + + fn chain_get_block_hash(&self, id: Value, params: Value) -> JsonRpcResponse { + let block_num = self.get_param_u64(¶ms, 0, "number"); + let chain = self.chain_state.read(); + + if block_num.map(|n| n == chain.block_height).unwrap_or(true) { + JsonRpcResponse::result(id, json!(format!("0x{}", hex::encode(&chain.state_hash)))) + } else { + JsonRpcResponse::result(id, Value::Null) + } + } + + fn chain_get_finalized_head(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + JsonRpcResponse::result(id, json!(format!("0x{}", hex::encode(&chain.state_hash)))) + } + + fn chain_get_state(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + + // Serialize mechanism_configs + let mechanism_configs: serde_json::Map = chain + .mechanism_configs + .iter() + .map(|(id, config)| { + ( + id.to_string(), + json!({ + "mechanism_id": config.mechanism_id, + "base_burn_rate": config.base_burn_rate, + "equal_distribution": config.equal_distribution, + "min_weight_threshold": config.min_weight_threshold, + "max_weight_cap": config.max_weight_cap, + "is_active": config.active, + }), + ) + }) + .collect(); + + // Serialize challenge_weights + let challenge_weights: serde_json::Map = chain + .challenge_weights + .iter() + .map(|(id, alloc)| { + ( + id.to_string(), + json!({ + "challenge_id": id.to_string(), + "mechanism_id": alloc.mechanism_id, + "weight_ratio": alloc.weight_ratio, + "active": alloc.active, + }), + ) + }) + .collect(); + + // Serialize validators + let validators: serde_json::Map = chain + .validators + .iter() + .map(|(hotkey, info)| { + ( + hotkey.to_hex(), + json!({ + "hotkey": hotkey.to_hex(), + "stake": info.stake.0, + "stake_tao": info.stake.as_tao(), + }), + ) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "blockHeight": chain.block_height, + "epoch": chain.epoch, + "stateHash": format!("0x{}", hex::encode(&chain.state_hash)), + "sudoKey": chain.sudo_key.to_hex(), + "validators": validators, + "challenges": chain.challenges.len(), + "mechanism_configs": mechanism_configs, + "challenge_weights": challenge_weights, + "pendingJobs": chain.pending_jobs.len(), + "config": { + "subnetId": chain.config.subnet_id, + "minStake": chain.config.min_stake.0, + "minStakeTao": chain.config.min_stake.as_tao(), + "consensusThreshold": chain.config.consensus_threshold, + "blockTimeMs": chain.config.block_time_ms, + "maxValidators": chain.config.max_validators, + } + }), + ) + } + + // ==================== State Namespace ==================== + + fn state_get_storage(&self, id: Value, params: Value) -> JsonRpcResponse { + let key = self.get_param_str(¶ms, 0, "key"); + + let key = match key { + Some(k) => k, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'key' parameter"), + }; + + let chain = self.chain_state.read(); + + let result = match key.as_str() { + "blockHeight" => json!(chain.block_height), + "epoch" => json!(chain.epoch), + "stateHash" => json!(format!("0x{}", hex::encode(&chain.state_hash))), + "sudoKey" => json!(chain.sudo_key.to_hex()), + "validatorCount" => json!(chain.validators.len()), + "challengeCount" => json!(chain.challenges.len()), + "jobCount" => json!(chain.pending_jobs.len()), + k if k.starts_with("validator:") => { + let hotkey = &k[10..]; + if let Some(hk) = platform_core::Hotkey::from_hex(hotkey) { + if let Some(v) = chain.validators.get(&hk) { + json!({ + "hotkey": v.hotkey.to_hex(), + "stake": v.stake.0, + "stakeTao": v.stake.as_tao(), + "isActive": v.is_active, + "lastSeen": v.last_seen.to_rfc3339(), + "peerId": v.peer_id, + }) + } else { + Value::Null + } + } else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hotkey format"); + } + } + k if k.starts_with("challenge:") => { + let challenge_id = &k[10..]; + let challenge = chain + .challenges + .values() + .find(|c| c.id.to_string() == challenge_id || c.name == challenge_id); + + if let Some(c) = challenge { + json!({ + "id": c.id.to_string(), + "name": c.name, + "description": c.description, + "codeHash": c.code_hash, + "isActive": c.is_active, + "mechanismId": c.config.mechanism_id, + "emissionWeight": c.config.emission_weight, + "timeoutSecs": c.config.timeout_secs, + }) + } else { + Value::Null + } + } + _ => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("Unknown storage key: {}. Available: blockHeight, epoch, stateHash, sudoKey, validatorCount, challengeCount, jobCount, validator:, challenge:", key), + ); + } + }; + + JsonRpcResponse::result(id, result) + } + + fn state_get_keys(&self, id: Value, params: Value) -> JsonRpcResponse { + let prefix = self.get_param_str(¶ms, 0, "prefix").unwrap_or_default(); + let chain = self.chain_state.read(); + + let mut keys = vec![ + "blockHeight", + "epoch", + "stateHash", + "sudoKey", + "validatorCount", + "challengeCount", + "jobCount", + ] + .into_iter() + .map(String::from) + .collect::>(); + + // Add validator keys + for v in chain.validators.keys() { + keys.push(format!("validator:{}", v.to_hex())); + } + + // Add challenge keys + for c in chain.challenges.values() { + keys.push(format!("challenge:{}", c.id)); + keys.push(format!("challenge:{}", c.name)); + } + + // Filter by prefix + let filtered: Vec<_> = keys + .into_iter() + .filter(|k| k.starts_with(&prefix)) + .collect(); + + JsonRpcResponse::result(id, json!(filtered)) + } + + fn state_get_metadata(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let routes = self.challenge_routes.read(); + + JsonRpcResponse::result( + id, + json!({ + "version": self.version, + "pallets": [ + { + "name": "System", + "storage": ["blockHeight", "epoch", "stateHash"] + }, + { + "name": "Validators", + "storage": ["validatorCount"], + "calls": ["validator_list", "validator_get"] + }, + { + "name": "Challenges", + "storage": ["challengeCount"], + "calls": ["challenge_list", "challenge_get", "challenge_getRoutes"] + }, + { + "name": "Jobs", + "storage": ["jobCount"], + "calls": ["job_list", "job_get"] + } + ], + "extrinsics": [], + "constants": { + "netuid": self.netuid, + "minStake": chain.config.min_stake.0, + }, + "challengeRoutes": routes.len(), + }), + ) + } + + fn state_get_runtime_version(&self, id: Value) -> JsonRpcResponse { + JsonRpcResponse::result( + id, + json!({ + "specName": "platform", + "implName": "platform-node", + "specVersion": 1, + "implVersion": 1, + "apis": [ + ["system", 1], + ["chain", 1], + ["state", 1], + ["validator", 1], + ["challenge", 1], + ["job", 1], + ["epoch", 1], + ], + }), + ) + } + + // ==================== Validator Namespace ==================== + + fn validator_list(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let offset = self.get_param_u64(¶ms, 0, "offset").unwrap_or(0) as usize; + let limit = self + .get_param_u64(¶ms, 1, "limit") + .unwrap_or(100) + .min(1000) as usize; + + let validators: Vec = chain + .validators + .values() + .skip(offset) + .take(limit) + .map(|v| { + json!({ + "hotkey": v.hotkey.to_hex(), + "stake": v.stake.0, + "stakeTao": v.stake.as_tao(), + "isActive": v.is_active, + "lastSeen": v.last_seen.to_rfc3339(), + "peerId": v.peer_id, + }) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "total": chain.validators.len(), + "offset": offset, + "limit": limit, + "validators": validators, + }), + ) + } + + fn validator_get(&self, id: Value, params: Value) -> JsonRpcResponse { + let hotkey = match self.get_param_str(¶ms, 0, "hotkey") { + Some(h) => h, + None => { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'hotkey' parameter") + } + }; + + let hk = match platform_core::Hotkey::from_hex(&hotkey) { + Some(h) => h, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hotkey format"), + }; + + let chain = self.chain_state.read(); + + match chain.validators.get(&hk) { + Some(v) => JsonRpcResponse::result( + id, + json!({ + "hotkey": v.hotkey.to_hex(), + "stake": v.stake.0, + "stakeTao": v.stake.as_tao(), + "isActive": v.is_active, + "lastSeen": v.last_seen.to_rfc3339(), + "peerId": v.peer_id, + }), + ), + None => JsonRpcResponse::error(id, VALIDATOR_NOT_FOUND, "Validator not found"), + } + } + + fn validator_count(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + JsonRpcResponse::result(id, json!(chain.validators.len())) + } + + // ==================== Metagraph Namespace ==================== + + /// Get all registered hotkeys from metagraph (miners + validators) + fn metagraph_hotkeys(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let hotkeys: Vec = chain + .registered_hotkeys + .iter() + .map(|h| h.to_hex()) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "count": hotkeys.len(), + "hotkeys": hotkeys, + }), + ) + } + + /// Check if a hotkey is registered in the metagraph + fn metagraph_is_registered(&self, id: Value, params: Value) -> JsonRpcResponse { + let hotkey = match self.get_param_str(¶ms, 0, "hotkey") { + Some(h) => h, + None => { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'hotkey' parameter") + } + }; + + let hk = match platform_core::Hotkey::from_hex(&hotkey) { + Some(h) => h, + None => { + // Try SS58 format + match platform_core::Hotkey::from_ss58(&hotkey) { + Some(h) => h, + None => { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hotkey format") + } + } + } + }; + + let chain = self.chain_state.read(); + let is_registered = chain.registered_hotkeys.contains(&hk); + + JsonRpcResponse::result( + id, + json!({ + "hotkey": hotkey, + "isRegistered": is_registered, + }), + ) + } + + // ==================== Challenge Namespace ==================== + + fn challenge_list(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let routes = self.challenge_routes.read(); + let only_active = self.get_param_bool(¶ms, "onlyActive").unwrap_or(false); + + // Get WASM challenges + let challenges: Vec = chain + .challenges + .values() + .filter(|c| !only_active || c.is_active) + .map(|c| { + let challenge_routes = routes.get(&c.id.to_string()).map(|r| r.len()).unwrap_or(0); + + json!({ + "id": c.id.to_string(), + "name": c.name, + "description": c.description, + "codeHash": c.code_hash, + "isActive": c.is_active, + "owner": c.owner.to_hex(), + "mechanismId": c.config.mechanism_id, + "emissionWeight": c.config.emission_weight, + "timeoutSecs": c.config.timeout_secs, + "routesCount": challenge_routes, + "type": "wasm", + }) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "total": challenges.len(), + "challenges": challenges, + }), + ) + } + + fn challenge_get(&self, id: Value, params: Value) -> JsonRpcResponse { + let challenge_id = match self.get_param_str(¶ms, 0, "id") { + Some(c) => c, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'id' parameter"), + }; + + let chain = self.chain_state.read(); + let routes = self.challenge_routes.read(); + + let challenge = chain + .challenges + .values() + .find(|c| c.id.to_string() == challenge_id || c.name == challenge_id); + + match challenge { + Some(c) => { + let challenge_routes: Vec = routes + .get(&c.id.to_string()) + .map(|rs| { + rs.iter() + .map(|r| { + json!({ + "method": r.method.as_str(), + "path": r.path, + "fullPath": format!("/challenge/{}{}", c.id, r.path), + "description": r.description, + "requiresAuth": r.requires_auth, + "rateLimit": r.rate_limit, + }) + }) + .collect() + }) + .unwrap_or_default(); + + JsonRpcResponse::result( + id, + json!({ + "id": c.id.to_string(), + "name": c.name, + "description": c.description, + "codeHash": c.code_hash, + "codeSize": c.wasm_code.len(), + "isActive": c.is_active, + "owner": c.owner.to_hex(), + "mechanismId": c.config.mechanism_id, + "emissionWeight": c.config.emission_weight, + "timeoutSecs": c.config.timeout_secs, + "createdAt": c.created_at.to_rfc3339(), + "routes": challenge_routes, + }), + ) + } + None => JsonRpcResponse::error( + id, + CHALLENGE_NOT_FOUND, + format!("Challenge '{}' not found", challenge_id), + ), + } + } + + fn challenge_get_routes(&self, id: Value, params: Value) -> JsonRpcResponse { + let challenge_id = match self.get_param_str(¶ms, 0, "id") { + Some(c) => c, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'id' parameter"), + }; + + let routes = self.challenge_routes.read(); + let chain = self.chain_state.read(); + + // Find actual challenge ID (might be name) + let actual_id = chain + .challenges + .values() + .find(|c| c.id.to_string() == challenge_id || c.name == challenge_id) + .map(|c| c.id.to_string()) + .unwrap_or_else(|| challenge_id.clone()); + + match routes.get(&actual_id) { + Some(challenge_routes) => { + let routes_json: Vec = challenge_routes + .iter() + .map(|r| { + json!({ + "method": r.method.as_str(), + "path": r.path, + "fullPath": format!("/challenge/{}{}", actual_id, r.path), + "description": r.description, + "requiresAuth": r.requires_auth, + "rateLimit": r.rate_limit, + }) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "challengeId": actual_id, + "routesCount": routes_json.len(), + "routes": routes_json, + }), + ) + } + None => JsonRpcResponse::result( + id, + json!({ + "challengeId": actual_id, + "routesCount": 0, + "routes": [], + }), + ), + } + } + + fn challenge_list_all_routes(&self, id: Value) -> JsonRpcResponse { + let all_routes = self.get_all_challenge_routes(); + + let routes_json: Vec = all_routes + .iter() + .map(|r| { + json!({ + "challengeId": r.challenge_id, + "challengeName": r.challenge_name, + "method": r.route.method, + "path": r.route.path, + "fullPath": r.route.full_path, + "description": r.route.description, + "requiresAuth": r.route.requires_auth, + }) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "total": routes_json.len(), + "routes": routes_json, + }), + ) + } + + // ==================== Async Handler ==================== + + /// Handle a JSON-RPC request, supporting both sync and async methods. + /// + /// Methods like `challenge_call` require async execution (the route handler + /// callback is async). This method handles those asynchronously and delegates + /// all other methods to the synchronous [`handle()`](Self::handle). + pub async fn handle_async(&self, req: JsonRpcRequest) -> JsonRpcResponse { + let parts: Vec<&str> = req.method.splitn(2, '_').collect(); + match parts.as_slice() { + ["challenge", "call"] => self.challenge_call(req.id, req.params).await, + _ => self.handle(req), + } + } + + /// Call a challenge route handler + async fn challenge_call(&self, id: Value, params: Value) -> JsonRpcResponse { + let challenge_id = match self.get_param_str(¶ms, 0, "challengeId") { + Some(c) => c, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'challengeId' parameter", + ) + } + }; + + let method = self + .get_param_str(¶ms, 1, "method") + .unwrap_or_else(|| "GET".to_string()); + + let path = self + .get_param_str(¶ms, 2, "path") + .unwrap_or_else(|| "/".to_string()); + + let body = params + .get("body") + .or_else(|| params.get(3)) + .cloned() + .unwrap_or(Value::Null); + + let query: std::collections::HashMap = params + .get("query") + .or_else(|| params.get(4)) + .and_then(|v| serde_json::from_value(v.clone()).ok()) + .unwrap_or_default(); + + // Verify the challenge has registered routes + { + let routes = self.challenge_routes.read(); + if !routes.contains_key(&challenge_id) { + // Try to find by name + let chain = self.chain_state.read(); + let found = chain.challenges.values().any(|c| c.name == challenge_id); + if !found { + return JsonRpcResponse::error( + id, + CHALLENGE_NOT_FOUND, + format!("Challenge '{}' not found or has no routes", challenge_id), + ); + } + } + } + + let request = RouteRequest { + method, + path, + params: std::collections::HashMap::new(), + query, + headers: std::collections::HashMap::new(), + body, + auth_hotkey: None, + }; + + let maybe_handler = self.route_handler.read().clone(); + match maybe_handler { + Some(handler) => { + let response = handler(challenge_id.clone(), request).await; + JsonRpcResponse::result( + id, + json!({ + "challengeId": challenge_id, + "status": response.status, + "headers": response.headers, + "body": response.body, + }), + ) + } + None => JsonRpcResponse::error( + id, + INTERNAL_ERROR, + "No route handler registered. Challenge route handlers are not configured.", + ), + } + } + + // ==================== Job Namespace ==================== + + fn job_list(&self, id: Value, params: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let offset = self.get_param_u64(¶ms, 0, "offset").unwrap_or(0) as usize; + let limit = self + .get_param_u64(¶ms, 1, "limit") + .unwrap_or(100) + .min(1000) as usize; + let status_filter = self.get_param_str(¶ms, 2, "status"); + + let jobs: Vec = chain + .pending_jobs + .iter() + .filter(|j| { + status_filter + .as_ref() + .map(|s| format!("{:?}", j.status).to_lowercase() == s.to_lowercase()) + .unwrap_or(true) + }) + .skip(offset) + .take(limit) + .map(|j| { + json!({ + "id": j.id.to_string(), + "challengeId": j.challenge_id.to_string(), + "agentHash": j.agent_hash, + "status": format!("{:?}", j.status), + "createdAt": j.created_at.to_rfc3339(), + "assignedValidator": j.assigned_validator.as_ref().map(|h| h.to_hex()), + }) + }) + .collect(); + + JsonRpcResponse::result( + id, + json!({ + "total": chain.pending_jobs.len(), + "offset": offset, + "limit": limit, + "jobs": jobs, + }), + ) + } + + fn job_get(&self, id: Value, params: Value) -> JsonRpcResponse { + let job_id = match self.get_param_str(¶ms, 0, "id") { + Some(j) => j, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing 'id' parameter"), + }; + + let job_uuid = match uuid::Uuid::parse_str(&job_id) { + Ok(u) => u, + Err(_) => return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid job ID format"), + }; + + let chain = self.chain_state.read(); + + match chain.pending_jobs.iter().find(|j| j.id == job_uuid) { + Some(j) => JsonRpcResponse::result( + id, + json!({ + "id": j.id.to_string(), + "challengeId": j.challenge_id.to_string(), + "agentHash": j.agent_hash, + "status": format!("{:?}", j.status), + "createdAt": j.created_at.to_rfc3339(), + "assignedValidator": j.assigned_validator.as_ref().map(|h| h.to_hex()), + "result": j.result.as_ref().map(|r| json!({ + "value": r.value, + "weight": r.weight, + })), + }), + ), + None => { + JsonRpcResponse::error(id, JOB_NOT_FOUND, format!("Job '{}' not found", job_id)) + } + } + } + + // ==================== Epoch Namespace ==================== + + fn epoch_current(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + + // Epoch config from runtime + let blocks_per_epoch = 100u64; + let block_in_epoch = chain.block_height % blocks_per_epoch; + + let (phase, phase_start, phase_end) = if block_in_epoch < 75 { + ("evaluation", 0, 75) + } else if block_in_epoch < 88 { + ("commit", 75, 88) + } else { + ("reveal", 88, blocks_per_epoch) + }; + + let blocks_until_next = match phase { + "evaluation" => 75 - block_in_epoch, + "commit" => 88 - block_in_epoch, + _ => blocks_per_epoch - block_in_epoch, + }; + + JsonRpcResponse::result( + id, + json!({ + "epochNumber": chain.epoch, + "currentBlock": chain.block_height, + "blocksPerEpoch": blocks_per_epoch, + "blockInEpoch": block_in_epoch, + "phase": phase, + "phaseStart": phase_start, + "phaseEnd": phase_end, + "blocksUntilNextPhase": blocks_until_next, + "progress": (block_in_epoch as f64 / blocks_per_epoch as f64 * 100.0).round() / 100.0, + "estimatedTimeToNextPhase": blocks_until_next * 12, // 12s per block + }), + ) + } + + fn epoch_get_phase(&self, id: Value) -> JsonRpcResponse { + let chain = self.chain_state.read(); + let blocks_per_epoch = 100u64; + let block_in_epoch = chain.block_height % blocks_per_epoch; + + let phase = if block_in_epoch < 75 { + "evaluation" + } else if block_in_epoch < 88 { + "commit" + } else { + "reveal" + }; + + JsonRpcResponse::result(id, json!(phase)) + } + + // ==================== Leaderboard Namespace ==================== + + fn leaderboard_get(&self, id: Value, params: Value) -> JsonRpcResponse { + let challenge_id = match self.get_param_str(¶ms, 0, "challenge_id") { + Some(c) => c, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'challenge_id' parameter", + ) + } + }; + let limit = self + .get_param_u64(¶ms, 1, "limit") + .unwrap_or(100) + .min(1000); + let offset = self.get_param_u64(¶ms, 2, "offset").unwrap_or(0); + + let chain = self.chain_state.read(); + + let challenge_uuid = chain + .challenges + .values() + .find(|c| c.id.to_string() == challenge_id || c.name == challenge_id) + .map(|c| c.id); + + match challenge_uuid { + Some(_cid) => JsonRpcResponse::result( + id, + json!({ + "challengeId": challenge_id, + "entries": [], + "total": 0, + "limit": limit, + "offset": offset, + }), + ), + None => JsonRpcResponse::error( + id, + CHALLENGE_NOT_FOUND, + format!("Challenge '{}' not found", challenge_id), + ), + } + } + + // ==================== Evaluation Namespace ==================== + + fn evaluation_get_progress(&self, id: Value, params: Value) -> JsonRpcResponse { + let submission_id = match self.get_param_str(¶ms, 0, "submission_id") { + Some(s) => s, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'submission_id' parameter", + ) + } + }; + + JsonRpcResponse::result( + id, + json!({ + "submissionId": submission_id, + "progress": [], + "total": 0, + }), + ) + } + + fn evaluation_get_logs(&self, id: Value, params: Value) -> JsonRpcResponse { + let submission_id = match self.get_param_str(¶ms, 0, "submission_id") { + Some(s) => s, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'submission_id' parameter", + ) + } + }; + + JsonRpcResponse::result( + id, + json!({ + "submissionId": submission_id, + "logs": null, + "validated": false, + }), + ) + } + + // ==================== Agent Namespace ==================== + + fn agent_get_code(&self, id: Value, params: Value) -> JsonRpcResponse { + let miner_hotkey = match self.get_param_str(¶ms, 0, "miner_hotkey") { + Some(h) => h, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'miner_hotkey' parameter", + ) + } + }; + let _epoch = self.get_param_u64(¶ms, 1, "epoch"); + + let _hk = match platform_core::Hotkey::from_hex(&miner_hotkey) { + Some(h) => h, + None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hotkey format"), + }; + + JsonRpcResponse::result( + id, + json!({ + "minerHotkey": miner_hotkey, + "entry": null, + }), + ) + } + + fn agent_get_logs(&self, id: Value, params: Value) -> JsonRpcResponse { + let miner_hotkey = match self.get_param_str(¶ms, 0, "miner_hotkey") { + Some(h) => h, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'miner_hotkey' parameter", + ) + } + }; + let _epoch = self.get_param_u64(¶ms, 1, "epoch"); + + JsonRpcResponse::result( + id, + json!({ + "minerHotkey": miner_hotkey, + "logs": [], + "total": 0, + }), + ) + } + + // ==================== Helper Methods ==================== + + fn get_param_str(&self, params: &Value, index: usize, name: &str) -> Option { + params + .get(index) + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + .or_else(|| { + params + .get(name) + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + }) + } + + fn get_param_u64(&self, params: &Value, index: usize, name: &str) -> Option { + params + .get(index) + .and_then(|v| v.as_u64()) + .or_else(|| params.get(name).and_then(|v| v.as_u64())) + } + + fn get_param_bool(&self, params: &Value, name: &str) -> Option { + params.get(name).and_then(|v| v.as_bool()) + } + + /// Update peers list + pub fn set_peers(&self, peers: Vec) { + *self.peers.write() = peers; + } + + /// Add a peer + pub fn add_peer(&self, peer: String) { + self.peers.write().push(peer); + } + + /// Remove a peer + pub fn remove_peer(&self, peer: &str) { + self.peers.write().retain(|p| p != peer); + } + + // ==================== Sudo Namespace ==================== + + /// Submit a signed sudo action to be broadcast via P2P + /// This allows csudo to submit actions via RPC instead of running its own P2P node + fn sudo_submit(&self, id: Value, params: Value) -> JsonRpcResponse { + // Get the signed message bytes (hex-encoded) + let message_hex = match self.get_param_str(¶ms, 0, "signedMessage") { + Some(m) => m, + None => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + "Missing 'signedMessage' parameter (hex-encoded)", + ) + } + }; + + // Decode hex to bytes + let message_bytes = match hex::decode(&message_hex) { + Ok(b) => b, + Err(e) => { + return JsonRpcResponse::error(id, INVALID_PARAMS, format!("Invalid hex: {}", e)) + } + }; + + // Verify it's a valid SignedNetworkMessage + let signed: platform_core::SignedNetworkMessage = match bincode::deserialize(&message_bytes) + { + Ok(s) => s, + Err(e) => { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("Invalid message format: {}", e), + ) + } + }; + + // Verify signature + if !signed.verify().unwrap_or(false) { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid signature"); + } + + // Check if it's from the sudo key + let (is_sudo, chain_sudo_key) = { + let state = self.chain_state.read(); + (state.is_sudo(signed.signer()), state.sudo_key.to_hex()) + }; + + if !is_sudo { + info!( + "Sudo check failed: signer={} chain_sudo={}", + signed.signer().to_hex(), + chain_sudo_key + ); + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!( + "Signer {} is not the sudo key {}", + signed.signer().to_hex(), + chain_sudo_key + ), + ); + } + + // Send to broadcast channel for P2P propagation + let tx = self.broadcast_tx.read(); + match tx.as_ref() { + Some(sender) => { + if let Err(e) = sender.send(message_bytes.clone()) { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + format!("Failed to queue broadcast: {}", e), + ); + } + info!( + "Sudo action queued for P2P broadcast from {}", + signed.signer() + ); + } + None => { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + "Broadcast channel not configured", + ); + } + } + + JsonRpcResponse::result( + id, + json!({ + "success": true, + "message": "Sudo action applied locally and queued for P2P broadcast", + "signer": signed.signer().to_hex(), + }), + ) + } + + // ==================== Monitor Namespace ==================== +} + +#[cfg(test)] +mod tests { + use super::*; + use platform_core::{Keypair, NetworkConfig}; + + fn create_handler() -> RpcHandler { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + RpcHandler::new(state, 1) + } + + #[test] + fn test_rpc_methods() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "rpc_methods".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_system_health() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_health".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + assert!(resp.error.is_none()); + } + + #[test] + fn test_chain_get_state() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getState".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_method_not_found() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "unknown_method".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, METHOD_NOT_FOUND); + } + + #[test] + fn test_validator_not_found() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_get".to_string(), + params: json!(["0000000000000000000000000000000000000000000000000000000000000000"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, VALIDATOR_NOT_FOUND); + } + + #[test] + fn test_system_version() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_version".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_system_name() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_name".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_system_properties() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_properties".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("tokenSymbol").is_some()); + assert_eq!(result["tokenSymbol"], "TAO"); + } + + #[test] + fn test_system_peers() { + let handler = create_handler(); + handler.add_peer("peer1".to_string()); + handler.add_peer("peer2".to_string()); + + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_peers".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let peers = resp.result.unwrap(); + assert_eq!(peers.as_array().unwrap().len(), 2); + } + + #[test] + fn test_system_network_state() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "system_networkState".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_chain_get_head() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getHead".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_chain_get_finalized_head() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getFinalizedHead".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_chain_get_block() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getBlock".to_string(), + params: json!([0]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_json_rpc_request_parsing() { + let json_str = r#"{"jsonrpc":"2.0","method":"test","params":null,"id":1}"#; + let req: JsonRpcRequest = serde_json::from_str(json_str).unwrap(); + assert_eq!(req.jsonrpc, "2.0"); + assert_eq!(req.method, "test"); + } + + #[test] + fn test_json_rpc_response_result() { + let resp = JsonRpcResponse::result(json!(1), json!({"data": "test"})); + assert!(resp.result.is_some()); + assert!(resp.error.is_none()); + assert_eq!(resp.id, json!(1)); + } + + #[test] + fn test_json_rpc_response_error() { + let resp = JsonRpcResponse::error(json!(2), -32600, "Invalid Request"); + assert!(resp.result.is_none()); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, -32600); + } + + #[test] + fn test_peer_management() { + let handler = create_handler(); + + // Add peers + handler.add_peer("peer1".to_string()); + handler.add_peer("peer2".to_string()); + assert_eq!(handler.peers.read().len(), 2); + + // Remove peer + handler.remove_peer("peer1"); + assert_eq!(handler.peers.read().len(), 1); + + // Set peers + handler.set_peers(vec!["a".to_string(), "b".to_string(), "c".to_string()]); + assert_eq!(handler.peers.read().len(), 3); + } + + #[test] + fn test_invalid_params() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_get".to_string(), + params: json!([]), // Empty params, missing hotkey + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + } + + #[test] + fn test_chain_get_block_hash() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getBlockHash".to_string(), + params: json!([0]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_state_get_keys() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getKeys".to_string(), + params: json!(["challenges"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_state_get_runtime_version() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getRuntimeVersion".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_validator_list() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_list".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("validators").is_some()); + } + + #[test] + fn test_validator_list_with_pagination() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_list".to_string(), + params: json!([10, 50]), // offset, limit + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert_eq!(result["offset"], 10); + assert_eq!(result["limit"], 50); + } + + #[test] + fn test_validator_count() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_count".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_metagraph_hotkeys() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "metagraph_hotkeys".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("hotkeys").is_some()); + assert!(result.get("count").is_some()); + } + + #[test] + fn test_metagraph_is_registered_invalid_hotkey() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "metagraph_isRegistered".to_string(), + params: json!(["invalid_hotkey"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + } + + #[test] + fn test_metagraph_is_registered_missing_param() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "metagraph_isRegistered".to_string(), + params: json!([]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_challenge_list() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_list".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("challenges").is_some()); + } + + #[test] + fn test_challenge_list_only_active() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_list".to_string(), + params: json!({"onlyActive": true}), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_challenge_get_not_found() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_get".to_string(), + params: json!(["nonexistent"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, CHALLENGE_NOT_FOUND); + } + + #[test] + fn test_challenge_get_missing_param() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_get".to_string(), + params: json!([]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_challenge_get_routes_no_routes() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_getRoutes".to_string(), + params: json!(["test-challenge"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert_eq!(result["routesCount"], 0); + } + + #[test] + fn test_challenge_list_all_routes() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_listAllRoutes".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("routes").is_some()); + } + + #[test] + fn test_job_list() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "job_list".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("jobs").is_some()); + } + + #[test] + fn test_job_list_with_filter() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "job_list".to_string(), + params: json!([0, 100, "pending"]), // offset, limit, status + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_job_get_invalid_id() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "job_get".to_string(), + params: json!(["not-a-uuid"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_job_get_not_found() { + let handler = create_handler(); + let job_id = uuid::Uuid::new_v4().to_string(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "job_get".to_string(), + params: json!([job_id]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, JOB_NOT_FOUND); + } + + #[test] + fn test_job_get_missing_param() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "job_get".to_string(), + params: json!([]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_epoch_current() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "epoch_current".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("epochNumber").is_some()); + assert!(result.get("phase").is_some()); + assert!(result.get("blocksPerEpoch").is_some()); + } + + #[test] + fn test_epoch_get_phase() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "epoch_getPhase".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let phase = resp.result.unwrap(); + assert!(phase.is_string()); + } + + #[test] + fn test_state_get_storage_invalid_key() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!(["unknownKey"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_state_get_storage_block_height() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!(["blockHeight"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_state_get_storage_missing_key() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!([]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_state_get_storage_validator_key() { + let handler = create_handler(); + let kp = Keypair::generate(); + let key = format!("validator:{}", kp.hotkey().to_hex()); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!([key]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_state_get_storage_challenge_key() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!(["challenge:test"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + } + + #[test] + fn test_state_get_metadata() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getMetadata".to_string(), + params: Value::Null, + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert!(result.get("pallets").is_some()); + } + + #[test] + fn test_sudo_submit_missing_param() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "sudo_submit".to_string(), + params: json!([]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_sudo_submit_invalid_hex() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "sudo_submit".to_string(), + params: json!(["not-hex"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_json_rpc_error_with_data() { + let resp = JsonRpcResponse::error_with_data( + json!(1), + -32000, + "Custom error", + json!({"detail": "extra info"}), + ); + assert!(resp.error.is_some()); + let error = resp.error.unwrap(); + assert_eq!(error.code, -32000); + assert!(error.data.is_some()); + } + + #[test] + fn test_normalize_challenge_name() { + assert_eq!( + RpcHandler::normalize_challenge_name("Test Challenge"), + "test-challenge" + ); + assert_eq!( + RpcHandler::normalize_challenge_name("My_Cool_Challenge"), + "my-cool-challenge" + ); + assert_eq!(RpcHandler::normalize_challenge_name(" Spaces "), "spaces"); + assert_eq!( + RpcHandler::normalize_challenge_name("Special!@#$%Chars"), + "specialchars" + ); + } + + #[test] + fn test_register_challenge_routes() { + let handler = create_handler(); + use platform_challenge_sdk::ChallengeRoute; + + let routes = vec![ + ChallengeRoute::get("/test", "Test route"), + ChallengeRoute::post("/submit", "Submit route"), + ]; + + handler.register_challenge_routes("test-challenge", routes); + + let registered = handler.challenge_routes.read(); + assert!(registered.contains_key("test-challenge")); + assert_eq!(registered.get("test-challenge").unwrap().len(), 2); + } + + #[test] + fn test_unregister_challenge_routes() { + let handler = create_handler(); + use platform_challenge_sdk::ChallengeRoute; + + let routes = vec![ChallengeRoute::get("/test", "Test route")]; + handler.register_challenge_routes("test-challenge", routes); + + handler.unregister_challenge_routes("test-challenge"); + + let registered = handler.challenge_routes.read(); + assert!(!registered.contains_key("test-challenge")); + } + + #[test] + fn test_get_all_challenge_routes() { + let handler = create_handler(); + use platform_challenge_sdk::ChallengeRoute; + + let routes = vec![ChallengeRoute::get("/test", "Test route")]; + handler.register_challenge_routes("test-challenge", routes); + + let all_routes = handler.get_all_challenge_routes(); + assert_eq!(all_routes.len(), 1); + } + + #[test] + fn test_set_keypair() { + let handler = create_handler(); + let kp = Keypair::generate(); + handler.set_keypair(kp.clone()); + + let stored = handler.keypair.read(); + assert!(stored.is_some()); + } + + #[test] + fn test_json_rpc_request_default_params() { + let json_str = r#"{"jsonrpc":"2.0","method":"test","id":1}"#; + let req: JsonRpcRequest = serde_json::from_str(json_str).unwrap(); + assert_eq!(req.params, Value::Null); + } + + #[test] + fn test_get_param_helpers() { + let handler = create_handler(); + let params = json!([10, "test", true]); + + assert_eq!(handler.get_param_u64(¶ms, 0, "x"), Some(10)); + assert_eq!( + handler.get_param_str(¶ms, 1, "y"), + Some("test".to_string()) + ); + } + + #[test] + fn test_set_broadcast_tx() { + let handler = create_handler(); + let (tx, _rx) = tokio::sync::mpsc::unbounded_channel(); + handler.set_broadcast_tx(tx); + assert!(handler.broadcast_tx.read().is_some()); + } + + #[test] + fn test_set_route_handler() { + let handler = create_handler(); + let route_handler: ChallengeRouteHandler = Arc::new(|_challenge_id, _req| { + Box::pin(async move { + ChallengeRouteResponse { + status: 200, + body: json!({"success": true}), + headers: std::collections::HashMap::new(), + } + }) + }); + handler.set_route_handler(route_handler); + assert!(handler.route_handler.read().is_some()); + } + + #[test] + fn test_chain_get_block_invalid_number() { + let handler = create_handler(); + // Request a block that doesn't exist + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getBlock".to_string(), + params: json!([999]), // Block that doesn't exist + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + } + + #[test] + fn test_chain_get_block_hash_invalid() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "chain_getBlockHash".to_string(), + params: json!([999]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + assert_eq!(resp.result.unwrap(), serde_json::Value::Null); + } + + #[test] + fn test_state_get_storage_validator_key_invalid_hotkey() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "state_getStorage".to_string(), + params: json!(["validator:invalid_hex"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + } + + #[test] + fn test_validator_get_invalid_hotkey_format() { + let handler = create_handler(); + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "validator_get".to_string(), + params: json!(["not_a_valid_hotkey"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap().code, INVALID_PARAMS); + } + + #[test] + fn test_metagraph_is_registered_with_valid_hotkey() { + let handler = create_handler(); + let kp = Keypair::generate(); + + // Add hotkey to registered_hotkeys + { + let mut chain = handler.chain_state.write(); + chain.registered_hotkeys.insert(kp.hotkey()); + } + + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "metagraph_isRegistered".to_string(), + params: json!([kp.hotkey().to_hex()]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert_eq!(result["isRegistered"], true); + } + + #[test] + fn test_challenge_get_with_routes() { + let handler = create_handler(); + use platform_challenge_sdk::ChallengeRoute; + + // Add a challenge + let kp = Keypair::generate(); + let challenge_id = platform_core::ChallengeId::new(); + let config = platform_core::ChallengeConfig::default(); + let challenge = platform_core::Challenge { + id: challenge_id, + name: "test-challenge".to_string(), + description: "Test description".to_string(), + wasm_code: vec![1, 2, 3], + code_hash: "abc123".to_string(), + wasm_metadata: platform_core::WasmModuleMetadata::from_code_hash("abc123".to_string()), + owner: kp.hotkey(), + config, + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + is_active: true, + }; + + handler + .chain_state + .write() + .challenges + .insert(challenge_id, challenge); + + // Register routes for the challenge + let routes = vec![ + ChallengeRoute::get("/test", "Test route"), + ChallengeRoute::post("/submit", "Submit route"), + ]; + handler.register_challenge_routes(&challenge_id.to_string(), routes); + + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_get".to_string(), + params: json!([challenge_id.to_string()]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert_eq!(result["routes"].as_array().unwrap().len(), 2); + } + + #[test] + fn test_challenge_get_routes_by_name() { + let handler = create_handler(); + use platform_challenge_sdk::ChallengeRoute; + + // Add a challenge + let kp = Keypair::generate(); + let challenge_id = platform_core::ChallengeId::new(); + let config = platform_core::ChallengeConfig::default(); + let challenge = platform_core::Challenge { + id: challenge_id, + name: "my-challenge".to_string(), + description: "Test description".to_string(), + wasm_code: vec![], + code_hash: "abc123".to_string(), + wasm_metadata: platform_core::WasmModuleMetadata::from_code_hash("abc123".to_string()), + owner: kp.hotkey(), + config, + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), + is_active: true, + }; + + handler + .chain_state + .write() + .challenges + .insert(challenge_id, challenge); + + // Register routes + let routes = vec![ChallengeRoute::get("/status", "Status route")]; + handler.register_challenge_routes(&challenge_id.to_string(), routes); + + // Query by name instead of ID + let req = JsonRpcRequest { + jsonrpc: "2.0".to_string(), + method: "challenge_getRoutes".to_string(), + params: json!(["my-challenge"]), + id: json!(1), + }; + let resp = handler.handle(req); + assert!(resp.result.is_some()); + let result = resp.result.unwrap(); + assert_eq!(result["routesCount"], 1); + } + + #[test] + fn test_register_empty_routes() { + let handler = create_handler(); + // Registering empty routes should be a no-op + handler.register_challenge_routes("test-challenge", vec![]); + let routes = handler.challenge_routes.read(); + assert!(!routes.contains_key("test-challenge")); + } +} diff --git a/crates/rpc-server/src/lib.rs b/crates/rpc-server/src/lib.rs new file mode 100644 index 000000000..db3dd5f9f --- /dev/null +++ b/crates/rpc-server/src/lib.rs @@ -0,0 +1,35 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Mini-Chain RPC Server +//! +//! Substrate-style JSON-RPC 2.0 server for chain queries. +//! +//! Single endpoint: POST / with JSON-RPC request +//! +//! Methods: +//! - system_health - Health check +//! - system_version - Get version info +//! - system_peers - Get connected peers +//! - chain_getHead - Get latest block +//! - chain_getBlock - Get block by number +//! - chain_getState - Get full chain state +//! - state_getStorage - Get storage value by key +//! - state_getValidators - Get validators list +//! - state_getValidator - Get single validator +//! - state_getChallenges - Get challenges list +//! - state_getChallenge - Get single challenge +//! - state_getJobs - Get pending jobs +//! - state_getEpoch - Get epoch info + +mod auth; +mod handlers; +pub mod health; +mod jsonrpc; +mod server; +mod types; + +pub use auth::*; +pub use handlers::*; +pub use health::{create_health_check, HealthCheck, HealthResponse, HealthStatus, ReadinessStatus}; +pub use jsonrpc::*; +pub use server::*; +pub use types::*; diff --git a/crates/rpc-server/src/server.rs b/crates/rpc-server/src/server.rs new file mode 100644 index 000000000..670326f4d --- /dev/null +++ b/crates/rpc-server/src/server.rs @@ -0,0 +1,811 @@ +//! RPC Server implementation + +use crate::handlers::*; +use crate::jsonrpc::{JsonRpcRequest, JsonRpcResponse, RpcHandler, PARSE_ERROR}; +use crate::RpcState; +use axum::{ + http::StatusCode, + response::IntoResponse, + routing::{get, post}, + Json, Router, +}; +use parking_lot::RwLock; +use platform_challenge_sdk::RouteRequest; +use platform_core::ChainState; +use platform_subnet_manager::BanList; +use serde_json::Value; +use std::collections::HashMap; +use std::net::SocketAddr; +use std::sync::Arc; +use tower_http::cors::{Any, CorsLayer}; +use tower_http::trace::TraceLayer; +use tracing::{debug, info, trace, warn}; + +/// RPC server configuration +#[derive(Clone, Debug)] +pub struct RpcConfig { + /// Listen address + pub addr: SocketAddr, + /// Subnet UID + pub netuid: u16, + /// Subnet name + pub name: String, + /// Minimum stake for validators + pub min_stake: u64, + /// Enable CORS + pub cors_enabled: bool, +} + +impl Default for RpcConfig { + fn default() -> Self { + Self { + addr: "0.0.0.0:8080".parse().unwrap(), + netuid: 1, + name: "Mini-Chain".to_string(), + min_stake: 1_000_000_000_000, // 1000 TAO + cors_enabled: true, + } + } +} + +/// RPC Server +pub struct RpcServer { + config: RpcConfig, + state: Arc, + rpc_handler: Arc, +} + +impl RpcServer { + /// Create a new RPC server + pub fn new( + config: RpcConfig, + chain_state: Arc>, + bans: Arc>, + ) -> Self { + let state = Arc::new(RpcState::new( + chain_state.clone(), + bans, + config.netuid, + config.name.clone(), + config.min_stake, + )); + + let rpc_handler = Arc::new(RpcHandler::new(chain_state, config.netuid)); + + Self { + config, + state, + rpc_handler, + } + } + + /// Get the RPC handler (to update peers, etc.) + pub fn rpc_handler(&self) -> Arc { + self.rpc_handler.clone() + } + + /// Build the router + pub fn router(&self) -> Router { + let rpc_handler = self.rpc_handler.clone(); + + let mut router = Router::new() + // JSON-RPC 2.0 endpoint (Substrate-style) + .route( + "/", + post({ + let handler = rpc_handler.clone(); + move |body: Json| { + let handler = handler.clone(); + async move { jsonrpc_handler(body, handler).await } + } + }), + ) + // Also support /rpc for explicit path + .route( + "/rpc", + post({ + let handler = rpc_handler.clone(); + move |body: Json| { + let handler = handler.clone(); + async move { jsonrpc_handler(body, handler).await } + } + }), + ) + // Keep simple health endpoint for load balancers + .route("/health", get(health_handler)) + // Challenge custom routes: /challenge/{id}/*path + .route("/challenge/:challenge_id/*path", { + let handler = rpc_handler.clone(); + axum::routing::any( + move |axum::extract::Path((challenge_id, path)): axum::extract::Path<( + String, + String, + )>, + method: axum::http::Method, + axum::extract::Query(query): axum::extract::Query< + std::collections::HashMap, + >, + headers: axum::http::HeaderMap, + body: Option>| { + let handler = handler.clone(); + async move { + challenge_route_handler( + handler, + challenge_id, + path, + method.as_str().to_string(), + query, + headers, + body.map(|b| b.0).unwrap_or(Value::Null), + ) + .await + } + }, + ) + }) + // Challenge route without subpath: /challenge/{id} + .route("/challenge/:challenge_id", { + let handler = rpc_handler.clone(); + axum::routing::any( + move |axum::extract::Path(challenge_id): axum::extract::Path, + method: axum::http::Method, + axum::extract::Query(query): axum::extract::Query< + std::collections::HashMap, + >, + headers: axum::http::HeaderMap, + body: Option>| { + let handler = handler.clone(); + async move { + challenge_route_handler( + handler, + challenge_id, + "".to_string(), + method.as_str().to_string(), + query, + headers, + body.map(|b| b.0).unwrap_or(Value::Null), + ) + .await + } + }, + ) + }) + // Webhook endpoint for progress callbacks from challenge containers + .route("/webhook/progress", { + let handler = rpc_handler.clone(); + post(move |body: Json| { + let handler = handler.clone(); + async move { webhook_progress_handler(handler, body.0).await } + }) + }) + .with_state(self.state.clone()) + .layer(TraceLayer::new_for_http()); + + if self.config.cors_enabled { + router = router.layer( + CorsLayer::new() + .allow_origin(Any) + .allow_methods(Any) + .allow_headers(Any), + ); + } + + router + } + + /// Start the server + pub async fn run(self) -> anyhow::Result<()> { + let addr = self.config.addr; + let router = self.router(); + + info!("RPC server starting on {}", addr); + + let listener = tokio::net::TcpListener::bind(addr).await?; + axum::serve(listener, router).await?; + + Ok(()) + } + + /// Start the server in a background task + pub fn spawn(self) -> tokio::task::JoinHandle> { + tokio::spawn(async move { self.run().await }) + } + + /// Get the listen address + pub fn addr(&self) -> SocketAddr { + self.config.addr + } +} + +/// Handler for challenge custom routes +async fn challenge_route_handler( + handler: Arc, + challenge_id: String, + path: String, + method: String, + query: HashMap, + headers: axum::http::HeaderMap, + body: Value, +) -> impl IntoResponse { + let path = if path.is_empty() { + "/".to_string() + } else { + format!("/{}", path) + }; + + trace!("Challenge route: {} {} {}", challenge_id, method, path); + + // Check if challenge has registered routes + // Clone the routes while holding the lock, then drop it + let challenge_routes = { + let routes = handler.challenge_routes.read(); + let result = routes.get(&challenge_id).cloned(); + + if result.is_none() { + // Try to find by name + let chain = handler.chain_state.read(); + let actual_id = chain + .challenges + .values() + .find(|c| c.name == challenge_id) + .map(|c| c.id.to_string()); + drop(chain); + + actual_id.and_then(|id| routes.get(&id).cloned()) + } else { + result + } + }; + + let challenge_routes = match challenge_routes { + Some(r) => r, + None => { + return ( + StatusCode::NOT_FOUND, + Json(serde_json::json!({ + "error": "challenge_not_found", + "message": format!("Challenge '{}' not found or has no routes", challenge_id) + })), + ); + } + }; + + // Find matching route + let mut matched_route = None; + let mut params = HashMap::new(); + + for route in &challenge_routes { + if let Some(p) = route.matches(&method, &path) { + matched_route = Some(route.clone()); + params = p; + break; + } + } + + let route = match matched_route { + Some(r) => r, + None => { + return ( + StatusCode::NOT_FOUND, + Json(serde_json::json!({ + "error": "route_not_found", + "message": format!("No route matches {} {}", method, path), + "availableRoutes": challenge_routes.iter() + .map(|r| format!("{} {}", r.method.as_str(), r.path)) + .collect::>() + })), + ); + } + }; + + // Build request + let mut headers_map = HashMap::new(); + for (key, value) in headers.iter() { + if let Ok(v) = value.to_str() { + headers_map.insert(key.as_str().to_string(), v.to_string()); + } + } + + let request = RouteRequest { + method, + path, + params, + query, + headers: headers_map, + body, + auth_hotkey: None, + }; + + // Call the route handler if registered + let maybe_handler = handler.route_handler.read().clone(); + match maybe_handler { + Some(handle) => { + let response = handle(challenge_id.clone(), request).await; + ( + StatusCode::from_u16(response.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), + Json(response.body), + ) + } + None => { + // No handler registered - return info about the route + ( + StatusCode::OK, + Json(serde_json::json!({ + "route": { + "method": route.method.as_str(), + "path": route.path, + "description": route.description, + }, + "message": "Route handler not registered. Use RPC to interact with challenges.", + "hint": "Use JSON-RPC method 'challenge_callRoute' to invoke this route" + })), + ) + } + } +} + +/// JSON-RPC 2.0 request handler +async fn jsonrpc_handler( + Json(body): Json, + handler: Arc, +) -> (StatusCode, Json) { + // Handle batch requests + if let Some(arr) = body.as_array() { + // For batch, we'd return an array - for now just handle first + if let Some(first) = arr.first() { + return handle_single_request(first.clone(), &handler).await; + } + return ( + StatusCode::BAD_REQUEST, + Json(JsonRpcResponse::error( + Value::Null, + PARSE_ERROR, + "Empty batch", + )), + ); + } + + handle_single_request(body, &handler).await +} + +async fn handle_single_request( + body: Value, + handler: &RpcHandler, +) -> (StatusCode, Json) { + // Parse the request + let req: JsonRpcRequest = match serde_json::from_value(body) { + Ok(r) => r, + Err(e) => { + return ( + StatusCode::BAD_REQUEST, + Json(JsonRpcResponse::error( + Value::Null, + PARSE_ERROR, + format!("Parse error: {}", e), + )), + ); + } + }; + + // Validate jsonrpc version + if req.jsonrpc != "2.0" { + return ( + StatusCode::BAD_REQUEST, + Json(JsonRpcResponse::error( + req.id, + PARSE_ERROR, + "Invalid JSON-RPC version", + )), + ); + } + + // Handle the request (supports both sync and async methods like challenge_call) + let response = handler.handle_async(req).await; + + // JSON-RPC always returns 200 OK (errors are in the response body) + (StatusCode::OK, Json(response)) +} + +/// Handler for webhook progress callbacks from challenge containers +/// Broadcasts TaskProgressMessage via P2P to other validators +async fn webhook_progress_handler(handler: Arc, body: Value) -> impl IntoResponse { + use platform_core::{Keypair, NetworkMessage, SignedNetworkMessage, TaskProgressMessage}; + + // Parse the progress data + let msg_type = body.get("type").and_then(|v| v.as_str()).unwrap_or(""); + + match msg_type { + "task_progress" => { + // Extract task progress data + let progress = TaskProgressMessage { + challenge_id: body + .get("challenge_id") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + agent_hash: body + .get("agent_hash") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + evaluation_id: body + .get("evaluation_id") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + task_id: body + .get("task_id") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + task_index: body.get("task_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32, + total_tasks: body + .get("total_tasks") + .and_then(|v| v.as_u64()) + .unwrap_or(0) as u32, + passed: body + .get("passed") + .and_then(|v| v.as_bool()) + .unwrap_or(false), + score: body.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0), + execution_time_ms: body + .get("execution_time_ms") + .and_then(|v| v.as_u64()) + .unwrap_or(0), + cost_usd: body.get("cost_usd").and_then(|v| v.as_f64()).unwrap_or(0.0), + error: body.get("error").and_then(|v| v.as_str()).map(String::from), + validator_hotkey: body + .get("validator_hotkey") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + timestamp: chrono::Utc::now().timestamp() as u64, + }; + + info!( + "Webhook received task progress: [{}/{}] agent={} task={} passed={}", + progress.task_index, + progress.total_tasks, + &progress.agent_hash[..16.min(progress.agent_hash.len())], + progress.task_id, + progress.passed + ); + + // Broadcast via P2P if we have a broadcast channel and keypair + let broadcast_tx = handler.broadcast_tx.read(); + let keypair = handler.keypair.read(); + + if let (Some(tx), Some(kp)) = (broadcast_tx.as_ref(), keypair.as_ref()) { + let network_msg = NetworkMessage::TaskProgress(progress.clone()); + + match SignedNetworkMessage::new(network_msg, kp) { + Ok(signed) => { + if let Ok(bytes) = bincode::serialize(&signed) { + if tx.send(bytes).is_ok() { + debug!("TaskProgress broadcast via P2P: task={}", progress.task_id); + } + } + } + Err(e) => { + warn!("Failed to sign TaskProgress message: {}", e); + } + } + } else if broadcast_tx.is_none() { + debug!("No broadcast channel available for TaskProgress"); + } else if keypair.is_none() { + debug!("No keypair available for signing TaskProgress"); + } + + ( + StatusCode::OK, + Json(serde_json::json!({ + "success": true, + "message": "Task progress received" + })), + ) + } + "evaluation_complete" => { + info!( + "Webhook received evaluation complete: agent={} score={:.2}", + body.get("agent_hash") + .and_then(|v| v.as_str()) + .unwrap_or("unknown"), + body.get("final_score") + .and_then(|v| v.as_f64()) + .unwrap_or(0.0) + ); + + ( + StatusCode::OK, + Json(serde_json::json!({ + "success": true, + "message": "Evaluation complete received" + })), + ) + } + _ => { + warn!("Unknown webhook type: {}", msg_type); + ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!({ + "success": false, + "error": format!("Unknown webhook type: {}", msg_type) + })), + ) + } + } +} + +/// Quick helper to create and start a server +pub async fn start_rpc_server( + addr: &str, + chain_state: Arc>, + bans: Arc>, + netuid: u16, + name: &str, +) -> anyhow::Result<()> { + let config = RpcConfig { + addr: addr.parse()?, + netuid, + name: name.to_string(), + ..Default::default() + }; + + let server = RpcServer::new(config, chain_state, bans); + server.run().await +} + +#[cfg(test)] +mod tests { + use super::*; + use platform_core::{Keypair, NetworkConfig}; + use serde_json::json; + + #[test] + fn test_rpc_config_default() { + let config = RpcConfig::default(); + assert_eq!(config.netuid, 1); + assert!(config.cors_enabled); + } + + #[tokio::test] + async fn test_rpc_server_creation() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig::default(); + let server = RpcServer::new(config, state, bans); + + let router = server.router(); + // Router created successfully + } + + #[test] + fn test_rpc_server_rpc_handler() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig::default(); + let server = RpcServer::new(config, state, bans); + + let handler = server.rpc_handler(); + assert_eq!(handler.netuid, 1); + } + + #[test] + fn test_rpc_server_addr() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig { + addr: "127.0.0.1:9999".parse().unwrap(), + netuid: 42, + name: "Test".to_string(), + min_stake: 1000, + cors_enabled: false, + }; + let server = RpcServer::new(config, state, bans); + + assert_eq!(server.addr().port(), 9999); + } + + #[tokio::test] + async fn test_handle_single_request_invalid_json() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let handler = Arc::new(RpcHandler::new(state, 1)); + + let invalid_body = json!({"method": "test"}); // Missing required fields + let (status, resp) = handle_single_request(invalid_body, &handler).await; + + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(resp.0.error.is_some()); + assert_eq!(resp.0.error.unwrap().code, PARSE_ERROR); + } + + #[tokio::test] + async fn test_handle_single_request_invalid_jsonrpc_version() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let handler = Arc::new(RpcHandler::new(state, 1)); + + let body = json!({ + "jsonrpc": "1.0", // Wrong version + "method": "test", + "params": null, + "id": 1 + }); + let (status, resp) = handle_single_request(body, &handler).await; + + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(resp.0.error.is_some()); + } + + #[tokio::test] + async fn test_handle_single_request_success() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let handler = Arc::new(RpcHandler::new(state, 1)); + + let body = json!({ + "jsonrpc": "2.0", + "method": "system_version", + "params": null, + "id": 1 + }); + let (status, resp) = handle_single_request(body, &handler).await; + + assert_eq!(status, StatusCode::OK); + assert!(resp.0.result.is_some()); + } + + #[test] + fn test_rpc_config_custom() { + let config = RpcConfig { + addr: "0.0.0.0:3000".parse().unwrap(), + netuid: 99, + name: "CustomChain".to_string(), + min_stake: 5_000_000_000_000, + cors_enabled: false, + }; + + assert_eq!(config.netuid, 99); + assert_eq!(config.name, "CustomChain"); + assert!(!config.cors_enabled); + } + + #[test] + fn test_rpc_config_clone() { + let config = RpcConfig { + addr: "127.0.0.1:8080".parse().unwrap(), + netuid: 42, + name: "CloneTest".to_string(), + min_stake: 1_000_000, + cors_enabled: true, + }; + + let cloned = config.clone(); + + assert_eq!(cloned.addr, config.addr); + assert_eq!(cloned.netuid, config.netuid); + assert_eq!(cloned.name, config.name); + assert_eq!(cloned.min_stake, config.min_stake); + assert_eq!(cloned.cors_enabled, config.cors_enabled); + } + + #[test] + fn test_rpc_config_debug() { + let config = RpcConfig { + addr: "0.0.0.0:9000".parse().unwrap(), + netuid: 7, + name: "DebugTest".to_string(), + min_stake: 500_000, + cors_enabled: false, + }; + + let debug_str = format!("{:?}", config); + + assert!(debug_str.contains("RpcConfig")); + assert!(debug_str.contains("9000")); + assert!(debug_str.contains("7")); + assert!(debug_str.contains("DebugTest")); + assert!(debug_str.contains("500000")); + assert!(debug_str.contains("false")); + } + + #[tokio::test] + async fn test_rpc_server_router_has_routes() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig::default(); + let server = RpcServer::new(config, state, bans); + + // The router() method should create a router with routes defined + // We verify this by checking that the router can be created without panicking + let router = server.router(); + + // The router is created successfully, which means routes for /, /rpc, /health are configured + // We can't directly inspect routes, but creation success proves they're registered + assert!(std::mem::size_of_val(&router) > 0); + } + + #[tokio::test] + async fn test_rpc_server_cors_disabled() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig { + addr: "127.0.0.1:8081".parse().unwrap(), + netuid: 1, + name: "NoCorsTest".to_string(), + min_stake: 1_000_000_000_000, + cors_enabled: false, + }; + + // Verify config is set correctly + assert!(!config.cors_enabled); + + let server = RpcServer::new(config, state, bans); + + // Server creation should succeed with cors disabled + let router = server.router(); + assert!(std::mem::size_of_val(&router) > 0); + + // Verify the addr is set correctly + assert_eq!(server.addr().port(), 8081); + } + + #[tokio::test] + async fn test_handle_batch_request_empty() { + let kp = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + kp.hotkey(), + NetworkConfig::default(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + let config = RpcConfig::default(); + let server = RpcServer::new(config, state, bans); + let handler = server.rpc_handler(); + + // Empty batch array should return error + let empty_batch = json!([]); + let (status, response) = jsonrpc_handler(Json(empty_batch), handler).await; + + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(response.0.error.is_some()); + let error = response.0.error.unwrap(); + assert_eq!(error.code, PARSE_ERROR); + assert!(error.message.contains("Empty batch")); + } +} diff --git a/crates/rpc-server/src/types.rs b/crates/rpc-server/src/types.rs new file mode 100644 index 000000000..311275f38 --- /dev/null +++ b/crates/rpc-server/src/types.rs @@ -0,0 +1,517 @@ +//! RPC request and response types + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +/// Generic RPC response wrapper +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RpcResponse { + pub success: bool, + pub data: Option, + pub error: Option, + pub timestamp: DateTime, +} + +impl RpcResponse { + pub fn ok(data: T) -> Self { + Self { + success: true, + data: Some(data), + error: None, + timestamp: Utc::now(), + } + } + + pub fn error(msg: impl Into) -> Self { + Self { + success: false, + data: None, + error: Some(msg.into()), + timestamp: Utc::now(), + } + } +} + +/// Health check response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HealthResponse { + pub status: String, + pub version: String, + pub uptime_secs: u64, +} + +/// Subnet status response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StatusResponse { + pub netuid: u16, + pub name: String, + pub version: String, + pub block_height: u64, + pub epoch: u64, + pub validators_count: usize, + pub challenges_count: usize, + pub pending_jobs: usize, + pub is_paused: bool, +} + +/// Validator info for RPC +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ValidatorResponse { + pub hotkey: String, + pub stake: u64, + pub stake_tao: f64, + pub is_active: bool, + pub last_seen: DateTime, + pub peer_id: Option, + /// X25519 public key for API key encryption (hex, 32 bytes) + /// Derived from validator's sr25519 seed + #[serde(default, skip_serializing_if = "Option::is_none")] + pub x25519_pubkey: Option, +} + +/// Challenge info for RPC +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeResponse { + pub id: String, + pub name: String, + pub description: String, + pub code_hash: String, + pub is_active: bool, + pub emission_weight: f64, + pub timeout_secs: u64, +} + +/// Register validator request +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RegisterRequest { + pub hotkey: String, + pub signature: String, + pub message: String, + pub peer_id: Option, +} + +/// Register response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RegisterResponse { + pub accepted: bool, + pub uid: Option, + pub reason: Option, +} + +/// Heartbeat request +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HeartbeatRequest { + pub hotkey: String, + pub signature: String, + pub block_height: u64, + pub peer_id: Option, +} + +/// Heartbeat response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HeartbeatResponse { + pub accepted: bool, + pub current_block: u64, + pub current_epoch: u64, + pub next_sync_block: Option, +} + +/// Job info for RPC +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobResponse { + pub id: String, + pub challenge_id: String, + pub agent_hash: String, + pub status: String, + pub created_at: DateTime, + pub assigned_validator: Option, +} + +/// Job result submission +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobResultRequest { + pub job_id: String, + pub hotkey: String, + pub signature: String, + pub score: f64, + pub metadata: Option, +} + +/// Job result response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct JobResultResponse { + pub accepted: bool, + pub job_id: String, +} + +/// Weight assignment +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightEntry { + pub hotkey: String, + pub weight: f64, +} + +/// Current weights response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightsResponse { + pub epoch: u64, + pub challenge_id: String, + pub weights: Vec, + pub finalized: bool, +} + +/// Weight commit request +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightCommitRequest { + pub hotkey: String, + pub signature: String, + pub challenge_id: String, + pub commitment_hash: String, + pub epoch: u64, +} + +/// Weight reveal request +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WeightRevealRequest { + pub hotkey: String, + pub signature: String, + pub challenge_id: String, + pub weights: Vec, + pub salt: String, + pub epoch: u64, +} + +/// Epoch info response +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EpochResponse { + pub current_epoch: u64, + pub current_block: u64, + pub blocks_per_epoch: u64, + pub phase: String, + pub phase_progress: f64, + pub blocks_until_next_phase: u64, +} + +/// Sync data response (for new validators) +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SyncResponse { + pub block_height: u64, + pub epoch: u64, + pub state_hash: String, + pub validators: Vec, + pub challenges: Vec, +} + +/// Pagination params +#[derive(Clone, Debug, Deserialize)] +pub struct PaginationParams { + pub offset: Option, + pub limit: Option, +} + +impl Default for PaginationParams { + fn default() -> Self { + Self { + offset: Some(0), + limit: Some(100), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pagination_params_default() { + let params = PaginationParams::default(); + assert_eq!(params.offset, Some(0)); + assert_eq!(params.limit, Some(100)); + } + + #[test] + fn test_status_response_serde() { + let status = StatusResponse { + netuid: 100, + name: "Mini-Chain".to_string(), + version: "1.0".to_string(), + block_height: 100, + epoch: 5, + validators_count: 10, + challenges_count: 3, + pending_jobs: 50, + is_paused: false, + }; + let json = serde_json::to_string(&status).unwrap(); + let parsed: StatusResponse = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.block_height, 100); + assert_eq!(parsed.validators_count, 10); + } + + #[test] + fn test_challenge_response_serde() { + let challenge = ChallengeResponse { + id: "test-id".to_string(), + name: "Test Challenge".to_string(), + description: "Desc".to_string(), + code_hash: "abc123".to_string(), + is_active: true, + emission_weight: 0.5, + timeout_secs: 300, + }; + let json = serde_json::to_string(&challenge).unwrap(); + let parsed: ChallengeResponse = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.name, "Test Challenge"); + } + + #[test] + fn test_validator_response_serde() { + let validator = ValidatorResponse { + hotkey: "abc123".to_string(), + stake: 1_000_000_000_000, + stake_tao: 1000.0, + is_active: true, + last_seen: chrono::Utc::now(), + peer_id: Some("peer1".to_string()), + x25519_pubkey: None, + }; + let json = serde_json::to_string(&validator).unwrap(); + let parsed: ValidatorResponse = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.stake_tao, 1000.0); + assert!(parsed.is_active); + } + + #[test] + fn test_heartbeat_request_serde() { + let req = HeartbeatRequest { + hotkey: "hotkey1".to_string(), + signature: "sig".to_string(), + block_height: 500, + peer_id: Some("peer".to_string()), + }; + let json = serde_json::to_string(&req).unwrap(); + let parsed: HeartbeatRequest = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.block_height, 500); + } + + #[test] + fn test_heartbeat_response() { + let resp = HeartbeatResponse { + accepted: true, + current_block: 100, + current_epoch: 5, + next_sync_block: Some(110), + }; + assert!(resp.accepted); + assert_eq!(resp.current_epoch, 5); + } + + #[test] + fn test_job_response_serde() { + let job = JobResponse { + id: "job-1".to_string(), + challenge_id: "ch-1".to_string(), + agent_hash: "agent".to_string(), + status: "pending".to_string(), + created_at: chrono::Utc::now(), + assigned_validator: None, + }; + let json = serde_json::to_string(&job).unwrap(); + assert!(json.contains("pending")); + } + + #[test] + fn test_weight_entry() { + let entry = WeightEntry { + hotkey: "hk1".to_string(), + weight: 0.75, + }; + assert_eq!(entry.weight, 0.75); + } + + #[test] + fn test_weights_response() { + let resp = WeightsResponse { + epoch: 10, + challenge_id: "ch1".to_string(), + weights: vec![ + WeightEntry { + hotkey: "h1".to_string(), + weight: 0.5, + }, + WeightEntry { + hotkey: "h2".to_string(), + weight: 0.5, + }, + ], + finalized: true, + }; + assert_eq!(resp.weights.len(), 2); + assert!(resp.finalized); + } + + #[test] + fn test_epoch_response() { + let resp = EpochResponse { + current_epoch: 5, + current_block: 250, + blocks_per_epoch: 100, + phase: "evaluation".to_string(), + phase_progress: 0.5, + blocks_until_next_phase: 25, + }; + assert_eq!(resp.phase, "evaluation"); + } + + #[test] + fn test_sync_response() { + let resp = SyncResponse { + block_height: 1000, + epoch: 10, + state_hash: "hash123".to_string(), + validators: vec![], + challenges: vec![], + }; + assert_eq!(resp.block_height, 1000); + assert!(resp.validators.is_empty()); + } + + #[test] + fn test_rpc_response_ok() { + let resp: RpcResponse = RpcResponse::ok("test data".to_string()); + assert!(resp.data.is_some()); + assert!(resp.error.is_none()); + assert_eq!(resp.data.unwrap(), "test data"); + } + + #[test] + fn test_rpc_response_error() { + let resp: RpcResponse = RpcResponse::error("error message"); + assert!(resp.data.is_none()); + assert!(resp.error.is_some()); + assert_eq!(resp.error.unwrap(), "error message"); + } + + #[test] + fn test_health_response_serde() { + let health = HealthResponse { + status: "healthy".to_string(), + version: "1.0".to_string(), + uptime_secs: 100, + }; + let json = serde_json::to_string(&health).unwrap(); + let parsed: HealthResponse = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.status, "healthy"); + } + + #[test] + fn test_register_request_serde() { + let req = RegisterRequest { + hotkey: "hotkey123".to_string(), + signature: "sig".to_string(), + message: "msg".to_string(), + peer_id: Some("peer123".to_string()), + }; + let json = serde_json::to_string(&req).unwrap(); + let parsed: RegisterRequest = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.hotkey, "hotkey123"); + assert!(parsed.peer_id.is_some()); + } + + #[test] + fn test_register_response_accepted() { + let resp = RegisterResponse { + accepted: true, + uid: Some(5), + reason: None, + }; + assert!(resp.accepted); + assert_eq!(resp.uid, Some(5)); + } + + #[test] + fn test_register_response_rejected() { + let resp = RegisterResponse { + accepted: false, + uid: None, + reason: Some("Insufficient stake".to_string()), + }; + assert!(!resp.accepted); + assert!(resp.reason.is_some()); + } + + #[test] + fn test_job_result_request_serde() { + let req = JobResultRequest { + job_id: "job-1".to_string(), + hotkey: "hk".to_string(), + signature: "sig".to_string(), + score: 0.85, + metadata: Some(serde_json::json!({"key": "value"})), + }; + let json = serde_json::to_string(&req).unwrap(); + let parsed: JobResultRequest = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.score, 0.85); + assert!(parsed.metadata.is_some()); + } + + #[test] + fn test_job_result_response() { + let resp = JobResultResponse { + accepted: true, + job_id: "job-123".to_string(), + }; + assert!(resp.accepted); + assert_eq!(resp.job_id, "job-123"); + } + + #[test] + fn test_weight_commit_request_serde() { + let req = WeightCommitRequest { + hotkey: "hk".to_string(), + signature: "sig".to_string(), + challenge_id: "ch1".to_string(), + commitment_hash: "hash".to_string(), + epoch: 10, + }; + let json = serde_json::to_string(&req).unwrap(); + let parsed: WeightCommitRequest = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.epoch, 10); + assert_eq!(parsed.challenge_id, "ch1"); + } + + #[test] + fn test_weight_reveal_request_serde() { + let req = WeightRevealRequest { + hotkey: "hk".to_string(), + signature: "sig".to_string(), + challenge_id: "ch1".to_string(), + weights: vec![ + WeightEntry { + hotkey: "h1".to_string(), + weight: 0.6, + }, + WeightEntry { + hotkey: "h2".to_string(), + weight: 0.4, + }, + ], + salt: "salt123".to_string(), + epoch: 10, + }; + let json = serde_json::to_string(&req).unwrap(); + let parsed: WeightRevealRequest = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.weights.len(), 2); + assert_eq!(parsed.salt, "salt123"); + } + + #[test] + fn test_pagination_params_custom() { + let params = PaginationParams { + offset: Some(50), + limit: Some(200), + }; + assert_eq!(params.offset, Some(50)); + assert_eq!(params.limit, Some(200)); + } +} diff --git a/crates/storage/Cargo.toml b/crates/storage/Cargo.toml new file mode 100644 index 000000000..9a5af5343 --- /dev/null +++ b/crates/storage/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "platform-storage" +version.workspace = true +edition.workspace = true + +[dependencies] +platform-core = { path = "../core" } + +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +sled = { workspace = true } +uuid = { workspace = true } +chrono = { workspace = true } + +# Crypto for checksums +sha2 = { workspace = true } +hex = { workspace = true } + +# Compression (LZ4 - fast) +lz4_flex = "0.11" + +# Concurrency +parking_lot = { workspace = true } + +tracing = { workspace = true } +anyhow = { workspace = true } +thiserror = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } diff --git a/crates/storage/src/blockchain.rs b/crates/storage/src/blockchain.rs new file mode 100644 index 000000000..f5041a5ab --- /dev/null +++ b/crates/storage/src/blockchain.rs @@ -0,0 +1,1255 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Blockchain-like structure for validator consensus +//! +//! This module provides a blockchain structure for maintaining validated state +//! across the P2P validator network. It supports: +//! +//! - Block headers with merkle roots and validator signatures +//! - State transitions for tracking changes +//! - Historical state access for verification +//! - Signature verification for 2f+1 consensus +//! +//! # Example +//! +//! ```text +//! use platform_storage::blockchain::BlockchainStorage; +//! +//! let db = sled::open("./blockchain")?; +//! let mut storage = BlockchainStorage::new(&db)?; +//! +//! // Append a new block +//! storage.append_block(block)?; +//! +//! // Query historical state +//! let root = storage.get_state_root_at_block(10, None)?; +//! ``` + +use platform_core::{ChallengeId, Hotkey, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sled::{Db, Tree}; +use std::collections::HashMap; +use tracing::{debug, info, warn}; + +/// Signature from a validator for block attestation +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct ValidatorSignature { + /// Validator's hotkey who signed the block + pub validator: Hotkey, + /// The cryptographic signature over the block hash + pub signature: Vec, + /// Timestamp when the signature was created + pub timestamp: i64, +} + +impl ValidatorSignature { + /// Create a new validator signature + /// + /// # Arguments + /// + /// * `validator` - The validator's hotkey + /// * `signature` - The cryptographic signature bytes + /// * `timestamp` - Unix timestamp of signature creation + pub fn new(validator: Hotkey, signature: Vec, timestamp: i64) -> Self { + Self { + validator, + signature, + timestamp, + } + } +} + +/// Header of a block containing metadata and state roots +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct BlockHeader { + /// Sequential block number starting from 0 + pub block_number: u64, + /// Hash of the parent block (all zeros for genesis) + pub parent_hash: [u8; 32], + /// Global state root hash across all challenges + pub state_root: [u8; 32], + /// Per-challenge state root hashes for verification + pub challenge_roots: HashMap, + /// Unix timestamp when the block was created + pub timestamp: i64, + /// Hotkey of the validator who proposed this block + pub proposer: Hotkey, + /// Validator signatures attesting to this block (requires 2f+1 for validity) + pub validator_signatures: Vec, +} + +impl BlockHeader { + /// Create a new block header + /// + /// # Arguments + /// + /// * `block_number` - Sequential block number + /// * `parent_hash` - Hash of the parent block + /// * `state_root` - Global state root hash + /// * `timestamp` - Block creation timestamp + /// * `proposer` - Hotkey of the block proposer + pub fn new( + block_number: u64, + parent_hash: [u8; 32], + state_root: [u8; 32], + timestamp: i64, + proposer: Hotkey, + ) -> Self { + Self { + block_number, + parent_hash, + state_root, + challenge_roots: HashMap::new(), + timestamp, + proposer, + validator_signatures: Vec::new(), + } + } + + /// Create the genesis block header + /// + /// # Arguments + /// + /// * `proposer` - Hotkey of the genesis block proposer (typically sudo) + /// * `timestamp` - Genesis block timestamp + pub fn genesis(proposer: Hotkey, timestamp: i64) -> Self { + Self { + block_number: 0, + parent_hash: [0u8; 32], + state_root: [0u8; 32], + challenge_roots: HashMap::new(), + timestamp, + proposer, + validator_signatures: Vec::new(), + } + } + + /// Add a challenge-specific state root + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge identifier + /// * `root` - The merkle root for the challenge's state + pub fn with_challenge_root(mut self, challenge_id: ChallengeId, root: [u8; 32]) -> Self { + self.challenge_roots.insert(challenge_id, root); + self + } + + /// Add a validator signature to the header + /// + /// # Arguments + /// + /// * `signature` - The validator signature to add + pub fn add_signature(&mut self, signature: ValidatorSignature) { + self.validator_signatures.push(signature); + } + + /// Get the number of signatures on this header + pub fn signature_count(&self) -> usize { + self.validator_signatures.len() + } +} + +/// State transition types that can occur in a block +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum StateTransition { + /// A new challenge was registered on the network + ChallengeRegistered { + /// The unique challenge identifier + challenge_id: ChallengeId, + /// Hash of the challenge configuration + config_hash: [u8; 32], + }, + /// The state root for a challenge was updated + StateRootUpdate { + /// The challenge whose state was updated + challenge_id: ChallengeId, + /// Previous state root + old_root: [u8; 32], + /// New state root after the update + new_root: [u8; 32], + }, + /// A migration was applied to the system + MigrationApplied { + /// Optional challenge ID if migration was challenge-specific + challenge_id: Option, + /// Migration version number + version: u64, + }, + /// The validator set changed (validators added or removed) + ValidatorSetChange { + /// Validators that were added + added: Vec, + /// Validators that were removed + removed: Vec, + }, +} + +impl StateTransition { + /// Create a challenge registered transition + pub fn challenge_registered(challenge_id: ChallengeId, config_hash: [u8; 32]) -> Self { + Self::ChallengeRegistered { + challenge_id, + config_hash, + } + } + + /// Create a state root update transition + pub fn state_root_update( + challenge_id: ChallengeId, + old_root: [u8; 32], + new_root: [u8; 32], + ) -> Self { + Self::StateRootUpdate { + challenge_id, + old_root, + new_root, + } + } + + /// Create a migration applied transition + pub fn migration_applied(challenge_id: Option, version: u64) -> Self { + Self::MigrationApplied { + challenge_id, + version, + } + } + + /// Create a validator set change transition + pub fn validator_set_change(added: Vec, removed: Vec) -> Self { + Self::ValidatorSetChange { added, removed } + } +} + +/// A complete block containing header, transitions, and computed hash +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct Block { + /// The block header with metadata + pub header: BlockHeader, + /// State transitions included in this block + pub state_transitions: Vec, + /// Computed hash of the block (derived from header) + pub block_hash: [u8; 32], +} + +impl Block { + /// Create a new block from a header and transitions + /// + /// The block hash is computed automatically from the header. + /// + /// # Arguments + /// + /// * `header` - The block header + /// * `state_transitions` - State transitions in this block + pub fn new(header: BlockHeader, state_transitions: Vec) -> Self { + let block_hash = BlockchainStorage::compute_block_hash(&header); + Self { + header, + state_transitions, + block_hash, + } + } + + /// Create the genesis block + /// + /// # Arguments + /// + /// * `proposer` - Hotkey of the genesis proposer + /// * `timestamp` - Genesis timestamp + pub fn genesis(proposer: Hotkey, timestamp: i64) -> Self { + let header = BlockHeader::genesis(proposer, timestamp); + Self::new(header, Vec::new()) + } + + /// Get the block number + pub fn block_number(&self) -> u64 { + self.header.block_number + } + + /// Get the parent hash + pub fn parent_hash(&self) -> &[u8; 32] { + &self.header.parent_hash + } + + /// Get the state root + pub fn state_root(&self) -> &[u8; 32] { + &self.header.state_root + } + + /// Check if this is the genesis block + pub fn is_genesis(&self) -> bool { + self.header.block_number == 0 + } + + /// Verify that the block hash is correctly computed + pub fn verify_hash(&self) -> bool { + let computed = BlockchainStorage::compute_block_hash(&self.header); + computed == self.block_hash + } +} + +/// Storage tree names for blockchain data +const TREE_BLOCKS: &str = "blockchain_blocks"; +const TREE_BLOCK_BY_HASH: &str = "blockchain_by_hash"; +const TREE_METADATA: &str = "blockchain_metadata"; + +/// Key for storing the latest block number +const KEY_LATEST_BLOCK: &str = "latest_block_number"; + +/// Blockchain storage for persisting and querying blocks +pub struct BlockchainStorage { + /// Tree storing blocks by block number + blocks_tree: Tree, + /// Tree for looking up blocks by hash + hash_index_tree: Tree, + /// Tree for metadata (latest block number, etc.) + metadata_tree: Tree, +} + +impl BlockchainStorage { + /// Create a new blockchain storage instance + /// + /// # Arguments + /// + /// * `db` - Reference to the sled database + /// + /// # Errors + /// + /// Returns an error if the database trees cannot be opened. + pub fn new(db: &Db) -> Result { + let blocks_tree = db + .open_tree(TREE_BLOCKS) + .map_err(|e| MiniChainError::Storage(format!("Failed to open blocks tree: {}", e)))?; + + let hash_index_tree = db.open_tree(TREE_BLOCK_BY_HASH).map_err(|e| { + MiniChainError::Storage(format!("Failed to open hash index tree: {}", e)) + })?; + + let metadata_tree = db + .open_tree(TREE_METADATA) + .map_err(|e| MiniChainError::Storage(format!("Failed to open metadata tree: {}", e)))?; + + debug!("BlockchainStorage initialized"); + Ok(Self { + blocks_tree, + hash_index_tree, + metadata_tree, + }) + } + + /// Compute the hash of a block header + /// + /// Uses SHA-256 over the bincode-serialized header. + /// + /// # Arguments + /// + /// * `header` - The block header to hash + pub fn compute_block_hash(header: &BlockHeader) -> [u8; 32] { + let mut hasher = Sha256::new(); + + // Hash the core header fields deterministically + hasher.update(header.block_number.to_le_bytes()); + hasher.update(header.parent_hash); + hasher.update(header.state_root); + hasher.update(header.timestamp.to_le_bytes()); + hasher.update(header.proposer.0); + + // Hash challenge roots in deterministic order + let mut sorted_challenges: Vec<_> = header.challenge_roots.iter().collect(); + sorted_challenges.sort_by(|a, b| a.0 .0.cmp(&b.0 .0)); + for (challenge_id, root) in sorted_challenges { + hasher.update(challenge_id.0.as_bytes()); + hasher.update(root); + } + + hasher.finalize().into() + } + + /// Get the latest block in the chain + /// + /// # Returns + /// + /// The latest block if the chain is non-empty, None otherwise. + pub fn get_latest_block(&self) -> Result> { + let latest_number = match self.get_latest_block_number()? { + Some(n) => n, + None => return Ok(None), + }; + self.get_block_by_number(latest_number) + } + + /// Get a block by its block number + /// + /// # Arguments + /// + /// * `number` - The block number to retrieve + /// + /// # Returns + /// + /// The block if found, None otherwise. + pub fn get_block_by_number(&self, number: u64) -> Result> { + let key = number.to_be_bytes(); + + let data = self.blocks_tree.get(key).map_err(|e| { + MiniChainError::Storage(format!("Failed to read block {}: {}", number, e)) + })?; + + match data { + Some(bytes) => { + let block: Block = bincode::deserialize(&bytes) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(Some(block)) + } + None => Ok(None), + } + } + + /// Get a block by its hash + /// + /// # Arguments + /// + /// * `hash` - The 32-byte block hash + /// + /// # Returns + /// + /// The block if found, None otherwise. + pub fn get_block_by_hash(&self, hash: &[u8; 32]) -> Result> { + // Look up block number from hash index + let block_number_bytes = self + .hash_index_tree + .get(hash) + .map_err(|e| MiniChainError::Storage(format!("Failed to read hash index: {}", e)))?; + + match block_number_bytes { + Some(bytes) => { + if bytes.len() != 8 { + return Err(MiniChainError::Storage( + "Invalid block number in hash index".to_string(), + )); + } + let mut arr = [0u8; 8]; + arr.copy_from_slice(&bytes); + let number = u64::from_be_bytes(arr); + self.get_block_by_number(number) + } + None => Ok(None), + } + } + + /// Append a new block to the chain + /// + /// Validates that the block's parent hash matches the current chain tip + /// before appending. + /// + /// # Arguments + /// + /// * `block` - The block to append + /// + /// # Errors + /// + /// Returns an error if: + /// - The parent hash doesn't match the previous block's hash + /// - The block number is not sequential + /// - The block hash verification fails + pub fn append_block(&mut self, block: Block) -> Result<()> { + // Verify the block hash is correctly computed + if !block.verify_hash() { + return Err(MiniChainError::Validation( + "Block hash verification failed".to_string(), + )); + } + + let latest_number = self.get_latest_block_number()?; + + // Validate block number + let expected_number = latest_number.map(|n| n + 1).unwrap_or(0); + if block.header.block_number != expected_number { + return Err(MiniChainError::Validation(format!( + "Invalid block number: expected {}, got {}", + expected_number, block.header.block_number + ))); + } + + // Validate parent hash for non-genesis blocks + if let Some(prev_number) = latest_number { + let prev_block = self + .get_block_by_number(prev_number)? + .ok_or_else(|| MiniChainError::NotFound("Previous block not found".to_string()))?; + + if block.header.parent_hash != prev_block.block_hash { + return Err(MiniChainError::Validation(format!( + "Parent hash mismatch: expected {:?}, got {:?}", + hex::encode(prev_block.block_hash), + hex::encode(block.header.parent_hash) + ))); + } + } else { + // Genesis block should have zero parent hash + if block.header.parent_hash != [0u8; 32] { + return Err(MiniChainError::Validation( + "Genesis block must have zero parent hash".to_string(), + )); + } + } + + // Serialize and store the block + let block_bytes = + bincode::serialize(&block).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + let block_number_key = block.header.block_number.to_be_bytes(); + + self.blocks_tree + .insert(block_number_key, block_bytes) + .map_err(|e| MiniChainError::Storage(format!("Failed to store block: {}", e)))?; + + // Update hash index + self.hash_index_tree + .insert(block.block_hash, &block_number_key) + .map_err(|e| MiniChainError::Storage(format!("Failed to update hash index: {}", e)))?; + + // Update latest block number + self.metadata_tree + .insert(KEY_LATEST_BLOCK, &block_number_key) + .map_err(|e| { + MiniChainError::Storage(format!("Failed to update latest block number: {}", e)) + })?; + + info!( + block_number = block.header.block_number, + hash = hex::encode(block.block_hash), + transitions = block.state_transitions.len(), + "Appended block to chain" + ); + + Ok(()) + } + + /// Verify that a block has sufficient validator signatures (2f+1) + /// + /// This checks that the block has at least 2f+1 signatures from valid validators + /// where f is the maximum number of faulty validators tolerated. + /// + /// # Arguments + /// + /// * `block` - The block to verify + /// + /// # Returns + /// + /// True if the block has sufficient signatures, false otherwise. + /// + /// # Note + /// + /// This implementation checks signature count against a threshold. + /// In production, you would also verify each signature cryptographically + /// against the validator's public key. + pub fn verify_block(&self, block: &Block) -> Result { + // First verify the hash is correct + if !block.verify_hash() { + warn!( + block_number = block.header.block_number, + "Block hash verification failed" + ); + return Ok(false); + } + + // Genesis block doesn't require signatures + if block.is_genesis() { + return Ok(true); + } + + let signature_count = block.header.validator_signatures.len(); + + // Check for duplicate validators in signatures + let mut seen_validators = std::collections::HashSet::new(); + for sig in &block.header.validator_signatures { + if !seen_validators.insert(&sig.validator) { + warn!( + block_number = block.header.block_number, + validator = %sig.validator.to_hex(), + "Duplicate validator signature detected" + ); + return Ok(false); + } + } + + // For Byzantine fault tolerance with n validators, we need at least 2f+1 signatures + // where f = floor((n-1)/3) is the max faulty validators + // This means we need at least ceiling(2n/3) signatures + // + // For a practical minimum, we require at least 1 signature (the proposer) + // In production, this threshold should be calculated from the active validator set + if signature_count == 0 { + warn!( + block_number = block.header.block_number, + "Block has no validator signatures" + ); + return Ok(false); + } + + debug!( + block_number = block.header.block_number, + signature_count, "Block signature verification passed" + ); + + Ok(true) + } + + /// Check if a block has quorum (2f+1) given the total validator count + /// + /// # Arguments + /// + /// * `block` - The block to check + /// * `total_validators` - Total number of validators in the network + /// + /// # Returns + /// + /// True if the block has 2f+1 signatures for the given validator count. + pub fn has_quorum(&self, block: &Block, total_validators: usize) -> bool { + if total_validators == 0 { + return false; + } + + // Calculate required signatures for 2f+1 (Byzantine majority) + // n = total_validators, f = floor((n-1)/3) + // Required = n - f = n - floor((n-1)/3) + // Simplified: ceiling(2n/3) + 1 for n > 1, or n for n <= 1 + let required_signatures = if total_validators <= 1 { + total_validators + } else { + // ceiling((2 * n + 2) / 3) + (2 * total_validators).div_ceil(3) + }; + + let signature_count = block.header.validator_signatures.len(); + signature_count >= required_signatures + } + + /// Get the state root at a specific block number + /// + /// # Arguments + /// + /// * `block_number` - The block number to query + /// * `challenge_id` - Optional challenge ID for challenge-specific root + /// + /// # Returns + /// + /// The state root if found, None otherwise. + pub fn get_state_root_at_block( + &self, + block_number: u64, + challenge_id: Option<&ChallengeId>, + ) -> Result> { + let block = match self.get_block_by_number(block_number)? { + Some(b) => b, + None => return Ok(None), + }; + + match challenge_id { + Some(id) => Ok(block.header.challenge_roots.get(id).copied()), + None => Ok(Some(block.header.state_root)), + } + } + + /// Get the state root for a specific challenge at a block number + /// + /// # Arguments + /// + /// * `block_number` - The block number to query + /// * `challenge_id` - The challenge identifier + /// + /// # Returns + /// + /// The challenge's state root if found, None otherwise. + pub fn get_challenge_root_at_block( + &self, + block_number: u64, + challenge_id: &ChallengeId, + ) -> Result> { + self.get_state_root_at_block(block_number, Some(challenge_id)) + } + + /// List all blocks in a given range (inclusive) + /// + /// # Arguments + /// + /// * `start` - Starting block number (inclusive) + /// * `end` - Ending block number (inclusive) + /// + /// # Returns + /// + /// A vector of blocks in the range, ordered by block number. + pub fn list_blocks_in_range(&self, start: u64, end: u64) -> Result> { + if start > end { + return Ok(Vec::new()); + } + + let mut blocks = Vec::new(); + for number in start..=end { + if let Some(block) = self.get_block_by_number(number)? { + blocks.push(block); + } + } + Ok(blocks) + } + + /// Get the current chain height (latest block number) + /// + /// # Returns + /// + /// The latest block number if the chain is non-empty, None otherwise. + pub fn get_latest_block_number(&self) -> Result> { + let data = self + .metadata_tree + .get(KEY_LATEST_BLOCK) + .map_err(|e| MiniChainError::Storage(format!("Failed to read latest block: {}", e)))?; + + match data { + Some(bytes) => { + if bytes.len() != 8 { + return Err(MiniChainError::Storage( + "Invalid latest block number".to_string(), + )); + } + let mut arr = [0u8; 8]; + arr.copy_from_slice(&bytes); + Ok(Some(u64::from_be_bytes(arr))) + } + None => Ok(None), + } + } + + /// Get the total number of blocks in the chain + pub fn chain_length(&self) -> Result { + Ok(self.get_latest_block_number()?.map(|n| n + 1).unwrap_or(0)) + } + + /// Check if the chain is empty + pub fn is_empty(&self) -> Result { + Ok(self.get_latest_block_number()?.is_none()) + } + + /// Flush all pending writes to disk + pub fn flush(&self) -> Result<()> { + self.blocks_tree + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush blocks: {}", e)))?; + self.hash_index_tree + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush hash index: {}", e)))?; + self.metadata_tree + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush metadata: {}", e)))?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + fn create_test_db() -> sled::Db { + let dir = tempdir().expect("Failed to create temp dir"); + sled::open(dir.path()).expect("Failed to open test database") + } + + fn create_test_hotkey(seed: u8) -> Hotkey { + Hotkey([seed; 32]) + } + + fn create_test_signature(validator: Hotkey, timestamp: i64) -> ValidatorSignature { + ValidatorSignature::new(validator, vec![0u8; 64], timestamp) + } + + #[test] + fn test_blockchain_storage_new() { + let db = create_test_db(); + let storage = BlockchainStorage::new(&db); + assert!(storage.is_ok()); + } + + #[test] + fn test_genesis_block() { + let proposer = create_test_hotkey(1); + let timestamp = 1000; + + let genesis = Block::genesis(proposer.clone(), timestamp); + + assert_eq!(genesis.header.block_number, 0); + assert_eq!(genesis.header.parent_hash, [0u8; 32]); + assert_eq!(genesis.header.proposer, proposer); + assert!(genesis.is_genesis()); + assert!(genesis.state_transitions.is_empty()); + } + + #[test] + fn test_append_genesis_block() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + let result = storage.append_block(genesis.clone()); + assert!(result.is_ok()); + + let latest = storage.get_latest_block().expect("Failed to get latest"); + assert!(latest.is_some()); + assert_eq!(latest.unwrap().header.block_number, 0); + } + + #[test] + fn test_append_multiple_blocks() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + + // Append genesis + let genesis = Block::genesis(proposer.clone(), 1000); + storage + .append_block(genesis.clone()) + .expect("Failed to append genesis"); + + // Create and append block 1 + let mut header1 = + BlockHeader::new(1, genesis.block_hash, [1u8; 32], 2000, proposer.clone()); + header1.add_signature(create_test_signature(proposer.clone(), 2000)); + let block1 = Block::new(header1, vec![]); + storage + .append_block(block1.clone()) + .expect("Failed to append block 1"); + + // Create and append block 2 + let mut header2 = BlockHeader::new(2, block1.block_hash, [2u8; 32], 3000, proposer.clone()); + header2.add_signature(create_test_signature(proposer.clone(), 3000)); + let block2 = Block::new(header2, vec![]); + storage + .append_block(block2) + .expect("Failed to append block 2"); + + assert_eq!(storage.chain_length().expect("chain_length failed"), 3); + } + + #[test] + fn test_get_block_by_number() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + storage.append_block(genesis).expect("Failed to append"); + + let block = storage.get_block_by_number(0).expect("Failed to get block"); + assert!(block.is_some()); + assert_eq!(block.unwrap().header.block_number, 0); + + let none_block = storage + .get_block_by_number(999) + .expect("Failed to get nonexistent block"); + assert!(none_block.is_none()); + } + + #[test] + fn test_get_block_by_hash() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + let hash = genesis.block_hash; + storage + .append_block(genesis) + .expect("Failed to append genesis"); + + let block = storage + .get_block_by_hash(&hash) + .expect("Failed to get block"); + assert!(block.is_some()); + assert_eq!(block.unwrap().block_hash, hash); + + let none_block = storage + .get_block_by_hash(&[99u8; 32]) + .expect("Failed to get nonexistent block"); + assert!(none_block.is_none()); + } + + #[test] + fn test_invalid_parent_hash() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + storage.append_block(genesis).expect("Failed to append"); + + // Try to append a block with wrong parent hash + let mut bad_header = + BlockHeader::new(1, [99u8; 32], [1u8; 32], 2000, create_test_hotkey(1)); + bad_header.add_signature(create_test_signature(create_test_hotkey(1), 2000)); + let bad_block = Block::new(bad_header, vec![]); + + let result = storage.append_block(bad_block); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Parent hash mismatch")); + } + + #[test] + fn test_invalid_block_number() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + storage.append_block(genesis).expect("Failed to append"); + + // Try to append a block with wrong block number + let bad_header = BlockHeader::new(99, [0u8; 32], [1u8; 32], 2000, create_test_hotkey(1)); + let bad_block = Block::new(bad_header, vec![]); + + let result = storage.append_block(bad_block); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Invalid block number")); + } + + #[test] + fn test_state_transitions() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + let genesis = Block::genesis(proposer.clone(), 1000); + storage.append_block(genesis.clone()).expect("Failed"); + + let challenge_id = ChallengeId::new(); + let transitions = vec![ + StateTransition::challenge_registered(challenge_id, [42u8; 32]), + StateTransition::state_root_update(challenge_id, [0u8; 32], [1u8; 32]), + ]; + + let mut header1 = + BlockHeader::new(1, genesis.block_hash, [1u8; 32], 2000, proposer.clone()); + header1.add_signature(create_test_signature(proposer, 2000)); + let block1 = Block::new(header1, transitions); + + storage.append_block(block1).expect("Failed to append"); + + let loaded = storage + .get_block_by_number(1) + .expect("Failed to get") + .expect("Block not found"); + assert_eq!(loaded.state_transitions.len(), 2); + } + + #[test] + fn test_challenge_roots() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + + let mut header = BlockHeader::genesis(proposer.clone(), 1000) + .with_challenge_root(challenge1, [11u8; 32]) + .with_challenge_root(challenge2, [22u8; 32]); + header.state_root = [99u8; 32]; + + let block = Block::new(header, vec![]); + storage.append_block(block).expect("Failed to append"); + + // Check global state root + let global_root = storage + .get_state_root_at_block(0, None) + .expect("Failed to get") + .expect("Root not found"); + assert_eq!(global_root, [99u8; 32]); + + // Check challenge-specific roots + let root1 = storage + .get_challenge_root_at_block(0, &challenge1) + .expect("Failed") + .expect("Root not found"); + assert_eq!(root1, [11u8; 32]); + + let root2 = storage + .get_challenge_root_at_block(0, &challenge2) + .expect("Failed") + .expect("Root not found"); + assert_eq!(root2, [22u8; 32]); + + // Non-existent challenge + let fake_challenge = ChallengeId::new(); + let no_root = storage + .get_challenge_root_at_block(0, &fake_challenge) + .expect("Failed"); + assert!(no_root.is_none()); + } + + #[test] + fn test_list_blocks_in_range() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + let genesis = Block::genesis(proposer.clone(), 1000); + storage.append_block(genesis.clone()).expect("Failed"); + + // Create 4 more blocks (total 5) + let mut prev_hash = genesis.block_hash; + for i in 1..5 { + let mut header = BlockHeader::new( + i, + prev_hash, + [i as u8; 32], + 1000 + (i * 1000) as i64, + proposer.clone(), + ); + header.add_signature(create_test_signature( + proposer.clone(), + 1000 + (i * 1000) as i64, + )); + let block = Block::new(header, vec![]); + prev_hash = block.block_hash; + storage.append_block(block).expect("Failed"); + } + + // Get range 1..3 + let blocks = storage.list_blocks_in_range(1, 3).expect("Failed to list"); + assert_eq!(blocks.len(), 3); + assert_eq!(blocks[0].header.block_number, 1); + assert_eq!(blocks[1].header.block_number, 2); + assert_eq!(blocks[2].header.block_number, 3); + + // Empty range + let empty = storage + .list_blocks_in_range(100, 200) + .expect("Failed to list"); + assert!(empty.is_empty()); + + // Reversed range + let reversed = storage.list_blocks_in_range(5, 1).expect("Failed to list"); + assert!(reversed.is_empty()); + } + + #[test] + fn test_verify_block_hash() { + let proposer = create_test_hotkey(1); + let block = Block::genesis(proposer, 1000); + + assert!(block.verify_hash()); + + // Tampered block + let mut tampered = block.clone(); + tampered.header.timestamp = 9999; + assert!(!tampered.verify_hash()); + } + + #[test] + fn test_verify_block_signatures() { + let db = create_test_db(); + let storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + let validator1 = create_test_hotkey(2); + let validator2 = create_test_hotkey(3); + + // Genesis doesn't need signatures + let genesis = Block::genesis(proposer.clone(), 1000); + assert!(storage.verify_block(&genesis).expect("Failed to verify")); + + // Non-genesis needs at least one signature + let mut header = BlockHeader::new(1, genesis.block_hash, [1u8; 32], 2000, proposer.clone()); + let no_sig_block = Block::new(header.clone(), vec![]); + assert!(!storage.verify_block(&no_sig_block).expect("Failed")); + + // With signatures + header.add_signature(create_test_signature(validator1.clone(), 2000)); + let signed_block = Block::new(header.clone(), vec![]); + assert!(storage.verify_block(&signed_block).expect("Failed")); + + // Duplicate validator signatures should fail + let mut dup_header = header.clone(); + dup_header.add_signature(create_test_signature(validator1.clone(), 2001)); // Same validator! + let dup_block = Block::new(dup_header, vec![]); + assert!(!storage.verify_block(&dup_block).expect("Failed")); + } + + #[test] + fn test_has_quorum() { + let db = create_test_db(); + let storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let proposer = create_test_hotkey(1); + + // Create a block with 2 signatures + let mut header = BlockHeader::new(1, [0u8; 32], [1u8; 32], 1000, proposer.clone()); + header.add_signature(create_test_signature(create_test_hotkey(1), 1000)); + header.add_signature(create_test_signature(create_test_hotkey(2), 1000)); + let block = Block::new(header, vec![]); + + // With 3 validators, need 2f+1 = 2 signatures (f=0) + assert!(storage.has_quorum(&block, 3)); + + // With 4 validators, need 2f+1 = 3 signatures (f=1) - but we only have 2 + assert!(!storage.has_quorum(&block, 4)); + + // Edge cases + assert!(!storage.has_quorum(&block, 0)); + } + + #[test] + fn test_empty_chain() { + let db = create_test_db(); + let storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + assert!(storage.is_empty().expect("is_empty failed")); + assert_eq!(storage.chain_length().expect("chain_length failed"), 0); + assert!(storage + .get_latest_block() + .expect("get_latest failed") + .is_none()); + } + + #[test] + fn test_block_hash_determinism() { + let proposer = create_test_hotkey(1); + let challenge1 = ChallengeId::new(); + let challenge2 = ChallengeId::new(); + + let header1 = BlockHeader::new(1, [0u8; 32], [1u8; 32], 1000, proposer.clone()) + .with_challenge_root(challenge1, [11u8; 32]) + .with_challenge_root(challenge2, [22u8; 32]); + + let header2 = BlockHeader::new(1, [0u8; 32], [1u8; 32], 1000, proposer.clone()) + .with_challenge_root(challenge2, [22u8; 32]) + .with_challenge_root(challenge1, [11u8; 32]); + + // Same data, different insertion order - should produce same hash + let hash1 = BlockchainStorage::compute_block_hash(&header1); + let hash2 = BlockchainStorage::compute_block_hash(&header2); + assert_eq!(hash1, hash2); + } + + #[test] + fn test_state_transition_constructors() { + let challenge_id = ChallengeId::new(); + + let reg = StateTransition::challenge_registered(challenge_id, [1u8; 32]); + assert!(matches!(reg, StateTransition::ChallengeRegistered { .. })); + + let update = StateTransition::state_root_update(challenge_id, [0u8; 32], [1u8; 32]); + assert!(matches!(update, StateTransition::StateRootUpdate { .. })); + + let migration = StateTransition::migration_applied(Some(challenge_id), 1); + assert!(matches!( + migration, + StateTransition::MigrationApplied { .. } + )); + + let global_migration = StateTransition::migration_applied(None, 2); + if let StateTransition::MigrationApplied { + challenge_id, + version, + } = global_migration + { + assert!(challenge_id.is_none()); + assert_eq!(version, 2); + } else { + panic!("Wrong variant"); + } + + let hotkey1 = create_test_hotkey(1); + let hotkey2 = create_test_hotkey(2); + let change = StateTransition::validator_set_change(vec![hotkey1.clone()], vec![hotkey2]); + if let StateTransition::ValidatorSetChange { added, removed } = change { + assert_eq!(added.len(), 1); + assert_eq!(removed.len(), 1); + } else { + panic!("Wrong variant"); + } + } + + #[test] + fn test_validator_signature_new() { + let validator = create_test_hotkey(42); + let signature = vec![1, 2, 3, 4, 5]; + let timestamp = 123456789; + + let sig = ValidatorSignature::new(validator.clone(), signature.clone(), timestamp); + + assert_eq!(sig.validator, validator); + assert_eq!(sig.signature, signature); + assert_eq!(sig.timestamp, timestamp); + } + + #[test] + fn test_block_header_signature_count() { + let proposer = create_test_hotkey(1); + let mut header = BlockHeader::new(0, [0u8; 32], [0u8; 32], 1000, proposer.clone()); + + assert_eq!(header.signature_count(), 0); + + header.add_signature(create_test_signature(create_test_hotkey(1), 1000)); + assert_eq!(header.signature_count(), 1); + + header.add_signature(create_test_signature(create_test_hotkey(2), 1000)); + assert_eq!(header.signature_count(), 2); + } + + #[test] + fn test_flush() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let genesis = Block::genesis(create_test_hotkey(1), 1000); + storage.append_block(genesis).expect("Failed to append"); + + let result = storage.flush(); + assert!(result.is_ok()); + } + + #[test] + fn test_genesis_non_zero_parent_hash() { + let db = create_test_db(); + let mut storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + // Genesis with non-zero parent hash should fail + let bad_genesis = Block::new( + BlockHeader::new(0, [1u8; 32], [0u8; 32], 1000, create_test_hotkey(1)), + vec![], + ); + + let result = storage.append_block(bad_genesis); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Genesis block must have zero parent hash")); + } + + #[test] + fn test_block_accessors() { + let proposer = create_test_hotkey(1); + let parent = [5u8; 32]; + let state_root = [10u8; 32]; + + let mut header = BlockHeader::new(42, parent, state_root, 1000, proposer); + header.add_signature(create_test_signature(create_test_hotkey(1), 1000)); + let block = Block::new(header, vec![]); + + assert_eq!(block.block_number(), 42); + assert_eq!(*block.parent_hash(), parent); + assert_eq!(*block.state_root(), state_root); + assert!(!block.is_genesis()); + } + + #[test] + fn test_get_state_root_nonexistent_block() { + let db = create_test_db(); + let storage = BlockchainStorage::new(&db).expect("Failed to create storage"); + + let result = storage.get_state_root_at_block(999, None).expect("Failed"); + assert!(result.is_none()); + } +} diff --git a/crates/storage/src/distributed.rs b/crates/storage/src/distributed.rs new file mode 100644 index 000000000..4a2fdb633 --- /dev/null +++ b/crates/storage/src/distributed.rs @@ -0,0 +1,2031 @@ +//! Distributed Blockchain Storage +//! +//! High-performance, compressed, replicated storage for blockchain data. +//! All data is stored with 50% consensus requirement and synced across validators. +//! +//! # Features +//! +//! - **LZ4 Compression**: Fast compression/decompression for all data +//! - **Sled Backend**: High-performance embedded database +//! - **50% Consensus**: Write operations require 50% validator agreement +//! - **Merkle Proofs**: Efficient verification of data integrity +//! - **Efficient Queries**: Index-based queries for agents, evaluations, submissions +//! - **P2P Replication**: Automatic sync across all validators +//! +//! # Data Categories +//! +//! - `submissions`: Agent submissions (encrypted/revealed) +//! - `agents`: Finalized agent records +//! - `evaluations`: Per-validator evaluation results +//! - `consensus`: Consensus results after 50% agreement +//! - `logs`: Compressed execution logs (TTL-limited) + +use lz4_flex::{compress_prepend_size, decompress_size_prepended}; +use parking_lot::RwLock; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sled::{Db, Tree}; +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::marker::PhantomData; +use std::sync::Arc; +use std::time::Instant; + +// ============================================================================ +// CONSTANTS +// ============================================================================ + +/// Consensus threshold (50%) +pub const CONSENSUS_THRESHOLD: f64 = 0.50; + +/// Maximum entry size before compression (10 MB) +pub const MAX_RAW_SIZE: usize = 10 * 1024 * 1024; + +/// Maximum compressed entry size (5 MB) +pub const MAX_COMPRESSED_SIZE: usize = 5 * 1024 * 1024; + +/// Maximum entries per category per challenge +pub const MAX_ENTRIES_PER_CATEGORY: usize = 100_000; + +/// No TTL - all data is permanent +pub const NO_TTL: u64 = 0; + +// ============================================================================ +// VALIDATION TYPES (for challenge rules) +// ============================================================================ + +/// Information about a write request for validation +#[derive(Debug, Clone)] +pub struct WriteRequestInfo { + /// Category of data + pub category: Category, + /// Challenge ID + pub challenge_id: String, + /// Key within category + pub key: String, + /// Serialized value + pub value: Vec, + /// Size in bytes + pub size: usize, + /// Creator validator hotkey + pub creator: String, + /// Creator's stake (in RAO) + pub creator_stake: u64, + /// Current block height + pub block: u64, + /// Is this an update to existing key? + pub is_update: bool, + /// Previous value hash (if update) + pub previous_hash: Option<[u8; 32]>, + /// Writes by this validator this epoch + pub writes_this_epoch: usize, + /// Total entries in this category + pub category_entry_count: usize, + /// Total validators in network + pub total_validators: usize, +} + +impl WriteRequestInfo { + /// Deserialize the value + pub fn deserialize_value(&self) -> Result { + serde_json::from_slice(&self.value) + } +} + +/// Result of write validation +#[derive(Debug, Clone)] +pub enum WriteValidationResult { + /// Accept the write + Accept, + /// Reject with reason + Reject(String), +} + +impl WriteValidationResult { + pub fn accept() -> Self { + WriteValidationResult::Accept + } + + pub fn reject(reason: impl Into) -> Self { + WriteValidationResult::Reject(reason.into()) + } + + pub fn is_accepted(&self) -> bool { + matches!(self, WriteValidationResult::Accept) + } +} + +// ============================================================================ +// STORAGE CATEGORIES +// ============================================================================ + +/// Data category for organization +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[repr(u8)] +pub enum Category { + /// Agent submissions (pending/revealed) + Submission = 0, + /// Finalized agents (after consensus) + Agent = 1, + /// Evaluation results per validator + Evaluation = 2, + /// Consensus results + Consensus = 3, + /// Execution logs (compressed, TTL) + Log = 4, + /// Indexes for fast lookup + Index = 5, + /// Metadata + Meta = 6, +} + +impl Category { + pub fn prefix(&self) -> &'static [u8] { + match self { + Category::Submission => b"sub:", + Category::Agent => b"agt:", + Category::Evaluation => b"evl:", + Category::Consensus => b"cns:", + Category::Log => b"log:", + Category::Index => b"idx:", + Category::Meta => b"met:", + } + } +} + +// ============================================================================ +// ENTRY METADATA +// ============================================================================ + +/// Entry header stored with each value +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EntryHeader { + /// Category + pub category: Category, + /// Challenge ID + pub challenge_id: String, + /// Entry key + pub key: String, + /// Version (monotonic) + pub version: u64, + /// Block when created + pub created_block: u64, + /// Block when updated + pub updated_block: u64, + /// Block when expires (0 = never) + pub expires_block: u64, + /// Creator validator + pub creator: String, + /// Hash of uncompressed value + pub value_hash: [u8; 32], + /// Uncompressed size + pub raw_size: u32, + /// Compressed size + pub compressed_size: u32, + /// Validators who have ACKed this entry + pub acks: HashSet, + /// Whether consensus is reached + pub consensus_reached: bool, +} + +/// A stored entry with header and compressed value +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StoredEntry { + pub header: EntryHeader, + /// LZ4 compressed value + pub compressed_value: Vec, +} + +impl StoredEntry { + /// Create new entry with compression + pub fn new( + category: Category, + challenge_id: &str, + key: &str, + value: &T, + creator: &str, + block: u64, + ttl_blocks: Option, + ) -> Result { + let raw_bytes = + bincode::serialize(value).map_err(|e| StorageError::Serialization(e.to_string()))?; + + if raw_bytes.len() > MAX_RAW_SIZE { + return Err(StorageError::TooLarge(raw_bytes.len(), MAX_RAW_SIZE)); + } + + let compressed = compress_prepend_size(&raw_bytes); + + if compressed.len() > MAX_COMPRESSED_SIZE { + return Err(StorageError::TooLarge( + compressed.len(), + MAX_COMPRESSED_SIZE, + )); + } + + let value_hash = { + let mut hasher = Sha256::new(); + hasher.update(&raw_bytes); + hasher.finalize().into() + }; + + let expires_block = ttl_blocks.map(|t| block + t).unwrap_or(0); + + Ok(Self { + header: EntryHeader { + category, + challenge_id: challenge_id.to_string(), + key: key.to_string(), + version: 1, + created_block: block, + updated_block: block, + expires_block, + creator: creator.to_string(), + value_hash, + raw_size: raw_bytes.len() as u32, + compressed_size: compressed.len() as u32, + acks: HashSet::new(), + consensus_reached: false, + }, + compressed_value: compressed, + }) + } + + /// Decompress and deserialize value with size limit + pub fn decompress(&self) -> Result { + use bincode::Options; + + let raw = decompress_size_prepended(&self.compressed_value) + .map_err(|e| StorageError::Decompression(e.to_string()))?; + + // Verify decompressed size matches header to prevent decompression bombs + if raw.len() != self.header.raw_size as usize { + return Err(StorageError::Decompression(format!( + "Decompressed size mismatch: expected {}, got {}", + self.header.raw_size, + raw.len() + ))); + } + + // Enforce MAX_RAW_SIZE limit (already defined as 10 MB) + // Use options compatible with bincode::serialize (little-endian, variable int, trailing allowed) + bincode::DefaultOptions::new() + .with_fixint_encoding() + .with_little_endian() + .allow_trailing_bytes() + .with_limit(MAX_RAW_SIZE as u64) + .deserialize(&raw) + .map_err(|e| StorageError::Serialization(e.to_string())) + } + + /// Decompress and return raw bytes (for validation) + pub fn decompress_raw(&self) -> Result, StorageError> { + decompress_size_prepended(&self.compressed_value) + .map_err(|e| StorageError::Decompression(e.to_string())) + } + + /// Verify integrity + pub fn verify(&self) -> bool { + if let Ok(raw) = decompress_size_prepended(&self.compressed_value) { + let mut hasher = Sha256::new(); + hasher.update(&raw); + let hash: [u8; 32] = hasher.finalize().into(); + hash == self.header.value_hash + } else { + false + } + } + + /// Check if expired + pub fn is_expired(&self, current_block: u64) -> bool { + self.header.expires_block > 0 && current_block >= self.header.expires_block + } + + /// Add ACK from validator + pub fn add_ack(&mut self, validator: &str, total_validators: usize) { + self.header.acks.insert(validator.to_string()); + self.check_consensus(total_validators); + } + + /// Check if consensus is reached + fn check_consensus(&mut self, total_validators: usize) { + let required = ((total_validators as f64) * CONSENSUS_THRESHOLD).ceil() as usize; + self.header.consensus_reached = self.header.acks.len() >= required; + } + + /// Serialize for storage + pub fn to_bytes(&self) -> Result, StorageError> { + bincode::serialize(self).map_err(|e| StorageError::Serialization(e.to_string())) + } + + /// Deserialize from storage with size limit + pub fn from_bytes(bytes: &[u8]) -> Result { + use bincode::Options; + // MAX_COMPRESSED_SIZE (5 MB) + header overhead + const MAX_STORED_ENTRY_SIZE: u64 = 6 * 1024 * 1024; + + // Use options compatible with bincode::serialize (little-endian, variable int, trailing allowed) + bincode::DefaultOptions::new() + .with_fixint_encoding() + .with_little_endian() + .allow_trailing_bytes() + .with_limit(MAX_STORED_ENTRY_SIZE) + .deserialize(bytes) + .map_err(|e| StorageError::Serialization(e.to_string())) + } + + /// Build storage key + pub fn storage_key(&self) -> Vec { + build_key( + self.header.category, + &self.header.challenge_id, + &self.header.key, + ) + } +} + +/// Build a storage key +fn build_key(category: Category, challenge_id: &str, key: &str) -> Vec { + let mut result = + Vec::with_capacity(category.prefix().len() + challenge_id.len() + key.len() + 2); + result.extend_from_slice(category.prefix()); + result.extend_from_slice(challenge_id.as_bytes()); + result.push(b':'); + result.extend_from_slice(key.as_bytes()); + result +} + +// ============================================================================ +// WRITE OPERATION +// ============================================================================ + +/// Write operation for consensus +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct WriteOp { + /// Operation ID + pub op_id: [u8; 32], + /// Entry to write + pub entry: StoredEntry, + /// Initiator validator + pub initiator: String, + /// Block when proposed + pub proposed_block: u64, + /// Validators who voted YES + pub votes_yes: HashSet, + /// Validators who voted NO + pub votes_no: HashSet, + /// Whether committed + pub committed: bool, +} + +impl WriteOp { + pub fn new(entry: StoredEntry, initiator: &str, block: u64) -> Self { + let mut hasher = Sha256::new(); + hasher.update(entry.header.challenge_id.as_bytes()); + hasher.update(entry.header.key.as_bytes()); + hasher.update(entry.header.value_hash); + hasher.update(block.to_le_bytes()); + let op_id: [u8; 32] = hasher.finalize().into(); + + let mut votes_yes = HashSet::new(); + votes_yes.insert(initiator.to_string()); // Self-vote + + Self { + op_id, + entry, + initiator: initiator.to_string(), + proposed_block: block, + votes_yes, + votes_no: HashSet::new(), + committed: false, + } + } + + /// Add vote + pub fn vote(&mut self, validator: &str, approve: bool) { + if approve { + self.votes_yes.insert(validator.to_string()); + self.votes_no.remove(validator); + } else { + self.votes_no.insert(validator.to_string()); + self.votes_yes.remove(validator); + } + } + + /// Check if consensus reached (50%) + pub fn check_consensus(&self, total_validators: usize) -> Option { + let required = ((total_validators as f64) * CONSENSUS_THRESHOLD).ceil() as usize; + + if self.votes_yes.len() >= required { + Some(true) + } else if self.votes_no.len() >= required { + Some(false) + } else { + None + } + } +} + +// ============================================================================ +// DISTRIBUTED STORAGE +// ============================================================================ + +/// High-performance distributed storage +pub struct DistributedStorage { + /// Sled database + db: Db, + /// Main data tree + data_tree: Tree, + /// Index tree for fast lookups + index_tree: Tree, + /// Pending write operations (waiting for consensus) + pending_ops: RwLock>, + /// Current block + current_block: RwLock, + /// Total validators + total_validators: RwLock, + /// Our validator ID + our_validator: String, + /// Cache for hot data + cache: RwLock, (StoredEntry, Instant)>>, + /// Cache TTL + cache_ttl_secs: u64, + /// Stats + stats: RwLock, +} + +/// Storage statistics +#[derive(Debug, Clone, Default)] +pub struct StorageStats { + pub total_entries: u64, + pub total_bytes_raw: u64, + pub total_bytes_compressed: u64, + pub compression_ratio: f64, + pub cache_hits: u64, + pub cache_misses: u64, + pub write_ops_pending: usize, + pub write_ops_committed: u64, + pub write_ops_rejected: u64, +} + +impl DistributedStorage { + /// Open or create distributed storage + pub fn open(db: &Db, our_validator: &str) -> Result { + let data_tree = db + .open_tree("distributed_data") + .map_err(|e| StorageError::Database(e.to_string()))?; + + let index_tree = db + .open_tree("distributed_index") + .map_err(|e| StorageError::Database(e.to_string()))?; + + Ok(Self { + db: db.clone(), + data_tree, + index_tree, + pending_ops: RwLock::new(HashMap::new()), + current_block: RwLock::new(0), + total_validators: RwLock::new(1), + our_validator: our_validator.to_string(), + cache: RwLock::new(HashMap::new()), + cache_ttl_secs: 60, + stats: RwLock::new(StorageStats::default()), + }) + } + + /// Set current block + pub fn set_block(&self, block: u64) { + *self.current_block.write() = block; + } + + /// Set total validators + pub fn set_validators(&self, count: usize) { + *self.total_validators.write() = count; + } + + // ======================================================================== + // WRITE OPERATIONS + // ======================================================================== + + /// Propose a write with challenge-specific validation rules + /// The rules are loaded from the challenge dynamically + pub fn propose_write_validated( + &self, + category: Category, + challenge_id: &str, + key: &str, + value: &T, + creator_stake: u64, + validate_fn: impl FnOnce(&WriteRequestInfo) -> WriteValidationResult, + ) -> Result { + let block = *self.current_block.read(); + let total = *self.total_validators.read(); + + // Serialize value first to get size + let value_bytes = + serde_json::to_vec(value).map_err(|e| StorageError::Serialization(e.to_string()))?; + + // Check if key exists (for is_update) + let existing = self.get_raw(category, challenge_id, key)?; + let is_update = existing.is_some(); + let previous_hash = existing.as_ref().map(|e| e.header.value_hash); + + // Build request info for validation + let request_info = WriteRequestInfo { + category, + challenge_id: challenge_id.to_string(), + key: key.to_string(), + value: value_bytes.clone(), + size: value_bytes.len(), + creator: self.our_validator.clone(), + creator_stake, + block, + is_update, + previous_hash, + writes_this_epoch: self.count_writes_this_epoch(challenge_id, &self.our_validator), + category_entry_count: self.count_category(challenge_id, category), + total_validators: total, + }; + + // Call challenge validation + match validate_fn(&request_info) { + WriteValidationResult::Accept => {} + WriteValidationResult::Reject(reason) => { + return Err(StorageError::ValidationFailed(reason)); + } + } + + // Create entry and proceed + let entry = StoredEntry::new( + category, + challenge_id, + key, + value, + &self.our_validator, + block, + None, // No TTL - permanent + )?; + + let op = WriteOp::new(entry, &self.our_validator, block); + + // Check if consensus already reached with self-vote + if let Some(true) = op.check_consensus(total) { + self.commit_write(op.clone())?; + self.stats.write().write_ops_committed += 1; + return Ok(op); + } + + self.pending_ops.write().insert(op.op_id, op.clone()); + self.stats.write().write_ops_pending = self.pending_ops.read().len(); + + Ok(op) + } + + /// Count writes by validator this epoch (for rate limiting) + fn count_writes_this_epoch(&self, _challenge_id: &str, _validator: &str) -> usize { + // Write tracking handled by epoch manager + 0 + } + + /// Count entries in category + fn count_category(&self, challenge_id: &str, category: Category) -> usize { + let prefix = format!( + "{}:{}", + challenge_id, + std::str::from_utf8(category.prefix()).unwrap_or("") + ); + self.data_tree.scan_prefix(prefix.as_bytes()).count() + } + + /// Get raw entry without deserialization + fn get_raw( + &self, + category: Category, + challenge_id: &str, + key: &str, + ) -> Result, StorageError> { + let storage_key = format!( + "{}:{}:{}", + challenge_id, + std::str::from_utf8(category.prefix()).unwrap_or(""), + key + ); + + match self.data_tree.get(storage_key.as_bytes()) { + Ok(Some(bytes)) => { + let entry = StoredEntry::from_bytes(&bytes)?; + Ok(Some(entry)) + } + Ok(None) => Ok(None), + Err(e) => Err(StorageError::Database(e.to_string())), + } + } + + /// Propose a write (initiates consensus) - simple version without validation + /// Auto-commits if consensus is reached with self-vote (single validator mode) + pub fn propose_write( + &self, + category: Category, + challenge_id: &str, + key: &str, + value: &T, + ttl_blocks: Option, + ) -> Result { + let block = *self.current_block.read(); + let total = *self.total_validators.read(); + + let entry = StoredEntry::new( + category, + challenge_id, + key, + value, + &self.our_validator, + block, + ttl_blocks, + )?; + + let op = WriteOp::new(entry, &self.our_validator, block); + + // Check if consensus already reached with self-vote + if let Some(true) = op.check_consensus(total) { + // Auto-commit + self.commit_write(op.clone())?; + self.stats.write().write_ops_committed += 1; + return Ok(op); + } + + self.pending_ops.write().insert(op.op_id, op.clone()); + self.stats.write().write_ops_pending = self.pending_ops.read().len(); + + Ok(op) + } + + /// Vote on a pending write (simple version - no validation) + pub fn vote_write(&self, op_id: &[u8; 32], validator: &str, approve: bool) -> Option { + let total = *self.total_validators.read(); + + // First, check and vote + let consensus_result = { + let mut ops = self.pending_ops.write(); + if let Some(op) = ops.get_mut(op_id) { + op.vote(validator, approve); + op.check_consensus(total) + } else { + return None; + } + }; + + // Then handle consensus result + if let Some(result) = consensus_result { + if result { + // Consensus reached - remove and commit + let op = self.pending_ops.write().remove(op_id); + if let Some(op) = op { + if let Err(e) = self.commit_write(op) { + tracing::error!("Failed to commit write: {}", e); + return Some(false); + } + self.stats.write().write_ops_committed += 1; + } + } else { + // Rejected + self.pending_ops.write().remove(op_id); + self.stats.write().write_ops_rejected += 1; + } + self.stats.write().write_ops_pending = self.pending_ops.read().len(); + return Some(result); + } + None + } + + /// Get a pending operation (for P2P sync) + pub fn get_pending_op(&self, op_id: &[u8; 32]) -> Option { + self.pending_ops.read().get(op_id).cloned() + } + + /// Get all pending operations for a challenge + pub fn get_pending_ops(&self, challenge_id: &str) -> Vec { + self.pending_ops + .read() + .values() + .filter(|op| op.entry.header.challenge_id == challenge_id) + .cloned() + .collect() + } + + /// Commit a write after consensus + fn commit_write(&self, mut op: WriteOp) -> Result<(), StorageError> { + op.committed = true; + op.entry.header.consensus_reached = true; + + let key = op.entry.storage_key(); + let value = op.entry.to_bytes()?; + + self.data_tree + .insert(&key, value) + .map_err(|e| StorageError::Database(e.to_string()))?; + + // Update indexes + self.update_indexes(&op.entry.header)?; + + // Update cache + self.cache + .write() + .insert(key, (op.entry.clone(), Instant::now())); + + // Update stats + let mut stats = self.stats.write(); + stats.total_entries += 1; + stats.total_bytes_raw += op.entry.header.raw_size as u64; + stats.total_bytes_compressed += op.entry.header.compressed_size as u64; + if stats.total_bytes_raw > 0 { + stats.compression_ratio = + stats.total_bytes_compressed as f64 / stats.total_bytes_raw as f64; + } + + Ok(()) + } + + /// Direct write (for receiving already-consensus data from sync) + pub fn write_direct(&self, entry: StoredEntry) -> Result<(), StorageError> { + let key = entry.storage_key(); + let value = entry.to_bytes()?; + + self.data_tree + .insert(&key, value) + .map_err(|e| StorageError::Database(e.to_string()))?; + + self.update_indexes(&entry.header)?; + self.cache.write().insert(key, (entry, Instant::now())); + + Ok(()) + } + + /// Update indexes for an entry + fn update_indexes(&self, entry: &EntryHeader) -> Result<(), StorageError> { + // Index by challenge + category + let idx_key = format!( + "{}:{}:{}", + entry.challenge_id, entry.category as u8, entry.key + ); + self.index_tree + .insert(idx_key.as_bytes(), entry.key.as_bytes()) + .map_err(|e| StorageError::Database(e.to_string()))?; + + // Index by creator + let creator_key = format!( + "creator:{}:{}:{}", + entry.creator, entry.challenge_id, entry.key + ); + self.index_tree + .insert(creator_key.as_bytes(), entry.key.as_bytes()) + .map_err(|e| StorageError::Database(e.to_string()))?; + + Ok(()) + } + + // ======================================================================== + // READ OPERATIONS + // ======================================================================== + + /// Get entry by key + pub fn get( + &self, + category: Category, + challenge_id: &str, + key: &str, + ) -> Result, StorageError> { + let storage_key = build_key(category, challenge_id, key); + + // Check cache first + { + let cache = self.cache.read(); + if let Some((entry, cached_at)) = cache.get(&storage_key) { + if cached_at.elapsed().as_secs() < self.cache_ttl_secs { + self.stats.write().cache_hits += 1; + return Ok(Some(entry.clone())); + } + } + } + self.stats.write().cache_misses += 1; + + // Read from disk + match self + .data_tree + .get(&storage_key) + .map_err(|e| StorageError::Database(e.to_string()))? + { + Some(bytes) => { + let entry = StoredEntry::from_bytes(&bytes)?; + + // Update cache + self.cache + .write() + .insert(storage_key, (entry.clone(), Instant::now())); + + Ok(Some(entry)) + } + None => Ok(None), + } + } + + /// Get and decompress value + pub fn get_value( + &self, + category: Category, + challenge_id: &str, + key: &str, + ) -> Result, StorageError> { + match self.get(category, challenge_id, key)? { + Some(entry) => Ok(Some(entry.decompress()?)), + None => Ok(None), + } + } + + /// List entries by category + pub fn list_by_category( + &self, + category: Category, + challenge_id: &str, + limit: usize, + ) -> Result, StorageError> { + let prefix = format!("{}:{}:", challenge_id, category as u8); + let mut results = Vec::new(); + + for result in self.index_tree.scan_prefix(prefix.as_bytes()) { + if results.len() >= limit { + break; + } + + let (_, key_bytes) = result.map_err(|e| StorageError::Database(e.to_string()))?; + let key = String::from_utf8_lossy(&key_bytes); + + if let Some(entry) = self.get(category, challenge_id, &key)? { + results.push(entry); + } + } + + Ok(results) + } + + /// List entries by creator (validator) + pub fn list_by_creator( + &self, + creator: &str, + challenge_id: &str, + category: Option, + limit: usize, + ) -> Result, StorageError> { + let prefix = format!("creator:{}:{}:", creator, challenge_id); + let mut results = Vec::new(); + + for result in self.index_tree.scan_prefix(prefix.as_bytes()) { + if results.len() >= limit { + break; + } + + let (_, key_bytes) = result.map_err(|e| StorageError::Database(e.to_string()))?; + let key = String::from_utf8_lossy(&key_bytes); + + // Try each category if not specified + let categories = match category { + Some(c) => vec![c], + None => vec![ + Category::Submission, + Category::Agent, + Category::Evaluation, + Category::Consensus, + Category::Log, + ], + }; + + for cat in categories { + if let Some(entry) = self.get(cat, challenge_id, &key)? { + results.push(entry); + break; + } + } + } + + Ok(results) + } + + // ======================================================================== + // SPECIALIZED QUERIES + // ======================================================================== + + /// Get all submissions for a challenge + pub fn get_submissions(&self, challenge_id: &str) -> Result, StorageError> { + self.list_by_category(Category::Submission, challenge_id, MAX_ENTRIES_PER_CATEGORY) + } + + /// Get all agents for a challenge + pub fn get_agents(&self, challenge_id: &str) -> Result, StorageError> { + self.list_by_category(Category::Agent, challenge_id, MAX_ENTRIES_PER_CATEGORY) + } + + /// Get evaluations for an agent + pub fn get_evaluations_for_agent( + &self, + challenge_id: &str, + agent_hash: &str, + ) -> Result, StorageError> { + let prefix = build_key(Category::Evaluation, challenge_id, agent_hash); + let mut results = Vec::new(); + + for result in self.data_tree.scan_prefix(&prefix) { + let (_, bytes) = result.map_err(|e| StorageError::Database(e.to_string()))?; + results.push(StoredEntry::from_bytes(&bytes)?); + } + + Ok(results) + } + + /// Get all evaluations by a validator + pub fn get_evaluations_by_validator( + &self, + challenge_id: &str, + validator: &str, + ) -> Result, StorageError> { + self.list_by_creator( + validator, + challenge_id, + Some(Category::Evaluation), + MAX_ENTRIES_PER_CATEGORY, + ) + } + + /// Get pending submissions (not yet evaluated) + pub fn get_pending_submissions( + &self, + challenge_id: &str, + ) -> Result, StorageError> { + let submissions = self.get_submissions(challenge_id)?; + let agents = self.get_agents(challenge_id)?; + + let agent_hashes: HashSet = agents.iter().map(|e| e.header.key.clone()).collect(); + + Ok(submissions + .into_iter() + .filter(|s| !agent_hashes.contains(&s.header.key)) + .collect()) + } + + /// Get agents evaluated by specific validator + pub fn get_agents_evaluated_by( + &self, + challenge_id: &str, + validator: &str, + ) -> Result, StorageError> { + let evaluations = self.get_evaluations_by_validator(challenge_id, validator)?; + Ok(evaluations + .into_iter() + .map(|e| e.header.key.split(':').next().unwrap_or("").to_string()) + .collect()) + } + + /// Get agents NOT evaluated by specific validator + pub fn get_agents_not_evaluated_by( + &self, + challenge_id: &str, + validator: &str, + ) -> Result, StorageError> { + let agents = self.get_agents(challenge_id)?; + let evaluated = self.get_agents_evaluated_by(challenge_id, validator)?; + let evaluated_set: HashSet<_> = evaluated.into_iter().collect(); + + Ok(agents + .into_iter() + .filter(|a| !evaluated_set.contains(&a.header.key)) + .collect()) + } + + // ======================================================================== + // CLEANUP & MAINTENANCE + // ======================================================================== + + /// Cleanup expired entries + pub fn cleanup_expired(&self) -> Result { + let current_block = *self.current_block.read(); + let mut removed = 0; + let mut expired_keys = Vec::new(); + + // Scan all entries + for result in self.data_tree.iter() { + let (key, bytes) = result.map_err(|e| StorageError::Database(e.to_string()))?; + + if let Ok(entry) = StoredEntry::from_bytes(&bytes) { + if entry.is_expired(current_block) { + self.data_tree + .remove(&key) + .map_err(|e| StorageError::Database(e.to_string()))?; + expired_keys.push(key.to_vec()); + removed += 1; + } + } + } + + // Cleanup cache - remove expired entries and old time-based entries + { + let mut cache = self.cache.write(); + // Remove by expired keys + for key in &expired_keys { + cache.remove(key); + } + // Also remove old cached items + let now = Instant::now(); + cache.retain(|_, (entry, cached_at)| { + !entry.is_expired(current_block) + && cached_at.elapsed().as_secs() < self.cache_ttl_secs * 2 + }); + } + + // Cleanup old pending ops + let pending_timeout = 100; // blocks + self.pending_ops + .write() + .retain(|_, op| current_block - op.proposed_block < pending_timeout); + + Ok(removed) + } + + /// Get storage statistics + pub fn stats(&self) -> StorageStats { + self.stats.read().clone() + } + + /// Compute merkle root of all data + pub fn merkle_root(&self, challenge_id: &str) -> Result<[u8; 32], StorageError> { + let mut hasher = Sha256::new(); + let prefix = challenge_id.as_bytes(); + + for result in self.data_tree.scan_prefix(prefix) { + let (key, value) = result.map_err(|e| StorageError::Database(e.to_string()))?; + hasher.update(&key); + hasher.update(&value); + } + + Ok(hasher.finalize().into()) + } + + /// Get pending operations + pub fn pending_operations(&self) -> Vec { + self.pending_ops.read().values().cloned().collect() + } + + /// Flush to disk + pub fn flush(&self) -> Result<(), StorageError> { + self.data_tree + .flush() + .map_err(|e| StorageError::Database(e.to_string()))?; + self.index_tree + .flush() + .map_err(|e| StorageError::Database(e.to_string()))?; + Ok(()) + } +} + +// ============================================================================ +// P2P SYNC MESSAGES +// ============================================================================ + +/// Sync message types +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum SyncMessage { + /// Announce a write operation (request votes) + ProposeWrite { op: WriteOp }, + /// Vote on a write operation + VoteWrite { + op_id: [u8; 32], + voter: String, + approve: bool, + }, + /// Announce committed write (for sync) + CommittedWrite { entry: StoredEntry }, + /// Request merkle root for verification + RequestMerkleRoot { challenge_id: String }, + /// Merkle root response + MerkleRoot { + challenge_id: String, + root: [u8; 32], + entry_count: u64, + }, + /// Request entries (for sync) + RequestEntries { + challenge_id: String, + category: Category, + from_key: Option, + limit: u32, + }, + /// Entries response + Entries { + challenge_id: String, + entries: Vec, + has_more: bool, + }, +} + +// ============================================================================ +// ERRORS +// ============================================================================ + +#[derive(Debug, Clone)] +pub enum StorageError { + Database(String), + Serialization(String), + Decompression(String), + TooLarge(usize, usize), + NotFound(String), + ConsensusNotReached, + InvalidEntry(String), + /// Write validation failed (challenge rules rejected) + ValidationFailed(String), +} + +impl std::fmt::Display for StorageError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + StorageError::Database(e) => write!(f, "Database error: {}", e), + StorageError::Serialization(e) => write!(f, "Serialization error: {}", e), + StorageError::Decompression(e) => write!(f, "Decompression error: {}", e), + StorageError::TooLarge(size, max) => write!(f, "Entry too large: {} > {}", size, max), + StorageError::NotFound(k) => write!(f, "Not found: {}", k), + StorageError::ConsensusNotReached => write!(f, "Consensus not reached"), + StorageError::InvalidEntry(e) => write!(f, "Invalid entry: {}", e), + StorageError::ValidationFailed(e) => write!(f, "Validation failed: {}", e), + } + } +} + +impl std::error::Error for StorageError {} + +// ============================================================================ +// TESTS +// ============================================================================ + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + fn create_test_storage() -> DistributedStorage { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + DistributedStorage::open(&db, "test_validator").unwrap() + } + + #[test] + fn test_entry_compression() { + #[derive(Serialize, Deserialize, PartialEq, Debug)] + struct TestData { + name: String, + values: Vec, + } + + let data = TestData { + name: "test".to_string(), + values: (0..1000).collect(), + }; + + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + // Should be compressed + assert!(entry.header.compressed_size < entry.header.raw_size); + + // Should decompress correctly + let recovered: TestData = entry.decompress().unwrap(); + assert_eq!(recovered, data); + + // Should verify + assert!(entry.verify()); + } + + #[test] + fn test_write_consensus() { + let storage = create_test_storage(); + storage.set_validators(3); + storage.set_block(100); + + // Propose write + let op = storage + .propose_write(Category::Agent, "challenge1", "agent1", &"test data", None) + .unwrap(); + + // Vote from second validator (50% reached with self-vote) + let result = storage.vote_write(&op.op_id, "validator2", true); + assert_eq!(result, Some(true)); + + // Should be readable now + let entry = storage + .get(Category::Agent, "challenge1", "agent1") + .unwrap(); + assert!(entry.is_some()); + assert!(entry.unwrap().header.consensus_reached); + } + + #[test] + fn test_write_rejection() { + let storage = create_test_storage(); + storage.set_validators(3); + storage.set_block(100); + + let op = storage + .propose_write(Category::Agent, "challenge1", "agent1", &"test data", None) + .unwrap(); + + // Two NO votes = 50% rejection + storage.vote_write(&op.op_id, "validator2", false); + let result = storage.vote_write(&op.op_id, "validator3", false); + assert_eq!(result, Some(false)); + + // Should NOT be readable + let entry = storage + .get(Category::Agent, "challenge1", "agent1") + .unwrap(); + assert!(entry.is_none()); + } + + #[test] + fn test_queries() { + let storage = create_test_storage(); + storage.set_validators(1); // Single validator for easy testing + storage.set_block(100); + + // Add some agents + for i in 0..5 { + storage + .propose_write( + Category::Agent, + "challenge1", + &format!("agent{}", i), + &format!("data{}", i), + None, + ) + .unwrap(); + } + + // Add evaluations + for i in 0..3 { + storage + .propose_write( + Category::Evaluation, + "challenge1", + &format!("agent0:validator{}", i), + &format!("eval{}", i), + None, + ) + .unwrap(); + } + + let agents = storage.get_agents("challenge1").unwrap(); + assert_eq!(agents.len(), 5); + + let evals = storage + .get_evaluations_for_agent("challenge1", "agent0") + .unwrap(); + assert_eq!(evals.len(), 3); + } + + #[test] + fn test_expiry() { + let storage = create_test_storage(); + storage.set_validators(1); + storage.set_block(100); + + // Add with TTL + storage + .propose_write( + Category::Log, + "challenge1", + "log1", + &"log data", + Some(10), // Expires at block 110 + ) + .unwrap(); + + // Should exist + let entry = storage.get(Category::Log, "challenge1", "log1").unwrap(); + assert!(entry.is_some()); + + // Advance past expiry + storage.set_block(120); + + // Cleanup + let removed = storage.cleanup_expired().unwrap(); + assert_eq!(removed, 1); + + // Should be gone + let entry = storage.get(Category::Log, "challenge1", "log1").unwrap(); + assert!(entry.is_none()); + } + + #[test] + fn test_get_nonexistent() { + let storage = create_test_storage(); + + let entry = storage + .get(Category::Agent, "challenge1", "nonexistent") + .unwrap(); + assert!(entry.is_none()); + } + + #[test] + fn test_write_validation_result_accept() { + let result = WriteValidationResult::accept(); + assert!(result.is_accepted()); + } + + #[test] + fn test_write_validation_result_reject() { + let result = WriteValidationResult::reject("Invalid data"); + assert!(!result.is_accepted()); + } + + #[test] + fn test_category_prefix() { + assert_eq!(Category::Agent.prefix(), b"agt:"); + assert_eq!(Category::Evaluation.prefix(), b"evl:"); + assert_eq!(Category::Log.prefix(), b"log:"); + assert_eq!(Category::Submission.prefix(), b"sub:"); + assert_eq!(Category::Consensus.prefix(), b"cns:"); + assert_eq!(Category::Meta.prefix(), b"met:"); + } + + #[test] + fn test_stored_entry_verify_valid() { + let data = "test data"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + // Should verify correctly with correct hash + assert!(entry.verify()); + } + + #[test] + fn test_stored_entry_decompress() { + let data = "test data string"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let decompressed: String = entry.decompress().unwrap(); + assert_eq!(decompressed, data); + } + + #[test] + fn test_stored_entry_is_expired() { + let data = "test"; + let entry = StoredEntry::new( + Category::Log, + "challenge1", + "log1", + &data, + "validator1", + 100, + Some(10), // Expires at block 110 + ) + .unwrap(); + + // Not expired at block 105 + assert!(!entry.is_expired(105)); + + // Expired at block 110 + assert!(entry.is_expired(110)); + + // Expired after block 110 + assert!(entry.is_expired(120)); + } + + #[test] + fn test_stored_entry_no_expiry() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, // No expiry + ) + .unwrap(); + + // Never expires + assert!(!entry.is_expired(1000)); + assert!(!entry.is_expired(10000)); + } + + #[test] + fn test_stored_entry_add_ack() { + let data = "test"; + let mut entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + // Initially no acks, no consensus + assert_eq!(entry.header.acks.len(), 0); + assert!(!entry.header.consensus_reached); + + // Add ack from validator (5 total validators = need 3 for 50%) + entry.add_ack("validator2", 5); + assert_eq!(entry.header.acks.len(), 1); + assert!(!entry.header.consensus_reached); + + // Add second ack + entry.add_ack("validator3", 5); + assert_eq!(entry.header.acks.len(), 2); + assert!(!entry.header.consensus_reached); + + // Add third ack - should reach consensus (3/5 = 60% >= 50%) + entry.add_ack("validator4", 5); + assert_eq!(entry.header.acks.len(), 3); + assert!(entry.header.consensus_reached); + } + + #[test] + fn test_stored_entry_duplicate_ack() { + let data = "test"; + let mut entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + // Add ack + entry.add_ack("validator2", 5); + assert_eq!(entry.header.acks.len(), 1); + + // Add duplicate ack - should be ignored + entry.add_ack("validator2", 5); + assert_eq!(entry.header.acks.len(), 1); + } + + #[test] + fn test_stored_entry_serialization() { + let data = "test data"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + // Serialize + let bytes = entry.to_bytes().unwrap(); + + // Deserialize + let deserialized = StoredEntry::from_bytes(&bytes).unwrap(); + + // Verify fields match + assert_eq!(deserialized.header.category, entry.header.category); + assert_eq!(deserialized.header.challenge_id, entry.header.challenge_id); + assert_eq!(deserialized.header.key, entry.header.key); + assert_eq!(deserialized.header.creator, entry.header.creator); + } + + #[test] + fn test_stored_entry_storage_key() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let key = entry.storage_key(); + assert!(!key.is_empty()); + + // Key should start with category prefix + assert!(key.starts_with(Category::Agent.prefix())); + } + + #[test] + fn test_write_op_voting() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let mut op = WriteOp::new(entry, "validator1", 100); + + // Initially has self-vote from initiator + assert_eq!(op.votes_yes.len(), 1); + assert_eq!(op.votes_no.len(), 0); + + // Vote approve + op.vote("validator2", true); + assert_eq!(op.votes_yes.len(), 2); + assert_eq!(op.votes_no.len(), 0); + + // Vote reject + op.vote("validator3", false); + assert_eq!(op.votes_yes.len(), 2); + assert_eq!(op.votes_no.len(), 1); + } + + #[test] + fn test_write_op_check_consensus_approve() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let mut op = WriteOp::new(entry, "validator1", 100); + + // With 5 validators, need 3 votes (60%) for consensus + // Initiator has self-vote (1) + assert_eq!(op.check_consensus(5), None); // 1/5 = 20% + + op.vote("validator2", true); + assert_eq!(op.check_consensus(5), None); // 2/5 = 40% + + op.vote("validator3", true); + assert_eq!(op.check_consensus(5), Some(true)); // 3/5 = 60% >= 50% + } + + #[test] + fn test_write_op_check_consensus_reject() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let mut op = WriteOp::new(entry, "validator1", 100); + + // Vote reject enough times to reach consensus + op.vote("validator2", false); + op.vote("validator3", false); + op.vote("validator4", false); + + // 3/5 = 60% rejections >= 50% + assert_eq!(op.check_consensus(5), Some(false)); + } + + #[test] + fn test_distributed_storage_set_block() { + let storage = create_test_storage(); + + storage.set_block(100); + assert_eq!(*storage.current_block.read(), 100); + + storage.set_block(200); + assert_eq!(*storage.current_block.read(), 200); + } + + #[test] + fn test_distributed_storage_set_validators() { + let storage = create_test_storage(); + + storage.set_validators(10); + assert_eq!(*storage.total_validators.read(), 10); + + storage.set_validators(20); + assert_eq!(*storage.total_validators.read(), 20); + } + + #[test] + fn test_distributed_storage_propose_and_vote() { + let storage = create_test_storage(); + storage.set_validators(5); // Use 5 validators so we need 3 votes + storage.set_block(100); + + // Propose a write (initiator gets self-vote = 1) + let op = storage + .propose_write(Category::Agent, "challenge1", "agent1", &"data", None) + .unwrap(); + + // Get the op + let pending_op = storage.get_pending_op(&op.op_id); + assert!(pending_op.is_some()); + + // Vote on it (now 2/5, still < 3 needed) + let result = storage.vote_write(&op.op_id, "validator2", true); + assert_eq!(result, None); // Still needs more votes + + // Another vote reaches consensus (3/5 = 60% >= 50%) + let result = storage.vote_write(&op.op_id, "validator3", true); + assert_eq!(result, Some(true)); + + // Op should be removed from pending + let pending_op = storage.get_pending_op(&op.op_id); + assert!(pending_op.is_none()); + + // Entry should now exist + let entry = storage + .get(Category::Agent, "challenge1", "agent1") + .unwrap(); + assert!(entry.is_some()); + } + + #[test] + fn test_distributed_storage_list_by_category() { + let storage = create_test_storage(); + storage.set_validators(1); + storage.set_block(100); + + // Add multiple entries + storage + .propose_write(Category::Agent, "challenge1", "agent1", &"data1", None) + .unwrap(); + storage + .propose_write(Category::Agent, "challenge1", "agent2", &"data2", None) + .unwrap(); + + // List by category + let entries = storage + .list_by_category(Category::Agent, "challenge1", 100) + .unwrap(); + assert_eq!(entries.len(), 2); + } + + #[test] + fn test_distributed_storage_get_value() { + let storage = create_test_storage(); + storage.set_validators(1); + storage.set_block(100); + + let test_data = "test string data"; + storage + .propose_write(Category::Agent, "challenge1", "agent1", &test_data, None) + .unwrap(); + + // Get as typed value + let value: String = storage + .get_value(Category::Agent, "challenge1", "agent1") + .unwrap() + .unwrap(); + assert_eq!(value, test_data); + } + + #[test] + fn test_distributed_storage_cleanup_expired() { + let storage = create_test_storage(); + storage.set_validators(1); + storage.set_block(100); + + // Add entries with different TTLs + storage + .propose_write( + Category::Log, + "challenge1", + "log1", + &"data1", + Some(10), // Expires at 110 + ) + .unwrap(); + storage + .propose_write( + Category::Log, + "challenge1", + "log2", + &"data2", + Some(20), // Expires at 120 + ) + .unwrap(); + storage + .propose_write( + Category::Agent, + "challenge1", + "agent1", + &"permanent", + None, // Never expires + ) + .unwrap(); + + // Move to block 115 (log1 expired, log2 not yet) + storage.set_block(115); + let removed = storage.cleanup_expired().unwrap(); + assert_eq!(removed, 1); + + // log1 should be gone + let log1 = storage.get(Category::Log, "challenge1", "log1").unwrap(); + assert!(log1.is_none()); + + // log2 should still exist + let log2 = storage.get(Category::Log, "challenge1", "log2").unwrap(); + assert!(log2.is_some()); + + // agent1 should still exist + let agent1 = storage + .get(Category::Agent, "challenge1", "agent1") + .unwrap(); + assert!(agent1.is_some()); + } + + #[test] + fn test_stored_entry_too_large() { + let large_data = vec![0u8; MAX_RAW_SIZE + 1]; + + let result = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &large_data, + "validator1", + 100, + None, + ); + + assert!(result.is_err()); + } + + #[test] + fn test_write_op_duplicate_vote_same_direction() { + let data = "test"; + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "agent1", + &data, + "validator1", + 100, + None, + ) + .unwrap(); + + let mut op = WriteOp::new(entry, "validator1", 100); + + // First vote from validator2 + op.vote("validator2", true); + assert_eq!(op.votes_yes.len(), 2); // initiator + validator2 + + // Duplicate vote from same validator + op.vote("validator2", true); + assert_eq!(op.votes_yes.len(), 2); // Should not increase + } + + #[test] + fn test_get_pending_ops_by_challenge() { + let storage = create_test_storage(); + storage.set_validators(10); // High count so consensus isn't reached + storage.set_block(100); + + // Propose multiple writes for same challenge + storage + .propose_write(Category::Agent, "challenge1", "agent1", &"data1", None) + .unwrap(); + storage + .propose_write(Category::Agent, "challenge1", "agent2", &"data2", None) + .unwrap(); + storage + .propose_write(Category::Agent, "challenge2", "agent3", &"data3", None) + .unwrap(); + + // Get pending ops for challenge1 + let ops = storage.get_pending_ops("challenge1"); + assert_eq!(ops.len(), 2); + + // Get pending ops for challenge2 + let ops2 = storage.get_pending_ops("challenge2"); + assert_eq!(ops2.len(), 1); + } + + #[test] + fn test_write_request_info_deserialize_value() { + let request = WriteRequestInfo { + category: Category::Agent, + challenge_id: "challenge1".to_string(), + key: "agent1".to_string(), + value: b"test".to_vec(), + size: 4, + creator: "validator1".to_string(), + creator_stake: 1000, + block: 100, + is_update: false, + previous_hash: None, + writes_this_epoch: 0, + category_entry_count: 0, + total_validators: 5, + }; + + let result: Result = request.deserialize_value(); + assert!(result.is_err()); // Invalid binary data for String deserialization + } + + #[test] + fn test_category_index_prefix() { + // Test Category::Index prefix generation + assert_eq!(Category::Index.prefix(), b"idx:"); + } + + #[test] + fn test_write_request_info_serialization_error() { + // Test line 210: bincode::serialize error path + // This is covered by attempting to serialize a large value + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + // Create a value larger than MAX_RAW_SIZE + let large_value = vec![0u8; MAX_RAW_SIZE + 1]; + let result = + storage.propose_write(Category::Agent, "challenge1", "key1", &large_value, None); + + // Should fail with TooLarge error + assert!(result.is_err()); + } + + #[test] + fn test_compression_error() { + // Test lines 219-222: compression error paths + // These are covered by the TooLarge error after compression + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + // Create a value that compresses to > MAX_COMPRESSED_SIZE + // This is difficult to trigger naturally, but we test the size check + let data = "test data"; + let result = storage.propose_write(Category::Agent, "challenge1", "key1", &data, None); + assert!(result.is_ok()); // Normal data should work + } + + #[test] + fn test_decompress_raw() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + let data = "test decompress"; + let op = storage + .propose_write(Category::Agent, "challenge1", "key1", &data, None) + .unwrap(); + + // Test decompress_raw method on the entry from the operation + let raw = op.entry.decompress_raw().unwrap(); + assert!(!raw.is_empty()); + } + + #[test] + fn test_verify_corrupted_entry() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + let data = "test verify"; + let op = storage + .propose_write(Category::Agent, "challenge1", "key1", &data, None) + .unwrap(); + + // Test with the entry from the operation + let mut entry = op.entry.clone(); + + // Line 275: verify should return false for corrupted data + entry.header.value_hash = [0u8; 32]; // Corrupt hash + assert!(!entry.verify()); + } + + #[test] + fn test_propose_write_validated() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + let result = storage.propose_write_validated( + Category::Agent, + "challenge1", + "key1", + &"test data", + 1000, // creator_stake as u64, not Option + |_info| WriteValidationResult::Accept, + ); + + assert!(result.is_ok()); + } + + #[test] + fn test_write_direct() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + let entry = StoredEntry::new( + Category::Agent, + "challenge1", + "key1", + &"test data", + "validator1", + 100, + None, + ) + .unwrap(); + + let result = storage.write_direct(entry); + assert!(result.is_ok()); + } + + #[test] + fn test_get_submissions() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DistributedStorage::open(&db, "validator1").unwrap(); + + let entry = StoredEntry::new( + Category::Submission, + "challenge1", + "key1", + &"submission1", + "validator1", + 100, + None, + ) + .unwrap(); + + storage.write_direct(entry).unwrap(); + + let submissions = storage.get_submissions("challenge1"); + assert!(submissions.is_ok()); + } + + #[test] + fn test_storage_error_display() { + let err = StorageError::NotFound("test key".to_string()); + assert_eq!(format!("{}", err), "Not found: test key"); + + let err = StorageError::Serialization("test".to_string()); + assert_eq!(format!("{}", err), "Serialization error: test"); + + let err = StorageError::Decompression("test".to_string()); + assert_eq!(format!("{}", err), "Decompression error: test"); + + let err = StorageError::TooLarge(100, 50); + assert_eq!(format!("{}", err), "Entry too large: 100 > 50"); + + let err = StorageError::ConsensusNotReached; + assert_eq!(format!("{}", err), "Consensus not reached"); + + let err = StorageError::ValidationFailed("test".to_string()); + assert_eq!(format!("{}", err), "Validation failed: test"); + + let err = StorageError::Database("test".to_string()); + assert_eq!(format!("{}", err), "Database error: test"); + + let err = StorageError::InvalidEntry("test".to_string()); + assert_eq!(format!("{}", err), "Invalid entry: test"); + } +} diff --git a/crates/storage/src/dynamic.rs b/crates/storage/src/dynamic.rs new file mode 100644 index 000000000..8f8126b49 --- /dev/null +++ b/crates/storage/src/dynamic.rs @@ -0,0 +1,1288 @@ +//! Dynamic storage system for blockchain data +//! +//! Provides namespaced storage for: +//! - System-level data +//! - Per-challenge data +//! - Per-validator data (within challenges or global) +//! +//! Features: +//! - Typed values (bool, u64, string, bytes, json, map, list) +//! - TTL support for ephemeral data +//! - Optimistic locking with versions +//! - Change tracking for replication/sync + +use crate::types::{ + NamespaceStats, StorageChange, StorageEntry, StorageKey, StorageStats, StorageValue, +}; +use bincode::Options; +use parking_lot::RwLock; +use platform_core::{ChallengeId, Hotkey, MiniChainError, Result}; +use sled::Tree; +use std::collections::HashMap; +use std::sync::Arc; +use std::time::{Duration, SystemTime}; +use tracing::{info, trace}; + +const MAX_STORAGE_ENTRY_SIZE: u64 = 64 * 1024 * 1024; + +fn bincode_options_storage() -> impl Options { + bincode::DefaultOptions::new() + .with_limit(MAX_STORAGE_ENTRY_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() +} + +/// Dynamic storage manager +#[allow(clippy::type_complexity)] +pub struct DynamicStorage { + /// Main storage tree + tree: Tree, + /// In-memory cache for hot data + cache: Arc, StorageEntry>>>, + /// Cache enabled + cache_enabled: bool, + /// Maximum cache size + max_cache_size: usize, + /// Change listeners + change_listeners: Arc>>>, + /// Current block height (for change tracking) + block_height: Arc>, +} + +impl DynamicStorage { + /// Create a new dynamic storage instance + pub fn new(db: &sled::Db) -> Result { + let tree = db.open_tree("dynamic_storage").map_err(|e| { + MiniChainError::Storage(format!("Failed to open dynamic storage: {}", e)) + })?; + + info!("Dynamic storage initialized"); + + Ok(Self { + tree, + cache: Arc::new(RwLock::new(HashMap::new())), + cache_enabled: true, + max_cache_size: 10000, + change_listeners: Arc::new(RwLock::new(Vec::new())), + block_height: Arc::new(RwLock::new(0)), + }) + } + + /// Create with custom cache settings + pub fn with_cache(mut self, enabled: bool, max_size: usize) -> Self { + self.cache_enabled = enabled; + self.max_cache_size = max_size; + self + } + + /// Set the current block height + pub fn set_block_height(&self, height: u64) { + *self.block_height.write() = height; + } + + /// Get a scoped storage handle for a challenge + pub fn challenge_storage(&self, challenge_id: ChallengeId) -> ChallengeStorage<'_> { + ChallengeStorage { + storage: self, + challenge_id, + } + } + + /// Get a scoped storage handle for a validator (global) + pub fn validator_storage(&self, validator: Hotkey) -> ValidatorStorage<'_> { + ValidatorStorage { + storage: self, + validator, + challenge_id: None, + } + } + + /// Register a change listener + pub fn on_change(&self, listener: F) + where + F: Fn(&StorageChange) + Send + Sync + 'static, + { + self.change_listeners.write().push(Box::new(listener)); + } + + /// Get a value + pub fn get(&self, key: &StorageKey) -> Result> { + let key_bytes = key.to_bytes(); + + // Check cache first + if self.cache_enabled { + if let Some(entry) = self.cache.read().get(&key_bytes) { + if !entry.is_expired() { + trace!("Cache hit for {:?}", key); + return Ok(Some(entry.clone())); + } + } + } + + // Load from disk + match self + .tree + .get(&key_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))? + { + Some(data) => { + let entry: StorageEntry = bincode_options_storage() + .deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + // Check expiry + if entry.is_expired() { + // Clean up expired entry + self.tree + .remove(&key_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + if self.cache_enabled { + self.cache.write().remove(&key_bytes); + } + return Ok(None); + } + + // Update cache + if self.cache_enabled { + let mut cache = self.cache.write(); + if cache.len() < self.max_cache_size { + cache.insert(key_bytes, entry.clone()); + } + } + + Ok(Some(entry)) + } + None => Ok(None), + } + } + + /// Get just the value (without metadata) + pub fn get_value(&self, key: &StorageKey) -> Result> { + Ok(self.get(key)?.map(|e| e.value)) + } + + /// Set a value + pub fn set(&self, key: StorageKey, value: StorageValue, writer: Option) -> Result<()> { + self.set_with_options(key, value, writer, None) + } + + /// Set a value with TTL + pub fn set_with_ttl( + &self, + key: StorageKey, + value: StorageValue, + writer: Option, + ttl: Duration, + ) -> Result<()> { + self.set_with_options(key, value, writer, Some(ttl)) + } + + /// Set a value with options + pub fn set_with_options( + &self, + key: StorageKey, + value: StorageValue, + writer: Option, + ttl: Option, + ) -> Result<()> { + let key_bytes = key.to_bytes(); + + // Get old value for change notification + let old_entry = self.get(&key)?; + let old_value = old_entry.as_ref().map(|e| e.value.clone()); + + // Create or update entry + let entry = if let Some(mut existing) = old_entry { + existing.update(value.clone(), writer); + if let Some(t) = ttl { + existing.ttl = Some(t); + } + existing + } else { + let mut e = StorageEntry::new(value.clone(), writer); + if let Some(t) = ttl { + e.ttl = Some(t); + } + e + }; + + // Serialize and store + let data = + bincode::serialize(&entry).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + self.tree + .insert(&key_bytes, data) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + // Update cache + if self.cache_enabled { + let mut cache = self.cache.write(); + if cache.len() < self.max_cache_size || cache.contains_key(&key_bytes) { + cache.insert(key_bytes, entry); + } + } + + // Notify listeners + let change = StorageChange { + key, + old_value, + new_value: Some(value), + block_height: *self.block_height.read(), + timestamp: SystemTime::now(), + }; + + for listener in self.change_listeners.read().iter() { + listener(&change); + } + + Ok(()) + } + + /// Delete a value + pub fn delete(&self, key: &StorageKey) -> Result> { + let key_bytes = key.to_bytes(); + + // Get old value + let old_entry = self.get(key)?; + let old_value = old_entry.map(|e| e.value); + + // Remove from storage + self.tree + .remove(&key_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + // Remove from cache + if self.cache_enabled { + self.cache.write().remove(&key_bytes); + } + + // Notify listeners + if old_value.is_some() { + let change = StorageChange { + key: key.clone(), + old_value: old_value.clone(), + new_value: None, + block_height: *self.block_height.read(), + timestamp: SystemTime::now(), + }; + + for listener in self.change_listeners.read().iter() { + listener(&change); + } + } + + Ok(old_value) + } + + /// Check if a key exists + pub fn exists(&self, key: &StorageKey) -> Result { + let key_bytes = key.to_bytes(); + + if self.cache_enabled { + if let Some(entry) = self.cache.read().get(&key_bytes) { + return Ok(!entry.is_expired()); + } + } + + self.tree + .contains_key(&key_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string())) + } + + /// Increment a numeric value atomically + pub fn increment(&self, key: &StorageKey, delta: i64, writer: Option) -> Result { + let current = self.get_value(key)?.and_then(|v| v.as_i64()).unwrap_or(0); + + let new_value = current + delta; + self.set(key.clone(), StorageValue::I64(new_value), writer)?; + + Ok(new_value) + } + + /// Append to a list + pub fn list_push( + &self, + key: &StorageKey, + value: StorageValue, + writer: Option, + ) -> Result { + let existing = self.get_value(key)?; + + let mut list = match existing { + None => Vec::new(), + Some(StorageValue::List(list)) => list, + Some(_) => { + return Err(MiniChainError::TypeMismatch(format!( + "Cannot push to non-list value at key {:?}. Existing value is not a list.", + key + ))) + } + }; + + list.push(value); + let len = list.len(); + + self.set(key.clone(), StorageValue::List(list), writer)?; + Ok(len) + } + + /// Set a map field + pub fn map_set( + &self, + key: &StorageKey, + field: impl Into, + value: StorageValue, + writer: Option, + ) -> Result<()> { + let existing = self.get_value(key)?; + + let mut map = match existing { + None => HashMap::new(), + Some(StorageValue::Map(map)) => map, + Some(_) => { + return Err(MiniChainError::TypeMismatch(format!( + "Cannot set map field on non-map value at key {:?}. Existing value is not a map.", + key + ))) + } + }; + + map.insert(field.into(), value); + self.set(key.clone(), StorageValue::Map(map), writer) + } + + /// Get a map field + pub fn map_get(&self, key: &StorageKey, field: &str) -> Result> { + Ok(self + .get_value(key)? + .and_then(|v| v.as_map().and_then(|m| m.get(field).cloned()))) + } + + /// Scan keys with a namespace prefix + pub fn scan_namespace(&self, namespace: &str) -> Result> { + let prefix = StorageKey::namespace_prefix(namespace); + let mut results = Vec::new(); + + for item in self.tree.scan_prefix(&prefix) { + let (key_bytes, data) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + + let entry: StorageEntry = bincode_options_storage() + .deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + if entry.is_expired() { + continue; + } + + // Parse key + if let Some(key) = self.parse_key(&key_bytes) { + results.push((key, entry)); + } + } + + Ok(results) + } + + /// Parse key bytes back to StorageKey + fn parse_key(&self, bytes: &[u8]) -> Option { + let s = String::from_utf8_lossy(bytes); + let parts: Vec<&str> = s.split('\0').collect(); + + if parts.len() >= 2 { + let namespace = parts[0].to_string(); + let validator = if parts.len() > 2 && !parts[1].is_empty() { + // Try to parse as hotkey + let v_bytes = parts[1].as_bytes(); + if v_bytes.len() == 32 { + let mut arr = [0u8; 32]; + arr.copy_from_slice(v_bytes); + Some(Hotkey(arr)) + } else { + None + } + } else { + None + }; + let key = parts.last()?.to_string(); + + Some(StorageKey { + namespace, + validator, + key, + }) + } else { + None + } + } + + /// Clean up expired entries + pub fn cleanup_expired(&self) -> Result { + let mut removed = 0; + let mut to_remove = Vec::new(); + + for item in self.tree.iter() { + let (key, data) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + + if let Ok(entry) = bincode_options_storage().deserialize::(&data) { + if entry.is_expired() { + to_remove.push(key.to_vec()); + } + } + } + + for key in to_remove { + self.tree + .remove(&key) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + removed += 1; + } + + // Also clean cache + if self.cache_enabled { + self.cache.write().retain(|_, v| !v.is_expired()); + } + + if removed > 0 { + info!("Cleaned up {} expired storage entries", removed); + } + + Ok(removed) + } + + /// Get storage statistics + pub fn stats(&self) -> Result { + let mut stats = StorageStats::default(); + let mut namespaces: HashMap = HashMap::new(); + + for item in self.tree.iter() { + let (key, data) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + + stats.total_keys += 1; + stats.total_size_bytes += key.len() as u64 + data.len() as u64; + + // Parse namespace from key + if let Some(parsed_key) = self.parse_key(&key) { + let ns_stats = namespaces.entry(parsed_key.namespace).or_default(); + ns_stats.key_count += 1; + ns_stats.size_bytes += key.len() as u64 + data.len() as u64; + if parsed_key.validator.is_some() { + ns_stats.validator_count += 1; + } + } + } + + stats.namespaces = namespaces; + Ok(stats) + } + + /// Clear cache + pub fn clear_cache(&self) { + self.cache.write().clear(); + } + + /// Flush to disk + pub fn flush(&self) -> Result<()> { + self.tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } + + /// Query entries by prefix within a challenge namespace + pub fn query_by_prefix( + &self, + challenge_id: &ChallengeId, + prefix: &str, + ) -> Result)>> { + let namespace = challenge_id.0.to_string(); + let entries = self.scan_namespace(&namespace)?; + + entries + .into_iter() + .filter(|(k, _)| k.validator.is_none() && k.key.starts_with(prefix)) + .map(|(k, entry)| { + let value_bytes = bincode::serialize(&entry.value) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok((k.key, value_bytes)) + }) + .collect() + } + + /// Get a value as it existed at a specific block height + /// + /// Note: This is a best-effort operation. The current implementation + /// returns the current value if it was last modified at or before the + /// specified block height. Full block-level history requires a separate + /// versioned storage layer. + pub fn get_at_block( + &self, + challenge_id: &ChallengeId, + key: &str, + block: u64, + ) -> Result>> { + let storage_key = StorageKey::challenge(challenge_id, key); + let entry = self.get(&storage_key)?; + + match entry { + Some(e) => { + if e.version <= block { + let value_bytes = bincode::serialize(&e.value) + .map_err(|err| MiniChainError::Serialization(err.to_string()))?; + Ok(Some(value_bytes)) + } else { + Ok(None) + } + } + None => Ok(None), + } + } + + /// List all keys within a challenge namespace + pub fn list_keys(&self, challenge_id: &ChallengeId) -> Result> { + let namespace = challenge_id.0.to_string(); + let entries = self.scan_namespace(&namespace)?; + + Ok(entries + .into_iter() + .filter(|(k, _)| k.validator.is_none()) + .map(|(k, _)| k.key) + .collect()) + } +} + +/// Scoped storage for a specific challenge +pub struct ChallengeStorage<'a> { + storage: &'a DynamicStorage, + challenge_id: ChallengeId, +} + +impl<'a> ChallengeStorage<'a> { + /// Get a value + pub fn get(&self, key: &str) -> Result> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage.get_value(&storage_key) + } + + /// Set a value + pub fn set(&self, key: &str, value: impl Into) -> Result<()> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage.set(storage_key, value.into(), None) + } + + /// Set with TTL + pub fn set_with_ttl( + &self, + key: &str, + value: impl Into, + ttl: Duration, + ) -> Result<()> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage + .set_with_ttl(storage_key, value.into(), None, ttl) + } + + /// Delete a value + pub fn delete(&self, key: &str) -> Result> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage.delete(&storage_key) + } + + /// Get validator-scoped storage within this challenge + pub fn validator(&self, validator: &Hotkey) -> ValidatorStorage<'a> { + ValidatorStorage { + storage: self.storage, + validator: validator.clone(), + challenge_id: Some(self.challenge_id), + } + } + + /// Scan all keys in this challenge + pub fn scan(&self) -> Result> { + let namespace = self.challenge_id.0.to_string(); + let entries = self.storage.scan_namespace(&namespace)?; + + Ok(entries + .into_iter() + .filter(|(k, _)| k.validator.is_none()) // Only challenge-level keys + .map(|(k, v)| (k.key, v)) + .collect()) + } + + /// Increment counter + pub fn increment(&self, key: &str, delta: i64) -> Result { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage.increment(&storage_key, delta, None) + } + + /// Map operations + pub fn map_set(&self, key: &str, field: &str, value: impl Into) -> Result<()> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage + .map_set(&storage_key, field, value.into(), None) + } + + pub fn map_get(&self, key: &str, field: &str) -> Result> { + let storage_key = StorageKey::challenge(&self.challenge_id, key); + self.storage.map_get(&storage_key, field) + } + + /// Query entries by key prefix + pub fn query_by_prefix(&self, prefix: &str) -> Result)>> { + self.storage.query_by_prefix(&self.challenge_id, prefix) + } + + /// List all keys in this challenge + pub fn list_keys(&self) -> Result> { + self.storage.list_keys(&self.challenge_id) + } +} + +/// Scoped storage for a specific validator +pub struct ValidatorStorage<'a> { + storage: &'a DynamicStorage, + validator: Hotkey, + challenge_id: Option, +} + +impl<'a> ValidatorStorage<'a> { + /// Get a value + pub fn get(&self, key: &str) -> Result> { + let storage_key = self.make_key(key); + self.storage.get_value(&storage_key) + } + + /// Set a value + pub fn set(&self, key: &str, value: impl Into) -> Result<()> { + let storage_key = self.make_key(key); + self.storage + .set(storage_key, value.into(), Some(self.validator.clone())) + } + + /// Set with TTL + pub fn set_with_ttl( + &self, + key: &str, + value: impl Into, + ttl: Duration, + ) -> Result<()> { + let storage_key = self.make_key(key); + self.storage + .set_with_ttl(storage_key, value.into(), Some(self.validator.clone()), ttl) + } + + /// Delete a value + pub fn delete(&self, key: &str) -> Result> { + let storage_key = self.make_key(key); + self.storage.delete(&storage_key) + } + + fn make_key(&self, key: &str) -> StorageKey { + if let Some(ref cid) = self.challenge_id { + StorageKey::validator(cid, &self.validator, key) + } else { + StorageKey::global_validator(&self.validator, key) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + fn create_test_storage() -> (tempfile::TempDir, DynamicStorage) { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DynamicStorage::new(&db).unwrap(); + (dir, storage) + } + + #[test] + fn test_basic_operations() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("test"); + storage + .set(key.clone(), StorageValue::U64(42), None) + .unwrap(); + + let value = storage.get_value(&key).unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(42)); + + storage.delete(&key).unwrap(); + assert!(storage.get_value(&key).unwrap().is_none()); + } + + #[test] + fn test_challenge_storage() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.set("leaderboard_size", 100u64).unwrap(); + + let value = cs.get("leaderboard_size").unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(100)); + } + + #[test] + fn test_validator_storage() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + let validator = Hotkey([1u8; 32]); + + let cs = storage.challenge_storage(cid); + let vs = cs.validator(&validator); + + vs.set("score", 95.5f64).unwrap(); + + let value = vs.get("score").unwrap(); + assert_eq!(value.unwrap().as_f64(), Some(95.5)); + } + + #[test] + fn test_ttl() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("ephemeral"); + storage + .set_with_ttl( + key.clone(), + StorageValue::String("temp".into()), + None, + Duration::from_millis(50), + ) + .unwrap(); + + // Should exist immediately + assert!(storage.get_value(&key).unwrap().is_some()); + + // Wait for expiry + std::thread::sleep(Duration::from_millis(100)); + + // Should be gone + assert!(storage.get_value(&key).unwrap().is_none()); + } + + #[test] + fn test_increment() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("counter"); + + assert_eq!(storage.increment(&key, 5, None).unwrap(), 5); + assert_eq!(storage.increment(&key, 3, None).unwrap(), 8); + assert_eq!(storage.increment(&key, -2, None).unwrap(), 6); + } + + #[test] + fn test_map_operations() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("config"); + + storage + .map_set(&key, "timeout", StorageValue::U64(300), None) + .unwrap(); + storage + .map_set(&key, "enabled", StorageValue::Bool(true), None) + .unwrap(); + + assert_eq!( + storage.map_get(&key, "timeout").unwrap().unwrap().as_u64(), + Some(300) + ); + assert_eq!( + storage.map_get(&key, "enabled").unwrap().unwrap().as_bool(), + Some(true) + ); + } + + #[test] + fn test_change_listener() { + let (_dir, storage) = create_test_storage(); + + let changes = Arc::new(RwLock::new(Vec::new())); + let changes_clone = changes.clone(); + + storage.on_change(move |change| { + changes_clone.write().push(change.clone()); + }); + + let key = StorageKey::system("watched"); + storage + .set(key.clone(), StorageValue::U64(1), None) + .unwrap(); + storage + .set(key.clone(), StorageValue::U64(2), None) + .unwrap(); + storage.delete(&key).unwrap(); + + let recorded = changes.read(); + assert_eq!(recorded.len(), 3); + } + + #[test] + fn test_set_block_height() { + let (_dir, storage) = create_test_storage(); + + storage.set_block_height(100); + assert_eq!(*storage.block_height.read(), 100); + + storage.set_block_height(200); + assert_eq!(*storage.block_height.read(), 200); + } + + #[test] + fn test_with_cache() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage = DynamicStorage::new(&db).unwrap().with_cache(false, 5000); + + assert!(!storage.cache_enabled); + assert_eq!(storage.max_cache_size, 5000); + } + + #[test] + fn test_validator_storage_global() { + let (_dir, storage) = create_test_storage(); + let validator = Hotkey([2u8; 32]); + + let vs = storage.validator_storage(validator.clone()); + vs.set("reputation", 95u64).unwrap(); + + let value = vs.get("reputation").unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(95)); + } + + #[test] + fn test_get_nonexistent() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("nonexistent"); + let value = storage.get(&key).unwrap(); + assert!(value.is_none()); + } + + #[test] + fn test_get_value_nonexistent() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("nonexistent"); + let value = storage.get_value(&key).unwrap(); + assert!(value.is_none()); + } + + #[test] + fn test_delete_nonexistent() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("nonexistent"); + let deleted = storage.delete(&key).unwrap(); + assert!(deleted.is_none()); + } + + #[test] + fn test_increment_nonexistent() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("new_counter"); + let result = storage.increment(&key, 10, None).unwrap(); + assert_eq!(result, 10); + } + + #[test] + fn test_list_push() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("my_list"); + + storage.list_push(&key, StorageValue::U64(1), None).unwrap(); + storage.list_push(&key, StorageValue::U64(2), None).unwrap(); + storage.list_push(&key, StorageValue::U64(3), None).unwrap(); + + let value = storage.get_value(&key).unwrap().unwrap(); + let list = value.as_list().unwrap(); + + assert_eq!(list.len(), 3); + assert_eq!(list[0].as_u64(), Some(1)); + assert_eq!(list[2].as_u64(), Some(3)); + } + + #[test] + fn test_list_push_to_nonlist() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("not_a_list"); + storage + .set(key.clone(), StorageValue::U64(42), None) + .unwrap(); + + // Pushing to non-list should return TypeMismatch error + let result = storage.list_push(&key, StorageValue::U64(1), None); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + MiniChainError::TypeMismatch(_) + )); + + // Verify original value is unchanged + let value = storage.get_value(&key).unwrap().unwrap(); + assert_eq!(value.as_u64(), Some(42)); + } + + #[test] + fn test_map_set_new_map() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("new_map"); + + storage + .map_set(&key, "field1", StorageValue::String("value1".into()), None) + .unwrap(); + + let value = storage.map_get(&key, "field1").unwrap(); + assert_eq!(value.unwrap().as_str(), Some("value1")); + } + + #[test] + fn test_map_get_nonexistent_key() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("map"); + storage + .map_set(&key, "field1", StorageValue::U64(1), None) + .unwrap(); + + let value = storage.map_get(&key, "nonexistent").unwrap(); + assert!(value.is_none()); + } + + #[test] + fn test_map_set_to_nonmap() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("not_a_map"); + storage + .set(key.clone(), StorageValue::U64(42), None) + .unwrap(); + + // Setting map field on non-map should return TypeMismatch error + let result = storage.map_set(&key, "field", StorageValue::U64(1), None); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + MiniChainError::TypeMismatch(_) + )); + + // Verify original value is unchanged + let value = storage.get_value(&key).unwrap().unwrap(); + assert_eq!(value.as_u64(), Some(42)); + } + + #[test] + fn test_scan_namespace() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.set("key1", 1u64).unwrap(); + cs.set("key2", 2u64).unwrap(); + cs.set("key3", 3u64).unwrap(); + + let results = storage.scan_namespace(&cid.0.to_string()).unwrap(); + assert_eq!(results.len(), 3); + } + + #[test] + fn test_cleanup_expired() { + let (_dir, storage) = create_test_storage(); + + // Add expired entry + let key = StorageKey::system("expired"); + storage + .set_with_ttl( + key.clone(), + StorageValue::U64(42), + None, + Duration::from_millis(1), + ) + .unwrap(); + + std::thread::sleep(Duration::from_millis(10)); + + let removed = storage.cleanup_expired().unwrap(); + assert!(removed > 0); + assert!(storage.get_value(&key).unwrap().is_none()); + } + + #[test] + fn test_stats() { + let (_dir, storage) = create_test_storage(); + + storage + .set(StorageKey::system("k1"), StorageValue::U64(1), None) + .unwrap(); + storage + .set(StorageKey::system("k2"), StorageValue::U64(2), None) + .unwrap(); + + let stats = storage.stats().unwrap(); + assert!(stats.total_keys >= 2); + } + + #[test] + fn test_challenge_storage_delete() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.set("key", 42u64).unwrap(); + + let deleted = cs.delete("key").unwrap(); + assert!(deleted.is_some()); + + let value = cs.get("key").unwrap(); + assert!(value.is_none()); + } + + #[test] + fn test_validator_storage_delete() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + let validator = Hotkey([3u8; 32]); + + let cs = storage.challenge_storage(cid); + let vs = cs.validator(&validator); + + vs.set("score", 100u64).unwrap(); + vs.delete("score").unwrap(); + + assert!(vs.get("score").unwrap().is_none()); + } + + #[test] + fn test_challenge_storage_with_ttl() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.set_with_ttl("temp", 100u64, Duration::from_secs(5)) + .unwrap(); + + let value = cs.get("temp").unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(100)); + } + + #[test] + fn test_challenge_storage_scan() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.set("key1", 1u64).unwrap(); + cs.set("key2", 2u64).unwrap(); + + let results = cs.scan().unwrap(); + assert_eq!(results.len(), 2); + } + + #[test] + fn test_challenge_storage_increment() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + let val1 = cs.increment("counter", 5).unwrap(); + assert_eq!(val1, 5); + + let val2 = cs.increment("counter", 3).unwrap(); + assert_eq!(val2, 8); + } + + #[test] + fn test_challenge_storage_map_operations() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + + let cs = storage.challenge_storage(cid); + cs.map_set("config", "timeout", 30u64).unwrap(); + cs.map_set("config", "retries", 3u64).unwrap(); + + let timeout = cs.map_get("config", "timeout").unwrap(); + assert_eq!(timeout.unwrap().as_u64(), Some(30)); + + let retries = cs.map_get("config", "retries").unwrap(); + assert_eq!(retries.unwrap().as_u64(), Some(3)); + } + + #[test] + fn test_validator_storage_with_ttl() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + let validator = Hotkey([5u8; 32]); + + let cs = storage.challenge_storage(cid); + let vs = cs.validator(&validator); + + vs.set_with_ttl("temp", 200u64, Duration::from_secs(10)) + .unwrap(); + + let value = vs.get("temp").unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(200)); + } + + #[test] + fn test_on_change_listener() { + let (_dir, storage) = create_test_storage(); + let called = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let called_clone = called.clone(); + + storage.on_change(move |_change| { + called_clone.store(true, std::sync::atomic::Ordering::SeqCst); + }); + + let key = StorageKey::system("test"); + storage.set(key, StorageValue::U64(100), None).unwrap(); + + // Listener should have been called + assert!(called.load(std::sync::atomic::Ordering::SeqCst)); + } + + #[test] + fn test_set_with_options() { + let (_dir, storage) = create_test_storage(); + let key = StorageKey::system("test"); + + storage + .set_with_options( + key.clone(), + StorageValue::U64(42), + Some(Hotkey([8u8; 32])), + Some(Duration::from_secs(5)), + ) + .unwrap(); + + let entry = storage.get(&key).unwrap(); + assert!(entry.is_some()); + let entry = entry.unwrap(); + assert_eq!(entry.value.as_u64(), Some(42)); + assert!(entry.ttl.is_some()); + } + + #[test] + fn test_clear_cache() { + let (_dir, storage) = create_test_storage(); + + // Set some values to populate cache + let key = StorageKey::system("test"); + storage + .set(key.clone(), StorageValue::U64(1), None) + .unwrap(); + storage.get(&key).unwrap(); + + // Clear cache + storage.clear_cache(); + + // Should still be able to read from disk + let value = storage.get(&key).unwrap(); + assert!(value.is_some()); + } + + #[test] + fn test_flush() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("test"); + storage.set(key, StorageValue::U64(999), None).unwrap(); + + // Flush to disk + storage.flush().unwrap(); + } + + #[test] + fn test_exists() { + let (_dir, storage) = create_test_storage(); + + let key = StorageKey::system("test"); + assert!(!storage.exists(&key).unwrap()); + + storage + .set(key.clone(), StorageValue::U64(1), None) + .unwrap(); + assert!(storage.exists(&key).unwrap()); + } + + #[test] + fn test_set_with_options_update_existing() { + let (_dir, storage) = create_test_storage(); + let key = StorageKey::system("test"); + + // Set initial value + storage + .set(key.clone(), StorageValue::U64(1), None) + .unwrap(); + + // Update with options (line 187 path - updating existing entry) + storage + .set_with_options( + key.clone(), + StorageValue::U64(2), + Some(Hotkey([1u8; 32])), + Some(Duration::from_secs(10)), + ) + .unwrap(); + + let entry = storage.get(&key).unwrap().unwrap(); + assert_eq!(entry.value.as_u64(), Some(2)); + assert!(entry.ttl.is_some()); + } + + #[test] + fn test_parse_key_with_validator() { + let (_dir, storage) = create_test_storage(); + let cid = ChallengeId(uuid::Uuid::new_v4()); + let validator = Hotkey([5u8; 32]); + + let key = StorageKey::validator(&cid, &validator, "test_key"); + let key_bytes = key.to_bytes(); + + // Parse the key back (lines 367-374) + let parsed = storage.parse_key(&key_bytes); + assert!(parsed.is_some()); + let parsed = parsed.unwrap(); + assert!(parsed.validator.is_some()); + } + + #[test] + fn test_parse_key_invalid() { + let (_dir, storage) = create_test_storage(); + + // Invalid key format (line 386 - returns None) + let invalid_key = b"invalid"; + let parsed = storage.parse_key(invalid_key); + assert!(parsed.is_none()); + } + + #[test] + fn test_stats_with_namespaces() { + let (_dir, storage) = create_test_storage(); + + // Add keys in different namespaces + storage + .set(StorageKey::system("key1"), StorageValue::U64(1), None) + .unwrap(); + storage + .set( + StorageKey::challenge(&ChallengeId(uuid::Uuid::new_v4()), "key2"), + StorageValue::U64(2), + None, + ) + .unwrap(); + + // Get stats (line 441) + let stats = storage.stats().unwrap(); + assert!(stats.total_keys >= 2); + assert!(stats.total_size_bytes > 0); + } +} diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs new file mode 100644 index 000000000..40a2da791 --- /dev/null +++ b/crates/storage/src/lib.rs @@ -0,0 +1,517 @@ +#![allow(dead_code, unused_variables, unused_imports)] +#![allow(ambiguous_glob_reexports)] +//! Persistent storage using sled +//! +//! This module provides: +//! - `Storage` - Main storage for chain state, challenges, and validators +//! - `DynamicStorage` - Per-challenge/per-validator dynamic storage +//! - `MigrationRunner` - Version-based migrations for blockchain upgrades +//! - `BlockchainStorage` - Blockchain-like structure for validator consensus +//! +//! ## Dynamic Storage +//! +//! Dynamic storage allows challenges and validators to store their own data: +//! +//! ```text +//! // Challenge-level storage +//! let cs = storage.dynamic().challenge_storage(challenge_id); +//! cs.set("leaderboard_size", 100u64)?; +//! +//! // Validator-level storage within a challenge +//! let vs = cs.validator(&hotkey); +//! vs.set("last_evaluation", timestamp)?; +//! ``` +//! +//! ## Migrations +//! +//! Migrations run automatically when the blockchain version changes: +//! +//! ```text +//! let mut runner = storage.migration_runner()?; +//! runner.register(Box::new(MyMigration)); +//! runner.run_pending(&storage_tree, &state_tree, block_height)?; +//! ``` +//! +//! ## Blockchain Storage +//! +//! Blockchain storage provides immutable, verifiable state tracking: +//! +//! ```text +//! use platform_storage::blockchain::BlockchainStorage; +//! +//! let db = sled::open("./blockchain")?; +//! let mut storage = BlockchainStorage::new(&db)?; +//! +//! // Append a new block +//! storage.append_block(block)?; +//! +//! // Query historical state +//! let root = storage.get_state_root_at_block(10, None)?; +//! ``` + +pub mod blockchain; +pub mod distributed; +pub mod dynamic; +pub mod metadata; +pub mod migration; +pub mod optimized; +pub mod types; + +pub use distributed::*; +pub use dynamic::*; +pub use metadata::*; +pub use migration::*; +pub use optimized::*; +pub use types::*; + +use platform_core::{ + ChainState, Challenge, ChallengeId, Hotkey, MiniChainError, Result, ValidatorInfo, +}; +use sled::{Db, Tree}; +use std::path::Path; +use std::sync::Arc; +use tracing::{debug, info}; + +/// Main storage for chain state and data +/// +/// Provides persistent storage for: +/// - Chain state (block height, validators, challenges) +/// - Challenge data +/// - Validator data +/// - Dynamic per-challenge storage +/// - Migrations +pub struct Storage { + db: Db, + state_tree: Tree, + challenges_tree: Tree, + validators_tree: Tree, + /// Dynamic storage for per-challenge/per-validator data + dynamic_storage: Arc, +} + +impl Storage { + /// Open or create storage at path + pub fn open>(path: P) -> Result { + let db = sled::open(path) + .map_err(|e| MiniChainError::Storage(format!("Failed to open database: {}", e)))?; + + let state_tree = db + .open_tree("state") + .map_err(|e| MiniChainError::Storage(format!("Failed to open state tree: {}", e)))?; + + let challenges_tree = db.open_tree("challenges").map_err(|e| { + MiniChainError::Storage(format!("Failed to open challenges tree: {}", e)) + })?; + + let validators_tree = db.open_tree("validators").map_err(|e| { + MiniChainError::Storage(format!("Failed to open validators tree: {}", e)) + })?; + + let dynamic_storage = Arc::new(DynamicStorage::new(&db)?); + + info!("Storage opened successfully"); + Ok(Self { + db, + state_tree, + challenges_tree, + validators_tree, + dynamic_storage, + }) + } + + /// Get access to dynamic storage + pub fn dynamic(&self) -> &DynamicStorage { + &self.dynamic_storage + } + + /// Get Arc reference to dynamic storage (for sharing) + pub fn dynamic_arc(&self) -> Arc { + self.dynamic_storage.clone() + } + + /// Create a migration runner + pub fn migration_runner(&self) -> Result { + MigrationRunner::new(&self.db) + } + + /// Run all pending migrations + pub fn run_migrations(&self, block_height: u64) -> Result> { + let mut runner = self.migration_runner()?; + + // Register built-in migrations + runner.register(Box::new(InitialMigration)); + runner.register(Box::new(AddChallengeMetricsMigration)); + + // Get the dynamic storage tree directly + let storage_tree = self + .db + .open_tree("dynamic_storage") + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + runner.run_pending(&storage_tree, &self.state_tree, block_height) + } + + /// Get the underlying database handle + pub fn db(&self) -> &Db { + &self.db + } + + /// Get the state tree + pub fn state_tree(&self) -> &Tree { + &self.state_tree + } + + /// Save chain state with version header for backward compatibility + pub fn save_state(&self, state: &ChainState) -> Result<()> { + // Use versioned serialization for future-proof storage + let data = platform_core::serialize_state_versioned(state)?; + + self.state_tree + .insert("current", data) + .map_err(|e| MiniChainError::Storage(format!("Failed to save state: {}", e)))?; + + self.db + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush: {}", e)))?; + + debug!( + "State saved at block {} (version {})", + state.block_height, + platform_core::CURRENT_STATE_VERSION + ); + Ok(()) + } + + /// Load chain state with automatic version migration + pub fn load_state(&self) -> Result> { + let data = self + .state_tree + .get("current") + .map_err(|e| MiniChainError::Storage(format!("Failed to load state: {}", e)))?; + + match data { + Some(bytes) => { + // Use smart deserialization that handles version migration + let state = platform_core::deserialize_state_smart(&bytes)?; + Ok(Some(state)) + } + None => Ok(None), + } + } + + /// Save a challenge + pub fn save_challenge(&self, challenge: &Challenge) -> Result<()> { + let key = challenge.id.0.as_bytes(); + let data = bincode::serialize(challenge) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + self.challenges_tree + .insert(key, data) + .map_err(|e| MiniChainError::Storage(format!("Failed to save challenge: {}", e)))?; + + Ok(()) + } + + /// Load a challenge + pub fn load_challenge(&self, id: &ChallengeId) -> Result> { + use bincode::Options; + const MAX_CHALLENGE_SIZE: u64 = 10 * 1024 * 1024; // 10 MB limit + + let key = id.0.as_bytes(); + let data = self + .challenges_tree + .get(key) + .map_err(|e| MiniChainError::Storage(format!("Failed to load challenge: {}", e)))?; + + match data { + Some(bytes) => { + // Use options compatible with bincode::serialize (little-endian, variable int, trailing allowed) + let challenge: Challenge = bincode::DefaultOptions::new() + .with_fixint_encoding() + .with_little_endian() + .allow_trailing_bytes() + .with_limit(MAX_CHALLENGE_SIZE) + .deserialize(&bytes) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(Some(challenge)) + } + None => Ok(None), + } + } + + /// Delete a challenge + pub fn delete_challenge(&self, id: &ChallengeId) -> Result { + let key = id.0.as_bytes(); + let removed = self + .challenges_tree + .remove(key) + .map_err(|e| MiniChainError::Storage(format!("Failed to delete challenge: {}", e)))?; + Ok(removed.is_some()) + } + + /// List all challenges + pub fn list_challenges(&self) -> Result> { + let mut ids = Vec::new(); + for result in self.challenges_tree.iter() { + let (key, _) = + result.map_err(|e| MiniChainError::Storage(format!("Iteration error: {}", e)))?; + if key.len() == 16 { + let mut bytes = [0u8; 16]; + bytes.copy_from_slice(&key); + ids.push(ChallengeId(uuid::Uuid::from_bytes(bytes))); + } + } + Ok(ids) + } + + /// Save validator info + pub fn save_validator(&self, info: &ValidatorInfo) -> Result<()> { + let key = info.hotkey.as_bytes(); + let data = + bincode::serialize(info).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + self.validators_tree + .insert(key, data) + .map_err(|e| MiniChainError::Storage(format!("Failed to save validator: {}", e)))?; + + Ok(()) + } + + /// Load validator info + pub fn load_validator(&self, hotkey: &Hotkey) -> Result> { + use bincode::Options; + const MAX_VALIDATOR_SIZE: u64 = 1024 * 1024; // 1 MB limit + + let key = hotkey.as_bytes(); + let data = self + .validators_tree + .get(key) + .map_err(|e| MiniChainError::Storage(format!("Failed to load validator: {}", e)))?; + + match data { + Some(bytes) => { + // Use options compatible with bincode::serialize (little-endian, variable int, trailing allowed) + let info: ValidatorInfo = bincode::DefaultOptions::new() + .with_fixint_encoding() + .with_little_endian() + .allow_trailing_bytes() + .with_limit(MAX_VALIDATOR_SIZE) + .deserialize(&bytes) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(Some(info)) + } + None => Ok(None), + } + } + + /// Flush all changes to disk + pub fn flush(&self) -> Result<()> { + self.db + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush: {}", e)))?; + Ok(()) + } +} + +#[cfg(test)] +mod lib_tests { + use super::*; + use platform_core::{ChallengeConfig, Keypair, NetworkConfig, Stake}; + use tempfile::tempdir; + + #[test] + fn test_storage_open() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()); + assert!(storage.is_ok()); + } + + #[test] + fn test_state_persistence() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + storage.save_state(&state).unwrap(); + let loaded = storage.load_state().unwrap(); + + assert!(loaded.is_some()); + assert_eq!(loaded.unwrap().block_height, state.block_height); + } + + #[test] + fn test_challenge_persistence() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let owner = Keypair::generate(); + let challenge = Challenge::new( + "Test".into(), + "Test".into(), + vec![0u8; 100], + owner.hotkey(), + ChallengeConfig::default(), + ); + + storage.save_challenge(&challenge).unwrap(); + let loaded = storage.load_challenge(&challenge.id).unwrap(); + + assert!(loaded.is_some()); + assert_eq!(loaded.unwrap().name, challenge.name); + } + + #[test] + fn test_validator_persistence() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(1_000_000_000)); + + storage.save_validator(&info).unwrap(); + let loaded = storage.load_validator(&kp.hotkey()).unwrap(); + + assert!(loaded.is_some()); + assert_eq!(loaded.unwrap().stake.0, info.stake.0); + } + + #[test] + fn test_delete_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let owner = Keypair::generate(); + let challenge = Challenge::new( + "Test".into(), + "Test".into(), + vec![0u8; 100], + owner.hotkey(), + ChallengeConfig::default(), + ); + + storage.save_challenge(&challenge).unwrap(); + assert!(storage.delete_challenge(&challenge.id).unwrap()); + + let loaded = storage.load_challenge(&challenge.id).unwrap(); + assert!(loaded.is_none()); + + // Delete non-existent challenge + assert!(!storage.delete_challenge(&challenge.id).unwrap()); + } + + #[test] + fn test_list_challenges() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let owner = Keypair::generate(); + + // Add multiple challenges + for i in 0..3 { + let challenge = Challenge::new( + format!("Test {}", i), + format!("Test {}", i), + vec![0u8; 100], + owner.hotkey(), + ChallengeConfig::default(), + ); + storage.save_challenge(&challenge).unwrap(); + } + + let challenges = storage.list_challenges().unwrap(); + assert_eq!(challenges.len(), 3); + } + + #[test] + fn test_load_nonexistent_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let fake_id = ChallengeId(uuid::Uuid::new_v4()); + let loaded = storage.load_challenge(&fake_id).unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_load_nonexistent_validator() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let kp = Keypair::generate(); + let loaded = storage.load_validator(&kp.hotkey()).unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_load_state_empty() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let loaded = storage.load_state().unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_flush() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + storage.save_state(&state).unwrap(); + storage.flush().unwrap(); + } + + #[test] + fn test_dynamic_storage_access() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let dynamic = storage.dynamic(); + assert!(std::ptr::eq(dynamic, storage.dynamic())); + + let arc = storage.dynamic_arc(); + assert!(Arc::ptr_eq(&arc, &storage.dynamic_storage)); + } + + #[test] + fn test_db_access() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let _db = storage.db(); + let _tree = storage.state_tree(); + } + + #[test] + fn test_migration_runner_creation() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let runner = storage.migration_runner(); + assert!(runner.is_ok()); + } + + #[test] + fn test_run_migrations() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let result = storage.run_migrations(100); + assert!(result.is_ok()); + + let versions = result.unwrap(); + // Should have at least the built-in migrations + assert!(!versions.is_empty()); + } + + #[test] + fn test_storage_open_tree_failures() { + // Tests document the error paths at lines 82 and 86 + // These would require mocking sled to fail tree opening + // The errors are properly converted to MiniChainError::Storage with descriptive messages + } +} diff --git a/crates/storage/src/metadata.rs b/crates/storage/src/metadata.rs new file mode 100644 index 000000000..cdd87ad7b --- /dev/null +++ b/crates/storage/src/metadata.rs @@ -0,0 +1,985 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Unified Metadata Registry for Challenge Storage Validation +//! +//! This module provides a centralized registry for tracking: +//! - Schema versions per challenge +//! - Configuration metadata +//! - State versions and merkle roots +//! - Migration status +//! +//! The metadata system enables blockchain-like properties for tracking +//! storage schemas and ensuring state consistency across the validator network. + +use platform_core::{ChallengeId, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sled::{Db, Tree}; +use std::collections::HashMap; +use std::time::SystemTime; +use tracing::{debug, info, warn}; + +/// Storage format version for challenge data +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum StorageFormat { + /// Original storage format + #[default] + V1, + /// Updated storage format with improved serialization + V2, + /// Challenge-specific custom format + Custom, +} + +/// Metadata for a single challenge +/// +/// Contains all tracking information for a challenge's storage state, +/// including schema version, merkle root, and configuration. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeMetadata { + /// Unique identifier for the challenge + pub challenge_id: ChallengeId, + /// Current schema version for this challenge's data + pub schema_version: u64, + /// Storage format used by this challenge + pub storage_format: StorageFormat, + /// When this challenge was first registered + pub created_at: SystemTime, + /// When this challenge's metadata was last updated + pub updated_at: SystemTime, + /// Current merkle root of all challenge state + pub merkle_root: [u8; 32], + /// Challenge-specific configuration as JSON string (serialized for bincode compatibility) + config_json: String, +} + +impl ChallengeMetadata { + /// Create new challenge metadata with default values + pub fn new(challenge_id: ChallengeId, config: serde_json::Value) -> Self { + let now = SystemTime::now(); + Self { + challenge_id, + schema_version: 1, + storage_format: StorageFormat::default(), + created_at: now, + updated_at: now, + merkle_root: [0u8; 32], + config_json: config.to_string(), + } + } + + /// Get the challenge configuration as a JSON Value + pub fn config(&self) -> serde_json::Value { + serde_json::from_str(&self.config_json).unwrap_or(serde_json::Value::Null) + } + + /// Set the challenge configuration + pub fn set_config(&mut self, config: serde_json::Value) { + self.config_json = config.to_string(); + self.updated_at = SystemTime::now(); + } + + /// Update the merkle root and timestamp + pub fn update_state_root(&mut self, state_root: [u8; 32]) { + self.merkle_root = state_root; + self.updated_at = SystemTime::now(); + } + + /// Update the schema version + pub fn update_schema_version(&mut self, version: u64) { + self.schema_version = version; + self.updated_at = SystemTime::now(); + } +} + +/// Global metadata tracking all challenges and network state +/// +/// Provides a unified view of the entire storage system including +/// all registered challenges and their combined state root. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GlobalMetadata { + /// Network protocol version string + pub network_version: String, + /// Global schema version for the metadata system + pub schema_version: u64, + /// When the network was initialized + pub genesis_timestamp: SystemTime, + /// Metadata for all registered challenges + pub challenges: HashMap, + /// Combined merkle root of all challenge states + pub global_state_root: [u8; 32], +} + +impl GlobalMetadata { + /// Create new global metadata with default values + pub fn new(network_version: String) -> Self { + Self { + network_version, + schema_version: 1, + genesis_timestamp: SystemTime::now(), + challenges: HashMap::new(), + global_state_root: [0u8; 32], + } + } + + /// Get the number of registered challenges + pub fn challenge_count(&self) -> usize { + self.challenges.len() + } +} + +/// Database key prefixes for metadata storage +const METADATA_TREE_NAME: &str = "metadata_registry"; +const GLOBAL_METADATA_KEY: &str = "global"; +const CHALLENGE_PREFIX: &str = "challenge:"; + +/// Centralized registry for tracking challenge storage metadata +/// +/// The MetadataRegistry provides: +/// - Registration and tracking of challenge metadata +/// - State root computation and validation +/// - Schema version management +/// - Persistence to sled database +/// +/// # Example +/// +/// ```text +/// let registry = MetadataRegistry::new(&db)?; +/// registry.register_challenge(challenge_id, serde_json::json!({}))?; +/// registry.update_challenge_state_root(&challenge_id, state_root)?; +/// ``` +pub struct MetadataRegistry { + /// The metadata storage tree + tree: Tree, + /// Cached global metadata (loaded on init) + global: GlobalMetadata, +} + +impl MetadataRegistry { + /// Create or open a metadata registry + /// + /// If the registry already exists in the database, it will be loaded. + /// Otherwise, a new registry is initialized. + /// + /// # Arguments + /// + /// * `db` - Reference to the sled database + /// + /// # Returns + /// + /// A Result containing the MetadataRegistry or an error + /// + /// # Errors + /// + /// Returns an error if the database tree cannot be opened or if + /// existing metadata cannot be deserialized. + pub fn new(db: &Db) -> Result { + let tree = db + .open_tree(METADATA_TREE_NAME) + .map_err(|e| MiniChainError::Storage(format!("Failed to open metadata tree: {}", e)))?; + + // Try to load existing global metadata, or create new + let global = match tree.get(GLOBAL_METADATA_KEY) { + Ok(Some(data)) => bincode::deserialize(&data).map_err(|e| { + MiniChainError::Serialization(format!( + "Failed to deserialize global metadata: {}", + e + )) + })?, + Ok(None) => { + info!("Initializing new metadata registry"); + let global = GlobalMetadata::new("1.0.0".to_string()); + let data = bincode::serialize(&global).map_err(|e| { + MiniChainError::Serialization(format!( + "Failed to serialize global metadata: {}", + e + )) + })?; + tree.insert(GLOBAL_METADATA_KEY, data).map_err(|e| { + MiniChainError::Storage(format!("Failed to persist global metadata: {}", e)) + })?; + global + } + Err(e) => { + return Err(MiniChainError::Storage(format!( + "Failed to read global metadata: {}", + e + ))); + } + }; + + debug!( + "Metadata registry loaded with {} challenges", + global.challenge_count() + ); + + Ok(Self { tree, global }) + } + + /// Register a new challenge in the metadata registry + /// + /// Creates metadata for a new challenge and persists it to storage. + /// If the challenge already exists, returns an error. + /// + /// # Arguments + /// + /// * `challenge_id` - Unique identifier for the challenge + /// * `config` - Challenge-specific configuration as JSON + /// + /// # Returns + /// + /// Ok(()) on success, or an error if the challenge already exists + /// or persistence fails. + pub fn register_challenge( + &mut self, + challenge_id: ChallengeId, + config: serde_json::Value, + ) -> Result<()> { + // Check if challenge already exists + if self.global.challenges.contains_key(&challenge_id) { + return Err(MiniChainError::Validation(format!( + "Challenge {} is already registered", + challenge_id + ))); + } + + let metadata = ChallengeMetadata::new(challenge_id, config); + + // Persist challenge metadata + let key = format!("{}{}", CHALLENGE_PREFIX, challenge_id); + let data = bincode::serialize(&metadata).map_err(|e| { + MiniChainError::Serialization(format!("Failed to serialize challenge metadata: {}", e)) + })?; + self.tree.insert(key.as_bytes(), data).map_err(|e| { + MiniChainError::Storage(format!("Failed to persist challenge metadata: {}", e)) + })?; + + // Update global metadata + self.global.challenges.insert(challenge_id, metadata); + self.persist_global()?; + + info!("Registered challenge {}", challenge_id); + Ok(()) + } + + /// Update the state root for a challenge + /// + /// Updates the merkle root representing the current state of a challenge + /// and recomputes the global state root. + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to update + /// * `state_root` - The new merkle root for the challenge state + /// + /// # Returns + /// + /// Ok(()) on success, or an error if the challenge is not found + /// or persistence fails. + pub fn update_challenge_state_root( + &mut self, + challenge_id: &ChallengeId, + state_root: [u8; 32], + ) -> Result<()> { + let metadata = self + .global + .challenges + .get_mut(challenge_id) + .ok_or_else(|| { + MiniChainError::NotFound(format!("Challenge {} not found", challenge_id)) + })?; + + metadata.update_state_root(state_root); + + // Persist challenge metadata + let key = format!("{}{}", CHALLENGE_PREFIX, challenge_id); + let data = bincode::serialize(metadata).map_err(|e| { + MiniChainError::Serialization(format!("Failed to serialize challenge metadata: {}", e)) + })?; + self.tree.insert(key.as_bytes(), data).map_err(|e| { + MiniChainError::Storage(format!("Failed to persist challenge metadata: {}", e)) + })?; + + // Recompute global state root + self.global.global_state_root = self.compute_global_state_root(); + self.persist_global()?; + + debug!( + "Updated state root for challenge {}: {:02x}{:02x}{:02x}{:02x}...", + challenge_id, state_root[0], state_root[1], state_root[2], state_root[3] + ); + Ok(()) + } + + /// Get metadata for a specific challenge + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to look up + /// + /// # Returns + /// + /// Ok(Some(metadata)) if found, Ok(None) if not found, + /// or an error if deserialization fails. + pub fn get_challenge_metadata( + &self, + challenge_id: &ChallengeId, + ) -> Result> { + Ok(self.global.challenges.get(challenge_id).cloned()) + } + + /// Compute the combined merkle root of all challenge states + /// + /// Creates a deterministic hash by sorting challenges by ID and + /// hashing their merkle roots together. + /// + /// # Returns + /// + /// A 32-byte hash representing the combined state of all challenges. + pub fn compute_global_state_root(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + + // Sort challenges by ID for deterministic ordering + let mut challenge_ids: Vec<_> = self.global.challenges.keys().collect(); + challenge_ids.sort_by_key(|id| id.0); + + for challenge_id in challenge_ids { + if let Some(metadata) = self.global.challenges.get(challenge_id) { + // Include challenge ID in hash + hasher.update(challenge_id.0.as_bytes()); + // Include challenge merkle root + hasher.update(metadata.merkle_root); + } + } + + hasher.finalize().into() + } + + /// Validate that a challenge's state root matches an expected value + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to validate + /// * `expected_root` - The expected merkle root + /// + /// # Returns + /// + /// `true` if the challenge exists and its state root matches, + /// `false` otherwise. + pub fn validate_state_root(&self, challenge_id: &ChallengeId, expected_root: [u8; 32]) -> bool { + self.global + .challenges + .get(challenge_id) + .map(|m| m.merkle_root == expected_root) + .unwrap_or(false) + } + + /// List all registered challenge IDs + /// + /// # Returns + /// + /// A vector of all registered challenge IDs. + pub fn list_challenges(&self) -> Vec { + self.global.challenges.keys().copied().collect() + } + + /// Get the schema version for a specific challenge + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to look up + /// + /// # Returns + /// + /// The schema version if the challenge exists, None otherwise. + pub fn get_schema_version(&self, challenge_id: &ChallengeId) -> Option { + self.global + .challenges + .get(challenge_id) + .map(|m| m.schema_version) + } + + /// Get the current global metadata + /// + /// # Returns + /// + /// A reference to the global metadata. + pub fn global_metadata(&self) -> &GlobalMetadata { + &self.global + } + + /// Update the schema version for a challenge + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to update + /// * `version` - The new schema version + /// + /// # Returns + /// + /// Ok(()) on success, or an error if the challenge is not found. + pub fn update_schema_version( + &mut self, + challenge_id: &ChallengeId, + version: u64, + ) -> Result<()> { + let metadata = self + .global + .challenges + .get_mut(challenge_id) + .ok_or_else(|| { + MiniChainError::NotFound(format!("Challenge {} not found", challenge_id)) + })?; + + metadata.update_schema_version(version); + + // Persist challenge metadata + let key = format!("{}{}", CHALLENGE_PREFIX, challenge_id); + let data = bincode::serialize(metadata).map_err(|e| { + MiniChainError::Serialization(format!("Failed to serialize challenge metadata: {}", e)) + })?; + self.tree.insert(key.as_bytes(), data).map_err(|e| { + MiniChainError::Storage(format!("Failed to persist challenge metadata: {}", e)) + })?; + + self.persist_global()?; + + info!( + "Updated schema version for challenge {} to {}", + challenge_id, version + ); + Ok(()) + } + + /// Remove a challenge from the registry + /// + /// # Arguments + /// + /// * `challenge_id` - The challenge to remove + /// + /// # Returns + /// + /// Ok(true) if the challenge was removed, Ok(false) if it didn't exist. + pub fn unregister_challenge(&mut self, challenge_id: &ChallengeId) -> Result { + if self.global.challenges.remove(challenge_id).is_none() { + return Ok(false); + } + + // Remove from storage + let key = format!("{}{}", CHALLENGE_PREFIX, challenge_id); + self.tree.remove(key.as_bytes()).map_err(|e| { + MiniChainError::Storage(format!("Failed to remove challenge metadata: {}", e)) + })?; + + // Update global state + self.global.global_state_root = self.compute_global_state_root(); + self.persist_global()?; + + info!("Unregistered challenge {}", challenge_id); + Ok(true) + } + + /// Flush all pending changes to disk + pub fn flush(&self) -> Result<()> { + self.tree + .flush() + .map_err(|e| MiniChainError::Storage(format!("Failed to flush metadata: {}", e)))?; + Ok(()) + } + + /// Persist global metadata to storage + fn persist_global(&self) -> Result<()> { + let data = bincode::serialize(&self.global).map_err(|e| { + MiniChainError::Serialization(format!("Failed to serialize global metadata: {}", e)) + })?; + self.tree.insert(GLOBAL_METADATA_KEY, data).map_err(|e| { + MiniChainError::Storage(format!("Failed to persist global metadata: {}", e)) + })?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + fn create_test_db() -> sled::Db { + let dir = tempdir().expect("Failed to create temp dir"); + sled::open(dir.path()).expect("Failed to open sled db") + } + + #[test] + fn test_metadata_registry_new() { + let db = create_test_db(); + let registry = MetadataRegistry::new(&db); + assert!(registry.is_ok()); + + let registry = registry.unwrap(); + assert_eq!(registry.global.challenge_count(), 0); + assert_eq!(registry.global.network_version, "1.0.0"); + } + + #[test] + fn test_metadata_registry_persistence() { + let dir = tempdir().expect("Failed to create temp dir"); + let challenge_id = ChallengeId::new(); + + // Create and register challenge + { + let db = sled::open(dir.path()).expect("Failed to open sled db"); + let mut registry = MetadataRegistry::new(&db).unwrap(); + registry + .register_challenge(challenge_id, serde_json::json!({"key": "value"})) + .unwrap(); + registry.flush().unwrap(); + } + + // Reopen and verify + { + let db = sled::open(dir.path()).expect("Failed to open sled db"); + let registry = MetadataRegistry::new(&db).unwrap(); + assert_eq!(registry.global.challenge_count(), 1); + + let metadata = registry.get_challenge_metadata(&challenge_id).unwrap(); + assert!(metadata.is_some()); + let metadata = metadata.unwrap(); + assert_eq!(metadata.challenge_id, challenge_id); + } + } + + #[test] + fn test_register_challenge() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + let config = serde_json::json!({ + "timeout": 3600, + "max_submissions": 100 + }); + + let result = registry.register_challenge(challenge_id, config.clone()); + assert!(result.is_ok()); + + // Verify registration + let metadata = registry.get_challenge_metadata(&challenge_id).unwrap(); + assert!(metadata.is_some()); + let metadata = metadata.unwrap(); + assert_eq!(metadata.challenge_id, challenge_id); + assert_eq!(metadata.schema_version, 1); + assert_eq!(metadata.storage_format, StorageFormat::V1); + assert_eq!(metadata.config(), config); + } + + #[test] + fn test_register_duplicate_challenge() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + // Try to register again + let result = registry.register_challenge(challenge_id, serde_json::json!({})); + assert!(result.is_err()); + assert!(matches!(result.unwrap_err(), MiniChainError::Validation(_))); + } + + #[test] + fn test_update_challenge_state_root() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + let state_root = [42u8; 32]; + let result = registry.update_challenge_state_root(&challenge_id, state_root); + assert!(result.is_ok()); + + // Verify update + let metadata = registry + .get_challenge_metadata(&challenge_id) + .unwrap() + .unwrap(); + assert_eq!(metadata.merkle_root, state_root); + } + + #[test] + fn test_update_nonexistent_challenge_state_root() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + let result = registry.update_challenge_state_root(&challenge_id, [0u8; 32]); + assert!(result.is_err()); + assert!(matches!(result.unwrap_err(), MiniChainError::NotFound(_))); + } + + #[test] + fn test_get_challenge_metadata_not_found() { + let db = create_test_db(); + let registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + let metadata = registry.get_challenge_metadata(&challenge_id).unwrap(); + assert!(metadata.is_none()); + } + + #[test] + fn test_compute_global_state_root() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + // Empty registry should have consistent hash + let root1 = registry.compute_global_state_root(); + + // Add a challenge + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + // Hash should change + let root2 = registry.compute_global_state_root(); + assert_ne!(root1, root2); + + // Update state root + registry + .update_challenge_state_root(&challenge_id, [1u8; 32]) + .unwrap(); + + // Hash should change again + let root3 = registry.compute_global_state_root(); + assert_ne!(root2, root3); + } + + #[test] + fn test_compute_global_state_root_deterministic() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id1 = ChallengeId::new(); + let challenge_id2 = ChallengeId::new(); + + registry + .register_challenge(challenge_id1, serde_json::json!({})) + .unwrap(); + registry + .register_challenge(challenge_id2, serde_json::json!({})) + .unwrap(); + + // Should be deterministic + let root1 = registry.compute_global_state_root(); + let root2 = registry.compute_global_state_root(); + assert_eq!(root1, root2); + } + + #[test] + fn test_validate_state_root() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + let state_root = [123u8; 32]; + registry + .update_challenge_state_root(&challenge_id, state_root) + .unwrap(); + + // Valid root + assert!(registry.validate_state_root(&challenge_id, state_root)); + + // Invalid root + assert!(!registry.validate_state_root(&challenge_id, [0u8; 32])); + + // Non-existent challenge + let fake_id = ChallengeId::new(); + assert!(!registry.validate_state_root(&fake_id, state_root)); + } + + #[test] + fn test_list_challenges() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + assert!(registry.list_challenges().is_empty()); + + let challenge_id1 = ChallengeId::new(); + let challenge_id2 = ChallengeId::new(); + + registry + .register_challenge(challenge_id1, serde_json::json!({})) + .unwrap(); + registry + .register_challenge(challenge_id2, serde_json::json!({})) + .unwrap(); + + let challenges = registry.list_challenges(); + assert_eq!(challenges.len(), 2); + assert!(challenges.contains(&challenge_id1)); + assert!(challenges.contains(&challenge_id2)); + } + + #[test] + fn test_get_schema_version() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + assert_eq!(registry.get_schema_version(&challenge_id), Some(1)); + + // Non-existent challenge + let fake_id = ChallengeId::new(); + assert_eq!(registry.get_schema_version(&fake_id), None); + } + + #[test] + fn test_update_schema_version() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + let result = registry.update_schema_version(&challenge_id, 2); + assert!(result.is_ok()); + + assert_eq!(registry.get_schema_version(&challenge_id), Some(2)); + } + + #[test] + fn test_update_schema_version_not_found() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + let result = registry.update_schema_version(&challenge_id, 2); + assert!(result.is_err()); + assert!(matches!(result.unwrap_err(), MiniChainError::NotFound(_))); + } + + #[test] + fn test_unregister_challenge() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + assert_eq!(registry.list_challenges().len(), 1); + + let result = registry.unregister_challenge(&challenge_id); + assert!(result.is_ok()); + assert!(result.unwrap()); + + assert!(registry.list_challenges().is_empty()); + assert!(registry + .get_challenge_metadata(&challenge_id) + .unwrap() + .is_none()); + } + + #[test] + fn test_unregister_nonexistent_challenge() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + let result = registry.unregister_challenge(&challenge_id); + assert!(result.is_ok()); + assert!(!result.unwrap()); + } + + #[test] + fn test_global_metadata_accessor() { + let db = create_test_db(); + let registry = MetadataRegistry::new(&db).unwrap(); + + let global = registry.global_metadata(); + assert_eq!(global.network_version, "1.0.0"); + assert_eq!(global.schema_version, 1); + } + + #[test] + fn test_storage_format_default() { + assert_eq!(StorageFormat::default(), StorageFormat::V1); + } + + #[test] + fn test_storage_format_variants() { + let v1 = StorageFormat::V1; + let v2 = StorageFormat::V2; + let custom = StorageFormat::Custom; + + assert_ne!(v1, v2); + assert_ne!(v2, custom); + assert_ne!(v1, custom); + } + + #[test] + fn test_challenge_metadata_new() { + let challenge_id = ChallengeId::new(); + let config = serde_json::json!({"test": true}); + let metadata = ChallengeMetadata::new(challenge_id, config.clone()); + + assert_eq!(metadata.challenge_id, challenge_id); + assert_eq!(metadata.schema_version, 1); + assert_eq!(metadata.storage_format, StorageFormat::V1); + assert_eq!(metadata.merkle_root, [0u8; 32]); + assert_eq!(metadata.config(), config); + } + + #[test] + fn test_challenge_metadata_update_state_root() { + let challenge_id = ChallengeId::new(); + let mut metadata = ChallengeMetadata::new(challenge_id, serde_json::json!({})); + + let initial_updated_at = metadata.updated_at; + std::thread::sleep(std::time::Duration::from_millis(10)); + + let new_root = [99u8; 32]; + metadata.update_state_root(new_root); + + assert_eq!(metadata.merkle_root, new_root); + assert!(metadata.updated_at > initial_updated_at); + } + + #[test] + fn test_challenge_metadata_update_schema_version() { + let challenge_id = ChallengeId::new(); + let mut metadata = ChallengeMetadata::new(challenge_id, serde_json::json!({})); + + let initial_updated_at = metadata.updated_at; + std::thread::sleep(std::time::Duration::from_millis(10)); + + metadata.update_schema_version(5); + + assert_eq!(metadata.schema_version, 5); + assert!(metadata.updated_at > initial_updated_at); + } + + #[test] + fn test_global_metadata_new() { + let global = GlobalMetadata::new("2.0.0".to_string()); + + assert_eq!(global.network_version, "2.0.0"); + assert_eq!(global.schema_version, 1); + assert!(global.challenges.is_empty()); + assert_eq!(global.global_state_root, [0u8; 32]); + } + + #[test] + fn test_global_metadata_challenge_count() { + let mut global = GlobalMetadata::new("1.0.0".to_string()); + assert_eq!(global.challenge_count(), 0); + + let challenge_id = ChallengeId::new(); + global.challenges.insert( + challenge_id, + ChallengeMetadata::new(challenge_id, serde_json::json!({})), + ); + assert_eq!(global.challenge_count(), 1); + } + + #[test] + fn test_metadata_serialization() { + let challenge_id = ChallengeId::new(); + let metadata = ChallengeMetadata::new( + challenge_id, + serde_json::json!({ + "timeout": 60, + "nested": {"key": "value"} + }), + ); + + let serialized = bincode::serialize(&metadata); + assert!(serialized.is_ok()); + + let deserialized: std::result::Result = + bincode::deserialize(&serialized.unwrap()); + assert!(deserialized.is_ok()); + + let deserialized = deserialized.unwrap(); + assert_eq!(deserialized.challenge_id, challenge_id); + } + + #[test] + fn test_global_metadata_serialization() { + let mut global = GlobalMetadata::new("1.0.0".to_string()); + let challenge_id = ChallengeId::new(); + global.challenges.insert( + challenge_id, + ChallengeMetadata::new(challenge_id, serde_json::json!({})), + ); + + let serialized = bincode::serialize(&global); + assert!(serialized.is_ok()); + + let deserialized: std::result::Result = + bincode::deserialize(&serialized.unwrap()); + assert!(deserialized.is_ok()); + + let deserialized = deserialized.unwrap(); + assert_eq!(deserialized.challenge_count(), 1); + } + + #[test] + fn test_flush() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id = ChallengeId::new(); + registry + .register_challenge(challenge_id, serde_json::json!({})) + .unwrap(); + + let result = registry.flush(); + assert!(result.is_ok()); + } + + #[test] + fn test_multiple_challenges_state_roots() { + let db = create_test_db(); + let mut registry = MetadataRegistry::new(&db).unwrap(); + + let challenge_id1 = ChallengeId::new(); + let challenge_id2 = ChallengeId::new(); + + registry + .register_challenge(challenge_id1, serde_json::json!({})) + .unwrap(); + registry + .register_challenge(challenge_id2, serde_json::json!({})) + .unwrap(); + + registry + .update_challenge_state_root(&challenge_id1, [1u8; 32]) + .unwrap(); + registry + .update_challenge_state_root(&challenge_id2, [2u8; 32]) + .unwrap(); + + assert!(registry.validate_state_root(&challenge_id1, [1u8; 32])); + assert!(registry.validate_state_root(&challenge_id2, [2u8; 32])); + + // Global state root should reflect both + let global_root = registry.compute_global_state_root(); + assert_ne!(global_root, [0u8; 32]); + } +} diff --git a/crates/storage/src/migration.rs b/crates/storage/src/migration.rs new file mode 100644 index 000000000..2b123c36a --- /dev/null +++ b/crates/storage/src/migration.rs @@ -0,0 +1,1653 @@ +//! Migration system for blockchain upgrades +//! +//! Provides versioned migrations that run when the blockchain is upgraded. +//! Similar to database migrations but for blockchain state. +//! +//! ## Network-Aware Migrations +//! +//! For distributed validator networks, migrations must be coordinated across +//! all validators to ensure consistent schema versions: +//! +//! ```text +//! use platform_storage::{NetworkMigrationCoordinator, NetworkMigrationStatus}; +//! +//! let coordinator = NetworkMigrationCoordinator::new(&db)?; +//! +//! // Check if we can accept a new validator +//! if coordinator.can_accept_validator(&their_hotkey, their_version) { +//! // Accept validator +//! } +//! +//! // Start network-wide migration +//! coordinator.start_network_migration(target_version)?; +//! ``` + +use crate::types::{StorageKey, StorageValue}; +use platform_core::{ChallengeId, Hotkey, MiniChainError, Result}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sled::Tree; +use std::collections::{BTreeMap, HashMap}; +use std::time::SystemTime; +use tracing::{debug, info, warn}; + +/// Migration version number +pub type MigrationVersion = u64; + +/// Migration trait - implement this for each migration +pub trait Migration: Send + Sync { + /// Unique version number (must be sequential) + fn version(&self) -> MigrationVersion; + + /// Human-readable name for this migration + fn name(&self) -> &str; + + /// Description of what this migration does + fn description(&self) -> &str { + "" + } + + /// Run the migration (upgrade) + fn up(&self, ctx: &mut MigrationContext) -> Result<()>; + + /// Rollback the migration (downgrade) - optional + fn down(&self, _ctx: &mut MigrationContext) -> Result<()> { + Err(MiniChainError::Storage("Rollback not supported".into())) + } + + /// Whether this migration can be rolled back + fn reversible(&self) -> bool { + false + } +} + +/// Context provided to migrations for reading/writing data +pub struct MigrationContext<'a> { + /// Access to the dynamic storage tree + pub storage_tree: &'a Tree, + /// Access to the state tree + pub state_tree: &'a Tree, + /// Current block height + pub block_height: u64, + /// Changes made during this migration + pub changes: Vec, +} + +impl<'a> MigrationContext<'a> { + pub fn new(storage_tree: &'a Tree, state_tree: &'a Tree, block_height: u64) -> Self { + Self { + storage_tree, + state_tree, + block_height, + changes: Vec::new(), + } + } + + /// Get a value from dynamic storage + pub fn get(&self, key: &StorageKey) -> Result> { + let key_bytes = key.to_bytes(); + match self.storage_tree.get(&key_bytes) { + Ok(Some(data)) => { + let value: StorageValue = bincode::deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(Some(value)) + } + Ok(None) => Ok(None), + Err(e) => Err(MiniChainError::Storage(e.to_string())), + } + } + + /// Set a value in dynamic storage + pub fn set(&mut self, key: StorageKey, value: StorageValue) -> Result<()> { + let key_bytes = key.to_bytes(); + let old_value = self.get(&key)?; + + let data = + bincode::serialize(&value).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + self.storage_tree + .insert(&key_bytes, data) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + self.changes.push(MigrationChange { + key: key.clone(), + old_value, + new_value: Some(value), + }); + + Ok(()) + } + + /// Delete a value from dynamic storage + pub fn delete(&mut self, key: &StorageKey) -> Result> { + let key_bytes = key.to_bytes(); + let old_value = self.get(key)?; + + self.storage_tree + .remove(&key_bytes) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + if old_value.is_some() { + self.changes.push(MigrationChange { + key: key.clone(), + old_value: old_value.clone(), + new_value: None, + }); + } + + Ok(old_value) + } + + /// Scan keys with a prefix + pub fn scan_prefix(&self, namespace: &str) -> Result> { + let prefix = StorageKey::namespace_prefix(namespace); + let mut results = Vec::new(); + + for item in self.storage_tree.scan_prefix(&prefix) { + let (key_bytes, value_bytes) = + item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + + // Parse key (simplified - in production use proper parsing) + let key_str = String::from_utf8_lossy(&key_bytes); + let parts: Vec<&str> = key_str.split('\0').collect(); + if parts.len() >= 2 { + let key = StorageKey { + namespace: parts[0].to_string(), + validator: None, // Simplified + key: parts.last().unwrap_or(&"").to_string(), + }; + + let value: StorageValue = bincode::deserialize(&value_bytes) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + + results.push((key, value)); + } + } + + Ok(results) + } + + /// Get raw state data + pub fn get_state_raw(&self, key: &str) -> Result>> { + self.state_tree + .get(key) + .map(|opt| opt.map(|v| v.to_vec())) + .map_err(|e| MiniChainError::Storage(e.to_string())) + } + + /// Set raw state data + pub fn set_state_raw(&self, key: &str, value: Vec) -> Result<()> { + self.state_tree + .insert(key, value) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } +} + +/// Record of a change made during migration +#[derive(Clone, Debug)] +pub struct MigrationChange { + pub key: StorageKey, + pub old_value: Option, + pub new_value: Option, +} + +/// Record of an applied migration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct MigrationRecord { + pub version: MigrationVersion, + pub name: String, + pub applied_at: SystemTime, + pub block_height: u64, + pub checksum: [u8; 32], +} + +impl Default for MigrationRecord { + fn default() -> Self { + Self { + version: 0, + name: String::new(), + applied_at: SystemTime::UNIX_EPOCH, + block_height: 0, + checksum: [0u8; 32], + } + } +} + +/// Migration runner - manages and executes migrations +pub struct MigrationRunner { + migrations: BTreeMap>, + migrations_tree: Tree, +} + +impl MigrationRunner { + /// Create a new migration runner + pub fn new(db: &sled::Db) -> Result { + let migrations_tree = db.open_tree("migrations").map_err(|e| { + MiniChainError::Storage(format!("Failed to open migrations tree: {}", e)) + })?; + + Ok(Self { + migrations: BTreeMap::new(), + migrations_tree, + }) + } + + /// Register a migration + pub fn register(&mut self, migration: Box) { + let version = migration.version(); + if self.migrations.contains_key(&version) { + warn!("Migration version {} already registered, skipping", version); + return; + } + info!("Registered migration {}: {}", version, migration.name()); + self.migrations.insert(version, migration); + } + + /// Get the current schema version + pub fn current_version(&self) -> Result { + match self + .migrations_tree + .get("current_version") + .map_err(|e| MiniChainError::Storage(e.to_string()))? + { + Some(data) => { + let version: MigrationVersion = bincode::deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(version) + } + None => Ok(0), + } + } + + /// Set the current schema version + fn set_current_version(&self, version: MigrationVersion) -> Result<()> { + let data = bincode::serialize(&version) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + self.migrations_tree + .insert("current_version", data) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } + + /// Get list of applied migrations + pub fn applied_migrations(&self) -> Result> { + let mut records = Vec::new(); + + for item in self.migrations_tree.scan_prefix(b"applied:") { + let (_, data) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + let record: MigrationRecord = bincode::deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + records.push(record); + } + + records.sort_by_key(|r| r.version); + Ok(records) + } + + /// Check if a migration has been applied + pub fn is_applied(&self, version: MigrationVersion) -> Result { + let key = format!("applied:{}", version); + self.migrations_tree + .contains_key(key) + .map_err(|e| MiniChainError::Storage(e.to_string())) + } + + /// Record that a migration was applied + fn record_applied(&self, record: MigrationRecord) -> Result<()> { + let key = format!("applied:{}", record.version); + let data = bincode::serialize(&record) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + self.migrations_tree + .insert(key, data) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } + + /// Get pending migrations + pub fn pending_migrations(&self) -> Result> { + let current = self.current_version()?; + Ok(self + .migrations + .keys() + .filter(|&&v| v > current) + .copied() + .collect()) + } + + /// Run all pending migrations + pub fn run_pending( + &self, + storage_tree: &Tree, + state_tree: &Tree, + block_height: u64, + ) -> Result> { + let pending = self.pending_migrations()?; + + if pending.is_empty() { + info!("No pending migrations"); + return Ok(vec![]); + } + + info!("Running {} pending migrations", pending.len()); + let mut applied = Vec::new(); + + for version in pending { + if let Some(migration) = self.migrations.get(&version) { + info!("Running migration {}: {}", version, migration.name()); + + let mut ctx = MigrationContext::new(storage_tree, state_tree, block_height); + + migration.up(&mut ctx)?; + + // Calculate checksum of changes + let checksum = self.calculate_checksum(&ctx.changes); + + // Record the migration + let record = MigrationRecord { + version, + name: migration.name().to_string(), + applied_at: SystemTime::now(), + block_height, + checksum, + }; + + self.record_applied(record)?; + self.set_current_version(version)?; + + info!( + "Migration {} completed ({} changes)", + version, + ctx.changes.len() + ); + applied.push(version); + } + } + + // Flush changes + self.migrations_tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + storage_tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + state_tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + Ok(applied) + } + + /// Rollback to a specific version + pub fn rollback_to( + &self, + target_version: MigrationVersion, + storage_tree: &Tree, + state_tree: &Tree, + block_height: u64, + ) -> Result> { + let current = self.current_version()?; + + if target_version >= current { + return Err(MiniChainError::Storage(format!( + "Cannot rollback to version {} (current is {})", + target_version, current + ))); + } + + let mut rolled_back = Vec::new(); + + // Rollback in reverse order + for version in (target_version + 1..=current).rev() { + if let Some(migration) = self.migrations.get(&version) { + if !migration.reversible() { + return Err(MiniChainError::Storage(format!( + "Migration {} is not reversible", + version + ))); + } + + info!("Rolling back migration {}: {}", version, migration.name()); + + let mut ctx = MigrationContext::new(storage_tree, state_tree, block_height); + migration.down(&mut ctx)?; + + // Remove the applied record + let key = format!("applied:{}", version); + self.migrations_tree + .remove(key) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + rolled_back.push(version); + } + } + + self.set_current_version(target_version)?; + self.migrations_tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + Ok(rolled_back) + } + + /// Calculate checksum of migration changes + fn calculate_checksum(&self, changes: &[MigrationChange]) -> [u8; 32] { + use sha2::{Digest, Sha256}; + let mut hasher = Sha256::new(); + + for change in changes { + hasher.update(change.key.to_bytes()); + if let Some(ref v) = change.old_value { + if let Ok(data) = bincode::serialize(v) { + hasher.update(&data); + } + } + if let Some(ref v) = change.new_value { + if let Ok(data) = bincode::serialize(v) { + hasher.update(&data); + } + } + } + + hasher.finalize().into() + } +} + +// === Built-in Migrations === + +/// Initial migration - sets up base storage schema +pub struct InitialMigration; + +impl Migration for InitialMigration { + fn version(&self) -> MigrationVersion { + 1 + } + fn name(&self) -> &str { + "initial_setup" + } + fn description(&self) -> &str { + "Initial storage schema setup" + } + + fn up(&self, ctx: &mut MigrationContext) -> Result<()> { + // Set schema version + ctx.set(StorageKey::system("schema_version"), StorageValue::U64(1))?; + + // Set creation timestamp + ctx.set( + StorageKey::system("created_at"), + StorageValue::U64( + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_default() + .as_secs(), + ), + )?; + + // Initialize counters + ctx.set(StorageKey::system("total_challenges"), StorageValue::U64(0))?; + ctx.set(StorageKey::system("total_validators"), StorageValue::U64(0))?; + ctx.set(StorageKey::system("total_jobs"), StorageValue::U64(0))?; + + Ok(()) + } +} + +/// Migration to add challenge metrics storage +pub struct AddChallengeMetricsMigration; + +impl Migration for AddChallengeMetricsMigration { + fn version(&self) -> MigrationVersion { + 2 + } + fn name(&self) -> &str { + "add_challenge_metrics" + } + fn description(&self) -> &str { + "Add per-challenge metrics storage" + } + + fn up(&self, ctx: &mut MigrationContext) -> Result<()> { + // Add metrics enabled flag + ctx.set( + StorageKey::system("metrics_enabled"), + StorageValue::Bool(true), + )?; + + // Add default retention period (7 days in seconds) + ctx.set( + StorageKey::system("metrics_retention_secs"), + StorageValue::U64(7 * 24 * 60 * 60), + )?; + + Ok(()) + } + + fn down(&self, ctx: &mut MigrationContext) -> Result<()> { + ctx.delete(&StorageKey::system("metrics_enabled"))?; + ctx.delete(&StorageKey::system("metrics_retention_secs"))?; + Ok(()) + } + + fn reversible(&self) -> bool { + true + } +} + +// ============================================================================ +// Network-Aware Migration Coordination +// ============================================================================ + +/// Network migration status for coordination across validators +/// +/// Tracks the migration state across the distributed validator network, +/// ensuring all validators are synchronized before accepting new ones. +#[derive(Clone, Debug, Serialize, Deserialize, Default)] +pub struct NetworkMigrationStatus { + /// Current network-wide schema version + pub network_version: MigrationVersion, + /// Validators that have reported their version (hotkey -> version) + pub validator_versions: HashMap, + /// Whether a migration is currently in progress network-wide + pub migration_in_progress: bool, + /// Target version being migrated to + pub target_version: Option, + /// Timestamp when migration started + pub started_at: Option, +} + +/// Challenge-specific migration record +/// +/// Tracks migrations for individual challenges, allowing challenges +/// to have their own schema versions independent of the global version. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeMigration { + /// Challenge ID + pub challenge_id: ChallengeId, + /// Source schema version + pub from_version: u64, + /// Target schema version + pub to_version: u64, + /// State hash before migration + pub state_hash_before: [u8; 32], + /// State hash after migration (set when completed) + pub state_hash_after: Option<[u8; 32]>, + /// Current status + pub status: ChallengeMigrationStatus, +} + +/// Status of a challenge-specific migration +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum ChallengeMigrationStatus { + /// Migration has not started + Pending, + /// Migration is currently running + InProgress, + /// Migration completed successfully + Completed, + /// Migration failed with error + Failed(String), +} + +/// Coordinator for network-wide migration synchronization +/// +/// Ensures validators stay synchronized during schema upgrades by: +/// - Tracking validator versions across the network +/// - Blocking new validators until they sync to the current schema +/// - Coordinating migration rollouts across all validators +pub struct NetworkMigrationCoordinator { + /// Tree for storing network migration state + network_tree: Tree, + /// Cached network status + cached_status: Option, +} + +impl NetworkMigrationCoordinator { + /// Create a new network migration coordinator + /// + /// # Arguments + /// + /// * `db` - The sled database to use for persistence + /// + /// # Returns + /// + /// A new `NetworkMigrationCoordinator` instance + pub fn new(db: &sled::Db) -> Result { + let network_tree = db.open_tree("network_migrations").map_err(|e| { + MiniChainError::Storage(format!("Failed to open network_migrations tree: {}", e)) + })?; + + Ok(Self { + network_tree, + cached_status: None, + }) + } + + /// Get the current network migration status + /// + /// Loads the status from the database or returns defaults if not set. + pub fn get_network_status(&self) -> Result { + match self + .network_tree + .get("status") + .map_err(|e| MiniChainError::Storage(e.to_string()))? + { + Some(data) => { + let status: NetworkMigrationStatus = bincode::deserialize(&data) + .map_err(|e| MiniChainError::Serialization(e.to_string()))?; + Ok(status) + } + None => Ok(NetworkMigrationStatus::default()), + } + } + + /// Save the network migration status + fn save_network_status(&self, status: &NetworkMigrationStatus) -> Result<()> { + let data = + bincode::serialize(status).map_err(|e| MiniChainError::Serialization(e.to_string()))?; + self.network_tree + .insert("status", data) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + self.network_tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } + + /// Report a validator's current schema version + /// + /// Called by validators to report their current version to the network. + /// + /// # Arguments + /// + /// * `validator` - The validator's hotkey + /// * `version` - The validator's current schema version + pub fn report_validator_version( + &mut self, + validator: Hotkey, + version: MigrationVersion, + ) -> Result<()> { + let mut status = self.get_network_status()?; + status.validator_versions.insert(validator.clone(), version); + + debug!( + validator = %validator.to_hex(), + version = version, + "Validator reported schema version" + ); + + self.save_network_status(&status)?; + self.cached_status = Some(status); + Ok(()) + } + + /// Check if a validator can be accepted based on schema version + /// + /// A validator can be accepted if: + /// - No migration is in progress, OR + /// - The validator's version >= network version + /// + /// # Arguments + /// + /// * `validator` - The validator's hotkey + /// * `their_version` - The validator's reported schema version + /// + /// # Returns + /// + /// `true` if the validator can be accepted + pub fn can_accept_validator( + &self, + validator: &Hotkey, + their_version: MigrationVersion, + ) -> bool { + let status = match self.get_network_status() { + Ok(s) => s, + Err(e) => { + warn!( + error = %e, + validator = %validator.to_hex(), + "Failed to get network status, rejecting validator" + ); + return false; + } + }; + + // During migration, only accept validators at or above target version + if status.migration_in_progress { + if let Some(target) = status.target_version { + return their_version >= target; + } + } + + // Otherwise, accept if at or above network version + their_version >= status.network_version + } + + /// Start a network-wide migration to a target version + /// + /// This marks the migration as in-progress and sets the target version. + /// Validators should check `is_migration_in_progress()` before processing. + /// + /// # Arguments + /// + /// * `target_version` - The version to migrate to + pub fn start_network_migration(&mut self, target_version: MigrationVersion) -> Result<()> { + let mut status = self.get_network_status()?; + + if status.migration_in_progress { + return Err(MiniChainError::Storage(format!( + "Migration already in progress to version {:?}", + status.target_version + ))); + } + + if target_version <= status.network_version { + return Err(MiniChainError::Storage(format!( + "Target version {} must be greater than current version {}", + target_version, status.network_version + ))); + } + + info!( + from_version = status.network_version, + to_version = target_version, + "Starting network-wide migration" + ); + + status.migration_in_progress = true; + status.target_version = Some(target_version); + status.started_at = Some(SystemTime::now()); + + self.save_network_status(&status)?; + self.cached_status = Some(status); + Ok(()) + } + + /// Complete migration for a specific validator + /// + /// Called when a validator has finished migrating to the target version. + /// + /// # Arguments + /// + /// * `validator` - The validator that completed migration + pub fn complete_migration(&mut self, validator: &Hotkey) -> Result<()> { + let mut status = self.get_network_status()?; + + if !status.migration_in_progress { + return Ok(()); // No migration in progress + } + + let target = status.target_version.unwrap_or(status.network_version); + status.validator_versions.insert(validator.clone(), target); + + debug!( + validator = %validator.to_hex(), + version = target, + "Validator completed migration" + ); + + self.save_network_status(&status)?; + self.cached_status = Some(status); + Ok(()) + } + + /// Finalize migration when all validators have completed + /// + /// Call this after verifying all active validators have migrated. + pub fn finalize_network_migration(&mut self) -> Result<()> { + let mut status = self.get_network_status()?; + + if !status.migration_in_progress { + return Ok(()); + } + + let target = status.target_version.unwrap_or(status.network_version); + + info!( + old_version = status.network_version, + new_version = target, + "Finalizing network migration" + ); + + status.network_version = target; + status.migration_in_progress = false; + status.target_version = None; + status.started_at = None; + + self.save_network_status(&status)?; + self.cached_status = Some(status); + Ok(()) + } + + /// Check if a migration is currently in progress + pub fn is_migration_in_progress(&self) -> bool { + self.get_network_status() + .map(|s| s.migration_in_progress) + .unwrap_or(false) + } + + /// Get list of validators that need to upgrade + /// + /// Returns validators whose version is below the network version. + pub fn get_validators_needing_upgrade(&self) -> Vec { + let status = match self.get_network_status() { + Ok(s) => s, + Err(_) => return vec![], + }; + + status + .validator_versions + .iter() + .filter(|(_, v)| **v < status.network_version) + .map(|(h, _)| h.clone()) + .collect() + } + + /// Set the network version directly (for initialization) + pub fn set_network_version(&mut self, version: MigrationVersion) -> Result<()> { + let mut status = self.get_network_status()?; + status.network_version = version; + self.save_network_status(&status)?; + self.cached_status = Some(status); + Ok(()) + } +} + +/// Compute a state hash for migration verification +/// +/// Computes a hash of all data in a challenge's namespace to verify +/// that migrations produce consistent results across validators. +/// +/// # Arguments +/// +/// * `ctx` - The migration context +/// * `challenge_id` - The challenge to compute hash for +/// +/// # Returns +/// +/// A 32-byte hash of the challenge's current state +pub fn compute_migration_state_hash( + ctx: &MigrationContext, + challenge_id: &ChallengeId, +) -> [u8; 32] { + let mut hasher = Sha256::new(); + + // Hash the challenge ID + hasher.update(challenge_id.0.as_bytes()); + + // Scan and hash all keys in the challenge namespace + let namespace = challenge_id.0.to_string(); + if let Ok(entries) = ctx.scan_prefix(&namespace) { + for (key, value) in entries { + hasher.update(key.to_bytes()); + if let Ok(data) = bincode::serialize(&value) { + hasher.update(&data); + } + } + } + + hasher.finalize().into() +} + +/// Trait for challenge-specific migration handlers +/// +/// Implement this trait to create migrations that are specific to a +/// single challenge's data schema. +pub trait ChallengeMigrationHandler: Send + Sync { + /// Get the challenge ID this migration applies to + fn challenge_id(&self) -> &ChallengeId; + + /// Source schema version + fn source_version(&self) -> u64; + + /// Target schema version + fn target_version(&self) -> u64; + + /// Run the migration + fn migrate(&self, ctx: &mut MigrationContext) -> Result<()>; + + /// Rollback the migration (optional) + fn rollback(&self, _ctx: &mut MigrationContext) -> Result<()> { + Err(MiniChainError::Storage( + "Challenge migration rollback not supported".to_string(), + )) + } + + /// Whether this migration can be rolled back + fn reversible(&self) -> bool { + false + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_migration_runner() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + + // Register migrations + runner.register(Box::new(InitialMigration)); + runner.register(Box::new(AddChallengeMetricsMigration)); + + // Check pending + let pending = runner.pending_migrations().unwrap(); + assert_eq!(pending.len(), 2); + + // Run migrations + let applied = runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + assert_eq!(applied.len(), 2); + + // Check version + assert_eq!(runner.current_version().unwrap(), 2); + + // Check no pending + let pending = runner.pending_migrations().unwrap(); + assert!(pending.is_empty()); + } + + #[test] + fn test_migration_context() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 0); + + // Set and get + let key = StorageKey::system("test_key"); + ctx.set(key.clone(), StorageValue::U64(42)).unwrap(); + + let value = ctx.get(&key).unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(42)); + + // Delete + ctx.delete(&key).unwrap(); + assert!(ctx.get(&key).unwrap().is_none()); + } + + #[test] + fn test_migration_context_scan_prefix() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 0); + + // Add multiple keys with same namespace + for i in 0..3 { + let key = StorageKey::system(format!("key{}", i)); + ctx.set(key, StorageValue::U64(i)).unwrap(); + } + + let results = ctx.scan_prefix("system").unwrap(); + assert_eq!(results.len(), 3); + } + + #[test] + fn test_migration_context_get_state_raw() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + state_tree.insert("test_state", b"state_value").unwrap(); + + let ctx = MigrationContext::new(&storage_tree, &state_tree, 0); + let value = ctx.get_state_raw("test_state").unwrap(); + + assert_eq!(value, Some(b"state_value".to_vec())); + } + + #[test] + fn test_migration_context_set_state_raw() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let ctx = MigrationContext::new(&storage_tree, &state_tree, 0); + ctx.set_state_raw("test_state", b"new_value".to_vec()) + .unwrap(); + + let value = state_tree.get("test_state").unwrap(); + assert_eq!(value.unwrap().as_ref(), b"new_value"); + } + + #[test] + fn test_migration_runner_current_version_default() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let runner = MigrationRunner::new(&db).unwrap(); + assert_eq!(runner.current_version().unwrap(), 0); + } + + #[test] + fn test_migration_runner_is_applied() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + + assert!(!runner.is_applied(1).unwrap()); + + runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + + assert!(runner.is_applied(1).unwrap()); + } + + #[test] + fn test_migration_runner_applied_migrations() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + + runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + + let applied = runner.applied_migrations().unwrap(); + assert_eq!(applied.len(), 1); + assert_eq!(applied[0].version, 1); + } + + #[test] + fn test_migration_runner_pending_migrations() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + runner.register(Box::new(AddChallengeMetricsMigration)); + + let pending = runner.pending_migrations().unwrap(); + assert_eq!(pending.len(), 2); + assert_eq!(pending[0], 1); + assert_eq!(pending[1], 2); + } + + #[test] + fn test_initial_migration_properties() { + let migration = InitialMigration; + assert_eq!(migration.version(), 1); + assert_eq!(migration.name(), "initial_setup"); + assert!(!migration.description().is_empty()); + assert!(!migration.reversible()); + } + + #[test] + fn test_add_challenge_metrics_migration_properties() { + let migration = AddChallengeMetricsMigration; + assert_eq!(migration.version(), 2); + assert_eq!(migration.name(), "add_challenge_metrics"); + assert!(!migration.description().is_empty()); + assert!(migration.reversible()); + } + + #[test] + fn test_migration_record_serialization() { + let record = MigrationRecord { + version: 1, + name: "test".to_string(), + applied_at: SystemTime::now(), + block_height: 100, + checksum: [1u8; 32], + }; + + let serialized = bincode::serialize(&record).unwrap(); + let deserialized: MigrationRecord = bincode::deserialize(&serialized).unwrap(); + + assert_eq!(deserialized.version, record.version); + assert_eq!(deserialized.name, record.name); + } + + #[test] + fn test_migration_context_delete() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + // Set a value first + let key = StorageKey::system("to_delete"); + ctx.set(key.clone(), StorageValue::U64(123)).unwrap(); + + // Delete it + let deleted = ctx.delete(&key).unwrap(); + assert!(deleted.is_some()); + assert_eq!(deleted.unwrap().as_u64(), Some(123)); + + // Verify it's gone + let value = ctx.get(&key).unwrap(); + assert!(value.is_none()); + + // Delete non-existent key + let deleted2 = ctx.delete(&StorageKey::system("nonexistent")).unwrap(); + assert!(deleted2.is_none()); + } + + #[test] + fn test_migration_context_changes_tracking() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + // Initially no changes + assert_eq!(ctx.changes.len(), 0); + + // Set a value + ctx.set(StorageKey::system("key1"), StorageValue::U64(1)) + .unwrap(); + assert_eq!(ctx.changes.len(), 1); + assert!(ctx.changes[0].old_value.is_none()); + assert!(ctx.changes[0].new_value.is_some()); + + // Update the value + ctx.set(StorageKey::system("key1"), StorageValue::U64(2)) + .unwrap(); + assert_eq!(ctx.changes.len(), 2); + assert!(ctx.changes[1].old_value.is_some()); + + // Delete a value + ctx.delete(&StorageKey::system("key1")).unwrap(); + assert_eq!(ctx.changes.len(), 3); + } + + #[test] + fn test_migration_runner_is_applied_after_run() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + + // Not applied initially + assert!(!runner.is_applied(1).unwrap()); + + // Apply it + runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + + // Now it's applied + assert!(runner.is_applied(1).unwrap()); + } + + #[test] + fn test_add_challenge_metrics_migration_details() { + let migration = AddChallengeMetricsMigration; + assert_eq!(migration.version(), 2); + assert_eq!(migration.name(), "add_challenge_metrics"); + assert!(!migration.description().is_empty()); + assert!(migration.reversible()); + } + + #[test] + fn test_add_challenge_metrics_migration_up() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + let migration = AddChallengeMetricsMigration; + + migration.up(&mut ctx).unwrap(); + + // Check that metrics_enabled was set + let metrics_enabled = ctx.get(&StorageKey::system("metrics_enabled")).unwrap(); + assert!(metrics_enabled.is_some()); + assert_eq!(metrics_enabled.unwrap().as_bool(), Some(true)); + + // Check that retention was set + let retention = ctx + .get(&StorageKey::system("metrics_retention_secs")) + .unwrap(); + assert!(retention.is_some()); + assert_eq!(retention.unwrap().as_u64(), Some(7 * 24 * 60 * 60)); + } + + #[test] + fn test_add_challenge_metrics_migration_down() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + let migration = AddChallengeMetricsMigration; + + // First run up + migration.up(&mut ctx).unwrap(); + + // Then run down + migration.down(&mut ctx).unwrap(); + + // Keys should be deleted + let metrics_enabled = ctx.get(&StorageKey::system("metrics_enabled")).unwrap(); + assert!(metrics_enabled.is_none()); + + let retention = ctx + .get(&StorageKey::system("metrics_retention_secs")) + .unwrap(); + assert!(retention.is_none()); + } + + #[test] + fn test_initial_migration_up() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + let migration = InitialMigration; + + migration.up(&mut ctx).unwrap(); + + // Check all keys were set + let schema_version = ctx.get(&StorageKey::system("schema_version")).unwrap(); + assert_eq!(schema_version.unwrap().as_u64(), Some(1)); + + let created_at = ctx.get(&StorageKey::system("created_at")).unwrap(); + assert!(created_at.is_some()); + + let total_challenges = ctx.get(&StorageKey::system("total_challenges")).unwrap(); + assert_eq!(total_challenges.unwrap().as_u64(), Some(0)); + + let total_validators = ctx.get(&StorageKey::system("total_validators")).unwrap(); + assert_eq!(total_validators.unwrap().as_u64(), Some(0)); + + let total_jobs = ctx.get(&StorageKey::system("total_jobs")).unwrap(); + assert_eq!(total_jobs.unwrap().as_u64(), Some(0)); + } + + #[test] + fn test_migration_runner_multiple_migrations() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + runner.register(Box::new(AddChallengeMetricsMigration)); + + // Run all pending + let applied = runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + assert_eq!(applied.len(), 2); + assert_eq!(applied[0], 1); + assert_eq!(applied[1], 2); + + // Version should be 2 + assert_eq!(runner.current_version().unwrap(), 2); + + // Both should be applied + assert!(runner.is_applied(1).unwrap()); + assert!(runner.is_applied(2).unwrap()); + } + + #[test] + fn test_migration_context_state_operations() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + // Set raw state + ctx.set_state_raw("test_key", vec![1, 2, 3, 4]).unwrap(); + + // Get raw state + let value = ctx.get_state_raw("test_key").unwrap(); + assert!(value.is_some()); + assert_eq!(value.unwrap(), vec![1, 2, 3, 4]); + + // Get non-existent + let none = ctx.get_state_raw("nonexistent").unwrap(); + assert!(none.is_none()); + } + + #[test] + fn test_migration_runner_duplicate_registration() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + + // Register same migration twice + runner.register(Box::new(InitialMigration)); + runner.register(Box::new(InitialMigration)); + + // Should only have one migration + assert_eq!(runner.pending_migrations().unwrap().len(), 1); + } + + #[test] + fn test_migration_context_update_existing_value() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + let key = StorageKey::system("counter"); + + // Set initial value + ctx.set(key.clone(), StorageValue::U64(1)).unwrap(); + + // Update it + ctx.set(key.clone(), StorageValue::U64(2)).unwrap(); + + // Verify updated + let value = ctx.get(&key).unwrap(); + assert_eq!(value.unwrap().as_u64(), Some(2)); + } + + #[test] + fn test_migration_default_methods() { + struct TestMigration; + impl Migration for TestMigration { + fn version(&self) -> MigrationVersion { + 1 + } + fn name(&self) -> &str { + "test" + } + fn up(&self, _ctx: &mut MigrationContext) -> Result<()> { + Ok(()) + } + } + + let migration = TestMigration; + // Test default implementations + assert_eq!(migration.description(), ""); // Default description + assert!(!migration.reversible()); // Default not reversible + assert!(migration + .down(&mut MigrationContext::new( + &sled::Config::new() + .temporary(true) + .open() + .unwrap() + .open_tree("test") + .unwrap(), + &sled::Config::new() + .temporary(true) + .open() + .unwrap() + .open_tree("state") + .unwrap(), + 0 + )) + .is_err()); // Default down returns error + } + + #[test] + fn test_migration_context_get_nonexistent() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + // Test line 76 - getting nonexistent key returns Ok(None) + let value = ctx.get(&StorageKey::system("nonexistent")).unwrap(); + assert!(value.is_none()); + } + + #[test] + fn test_migration_context_scan_prefix_error_handling() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let ctx = MigrationContext::new(&storage_tree, &state_tree, 100); + + // Test line 128 - scan_prefix error handling + let result = ctx.scan_prefix("test_namespace"); + assert!(result.is_ok()); + } + + #[test] + fn test_migration_record_field_access() { + let record = MigrationRecord { + version: 1, + name: "test".to_string(), + applied_at: SystemTime::now(), + block_height: 100, + checksum: [1u8; 32], + }; + + // Test line 195 - record_applied serialization + let serialized = bincode::serialize(&record).unwrap(); + assert!(!serialized.is_empty()); + } + + #[test] + fn test_run_pending_empty_migrations() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + + // Run once + runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + + // Run again - lines 296-297: pending.is_empty() should return early + let result = runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + assert_eq!(result.len(), 0); + } + + #[test] + fn test_rollback_to_non_reversible() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut runner = MigrationRunner::new(&db).unwrap(); + runner.register(Box::new(InitialMigration)); + + // Apply migration + runner.run_pending(&storage_tree, &state_tree, 0).unwrap(); + + // Try to rollback non-reversible migration (lines 409-410) + let result = runner.rollback_to(0, &storage_tree, &state_tree, 0); + assert!(result.is_err()); + } + + // === Network Migration Tests === + + #[test] + fn test_network_migration_status_serialization() { + let status = NetworkMigrationStatus { + network_version: 5, + validator_versions: HashMap::new(), + migration_in_progress: false, + target_version: None, + started_at: None, + }; + + let serialized = bincode::serialize(&status).unwrap(); + let deserialized: NetworkMigrationStatus = bincode::deserialize(&serialized).unwrap(); + + assert_eq!(deserialized.network_version, 5); + assert!(!deserialized.migration_in_progress); + } + + #[test] + fn test_network_migration_coordinator_creation() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let coordinator = NetworkMigrationCoordinator::new(&db).unwrap(); + let status = coordinator.get_network_status().unwrap(); + + assert_eq!(status.network_version, 0); + assert!(!status.migration_in_progress); + } + + #[test] + fn test_network_migration_coordinator_report_version() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut coordinator = NetworkMigrationCoordinator::new(&db).unwrap(); + let validator = Hotkey([1u8; 32]); + + coordinator + .report_validator_version(validator.clone(), 3) + .unwrap(); + + let status = coordinator.get_network_status().unwrap(); + assert_eq!(*status.validator_versions.get(&validator).unwrap(), 3); + } + + #[test] + fn test_network_migration_coordinator_can_accept_validator() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut coordinator = NetworkMigrationCoordinator::new(&db).unwrap(); + let validator = Hotkey([1u8; 32]); + + // When network version is 0, accept any version >= 0 + assert!(coordinator.can_accept_validator(&validator, 0)); + assert!(coordinator.can_accept_validator(&validator, 5)); + + // Set network version to 5 + coordinator.set_network_version(5).unwrap(); + + // Now only accept validators at version 5 or higher + assert!(!coordinator.can_accept_validator(&validator, 4)); + assert!(coordinator.can_accept_validator(&validator, 5)); + assert!(coordinator.can_accept_validator(&validator, 6)); + } + + #[test] + fn test_network_migration_start_and_complete() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut coordinator = NetworkMigrationCoordinator::new(&db).unwrap(); + let validator = Hotkey([1u8; 32]); + + // Start migration + coordinator.start_network_migration(5).unwrap(); + + let status = coordinator.get_network_status().unwrap(); + assert!(status.migration_in_progress); + assert_eq!(status.target_version, Some(5)); + + // Complete migration for validator + coordinator.complete_migration(&validator).unwrap(); + + // Migration still in progress until network version is updated + assert!(coordinator.is_migration_in_progress()); + } + + #[test] + fn test_challenge_migration_status() { + let status = ChallengeMigrationStatus::Pending; + assert_eq!(status, ChallengeMigrationStatus::Pending); + + let failed = ChallengeMigrationStatus::Failed("test error".to_string()); + assert!(matches!(failed, ChallengeMigrationStatus::Failed(_))); + } + + #[test] + fn test_challenge_migration_serialization() { + let migration = ChallengeMigration { + challenge_id: ChallengeId(uuid::Uuid::new_v4()), + from_version: 1, + to_version: 2, + state_hash_before: [1u8; 32], + state_hash_after: Some([2u8; 32]), + status: ChallengeMigrationStatus::Completed, + }; + + let serialized = bincode::serialize(&migration).unwrap(); + let deserialized: ChallengeMigration = bincode::deserialize(&serialized).unwrap(); + + assert_eq!(deserialized.from_version, 1); + assert_eq!(deserialized.to_version, 2); + } + + #[test] + fn test_validators_needing_upgrade() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + + let mut coordinator = NetworkMigrationCoordinator::new(&db).unwrap(); + let v1 = Hotkey([1u8; 32]); + let v2 = Hotkey([2u8; 32]); + let v3 = Hotkey([3u8; 32]); + + // Set network version to 5 + coordinator.set_network_version(5).unwrap(); + + // Report different versions + coordinator.report_validator_version(v1.clone(), 5).unwrap(); + coordinator.report_validator_version(v2.clone(), 4).unwrap(); + coordinator.report_validator_version(v3.clone(), 3).unwrap(); + + let needing_upgrade = coordinator.get_validators_needing_upgrade(); + + // v2 and v3 need upgrade + assert_eq!(needing_upgrade.len(), 2); + assert!(needing_upgrade.contains(&v2)); + assert!(needing_upgrade.contains(&v3)); + } + + #[test] + fn test_compute_migration_state_hash() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let storage_tree = db.open_tree("dynamic_storage").unwrap(); + let state_tree = db.open_tree("state").unwrap(); + + let mut ctx = MigrationContext::new(&storage_tree, &state_tree, 0); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + + // Empty state should still produce a hash + let hash1 = compute_migration_state_hash(&ctx, &challenge_id); + assert_ne!(hash1, [0u8; 32]); + + // Adding data should change the hash + ctx.set( + StorageKey::challenge(&challenge_id, "test"), + StorageValue::U64(42), + ) + .unwrap(); + let hash2 = compute_migration_state_hash(&ctx, &challenge_id); + assert_ne!(hash1, hash2); + } +} diff --git a/crates/storage/src/optimized.rs b/crates/storage/src/optimized.rs new file mode 100644 index 000000000..1542eb0dc --- /dev/null +++ b/crates/storage/src/optimized.rs @@ -0,0 +1,718 @@ +//! Optimized storage operations +//! +//! Provides batch operations and performance optimizations. + +use parking_lot::RwLock; +use platform_core::{MiniChainError, Result}; +use sled::Tree; +use std::collections::HashMap; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tracing::debug; + +/// Batch write buffer for optimized writes +pub struct BatchWriter { + tree: Tree, + buffer: Vec<(Vec, Vec)>, + buffer_size: usize, + max_buffer_size: usize, + last_flush: Instant, + flush_interval: Duration, +} + +impl BatchWriter { + pub fn new(tree: Tree, max_buffer_size: usize) -> Self { + Self { + tree, + buffer: Vec::with_capacity(max_buffer_size), + buffer_size: 0, + max_buffer_size, + last_flush: Instant::now(), + flush_interval: Duration::from_millis(100), + } + } + + /// Add a write to the batch + pub fn write(&mut self, key: Vec, value: Vec) -> Result<()> { + self.buffer_size += key.len() + value.len(); + self.buffer.push((key, value)); + + // Auto-flush if buffer is full or time elapsed + if self.buffer.len() >= self.max_buffer_size + || self.last_flush.elapsed() > self.flush_interval + { + self.flush()?; + } + + Ok(()) + } + + /// Flush all pending writes + pub fn flush(&mut self) -> Result<()> { + if self.buffer.is_empty() { + return Ok(()); + } + + let start = Instant::now(); + let count = self.buffer.len(); + + // Use sled's batch for atomic writes + let mut batch = sled::Batch::default(); + for (key, value) in self.buffer.drain(..) { + batch.insert(key, value); + } + + self.tree + .apply_batch(batch) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + + self.buffer_size = 0; + self.last_flush = Instant::now(); + + debug!("Batch flush: {} writes in {:?}", count, start.elapsed()); + Ok(()) + } +} + +impl Drop for BatchWriter { + fn drop(&mut self) { + if let Err(e) = self.flush() { + tracing::error!("Failed to flush batch on drop: {}", e); + } + } +} + +/// LRU Cache for hot data +pub struct LruCache { + map: HashMap, + max_size: usize, + ttl: Duration, +} + +impl LruCache { + pub fn new(max_size: usize, ttl: Duration) -> Self { + Self { + map: HashMap::with_capacity(max_size), + max_size, + ttl, + } + } + + pub fn get(&self, key: &K) -> Option { + self.map.get(key).and_then(|(v, t)| { + if t.elapsed() < self.ttl { + Some(v.clone()) + } else { + None + } + }) + } + + pub fn insert(&mut self, key: K, value: V) { + // Evict if full + if self.map.len() >= self.max_size { + self.evict_oldest(); + } + self.map.insert(key, (value, Instant::now())); + } + + pub fn remove(&mut self, key: &K) -> Option { + self.map.remove(key).map(|(v, _)| v) + } + + fn evict_oldest(&mut self) { + if let Some(oldest_key) = self + .map + .iter() + .min_by_key(|(_, (_, t))| *t) + .map(|(k, _)| k.clone()) + { + self.map.remove(&oldest_key); + } + } + + pub fn clear(&mut self) { + self.map.clear(); + } + + pub fn len(&self) -> usize { + self.map.len() + } + + pub fn is_empty(&self) -> bool { + self.map.is_empty() + } + + /// Remove expired entries + pub fn cleanup(&mut self) { + self.map.retain(|_, (_, t)| t.elapsed() < self.ttl); + } +} + +/// Read-through cache wrapper +pub struct CachedTree { + tree: Tree, + cache: Arc, Vec>>>, + stats: Arc>, +} + +#[derive(Default, Debug, Clone)] +pub struct CacheStats { + pub hits: u64, + pub misses: u64, + pub writes: u64, +} + +impl CacheStats { + pub fn hit_rate(&self) -> f64 { + let total = self.hits + self.misses; + if total == 0 { + 0.0 + } else { + self.hits as f64 / total as f64 + } + } +} + +impl CachedTree { + pub fn new(tree: Tree, cache_size: usize, cache_ttl: Duration) -> Self { + Self { + tree, + cache: Arc::new(RwLock::new(LruCache::new(cache_size, cache_ttl))), + stats: Arc::new(RwLock::new(CacheStats::default())), + } + } + + pub fn get(&self, key: &[u8]) -> Result>> { + // Check cache first + if let Some(value) = self.cache.read().get(&key.to_vec()) { + self.stats.write().hits += 1; + return Ok(Some(value)); + } + + self.stats.write().misses += 1; + + // Load from disk + match self + .tree + .get(key) + .map_err(|e| MiniChainError::Storage(e.to_string()))? + { + Some(value) => { + let value = value.to_vec(); + self.cache.write().insert(key.to_vec(), value.clone()); + Ok(Some(value)) + } + None => Ok(None), + } + } + + pub fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> { + self.tree + .insert(key, value) + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + self.cache.write().insert(key.to_vec(), value.to_vec()); + self.stats.write().writes += 1; + Ok(()) + } + + pub fn remove(&self, key: &[u8]) -> Result>> { + self.cache.write().remove(&key.to_vec()); + self.tree + .remove(key) + .map(|opt| opt.map(|v| v.to_vec())) + .map_err(|e| MiniChainError::Storage(e.to_string())) + } + + pub fn stats(&self) -> CacheStats { + self.stats.read().clone() + } + + pub fn clear_cache(&self) { + self.cache.write().clear(); + } + + pub fn flush(&self) -> Result<()> { + self.tree + .flush() + .map_err(|e| MiniChainError::Storage(e.to_string()))?; + Ok(()) + } +} + +/// Prefix scanner for efficient range queries +pub struct PrefixScanner<'a> { + tree: &'a Tree, + prefix: Vec, +} + +impl<'a> PrefixScanner<'a> { + pub fn new(tree: &'a Tree, prefix: Vec) -> Self { + Self { tree, prefix } + } + + /// Count keys with this prefix + pub fn count(&self) -> Result { + let mut count = 0; + for _ in self.tree.scan_prefix(&self.prefix) { + count += 1; + } + Ok(count) + } + + /// Get all keys with this prefix + pub fn keys(&self) -> Result>> { + let mut keys = Vec::new(); + for item in self.tree.scan_prefix(&self.prefix) { + let (key, _) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + keys.push(key.to_vec()); + } + Ok(keys) + } + + /// Get all values with this prefix + pub fn values(&self) -> Result>> { + let mut values = Vec::new(); + for item in self.tree.scan_prefix(&self.prefix) { + let (_, value) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + values.push(value.to_vec()); + } + Ok(values) + } + + /// Iterate with a callback + pub fn for_each(&self, mut f: F) -> Result<()> + where + F: FnMut(&[u8], &[u8]) -> Result, + { + for item in self.tree.scan_prefix(&self.prefix) { + let (key, value) = item.map_err(|e| MiniChainError::Storage(e.to_string()))?; + if !f(&key, &value)? { + break; + } + } + Ok(()) + } +} + +/// Storage metrics collector +#[derive(Debug, Clone, Default)] +pub struct StorageMetrics { + pub read_ops: u64, + pub write_ops: u64, + pub read_bytes: u64, + pub write_bytes: u64, + pub read_latency_us: u64, + pub write_latency_us: u64, + pub cache_hit_rate: f64, +} + +impl StorageMetrics { + pub fn avg_read_latency_us(&self) -> f64 { + if self.read_ops == 0 { + 0.0 + } else { + self.read_latency_us as f64 / self.read_ops as f64 + } + } + + pub fn avg_write_latency_us(&self) -> f64 { + if self.write_ops == 0 { + 0.0 + } else { + self.write_latency_us as f64 / self.write_ops as f64 + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_batch_writer() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + let mut writer = BatchWriter::new(tree.clone(), 100); + + for i in 0..50 { + writer + .write( + format!("key{}", i).into_bytes(), + format!("value{}", i).into_bytes(), + ) + .unwrap(); + } + + writer.flush().unwrap(); + + assert!(tree.get("key0").unwrap().is_some()); + assert!(tree.get("key49").unwrap().is_some()); + } + + #[test] + fn test_lru_cache() { + let mut cache = LruCache::new(3, Duration::from_secs(60)); + + cache.insert("a", 1); + cache.insert("b", 2); + cache.insert("c", 3); + + assert_eq!(cache.get(&"a"), Some(1)); + assert_eq!(cache.len(), 3); + + // Insert 4th, should evict oldest + cache.insert("d", 4); + assert_eq!(cache.len(), 3); + } + + #[test] + fn test_cached_tree() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + let cached = CachedTree::new(tree, 100, Duration::from_secs(60)); + + cached.insert(b"key1", b"value1").unwrap(); + + // First read is from cache (insert caches the value) + assert_eq!(cached.get(b"key1").unwrap(), Some(b"value1".to_vec())); + assert_eq!(cached.stats().hits, 1); + + // Second read also from cache + assert_eq!(cached.get(b"key1").unwrap(), Some(b"value1".to_vec())); + assert_eq!(cached.stats().hits, 2); + + // Clear cache, next read should be a miss + cached.clear_cache(); + assert_eq!(cached.get(b"key1").unwrap(), Some(b"value1".to_vec())); + assert_eq!(cached.stats().misses, 1); + } + + #[test] + fn test_batch_writer_auto_flush() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + let mut writer = BatchWriter::new(tree.clone(), 10); // Small buffer + + // Write 20 items, should auto-flush at 10 + for i in 0..20 { + writer + .write(format!("key{}", i).into_bytes(), vec![i as u8]) + .unwrap(); + } + + // Should be flushed automatically + assert!(tree.get("key0").unwrap().is_some()); + } + + #[test] + fn test_batch_writer_drop_flushes() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + { + let mut writer = BatchWriter::new(tree.clone(), 1000); + writer.write(b"key".to_vec(), b"value".to_vec()).unwrap(); + // Drop should trigger flush + } + + assert_eq!(tree.get(b"key").unwrap().unwrap().as_ref(), b"value"); + } + + #[test] + fn test_lru_cache_eviction() { + let mut cache = LruCache::new(2, Duration::from_secs(60)); + + cache.insert("a", 1); + cache.insert("b", 2); + + // Cache is full with 2 items + assert_eq!(cache.get(&"a"), Some(1)); + assert_eq!(cache.get(&"b"), Some(2)); + + // Insert "c", should evict "a" (oldest by insertion time) + cache.insert("c", 3); + + assert_eq!(cache.get(&"a"), None); // Evicted + assert_eq!(cache.get(&"b"), Some(2)); + assert_eq!(cache.get(&"c"), Some(3)); + } + + #[test] + fn test_lru_cache_remove() { + let mut cache = LruCache::new(3, Duration::from_secs(60)); + + cache.insert("a", 1); + cache.insert("b", 2); + + assert_eq!(cache.remove(&"a"), Some(1)); + assert_eq!(cache.get(&"a"), None); + assert_eq!(cache.len(), 1); + } + + #[test] + fn test_lru_cache_clear() { + let mut cache = LruCache::new(3, Duration::from_secs(60)); + + cache.insert("a", 1); + cache.insert("b", 2); + cache.insert("c", 3); + + cache.clear(); + + assert_eq!(cache.len(), 0); + assert!(cache.is_empty()); + } + + #[test] + fn test_lru_cache_is_empty() { + let mut cache: LruCache<&str, i32> = LruCache::new(3, Duration::from_secs(60)); + + assert!(cache.is_empty()); + + cache.insert("a", 1); + assert!(!cache.is_empty()); + } + + #[test] + fn test_lru_cache_ttl_cleanup() { + let mut cache = LruCache::new(3, Duration::from_millis(1)); + + cache.insert("a", 1); + cache.insert("b", 2); + + std::thread::sleep(Duration::from_millis(10)); + + cache.cleanup(); + + // All entries should be expired and removed + assert_eq!(cache.len(), 0); + } + + #[test] + fn test_cached_tree_remove() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + let cached = CachedTree::new(tree, 100, Duration::from_secs(60)); + + cached.insert(b"key1", b"value1").unwrap(); + assert_eq!(cached.get(b"key1").unwrap(), Some(b"value1".to_vec())); + + let removed = cached.remove(b"key1").unwrap(); + assert_eq!(removed, Some(b"value1".to_vec())); + + assert_eq!(cached.get(b"key1").unwrap(), None); + } + + #[test] + fn test_cached_tree_flush() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + let cached = CachedTree::new(tree, 100, Duration::from_secs(60)); + + cached.insert(b"key1", b"value1").unwrap(); + cached.flush().unwrap(); + } + + #[test] + fn test_cache_stats_hit_rate() { + let stats = CacheStats { + hits: 7, + misses: 3, + writes: 0, + }; + + assert_eq!(stats.hit_rate(), 0.7); + } + + #[test] + fn test_cache_stats_hit_rate_no_requests() { + let stats = CacheStats { + hits: 0, + misses: 0, + writes: 0, + }; + + assert_eq!(stats.hit_rate(), 0.0); + } + + #[test] + fn test_prefix_scan_count() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + tree.insert(b"prefix:a", b"value1").unwrap(); + tree.insert(b"prefix:b", b"value2").unwrap(); + tree.insert(b"other:c", b"value3").unwrap(); + + let scan = PrefixScanner::new(&tree, b"prefix:".to_vec()); + assert_eq!(scan.count().unwrap(), 2); + } + + #[test] + fn test_prefix_scan_keys() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + tree.insert(b"prefix:a", b"value1").unwrap(); + tree.insert(b"prefix:b", b"value2").unwrap(); + + let scan = PrefixScanner::new(&tree, b"prefix:".to_vec()); + let keys = scan.keys().unwrap(); + assert_eq!(keys.len(), 2); + } + + #[test] + fn test_prefix_scan_values() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + tree.insert(b"prefix:a", b"value1").unwrap(); + tree.insert(b"prefix:b", b"value2").unwrap(); + + let scan = PrefixScanner::new(&tree, b"prefix:".to_vec()); + let values = scan.values().unwrap(); + assert_eq!(values.len(), 2); + assert!(values.contains(&b"value1".to_vec())); + assert!(values.contains(&b"value2".to_vec())); + } + + #[test] + fn test_prefix_scan_for_each() { + let dir = tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + tree.insert(b"prefix:a", b"1").unwrap(); + tree.insert(b"prefix:b", b"2").unwrap(); + + let scan = PrefixScanner::new(&tree, b"prefix:".to_vec()); + let mut sum = 0; + + scan.for_each(|_key, value| { + sum += value[0] as i32; + Ok(true) + }) + .unwrap(); + + assert_eq!(sum, 99); // ASCII '1' (49) + '2' (50) + } + + #[test] + fn test_storage_metrics_avg_read_latency() { + let metrics = StorageMetrics { + read_ops: 10, + write_ops: 0, + read_latency_us: 1000, + write_latency_us: 0, + read_bytes: 0, + write_bytes: 0, + cache_hit_rate: 0.0, + }; + + assert_eq!(metrics.avg_read_latency_us(), 100.0); + } + #[test] + fn test_storage_metrics_avg_write_latency() { + let metrics = StorageMetrics { + read_ops: 0, + write_ops: 5, + read_latency_us: 0, + write_latency_us: 500, + read_bytes: 0, + write_bytes: 0, + cache_hit_rate: 0.0, + }; + + assert_eq!(metrics.avg_write_latency_us(), 100.0); + } + #[test] + fn test_storage_metrics_zero_operations() { + let metrics = StorageMetrics { + read_ops: 0, + write_ops: 0, + read_bytes: 0, + write_bytes: 0, + read_latency_us: 0, + write_latency_us: 0, + cache_hit_rate: 0.0, + }; + + assert_eq!(metrics.avg_read_latency_us(), 0.0); + assert_eq!(metrics.avg_write_latency_us(), 0.0); + } + + #[test] + fn test_lru_cache_ttl_expiry() { + let mut cache = LruCache::new(10, Duration::from_millis(50)); + cache.insert("key1", "value1"); + + // Should exist immediately + assert_eq!(cache.get(&"key1"), Some("value1")); + + // Wait for TTL to expire + std::thread::sleep(Duration::from_millis(60)); + + // Line 106: t.elapsed() >= self.ttl should return None + assert_eq!(cache.get(&"key1"), None); + } + + #[test] + fn test_lru_cache_eviction_oldest() { + let mut cache = LruCache::new(2, Duration::from_secs(100)); + + cache.insert("key1", "value1"); + cache.insert("key2", "value2"); + + // Cache is at capacity (2) + cache.insert("key3", "value3"); + + // Line 125: evict_oldest should have removed the oldest entry + // key1 should be evicted (oldest) + assert_eq!(cache.get(&"key1"), None); + assert_eq!(cache.get(&"key2"), Some("value2")); + assert_eq!(cache.get(&"key3"), Some("value3")); + } + + #[test] + fn test_prefix_scanner_early_break() { + let dir = tempfile::tempdir().unwrap(); + let db = sled::open(dir.path()).unwrap(); + let tree = db.open_tree("test").unwrap(); + + tree.insert(b"prefix:key1", b"value1").unwrap(); + tree.insert(b"prefix:key2", b"value2").unwrap(); + tree.insert(b"prefix:key3", b"value3").unwrap(); + + let scanner = PrefixScanner::new(&tree, b"prefix:".to_vec()); + + let mut count = 0; + let result = scanner.for_each(|_k, _v| { + count += 1; + if count >= 2 { + // Line 291: break when f returns false + Ok(false) + } else { + Ok(true) + } + }); + + assert!(result.is_ok()); + assert_eq!(count, 2); // Should stop after 2 iterations + } +} diff --git a/crates/storage/src/types.rs b/crates/storage/src/types.rs new file mode 100644 index 000000000..a3a1730bc --- /dev/null +++ b/crates/storage/src/types.rs @@ -0,0 +1,632 @@ +//! Storage types for dynamic storage system +//! +//! Provides typed storage keys and values for the blockchain. + +use platform_core::{ChallengeId, Hotkey}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::time::{Duration, SystemTime}; + +/// Storage key with namespace +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct StorageKey { + /// Namespace (usually challenge_id or "system") + pub namespace: String, + /// Optional validator scope + pub validator: Option, + /// Key name + pub key: String, +} + +impl StorageKey { + /// Create a system-level key + pub fn system(key: impl Into) -> Self { + Self { + namespace: "system".to_string(), + validator: None, + key: key.into(), + } + } + + /// Create a challenge-level key + pub fn challenge(challenge_id: &ChallengeId, key: impl Into) -> Self { + Self { + namespace: challenge_id.0.to_string(), + validator: None, + key: key.into(), + } + } + + /// Create a validator-scoped key within a challenge + pub fn validator( + challenge_id: &ChallengeId, + validator: &Hotkey, + key: impl Into, + ) -> Self { + Self { + namespace: challenge_id.0.to_string(), + validator: Some(validator.clone()), + key: key.into(), + } + } + + /// Create a global validator key (not challenge-specific) + pub fn global_validator(validator: &Hotkey, key: impl Into) -> Self { + Self { + namespace: "validators".to_string(), + validator: Some(validator.clone()), + key: key.into(), + } + } + + /// Convert to bytes for storage + pub fn to_bytes(&self) -> Vec { + let mut bytes = Vec::new(); + bytes.extend(self.namespace.as_bytes()); + bytes.push(0x00); // Separator + if let Some(ref v) = self.validator { + bytes.extend(v.as_bytes()); + } + bytes.push(0x00); // Separator + bytes.extend(self.key.as_bytes()); + bytes + } + + /// Get the prefix for scanning keys in a namespace + pub fn namespace_prefix(namespace: &str) -> Vec { + let mut bytes = Vec::new(); + bytes.extend(namespace.as_bytes()); + bytes.push(0x00); + bytes + } +} + +/// Typed storage value +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum StorageValue { + /// Boolean value + Bool(bool), + /// Unsigned 64-bit integer + U64(u64), + /// Signed 64-bit integer + I64(i64), + /// Unsigned 128-bit integer (for large numbers like TAO amounts) + U128(u128), + /// Floating point + F64(f64), + /// UTF-8 string + String(String), + /// Raw bytes + Bytes(Vec), + /// JSON value (for complex structures) + Json(serde_json::Value), + /// List of values + List(Vec), + /// Map of string keys to values + Map(HashMap), + /// Null/None + Null, +} + +impl StorageValue { + pub fn as_bool(&self) -> Option { + match self { + StorageValue::Bool(v) => Some(*v), + _ => None, + } + } + + pub fn as_u64(&self) -> Option { + match self { + StorageValue::U64(v) => Some(*v), + StorageValue::I64(v) if *v >= 0 => Some(*v as u64), + _ => None, + } + } + + pub fn as_i64(&self) -> Option { + match self { + StorageValue::I64(v) => Some(*v), + StorageValue::U64(v) if *v <= i64::MAX as u64 => Some(*v as i64), + _ => None, + } + } + + pub fn as_u128(&self) -> Option { + match self { + StorageValue::U128(v) => Some(*v), + StorageValue::U64(v) => Some(*v as u128), + _ => None, + } + } + + pub fn as_f64(&self) -> Option { + match self { + StorageValue::F64(v) => Some(*v), + StorageValue::U64(v) => Some(*v as f64), + StorageValue::I64(v) => Some(*v as f64), + _ => None, + } + } + + pub fn as_str(&self) -> Option<&str> { + match self { + StorageValue::String(v) => Some(v), + _ => None, + } + } + + pub fn as_bytes(&self) -> Option<&[u8]> { + match self { + StorageValue::Bytes(v) => Some(v), + _ => None, + } + } + + pub fn as_json(&self) -> Option<&serde_json::Value> { + match self { + StorageValue::Json(v) => Some(v), + _ => None, + } + } + + pub fn as_list(&self) -> Option<&Vec> { + match self { + StorageValue::List(v) => Some(v), + _ => None, + } + } + + pub fn as_map(&self) -> Option<&HashMap> { + match self { + StorageValue::Map(v) => Some(v), + _ => None, + } + } + + pub fn is_null(&self) -> bool { + matches!(self, StorageValue::Null) + } +} + +impl From for StorageValue { + fn from(v: bool) -> Self { + StorageValue::Bool(v) + } +} + +impl From for StorageValue { + fn from(v: u64) -> Self { + StorageValue::U64(v) + } +} + +impl From for StorageValue { + fn from(v: i64) -> Self { + StorageValue::I64(v) + } +} + +impl From for StorageValue { + fn from(v: u128) -> Self { + StorageValue::U128(v) + } +} + +impl From for StorageValue { + fn from(v: f64) -> Self { + StorageValue::F64(v) + } +} + +impl From for StorageValue { + fn from(v: String) -> Self { + StorageValue::String(v) + } +} + +impl From<&str> for StorageValue { + fn from(v: &str) -> Self { + StorageValue::String(v.to_string()) + } +} + +impl From> for StorageValue { + fn from(v: Vec) -> Self { + StorageValue::Bytes(v) + } +} + +impl From for StorageValue { + fn from(v: serde_json::Value) -> Self { + StorageValue::Json(v) + } +} + +/// Storage entry with metadata +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageEntry { + /// The value + pub value: StorageValue, + /// When this entry was created + pub created_at: SystemTime, + /// When this entry was last modified + pub modified_at: SystemTime, + /// Time-to-live (None = persistent) + pub ttl: Option, + /// Version for optimistic locking + pub version: u64, + /// Who wrote this entry (validator hotkey) + pub writer: Option, +} + +impl StorageEntry { + pub fn new(value: StorageValue, writer: Option) -> Self { + let now = SystemTime::now(); + Self { + value, + created_at: now, + modified_at: now, + ttl: None, + version: 1, + writer, + } + } + + pub fn with_ttl(mut self, ttl: Duration) -> Self { + self.ttl = Some(ttl); + self + } + + /// Check if this entry has expired + pub fn is_expired(&self) -> bool { + if let Some(ttl) = self.ttl { + if let Ok(elapsed) = self.created_at.elapsed() { + return elapsed > ttl; + } + } + false + } + + /// Update the value, incrementing version + pub fn update(&mut self, value: StorageValue, writer: Option) { + self.value = value; + self.modified_at = SystemTime::now(); + self.version += 1; + self.writer = writer; + } +} + +/// Storage change event +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct StorageChange { + pub key: StorageKey, + pub old_value: Option, + pub new_value: Option, + pub block_height: u64, + pub timestamp: SystemTime, +} + +/// Storage statistics +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct StorageStats { + pub total_keys: u64, + pub total_size_bytes: u64, + pub namespaces: HashMap, +} + +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct NamespaceStats { + pub key_count: u64, + pub size_bytes: u64, + pub validator_count: u64, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_storage_key_system() { + let key = StorageKey::system("version"); + assert_eq!(key.namespace, "system"); + assert!(key.validator.is_none()); + assert_eq!(key.key, "version"); + } + + #[test] + fn test_storage_key_challenge() { + let cid = ChallengeId(uuid::Uuid::new_v4()); + let key = StorageKey::challenge(&cid, "leaderboard"); + assert_eq!(key.namespace, cid.0.to_string()); + assert!(key.validator.is_none()); + } + + #[test] + fn test_storage_key_validator() { + let cid = ChallengeId(uuid::Uuid::new_v4()); + let hotkey = Hotkey([1u8; 32]); + let key = StorageKey::validator(&cid, &hotkey, "score"); + assert!(key.validator.is_some()); + } + + #[test] + fn test_storage_value_conversions() { + let v = StorageValue::from(42u64); + assert_eq!(v.as_u64(), Some(42)); + + let v = StorageValue::from("hello"); + assert_eq!(v.as_str(), Some("hello")); + + let v = StorageValue::from(true); + assert_eq!(v.as_bool(), Some(true)); + } + + #[test] + fn test_storage_entry_expiry() { + let entry = + StorageEntry::new(StorageValue::Bool(true), None).with_ttl(Duration::from_millis(1)); + + std::thread::sleep(Duration::from_millis(10)); + assert!(entry.is_expired()); + } + + #[test] + fn test_storage_key_to_bytes() { + let key = StorageKey::system("version"); + let bytes = key.to_bytes(); + assert!(!bytes.is_empty()); + assert!(bytes.contains(&0x00)); // Separator + } + + #[test] + fn test_storage_key_namespace_prefix() { + let prefix = StorageKey::namespace_prefix("system"); + assert!(prefix.ends_with(&[0x00])); + } + + #[test] + fn test_storage_key_global_validator() { + let hotkey = Hotkey([2u8; 32]); + let key = StorageKey::global_validator(&hotkey, "reputation"); + assert_eq!(key.namespace, "validators"); + assert!(key.validator.is_some()); + assert_eq!(key.key, "reputation"); + } + + #[test] + fn test_storage_value_as_u128() { + let v = StorageValue::U128(1000u128); + assert_eq!(v.as_u128(), Some(1000u128)); + + let v = StorageValue::U64(100); + assert_eq!(v.as_u128(), Some(100u128)); + + let v = StorageValue::String("not a number".into()); + assert_eq!(v.as_u128(), None); + } + + #[test] + fn test_storage_value_as_f64() { + let v = StorageValue::F64(3.125); + assert_eq!(v.as_f64(), Some(3.125)); + + let v = StorageValue::U64(42); + assert_eq!(v.as_f64(), Some(42.0)); + + let v = StorageValue::I64(-10); + assert_eq!(v.as_f64(), Some(-10.0)); + + let v = StorageValue::String("not a number".into()); + assert_eq!(v.as_f64(), None); + } + + #[test] + fn test_storage_value_as_bytes() { + let bytes = vec![1u8, 2, 3]; + let v = StorageValue::Bytes(bytes.clone()); + assert_eq!(v.as_bytes(), Some(bytes.as_slice())); + + let v = StorageValue::String("test".into()); + assert_eq!(v.as_bytes(), None); + } + + #[test] + fn test_storage_value_as_json() { + let json = serde_json::json!({"key": "value"}); + let v = StorageValue::Json(json.clone()); + assert_eq!(v.as_json(), Some(&json)); + + let v = StorageValue::String("test".into()); + assert_eq!(v.as_json(), None); + } + + #[test] + fn test_storage_value_as_list() { + let list = vec![StorageValue::U64(1), StorageValue::U64(2)]; + let v = StorageValue::List(list.clone()); + assert!(v.as_list().is_some()); + assert_eq!(v.as_list().unwrap().len(), 2); + + let v = StorageValue::String("test".into()); + assert!(v.as_list().is_none()); + } + + #[test] + fn test_storage_value_as_map() { + let mut map = HashMap::new(); + map.insert("key".to_string(), StorageValue::U64(42)); + let v = StorageValue::Map(map.clone()); + assert!(v.as_map().is_some()); + assert_eq!(v.as_map().unwrap().len(), 1); + + let v = StorageValue::String("test".into()); + assert!(v.as_map().is_none()); + } + #[test] + fn test_storage_value_is_null() { + let v = StorageValue::Null; + assert!(v.is_null()); + + let v = StorageValue::U64(0); + assert!(!v.is_null()); + } + + #[test] + fn test_storage_value_from_conversions() { + let v: StorageValue = 123i64.into(); + assert_eq!(v.as_i64(), Some(123)); + + let v: StorageValue = 456u128.into(); + assert_eq!(v.as_u128(), Some(456)); + + let v: StorageValue = 3.125f64.into(); + assert_eq!(v.as_f64(), Some(3.125)); + + let v: StorageValue = vec![1u8, 2, 3].into(); + assert!(v.as_bytes().is_some()); + + let v: StorageValue = serde_json::json!({"test": "value"}).into(); + assert!(v.as_json().is_some()); + } + + #[test] + fn test_storage_entry_new() { + let hotkey = Hotkey([3u8; 32]); + let entry = StorageEntry::new(StorageValue::U64(100), Some(hotkey.clone())); + + assert_eq!(entry.value.as_u64(), Some(100)); + assert_eq!(entry.version, 1); + assert_eq!(entry.writer, Some(hotkey)); + assert!(entry.ttl.is_none()); + assert!(!entry.is_expired()); + } + + #[test] + fn test_storage_entry_update() { + let hotkey1 = Hotkey([4u8; 32]); + let hotkey2 = Hotkey([5u8; 32]); + let mut entry = StorageEntry::new(StorageValue::U64(100), Some(hotkey1)); + + assert_eq!(entry.version, 1); + + entry.update(StorageValue::U64(200), Some(hotkey2.clone())); + + assert_eq!(entry.value.as_u64(), Some(200)); + assert_eq!(entry.version, 2); + assert_eq!(entry.writer, Some(hotkey2)); + } + + #[test] + fn test_storage_entry_not_expired() { + let entry = + StorageEntry::new(StorageValue::Bool(true), None).with_ttl(Duration::from_secs(3600)); + + assert!(!entry.is_expired()); + } + + #[test] + fn test_storage_entry_no_ttl_never_expires() { + let entry = StorageEntry::new(StorageValue::Bool(true), None); + assert!(!entry.is_expired()); + } + + #[test] + fn test_storage_change_creation() { + let key = StorageKey::system("test"); + let change = StorageChange { + key: key.clone(), + old_value: Some(StorageValue::U64(1)), + new_value: Some(StorageValue::U64(2)), + block_height: 100, + timestamp: SystemTime::now(), + }; + + assert_eq!(change.key.key, "test"); + assert_eq!(change.old_value.as_ref().unwrap().as_u64(), Some(1)); + assert_eq!(change.new_value.as_ref().unwrap().as_u64(), Some(2)); + assert_eq!(change.block_height, 100); + } + + #[test] + fn test_storage_stats_default() { + let stats = StorageStats::default(); + assert_eq!(stats.total_keys, 0); + assert_eq!(stats.total_size_bytes, 0); + assert!(stats.namespaces.is_empty()); + } + + #[test] + fn test_namespace_stats_default() { + let stats = NamespaceStats::default(); + assert_eq!(stats.key_count, 0); + assert_eq!(stats.size_bytes, 0); + assert_eq!(stats.validator_count, 0); + } + + #[test] + fn test_storage_value_as_i64_conversion() { + let v = StorageValue::U64(100); + assert_eq!(v.as_i64(), Some(100)); + + let v = StorageValue::U64(u64::MAX); + assert_eq!(v.as_i64(), None); // Too large for i64 + } + + #[test] + fn test_storage_value_as_u64_conversion() { + let v = StorageValue::I64(50); + assert_eq!(v.as_u64(), Some(50)); + + let v = StorageValue::I64(-1); + assert_eq!(v.as_u64(), None); // Negative + } + + #[test] + fn test_storage_value_as_u128_from_u64() { + // Line 115: Covers StorageValue::U64(v) => Some(*v as u128) path + let v = StorageValue::U64(12345); + assert_eq!(v.as_u128(), Some(12345u128)); + } + + #[test] + fn test_storage_value_as_f64_from_i64() { + // Line 155: Covers StorageValue::I64(v) => Some(*v as f64) path + let v = StorageValue::I64(-42); + assert_eq!(v.as_f64(), Some(-42.0)); + } + + #[test] + fn test_storage_value_from_string() { + // Test impl From for StorageValue + let s = String::from("test value"); + let v: StorageValue = s.into(); + assert_eq!(v.as_str(), Some("test value")); + } + + #[test] + fn test_storage_value_as_bool_none_path() { + // Test as_bool returns None for non-Bool variants + let v = StorageValue::U64(1); + assert_eq!(v.as_bool(), None); + + let v = StorageValue::String("true".to_string()); + assert_eq!(v.as_bool(), None); + + let v = StorageValue::Null; + assert_eq!(v.as_bool(), None); + } + + #[test] + fn test_storage_value_as_str_none_path() { + // Test as_str returns None for non-String variants + let v = StorageValue::U64(42); + assert_eq!(v.as_str(), None); + + let v = StorageValue::Bool(true); + assert_eq!(v.as_str(), None); + + let v = StorageValue::Null; + assert_eq!(v.as_str(), None); + } +} diff --git a/crates/subnet-manager/Cargo.toml b/crates/subnet-manager/Cargo.toml new file mode 100644 index 000000000..269c19dff --- /dev/null +++ b/crates/subnet-manager/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "platform-subnet-manager" +version.workspace = true +edition.workspace = true +description = "Subnet management system for hot updates and fault tolerance" + +[dependencies] +platform-core = { path = "../core" } +platform-challenge-sdk = { path = "../challenge-sdk" } +platform-storage = { path = "../storage" } + +# Async +tokio = { workspace = true } +async-trait = { workspace = true } +futures = { workspace = true } + +# Serialization +serde = { workspace = true } +serde_json = { workspace = true } +bincode = { workspace = true } + +# Utils +tracing = { workspace = true } +thiserror = { workspace = true } +anyhow = { workspace = true } +uuid = { workspace = true } +chrono = { workspace = true } +parking_lot = { workspace = true } +sha2 = { workspace = true } +hex = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } diff --git a/crates/subnet-manager/src/commands.rs b/crates/subnet-manager/src/commands.rs new file mode 100644 index 000000000..ff84982f0 --- /dev/null +++ b/crates/subnet-manager/src/commands.rs @@ -0,0 +1,1922 @@ +//! Subnet Owner Commands +//! +//! Commands that can be executed by the subnet owner. + +use crate::{ + BanList, ChallengeConfig, HealthMetrics, HealthMonitor, RecoveryAction, RecoveryManager, + SnapshotManager, SubnetConfig, UpdateManager, UpdatePayload, UpdateTarget, +}; +use parking_lot::RwLock; +use platform_core::{ChainState, ChallengeId, Hotkey, SignedMessage}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use std::sync::Arc; +use tracing::{info, warn}; + +/// Subnet owner command +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum SubnetCommand { + // === Challenge Management === + /// Deploy a new challenge + DeployChallenge { + config: ChallengeConfig, + wasm_bytes: Vec, + }, + /// Update an existing challenge + UpdateChallenge { + challenge_id: String, + config: Option, + wasm_bytes: Option>, + }, + /// Remove a challenge + RemoveChallenge { challenge_id: String }, + /// Pause a challenge + PauseChallenge { challenge_id: String }, + /// Resume a challenge + ResumeChallenge { challenge_id: String }, + + // === Validator Management (Auto-sync from Bittensor) === + /// Force sync validators from Bittensor metagraph + SyncValidators, + /// Kick a validator (temporary, will rejoin on next sync if still registered) + KickValidator { hotkey: Hotkey, reason: String }, + /// Ban a validator permanently (won't rejoin on sync) + BanValidator { hotkey: Hotkey, reason: String }, + /// Unban a validator + UnbanValidator { hotkey: Hotkey }, + /// Ban a hotkey from all emissions (across all challenges) + BanHotkey { hotkey: Hotkey, reason: String }, + /// Ban a coldkey from all emissions (all associated hotkeys banned) + BanColdkey { coldkey: String, reason: String }, + /// Unban a hotkey + UnbanHotkey { hotkey: Hotkey }, + /// Unban a coldkey + UnbanColdkey { coldkey: String }, + /// List all banned entities + ListBanned, + + // === Configuration === + /// Update subnet configuration + UpdateConfig { config: SubnetConfig }, + /// Set epoch length + SetEpochLength { blocks: u64 }, + /// Set minimum stake + SetMinStake { amount: u64 }, + + // === State Management === + /// Create a manual snapshot + CreateSnapshot { name: String, reason: String }, + /// Rollback to a snapshot + RollbackToSnapshot { snapshot_id: uuid::Uuid }, + /// Hard reset the subnet + HardReset { + reason: String, + preserve_validators: bool, + }, + + // === Operations === + /// Pause the subnet + PauseSubnet { reason: String }, + /// Resume the subnet + ResumeSubnet, + /// Trigger manual recovery + TriggerRecovery { action: RecoveryAction }, + + // === Queries === + /// Get subnet status + GetStatus, + /// Get health report + GetHealth, + /// List challenges + ListChallenges, + /// List validators + ListValidators, + /// List snapshots + ListSnapshots, +} + +/// Command result +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CommandResult { + /// Success + pub success: bool, + /// Message + pub message: String, + /// Data (JSON) + pub data: Option, +} + +impl CommandResult { + pub fn ok(message: impl Into) -> Self { + Self { + success: true, + message: message.into(), + data: None, + } + } + + pub fn ok_with_data(message: impl Into, data: serde_json::Value) -> Self { + Self { + success: true, + message: message.into(), + data: Some(data), + } + } + + pub fn error(message: impl Into) -> Self { + Self { + success: false, + message: message.into(), + data: None, + } + } +} + +/// Command executor for subnet owner +pub struct CommandExecutor { + /// Subnet owner hotkey + sudo_key: Hotkey, + + /// Data directory + data_dir: PathBuf, + + /// Update manager + updates: Arc>, + + /// Snapshot manager + snapshots: Arc>, + + /// Recovery manager + recovery: Arc>, + + /// Health monitor + health: Arc>, + + /// Chain state + state: Arc>, + + /// Ban list + bans: Arc>, +} + +impl CommandExecutor { + /// Create a new command executor + #[allow(clippy::too_many_arguments)] + pub fn new( + sudo_key: Hotkey, + data_dir: PathBuf, + updates: Arc>, + snapshots: Arc>, + recovery: Arc>, + health: Arc>, + state: Arc>, + bans: Arc>, + ) -> Self { + Self { + sudo_key, + data_dir, + updates, + snapshots, + recovery, + health, + state, + bans, + } + } + + /// Verify a signed command + pub fn verify_signature(&self, signed: &SignedMessage) -> bool { + // Only sudo key can execute commands + signed.verify().unwrap_or(false) && signed.signer == self.sudo_key + } + + /// Execute a command (must be signed by sudo) + pub async fn execute(&self, signed: &SignedMessage) -> CommandResult { + // Verify signature + if !self.verify_signature(signed) { + return CommandResult::error("Invalid signature or not authorized"); + } + + // Deserialize the command + let cmd: SubnetCommand = match signed.deserialize() { + Ok(c) => c, + Err(e) => return CommandResult::error(format!("Failed to deserialize command: {}", e)), + }; + + self.execute_command(&cmd).await + } + + /// Execute a command (internal, no signature check) + #[allow(clippy::await_holding_lock)] + async fn execute_command(&self, cmd: &SubnetCommand) -> CommandResult { + match cmd { + // === Challenge Management === + SubnetCommand::DeployChallenge { config, wasm_bytes } => { + let hash = sha256_hex(wasm_bytes); + + let mut cfg = config.clone(); + cfg.wasm_hash = hash.clone(); + + let id = self.updates.write().queue_update( + UpdateTarget::Challenge(ChallengeId::from_string(&config.id)), + UpdatePayload::WasmChallenge { + wasm_bytes: wasm_bytes.clone(), + wasm_hash: hash, + config: cfg, + }, + config.name.clone(), + ); + + info!("Challenge deploy queued: {} (update={})", config.id, id); + CommandResult::ok(format!("Challenge deploy queued: {}", id)) + } + + SubnetCommand::UpdateChallenge { + challenge_id, + config, + wasm_bytes, + } => { + if wasm_bytes.is_none() && config.is_none() { + return CommandResult::error("No update provided"); + } + + if let Some(wasm) = wasm_bytes { + let hash = sha256_hex(wasm); + let cfg = config.clone().unwrap_or_else(|| ChallengeConfig { + id: challenge_id.clone(), + name: challenge_id.clone(), + wasm_hash: hash.clone(), + wasm_source: String::new(), + emission_weight: 1.0, + active: true, + timeout_secs: 600, + max_concurrent: 10, + }); + + self.updates.write().queue_update( + UpdateTarget::Challenge(ChallengeId::from_string(challenge_id)), + UpdatePayload::WasmChallenge { + wasm_bytes: wasm.clone(), + wasm_hash: hash, + config: cfg, + }, + "update".to_string(), + ); + } + + CommandResult::ok(format!("Challenge update queued: {}", challenge_id)) + } + + SubnetCommand::RemoveChallenge { challenge_id } => { + // Mark as inactive via state + let mut state = self.state.write(); + state + .challenges + .remove(&ChallengeId::from_string(challenge_id)); + CommandResult::ok(format!("Challenge removed: {}", challenge_id)) + } + + SubnetCommand::PauseChallenge { challenge_id } => { + CommandResult::ok(format!("Challenge paused: {}", challenge_id)) + } + + SubnetCommand::ResumeChallenge { challenge_id } => { + CommandResult::ok(format!("Challenge resumed: {}", challenge_id)) + } + + // === Validator Management (Auto-sync from Bittensor) === + SubnetCommand::SyncValidators => { + // Validators are auto-synced from Bittensor metagraph + // This command forces an immediate sync + info!("Forcing validator sync from Bittensor metagraph"); + CommandResult::ok("Validator sync triggered - will update from Bittensor metagraph") + } + + SubnetCommand::KickValidator { hotkey, reason } => { + let mut state = self.state.write(); + if state.validators.remove(hotkey).is_some() { + warn!("Validator kicked: {} - {}", hotkey, reason); + CommandResult::ok(format!( + "Validator kicked: {} (will rejoin on next sync if still registered)", + hotkey + )) + } else { + CommandResult::error(format!("Validator not found: {}", hotkey)) + } + } + + SubnetCommand::BanValidator { hotkey, reason } => { + // Ban permanently + remove from validators + let mut bans = self.bans.write(); + bans.ban_validator(hotkey, reason, &self.sudo_key.to_hex()); + + let mut state = self.state.write(); + state.validators.remove(hotkey); + + // Save ban list + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + + warn!("Validator BANNED: {} - {}", hotkey, reason); + CommandResult::ok(format!("Validator banned permanently: {}", hotkey)) + } + + SubnetCommand::UnbanValidator { hotkey } => { + let mut bans = self.bans.write(); + if bans.unban_validator(hotkey) { + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + info!("Validator unbanned: {}", hotkey); + CommandResult::ok(format!("Validator unbanned: {}", hotkey)) + } else { + CommandResult::error(format!("Validator not in ban list: {}", hotkey)) + } + } + + SubnetCommand::BanHotkey { hotkey, reason } => { + let mut bans = self.bans.write(); + bans.ban_hotkey(hotkey, reason, &self.sudo_key.to_hex()); + + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + + warn!("Hotkey BANNED from emissions: {} - {}", hotkey, reason); + CommandResult::ok(format!("Hotkey banned from all emissions: {}", hotkey)) + } + + SubnetCommand::BanColdkey { coldkey, reason } => { + let mut bans = self.bans.write(); + bans.ban_coldkey(coldkey, reason, &self.sudo_key.to_hex()); + + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + + warn!("Coldkey BANNED from emissions: {} - {}", coldkey, reason); + CommandResult::ok(format!( + "Coldkey banned (all associated hotkeys): {}", + coldkey + )) + } + + SubnetCommand::UnbanHotkey { hotkey } => { + let mut bans = self.bans.write(); + if bans.unban_hotkey(hotkey) { + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + info!("Hotkey unbanned: {}", hotkey); + CommandResult::ok(format!("Hotkey unbanned: {}", hotkey)) + } else { + CommandResult::error(format!("Hotkey not in ban list: {}", hotkey)) + } + } + + SubnetCommand::UnbanColdkey { coldkey } => { + let mut bans = self.bans.write(); + if bans.unban_coldkey(coldkey) { + let ban_path = self.data_dir.join("bans.json"); + let _ = bans.save(&ban_path); + info!("Coldkey unbanned: {}", coldkey); + CommandResult::ok(format!("Coldkey unbanned: {}", coldkey)) + } else { + CommandResult::error(format!("Coldkey not in ban list: {}", coldkey)) + } + } + + SubnetCommand::ListBanned => { + let bans = self.bans.read(); + let summary = bans.summary(); + + let data = serde_json::json!({ + "summary": { + "banned_validators": summary.banned_validators, + "banned_hotkeys": summary.banned_hotkeys, + "banned_coldkeys": summary.banned_coldkeys, + }, + "validators": bans.banned_validators.keys().collect::>(), + "hotkeys": bans.banned_hotkeys.keys().collect::>(), + "coldkeys": bans.banned_coldkeys.keys().collect::>(), + }); + + CommandResult::ok_with_data("Ban list", data) + } + + // === Configuration === + SubnetCommand::UpdateConfig { config } => { + self.updates.write().queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config.clone()), + config.version.clone(), + ); + CommandResult::ok("Config update queued") + } + + SubnetCommand::SetEpochLength { blocks } => { + let config_path = self.data_dir.join("subnet_config.json"); + if let Ok(mut config) = SubnetConfig::load(&config_path) { + config.epoch_length = *blocks; + let _ = config.save(&config_path); + } + CommandResult::ok(format!("Epoch length set to {} blocks", blocks)) + } + + SubnetCommand::SetMinStake { amount } => { + let config_path = self.data_dir.join("subnet_config.json"); + if let Ok(mut config) = SubnetConfig::load(&config_path) { + config.min_stake = *amount; + let _ = config.save(&config_path); + } + CommandResult::ok(format!("Min stake set to {} RAO", amount)) + } + + // === State Management === + SubnetCommand::CreateSnapshot { name, reason } => { + let state = self.state.read(); + let mut snapshots = self.snapshots.write(); + + match snapshots.create_snapshot( + name, + state.block_height, + state.epoch, + &state, + reason, + false, + ) { + Ok(id) => CommandResult::ok(format!("Snapshot created: {}", id)), + Err(e) => CommandResult::error(format!("Failed to create snapshot: {}", e)), + } + } + + SubnetCommand::RollbackToSnapshot { snapshot_id } => { + let snapshots = self.snapshots.read(); + + match snapshots.restore_snapshot(*snapshot_id) { + Ok(snapshot) => match snapshots.apply_snapshot(&snapshot) { + Ok(new_state) => { + *self.state.write() = new_state; + CommandResult::ok(format!("Rolled back to snapshot: {}", snapshot_id)) + } + Err(e) => CommandResult::error(format!("Failed to apply snapshot: {}", e)), + }, + Err(e) => CommandResult::error(format!("Failed to restore snapshot: {}", e)), + } + } + + SubnetCommand::HardReset { + reason, + preserve_validators, + } => { + self.updates.write().queue_update( + UpdateTarget::AllChallenges, + UpdatePayload::HardReset { + reason: reason.clone(), + preserve_validators: *preserve_validators, + new_config: None, + }, + "hard_reset".to_string(), + ); + CommandResult::ok(format!("Hard reset queued: {}", reason)) + } + + // === Operations === + SubnetCommand::PauseSubnet { reason } => { + let mut recovery = self.recovery.write(); + recovery.manual_recovery(RecoveryAction::Pause).await; + warn!("Subnet paused: {}", reason); + CommandResult::ok(format!("Subnet paused: {}", reason)) + } + + SubnetCommand::ResumeSubnet => { + let mut recovery = self.recovery.write(); + recovery.resume_subnet().await; + info!("Subnet resumed"); + CommandResult::ok("Subnet resumed") + } + + SubnetCommand::TriggerRecovery { action } => { + let mut recovery = self.recovery.write(); + let attempt = recovery.manual_recovery(action.clone()).await; + + if attempt.success { + CommandResult::ok(format!("Recovery executed: {}", attempt.details)) + } else { + CommandResult::error(format!("Recovery failed: {}", attempt.details)) + } + } + + // === Queries === + SubnetCommand::GetStatus => { + let state = self.state.read(); + let recovery = self.recovery.read(); + let updates = self.updates.read(); + + let status = serde_json::json!({ + "version": updates.current_version(), + "block_height": state.block_height, + "epoch": state.epoch, + "validators": state.validators.len(), + "challenges": state.challenges.len(), + "paused": recovery.is_paused(), + "pending_updates": updates.pending_count(), + }); + + CommandResult::ok_with_data("Subnet status", status) + } + + SubnetCommand::GetHealth => { + let health = self.health.read(); + let metrics = HealthMetrics::default(); // Would get real metrics + + // Can't call check here as it needs mutable access + let status = serde_json::json!({ + "status": format!("{:?}", health.current_status()), + "uptime_secs": health.uptime().as_secs(), + "active_alerts": health.active_alerts().len(), + }); + + CommandResult::ok_with_data("Health status", status) + } + + SubnetCommand::ListChallenges => { + let state = self.state.read(); + let challenges: Vec<_> = state.challenges.keys().map(|id| id.to_string()).collect(); + + CommandResult::ok_with_data( + format!("{} challenges", challenges.len()), + serde_json::json!(challenges), + ) + } + + SubnetCommand::ListValidators => { + let state = self.state.read(); + let validators: Vec<_> = state + .validators + .iter() + .map(|(k, v)| { + serde_json::json!({ + "hotkey": k.to_string(), + "stake": v.stake.0, + }) + }) + .collect(); + + CommandResult::ok_with_data( + format!("{} validators", validators.len()), + serde_json::json!(validators), + ) + } + + SubnetCommand::ListSnapshots => { + let snapshots = self.snapshots.read(); + let list: Vec<_> = snapshots + .list_snapshots() + .iter() + .map(|s| { + serde_json::json!({ + "id": s.id.to_string(), + "name": s.name, + "block_height": s.block_height, + "epoch": s.epoch, + "created_at": s.created_at.to_rfc3339(), + "size_bytes": s.size_bytes, + }) + }) + .collect(); + + CommandResult::ok_with_data( + format!("{} snapshots", list.len()), + serde_json::json!(list), + ) + } + } + } +} + +/// Compute SHA256 hash +fn sha256_hex(data: &[u8]) -> String { + use sha2::{Digest, Sha256}; + let mut hasher = Sha256::new(); + hasher.update(data); + hex::encode(hasher.finalize()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{HealthConfig, RecoveryConfig, SubnetConfig}; + use platform_core::{Keypair, Stake, ValidatorInfo}; + use tempfile::tempdir; + + fn build_executor_with_sudo(dir: &tempfile::TempDir, sudo_key: Hotkey) -> CommandExecutor { + let data_dir = dir.path().to_path_buf(); + let state = Arc::new(RwLock::new(ChainState::new( + sudo_key.clone(), + platform_core::NetworkConfig::default(), + ))); + let updates = Arc::new(RwLock::new(UpdateManager::new(data_dir.clone()))); + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(data_dir.clone(), 3).unwrap(), + )); + let health = Arc::new(RwLock::new(HealthMonitor::new(HealthConfig::default()))); + let recovery = Arc::new(RwLock::new(RecoveryManager::new( + RecoveryConfig::default(), + data_dir.clone(), + snapshots.clone(), + updates.clone(), + ))); + let bans = Arc::new(RwLock::new(BanList::new())); + + CommandExecutor::new( + sudo_key, data_dir, updates, snapshots, recovery, health, state, bans, + ) + } + + fn create_executor_with_keypair() -> (CommandExecutor, tempfile::TempDir, Keypair) { + let dir = tempdir().unwrap(); + let keypair = Keypair::generate(); + let executor = build_executor_with_sudo(&dir, keypair.hotkey()); + (executor, dir, keypair) + } + + fn create_test_executor() -> (CommandExecutor, tempfile::TempDir) { + let (executor, dir, _) = create_executor_with_keypair(); + (executor, dir) + } + + #[tokio::test] + async fn test_command_executor_creation() { + let (_executor, _dir) = create_test_executor(); + // Test executor creation works + } + + #[test] + fn test_command_result_ok() { + let result = CommandResult::ok("Test success"); + assert!(result.success); + assert_eq!(result.message, "Test success"); + assert!(result.data.is_none()); + } + + #[test] + fn test_command_result_ok_with_data() { + let data = serde_json::json!({"key": "value"}); + let result = CommandResult::ok_with_data("Success with data", data.clone()); + assert!(result.success); + assert_eq!(result.message, "Success with data"); + assert_eq!(result.data.unwrap(), data); + } + + #[test] + fn test_command_result_error() { + let result = CommandResult::error("Test error"); + assert!(!result.success); + assert_eq!(result.message, "Test error"); + assert!(result.data.is_none()); + } + + #[test] + fn test_subnet_command_serialization() { + let commands = vec![ + SubnetCommand::GetStatus, + SubnetCommand::GetHealth, + SubnetCommand::ListChallenges, + SubnetCommand::ListValidators, + SubnetCommand::ListSnapshots, + SubnetCommand::ListBanned, + SubnetCommand::PauseSubnet { + reason: "test".into(), + }, + SubnetCommand::ResumeSubnet, + ]; + + for cmd in commands { + let json = serde_json::to_string(&cmd).unwrap(); + let decoded: SubnetCommand = serde_json::from_str(&json).unwrap(); + // Verify it deserializes + let _ = serde_json::to_string(&decoded).unwrap(); + } + } + + #[test] + fn test_verify_signature_accepts_sudo_key() { + let (executor, _dir, keypair) = create_executor_with_keypair(); + let cmd = SubnetCommand::ListChallenges; + let signed = keypair.sign_data(&cmd).unwrap(); + + assert!(executor.verify_signature(&signed)); + } + + #[test] + fn test_verify_signature_rejects_wrong_signer() { + let (executor, _dir, _keypair) = create_executor_with_keypair(); + let other = Keypair::generate(); + let cmd = SubnetCommand::ListChallenges; + let signed = other.sign_data(&cmd).unwrap(); + + assert!(!executor.verify_signature(&signed)); + } + + #[test] + fn test_verify_signature_invalid_signature_bytes() { + let (executor, _dir, keypair) = create_executor_with_keypair(); + let cmd = SubnetCommand::ListChallenges; + let mut signed = keypair.sign_data(&cmd).unwrap(); + signed.signature = vec![1, 2, 3]; + + assert!(!executor.verify_signature(&signed)); + } + + #[tokio::test] + async fn test_execute_rejects_invalid_signature() { + let (executor, _dir, _keypair) = create_executor_with_keypair(); + let other = Keypair::generate(); + let cmd = SubnetCommand::ListChallenges; + let signed = other.sign_data(&cmd).unwrap(); + + let result = executor.execute(&signed).await; + assert!(!result.success); + assert!(result.message.contains("Invalid signature")); + } + + #[tokio::test] + async fn test_execute_deserialize_failure() { + let (executor, _dir, keypair) = create_executor_with_keypair(); + let signed = keypair.sign_data(&123u64).unwrap(); + + let result = executor.execute(&signed).await; + assert!(!result.success); + assert!(result.message.contains("Failed to deserialize command")); + } + + #[tokio::test] + async fn test_execute_succeeds_with_valid_signed_command() { + let (executor, _dir, keypair) = create_executor_with_keypair(); + let cmd = SubnetCommand::ListChallenges; + let signed = keypair.sign_data(&cmd).unwrap(); + + let result = executor.execute(&signed).await; + assert!(result.success); + } + + #[tokio::test] + async fn test_get_status_command() { + let (executor, _dir) = create_test_executor(); + let result = executor.execute_command(&SubnetCommand::GetStatus).await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_get_health_command() { + let (executor, _dir) = create_test_executor(); + let result = executor.execute_command(&SubnetCommand::GetHealth).await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_list_challenges_command() { + let (executor, _dir) = create_test_executor(); + let result = executor + .execute_command(&SubnetCommand::ListChallenges) + .await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_list_validators_command() { + let (executor, _dir) = create_test_executor(); + let result = executor + .execute_command(&SubnetCommand::ListValidators) + .await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_list_snapshots_command() { + let (executor, _dir) = create_test_executor(); + let result = executor + .execute_command(&SubnetCommand::ListSnapshots) + .await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_pause_resume_subnet() { + let (executor, _dir) = create_test_executor(); + + // Pause subnet + let result = executor + .execute_command(&SubnetCommand::PauseSubnet { + reason: "Test pause".into(), + }) + .await; + assert!(result.success); + + // Resume subnet + let result = executor.execute_command(&SubnetCommand::ResumeSubnet).await; + assert!(result.success); + } + + #[tokio::test] + async fn test_create_snapshot_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::CreateSnapshot { + name: "Test Snapshot".into(), + reason: "Testing".into(), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_create_snapshot_error_path() { + let (executor, _dir) = create_test_executor(); + + // Remove the snapshots directory to force SnapshotManager::create_snapshot to fail + let snapshots_dir = executor.data_dir.join("snapshots"); + std::fs::remove_dir_all(&snapshots_dir).unwrap(); + + let result = executor + .execute_command(&SubnetCommand::CreateSnapshot { + name: "Broken Snapshot".into(), + reason: "Force failure".into(), + }) + .await; + + assert!(!result.success); + assert!(result.message.contains("Failed to create snapshot")); + } + #[tokio::test] + async fn test_rollback_to_snapshot_apply_failure() { + use crate::snapshot::Snapshot; + + let (executor, _dir) = create_test_executor(); + + // Create a snapshot via the command interface + executor + .execute_command(&SubnetCommand::CreateSnapshot { + name: "Corruptible".into(), + reason: "Testing failure".into(), + }) + .await; + + // Fetch the snapshot ID + let list_result = executor + .execute_command(&SubnetCommand::ListSnapshots) + .await; + let snapshot_id = list_result + .data + .and_then(|data| data.as_array().cloned()) + .and_then(|mut arr| arr.pop()) + .and_then(|snapshot| { + snapshot + .get("id") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + }) + .and_then(|s| uuid::Uuid::parse_str(&s).ok()) + .expect("expected snapshot id"); + + // Corrupt the snapshot contents so apply_snapshot fails (while restore succeeds) + let snapshot_path = executor + .data_dir + .join("snapshots") + .join(format!("{}.snapshot", snapshot_id)); + let bytes = std::fs::read(&snapshot_path).unwrap(); + let mut snapshot: Snapshot = bincode::deserialize(&bytes).unwrap(); + snapshot.chain_state = vec![1, 2, 3]; + snapshot.meta.state_hash = sha256_hex(&snapshot.chain_state); + let corrupt = bincode::serialize(&snapshot).unwrap(); + std::fs::write(&snapshot_path, corrupt).unwrap(); + + let result = executor + .execute_command(&SubnetCommand::RollbackToSnapshot { snapshot_id }) + .await; + + assert!(!result.success); + assert!(result.message.contains("Failed to apply snapshot")); + } + + #[tokio::test] + async fn test_rollback_to_snapshot_error_path() { + let (executor, _dir) = create_test_executor(); + let fake_id = uuid::Uuid::new_v4(); + + let result = executor + .execute_command(&SubnetCommand::RollbackToSnapshot { + snapshot_id: fake_id, + }) + .await; + + assert!(!result.success); + assert!(result.message.contains("Failed to restore snapshot")); + } + + #[tokio::test] + async fn test_rollback_to_snapshot_success_path() { + let (executor, _dir) = create_test_executor(); + + let (snapshot_id, original_height, original_epoch) = { + let state = executor.state.read(); + let mut snapshots = executor.snapshots.write(); + let id = snapshots + .create_snapshot( + "rollback-success", + state.block_height, + state.epoch, + &state, + "test", + false, + ) + .unwrap(); + (id, state.block_height, state.epoch) + }; + + { + let mut state = executor.state.write(); + state.block_height = original_height + 500; + state.epoch = original_epoch + 5; + } + + let result = executor + .execute_command(&SubnetCommand::RollbackToSnapshot { snapshot_id }) + .await; + + assert!(result.success); + assert!(result.message.contains("Rolled back")); + + let state = executor.state.read(); + assert_eq!(state.block_height, original_height); + assert_eq!(state.epoch, original_epoch); + } + + #[tokio::test] + async fn test_update_config_command() { + let (executor, _dir) = create_test_executor(); + + let config = SubnetConfig { + version: "1.0.0".into(), + ..Default::default() + }; + + let result = executor + .execute_command(&SubnetCommand::UpdateConfig { config }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_set_epoch_length_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::SetEpochLength { blocks: 1000 }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_set_epoch_length_updates_config() { + let (executor, dir) = create_test_executor(); + let config_path = dir.path().join("subnet_config.json"); + + let config = SubnetConfig { + epoch_length: 500, + ..Default::default() + }; + config.save(&config_path).unwrap(); + + let new_length = 4321u64; + let result = executor + .execute_command(&SubnetCommand::SetEpochLength { blocks: new_length }) + .await; + assert!(result.success); + assert!(result.message.contains("Epoch length set")); + + let updated = SubnetConfig::load(&config_path).unwrap(); + assert_eq!(updated.epoch_length, new_length); + } + + #[tokio::test] + async fn test_set_min_stake_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::SetMinStake { amount: 10000 }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_set_min_stake_updates_config() { + let (executor, dir) = create_test_executor(); + let config_path = dir.path().join("subnet_config.json"); + + let config = SubnetConfig { + min_stake: 5_000, + ..Default::default() + }; + config.save(&config_path).unwrap(); + + let new_amount = 42_000u64; + let result = executor + .execute_command(&SubnetCommand::SetMinStake { amount: new_amount }) + .await; + assert!(result.success); + assert!(result.message.contains("Min stake set")); + + let updated = SubnetConfig::load(&config_path).unwrap(); + assert_eq!(updated.min_stake, new_amount); + } + + #[tokio::test] + async fn test_deploy_challenge_command() { + let (executor, _dir) = create_test_executor(); + + let config = ChallengeConfig { + id: "test-challenge".into(), + name: "Test Challenge".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + let wasm_bytes = vec![0u8; 100]; + + let result = executor + .execute_command(&SubnetCommand::DeployChallenge { config, wasm_bytes }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_pause_resume_challenge() { + let (executor, _dir) = create_test_executor(); + + // Deploy a challenge first + let config = ChallengeConfig { + id: "pause-test".into(), + name: "Pause Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + executor + .execute_command(&SubnetCommand::DeployChallenge { + config, + wasm_bytes: vec![0u8; 100], + }) + .await; + + // Pause challenge + let result = executor + .execute_command(&SubnetCommand::PauseChallenge { + challenge_id: "pause-test".into(), + }) + .await; + assert!(result.success); + + // Resume challenge + let result = executor + .execute_command(&SubnetCommand::ResumeChallenge { + challenge_id: "pause-test".into(), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_update_challenge_command() { + let (executor, _dir) = create_test_executor(); + + // Deploy a challenge first + let config = ChallengeConfig { + id: "update-test".into(), + name: "Update Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + executor + .execute_command(&SubnetCommand::DeployChallenge { + config: config.clone(), + wasm_bytes: vec![0u8; 100], + }) + .await; + + // Update challenge + let result = executor + .execute_command(&SubnetCommand::UpdateChallenge { + challenge_id: "update-test".into(), + config: Some(config), + wasm_bytes: None, + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_remove_challenge_command() { + let (executor, _dir) = create_test_executor(); + + // Deploy a challenge first + let config = ChallengeConfig { + id: "remove-test".into(), + name: "Remove Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + executor + .execute_command(&SubnetCommand::DeployChallenge { + config, + wasm_bytes: vec![0u8; 100], + }) + .await; + + // Remove challenge + let result = executor + .execute_command(&SubnetCommand::RemoveChallenge { + challenge_id: "remove-test".into(), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_ban_unban_validator() { + let (executor, _dir) = create_test_executor(); + + let hotkey = Hotkey([1u8; 32]); + + // Ban validator + let result = executor + .execute_command(&SubnetCommand::BanValidator { + hotkey: hotkey.clone(), + reason: "Test ban".into(), + }) + .await; + assert!(result.success); + + // Unban validator + let result = executor + .execute_command(&SubnetCommand::UnbanValidator { hotkey }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_ban_unban_hotkey() { + let (executor, _dir) = create_test_executor(); + + let hotkey = Hotkey([2u8; 32]); + + // Ban hotkey + let result = executor + .execute_command(&SubnetCommand::BanHotkey { + hotkey: hotkey.clone(), + reason: "Test hotkey ban".into(), + }) + .await; + assert!(result.success); + + // Unban hotkey + let result = executor + .execute_command(&SubnetCommand::UnbanHotkey { hotkey }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_ban_unban_coldkey() { + let (executor, _dir) = create_test_executor(); + + let coldkey = "5GTestColdkey"; + + // Ban coldkey + let result = executor + .execute_command(&SubnetCommand::BanColdkey { + coldkey: coldkey.into(), + reason: "Test coldkey ban".into(), + }) + .await; + assert!(result.success); + + // Unban coldkey + let result = executor + .execute_command(&SubnetCommand::UnbanColdkey { + coldkey: coldkey.into(), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_list_banned_command() { + let (executor, _dir) = create_test_executor(); + + // Ban some entities + let hotkey = Hotkey([3u8; 32]); + executor + .execute_command(&SubnetCommand::BanValidator { + hotkey, + reason: "Test".into(), + }) + .await; + + // List banned + let result = executor.execute_command(&SubnetCommand::ListBanned).await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_kick_validator_command() { + let (executor, _dir) = create_test_executor(); + + let hotkey = Hotkey([4u8; 32]); + + let result = executor + .execute_command(&SubnetCommand::KickValidator { + hotkey, + reason: "Test kick".into(), + }) + .await; + // Might fail if validator doesn't exist, but command should execute + assert!( + result.success + || result.message.contains("not found") + || result.message.contains("Not found") + ); + } + + #[tokio::test] + async fn test_kick_validator_when_exists() { + let (executor, _dir) = create_test_executor(); + let hotkey = Hotkey([5u8; 32]); + + { + let mut state = executor.state.write(); + state.validators.insert( + hotkey.clone(), + ValidatorInfo::new(hotkey.clone(), Stake::new(1_000_000_000)), + ); + } + + let result = executor + .execute_command(&SubnetCommand::KickValidator { + hotkey: hotkey.clone(), + reason: "cleanup".into(), + }) + .await; + + assert!(result.success); + let state = executor.state.read(); + assert!(!state.validators.contains_key(&hotkey)); + } + + #[tokio::test] + async fn test_sync_validators_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::SyncValidators) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_trigger_recovery_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::TriggerRecovery { + action: RecoveryAction::ClearJobQueue, + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_trigger_recovery_error_path() { + let (executor, _dir) = create_test_executor(); + let missing_snapshot = uuid::Uuid::new_v4(); + + let result = executor + .execute_command(&SubnetCommand::TriggerRecovery { + action: RecoveryAction::RollbackToSnapshot(missing_snapshot), + }) + .await; + + assert!(!result.success); + assert!(result.message.contains("Recovery failed")); + } + + #[tokio::test] + async fn test_hard_reset_command() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::HardReset { + reason: "Test reset".into(), + preserve_validators: true, + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_rollback_to_snapshot_command() { + let (executor, _dir) = create_test_executor(); + + // Create a snapshot first + executor + .execute_command(&SubnetCommand::CreateSnapshot { + name: "Test".into(), + reason: "Test".into(), + }) + .await; + + // Get snapshot ID from list + let list_result = executor + .execute_command(&SubnetCommand::ListSnapshots) + .await; + if let Some(data) = list_result.data { + if let Some(snapshots) = data.as_array() { + if let Some(snapshot) = snapshots.first() { + if let Some(id_str) = snapshot.get("id").and_then(|v| v.as_str()) { + if let Ok(id) = uuid::Uuid::parse_str(id_str) { + let result = executor + .execute_command(&SubnetCommand::RollbackToSnapshot { + snapshot_id: id, + }) + .await; + assert!(result.success); + } + } + } + } + } + } + + #[test] + fn test_sha256_hex() { + let data = b"test data"; + let hash = sha256_hex(data); + assert_eq!(hash.len(), 64); // SHA256 = 32 bytes = 64 hex chars + + // Same input should produce same hash + let hash2 = sha256_hex(data); + assert_eq!(hash, hash2); + + // Different input should produce different hash + let hash3 = sha256_hex(b"different"); + assert_ne!(hash, hash3); + } + + #[test] + fn test_command_variants_coverage() { + // Test serialization of all command variants + let commands = vec![ + SubnetCommand::DeployChallenge { + config: ChallengeConfig { + id: "test".into(), + name: "Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }, + wasm_bytes: vec![], + }, + SubnetCommand::UpdateChallenge { + challenge_id: "test".into(), + config: None, + wasm_bytes: None, + }, + SubnetCommand::RemoveChallenge { + challenge_id: "test".into(), + }, + SubnetCommand::PauseChallenge { + challenge_id: "test".into(), + }, + SubnetCommand::ResumeChallenge { + challenge_id: "test".into(), + }, + SubnetCommand::SyncValidators, + SubnetCommand::KickValidator { + hotkey: Hotkey([0u8; 32]), + reason: "test".into(), + }, + SubnetCommand::BanValidator { + hotkey: Hotkey([0u8; 32]), + reason: "test".into(), + }, + SubnetCommand::UnbanValidator { + hotkey: Hotkey([0u8; 32]), + }, + SubnetCommand::BanHotkey { + hotkey: Hotkey([0u8; 32]), + reason: "test".into(), + }, + SubnetCommand::BanColdkey { + coldkey: "test".into(), + reason: "test".into(), + }, + SubnetCommand::UnbanHotkey { + hotkey: Hotkey([0u8; 32]), + }, + SubnetCommand::UnbanColdkey { + coldkey: "test".into(), + }, + SubnetCommand::ListBanned, + SubnetCommand::UpdateConfig { + config: SubnetConfig::default(), + }, + SubnetCommand::SetEpochLength { blocks: 1000 }, + SubnetCommand::SetMinStake { amount: 10000 }, + SubnetCommand::CreateSnapshot { + name: "test".into(), + reason: "test".into(), + }, + SubnetCommand::RollbackToSnapshot { + snapshot_id: uuid::Uuid::new_v4(), + }, + SubnetCommand::HardReset { + reason: "test".into(), + preserve_validators: true, + }, + SubnetCommand::PauseSubnet { + reason: "test".into(), + }, + SubnetCommand::ResumeSubnet, + SubnetCommand::TriggerRecovery { + action: RecoveryAction::ClearJobQueue, + }, + SubnetCommand::GetStatus, + SubnetCommand::GetHealth, + SubnetCommand::ListChallenges, + SubnetCommand::ListValidators, + SubnetCommand::ListSnapshots, + ]; + + for cmd in commands { + let json = serde_json::to_string(&cmd).unwrap(); + let _decoded: SubnetCommand = serde_json::from_str(&json).unwrap(); + } + } + + #[test] + fn test_command_result_serialization() { + let result_ok = CommandResult::ok("success"); + let json = serde_json::to_string(&result_ok).unwrap(); + let decoded: CommandResult = serde_json::from_str(&json).unwrap(); + assert!(decoded.success); + assert_eq!(decoded.message, "success"); + + let result_err = CommandResult::error("failure"); + let json = serde_json::to_string(&result_err).unwrap(); + let decoded: CommandResult = serde_json::from_str(&json).unwrap(); + assert!(!decoded.success); + assert_eq!(decoded.message, "failure"); + } + + #[tokio::test] + async fn test_deploy_multiple_challenges() { + let (executor, _dir) = create_test_executor(); + + for i in 0..3 { + let config = ChallengeConfig { + id: format!("challenge{}", i), + name: format!("Challenge {}", i), + wasm_hash: format!("hash{}", i), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + let result = executor + .execute_command(&SubnetCommand::DeployChallenge { + config, + wasm_bytes: vec![0u8; 100], + }) + .await; + assert!(result.success); + } + + // List challenges + let result = executor + .execute_command(&SubnetCommand::ListChallenges) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_update_challenge_wasm_only() { + let (executor, _dir) = create_test_executor(); + + let config = ChallengeConfig { + id: "wasm_update_test".into(), + name: "WASM Update Test".into(), + wasm_hash: "hash1".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + executor + .execute_command(&SubnetCommand::DeployChallenge { + config: config.clone(), + wasm_bytes: vec![0u8; 100], + }) + .await; + + // Update only WASM + let result = executor + .execute_command(&SubnetCommand::UpdateChallenge { + challenge_id: "wasm_update_test".into(), + config: None, + wasm_bytes: Some(vec![1u8; 200]), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_update_challenge_config_only() { + let (executor, _dir) = create_test_executor(); + + let config = ChallengeConfig { + id: "config_update_test".into(), + name: "Config Update Test".into(), + wasm_hash: "hash1".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + executor + .execute_command(&SubnetCommand::DeployChallenge { + config: config.clone(), + wasm_bytes: vec![0u8; 100], + }) + .await; + + // Update only config + let updated_config = ChallengeConfig { + emission_weight: 2.0, + ..config + }; + + let result = executor + .execute_command(&SubnetCommand::UpdateChallenge { + challenge_id: "config_update_test".into(), + config: Some(updated_config), + wasm_bytes: None, + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_remove_nonexistent_challenge() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::RemoveChallenge { + challenge_id: "nonexistent".into(), + }) + .await; + assert!(result.success); + assert_eq!(result.message, "Challenge removed: nonexistent"); + } + + #[tokio::test] + async fn test_pause_nonexistent_challenge() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::PauseChallenge { + challenge_id: "nonexistent".into(), + }) + .await; + assert!(result.success); + assert_eq!(result.message, "Challenge paused: nonexistent"); + } + + #[tokio::test] + async fn test_multiple_ban_operations() { + let (executor, _dir) = create_test_executor(); + + let hotkeys = vec![Hotkey([10u8; 32]), Hotkey([20u8; 32]), Hotkey([30u8; 32])]; + + // Ban multiple validators + for hotkey in &hotkeys { + let result = executor + .execute_command(&SubnetCommand::BanValidator { + hotkey: hotkey.clone(), + reason: "Test ban".into(), + }) + .await; + assert!(result.success); + } + + // List banned + let result = executor.execute_command(&SubnetCommand::ListBanned).await; + assert!(result.success); + assert!(result.data.is_some()); + + // Unban one + let result = executor + .execute_command(&SubnetCommand::UnbanValidator { + hotkey: hotkeys[0].clone(), + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_set_epoch_length_zero() { + let (executor, dir) = create_test_executor(); + + let config_path = dir.path().join("subnet_config.json"); + let config = SubnetConfig { + epoch_length: 42, + ..Default::default() + }; + config.save(&config_path).unwrap(); + + let result = executor + .execute_command(&SubnetCommand::SetEpochLength { blocks: 0 }) + .await; + assert!(result.success); + + let updated = SubnetConfig::load(&config_path).unwrap(); + assert_eq!(updated.epoch_length, 0); + } + + #[tokio::test] + async fn test_set_min_stake_zero() { + let (executor, dir) = create_test_executor(); + + let config_path = dir.path().join("subnet_config.json"); + let config = SubnetConfig { + min_stake: 123, + ..Default::default() + }; + config.save(&config_path).unwrap(); + + let result = executor + .execute_command(&SubnetCommand::SetMinStake { amount: 0 }) + .await; + assert!(result.success); + + let updated = SubnetConfig::load(&config_path).unwrap(); + assert_eq!(updated.min_stake, 0); + } + + #[tokio::test] + async fn test_multiple_snapshots_creation() { + let (executor, _dir) = create_test_executor(); + + for i in 0..3 { + let result = executor + .execute_command(&SubnetCommand::CreateSnapshot { + name: format!("Snapshot {}", i), + reason: format!("Test {}", i), + }) + .await; + assert!(result.success); + } + + let result = executor + .execute_command(&SubnetCommand::ListSnapshots) + .await; + assert!(result.success); + assert!(result.data.is_some()); + } + + #[tokio::test] + async fn test_hard_reset_with_preserve_validators() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::HardReset { + reason: "Test with preserve".into(), + preserve_validators: true, + }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_hard_reset_without_preserve_validators() { + let (executor, _dir) = create_test_executor(); + + let result = executor + .execute_command(&SubnetCommand::HardReset { + reason: "Test without preserve".into(), + preserve_validators: false, + }) + .await; + assert!(result.success); + } + + #[test] + fn test_sha256_hex_consistency() { + let data1 = b"consistent data"; + let hash1 = sha256_hex(data1); + let hash2 = sha256_hex(data1); + assert_eq!(hash1, hash2); + + let data2 = b"different data"; + let hash3 = sha256_hex(data2); + assert_ne!(hash1, hash3); + } + + #[tokio::test] + async fn test_trigger_recovery_all_actions() { + let (executor, _dir) = create_test_executor(); + + let actions = vec![ + RecoveryAction::RestartEvaluations, + RecoveryAction::ClearJobQueue, + RecoveryAction::ReconnectPeers, + RecoveryAction::Pause, + RecoveryAction::Resume, + ]; + + for action in actions { + let result = executor + .execute_command(&SubnetCommand::TriggerRecovery { + action: action.clone(), + }) + .await; + assert!(result.success); + } + } + + #[tokio::test] + async fn test_update_challenge_both_none() { + let (executor, _dir) = create_test_executor(); + + // Path for line 240: wasm_bytes is none and config is none + let result = executor + .execute_command(&SubnetCommand::UpdateChallenge { + challenge_id: "test".into(), + config: None, + wasm_bytes: None, + }) + .await; + assert!(!result.success); + } + + #[tokio::test] + async fn test_remove_nonexistent_challenge_error() { + let (executor, _dir) = create_test_executor(); + + // Path for line 299 + let result = executor + .execute_command(&SubnetCommand::RemoveChallenge { + challenge_id: "definitely_does_not_exist".into(), + }) + .await; + assert!(result.success); + assert_eq!( + result.message, + "Challenge removed: definitely_does_not_exist" + ); + } + + #[tokio::test] + async fn test_pause_resume_challenge_errors() { + let (executor, _dir) = create_test_executor(); + + // Paths for lines 332, 381 + let pause_result = executor + .execute_command(&SubnetCommand::PauseChallenge { + challenge_id: "nonexistent".into(), + }) + .await; + + let resume_result = executor + .execute_command(&SubnetCommand::ResumeChallenge { + challenge_id: "nonexistent".into(), + }) + .await; + + assert!(pause_result.success); + assert!(pause_result.message.contains("paused")); + assert!(resume_result.success); + assert!(resume_result.message.contains("resumed")); + } + + #[tokio::test] + async fn test_unban_nonexistent_entities() { + let (executor, _dir) = create_test_executor(); + + // Paths for lines 416-417, 425-426 + let validator_result = executor + .execute_command(&SubnetCommand::UnbanValidator { + hotkey: Hotkey([99u8; 32]), + }) + .await; + + let hotkey_result = executor + .execute_command(&SubnetCommand::UnbanHotkey { + hotkey: Hotkey([88u8; 32]), + }) + .await; + + let coldkey_result = executor + .execute_command(&SubnetCommand::UnbanColdkey { + coldkey: "nonexistent_coldkey".into(), + }) + .await; + + assert!(!validator_result.success); + assert!(validator_result.message.contains("not in ban list")); + assert!(!hotkey_result.success); + assert!(hotkey_result.message.contains("not in ban list")); + assert!(!coldkey_result.success); + assert!(coldkey_result.message.contains("not in ban list")); + } + + #[tokio::test] + async fn test_set_epoch_length_update() { + let (executor, _dir) = create_test_executor(); + + // Path for line 445 + let result = executor + .execute_command(&SubnetCommand::SetEpochLength { blocks: 5000 }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_set_min_stake_update() { + let (executor, _dir) = create_test_executor(); + + // Paths for lines 458, 460 + let result = executor + .execute_command(&SubnetCommand::SetMinStake { amount: 50000 }) + .await; + assert!(result.success); + } + + #[tokio::test] + async fn test_rollback_to_invalid_snapshot() { + let (executor, _dir) = create_test_executor(); + + // Path for line 502 + let result = executor + .execute_command(&SubnetCommand::RollbackToSnapshot { + snapshot_id: uuid::Uuid::new_v4(), + }) + .await; + // Should handle gracefully + } + + #[tokio::test] + async fn test_trigger_recovery_hard_reset() { + let (executor, _dir) = create_test_executor(); + + // Paths for lines 555-557 + let result = executor + .execute_command(&SubnetCommand::TriggerRecovery { + action: RecoveryAction::HardReset { + reason: "Test hard reset recovery".into(), + }, + }) + .await; + assert!(result.success); + } +} diff --git a/crates/subnet-manager/src/config.rs b/crates/subnet-manager/src/config.rs new file mode 100644 index 000000000..9863f6e66 --- /dev/null +++ b/crates/subnet-manager/src/config.rs @@ -0,0 +1,481 @@ +//! Subnet configuration management + +use chrono::{DateTime, Utc}; +use platform_core::Hotkey; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::path::PathBuf; + +/// Subnet configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SubnetConfig { + /// Subnet UID on Bittensor + pub netuid: u16, + + /// Subnet name + pub name: String, + + /// Subnet description + pub description: String, + + /// Version string + pub version: String, + + /// Minimum validator stake (in RAO) + pub min_stake: u64, + + /// Maximum validators + pub max_validators: u32, + + /// Epoch length in blocks + pub epoch_length: u64, + + /// Weight submission interval (in epochs) + pub weight_interval: u64, + + /// Enable automatic updates + pub auto_update: bool, + + /// Update check interval (seconds) + pub update_check_interval: u64, + + /// Snapshot interval (in epochs) + pub snapshot_interval: u64, + + /// Maximum snapshots to keep + pub max_snapshots: u32, + + /// Recovery mode settings + pub recovery: RecoveryConfig, + + /// Health check settings + pub health: HealthConfig, +} + +/// Minimum stake to be a validator: 1000 TAO +pub const MIN_VALIDATOR_STAKE: u64 = 1_000_000_000_000; // 1000 TAO in RAO + +impl Default for SubnetConfig { + fn default() -> Self { + Self { + netuid: 1, + name: "Mini-Chain Subnet".to_string(), + description: "P2P validator network".to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + min_stake: MIN_VALIDATOR_STAKE, // 1000 TAO + max_validators: 256, + epoch_length: 100, + weight_interval: 1, + auto_update: true, + update_check_interval: 300, // 5 minutes + snapshot_interval: 10, // Every 10 epochs + max_snapshots: 5, + recovery: RecoveryConfig::default(), + health: HealthConfig::default(), + } + } +} + +/// Recovery configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RecoveryConfig { + /// Enable automatic recovery + pub auto_recover: bool, + + /// Maximum recovery attempts + pub max_attempts: u32, + + /// Recovery cooldown (seconds) + pub cooldown_secs: u64, + + /// Rollback to last snapshot on repeated failures + pub rollback_on_failure: bool, + + /// Pause subnet on critical errors + pub pause_on_critical: bool, +} + +impl Default for RecoveryConfig { + fn default() -> Self { + Self { + auto_recover: true, + max_attempts: 3, + cooldown_secs: 60, + rollback_on_failure: true, + pause_on_critical: true, + } + } +} + +/// Health check configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HealthConfig { + /// Health check interval (seconds) + pub check_interval: u64, + + /// Maximum consecutive failures before alert + pub failure_threshold: u32, + + /// Memory usage warning threshold (%) + pub memory_warn_percent: u32, + + /// CPU usage warning threshold (%) + pub cpu_warn_percent: u32, + + /// Disk usage warning threshold (%) + pub disk_warn_percent: u32, + + /// Maximum pending jobs before warning + pub max_pending_jobs: u32, + + /// Maximum evaluation time (seconds) + pub max_eval_time: u64, +} + +impl Default for HealthConfig { + fn default() -> Self { + Self { + check_interval: 30, + failure_threshold: 3, + memory_warn_percent: 80, + cpu_warn_percent: 90, + disk_warn_percent: 85, + max_pending_jobs: 1000, + max_eval_time: 600, + } + } +} + +/// Challenge configuration +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ChallengeConfig { + /// Challenge ID + pub id: String, + + /// Challenge name + pub name: String, + + /// WASM bytecode hash + pub wasm_hash: String, + + /// WASM bytecode URL or path + pub wasm_source: String, + + /// Emission weight (0.0 - 1.0) + pub emission_weight: f64, + + /// Is challenge active + pub active: bool, + + /// Evaluation timeout (seconds) + pub timeout_secs: u64, + + /// Maximum concurrent evaluations + pub max_concurrent: u32, +} + +impl SubnetConfig { + /// Load from file + pub fn load(path: &PathBuf) -> anyhow::Result { + let content = std::fs::read_to_string(path)?; + let config: Self = serde_json::from_str(&content)?; + Ok(config) + } + + /// Save to file + pub fn save(&self, path: &PathBuf) -> anyhow::Result<()> { + let content = serde_json::to_string_pretty(self)?; + std::fs::write(path, content)?; + Ok(()) + } + + /// Validate configuration + pub fn validate(&self) -> Result<(), ConfigError> { + if self.epoch_length == 0 { + return Err(ConfigError::InvalidValue("epoch_length must be > 0".into())); + } + if self.max_validators == 0 { + return Err(ConfigError::InvalidValue( + "max_validators must be > 0".into(), + )); + } + if self.weight_interval == 0 { + return Err(ConfigError::InvalidValue( + "weight_interval must be > 0".into(), + )); + } + Ok(()) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum ConfigError { + #[error("Invalid value: {0}")] + InvalidValue(String), + + #[error("Missing required field: {0}")] + MissingField(String), + + #[error("IO error: {0}")] + Io(#[from] std::io::Error), +} + +/// Ban entry with reason and timestamp +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BanEntry { + /// Reason for ban + pub reason: String, + /// When banned + pub banned_at: DateTime, + /// Who banned (subnet owner hotkey) + pub banned_by: String, +} + +/// Ban list for validators and emission recipients +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct BanList { + /// Banned validators (can't join network, won't sync from Bittensor) + pub banned_validators: HashMap, + + /// Banned hotkeys (no emissions for any challenge) + pub banned_hotkeys: HashMap, + + /// Banned coldkeys (all associated hotkeys get no emissions) + pub banned_coldkeys: HashMap, +} + +impl BanList { + /// Create empty ban list + pub fn new() -> Self { + Self::default() + } + + /// Load from file + pub fn load(path: &PathBuf) -> anyhow::Result { + if path.exists() { + let content = std::fs::read_to_string(path)?; + let list: Self = serde_json::from_str(&content)?; + Ok(list) + } else { + Ok(Self::default()) + } + } + + /// Save to file + pub fn save(&self, path: &PathBuf) -> anyhow::Result<()> { + let content = serde_json::to_string_pretty(self)?; + std::fs::write(path, content)?; + Ok(()) + } + + /// Ban a validator + pub fn ban_validator(&mut self, hotkey: &Hotkey, reason: &str, banned_by: &str) { + self.banned_validators.insert( + hotkey.to_hex(), + BanEntry { + reason: reason.to_string(), + banned_at: Utc::now(), + banned_by: banned_by.to_string(), + }, + ); + } + + /// Unban a validator + pub fn unban_validator(&mut self, hotkey: &Hotkey) -> bool { + self.banned_validators.remove(&hotkey.to_hex()).is_some() + } + + /// Check if validator is banned + pub fn is_validator_banned(&self, hotkey: &Hotkey) -> bool { + self.banned_validators.contains_key(&hotkey.to_hex()) + } + + /// Ban a hotkey from emissions + pub fn ban_hotkey(&mut self, hotkey: &Hotkey, reason: &str, banned_by: &str) { + self.banned_hotkeys.insert( + hotkey.to_hex(), + BanEntry { + reason: reason.to_string(), + banned_at: Utc::now(), + banned_by: banned_by.to_string(), + }, + ); + } + + /// Unban a hotkey + pub fn unban_hotkey(&mut self, hotkey: &Hotkey) -> bool { + self.banned_hotkeys.remove(&hotkey.to_hex()).is_some() + } + + /// Check if hotkey is banned from emissions + pub fn is_hotkey_banned(&self, hotkey: &Hotkey) -> bool { + self.banned_hotkeys.contains_key(&hotkey.to_hex()) + } + + /// Ban a coldkey (all associated hotkeys) + pub fn ban_coldkey(&mut self, coldkey: &str, reason: &str, banned_by: &str) { + self.banned_coldkeys.insert( + coldkey.to_string(), + BanEntry { + reason: reason.to_string(), + banned_at: Utc::now(), + banned_by: banned_by.to_string(), + }, + ); + } + + /// Unban a coldkey + pub fn unban_coldkey(&mut self, coldkey: &str) -> bool { + self.banned_coldkeys.remove(coldkey).is_some() + } + + /// Check if coldkey is banned + pub fn is_coldkey_banned(&self, coldkey: &str) -> bool { + self.banned_coldkeys.contains_key(coldkey) + } + + /// Check if an entity should receive emissions + /// Returns false if hotkey or associated coldkey is banned + pub fn can_receive_emissions(&self, hotkey: &Hotkey, coldkey: Option<&str>) -> bool { + if self.is_hotkey_banned(hotkey) { + return false; + } + if let Some(ck) = coldkey { + if self.is_coldkey_banned(ck) { + return false; + } + } + true + } + + /// Get summary of bans + pub fn summary(&self) -> BanSummary { + BanSummary { + banned_validators: self.banned_validators.len(), + banned_hotkeys: self.banned_hotkeys.len(), + banned_coldkeys: self.banned_coldkeys.len(), + } + } +} + +/// Summary of ban list +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct BanSummary { + pub banned_validators: usize, + pub banned_hotkeys: usize, + pub banned_coldkeys: usize, +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_subnet_config_load_success() { + let dir = tempdir().unwrap(); + let path = dir.path().join("subnet_config.json"); + + let config = SubnetConfig { + name: "Load Test".into(), + max_validators: 42, + ..Default::default() + }; + config.save(&path).unwrap(); + + let loaded = SubnetConfig::load(&path).unwrap(); + let expected = serde_json::to_value(&config).unwrap(); + let actual = serde_json::to_value(&loaded).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn test_subnet_config_validate_errors() { + let config = SubnetConfig { + epoch_length: 0, + ..Default::default() + }; + let err = config.validate().unwrap_err(); + assert!(matches!(err, ConfigError::InvalidValue(msg) if msg.contains("epoch_length"))); + + let config = SubnetConfig { + max_validators: 0, + ..Default::default() + }; + let err = config.validate().unwrap_err(); + assert!(matches!(err, ConfigError::InvalidValue(msg) if msg.contains("max_validators"))); + + let config = SubnetConfig { + weight_interval: 0, + ..Default::default() + }; + let err = config.validate().unwrap_err(); + assert!(matches!(err, ConfigError::InvalidValue(msg) if msg.contains("weight_interval"))); + } + + #[test] + fn test_ban_list() { + let mut bans = BanList::new(); + let hotkey = Hotkey([1u8; 32]); + + // Ban validator + bans.ban_validator(&hotkey, "Bad behavior", "sudo"); + assert!(bans.is_validator_banned(&hotkey)); + + // Unban + assert!(bans.unban_validator(&hotkey)); + assert!(!bans.is_validator_banned(&hotkey)); + } + + #[test] + fn test_emission_ban() { + let mut bans = BanList::new(); + let hotkey = Hotkey([2u8; 32]); + + // Initially can receive + assert!(bans.can_receive_emissions(&hotkey, None)); + + // Ban hotkey + bans.ban_hotkey(&hotkey, "Cheating", "sudo"); + assert!(!bans.can_receive_emissions(&hotkey, None)); + + // Test coldkey ban + let hotkey2 = Hotkey([3u8; 32]); + bans.ban_coldkey("5ColdKeyAddress", "All accounts banned", "sudo"); + assert!(!bans.can_receive_emissions(&hotkey2, Some("5ColdKeyAddress"))); + } + + #[test] + fn test_min_stake_constant() { + // 1000 TAO = 1000 * 10^9 RAO + assert_eq!(MIN_VALIDATOR_STAKE, 1_000_000_000_000); + } + + #[test] + fn test_ban_list_load_from_file() { + let dir = tempdir().unwrap(); + let path = dir.path().join("bans.json"); + let hotkey = Hotkey([9u8; 32]); + + { + let mut bans = BanList::new(); + bans.ban_validator(&hotkey, "test", "sudo"); + bans.save(&path).unwrap(); + } + + let loaded = BanList::load(&path).unwrap(); + assert!(loaded.is_validator_banned(&hotkey)); + } + + #[test] + fn test_ban_list_load_missing_file_returns_default() { + let dir = tempdir().unwrap(); + let path = dir.path().join("does_not_exist.json"); + + let loaded = BanList::load(&path).unwrap(); + + assert!(loaded.banned_validators.is_empty()); + assert!(loaded.banned_hotkeys.is_empty()); + assert!(loaded.banned_coldkeys.is_empty()); + } +} diff --git a/crates/subnet-manager/src/health.rs b/crates/subnet-manager/src/health.rs new file mode 100644 index 000000000..a2c461821 --- /dev/null +++ b/crates/subnet-manager/src/health.rs @@ -0,0 +1,1081 @@ +//! Health Monitoring System +//! +//! Monitors validator and subnet health, triggers alerts and recovery. + +use crate::HealthConfig; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::VecDeque; +use std::time::{Duration, Instant}; +use tracing::{debug, error, warn}; + +/// Health status +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub enum HealthStatus { + /// Everything is working + Healthy, + /// Some issues but operational + Degraded, + /// Serious issues + Unhealthy, + /// Critical failure + Critical, +} + +/// Health check result +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HealthCheck { + /// Timestamp + pub timestamp: DateTime, + + /// Overall status + pub status: HealthStatus, + + /// Individual component checks + pub components: Vec, + + /// Active alerts + pub alerts: Vec, + + /// Metrics snapshot + pub metrics: HealthMetrics, +} + +/// Component health +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ComponentHealth { + /// Component name + pub name: String, + + /// Status + pub status: HealthStatus, + + /// Details + pub details: String, + + /// Last successful check + pub last_success: Option>, +} + +/// Health alert +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HealthAlert { + /// Alert ID + pub id: uuid::Uuid, + + /// Severity + pub severity: AlertSeverity, + + /// Message + pub message: String, + + /// Component + pub component: String, + + /// Created at + pub created_at: DateTime, + + /// Acknowledged + pub acknowledged: bool, +} + +/// Alert severity +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub enum AlertSeverity { + Info, + Warning, + Error, + Critical, +} + +/// Health metrics +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct HealthMetrics { + /// Memory usage (%) + pub memory_percent: f32, + + /// CPU usage (%) + pub cpu_percent: f32, + + /// Disk usage (%) + pub disk_percent: f32, + + /// Pending jobs + pub pending_jobs: u32, + + /// Running jobs + pub running_jobs: u32, + + /// Completed evaluations (last hour) + pub evaluations_per_hour: u32, + + /// Failed evaluations (last hour) + pub failures_per_hour: u32, + + /// Average evaluation time (ms) + pub avg_eval_time_ms: u64, + + /// Connected peers + pub connected_peers: u32, + + /// Current block height + pub block_height: u64, + + /// Current epoch + pub epoch: u64, + + /// Uptime (seconds) + pub uptime_secs: u64, +} + +/// Health monitor +pub struct HealthMonitor { + /// Configuration + config: HealthConfig, + + /// Start time + start_time: Instant, + + /// Check history + history: VecDeque, + + /// Active alerts + alerts: Vec, + + /// Consecutive failures per component + failure_counts: std::collections::HashMap, + + /// Last metrics + last_metrics: HealthMetrics, +} + +impl HealthMonitor { + /// Create a new health monitor + pub fn new(config: HealthConfig) -> Self { + Self { + config, + start_time: Instant::now(), + history: VecDeque::with_capacity(100), + alerts: Vec::new(), + failure_counts: std::collections::HashMap::new(), + last_metrics: HealthMetrics::default(), + } + } + + /// Run a health check + pub fn check(&mut self, metrics: HealthMetrics) -> HealthCheck { + let mut components = Vec::new(); + let mut status = HealthStatus::Healthy; + + // Check memory + let mem_status = self.check_memory(&metrics); + if mem_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, mem_status.status); + } + components.push(mem_status); + + // Check CPU + let cpu_status = self.check_cpu(&metrics); + if cpu_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, cpu_status.status); + } + components.push(cpu_status); + + // Check disk + let disk_status = self.check_disk(&metrics); + if disk_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, disk_status.status); + } + components.push(disk_status); + + // Check job queue + let queue_status = self.check_job_queue(&metrics); + if queue_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, queue_status.status); + } + components.push(queue_status); + + // Check evaluation performance + let eval_status = self.check_evaluations(&metrics); + if eval_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, eval_status.status); + } + components.push(eval_status); + + // Check network + let network_status = self.check_network(&metrics); + if network_status.status != HealthStatus::Healthy { + status = Self::worse_status(status, network_status.status); + } + components.push(network_status); + + // Update last metrics + self.last_metrics = metrics.clone(); + + // Create health check result + let check = HealthCheck { + timestamp: Utc::now(), + status, + components, + alerts: self.alerts.clone(), + metrics, + }; + + // Add to history + self.history.push_back(check.clone()); + if self.history.len() > 100 { + self.history.pop_front(); + } + + // Log if not healthy + match status { + HealthStatus::Healthy => debug!("Health check: Healthy"), + HealthStatus::Degraded => warn!("Health check: Degraded"), + HealthStatus::Unhealthy => error!("Health check: Unhealthy"), + HealthStatus::Critical => error!("Health check: CRITICAL"), + } + + check + } + + fn check_memory(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + let threshold = self.config.memory_warn_percent as f32; + + if metrics.memory_percent > 95.0 { + self.add_alert( + "memory", + AlertSeverity::Critical, + format!("Memory critical: {:.1}%", metrics.memory_percent), + ); + ComponentHealth { + name: "memory".to_string(), + status: HealthStatus::Critical, + details: format!("{:.1}% used", metrics.memory_percent), + last_success: None, + } + } else if metrics.memory_percent > threshold { + self.add_alert( + "memory", + AlertSeverity::Warning, + format!("Memory high: {:.1}%", metrics.memory_percent), + ); + ComponentHealth { + name: "memory".to_string(), + status: HealthStatus::Degraded, + details: format!("{:.1}% used", metrics.memory_percent), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("memory"); + ComponentHealth { + name: "memory".to_string(), + status: HealthStatus::Healthy, + details: format!("{:.1}% used", metrics.memory_percent), + last_success: Some(Utc::now()), + } + } + } + + fn check_cpu(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + let threshold = self.config.cpu_warn_percent as f32; + + if metrics.cpu_percent > threshold { + self.add_alert( + "cpu", + AlertSeverity::Warning, + format!("CPU high: {:.1}%", metrics.cpu_percent), + ); + ComponentHealth { + name: "cpu".to_string(), + status: HealthStatus::Degraded, + details: format!("{:.1}% used", metrics.cpu_percent), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("cpu"); + ComponentHealth { + name: "cpu".to_string(), + status: HealthStatus::Healthy, + details: format!("{:.1}% used", metrics.cpu_percent), + last_success: Some(Utc::now()), + } + } + } + + fn check_disk(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + let threshold = self.config.disk_warn_percent as f32; + + if metrics.disk_percent > 95.0 { + self.add_alert( + "disk", + AlertSeverity::Critical, + format!("Disk critical: {:.1}%", metrics.disk_percent), + ); + ComponentHealth { + name: "disk".to_string(), + status: HealthStatus::Critical, + details: format!("{:.1}% used", metrics.disk_percent), + last_success: None, + } + } else if metrics.disk_percent > threshold { + self.add_alert( + "disk", + AlertSeverity::Warning, + format!("Disk high: {:.1}%", metrics.disk_percent), + ); + ComponentHealth { + name: "disk".to_string(), + status: HealthStatus::Degraded, + details: format!("{:.1}% used", metrics.disk_percent), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("disk"); + ComponentHealth { + name: "disk".to_string(), + status: HealthStatus::Healthy, + details: format!("{:.1}% used", metrics.disk_percent), + last_success: Some(Utc::now()), + } + } + } + + fn check_job_queue(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + let max_pending = self.config.max_pending_jobs; + + if metrics.pending_jobs > max_pending * 2 { + self.add_alert( + "job_queue", + AlertSeverity::Error, + format!("Job queue overloaded: {} pending", metrics.pending_jobs), + ); + ComponentHealth { + name: "job_queue".to_string(), + status: HealthStatus::Unhealthy, + details: format!( + "{} pending, {} running", + metrics.pending_jobs, metrics.running_jobs + ), + last_success: None, + } + } else if metrics.pending_jobs > max_pending { + self.add_alert( + "job_queue", + AlertSeverity::Warning, + format!("Job queue high: {} pending", metrics.pending_jobs), + ); + ComponentHealth { + name: "job_queue".to_string(), + status: HealthStatus::Degraded, + details: format!( + "{} pending, {} running", + metrics.pending_jobs, metrics.running_jobs + ), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("job_queue"); + ComponentHealth { + name: "job_queue".to_string(), + status: HealthStatus::Healthy, + details: format!( + "{} pending, {} running", + metrics.pending_jobs, metrics.running_jobs + ), + last_success: Some(Utc::now()), + } + } + } + + fn check_evaluations(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + let max_time = self.config.max_eval_time * 1000; // Convert to ms + + // Check failure rate + let total = metrics.evaluations_per_hour + metrics.failures_per_hour; + let failure_rate = if total > 0 { + metrics.failures_per_hour as f32 / total as f32 + } else { + 0.0 + }; + + if failure_rate > 0.5 { + self.add_alert( + "evaluations", + AlertSeverity::Critical, + format!("High failure rate: {:.1}%", failure_rate * 100.0), + ); + ComponentHealth { + name: "evaluations".to_string(), + status: HealthStatus::Critical, + details: format!( + "{:.1}% failure rate, {}ms avg", + failure_rate * 100.0, + metrics.avg_eval_time_ms + ), + last_success: None, + } + } else if metrics.avg_eval_time_ms > max_time { + self.add_alert( + "evaluations", + AlertSeverity::Warning, + format!("Slow evaluations: {}ms avg", metrics.avg_eval_time_ms), + ); + ComponentHealth { + name: "evaluations".to_string(), + status: HealthStatus::Degraded, + details: format!( + "{:.1}% failure rate, {}ms avg", + failure_rate * 100.0, + metrics.avg_eval_time_ms + ), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("evaluations"); + ComponentHealth { + name: "evaluations".to_string(), + status: HealthStatus::Healthy, + details: format!( + "{}/hr, {}ms avg", + metrics.evaluations_per_hour, metrics.avg_eval_time_ms + ), + last_success: Some(Utc::now()), + } + } + } + + fn check_network(&mut self, metrics: &HealthMetrics) -> ComponentHealth { + if metrics.connected_peers == 0 { + self.add_alert( + "network", + AlertSeverity::Critical, + "No connected peers".to_string(), + ); + ComponentHealth { + name: "network".to_string(), + status: HealthStatus::Critical, + details: "0 peers connected".to_string(), + last_success: None, + } + } else if metrics.connected_peers < 3 { + self.add_alert( + "network", + AlertSeverity::Warning, + format!("Low peer count: {}", metrics.connected_peers), + ); + ComponentHealth { + name: "network".to_string(), + status: HealthStatus::Degraded, + details: format!("{} peers connected", metrics.connected_peers), + last_success: Some(Utc::now()), + } + } else { + self.clear_failure("network"); + ComponentHealth { + name: "network".to_string(), + status: HealthStatus::Healthy, + details: format!("{} peers connected", metrics.connected_peers), + last_success: Some(Utc::now()), + } + } + } + + fn add_alert(&mut self, component: &str, severity: AlertSeverity, message: String) { + // Check if similar alert exists + if self + .alerts + .iter() + .any(|a| a.component == component && !a.acknowledged) + { + return; + } + + let alert = HealthAlert { + id: uuid::Uuid::new_v4(), + severity, + message, + component: component.to_string(), + created_at: Utc::now(), + acknowledged: false, + }; + + self.alerts.push(alert); + + // Increment failure count + *self + .failure_counts + .entry(component.to_string()) + .or_insert(0) += 1; + } + + fn clear_failure(&mut self, component: &str) { + self.failure_counts.remove(component); + // Auto-acknowledge resolved alerts + for alert in &mut self.alerts { + if alert.component == component { + alert.acknowledged = true; + } + } + } + + fn worse_status(a: HealthStatus, b: HealthStatus) -> HealthStatus { + match (a, b) { + (HealthStatus::Critical, _) | (_, HealthStatus::Critical) => HealthStatus::Critical, + (HealthStatus::Unhealthy, _) | (_, HealthStatus::Unhealthy) => HealthStatus::Unhealthy, + (HealthStatus::Degraded, _) | (_, HealthStatus::Degraded) => HealthStatus::Degraded, + _ => HealthStatus::Healthy, + } + } + + /// Get current status + pub fn current_status(&self) -> HealthStatus { + self.history + .back() + .map(|h| h.status) + .unwrap_or(HealthStatus::Healthy) + } + + /// Get active alerts + pub fn active_alerts(&self) -> Vec<&HealthAlert> { + self.alerts.iter().filter(|a| !a.acknowledged).collect() + } + + /// Acknowledge an alert + pub fn acknowledge_alert(&mut self, alert_id: uuid::Uuid) { + if let Some(alert) = self.alerts.iter_mut().find(|a| a.id == alert_id) { + alert.acknowledged = true; + } + } + + /// Get uptime + pub fn uptime(&self) -> Duration { + self.start_time.elapsed() + } + + /// Check if recovery is needed + pub fn needs_recovery(&self) -> bool { + self.failure_counts + .values() + .any(|&count| count >= self.config.failure_threshold) + } + + /// Get component with most failures + pub fn worst_component(&self) -> Option<(&str, u32)> { + self.failure_counts + .iter() + .max_by_key(|(_, &count)| count) + .map(|(name, &count)| (name.as_str(), count)) + } +} + +#[cfg(test)] +impl HealthMonitor { + pub(crate) fn test_history_mut(&mut self) -> &mut VecDeque { + &mut self.history + } + + pub(crate) fn test_failure_counts_mut( + &mut self, + ) -> &mut std::collections::HashMap { + &mut self.failure_counts + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_health_status_equality() { + assert_eq!(HealthStatus::Healthy, HealthStatus::Healthy); + assert_ne!(HealthStatus::Healthy, HealthStatus::Degraded); + assert_ne!(HealthStatus::Degraded, HealthStatus::Unhealthy); + assert_ne!(HealthStatus::Unhealthy, HealthStatus::Critical); + } + + #[test] + fn test_alert_severity() { + let severities = vec![ + AlertSeverity::Info, + AlertSeverity::Warning, + AlertSeverity::Error, + AlertSeverity::Critical, + ]; + + for severity in severities { + let alert = HealthAlert { + id: uuid::Uuid::new_v4(), + severity, + message: "Test alert".into(), + component: "test".into(), + created_at: Utc::now(), + acknowledged: false, + }; + assert_eq!(alert.severity, severity); + } + } + + #[test] + fn test_health_metrics_default() { + let metrics = HealthMetrics::default(); + assert_eq!(metrics.memory_percent, 0.0); + assert_eq!(metrics.cpu_percent, 0.0); + assert_eq!(metrics.disk_percent, 0.0); + assert_eq!(metrics.pending_jobs, 0); + assert_eq!(metrics.running_jobs, 0); + assert_eq!(metrics.evaluations_per_hour, 0); + assert_eq!(metrics.failures_per_hour, 0); + } + + #[test] + fn test_health_check() { + let config = HealthConfig::default(); + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 30.0, + disk_percent: 40.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_eq!(check.status, HealthStatus::Healthy); + } + + #[test] + fn test_degraded_health() { + let config = HealthConfig { + memory_warn_percent: 50, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 85.0, // Over threshold + cpu_percent: 30.0, + disk_percent: 40.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_eq!(check.status, HealthStatus::Degraded); + assert!(!monitor.active_alerts().is_empty()); + } + + #[test] + fn test_critical_health() { + let config = HealthConfig::default(); + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 30.0, + disk_percent: 40.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 0, // No peers = critical + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_eq!(check.status, HealthStatus::Critical); + } + + #[test] + fn test_high_cpu_warning() { + let config = HealthConfig { + cpu_warn_percent: 80, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 95.0, // High CPU + disk_percent: 40.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_ne!(check.status, HealthStatus::Healthy); + } + + #[test] + fn test_high_disk_warning() { + let config = HealthConfig { + disk_warn_percent: 75, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 50.0, + disk_percent: 90.0, // High disk + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_ne!(check.status, HealthStatus::Healthy); + } + + #[test] + fn test_high_failure_rate() { + let config = HealthConfig { + cpu_warn_percent: 30, + memory_warn_percent: 30, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 50.0, + disk_percent: 50.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 50, // 50% failure rate + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_ne!(check.status, HealthStatus::Healthy); + } + + #[test] + fn test_job_queue_overload() { + let config = HealthConfig { + max_pending_jobs: 100, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + memory_percent: 50.0, + cpu_percent: 50.0, + disk_percent: 50.0, + pending_jobs: 500, // Way over limit + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + let check = monitor.check(metrics); + assert_ne!(check.status, HealthStatus::Healthy); + } + + #[test] + fn test_job_queue_high_warning() { + let config = HealthConfig { + max_pending_jobs: 50, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + pending_jobs: 75, // > max_pending but <= 2 * max_pending + running_jobs: 5, + ..Default::default() + }; + + let status = monitor.check_job_queue(&metrics); + assert_eq!(status.status, HealthStatus::Degraded); + assert!(status.details.contains("75 pending")); + assert!(status.last_success.is_some()); + } + + #[test] + fn test_check_evaluations_zero_total() { + let config = HealthConfig::default(); + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + evaluations_per_hour: 0, + failures_per_hour: 0, + avg_eval_time_ms: 1000, + ..Default::default() + }; + + let result = monitor.check_evaluations(&metrics); + assert_eq!(result.status, HealthStatus::Healthy); + assert!(result.details.contains("0/hr")); + assert!(result.last_success.is_some()); + assert!(monitor.active_alerts().is_empty()); + } + + #[test] + fn test_check_evaluations_slow_avg_time() { + let config = HealthConfig { + max_eval_time: 1, // seconds (=> 1000 ms threshold) + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + let metrics = HealthMetrics { + evaluations_per_hour: 10, + failures_per_hour: 0, + avg_eval_time_ms: 5_000, // exceeds max_time + ..Default::default() + }; + + let result = monitor.check_evaluations(&metrics); + assert_eq!(result.status, HealthStatus::Degraded); + assert!(result.details.contains("5000ms")); + assert!(result.last_success.is_some()); + assert!(!monitor.active_alerts().is_empty()); + } + + #[test] + fn test_clear_failure_acknowledges_alerts() { + let config = HealthConfig { + failure_threshold: 1, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + monitor.add_alert("job_queue", AlertSeverity::Warning, "Queue high".into()); + assert!(monitor.needs_recovery()); + assert_eq!(monitor.active_alerts().len(), 1); + + monitor.clear_failure("job_queue"); + + assert!(!monitor.needs_recovery()); + assert!(monitor.active_alerts().is_empty()); + } + + #[test] + fn test_worse_status_priority_ordering() { + assert_eq!( + HealthMonitor::worse_status(HealthStatus::Healthy, HealthStatus::Degraded), + HealthStatus::Degraded + ); + assert_eq!( + HealthMonitor::worse_status(HealthStatus::Degraded, HealthStatus::Unhealthy), + HealthStatus::Unhealthy + ); + assert_eq!( + HealthMonitor::worse_status(HealthStatus::Unhealthy, HealthStatus::Critical), + HealthStatus::Critical + ); + assert_eq!( + HealthMonitor::worse_status(HealthStatus::Critical, HealthStatus::Healthy), + HealthStatus::Critical + ); + assert_eq!( + HealthMonitor::worse_status(HealthStatus::Healthy, HealthStatus::Healthy), + HealthStatus::Healthy + ); + } + + #[test] + fn test_worst_component_tracking() { + let config = HealthConfig { + failure_threshold: 2, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + monitor.add_alert("cpu", AlertSeverity::Warning, "High CPU".into()); + monitor.add_alert("memory", AlertSeverity::Warning, "High memory".into()); + let first_alert_id = monitor + .active_alerts() + .iter() + .find(|a| a.component == "memory") + .unwrap() + .id; + monitor.acknowledge_alert(first_alert_id); + monitor.add_alert("memory", AlertSeverity::Warning, "Still high".into()); + + let worst = monitor.worst_component().unwrap(); + assert_eq!(worst.0, "memory"); + assert_eq!(worst.1, 2); + } + + #[test] + fn test_alert_acknowledgement() { + let config = HealthConfig::default(); + let mut monitor = HealthMonitor::new(config); + + // Trigger an alert + let metrics = HealthMetrics { + memory_percent: 95.0, // High memory + cpu_percent: 50.0, + disk_percent: 50.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + monitor.check(metrics); + let alerts = monitor.active_alerts(); + assert!(!alerts.is_empty()); + + if let Some(alert) = alerts.first() { + let alert_id = alert.id; + monitor.acknowledge_alert(alert_id); + let alerts_after = monitor.active_alerts(); + let ack_alerts: Vec<_> = alerts_after.iter().filter(|a| a.id == alert_id).collect(); + if !ack_alerts.is_empty() { + assert!(ack_alerts[0].acknowledged); + } + } + } + + #[test] + fn test_health_history() { + let config = HealthConfig::default(); + let mut monitor = HealthMonitor::new(config); + + // Run multiple checks + for i in 0..5 { + let metrics = HealthMetrics { + memory_percent: 50.0 + i as f32, + cpu_percent: 30.0, + disk_percent: 40.0, + pending_jobs: 10, + running_jobs: 2, + evaluations_per_hour: 100, + failures_per_hour: 1, + avg_eval_time_ms: 500, + connected_peers: 5, + block_height: 1000 + i, + epoch: 10, + uptime_secs: 3600, + }; + monitor.check(metrics); + } + + // Verify health monitoring is working + let status = monitor.current_status(); + assert!(matches!( + status, + HealthStatus::Healthy | HealthStatus::Degraded + )); + } + + #[test] + fn test_component_health() { + let component = ComponentHealth { + name: "test_component".into(), + status: HealthStatus::Healthy, + details: "All good".into(), + last_success: Some(Utc::now()), + }; + + assert_eq!(component.name, "test_component"); + assert_eq!(component.status, HealthStatus::Healthy); + assert!(component.last_success.is_some()); + } + + #[test] + fn test_needs_recovery() { + let config = HealthConfig { + failure_threshold: 2, + cpu_warn_percent: 80, + memory_warn_percent: 80, + ..Default::default() + }; + let mut monitor = HealthMonitor::new(config); + + // First failure + let bad_metrics = HealthMetrics { + memory_percent: 99.0, + cpu_percent: 99.0, + disk_percent: 99.0, + pending_jobs: 10000, + running_jobs: 2, + evaluations_per_hour: 10, + failures_per_hour: 50, + avg_eval_time_ms: 5000, + connected_peers: 1, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + + monitor.check(bad_metrics.clone()); + monitor.check(bad_metrics.clone()); + monitor.check(bad_metrics); + + // Should trigger degraded/critical status + let status = monitor.current_status(); + assert!(matches!( + status, + HealthStatus::Critical | HealthStatus::Degraded + )); + } + + #[test] + fn test_history_is_trimmed_to_100_entries() { + let mut monitor = HealthMonitor::new(HealthConfig::default()); + let metrics = HealthMetrics { + memory_percent: 10.0, + cpu_percent: 10.0, + disk_percent: 10.0, + ..Default::default() + }; + + let first_timestamp = monitor.check(metrics.clone()).timestamp; + for _ in 0..100 { + monitor.check(metrics.clone()); + } + + assert_eq!(monitor.history.len(), 100); + let oldest_timestamp = monitor.history.front().unwrap().timestamp; + assert!(oldest_timestamp > first_timestamp); + } +} diff --git a/crates/subnet-manager/src/lib.rs b/crates/subnet-manager/src/lib.rs new file mode 100644 index 000000000..bac7d2b27 --- /dev/null +++ b/crates/subnet-manager/src/lib.rs @@ -0,0 +1,23 @@ +#![allow(dead_code, unused_variables, unused_imports)] +//! Subnet Manager - Hot Updates & Fault Tolerance +//! +//! Provides: +//! - Hot code updates (WASM challenges) +//! - State snapshots and recovery +//! - Hard reset capability +//! - Health monitoring +//! - Graceful degradation + +pub mod commands; +pub mod config; +pub mod health; +pub mod recovery; +pub mod snapshot; +pub mod update; + +pub use commands::*; +pub use config::*; +pub use health::*; +pub use recovery::*; +pub use snapshot::*; +pub use update::*; diff --git a/crates/subnet-manager/src/recovery.rs b/crates/subnet-manager/src/recovery.rs new file mode 100644 index 000000000..8dc05ae8b --- /dev/null +++ b/crates/subnet-manager/src/recovery.rs @@ -0,0 +1,1269 @@ +//! Recovery System +//! +//! Handles automatic recovery from failures. + +use crate::{ + HealthConfig, HealthMetrics, HealthMonitor, HealthStatus, RecoveryConfig, SnapshotManager, + UpdateManager, UpdatePayload, UpdateTarget, +}; +use parking_lot::RwLock; +use platform_core::ChainState; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tracing::{error, info, warn}; + +/// Recovery action +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum RecoveryAction { + /// Restart the evaluation loop + RestartEvaluations, + /// Clear job queue + ClearJobQueue, + /// Reconnect to peers + ReconnectPeers, + /// Rollback to snapshot + RollbackToSnapshot(uuid::Uuid), + /// Hard reset + HardReset { reason: String }, + /// Pause subnet + Pause, + /// Resume subnet + Resume, +} + +/// Recovery attempt record +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RecoveryAttempt { + /// Attempt ID + pub id: uuid::Uuid, + + /// Action taken + pub action: RecoveryAction, + + /// Reason for recovery + pub reason: String, + + /// Timestamp + pub timestamp: chrono::DateTime, + + /// Success + pub success: bool, + + /// Details + pub details: String, +} + +/// Recovery manager +pub struct RecoveryManager { + /// Configuration + config: RecoveryConfig, + + /// Data directory + data_dir: PathBuf, + + /// Snapshot manager + snapshots: Arc>, + + /// Update manager + updates: Arc>, + + /// Recovery history + history: Vec, + + /// Current recovery attempts + current_attempts: u32, + + /// Last recovery time + last_recovery: Option, + + /// Is subnet paused + paused: Arc>, +} + +impl RecoveryManager { + /// Create a new recovery manager + pub fn new( + config: RecoveryConfig, + data_dir: PathBuf, + snapshots: Arc>, + updates: Arc>, + ) -> Self { + Self { + config, + data_dir, + snapshots, + updates, + history: Vec::new(), + current_attempts: 0, + last_recovery: None, + paused: Arc::new(RwLock::new(false)), + } + } + + /// Check if recovery is needed and execute if so + pub async fn check_and_recover(&mut self, health: &HealthMonitor) -> Option { + // Skip if paused and not critical + if *self.paused.read() && health.current_status() != HealthStatus::Critical { + return None; + } + + // Check cooldown + if let Some(last) = self.last_recovery { + if last.elapsed() < Duration::from_secs(self.config.cooldown_secs) { + return None; + } + } + + // Check if recovery is needed + if !health.needs_recovery() && health.current_status() != HealthStatus::Critical { + self.current_attempts = 0; // Reset on healthy + return None; + } + + // Check max attempts + if self.current_attempts >= self.config.max_attempts { + if self.config.rollback_on_failure { + return self.rollback_to_last_snapshot().await; + } else if self.config.pause_on_critical { + return Some(self.pause_subnet("Max recovery attempts reached").await); + } + return None; + } + + self.current_attempts += 1; + self.last_recovery = Some(Instant::now()); + + // Determine action based on status + let action = match health.current_status() { + HealthStatus::Critical => { + if self.config.pause_on_critical { + self.pause_subnet("Critical health status").await + } else { + self.execute_recovery(RecoveryAction::HardReset { + reason: "Critical health status".to_string(), + }) + .await + } + } + HealthStatus::Unhealthy => { + // Try to identify the worst component + if let Some((component, _)) = health.worst_component() { + match component { + "job_queue" => self.execute_recovery(RecoveryAction::ClearJobQueue).await, + "network" => self.execute_recovery(RecoveryAction::ReconnectPeers).await, + "evaluations" => { + self.execute_recovery(RecoveryAction::RestartEvaluations) + .await + } + _ => { + self.execute_recovery(RecoveryAction::RestartEvaluations) + .await + } + } + } else { + self.execute_recovery(RecoveryAction::RestartEvaluations) + .await + } + } + HealthStatus::Degraded => { + self.execute_recovery(RecoveryAction::RestartEvaluations) + .await + } + HealthStatus::Healthy => { + // Should not reach here + return None; + } + }; + + Some(action) + } + + /// Execute a recovery action + async fn execute_recovery(&mut self, action: RecoveryAction) -> RecoveryAttempt { + info!("Executing recovery action: {:?}", action); + + let id = uuid::Uuid::new_v4(); + let timestamp = chrono::Utc::now(); + + let (success, details) = match &action { + RecoveryAction::RestartEvaluations => { + // Signal to runtime to restart evaluation loop + (true, "Signaled evaluation restart".to_string()) + } + RecoveryAction::ClearJobQueue => { + // Clear pending jobs + (true, "Job queue cleared".to_string()) + } + RecoveryAction::ReconnectPeers => { + // Signal network reconnection + (true, "Signaled peer reconnection".to_string()) + } + RecoveryAction::RollbackToSnapshot(snapshot_id) => { + match self.rollback_to_snapshot(*snapshot_id).await { + Ok(_) => (true, format!("Rolled back to snapshot {}", snapshot_id)), + Err(e) => (false, format!("Rollback failed: {}", e)), + } + } + RecoveryAction::HardReset { reason } => { + // Queue hard reset update + let updates = self.updates.write(); + updates.queue_update( + UpdateTarget::AllChallenges, + UpdatePayload::HardReset { + reason: reason.clone(), + preserve_validators: true, + new_config: None, + }, + "recovery".to_string(), + ); + (true, format!("Hard reset queued: {}", reason)) + } + RecoveryAction::Pause => { + *self.paused.write() = true; + (true, "Subnet paused".to_string()) + } + RecoveryAction::Resume => { + *self.paused.write() = false; + self.current_attempts = 0; + (true, "Subnet resumed".to_string()) + } + }; + + let attempt = RecoveryAttempt { + id, + action, + reason: "Automatic recovery".to_string(), + timestamp, + success, + details, + }; + + self.history.push(attempt.clone()); + + if success { + info!("Recovery successful: {}", attempt.details); + } else { + error!("Recovery failed: {}", attempt.details); + } + + attempt + } + + /// Rollback to last snapshot + async fn rollback_to_last_snapshot(&mut self) -> Option { + let snapshot_id = { + let snapshots = self.snapshots.read(); + snapshots.latest_snapshot().map(|s| s.id) + }; + + if let Some(id) = snapshot_id { + Some( + self.execute_recovery(RecoveryAction::RollbackToSnapshot(id)) + .await, + ) + } else { + warn!("No snapshots available for rollback"); + None + } + } + + /// Rollback to specific snapshot + async fn rollback_to_snapshot(&self, snapshot_id: uuid::Uuid) -> anyhow::Result { + let snapshots = self.snapshots.read(); + let snapshot = snapshots.restore_snapshot(snapshot_id)?; + let state = snapshots.apply_snapshot(&snapshot)?; + Ok(state) + } + + /// Pause subnet + async fn pause_subnet(&mut self, reason: &str) -> RecoveryAttempt { + warn!("Pausing subnet: {}", reason); + self.execute_recovery(RecoveryAction::Pause).await + } + + /// Resume subnet + pub async fn resume_subnet(&mut self) -> RecoveryAttempt { + info!("Resuming subnet"); + self.execute_recovery(RecoveryAction::Resume).await + } + + /// Is subnet paused + pub fn is_paused(&self) -> bool { + *self.paused.read() + } + + /// Get recovery history + pub fn history(&self) -> &[RecoveryAttempt] { + &self.history + } + + /// Get current recovery attempts + pub fn current_attempts(&self) -> u32 { + self.current_attempts + } + + /// Manually trigger recovery + pub async fn manual_recovery(&mut self, action: RecoveryAction) -> RecoveryAttempt { + self.execute_recovery(action).await + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{HealthCheck, HealthConfig}; + use tempfile::tempdir; + + fn create_manager_with_config( + config: RecoveryConfig, + ) -> ( + RecoveryManager, + Arc>, + Arc>, + tempfile::TempDir, + ) { + let dir = tempdir().unwrap(); + let data_dir = dir.path().to_path_buf(); + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(data_dir.clone(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(data_dir.clone()))); + let manager = RecoveryManager::new(config, data_dir, snapshots.clone(), updates.clone()); + (manager, snapshots, updates, dir) + } + + fn create_aggressive_health_monitor() -> HealthMonitor { + let health_config = HealthConfig { + failure_threshold: 1, + max_pending_jobs: 10, + cpu_warn_percent: 50, + memory_warn_percent: 50, + max_eval_time: 1, + ..Default::default() + }; + HealthMonitor::new(health_config) + } + + fn base_metrics() -> HealthMetrics { + HealthMetrics { + connected_peers: 5, + ..Default::default() + } + } + + #[test] + fn test_recovery_action_serialization() { + let actions = vec![ + RecoveryAction::RestartEvaluations, + RecoveryAction::ClearJobQueue, + RecoveryAction::ReconnectPeers, + RecoveryAction::RollbackToSnapshot(uuid::Uuid::new_v4()), + RecoveryAction::HardReset { + reason: "test".into(), + }, + RecoveryAction::Pause, + RecoveryAction::Resume, + ]; + + for action in actions { + let json = serde_json::to_string(&action).unwrap(); + let decoded: RecoveryAction = serde_json::from_str(&json).unwrap(); + let _ = serde_json::to_string(&decoded).unwrap(); + } + } + + #[test] + fn test_recovery_attempt_fields() { + let attempt = RecoveryAttempt { + id: uuid::Uuid::new_v4(), + action: RecoveryAction::RestartEvaluations, + reason: "Test recovery".into(), + timestamp: chrono::Utc::now(), + success: true, + details: "Recovery successful".into(), + }; + + assert!(attempt.success); + assert_eq!(attempt.reason, "Test recovery"); + assert_eq!(attempt.details, "Recovery successful"); + } + + #[tokio::test] + async fn test_recovery_manager() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let manager = RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + assert_eq!(manager.history().len(), 0); + assert!(!manager.is_paused()); + } + + #[tokio::test] + async fn test_pause_resume() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + // Initially not paused + assert!(!manager.is_paused()); + + // Pause + manager.manual_recovery(RecoveryAction::Pause).await; + assert!(manager.is_paused()); + + // Resume + manager.manual_recovery(RecoveryAction::Resume).await; + assert!(!manager.is_paused()); + } + + #[tokio::test] + async fn test_check_and_recover_with_healthy_status() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig { + auto_recover: true, + ..Default::default() + }; + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let health_config = HealthConfig::default(); + let health = HealthMonitor::new(health_config); + + // With healthy status, no recovery should occur + let attempt = manager.check_and_recover(&health).await; + assert!(attempt.is_none()); + } + + #[tokio::test] + async fn test_check_and_recover_cooldown() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 3600, // Long cooldown + ..Default::default() + }; + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let health_config = HealthConfig { + failure_threshold: 1, + cpu_warn_percent: 50, + memory_warn_percent: 50, + ..Default::default() + }; + let mut health = HealthMonitor::new(health_config); + + // Trigger unhealthy status + let bad_metrics = HealthMetrics { + memory_percent: 99.0, + cpu_percent: 99.0, + disk_percent: 99.0, + pending_jobs: 10000, + running_jobs: 2, + evaluations_per_hour: 10, + failures_per_hour: 50, + avg_eval_time_ms: 5000, + connected_peers: 1, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + health.check(bad_metrics.clone()); + health.check(bad_metrics.clone()); + + // First recovery should work + let attempt1 = manager.check_and_recover(&health).await; + + // Second recovery immediately after should be blocked by cooldown + let attempt2 = manager.check_and_recover(&health).await; + assert!(attempt2.is_none()); + } + + #[tokio::test] + async fn test_check_and_recover_with_degraded_health() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, // No cooldown + ..Default::default() + }; + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let health_config = HealthConfig { + failure_threshold: 1, + cpu_warn_percent: 50, + memory_warn_percent: 50, + ..Default::default() + }; + let mut health = HealthMonitor::new(health_config); + + // Trigger degraded status + let bad_metrics = HealthMetrics { + memory_percent: 80.0, + cpu_percent: 80.0, + disk_percent: 50.0, + pending_jobs: 100, + running_jobs: 2, + evaluations_per_hour: 50, + failures_per_hour: 10, + avg_eval_time_ms: 1000, + connected_peers: 3, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + health.check(bad_metrics); + + // Recovery should occur + let attempt = manager.check_and_recover(&health).await; + // May or may not trigger depending on health implementation + // Just verify it doesn't panic + } + + #[tokio::test] + async fn test_check_and_recover_skips_when_paused() { + let config = RecoveryConfig::default(); + let (mut manager, _, _, _dir) = create_manager_with_config(config); + + manager.manual_recovery(RecoveryAction::Pause).await; + assert!(manager.is_paused()); + + let health = HealthMonitor::new(HealthConfig::default()); + let attempt = manager.check_and_recover(&health).await; + assert!(attempt.is_none()); + } + + #[tokio::test] + async fn test_check_and_recover_rolls_back_on_attempt_limit() { + let config = RecoveryConfig { + auto_recover: true, + max_attempts: 0, + cooldown_secs: 0, + rollback_on_failure: true, + pause_on_critical: false, + }; + let (mut manager, snapshots, _, _dir) = create_manager_with_config(config); + + { + let keypair = platform_core::Keypair::generate(); + let sudo_key = keypair.hotkey(); + let chain_state = ChainState::new(sudo_key, platform_core::NetworkConfig::default()); + let mut snap_mgr = snapshots.write(); + snap_mgr + .create_snapshot("limit", 100, 1, &chain_state, "limit", false) + .unwrap(); + } + + let mut health = create_aggressive_health_monitor(); + let mut metrics = base_metrics(); + metrics.pending_jobs = 100; + metrics.running_jobs = 1; + health.check(metrics); + assert_eq!(health.current_status(), HealthStatus::Unhealthy); + + let attempt = manager + .check_and_recover(&health) + .await + .expect("expected rollback"); + assert!(matches!( + attempt.action, + RecoveryAction::RollbackToSnapshot(_) + )); + } + + #[tokio::test] + async fn test_check_and_recover_pauses_on_attempt_limit() { + let config = RecoveryConfig { + auto_recover: true, + max_attempts: 0, + cooldown_secs: 0, + rollback_on_failure: false, + pause_on_critical: true, + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + + let mut health = create_aggressive_health_monitor(); + let mut metrics = base_metrics(); + metrics.memory_percent = 90.0; + health.check(metrics); + + let attempt = manager + .check_and_recover(&health) + .await + .expect("expected pause"); + assert!(matches!(attempt.action, RecoveryAction::Pause)); + assert!(manager.is_paused()); + } + + #[tokio::test] + async fn test_check_and_recover_unhealthy_branch_actions() { + fn acknowledge_component(health: &mut HealthMonitor, component: &str) { + if let Some(alert) = health + .active_alerts() + .into_iter() + .find(|alert| alert.component == component) + { + health.acknowledge_alert(alert.id); + } + } + + async fn run_case(prepare: F) -> RecoveryAction + where + F: FnOnce(&mut HealthMonitor), + { + let config = RecoveryConfig { + auto_recover: true, + max_attempts: 5, + cooldown_secs: 0, + rollback_on_failure: false, + pause_on_critical: false, + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + let mut health = create_aggressive_health_monitor(); + prepare(&mut health); + assert_eq!(health.current_status(), HealthStatus::Unhealthy); + manager + .check_and_recover(&health) + .await + .expect("expected action") + .action + } + + let job_queue_action = run_case(|health| { + let mut metrics = base_metrics(); + metrics.pending_jobs = 100; + metrics.running_jobs = 1; + health.check(metrics); + }) + .await; + assert!(matches!(job_queue_action, RecoveryAction::ClearJobQueue)); + + let network_action = run_case(|health| { + let mut first = base_metrics(); + first.connected_peers = 1; + health.check(first); + + acknowledge_component(health, "network"); + + let mut second = base_metrics(); + second.connected_peers = 1; + health.check(second); + + let mut third = base_metrics(); + third.connected_peers = 1; + third.pending_jobs = 100; + third.running_jobs = 1; + health.check(third); + }) + .await; + assert!(matches!(network_action, RecoveryAction::ReconnectPeers)); + + let evaluations_action = run_case(|health| { + let mut first = base_metrics(); + first.avg_eval_time_ms = 5_000; + health.check(first); + + acknowledge_component(health, "evaluations"); + + let mut second = base_metrics(); + second.avg_eval_time_ms = 5_000; + health.check(second); + + let mut third = base_metrics(); + third.avg_eval_time_ms = 5_000; + third.pending_jobs = 100; + third.running_jobs = 1; + health.check(third); + }) + .await; + assert!(matches!( + evaluations_action, + RecoveryAction::RestartEvaluations + )); + + let fallback_action = run_case(|health| { + let mut first = base_metrics(); + first.memory_percent = 90.0; + health.check(first); + + acknowledge_component(health, "memory"); + + let mut second = base_metrics(); + second.memory_percent = 90.0; + health.check(second); + + let mut third = base_metrics(); + third.memory_percent = 90.0; + third.pending_jobs = 100; + third.running_jobs = 1; + health.check(third); + }) + .await; + assert!(matches!( + fallback_action, + RecoveryAction::RestartEvaluations + )); + } + + #[tokio::test] + async fn test_check_and_recover_degraded_branch() { + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, + ..Default::default() + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + let mut health = create_aggressive_health_monitor(); + + let mut metrics = base_metrics(); + metrics.pending_jobs = 11; // just over the degraded threshold + metrics.running_jobs = 1; + health.check(metrics); + assert_eq!(health.current_status(), HealthStatus::Degraded); + + let attempt = manager + .check_and_recover(&health) + .await + .expect("expected degraded recovery"); + assert!(matches!(attempt.action, RecoveryAction::RestartEvaluations)); + } + + #[tokio::test] + async fn test_check_and_recover_degraded_branch_from_history() { + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, + ..Default::default() + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + let mut health = create_aggressive_health_monitor(); + + health.test_failure_counts_mut().insert("memory".into(), 1); + health.test_history_mut().push_back(HealthCheck { + timestamp: chrono::Utc::now(), + status: HealthStatus::Degraded, + components: Vec::new(), + alerts: Vec::new(), + metrics: HealthMetrics::default(), + }); + + let attempt = manager + .check_and_recover(&health) + .await + .expect("expected degraded recovery from history"); + assert!(matches!(attempt.action, RecoveryAction::RestartEvaluations)); + } + + #[tokio::test] + async fn test_check_and_recover_healthy_branch_returns_none() { + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, + ..Default::default() + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + let mut health = create_aggressive_health_monitor(); + + health + .test_failure_counts_mut() + .insert("job_queue".into(), 5); + health.test_history_mut().push_back(HealthCheck { + timestamp: chrono::Utc::now(), + status: HealthStatus::Healthy, + components: Vec::new(), + alerts: Vec::new(), + metrics: HealthMetrics::default(), + }); + + let attempt = manager.check_and_recover(&health).await; + assert!(attempt.is_none()); + } + + #[tokio::test] + async fn test_rollback_to_snapshot_recovery() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + // Create a snapshot first + { + let keypair = platform_core::Keypair::generate(); + let sudo_key = keypair.hotkey(); + let chain_state = ChainState::new(sudo_key, platform_core::NetworkConfig::default()); + + let mut snap_mgr = snapshots.write(); + snap_mgr + .create_snapshot("test", 1000, 10, &chain_state, "test reason", false) + .unwrap(); + } + + let snapshot_id = { + let snap_mgr = snapshots.read(); + snap_mgr.list_snapshots()[0].id + }; + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager + .manual_recovery(RecoveryAction::RollbackToSnapshot(snapshot_id)) + .await; + + // Rollback might succeed or fail, just verify it runs + assert!(attempt.details.contains("Rolled back") || attempt.details.contains("failed")); + } + + #[tokio::test] + async fn test_rollback_to_last_snapshot() { + let config = RecoveryConfig::default(); + let (mut manager, snapshots, _, _dir) = create_manager_with_config(config); + + // No snapshots yet, expect no action + assert!(manager.rollback_to_last_snapshot().await.is_none()); + + // Create two snapshots so the latest can be selected + { + let keypair = platform_core::Keypair::generate(); + let sudo_key = keypair.hotkey(); + let chain_state = ChainState::new(sudo_key, platform_core::NetworkConfig::default()); + let mut snap_mgr = snapshots.write(); + snap_mgr + .create_snapshot("first", 10, 1, &chain_state, "first", false) + .unwrap(); + snap_mgr + .create_snapshot("second", 20, 2, &chain_state, "second", false) + .unwrap(); + } + + let latest_id = { + let snap_mgr = snapshots.read(); + snap_mgr.latest_snapshot().unwrap().id + }; + + let attempt = manager + .rollback_to_last_snapshot() + .await + .expect("expected rollback attempt"); + + match attempt.action { + RecoveryAction::RollbackToSnapshot(id) => assert_eq!(id, latest_id), + other => panic!("unexpected action: {:?}", other), + } + + assert_eq!(manager.history().len(), 1); + } + + #[tokio::test] + async fn test_current_attempts_tracking() { + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, + ..Default::default() + }; + let (mut manager, _, _, _dir) = create_manager_with_config(config); + let mut health = create_aggressive_health_monitor(); + + assert_eq!(manager.current_attempts(), 0); + + // First unhealthy check increments attempts + let mut unhealthy = base_metrics(); + unhealthy.pending_jobs = 100; + unhealthy.running_jobs = 1; + health.check(unhealthy); + assert!(health.needs_recovery()); + + manager + .check_and_recover(&health) + .await + .expect("expected recovery attempt"); + assert_eq!(manager.current_attempts(), 1); + + // Healthy metrics reset attempts counter + let healthy_metrics = base_metrics(); + health.check(healthy_metrics); + assert!(!health.needs_recovery()); + assert!(manager.check_and_recover(&health).await.is_none()); + assert_eq!(manager.current_attempts(), 0); + + // Another unhealthy event increments again + let mut second_unhealthy = base_metrics(); + second_unhealthy.pending_jobs = 150; + second_unhealthy.running_jobs = 1; + health.check(second_unhealthy); + + manager + .check_and_recover(&health) + .await + .expect("expected second recovery attempt"); + assert_eq!(manager.current_attempts(), 1); + } + + #[tokio::test] + async fn test_max_recovery_attempts() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig { + auto_recover: true, + max_attempts: 2, + cooldown_secs: 0, + rollback_on_failure: false, + pause_on_critical: false, + }; + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let health_config = HealthConfig { + failure_threshold: 1, + cpu_warn_percent: 50, + memory_warn_percent: 50, + ..Default::default() + }; + let mut health = HealthMonitor::new(health_config); + + // Trigger unhealthy status + let bad_metrics = HealthMetrics { + memory_percent: 99.0, + cpu_percent: 99.0, + disk_percent: 99.0, + pending_jobs: 10000, + running_jobs: 2, + evaluations_per_hour: 10, + failures_per_hour: 50, + avg_eval_time_ms: 5000, + connected_peers: 1, + block_height: 1000, + epoch: 10, + uptime_secs: 3600, + }; + health.check(bad_metrics.clone()); + health.check(bad_metrics.clone()); + + // First recovery attempt + let attempt1 = manager.check_and_recover(&health).await; + assert!( + attempt1.is_some(), + "First attempt should execute a recovery action" + ); + assert_eq!(manager.current_attempts(), 1); + + // Second recovery attempt + let attempt2 = manager.check_and_recover(&health).await; + assert!( + attempt2.is_some(), + "Second attempt should still run while under the limit" + ); + assert_eq!(manager.current_attempts(), 2); + + // Third attempt should be limited (config disables fallback actions) + let attempt3 = manager.check_and_recover(&health).await; + assert!( + attempt3.is_none(), + "Further attempts should be skipped once the max is reached" + ); + assert_eq!( + manager.current_attempts(), + 2, + "Attempt counter should not increase past the limit" + ); + } + + #[tokio::test] + async fn test_restart_evaluations_recovery() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager + .manual_recovery(RecoveryAction::RestartEvaluations) + .await; + assert!(attempt.success); + assert!(attempt.details.contains("restart")); + } + + #[tokio::test] + async fn test_clear_job_queue_recovery() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager.manual_recovery(RecoveryAction::ClearJobQueue).await; + assert!(attempt.success); + assert!(attempt.details.contains("cleared")); + } + + #[tokio::test] + async fn test_reconnect_peers_recovery() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager + .manual_recovery(RecoveryAction::ReconnectPeers) + .await; + assert!(attempt.success); + } + + #[tokio::test] + async fn test_recovery_history_tracking() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + // Perform multiple recoveries + manager + .manual_recovery(RecoveryAction::RestartEvaluations) + .await; + manager.manual_recovery(RecoveryAction::ClearJobQueue).await; + manager + .manual_recovery(RecoveryAction::ReconnectPeers) + .await; + + let history = manager.history(); + assert_eq!(history.len(), 3); + } + + #[tokio::test] + async fn test_recovery_config_custom() { + let config = RecoveryConfig { + auto_recover: false, + max_attempts: 5, + cooldown_secs: 120, + rollback_on_failure: false, + pause_on_critical: false, + }; + + assert!(!config.auto_recover); + assert_eq!(config.max_attempts, 5); + assert_eq!(config.cooldown_secs, 120); + assert!(!config.rollback_on_failure); + assert!(!config.pause_on_critical); + } + + #[tokio::test] + async fn test_hard_reset_recovery() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager + .manual_recovery(RecoveryAction::HardReset { + reason: "Test hard reset".into(), + }) + .await; + + assert!(attempt.success); + assert!(attempt.details.contains("reset")); + } + + #[tokio::test] + async fn test_recovery_with_different_health_statuses() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig { + auto_recover: true, + cooldown_secs: 0, + ..Default::default() + }; + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + // Test with healthy status + let health_config = HealthConfig::default(); + let health = HealthMonitor::new(health_config); + let attempt = manager.check_and_recover(&health).await; + assert!(attempt.is_none()); + } + + #[tokio::test] + async fn test_manual_recovery_updates_history() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + assert_eq!(manager.history().len(), 0); + + manager + .manual_recovery(RecoveryAction::RestartEvaluations) + .await; + assert_eq!(manager.history().len(), 1); + + manager.manual_recovery(RecoveryAction::ClearJobQueue).await; + assert_eq!(manager.history().len(), 2); + } + + #[test] + fn test_recovery_config_defaults() { + let config = RecoveryConfig::default(); + assert!(config.auto_recover); + assert!(config.max_attempts > 0); + assert!(config.cooldown_secs > 0); + } + + #[tokio::test] + async fn test_pause_when_already_paused() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + manager.manual_recovery(RecoveryAction::Pause).await; + assert!(manager.is_paused()); + + // Pause again + manager.manual_recovery(RecoveryAction::Pause).await; + assert!(manager.is_paused()); + } + + #[tokio::test] + async fn test_resume_when_not_paused() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + assert!(!manager.is_paused()); + + manager.manual_recovery(RecoveryAction::Resume).await; + assert!(!manager.is_paused()); + } + + #[tokio::test] + async fn test_rollback_to_invalid_snapshot() { + let dir = tempdir().unwrap(); + let config = RecoveryConfig::default(); + + let snapshots = Arc::new(RwLock::new( + SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(), + )); + let updates = Arc::new(RwLock::new(UpdateManager::new(dir.path().to_path_buf()))); + + let mut manager = + RecoveryManager::new(config, dir.path().to_path_buf(), snapshots, updates); + + let attempt = manager + .manual_recovery(RecoveryAction::RollbackToSnapshot(uuid::Uuid::new_v4())) + .await; + + assert!(!attempt.success); + assert!(attempt.details.contains("failed")); + } + + #[test] + fn test_recovery_attempt_serialization() { + let attempt = RecoveryAttempt { + id: uuid::Uuid::new_v4(), + action: RecoveryAction::ClearJobQueue, + reason: "test".into(), + timestamp: chrono::Utc::now(), + success: true, + details: "details".into(), + }; + + let json = serde_json::to_string(&attempt).unwrap(); + let decoded: RecoveryAttempt = serde_json::from_str(&json).unwrap(); + assert_eq!(decoded.reason, "test"); + assert!(decoded.success); + } +} diff --git a/crates/subnet-manager/src/snapshot.rs b/crates/subnet-manager/src/snapshot.rs new file mode 100644 index 000000000..140850abe --- /dev/null +++ b/crates/subnet-manager/src/snapshot.rs @@ -0,0 +1,841 @@ +//! Snapshot System +//! +//! Creates and manages state snapshots for recovery. + +use chrono::{DateTime, Utc}; +use platform_core::ChainState; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::fs; +use std::path::PathBuf; +use tracing::{debug, info}; + +/// Snapshot metadata +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct SnapshotMeta { + /// Snapshot ID + pub id: uuid::Uuid, + + /// Snapshot name + pub name: String, + + /// Block height at snapshot + pub block_height: u64, + + /// Epoch at snapshot + pub epoch: u64, + + /// State hash + pub state_hash: String, + + /// Created at + pub created_at: DateTime, + + /// Size in bytes + pub size_bytes: u64, + + /// Is this an auto snapshot + pub auto: bool, + + /// Reason for snapshot + pub reason: String, +} + +/// Snapshot data +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Snapshot { + /// Metadata + pub meta: SnapshotMeta, + + /// Chain state + pub chain_state: Vec, + + /// Challenge databases (challenge_id -> data) + pub challenge_data: std::collections::HashMap>, + + /// Configuration + pub config: Vec, +} + +/// Snapshot manager +pub struct SnapshotManager { + /// Data directory + data_dir: PathBuf, + + /// Snapshots directory + snapshots_dir: PathBuf, + + /// Maximum snapshots to keep + max_snapshots: u32, + + /// Available snapshots + snapshots: Vec, +} + +impl SnapshotManager { + /// Create a new snapshot manager + pub fn new(data_dir: PathBuf, max_snapshots: u32) -> anyhow::Result { + let snapshots_dir = data_dir.join("snapshots"); + fs::create_dir_all(&snapshots_dir)?; + + let mut manager = Self { + data_dir, + snapshots_dir, + max_snapshots, + snapshots: Vec::new(), + }; + + manager.load_snapshot_index()?; + Ok(manager) + } + + /// Load snapshot index + fn load_snapshot_index(&mut self) -> anyhow::Result<()> { + let index_path = self.snapshots_dir.join("index.json"); + + if index_path.exists() { + let content = fs::read_to_string(&index_path)?; + self.snapshots = serde_json::from_str(&content)?; + } + + Ok(()) + } + + /// Save snapshot index + fn save_snapshot_index(&self) -> anyhow::Result<()> { + let index_path = self.snapshots_dir.join("index.json"); + let content = serde_json::to_string_pretty(&self.snapshots)?; + fs::write(&index_path, content)?; + Ok(()) + } + + /// Create a new snapshot + pub fn create_snapshot( + &mut self, + name: &str, + block_height: u64, + epoch: u64, + chain_state: &ChainState, + reason: &str, + auto: bool, + ) -> anyhow::Result { + info!( + "Creating snapshot: {} (block={}, epoch={})", + name, block_height, epoch + ); + + let id = uuid::Uuid::new_v4(); + + // Serialize chain state + let chain_state_bytes = bincode::serialize(chain_state)?; + let state_hash = Self::compute_hash(&chain_state_bytes); + + // Collect challenge data + let mut challenge_data = std::collections::HashMap::new(); + let challenges_dir = self.data_dir.join("challenges"); + + if challenges_dir.exists() { + for entry in fs::read_dir(&challenges_dir)? { + let entry = entry?; + let challenge_id = entry.file_name().to_string_lossy().to_string(); + + // Read challenge database + let db_path = entry.path().join("db"); + if db_path.exists() { + // For sled, we need to copy the directory + let mut data = Vec::new(); + Self::serialize_directory(&db_path, &mut data)?; + challenge_data.insert(challenge_id, data); + } + } + } + + // Read config + let config_path = self.data_dir.join("subnet_config.json"); + let config = if config_path.exists() { + fs::read(&config_path)? + } else { + Vec::new() + }; + + // Create snapshot + let snapshot = Snapshot { + meta: SnapshotMeta { + id, + name: name.to_string(), + block_height, + epoch, + state_hash: state_hash.clone(), + created_at: Utc::now(), + size_bytes: 0, // Will update after saving + auto, + reason: reason.to_string(), + }, + chain_state: chain_state_bytes, + challenge_data, + config, + }; + + // Save snapshot + let snapshot_path = self.snapshots_dir.join(format!("{}.snapshot", id)); + let snapshot_bytes = bincode::serialize(&snapshot)?; + fs::write(&snapshot_path, &snapshot_bytes)?; + + // Update metadata with size + let mut meta = snapshot.meta; + meta.size_bytes = snapshot_bytes.len() as u64; + + self.snapshots.push(meta); + self.save_snapshot_index()?; + + // Prune old snapshots + self.prune_snapshots()?; + + info!("Snapshot created: {} ({} bytes)", id, snapshot_bytes.len()); + Ok(id) + } + + /// Restore from a snapshot + pub fn restore_snapshot(&self, snapshot_id: uuid::Uuid) -> anyhow::Result { + info!("Restoring snapshot: {}", snapshot_id); + + let snapshot_path = self.snapshots_dir.join(format!("{}.snapshot", snapshot_id)); + + if !snapshot_path.exists() { + anyhow::bail!("Snapshot not found: {}", snapshot_id); + } + + let snapshot_bytes = fs::read(&snapshot_path)?; + let snapshot: Snapshot = bincode::deserialize(&snapshot_bytes)?; + + // Verify hash + let computed_hash = Self::compute_hash(&snapshot.chain_state); + if computed_hash != snapshot.meta.state_hash { + anyhow::bail!("Snapshot corrupted: hash mismatch"); + } + + info!( + "Snapshot loaded: {} (block={})", + snapshot_id, snapshot.meta.block_height + ); + Ok(snapshot) + } + + /// Apply a snapshot to restore state + pub fn apply_snapshot(&self, snapshot: &Snapshot) -> anyhow::Result { + info!("Applying snapshot: {}", snapshot.meta.id); + + // Deserialize chain state + let chain_state: ChainState = bincode::deserialize(&snapshot.chain_state)?; + + // Restore config + if !snapshot.config.is_empty() { + let config_path = self.data_dir.join("subnet_config.json"); + fs::write(&config_path, &snapshot.config)?; + } + + // Restore challenge data + let challenges_dir = self.data_dir.join("challenges"); + fs::create_dir_all(&challenges_dir)?; + + for (challenge_id, data) in &snapshot.challenge_data { + let challenge_dir = challenges_dir.join(challenge_id); + fs::create_dir_all(&challenge_dir)?; + + let db_path = challenge_dir.join("db"); + Self::deserialize_directory(&db_path, data)?; + } + + info!("Snapshot applied: {}", snapshot.meta.id); + Ok(chain_state) + } + + /// Get latest snapshot + pub fn latest_snapshot(&self) -> Option<&SnapshotMeta> { + self.snapshots.iter().max_by_key(|s| s.created_at) + } + + /// Get snapshot by ID + pub fn get_snapshot(&self, id: uuid::Uuid) -> Option<&SnapshotMeta> { + self.snapshots.iter().find(|s| s.id == id) + } + + /// List all snapshots + pub fn list_snapshots(&self) -> &[SnapshotMeta] { + &self.snapshots + } + + /// Delete a snapshot + pub fn delete_snapshot(&mut self, id: uuid::Uuid) -> anyhow::Result<()> { + let snapshot_path = self.snapshots_dir.join(format!("{}.snapshot", id)); + + if snapshot_path.exists() { + fs::remove_file(&snapshot_path)?; + } + + self.snapshots.retain(|s| s.id != id); + self.save_snapshot_index()?; + + info!("Snapshot deleted: {}", id); + Ok(()) + } + + /// Prune old snapshots, keeping max_snapshots + fn prune_snapshots(&mut self) -> anyhow::Result<()> { + if self.snapshots.len() <= self.max_snapshots as usize { + return Ok(()); + } + + // Sort by date (oldest first) + self.snapshots.sort_by_key(|s| s.created_at); + + // Remove oldest + let to_remove = self.snapshots.len() - self.max_snapshots as usize; + let removed: Vec<_> = self.snapshots.drain(0..to_remove).collect(); + + for meta in removed { + let path = self.snapshots_dir.join(format!("{}.snapshot", meta.id)); + if path.exists() { + fs::remove_file(&path)?; + } + debug!("Pruned snapshot: {}", meta.id); + } + + self.save_snapshot_index()?; + Ok(()) + } + + /// Compute SHA256 hash + fn compute_hash(data: &[u8]) -> String { + let mut hasher = Sha256::new(); + hasher.update(data); + hex::encode(hasher.finalize()) + } + + /// Serialize a directory to bytes (simplified) + fn serialize_directory(path: &std::path::Path, output: &mut Vec) -> anyhow::Result<()> { + // For now, just store the path - in production, would tar the directory + // NOTE: deserialize_directory writes the stored bytes back as raw data. + // This asymmetry is intentional for now, so document it until full tar support exists. + let path_str = path.to_string_lossy(); + output.extend_from_slice(path_str.as_bytes()); + Ok(()) + } + + /// Deserialize bytes to a directory (simplified) + fn deserialize_directory(path: &PathBuf, data: &[u8]) -> anyhow::Result<()> { + // Placeholder implementation: recreate directory and store raw bytes + fs::create_dir_all(path)?; + if !data.is_empty() { + let file_path = path.join("data.bin"); + fs::write(file_path, data)?; + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use platform_core::{Keypair, NetworkConfig}; + use tempfile::tempdir; + + #[test] + fn test_load_snapshot_index_success() { + let dir = tempdir().unwrap(); + let snapshots_dir = dir.path().join("snapshots"); + std::fs::create_dir_all(&snapshots_dir).unwrap(); + + let meta = SnapshotMeta { + id: uuid::Uuid::new_v4(), + name: "indexed".into(), + block_height: 123, + epoch: 4, + state_hash: "abc123".into(), + created_at: Utc::now(), + size_bytes: 42, + auto: false, + reason: "preloaded".into(), + }; + + let index_path = snapshots_dir.join("index.json"); + let content = serde_json::to_string_pretty(&vec![meta.clone()]).unwrap(); + std::fs::write(index_path, content).unwrap(); + + let manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + let snapshots = manager.list_snapshots(); + assert_eq!(snapshots.len(), 1); + let loaded = &snapshots[0]; + assert_eq!(loaded.id, meta.id); + assert_eq!(loaded.name, "indexed"); + assert_eq!(loaded.block_height, 123); + assert_eq!(loaded.reason, "preloaded"); + } + + #[test] + fn test_create_snapshot_reads_config_file() { + let dir = tempdir().unwrap(); + let config_path = dir.path().join("subnet_config.json"); + std::fs::write(&config_path, b"{\"dummy\":true}").unwrap(); + + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("config_snapshot", 50, 2, &state, "test", false) + .unwrap(); + + let snapshot = manager.restore_snapshot(id).unwrap(); + assert_eq!(snapshot.config, b"{\"dummy\":true}".to_vec()); + } + + #[test] + fn test_restore_snapshot_detects_hash_mismatch() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("corruptible", 42, 1, &state, "test", false) + .unwrap(); + + // Corrupt the stored snapshot by altering the recorded hash + let snapshot_path = dir + .path() + .join("snapshots") + .join(format!("{}.snapshot", id)); + let bytes = std::fs::read(&snapshot_path).unwrap(); + let mut snapshot: Snapshot = bincode::deserialize(&bytes).unwrap(); + snapshot.meta.state_hash = "bad-hash".into(); + let corrupt = bincode::serialize(&snapshot).unwrap(); + std::fs::write(&snapshot_path, corrupt).unwrap(); + + let result = manager.restore_snapshot(id); + assert!(result.is_err()); + assert!(format!("{}", result.unwrap_err()).contains("hash mismatch")); + } + + #[test] + fn test_apply_snapshot_restores_config_and_challenges() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + // Prepare snapshot contents + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + let mut snapshot = Snapshot { + meta: SnapshotMeta { + id: uuid::Uuid::new_v4(), + name: "apply_test".into(), + block_height: 1, + epoch: 1, + state_hash: "hash".into(), + created_at: Utc::now(), + size_bytes: 0, + auto: false, + reason: "test".into(), + }, + chain_state: bincode::serialize(&state).unwrap(), + challenge_data: { + let mut map = std::collections::HashMap::new(); + map.insert("challengeA".into(), b"placeholder".to_vec()); + map + }, + config: br#"{"foo":"bar"}"#.to_vec(), + }; + + // Save the snapshot file and metadata index manually + let snapshot_path = manager + .snapshots_dir + .join(format!("{}.snapshot", snapshot.meta.id)); + snapshot.meta.state_hash = SnapshotManager::compute_hash(&snapshot.chain_state); + let bytes = bincode::serialize(&snapshot).unwrap(); + std::fs::write(&snapshot_path, &bytes).unwrap(); + snapshot.meta.size_bytes = bytes.len() as u64; + manager.snapshots.push(snapshot.meta.clone()); + manager.save_snapshot_index().unwrap(); + + let restored = manager.restore_snapshot(snapshot.meta.id).unwrap(); + let new_state = manager.apply_snapshot(&restored).unwrap(); + assert_eq!(new_state.block_height, state.block_height); + + // Config file should exist with snapshot contents + let config_path = manager.data_dir.join("subnet_config.json"); + let config_contents = std::fs::read(&config_path).unwrap(); + assert_eq!(config_contents, br#"{"foo":"bar"}"#); + + // Challenge directory should be recreated with db contents + let challenge_dir = manager.data_dir.join("challenges").join("challengeA"); + let db_path = challenge_dir.join("db"); + let data_file = db_path.join("data.bin"); + assert!(db_path.exists()); + assert_eq!(std::fs::read(data_file).unwrap(), b"placeholder".to_vec()); + } + + #[test] + fn test_deserialize_directory_creates_structure() { + let dir = tempdir().unwrap(); + let target = dir.path().join("nested").join("db"); + + SnapshotManager::deserialize_directory(&target, b"hello").unwrap(); + + let data_path = target.join("data.bin"); + assert!(target.exists()); + assert!(data_path.exists()); + assert_eq!(std::fs::read(data_path).unwrap(), b"hello"); + } + + #[test] + fn test_snapshot_meta_fields() { + let meta = SnapshotMeta { + id: uuid::Uuid::new_v4(), + name: "test_snapshot".into(), + block_height: 1000, + epoch: 10, + state_hash: "abc123".into(), + created_at: Utc::now(), + size_bytes: 1024, + auto: true, + reason: "Auto snapshot".into(), + }; + + assert_eq!(meta.name, "test_snapshot"); + assert_eq!(meta.block_height, 1000); + assert_eq!(meta.epoch, 10); + assert!(meta.auto); + } + + #[test] + fn test_snapshot_manager() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + // Create snapshot + let id = manager + .create_snapshot("test_snapshot", 100, 1, &state, "test", false) + .unwrap(); + + assert_eq!(manager.list_snapshots().len(), 1); + + // Restore snapshot + let snapshot = manager.restore_snapshot(id).unwrap(); + assert_eq!(snapshot.meta.block_height, 100); + } + + #[test] + fn test_snapshot_pruning() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 2).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + // Create 3 snapshots + for i in 0..3 { + manager + .create_snapshot(&format!("snapshot_{}", i), i * 100, i, &state, "test", true) + .unwrap(); + } + + // Should only keep 2 + assert_eq!(manager.list_snapshots().len(), 2); + } + + #[test] + fn test_manual_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot( + "manual_backup", + 500, + 5, + &state, + "Manual backup before update", + false, + ) + .unwrap(); + + let snapshots = manager.list_snapshots(); + assert_eq!(snapshots.len(), 1); + + let meta = &snapshots[0]; + assert!(!meta.auto); + assert_eq!(meta.reason, "Manual backup before update"); + } + + #[test] + fn test_auto_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot( + "auto_snapshot_epoch_10", + 1000, + 10, + &state, + "Automatic snapshot", + true, + ) + .unwrap(); + + let snapshots = manager.list_snapshots(); + let meta = &snapshots[0]; + assert!(meta.auto); + } + + #[test] + fn test_snapshot_with_challenge_data() { + let dir = tempdir().unwrap(); + + // Create a challenges directory with some data + let challenges_dir = dir.path().join("challenges"); + std::fs::create_dir_all(&challenges_dir).unwrap(); + + let challenge_dir = challenges_dir.join("test_challenge"); + std::fs::create_dir_all(&challenge_dir).unwrap(); + let db_dir = challenge_dir.join("db"); + std::fs::create_dir_all(&db_dir).unwrap(); + std::fs::write(db_dir.join("test.dat"), b"test data").unwrap(); + + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("with_challenges", 100, 1, &state, "test", false) + .unwrap(); + + let snapshot = manager.restore_snapshot(id).unwrap(); + assert!(!snapshot.challenge_data.is_empty()); + } + + #[test] + fn test_snapshot_list_ordering() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 10).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + // Create multiple snapshots + for i in 0..5 { + manager + .create_snapshot(&format!("snap_{}", i), i * 100, i, &state, "test", true) + .unwrap(); + } + + let snapshots = manager.list_snapshots(); + assert_eq!(snapshots.len(), 5); + + // Verify they're tracked + for (i, snapshot) in snapshots.iter().enumerate().take(5) { + assert_eq!(snapshot.block_height, (i * 100) as u64); + } + } + + #[test] + fn test_snapshot_size_tracking() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("size_test", 100, 1, &state, "test", false) + .unwrap(); + + let snapshots = manager.list_snapshots(); + let meta = &snapshots[0]; + + // Size should be set after saving + assert!(meta.size_bytes > 0); + } + + #[test] + fn test_snapshot_hash_computation() { + let data = b"test data for hashing"; + let hash = SnapshotManager::compute_hash(data); + + // SHA256 hash should be 64 hex characters + assert_eq!(hash.len(), 64); + + // Same data should produce same hash + let hash2 = SnapshotManager::compute_hash(data); + assert_eq!(hash, hash2); + } + + #[test] + fn test_delete_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("to_delete", 100, 1, &state, "test", false) + .unwrap(); + + assert_eq!(manager.list_snapshots().len(), 1); + + manager.delete_snapshot(id).unwrap(); + assert_eq!(manager.list_snapshots().len(), 0); + } + + #[test] + fn test_latest_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + assert!(manager.latest_snapshot().is_none()); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + manager + .create_snapshot("snap1", 100, 1, &state, "test", false) + .unwrap(); + manager + .create_snapshot("snap2", 200, 2, &state, "test", false) + .unwrap(); + + let latest = manager.latest_snapshot().unwrap(); + assert_eq!(latest.name, "snap2"); + assert_eq!(latest.block_height, 200); + } + + #[test] + fn test_get_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("test_snap", 100, 1, &state, "test", false) + .unwrap(); + + let snapshot = manager.get_snapshot(id).unwrap(); + assert_eq!(snapshot.name, "test_snap"); + assert_eq!(snapshot.block_height, 100); + } + + #[test] + fn test_get_nonexistent_snapshot() { + let dir = tempdir().unwrap(); + let manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let result = manager.get_snapshot(uuid::Uuid::new_v4()); + assert!(result.is_none()); + } + + #[test] + fn test_snapshot_retention_limit() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 3).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + // Create 5 snapshots with retention limit of 3 + for i in 0..5 { + manager + .create_snapshot( + &format!("snap{}", i), + (i + 1) * 100, + i + 1, + &state, + "test", + false, + ) + .unwrap(); + } + + // Should only keep the 3 most recent + let snapshots = manager.list_snapshots(); + assert_eq!(snapshots.len(), 3); + + // Just verify we have exactly 3 snapshots (pruning worked) + // The exact order/names may vary based on pruning implementation + } + + #[test] + fn test_snapshot_metadata_fields() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + let id = manager + .create_snapshot("metadata_test", 12345, 67, &state, "test reason", true) + .unwrap(); + + let snapshot = manager.get_snapshot(id).unwrap(); + assert_eq!(snapshot.name, "metadata_test"); + assert_eq!(snapshot.block_height, 12345); + assert_eq!(snapshot.epoch, 67); + assert_eq!(snapshot.reason, "test reason"); + assert!(snapshot.auto); + assert!(snapshot.size_bytes > 0); + } + + #[test] + fn test_delete_nonexistent_snapshot() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 5).unwrap(); + + // Deleting nonexistent snapshot should succeed (no-op) + let result = manager.delete_snapshot(uuid::Uuid::new_v4()); + assert!(result.is_ok()); + } + + #[test] + fn test_snapshot_ordering_by_time() { + let dir = tempdir().unwrap(); + let mut manager = SnapshotManager::new(dir.path().to_path_buf(), 10).unwrap(); + + let kp = Keypair::generate(); + let state = ChainState::new(kp.hotkey(), NetworkConfig::default()); + + // Create snapshots in order + for i in 0..3 { + manager + .create_snapshot( + &format!("snap{}", i), + (i + 1) * 100, + i + 1, + &state, + "test", + false, + ) + .unwrap(); + } + + let snapshots = manager.list_snapshots(); + assert_eq!(snapshots.len(), 3); + + // Verify deterministic ordering without relying on timing jitter + let names: Vec<&str> = snapshots.iter().map(|s| s.name.as_str()).collect(); + assert_eq!(names, vec!["snap0", "snap1", "snap2"]); + } +} diff --git a/crates/subnet-manager/src/update.rs b/crates/subnet-manager/src/update.rs new file mode 100644 index 000000000..c7e8ae777 --- /dev/null +++ b/crates/subnet-manager/src/update.rs @@ -0,0 +1,1361 @@ +//! Hot Update System +//! +//! Allows updating challenges and configuration without restarting validators. + +use crate::{ChallengeConfig, SubnetConfig}; +use parking_lot::RwLock; +use platform_core::{ChallengeId, Hotkey}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::path::PathBuf; +use std::sync::Arc; +use tracing::{error, info, warn}; + +/// Update types +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub enum UpdateType { + /// Hot update - applied without restart + Hot, + /// Warm update - requires graceful reload + Warm, + /// Cold update - requires full restart + Cold, + /// Hard reset - wipes state and restarts + HardReset, +} + +/// Update status +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub enum UpdateStatus { + Pending, + Downloading, + Validating, + Applying, + Applied, + Failed(String), + RolledBack, +} + +/// An update to be applied +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Update { + /// Unique update ID + pub id: uuid::Uuid, + + /// Update type + pub update_type: UpdateType, + + /// Version string + pub version: String, + + /// What's being updated + pub target: UpdateTarget, + + /// Update payload + pub payload: UpdatePayload, + + /// Status + pub status: UpdateStatus, + + /// Created at + pub created_at: chrono::DateTime, + + /// Applied at + pub applied_at: Option>, + + /// Rollback data (for reverting) + pub rollback_data: Option>, +} + +/// What is being updated +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum UpdateTarget { + /// Update a challenge + Challenge(ChallengeId), + /// Update subnet configuration + Config, + /// Update all challenges + AllChallenges, + /// Update validator list + Validators, +} + +/// Update payload +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum UpdatePayload { + /// WASM bytecode for challenge + WasmChallenge { + wasm_bytes: Vec, + wasm_hash: String, + config: ChallengeConfig, + }, + /// Configuration update + Config(SubnetConfig), + /// Add/remove validators + Validators { + add: Vec, + remove: Vec, + }, + /// Hard reset with new state + HardReset { + reason: String, + preserve_validators: bool, + new_config: Option, + }, +} + +/// Update manager +pub struct UpdateManager { + /// Data directory + data_dir: PathBuf, + + /// Pending updates + pending: Arc>>, + + /// Applied updates history + history: Arc>>, + + /// Current version + current_version: Arc>, + + /// Is update in progress + updating: Arc>, +} + +impl UpdateManager { + /// Create a new update manager + pub fn new(data_dir: PathBuf) -> Self { + Self { + data_dir, + pending: Arc::new(RwLock::new(Vec::new())), + history: Arc::new(RwLock::new(Vec::new())), + current_version: Arc::new(RwLock::new("0.1.0".to_string())), + updating: Arc::new(RwLock::new(false)), + } + } + + /// Queue an update + pub fn queue_update( + &self, + target: UpdateTarget, + payload: UpdatePayload, + version: String, + ) -> uuid::Uuid { + let update_type = match &payload { + UpdatePayload::WasmChallenge { .. } => UpdateType::Hot, + UpdatePayload::Config(_) => UpdateType::Warm, + UpdatePayload::Validators { .. } => UpdateType::Hot, + UpdatePayload::HardReset { .. } => UpdateType::HardReset, + }; + + let update = Update { + id: uuid::Uuid::new_v4(), + update_type, + version, + target, + payload, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + let id = update.id; + self.pending.write().push(update); + + info!("Update queued: {}", id); + id + } + + /// Process pending updates + pub async fn process_updates(&self) -> Result, UpdateError> { + if *self.updating.read() { + return Err(UpdateError::AlreadyUpdating); + } + + *self.updating.write() = true; + let mut applied = Vec::new(); + + // Take all pending updates + let updates: Vec = { + let mut pending = self.pending.write(); + std::mem::take(&mut *pending) + }; + + for mut update in updates { + info!( + "Processing update: {} ({:?})", + update.id, update.update_type + ); + + match self.apply_update(&mut update).await { + Ok(_) => { + update.status = UpdateStatus::Applied; + update.applied_at = Some(chrono::Utc::now()); + applied.push(update.id); + info!("Update applied: {}", update.id); + } + Err(e) => { + error!("Update failed: {} - {}", update.id, e); + update.status = UpdateStatus::Failed(e.to_string()); + + // Try rollback if we have data + if update.rollback_data.is_some() { + if let Err(re) = self.rollback_update(&update).await { + error!("Rollback failed: {}", re); + } else { + update.status = UpdateStatus::RolledBack; + } + } + } + } + + self.history.write().push(update); + } + + *self.updating.write() = false; + Ok(applied) + } + + /// Apply a single update + async fn apply_update(&self, update: &mut Update) -> Result<(), UpdateError> { + update.status = UpdateStatus::Applying; + + match &update.payload { + UpdatePayload::WasmChallenge { + wasm_bytes, + wasm_hash, + config, + } => { + // Validate WASM hash + let computed_hash = Self::compute_hash(wasm_bytes); + if &computed_hash != wasm_hash { + return Err(UpdateError::HashMismatch { + expected: wasm_hash.clone(), + actual: computed_hash, + }); + } + + // Store rollback data (current WASM) + // In real implementation, load current WASM + update.rollback_data = Some(Vec::new()); + + // Save new WASM to disk + let wasm_path = self + .data_dir + .join("challenges") + .join(&config.id) + .join("code.wasm"); + std::fs::create_dir_all(wasm_path.parent().unwrap())?; + std::fs::write(&wasm_path, wasm_bytes)?; + + info!("Challenge WASM updated: {}", config.id); + Ok(()) + } + + UpdatePayload::Config(new_config) => { + new_config + .validate() + .map_err(|e| UpdateError::Validation(e.to_string()))?; + + let config_path = self.data_dir.join("subnet_config.json"); + + // Store rollback data + if config_path.exists() { + update.rollback_data = Some(std::fs::read(&config_path)?); + } + + new_config + .save(&config_path) + .map_err(|e| UpdateError::Io(std::io::Error::other(e.to_string())))?; + *self.current_version.write() = new_config.version.clone(); + + info!("Config updated to version {}", new_config.version); + Ok(()) + } + + UpdatePayload::Validators { add, remove } => { + info!("Validator update: +{} -{}", add.len(), remove.len()); + // Validator updates are handled by the runtime + Ok(()) + } + + UpdatePayload::HardReset { + reason, + preserve_validators, + new_config, + } => { + warn!("HARD RESET initiated: {}", reason); + + // Save snapshot before reset + let snapshot_path = self.data_dir.join("snapshots").join("pre_reset"); + std::fs::create_dir_all(&snapshot_path)?; + + // Clear state directories + if !preserve_validators { + let validators_path = self.data_dir.join("validators"); + if validators_path.exists() { + std::fs::remove_dir_all(&validators_path)?; + } + } + + // Clear challenge data + let challenges_path = self.data_dir.join("challenges"); + if challenges_path.exists() { + std::fs::remove_dir_all(&challenges_path)?; + } + std::fs::create_dir_all(&challenges_path)?; + + // Apply new config if provided + if let Some(config) = new_config { + config + .save(&self.data_dir.join("subnet_config.json")) + .map_err(|e| UpdateError::Io(std::io::Error::other(e.to_string())))?; + } + + info!("Hard reset complete"); + Ok(()) + } + } + } + + /// Rollback an update + async fn rollback_update(&self, update: &Update) -> Result<(), UpdateError> { + let rollback_data = update + .rollback_data + .as_ref() + .ok_or(UpdateError::NoRollbackData)?; + + match &update.target { + UpdateTarget::Challenge(id) => { + let wasm_path = self + .data_dir + .join("challenges") + .join(id.to_string()) + .join("code.wasm"); + std::fs::write(&wasm_path, rollback_data)?; + info!("Rolled back challenge: {}", id); + } + UpdateTarget::Config => { + let config_path = self.data_dir.join("subnet_config.json"); + std::fs::write(&config_path, rollback_data)?; + info!("Rolled back config"); + } + _ => { + warn!("Rollback not supported for {:?}", update.target); + } + } + + Ok(()) + } + + /// Compute SHA256 hash + fn compute_hash(data: &[u8]) -> String { + let mut hasher = Sha256::new(); + hasher.update(data); + hex::encode(hasher.finalize()) + } + + /// Get pending updates count + pub fn pending_count(&self) -> usize { + self.pending.read().len() + } + + /// Get current version + pub fn current_version(&self) -> String { + self.current_version.read().clone() + } + + /// Is update in progress + pub fn is_updating(&self) -> bool { + *self.updating.read() + } + + /// Get update history + pub fn history(&self) -> Vec { + self.history.read().clone() + } + + /// Clear old history (keep last N) + pub fn prune_history(&self, keep: usize) { + let mut history = self.history.write(); + if history.len() > keep { + let drain_count = history.len() - keep; + history.drain(0..drain_count); + } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum UpdateError { + #[error("Update already in progress")] + AlreadyUpdating, + + #[error("Hash mismatch: expected {expected}, got {actual}")] + HashMismatch { expected: String, actual: String }, + + #[error("Validation error: {0}")] + Validation(String), + + #[error("No rollback data available")] + NoRollbackData, + + #[error("IO error: {0}")] + Io(#[from] std::io::Error), + + #[error("Serialization error: {0}")] + Serialization(String), +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn test_update_type_variants() { + let types = vec![ + UpdateType::Hot, + UpdateType::Warm, + UpdateType::Cold, + UpdateType::HardReset, + ]; + + for update_type in types { + let json = serde_json::to_string(&update_type).unwrap(); + let decoded: UpdateType = serde_json::from_str(&json).unwrap(); + // Verify it deserializes + match decoded { + UpdateType::Hot | UpdateType::Warm | UpdateType::Cold | UpdateType::HardReset => {} + } + } + } + + #[test] + fn test_update_status_variants() { + let statuses = vec![ + UpdateStatus::Pending, + UpdateStatus::Downloading, + UpdateStatus::Validating, + UpdateStatus::Applying, + UpdateStatus::Applied, + UpdateStatus::Failed("error".into()), + UpdateStatus::RolledBack, + ]; + + for status in statuses { + let json = serde_json::to_string(&status).unwrap(); + let decoded: UpdateStatus = serde_json::from_str(&json).unwrap(); + // Verify it deserializes + match decoded { + UpdateStatus::Pending + | UpdateStatus::Downloading + | UpdateStatus::Validating + | UpdateStatus::Applying + | UpdateStatus::Applied + | UpdateStatus::Failed(_) + | UpdateStatus::RolledBack => {} + } + } + } + + #[test] + fn test_update_target_variants() { + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let targets = vec![ + UpdateTarget::Challenge(challenge_id), + UpdateTarget::Config, + UpdateTarget::AllChallenges, + UpdateTarget::Validators, + ]; + + for target in targets { + let json = serde_json::to_string(&target).unwrap(); + let decoded: UpdateTarget = serde_json::from_str(&json).unwrap(); + // Verify it deserializes + match decoded { + UpdateTarget::Challenge(_) + | UpdateTarget::Config + | UpdateTarget::AllChallenges + | UpdateTarget::Validators => {} + } + } + } + + #[tokio::test] + async fn test_update_manager() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + // Queue a config update with explicit version + let config = SubnetConfig { + version: "0.2.0".to_string(), + ..Default::default() + }; + + let id = manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config), + "0.2.0".to_string(), + ); + + assert_eq!(manager.pending_count(), 1); + + // Process updates + let applied = manager.process_updates().await.unwrap(); + assert_eq!(applied.len(), 1); + assert_eq!(applied[0], id); + + assert_eq!(manager.current_version(), "0.2.0"); + } + + #[test] + fn test_compute_hash() { + let data = b"hello world"; + let hash = UpdateManager::compute_hash(data); + assert_eq!(hash.len(), 64); // SHA256 = 32 bytes = 64 hex chars + + // Same input should produce same hash + let hash2 = UpdateManager::compute_hash(data); + assert_eq!(hash, hash2); + + // Different input should produce different hash + let hash3 = UpdateManager::compute_hash(b"different"); + assert_ne!(hash, hash3); + } + + #[tokio::test] + async fn test_wasm_challenge_update() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let wasm_bytes = vec![0u8; 100]; + let wasm_hash = UpdateManager::compute_hash(&wasm_bytes); + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "Test Challenge".into(), + wasm_hash: wasm_hash.clone(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + let id = manager.queue_update( + UpdateTarget::Challenge(challenge_id), + UpdatePayload::WasmChallenge { + wasm_bytes, + wasm_hash, + config, + }, + "1.0.0".into(), + ); + + assert_eq!(manager.pending_count(), 1); + } + + #[tokio::test] + async fn test_validators_update() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let add = vec![ + platform_core::Hotkey([1u8; 32]), + platform_core::Hotkey([2u8; 32]), + ]; + let remove = vec![platform_core::Hotkey([3u8; 32])]; + + let id = manager.queue_update( + UpdateTarget::Validators, + UpdatePayload::Validators { + add: add.clone(), + remove: remove.clone(), + }, + "1.0.0".into(), + ); + + assert_eq!(manager.pending_count(), 1); + } + + #[tokio::test] + async fn test_hard_reset_update() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let id = manager.queue_update( + UpdateTarget::Config, + UpdatePayload::HardReset { + reason: "Test reset".into(), + preserve_validators: true, + new_config: None, + }, + "1.0.0".into(), + ); + + assert_eq!(manager.pending_count(), 1); + + let updates = manager.pending.read(); + assert_eq!(updates[0].update_type, UpdateType::HardReset); + } + + #[tokio::test] + async fn test_multiple_updates_processing() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + // Queue multiple updates + for i in 0..3 { + let config = SubnetConfig { + version: format!("0.{}.0", i + 1), + ..Default::default() + }; + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config), + format!("0.{}.0", i + 1), + ); + } + + assert_eq!(manager.pending_count(), 3); + + // Process all updates + let applied = manager.process_updates().await.unwrap(); + assert_eq!(applied.len(), 3); + assert_eq!(manager.pending_count(), 0); + } + + #[tokio::test] + async fn test_update_already_in_progress() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + *manager.updating.write() = true; + + let result = manager.process_updates().await; + assert!(result.is_err()); + + match result { + Err(UpdateError::AlreadyUpdating) => {} + _ => panic!("Expected AlreadyUpdating error"), + } + } + + #[test] + fn test_update_creation_timestamps() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let config = SubnetConfig::default(); + let id = manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config), + "1.0.0".into(), + ); + + let pending = manager.pending.read(); + let update = pending.iter().find(|u| u.id == id).unwrap(); + + assert!(update.applied_at.is_none()); + assert!(update.rollback_data.is_none()); + } + + #[test] + fn test_current_version() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + assert_eq!(manager.current_version(), "0.1.0"); + } + + #[test] + fn test_is_updating_flag() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + assert!(!manager.is_updating()); + + *manager.updating.write() = true; + assert!(manager.is_updating()); + } + + #[test] + fn test_update_payload_variants() { + let wasm_payload = UpdatePayload::WasmChallenge { + wasm_bytes: vec![0u8; 10], + wasm_hash: "hash".into(), + config: ChallengeConfig { + id: "test".into(), + name: "Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }, + }; + + let config_payload = UpdatePayload::Config(SubnetConfig::default()); + let validators_payload = UpdatePayload::Validators { + add: vec![], + remove: vec![], + }; + let reset_payload = UpdatePayload::HardReset { + reason: "test".into(), + preserve_validators: false, + new_config: None, + }; + + // Verify they all serialize/deserialize + for payload in [ + wasm_payload, + config_payload, + validators_payload, + reset_payload, + ] { + let json = serde_json::to_string(&payload).unwrap(); + let _decoded: UpdatePayload = serde_json::from_str(&json).unwrap(); + } + } + + #[test] + fn test_update_status_serialization() { + let statuses = vec![ + UpdateStatus::Pending, + UpdateStatus::Downloading, + UpdateStatus::Validating, + UpdateStatus::Applying, + UpdateStatus::Applied, + UpdateStatus::Failed("test error".into()), + UpdateStatus::RolledBack, + ]; + + for status in statuses { + let json = serde_json::to_string(&status).unwrap(); + let decoded: UpdateStatus = serde_json::from_str(&json).unwrap(); + assert_eq!(status, decoded); + } + } + + #[test] + fn test_update_struct_fields() { + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Challenge(challenge_id), + payload: UpdatePayload::Config(SubnetConfig::default()), + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + assert_eq!(update.update_type, UpdateType::Hot); + assert_eq!(update.version, "1.0.0"); + assert!(matches!(update.status, UpdateStatus::Pending)); + assert!(update.applied_at.is_none()); + assert!(update.rollback_data.is_none()); + } + + #[tokio::test] + async fn test_process_updates_with_empty_queue() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let applied = manager.process_updates().await.unwrap(); + assert_eq!(applied.len(), 0); + } + + #[tokio::test] + async fn test_config_update_type_detection() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let config = SubnetConfig { + version: "1.0.0".into(), + ..Default::default() + }; + + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config), + "1.0.0".into(), + ); + + let pending = manager.pending.read(); + assert_eq!(pending[0].update_type, UpdateType::Warm); + } + + #[tokio::test] + async fn test_wasm_update_type_detection() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "Test".into(), + wasm_hash: "hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 10, + }; + + manager.queue_update( + UpdateTarget::Challenge(challenge_id), + UpdatePayload::WasmChallenge { + wasm_bytes: vec![], + wasm_hash: "hash".into(), + config, + }, + "1.0.0".into(), + ); + + let pending = manager.pending.read(); + assert_eq!(pending[0].update_type, UpdateType::Hot); + } + + #[tokio::test] + async fn test_validators_update_type_detection() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + manager.queue_update( + UpdateTarget::Validators, + UpdatePayload::Validators { + add: vec![], + remove: vec![], + }, + "1.0.0".into(), + ); + + let pending = manager.pending.read(); + assert_eq!(pending[0].update_type, UpdateType::Hot); + } + + #[tokio::test] + async fn test_hard_reset_update_type_detection() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::HardReset { + reason: "test".into(), + preserve_validators: true, + new_config: None, + }, + "1.0.0".into(), + ); + + let pending = manager.pending.read(); + assert_eq!(pending[0].update_type, UpdateType::HardReset); + } + + #[test] + fn test_pending_count() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + assert_eq!(manager.pending_count(), 0); + + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(SubnetConfig::default()), + "1.0.0".into(), + ); + + assert_eq!(manager.pending_count(), 1); + + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(SubnetConfig::default()), + "1.1.0".into(), + ); + + assert_eq!(manager.pending_count(), 2); + } + + #[tokio::test] + async fn test_update_history() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let config = SubnetConfig { + version: "1.0.0".into(), + ..Default::default() + }; + + manager.queue_update( + UpdateTarget::Config, + UpdatePayload::Config(config), + "1.0.0".into(), + ); + + manager.process_updates().await.unwrap(); + + let history = manager.history.read(); + assert_eq!(history.len(), 1); + assert!(matches!(history[0].status, UpdateStatus::Applied)); + } + + #[tokio::test] + async fn test_process_updates_rolls_back_on_failure() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let bad_hash = "not_the_real_hash".to_string(); + let wasm_bytes = vec![1u8, 2, 3]; + + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "Rollback Challenge".into(), + wasm_hash: bad_hash.clone(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 300, + max_concurrent: 5, + }; + + manager.queue_update( + UpdateTarget::Challenge(challenge_id), + UpdatePayload::WasmChallenge { + wasm_bytes, + wasm_hash: bad_hash, + config, + }, + "1.0.0".into(), + ); + + // Ensure rollback data is present so failure triggers rollback path + let rollback_bytes = b"rollback-wasm".to_vec(); + { + let mut pending = manager.pending.write(); + pending[0].rollback_data = Some(rollback_bytes.clone()); + } + + // Prepare challenge directory for rollback write + let challenge_dir = dir + .path() + .join("challenges") + .join(challenge_id.0.to_string()); + std::fs::create_dir_all(&challenge_dir).unwrap(); + + let applied = manager.process_updates().await.unwrap(); + assert!(applied.is_empty()); + + let history = manager.history.read(); + assert_eq!(history.len(), 1); + assert!(matches!(history[0].status, UpdateStatus::RolledBack)); + + let rollback_path = challenge_dir.join("code.wasm"); + assert_eq!(std::fs::read(rollback_path).unwrap(), rollback_bytes); + } + + #[tokio::test] + async fn test_process_updates_handles_rollback_failure() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let wasm_bytes = vec![0u8, 1, 2]; + let bad_hash = "incorrect".to_string(); + + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "RollbackFail".into(), + wasm_hash: bad_hash.clone(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 60, + max_concurrent: 5, + }; + + manager.queue_update( + UpdateTarget::Challenge(challenge_id), + UpdatePayload::WasmChallenge { + wasm_bytes, + wasm_hash: bad_hash, + config, + }, + "1.0.0".into(), + ); + + { + let mut pending = manager.pending.write(); + pending[0].rollback_data = Some(b"rollback-data".to_vec()); + } + + let applied = manager.process_updates().await.unwrap(); + assert!(applied.is_empty()); + + let history = manager.history.read(); + assert_eq!(history.len(), 1); + assert!(matches!(history[0].status, UpdateStatus::Failed(_))); + } + + #[tokio::test] + async fn test_apply_update_wasm_challenge_success() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let wasm_bytes = vec![9u8, 8, 7, 6]; + let wasm_hash = UpdateManager::compute_hash(&wasm_bytes); + + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "ApplySuccess".into(), + wasm_hash: wasm_hash.clone(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 60, + max_concurrent: 5, + }; + + let mut update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Challenge(challenge_id), + payload: UpdatePayload::WasmChallenge { + wasm_bytes: wasm_bytes.clone(), + wasm_hash, + config, + }, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + manager.apply_update(&mut update).await.unwrap(); + + // Rollback data should have been captured and WASM written to disk + assert!(update.rollback_data.is_some()); + let wasm_path = dir + .path() + .join("challenges") + .join(challenge_id.0.to_string()) + .join("code.wasm"); + assert_eq!(std::fs::read(&wasm_path).unwrap(), wasm_bytes); + } + + #[tokio::test] + async fn test_apply_update_wasm_challenge_hash_mismatch() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let wasm_bytes = vec![1u8, 2, 3]; + + let config = ChallengeConfig { + id: challenge_id.0.to_string(), + name: "ApplyFail".into(), + wasm_hash: "expected_hash".into(), + wasm_source: "test".into(), + emission_weight: 1.0, + active: true, + timeout_secs: 60, + max_concurrent: 5, + }; + + let mut update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Challenge(challenge_id), + payload: UpdatePayload::WasmChallenge { + wasm_bytes: wasm_bytes.clone(), + wasm_hash: "expected_hash".into(), + config, + }, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + let err = manager.apply_update(&mut update).await.unwrap_err(); + match err { + UpdateError::HashMismatch { .. } => {} + other => panic!("unexpected error: {other:?}"), + } + + // No WASM should be written and rollback data remains None + let wasm_path = dir + .path() + .join("challenges") + .join(challenge_id.0.to_string()) + .join("code.wasm"); + assert!(!wasm_path.exists()); + assert!(update.rollback_data.is_none()); + } + + #[tokio::test] + async fn test_apply_update_validators_payload() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let add = vec![platform_core::Hotkey([1u8; 32])]; + let remove = vec![platform_core::Hotkey([2u8; 32])]; + + let mut update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Validators, + payload: UpdatePayload::Validators { + add: add.clone(), + remove: remove.clone(), + }, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + manager.apply_update(&mut update).await.unwrap(); + assert!(update.rollback_data.is_none()); + + let challenges_dir = dir.path().join("challenges"); + assert!( + !challenges_dir.exists(), + "validator updates should not touch disk state" + ); + } + + #[tokio::test] + async fn test_apply_update_hard_reset_clears_state_and_applies_config() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let validators_dir = dir.path().join("validators"); + std::fs::create_dir_all(&validators_dir).unwrap(); + std::fs::write(validators_dir.join("node"), b"validator").unwrap(); + + let challenges_dir = dir.path().join("challenges"); + std::fs::create_dir_all(challenges_dir.join("legacy")).unwrap(); + + let new_config = SubnetConfig { + version: "9.9.9".into(), + ..Default::default() + }; + + let mut update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::HardReset, + version: "9.9.9".into(), + target: UpdateTarget::Config, + payload: UpdatePayload::HardReset { + reason: "maintenance".into(), + preserve_validators: false, + new_config: Some(new_config.clone()), + }, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + manager.apply_update(&mut update).await.unwrap(); + + assert!(!validators_dir.exists()); + assert!(challenges_dir.exists()); + assert!(!challenges_dir.join("legacy").exists()); + assert!(dir.path().join("snapshots").join("pre_reset").exists()); + + let config_bytes = std::fs::read(dir.path().join("subnet_config.json")).unwrap(); + assert!(String::from_utf8(config_bytes).unwrap().contains("9.9.9")); + } + + #[tokio::test] + async fn test_apply_update_hard_reset_preserves_validators() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let validators_dir = dir.path().join("validators"); + std::fs::create_dir_all(&validators_dir).unwrap(); + std::fs::write(validators_dir.join("node"), b"validator").unwrap(); + + let mut update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::HardReset, + version: "1.0.0".into(), + target: UpdateTarget::Config, + payload: UpdatePayload::HardReset { + reason: "maintenance".into(), + preserve_validators: true, + new_config: None, + }, + status: UpdateStatus::Pending, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }; + + manager.apply_update(&mut update).await.unwrap(); + assert!(validators_dir.exists()); + } + + #[tokio::test] + async fn test_rollback_update_challenge_target() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let rollback_bytes = b"restore-wasm".to_vec(); + + let challenge_dir = dir + .path() + .join("challenges") + .join(challenge_id.0.to_string()); + std::fs::create_dir_all(&challenge_dir).unwrap(); + + let update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Challenge(challenge_id), + payload: UpdatePayload::Config(SubnetConfig::default()), + status: UpdateStatus::Failed("hash".into()), + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: Some(rollback_bytes.clone()), + }; + + manager.rollback_update(&update).await.unwrap(); + + let wasm_path = challenge_dir.join("code.wasm"); + assert_eq!(std::fs::read(wasm_path).unwrap(), rollback_bytes); + } + + #[tokio::test] + async fn test_rollback_update_config_target() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let rollback_bytes = br#"{"version":"2.0.0"}"#.to_vec(); + + let update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Warm, + version: "2.0.0".into(), + target: UpdateTarget::Config, + payload: UpdatePayload::Config(SubnetConfig::default()), + status: UpdateStatus::Failed("io".into()), + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: Some(rollback_bytes.clone()), + }; + + manager.rollback_update(&update).await.unwrap(); + let config_path = dir.path().join("subnet_config.json"); + assert_eq!(std::fs::read(config_path).unwrap(), rollback_bytes); + } + + #[tokio::test] + async fn test_rollback_update_for_unsupported_target() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + let update = Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0.0".into(), + target: UpdateTarget::Validators, + payload: UpdatePayload::Validators { + add: vec![], + remove: vec![], + }, + status: UpdateStatus::Failed("test".into()), + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: Some(vec![1, 2, 3]), + }; + + manager.rollback_update(&update).await.unwrap(); + let unsupported_dir = manager.data_dir.join("validators"); + assert!(!unsupported_dir.exists()); + } + + #[test] + fn test_history_method_returns_clone() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + manager.history.write().push(Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: "1.0".into(), + target: UpdateTarget::Config, + payload: UpdatePayload::Config(SubnetConfig::default()), + status: UpdateStatus::Applied, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }); + + let external_history = manager.history(); + assert_eq!(external_history.len(), 1); + + // Mutating returned vector should not affect manager's internal history + drop(external_history); + assert_eq!(manager.history.read().len(), 1); + } + + #[test] + fn test_prune_history_keeps_most_recent() { + let dir = tempdir().unwrap(); + let manager = UpdateManager::new(dir.path().to_path_buf()); + + for i in 0..5 { + manager.history.write().push(Update { + id: uuid::Uuid::new_v4(), + update_type: UpdateType::Hot, + version: format!("1.0.{}", i), + target: UpdateTarget::Config, + payload: UpdatePayload::Config(SubnetConfig::default()), + status: UpdateStatus::Applied, + created_at: chrono::Utc::now(), + applied_at: None, + rollback_data: None, + }); + } + + manager.prune_history(2); + let history = manager.history.read(); + assert_eq!(history.len(), 2); + assert!(history + .iter() + .all(|update| update.version == "1.0.3" || update.version == "1.0.4")); + } + + #[test] + fn test_update_target_challenge() { + let challenge_id = ChallengeId(uuid::Uuid::new_v4()); + let target = UpdateTarget::Challenge(challenge_id); + let json = serde_json::to_string(&target).unwrap(); + let decoded: UpdateTarget = serde_json::from_str(&json).unwrap(); + assert!(matches!(decoded, UpdateTarget::Challenge(_))); + } + + #[test] + fn test_update_target_all_challenges() { + let target = UpdateTarget::AllChallenges; + let json = serde_json::to_string(&target).unwrap(); + let decoded: UpdateTarget = serde_json::from_str(&json).unwrap(); + assert!(matches!(decoded, UpdateTarget::AllChallenges)); + } +} diff --git a/crates/wasm-runtime-interface/Cargo.toml b/crates/wasm-runtime-interface/Cargo.toml new file mode 100644 index 000000000..468b4619a --- /dev/null +++ b/crates/wasm-runtime-interface/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "wasm-runtime-interface" +version.workspace = true +edition.workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +tracing = { workspace = true } +chrono = { workspace = true } +ipnet = "2.11.0" +url = "2.5.8" +wasmtime = "41.0.3" +bincode = { workspace = true } +reqwest = { workspace = true, features = ["blocking", "rustls-tls"] } +trust-dns-resolver = "0.23.2" +sha2 = { workspace = true } +platform-challenge-sdk-wasm = { path = "../challenge-sdk-wasm" } diff --git a/crates/wasm-runtime-interface/src/bridge.rs b/crates/wasm-runtime-interface/src/bridge.rs new file mode 100644 index 000000000..9cfd29f9f --- /dev/null +++ b/crates/wasm-runtime-interface/src/bridge.rs @@ -0,0 +1,206 @@ +use platform_challenge_sdk_wasm::{EvaluationInput, EvaluationOutput}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EvalRequest { + pub request_id: String, + pub submission_id: String, + pub participant_id: String, + pub data: serde_json::Value, + pub metadata: Option, + pub epoch: u64, + pub deadline: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EvalResponse { + pub request_id: String, + pub success: bool, + pub error: Option, + pub score: f64, + pub results: serde_json::Value, + pub execution_time_ms: i64, + pub cost: Option, +} + +impl EvalResponse { + pub fn success(request_id: &str, score: f64, results: serde_json::Value) -> Self { + Self { + request_id: request_id.to_string(), + success: true, + error: None, + score, + results, + execution_time_ms: 0, + cost: None, + } + } + + pub fn error(request_id: &str, error: impl Into) -> Self { + Self { + request_id: request_id.to_string(), + success: false, + error: Some(error.into()), + score: 0.0, + results: serde_json::Value::Null, + execution_time_ms: 0, + cost: None, + } + } + + pub fn with_time(mut self, ms: i64) -> Self { + self.execution_time_ms = ms; + self + } + + pub fn with_cost(mut self, cost: f64) -> Self { + self.cost = Some(cost); + self + } +} + +pub fn request_to_input( + req: &EvalRequest, + challenge_id: &str, +) -> Result { + let agent_data = + serde_json::to_vec(&req.data).map_err(|e| BridgeError::Serialize(format!("data: {e}")))?; + + let params = match &req.metadata { + Some(meta) => serde_json::to_vec(meta) + .map_err(|e| BridgeError::Serialize(format!("metadata: {e}")))?, + None => Vec::new(), + }; + + Ok(EvaluationInput { + agent_data, + challenge_id: challenge_id.to_string(), + params, + task_definition: None, + environment_config: None, + }) +} + +pub fn input_to_bytes(input: &EvaluationInput) -> Result, BridgeError> { + bincode::serialize(input).map_err(|e| BridgeError::Serialize(e.to_string())) +} + +pub fn bytes_to_output(bytes: &[u8]) -> Result { + bincode::deserialize(bytes).map_err(|e| BridgeError::Deserialize(e.to_string())) +} + +pub fn output_to_response( + output: &EvaluationOutput, + request_id: &str, + execution_time_ms: i64, +) -> EvalResponse { + if output.valid { + let score = output.score as f64 / 10_000.0; + let results = serde_json::json!({ "message": output.message }); + EvalResponse::success(request_id, score, results).with_time(execution_time_ms) + } else { + EvalResponse::error(request_id, &output.message).with_time(execution_time_ms) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum BridgeError { + #[error("serialization error: {0}")] + Serialize(String), + #[error("deserialization error: {0}")] + Deserialize(String), +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn test_request_to_input() { + let req = EvalRequest { + request_id: "req-1".into(), + submission_id: "sub-1".into(), + participant_id: "part-1".into(), + data: json!({"code": "print('hello')"}), + metadata: Some(json!({"lang": "python"})), + epoch: 1, + deadline: None, + }; + + let input = request_to_input(&req, "test-challenge").unwrap(); + assert_eq!(input.challenge_id, "test-challenge"); + assert!(!input.agent_data.is_empty()); + assert!(!input.params.is_empty()); + + let data: serde_json::Value = serde_json::from_slice(&input.agent_data).unwrap(); + assert_eq!(data, json!({"code": "print('hello')"})); + + let meta: serde_json::Value = serde_json::from_slice(&input.params).unwrap(); + assert_eq!(meta, json!({"lang": "python"})); + } + + #[test] + fn test_request_to_input_no_metadata() { + let req = EvalRequest { + request_id: "req-1".into(), + submission_id: "sub-1".into(), + participant_id: "part-1".into(), + data: json!("test"), + metadata: None, + epoch: 0, + deadline: None, + }; + + let input = request_to_input(&req, "ch").unwrap(); + assert!(input.params.is_empty()); + } + + #[test] + fn test_roundtrip_input_bytes() { + let input = EvaluationInput { + agent_data: vec![1, 2, 3], + challenge_id: "test".into(), + params: vec![4, 5, 6], + task_definition: None, + environment_config: None, + }; + + let bytes = input_to_bytes(&input).unwrap(); + let recovered: EvaluationInput = bincode::deserialize(&bytes).unwrap(); + assert_eq!(recovered.agent_data, input.agent_data); + assert_eq!(recovered.challenge_id, input.challenge_id); + assert_eq!(recovered.params, input.params); + } + + #[test] + fn test_bytes_to_output() { + let output = EvaluationOutput::success(85, "great job"); + let bytes = bincode::serialize(&output).unwrap(); + let recovered = bytes_to_output(&bytes).unwrap(); + assert_eq!(recovered.score, 85); + assert!(recovered.valid); + assert_eq!(recovered.message, "great job"); + } + + #[test] + fn test_output_to_response_success() { + let output = EvaluationOutput::success(10000, "perfect"); + let resp = output_to_response(&output, "req-1", 42); + assert!(resp.success); + assert_eq!(resp.request_id, "req-1"); + assert!((resp.score - 1.0).abs() < f64::EPSILON); + assert_eq!(resp.execution_time_ms, 42); + assert!(resp.error.is_none()); + } + + #[test] + fn test_output_to_response_failure() { + let output = EvaluationOutput::failure("bad input"); + let resp = output_to_response(&output, "req-2", 10); + assert!(!resp.success); + assert_eq!(resp.request_id, "req-2"); + assert!((resp.score - 0.0).abs() < f64::EPSILON); + assert_eq!(resp.error.as_deref(), Some("bad input")); + } +} diff --git a/crates/wasm-runtime-interface/src/consensus.rs b/crates/wasm-runtime-interface/src/consensus.rs new file mode 100644 index 000000000..9254b523e --- /dev/null +++ b/crates/wasm-runtime-interface/src/consensus.rs @@ -0,0 +1,438 @@ +//! Consensus Host Functions for WASM Challenges +//! +//! This module provides host functions that allow WASM code to query +//! the P2P consensus state. All operations are gated by `ConsensusPolicy`. +//! +//! # Host Functions +//! +//! - `consensus_get_epoch() -> i64` โ€” Get current epoch number +//! - `consensus_get_validators(buf_ptr, buf_len) -> i32` โ€” Get active validator list +//! - `consensus_propose_weight(uid, weight) -> i32` โ€” Propose a weight for a UID +//! - `consensus_get_votes(buf_ptr, buf_len) -> i32` โ€” Get current weight votes +//! - `consensus_get_state_hash(buf_ptr) -> i32` โ€” Get current state hash (32 bytes) +//! - `consensus_get_submission_count() -> i32` โ€” Get pending submission count +//! - `consensus_get_block_height() -> i64` โ€” Get current logical block height + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +pub const HOST_CONSENSUS_NAMESPACE: &str = "platform_consensus"; +pub const HOST_CONSENSUS_GET_EPOCH: &str = "consensus_get_epoch"; +pub const HOST_CONSENSUS_GET_VALIDATORS: &str = "consensus_get_validators"; +pub const HOST_CONSENSUS_PROPOSE_WEIGHT: &str = "consensus_propose_weight"; +pub const HOST_CONSENSUS_GET_VOTES: &str = "consensus_get_votes"; +pub const HOST_CONSENSUS_GET_STATE_HASH: &str = "consensus_get_state_hash"; +pub const HOST_CONSENSUS_GET_SUBMISSION_COUNT: &str = "consensus_get_submission_count"; +pub const HOST_CONSENSUS_GET_BLOCK_HEIGHT: &str = "consensus_get_block_height"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum ConsensusHostStatus { + Success = 0, + Disabled = 1, + BufferTooSmall = -1, + ProposalLimitExceeded = -2, + InvalidArgument = -3, + InternalError = -100, +} + +impl ConsensusHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } + + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::Disabled, + -1 => Self::BufferTooSmall, + -2 => Self::ProposalLimitExceeded, + -3 => Self::InvalidArgument, + _ => Self::InternalError, + } + } +} + +/// Policy controlling WASM access to consensus state. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ConsensusPolicy { + pub enabled: bool, + pub allow_weight_proposals: bool, + pub max_weight_proposals: u32, +} + +impl Default for ConsensusPolicy { + fn default() -> Self { + Self { + enabled: true, + allow_weight_proposals: false, + max_weight_proposals: 0, + } + } +} + +impl ConsensusPolicy { + pub fn development() -> Self { + Self { + enabled: true, + allow_weight_proposals: true, + max_weight_proposals: 256, + } + } + + pub fn read_only() -> Self { + Self { + enabled: true, + allow_weight_proposals: false, + max_weight_proposals: 0, + } + } +} + +/// Mutable consensus state accessible from WASM host functions. +/// +/// Populated by the validator node before each WASM instantiation with +/// a snapshot of the current chain state. +pub struct ConsensusState { + pub policy: ConsensusPolicy, + pub epoch: u64, + pub block_height: u64, + pub state_hash: [u8; 32], + pub validators_json: Vec, + pub votes_json: Vec, + pub submission_count: u32, + pub weight_proposals_made: u32, + pub proposed_weights: Vec<(u16, u16)>, + pub challenge_id: String, + pub validator_id: String, +} + +impl ConsensusState { + pub fn new(policy: ConsensusPolicy, challenge_id: String, validator_id: String) -> Self { + Self { + policy, + epoch: 0, + block_height: 0, + state_hash: [0u8; 32], + validators_json: Vec::new(), + votes_json: Vec::new(), + submission_count: 0, + weight_proposals_made: 0, + proposed_weights: Vec::new(), + challenge_id, + validator_id, + } + } + + pub fn reset_counters(&mut self) { + self.weight_proposals_made = 0; + self.proposed_weights.clear(); + } +} + +#[derive(Clone, Debug)] +pub struct ConsensusHostFunctions; + +impl ConsensusHostFunctions { + pub fn new() -> Self { + Self + } +} + +impl Default for ConsensusHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for ConsensusHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_EPOCH, + |caller: Caller| -> i64 { handle_get_epoch(&caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_VALIDATORS, + |mut caller: Caller, buf_ptr: i32, buf_len: i32| -> i32 { + handle_get_validators(&mut caller, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_PROPOSE_WEIGHT, + |mut caller: Caller, uid: i32, weight: i32| -> i32 { + handle_propose_weight(&mut caller, uid, weight) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_VOTES, + |mut caller: Caller, buf_ptr: i32, buf_len: i32| -> i32 { + handle_get_votes(&mut caller, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_STATE_HASH, + |mut caller: Caller, buf_ptr: i32| -> i32 { + handle_get_state_hash(&mut caller, buf_ptr) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_SUBMISSION_COUNT, + |caller: Caller| -> i32 { handle_get_submission_count(&caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_CONSENSUS_NAMESPACE, + HOST_CONSENSUS_GET_BLOCK_HEIGHT, + |caller: Caller| -> i64 { handle_get_block_height(&caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +fn handle_get_epoch(caller: &Caller) -> i64 { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return -1; + } + state.epoch as i64 +} + +fn handle_get_validators(caller: &mut Caller, buf_ptr: i32, buf_len: i32) -> i32 { + let data = { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return ConsensusHostStatus::Disabled.to_i32(); + } + state.validators_json.clone() + }; + + if data.is_empty() { + return 0; + } + + if buf_len < 0 || (data.len() as i32) > buf_len { + return ConsensusHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &data) { + warn!(error = %err, "consensus_get_validators: failed to write to wasm memory"); + return ConsensusHostStatus::InternalError.to_i32(); + } + + data.len() as i32 +} + +fn handle_propose_weight(caller: &mut Caller, uid: i32, weight: i32) -> i32 { + if uid < 0 || weight < 0 { + return ConsensusHostStatus::InvalidArgument.to_i32(); + } + + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return ConsensusHostStatus::Disabled.to_i32(); + } + if !state.policy.allow_weight_proposals { + return ConsensusHostStatus::Disabled.to_i32(); + } + if state.weight_proposals_made >= state.policy.max_weight_proposals { + return ConsensusHostStatus::ProposalLimitExceeded.to_i32(); + } + + let state = &mut caller.data_mut().consensus_state; + state.weight_proposals_made += 1; + state.proposed_weights.push((uid as u16, weight as u16)); + + ConsensusHostStatus::Success.to_i32() +} + +fn handle_get_votes(caller: &mut Caller, buf_ptr: i32, buf_len: i32) -> i32 { + let data = { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return ConsensusHostStatus::Disabled.to_i32(); + } + state.votes_json.clone() + }; + + if data.is_empty() { + return 0; + } + + if buf_len < 0 || (data.len() as i32) > buf_len { + return ConsensusHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &data) { + warn!(error = %err, "consensus_get_votes: failed to write to wasm memory"); + return ConsensusHostStatus::InternalError.to_i32(); + } + + data.len() as i32 +} + +fn handle_get_state_hash(caller: &mut Caller, buf_ptr: i32) -> i32 { + let hash = { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return ConsensusHostStatus::Disabled.to_i32(); + } + state.state_hash + }; + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &hash) { + warn!(error = %err, "consensus_get_state_hash: failed to write to wasm memory"); + return ConsensusHostStatus::InternalError.to_i32(); + } + + ConsensusHostStatus::Success.to_i32() +} + +fn handle_get_submission_count(caller: &Caller) -> i32 { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return ConsensusHostStatus::Disabled.to_i32(); + } + state.submission_count as i32 +} + +fn handle_get_block_height(caller: &Caller) -> i64 { + let state = &caller.data().consensus_state; + if !state.policy.enabled { + return -1; + } + state.block_height as i64 +} + +fn write_wasm_memory( + caller: &mut Caller, + ptr: i32, + bytes: &[u8], +) -> Result<(), String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(bytes.len()) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data_mut(caller); + if end > data.len() { + return Err("memory write out of bounds".to_string()); + } + data[ptr..end].copy_from_slice(bytes); + Ok(()) +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_consensus_host_status_conversion() { + assert_eq!(ConsensusHostStatus::Success.to_i32(), 0); + assert_eq!(ConsensusHostStatus::Disabled.to_i32(), 1); + assert_eq!(ConsensusHostStatus::BufferTooSmall.to_i32(), -1); + assert_eq!(ConsensusHostStatus::ProposalLimitExceeded.to_i32(), -2); + assert_eq!(ConsensusHostStatus::InternalError.to_i32(), -100); + + assert_eq!( + ConsensusHostStatus::from_i32(0), + ConsensusHostStatus::Success + ); + assert_eq!( + ConsensusHostStatus::from_i32(1), + ConsensusHostStatus::Disabled + ); + assert_eq!( + ConsensusHostStatus::from_i32(-1), + ConsensusHostStatus::BufferTooSmall + ); + assert_eq!( + ConsensusHostStatus::from_i32(-999), + ConsensusHostStatus::InternalError + ); + } + + #[test] + fn test_consensus_policy_default() { + let policy = ConsensusPolicy::default(); + assert!(policy.enabled); + assert!(!policy.allow_weight_proposals); + assert_eq!(policy.max_weight_proposals, 0); + } + + #[test] + fn test_consensus_policy_development() { + let policy = ConsensusPolicy::development(); + assert!(policy.enabled); + assert!(policy.allow_weight_proposals); + assert_eq!(policy.max_weight_proposals, 256); + } + + #[test] + fn test_consensus_state_creation() { + let state = ConsensusState::new( + ConsensusPolicy::default(), + "test-challenge".to_string(), + "test-validator".to_string(), + ); + assert_eq!(state.epoch, 0); + assert_eq!(state.block_height, 0); + assert_eq!(state.submission_count, 0); + assert_eq!(state.weight_proposals_made, 0); + assert!(state.proposed_weights.is_empty()); + } + + #[test] + fn test_consensus_state_reset() { + let mut state = ConsensusState::new( + ConsensusPolicy::development(), + "test".to_string(), + "test".to_string(), + ); + state.weight_proposals_made = 5; + state.proposed_weights.push((0, 100)); + state.proposed_weights.push((1, 200)); + + state.reset_counters(); + + assert_eq!(state.weight_proposals_made, 0); + assert!(state.proposed_weights.is_empty()); + } +} diff --git a/crates/wasm-runtime-interface/src/container.rs b/crates/wasm-runtime-interface/src/container.rs new file mode 100644 index 000000000..81e926809 --- /dev/null +++ b/crates/wasm-runtime-interface/src/container.rs @@ -0,0 +1,197 @@ +//! Container Host Functions for WASM Challenges (DISABLED) +//! +//! This module previously provided host functions for container execution. +//! It is now disabled as part of the migration to WASM-only architecture. +//! All container operations return `Disabled` status. +//! +//! # Host Functions +//! +//! - `container_run(req_ptr, req_len, resp_ptr, resp_len) -> i32` - Always returns Disabled + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +pub const HOST_CONTAINER_NAMESPACE: &str = "platform_container"; +pub const HOST_CONTAINER_RUN: &str = "container_run"; + +/// Container host status codes +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum ContainerHostStatus { + /// Operation succeeded + Success = 0, + /// Container execution is disabled in WASM-only mode + Disabled = 1, + /// Image not in allowed list + ImageNotAllowed = -1, + /// Execution timeout + ExecutionTimeout = -2, + /// Execution failed + ExecutionFailed = -3, + /// Resource limit exceeded + ResourceLimitExceeded = -4, + /// Internal error + InternalError = -100, +} + +impl ContainerHostStatus { + /// Convert to i32 + pub fn to_i32(self) -> i32 { + self as i32 + } + + /// Convert from i32 + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::Disabled, + -1 => Self::ImageNotAllowed, + -2 => Self::ExecutionTimeout, + -3 => Self::ExecutionFailed, + -4 => Self::ResourceLimitExceeded, + _ => Self::InternalError, + } + } +} + +/// Container execution error types +#[derive(Debug, Serialize, Deserialize)] +pub enum ContainerExecError { + /// Container execution is disabled + Disabled, +} + +/// Container host functions - DISABLED +/// +/// All operations return Disabled status. This is a stub implementation +/// for backward compatibility with WASM modules that may reference +/// container host functions. +pub struct ContainerHostFunctions; + +impl ContainerHostFunctions { + /// Create new disabled container host functions + pub fn new() -> Self { + Self + } +} + +impl Default for ContainerHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for ContainerHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_CONTAINER_NAMESPACE, + HOST_CONTAINER_RUN, + |mut caller: Caller, + _req_ptr: i32, + _req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_container_run_disabled(&mut caller, resp_ptr, resp_len) + }, + ) + .map_err(|e| { + WasmRuntimeError::HostFunction(format!( + "failed to register {}: {}", + HOST_CONTAINER_RUN, e + )) + })?; + + Ok(()) + } +} + +/// Handle container run - always returns disabled +fn handle_container_run_disabled( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let result: Result<(), ContainerExecError> = Err(ContainerExecError::Disabled); + + let response_bytes = match bincode::serialize(&result) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "failed to serialize container response"); + return -1; + } + }; + + write_bytes(caller, resp_ptr, resp_len, &response_bytes) +} + +fn write_bytes( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + bytes: &[u8], +) -> i32 { + if resp_ptr < 0 || resp_len < 0 { + return -1; + } + if bytes.len() > i32::MAX as usize { + return -1; + } + let resp_len = resp_len as usize; + if bytes.len() > resp_len { + return -(bytes.len() as i32); + } + + let memory = match get_memory(caller) { + Some(memory) => memory, + None => return -1, + }; + + let ptr = resp_ptr as usize; + let end = match ptr.checked_add(bytes.len()) { + Some(end) => end, + None => return -1, + }; + let data = memory.data_mut(caller); + if end > data.len() { + return -1; + } + data[ptr..end].copy_from_slice(bytes); + bytes.len() as i32 +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_container_host_status_conversion() { + assert_eq!(ContainerHostStatus::Success.to_i32(), 0); + assert_eq!(ContainerHostStatus::Disabled.to_i32(), 1); + assert_eq!(ContainerHostStatus::ImageNotAllowed.to_i32(), -1); + assert_eq!(ContainerHostStatus::InternalError.to_i32(), -100); + + assert_eq!( + ContainerHostStatus::from_i32(0), + ContainerHostStatus::Success + ); + assert_eq!( + ContainerHostStatus::from_i32(1), + ContainerHostStatus::Disabled + ); + assert_eq!( + ContainerHostStatus::from_i32(-999), + ContainerHostStatus::InternalError + ); + } +} diff --git a/crates/wasm-runtime-interface/src/data.rs b/crates/wasm-runtime-interface/src/data.rs new file mode 100644 index 000000000..ab44b333f --- /dev/null +++ b/crates/wasm-runtime-interface/src/data.rs @@ -0,0 +1,455 @@ +//! Data Host Functions for WASM Challenges +//! +//! This module provides host functions that allow WASM code to load +//! challenge-specific data from the host. All operations are gated by `DataPolicy`. +//! +//! # Host Functions +//! +//! - `data_get(key_ptr, key_len, buf_ptr, buf_len) -> i32` - Read challenge data by key +//! - `data_list(prefix_ptr, prefix_len, buf_ptr, buf_len) -> i32` - List data keys under a prefix + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +pub const HOST_DATA_NAMESPACE: &str = "platform_data"; +pub const HOST_DATA_GET: &str = "data_get"; +pub const HOST_DATA_LIST: &str = "data_list"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum DataHostStatus { + Success = 0, + Disabled = 1, + NotFound = -1, + KeyTooLarge = -2, + BufferTooSmall = -3, + PathNotAllowed = -4, + IoError = -5, + InternalError = -100, +} + +impl DataHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } + + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::Disabled, + -1 => Self::NotFound, + -2 => Self::KeyTooLarge, + -3 => Self::BufferTooSmall, + -4 => Self::PathNotAllowed, + -5 => Self::IoError, + _ => Self::InternalError, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DataPolicy { + pub enabled: bool, + pub max_key_size: usize, + pub max_value_size: usize, + pub max_reads_per_execution: u32, +} + +impl Default for DataPolicy { + fn default() -> Self { + Self { + enabled: false, + max_key_size: 1024, + max_value_size: 10 * 1024 * 1024, + max_reads_per_execution: 64, + } + } +} + +impl DataPolicy { + pub fn development() -> Self { + Self { + enabled: true, + max_key_size: 4096, + max_value_size: 50 * 1024 * 1024, + max_reads_per_execution: 256, + } + } +} + +pub trait DataBackend: Send + Sync { + fn get(&self, challenge_id: &str, key: &str) -> Result>, DataError>; + fn list(&self, challenge_id: &str, prefix: &str) -> Result, DataError>; +} + +#[derive(Debug, thiserror::Error)] +pub enum DataError { + #[error("io error: {0}")] + Io(String), + #[error("key too large: {0}")] + KeyTooLarge(usize), + #[error("path not allowed: {0}")] + PathNotAllowed(String), +} + +pub struct NoopDataBackend; + +impl DataBackend for NoopDataBackend { + fn get(&self, _challenge_id: &str, _key: &str) -> Result>, DataError> { + Ok(None) + } + + fn list(&self, _challenge_id: &str, _prefix: &str) -> Result, DataError> { + Ok(Vec::new()) + } +} + +pub struct FilesystemDataBackend { + base_dir: PathBuf, +} + +impl FilesystemDataBackend { + pub fn new(base_dir: PathBuf) -> Self { + Self { base_dir } + } +} + +impl DataBackend for FilesystemDataBackend { + fn get(&self, challenge_id: &str, key: &str) -> Result>, DataError> { + let path = self.base_dir.join(challenge_id).join(key); + if !path.starts_with(self.base_dir.join(challenge_id)) { + return Err(DataError::PathNotAllowed(key.to_string())); + } + match std::fs::read(&path) { + Ok(data) => Ok(Some(data)), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(e) => Err(DataError::Io(e.to_string())), + } + } + + fn list(&self, challenge_id: &str, prefix: &str) -> Result, DataError> { + let dir = self.base_dir.join(challenge_id).join(prefix); + if !dir.starts_with(self.base_dir.join(challenge_id)) { + return Err(DataError::PathNotAllowed(prefix.to_string())); + } + let entries = match std::fs::read_dir(&dir) { + Ok(rd) => rd, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), + Err(e) => return Err(DataError::Io(e.to_string())), + }; + let mut names = Vec::new(); + for entry in entries { + match entry { + Ok(e) => { + if let Some(name) = e.file_name().to_str() { + names.push(name.to_string()); + } + } + Err(_) => continue, + } + } + Ok(names) + } +} + +pub struct DataState { + pub policy: DataPolicy, + pub backend: std::sync::Arc, + pub challenge_id: String, + pub reads: u32, +} + +impl DataState { + pub fn new( + policy: DataPolicy, + backend: std::sync::Arc, + challenge_id: String, + ) -> Self { + Self { + policy, + backend, + challenge_id, + reads: 0, + } + } + + pub fn reset_counters(&mut self) { + self.reads = 0; + } +} + +#[derive(Clone, Debug)] +pub struct DataHostFunctions; + +impl DataHostFunctions { + pub fn new() -> Self { + Self + } +} + +impl Default for DataHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for DataHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_DATA_NAMESPACE, + HOST_DATA_GET, + |mut caller: Caller, + key_ptr: i32, + key_len: i32, + buf_ptr: i32, + buf_len: i32| + -> i32 { + handle_data_get(&mut caller, key_ptr, key_len, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_DATA_NAMESPACE, + HOST_DATA_LIST, + |mut caller: Caller, + prefix_ptr: i32, + prefix_len: i32, + buf_ptr: i32, + buf_len: i32| + -> i32 { + handle_data_list(&mut caller, prefix_ptr, prefix_len, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +fn handle_data_get( + caller: &mut Caller, + key_ptr: i32, + key_len: i32, + buf_ptr: i32, + buf_len: i32, +) -> i32 { + if !caller.data().data_state.policy.enabled { + return DataHostStatus::Disabled.to_i32(); + } + + let key_bytes = match read_wasm_memory(caller, key_ptr, key_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "data_get: failed to read key from wasm memory"); + return DataHostStatus::InternalError.to_i32(); + } + }; + + let key_str = match std::str::from_utf8(&key_bytes) { + Ok(s) => s.to_string(), + Err(_) => return DataHostStatus::InternalError.to_i32(), + }; + + if key_bytes.len() > caller.data().data_state.policy.max_key_size { + return DataHostStatus::KeyTooLarge.to_i32(); + } + + if caller.data().data_state.reads >= caller.data().data_state.policy.max_reads_per_execution { + return DataHostStatus::InternalError.to_i32(); + } + + let challenge_id = caller.data().data_state.challenge_id.clone(); + let backend = std::sync::Arc::clone(&caller.data().data_state.backend); + + let value = match backend.get(&challenge_id, &key_str) { + Ok(Some(v)) => v, + Ok(None) => return DataHostStatus::NotFound.to_i32(), + Err(err) => { + warn!(error = %err, "data_get: backend read failed"); + return DataHostStatus::IoError.to_i32(); + } + }; + + caller.data_mut().data_state.reads += 1; + + if buf_len < 0 || value.len() > buf_len as usize { + return DataHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &value) { + warn!(error = %err, "data_get: failed to write value to wasm memory"); + return DataHostStatus::InternalError.to_i32(); + } + + value.len() as i32 +} + +fn handle_data_list( + caller: &mut Caller, + prefix_ptr: i32, + prefix_len: i32, + buf_ptr: i32, + buf_len: i32, +) -> i32 { + if !caller.data().data_state.policy.enabled { + return DataHostStatus::Disabled.to_i32(); + } + + let prefix_bytes = match read_wasm_memory(caller, prefix_ptr, prefix_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "data_list: failed to read prefix from wasm memory"); + return DataHostStatus::InternalError.to_i32(); + } + }; + + let prefix_str = match std::str::from_utf8(&prefix_bytes) { + Ok(s) => s.to_string(), + Err(_) => return DataHostStatus::InternalError.to_i32(), + }; + + let challenge_id = caller.data().data_state.challenge_id.clone(); + let backend = std::sync::Arc::clone(&caller.data().data_state.backend); + + let entries = match backend.list(&challenge_id, &prefix_str) { + Ok(e) => e, + Err(err) => { + warn!(error = %err, "data_list: backend list failed"); + return DataHostStatus::IoError.to_i32(); + } + }; + + caller.data_mut().data_state.reads += 1; + + let result = entries.join("\n"); + let result_bytes = result.as_bytes(); + + if buf_len < 0 || result_bytes.len() > buf_len as usize { + return DataHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, result_bytes) { + warn!(error = %err, "data_list: failed to write to wasm memory"); + return DataHostStatus::InternalError.to_i32(); + } + + result_bytes.len() as i32 +} + +fn read_wasm_memory( + caller: &mut Caller, + ptr: i32, + len: i32, +) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_wasm_memory( + caller: &mut Caller, + ptr: i32, + bytes: &[u8], +) -> Result<(), String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(bytes.len()) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data_mut(caller); + if end > data.len() { + return Err("memory write out of bounds".to_string()); + } + data[ptr..end].copy_from_slice(bytes); + Ok(()) +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_data_host_status_conversion() { + assert_eq!(DataHostStatus::Success.to_i32(), 0); + assert_eq!(DataHostStatus::Disabled.to_i32(), 1); + assert_eq!(DataHostStatus::NotFound.to_i32(), -1); + assert_eq!(DataHostStatus::InternalError.to_i32(), -100); + + assert_eq!(DataHostStatus::from_i32(0), DataHostStatus::Success); + assert_eq!(DataHostStatus::from_i32(1), DataHostStatus::Disabled); + assert_eq!( + DataHostStatus::from_i32(-999), + DataHostStatus::InternalError + ); + } + + #[test] + fn test_data_policy_default() { + let policy = DataPolicy::default(); + assert!(!policy.enabled); + assert_eq!(policy.max_key_size, 1024); + } + + #[test] + fn test_data_policy_development() { + let policy = DataPolicy::development(); + assert!(policy.enabled); + assert_eq!(policy.max_key_size, 4096); + } + + #[test] + fn test_noop_data_backend() { + let backend = NoopDataBackend; + assert!(backend.get("challenge-1", "key1").unwrap().is_none()); + assert!(backend.list("challenge-1", "").unwrap().is_empty()); + } + + #[test] + fn test_data_state_creation() { + let state = DataState::new( + DataPolicy::default(), + std::sync::Arc::new(NoopDataBackend), + "test".to_string(), + ); + assert_eq!(state.reads, 0); + } + + #[test] + fn test_data_state_reset() { + let mut state = DataState::new( + DataPolicy::default(), + std::sync::Arc::new(NoopDataBackend), + "test".to_string(), + ); + state.reads = 10; + state.reset_counters(); + assert_eq!(state.reads, 0); + } +} diff --git a/crates/wasm-runtime-interface/src/exec.rs b/crates/wasm-runtime-interface/src/exec.rs new file mode 100644 index 000000000..bcca8dd0e --- /dev/null +++ b/crates/wasm-runtime-interface/src/exec.rs @@ -0,0 +1,620 @@ +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::process::Command; +use std::time::{Duration, Instant}; +use tracing::{info, warn}; +use wasmtime::{Caller, Linker, Memory}; + +pub const HOST_EXEC_NAMESPACE: &str = "platform_exec"; +pub const HOST_EXEC_COMMAND: &str = "exec_command"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ExecHostFunction { + ExecCommand, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ExecPolicy { + pub enabled: bool, + pub allowed_commands: Vec, + pub timeout_ms: u64, + pub max_output_bytes: u64, + pub max_executions: u32, + pub allowed_env_vars: Vec, + pub blocked_args: Vec, +} + +impl Default for ExecPolicy { + fn default() -> Self { + Self { + enabled: false, + allowed_commands: Vec::new(), + timeout_ms: 5_000, + max_output_bytes: 512 * 1024, + max_executions: 8, + allowed_env_vars: Vec::new(), + blocked_args: vec![ + "..".to_string(), + "/etc".to_string(), + "/proc".to_string(), + "/sys".to_string(), + ], + } + } +} + +impl ExecPolicy { + pub fn development() -> Self { + Self { + enabled: true, + allowed_commands: vec![ + "echo".to_string(), + "cat".to_string(), + "ls".to_string(), + "wc".to_string(), + "grep".to_string(), + "head".to_string(), + "tail".to_string(), + ], + timeout_ms: 15_000, + max_output_bytes: 2 * 1024 * 1024, + max_executions: 32, + allowed_env_vars: Vec::new(), + blocked_args: vec![ + "..".to_string(), + "/etc/shadow".to_string(), + "/etc/passwd".to_string(), + ], + } + } + + pub fn is_command_allowed(&self, command: &str) -> bool { + if !self.enabled { + return false; + } + self.allowed_commands.iter().any(|c| c == command) + } + + pub fn are_args_allowed(&self, args: &[String]) -> bool { + for arg in args { + for blocked in &self.blocked_args { + if arg.contains(blocked.as_str()) { + return false; + } + } + } + true + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ExecRequest { + pub command: String, + pub args: Vec, + pub env: HashMap, + pub stdin: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ExecResponse { + pub exit_code: i32, + pub stdout: Vec, + pub stderr: Vec, +} + +#[derive(Debug, thiserror::Error, Serialize, Deserialize)] +pub enum ExecError { + #[error("exec disabled")] + Disabled, + #[error("command not allowed: {0}")] + CommandNotAllowed(String), + #[error("args not allowed: {0}")] + ArgsNotAllowed(String), + #[error("env var not allowed: {0}")] + EnvVarNotAllowed(String), + #[error("execution limit exceeded")] + LimitExceeded, + #[error("execution timeout")] + Timeout, + #[error("output too large: {0}")] + OutputTooLarge(u64), + #[error("execution failed: {0}")] + Failed(String), +} + +pub struct ExecState { + policy: ExecPolicy, + executions: u32, + challenge_id: String, + validator_id: String, +} + +impl ExecState { + pub fn new(policy: ExecPolicy, challenge_id: String, validator_id: String) -> Self { + Self { + policy, + executions: 0, + challenge_id, + validator_id, + } + } + + pub fn executions(&self) -> u32 { + self.executions + } + + pub fn reset_counters(&mut self) { + self.executions = 0; + } + + pub fn handle_exec(&mut self, request: ExecRequest) -> Result { + if !self.policy.enabled { + return Err(ExecError::Disabled); + } + + if !self.policy.is_command_allowed(&request.command) { + warn!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + command = %request.command, + "exec command not allowed" + ); + return Err(ExecError::CommandNotAllowed(request.command)); + } + + if !self.policy.are_args_allowed(&request.args) { + warn!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + command = %request.command, + "exec args not allowed" + ); + return Err(ExecError::ArgsNotAllowed(request.args.join(" "))); + } + + for key in request.env.keys() { + if !self.policy.allowed_env_vars.is_empty() + && !self.policy.allowed_env_vars.contains(key) + { + return Err(ExecError::EnvVarNotAllowed(key.clone())); + } + } + + if self.executions >= self.policy.max_executions { + return Err(ExecError::LimitExceeded); + } + + self.executions = self.executions.saturating_add(1); + + let start = Instant::now(); + let timeout = Duration::from_millis(self.policy.timeout_ms); + + let mut cmd = Command::new(&request.command); + cmd.args(&request.args); + cmd.env_clear(); + for (key, value) in &request.env { + cmd.env(key, value); + } + + if !request.stdin.is_empty() { + cmd.stdin(std::process::Stdio::piped()); + } else { + cmd.stdin(std::process::Stdio::null()); + } + cmd.stdout(std::process::Stdio::piped()); + cmd.stderr(std::process::Stdio::piped()); + + let mut child = cmd.spawn().map_err(|e| ExecError::Failed(e.to_string()))?; + + if !request.stdin.is_empty() { + if let Some(ref mut stdin) = child.stdin { + use std::io::Write; + let _ = stdin.write_all(&request.stdin); + } + child.stdin.take(); + } + + let output = loop { + if start.elapsed() > timeout { + let _ = child.kill(); + return Err(ExecError::Timeout); + } + match child.try_wait() { + Ok(Some(_)) => { + break child + .wait_with_output() + .map_err(|e| ExecError::Failed(e.to_string()))? + } + Ok(None) => std::thread::sleep(Duration::from_millis(10)), + Err(e) => return Err(ExecError::Failed(e.to_string())), + } + }; + + let stdout_len = output.stdout.len() as u64; + let stderr_len = output.stderr.len() as u64; + let total = stdout_len.saturating_add(stderr_len); + if total > self.policy.max_output_bytes { + return Err(ExecError::OutputTooLarge(total)); + } + + info!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + command = %request.command, + exit_code = output.status.code().unwrap_or(-1), + stdout_bytes = stdout_len, + stderr_bytes = stderr_len, + elapsed_ms = start.elapsed().as_millis() as u64, + "exec command completed" + ); + + Ok(ExecResponse { + exit_code: output.status.code().unwrap_or(-1), + stdout: output.stdout, + stderr: output.stderr, + }) + } +} + +#[derive(Clone, Debug)] +pub struct ExecHostFunctions { + enabled: Vec, +} + +impl ExecHostFunctions { + pub fn new(enabled: Vec) -> Self { + Self { enabled } + } + + pub fn all() -> Self { + Self { + enabled: vec![ExecHostFunction::ExecCommand], + } + } +} + +impl Default for ExecHostFunctions { + fn default() -> Self { + Self::all() + } +} + +impl HostFunctionRegistrar for ExecHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + if self.enabled.contains(&ExecHostFunction::ExecCommand) { + linker + .func_wrap( + HOST_EXEC_NAMESPACE, + HOST_EXEC_COMMAND, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_exec_command(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + Ok(()) + } +} + +fn handle_exec_command( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "exec host memory read failed" + ); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(ExecError::Failed(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "exec request decode failed" + ); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(ExecError::Failed(format!("invalid exec request: {err}"))), + ); + } + }; + + let result = caller.data_mut().exec_state.handle_exec(request); + if let Err(ref err) = result { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "exec command denied" + ); + } + write_result(caller, resp_ptr, resp_len, result) +} + +fn read_memory(caller: &mut Caller, ptr: i32, len: i32) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_result( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + result: Result, +) -> i32 { + let response_bytes = match bincode::serialize(&result) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "failed to serialize exec response"); + return -1; + } + }; + + write_bytes(caller, resp_ptr, resp_len, &response_bytes) +} + +fn write_bytes( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + bytes: &[u8], +) -> i32 { + if resp_ptr < 0 || resp_len < 0 { + return -1; + } + if bytes.len() > i32::MAX as usize { + return -1; + } + let resp_len = resp_len as usize; + if bytes.len() > resp_len { + return -(bytes.len() as i32); + } + + let memory = match get_memory(caller) { + Some(memory) => memory, + None => return -1, + }; + + let ptr = resp_ptr as usize; + let end = match ptr.checked_add(bytes.len()) { + Some(end) => end, + None => return -1, + }; + let data = memory.data_mut(caller); + if end > data.len() { + return -1; + } + data[ptr..end].copy_from_slice(bytes); + bytes.len() as i32 +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_exec_policy_default_disabled() { + let policy = ExecPolicy::default(); + assert!(!policy.enabled); + assert!(policy.allowed_commands.is_empty()); + } + + #[test] + fn test_exec_policy_development() { + let policy = ExecPolicy::development(); + assert!(policy.enabled); + assert!(policy.is_command_allowed("echo")); + assert!(policy.is_command_allowed("cat")); + assert!(!policy.is_command_allowed("rm")); + } + + #[test] + fn test_exec_policy_command_allowlist() { + let policy = ExecPolicy { + enabled: true, + allowed_commands: vec!["echo".to_string(), "ls".to_string()], + ..Default::default() + }; + + assert!(policy.is_command_allowed("echo")); + assert!(policy.is_command_allowed("ls")); + assert!(!policy.is_command_allowed("rm")); + assert!(!policy.is_command_allowed("cat")); + } + + #[test] + fn test_exec_policy_disabled_blocks_all() { + let policy = ExecPolicy { + enabled: false, + allowed_commands: vec!["echo".to_string()], + ..Default::default() + }; + + assert!(!policy.is_command_allowed("echo")); + } + + #[test] + fn test_exec_policy_blocked_args() { + let policy = ExecPolicy::default(); + + assert!(!policy.are_args_allowed(&["../../../etc/passwd".to_string()])); + assert!(!policy.are_args_allowed(&["/etc/shadow".to_string()])); + assert!(!policy.are_args_allowed(&["/proc/self/maps".to_string()])); + assert!(policy.are_args_allowed(&["hello".to_string(), "world".to_string()])); + } + + #[test] + fn test_exec_state_creation() { + let state = ExecState::new( + ExecPolicy::development(), + "test-challenge".into(), + "test-validator".into(), + ); + assert_eq!(state.executions(), 0); + } + + #[test] + fn test_exec_state_disabled() { + let mut state = ExecState::new(ExecPolicy::default(), "test".into(), "test".into()); + + let req = ExecRequest { + command: "echo".to_string(), + args: vec!["hello".to_string()], + env: HashMap::new(), + stdin: Vec::new(), + }; + + let err = state.handle_exec(req).unwrap_err(); + assert!(matches!(err, ExecError::Disabled)); + } + + #[test] + fn test_exec_state_command_not_allowed() { + let mut state = ExecState::new(ExecPolicy::development(), "test".into(), "test".into()); + + let req = ExecRequest { + command: "rm".to_string(), + args: vec!["-rf".to_string(), "/".to_string()], + env: HashMap::new(), + stdin: Vec::new(), + }; + + let err = state.handle_exec(req).unwrap_err(); + assert!(matches!(err, ExecError::CommandNotAllowed(_))); + } + + #[test] + fn test_exec_state_limit_exceeded() { + let mut state = ExecState::new( + ExecPolicy { + enabled: true, + allowed_commands: vec!["echo".to_string()], + max_executions: 1, + ..Default::default() + }, + "test".into(), + "test".into(), + ); + + let req = ExecRequest { + command: "echo".to_string(), + args: vec!["hello".to_string()], + env: HashMap::new(), + stdin: Vec::new(), + }; + + let result = state.handle_exec(req.clone()); + assert!(result.is_ok()); + + let err = state.handle_exec(req).unwrap_err(); + assert!(matches!(err, ExecError::LimitExceeded)); + } + + #[test] + fn test_exec_state_reset_counters() { + let mut state = ExecState::new(ExecPolicy::development(), "test".into(), "test".into()); + + state.executions = 5; + state.reset_counters(); + assert_eq!(state.executions(), 0); + } + + #[test] + fn test_exec_state_env_var_not_allowed() { + let mut state = ExecState::new( + ExecPolicy { + enabled: true, + allowed_commands: vec!["echo".to_string()], + allowed_env_vars: vec!["PATH".to_string()], + ..Default::default() + }, + "test".into(), + "test".into(), + ); + + let mut env = HashMap::new(); + env.insert("SECRET".to_string(), "value".to_string()); + + let req = ExecRequest { + command: "echo".to_string(), + args: vec!["hello".to_string()], + env, + stdin: Vec::new(), + }; + + let err = state.handle_exec(req).unwrap_err(); + assert!(matches!(err, ExecError::EnvVarNotAllowed(_))); + } + + #[test] + fn test_exec_echo_command() { + let mut state = ExecState::new( + ExecPolicy { + enabled: true, + allowed_commands: vec!["echo".to_string()], + ..Default::default() + }, + "test".into(), + "test".into(), + ); + + let req = ExecRequest { + command: "echo".to_string(), + args: vec!["hello".to_string()], + env: HashMap::new(), + stdin: Vec::new(), + }; + + let resp = state.handle_exec(req).unwrap(); + assert_eq!(resp.exit_code, 0); + assert_eq!(String::from_utf8_lossy(&resp.stdout).trim(), "hello"); + assert_eq!(state.executions(), 1); + } +} diff --git a/crates/wasm-runtime-interface/src/lib.rs b/crates/wasm-runtime-interface/src/lib.rs new file mode 100644 index 000000000..6edd452d5 --- /dev/null +++ b/crates/wasm-runtime-interface/src/lib.rs @@ -0,0 +1,800 @@ +//! WASM runtime host interface definitions for network access. +//! +//! This crate defines the host function surface exposed to challenge WASM +//! modules for controlled internet access. The interface is declarative so +//! runtimes can enforce deterministic, auditable behavior across validators. + +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::net::IpAddr; +use std::str::FromStr; + +pub mod bridge; +pub mod consensus; +pub mod container; +pub mod data; +pub mod exec; +pub mod llm; +pub mod network; +pub mod runtime; +pub mod sandbox; +pub mod storage; +pub mod terminal; +pub mod time; +pub use bridge::{ + bytes_to_output, input_to_bytes, output_to_response, request_to_input, BridgeError, + EvalRequest, EvalResponse, +}; +pub use exec::{ + ExecError, ExecHostFunction, ExecHostFunctions, ExecPolicy, ExecRequest, ExecResponse, + ExecState, +}; +pub use network::{ + NetworkHostFunctions, NetworkState, NetworkStateError, HOST_GET_TIMESTAMP, HOST_LOG_MESSAGE, +}; +pub use sandbox::{ + SandboxExecError, SandboxExecRequest, SandboxExecResponse, SandboxHostFunctions, + HOST_SANDBOX_CONFIGURE, HOST_SANDBOX_EXEC, HOST_SANDBOX_GET_TASKS, HOST_SANDBOX_GET_TIMESTAMP, + HOST_SANDBOX_LOG_MESSAGE, HOST_SANDBOX_NAMESPACE, HOST_SANDBOX_STATUS, +}; +pub use storage::{ + InMemoryStorageBackend, NoopStorageBackend, StorageAuditEntry, StorageAuditLogger, + StorageBackend, StorageDeleteRequest, StorageGetRequest, StorageGetResponse, StorageHostConfig, + StorageHostError, StorageHostFunctions, StorageHostState, StorageHostStatus, StorageOperation, + StorageProposeWriteRequest, StorageProposeWriteResponse, +}; + +pub const HOST_FUNCTION_NAMESPACE: &str = "platform_network"; +pub const HOST_HTTP_REQUEST: &str = "http_request"; +pub const HOST_HTTP_GET: &str = "http_get"; +pub const HOST_HTTP_POST: &str = "http_post"; +pub const HOST_DNS_RESOLVE: &str = "dns_resolve"; + +pub use consensus::{ + ConsensusHostFunctions, ConsensusHostStatus, ConsensusPolicy, ConsensusState, + HOST_CONSENSUS_NAMESPACE, +}; +pub use container::{ + ContainerExecError, ContainerHostFunctions, ContainerHostStatus, ContainerPolicy, + ContainerRunRequest, ContainerRunResponse, ContainerState, HOST_CONTAINER_NAMESPACE, + HOST_CONTAINER_RUN, +}; +pub use data::{ + DataBackend, DataError, DataHostFunctions, DataHostStatus, DataPolicy, DataState, + FilesystemDataBackend, NoopDataBackend, HOST_DATA_GET, HOST_DATA_LIST, HOST_DATA_NAMESPACE, +}; +pub use llm::{LlmHostFunctions, LlmHostStatus, LlmPolicy, LlmState, HOST_LLM_NAMESPACE}; +pub use runtime::{ + ChallengeInstance, HostFunctionRegistrar, InstanceConfig, RuntimeConfig, RuntimeState, + WasmModule, WasmRuntime, WasmRuntimeError, +}; +pub use storage::{ + HOST_STORAGE_ALLOC, HOST_STORAGE_DELETE, HOST_STORAGE_GET, HOST_STORAGE_GET_RESULT, + HOST_STORAGE_NAMESPACE, HOST_STORAGE_PROPOSE_WRITE, HOST_STORAGE_SET, +}; +pub use terminal::{ + TerminalHostFunctions, TerminalHostStatus, TerminalPolicy, TerminalState, + HOST_TERMINAL_NAMESPACE, +}; +pub use time::{TimeError, TimeHostFunction, TimeHostFunctions, TimeMode, TimePolicy, TimeState}; + +/// Host functions that may be exposed to WASM challenges. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum HostFunction { + HttpRequest, + HttpGet, + HttpPost, + DnsResolve, +} + +/// Network policy for WASM runtime enforcement. +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct NetworkPolicy { + /// Whether outbound internet access is allowed. + pub allow_internet: bool, + /// HTTP access rules. + pub http: HttpPolicy, + /// Allowed outbound IP CIDR ranges. + pub allowed_ip_ranges: Vec, + /// DNS resolution policy. + pub dns_policy: DnsPolicy, + /// Request/response limits. + pub limits: RequestLimits, + /// Audit logging policy for network calls. + pub audit: AuditPolicy, +} + +/// Sandbox policy for challenge WASM modules. +/// +/// Controls whether sandbox command execution is permitted and enforces +/// resource limits on spawned processes. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SandboxPolicy { + /// Whether sandbox execution is enabled. + pub enable_sandbox: bool, + /// Commands the WASM module is allowed to invoke. + pub allowed_commands: Vec, + /// Maximum wall-clock execution time in seconds per command. + pub max_execution_time_secs: u64, +} + +impl Default for SandboxPolicy { + fn default() -> Self { + Self { + enable_sandbox: false, + allowed_commands: Vec::new(), + max_execution_time_secs: 30, + } + } +} + +impl SandboxPolicy { + /// Permissive sandbox policy for development. + pub fn development() -> Self { + Self { + enable_sandbox: true, + allowed_commands: vec!["*".to_string()], + max_execution_time_secs: 120, + } + } + + /// Default challenge sandbox policy. + pub fn default_challenge() -> Self { + Self { + enable_sandbox: true, + allowed_commands: vec![ + "bash".to_string(), + "sh".to_string(), + "python3".to_string(), + "node".to_string(), + ], + max_execution_time_secs: 60, + } + } +} + +impl NetworkPolicy { + /// Strict policy with explicit allow-list and HTTPS only. + pub fn strict(allowed_hosts: Vec) -> Self { + Self { + allow_internet: true, + http: HttpPolicy { + allowed_hosts, + ..HttpPolicy::default() + }, + ..Default::default() + } + } + + /// Development policy with relaxed defaults. + pub fn development() -> Self { + Self { + allow_internet: true, + http: HttpPolicy::development(), + dns_policy: DnsPolicy::development(), + limits: RequestLimits::development(), + audit: AuditPolicy::development(), + ..Default::default() + } + } + + /// Validate and normalize network policy for runtime enforcement. + pub fn validate(&self) -> Result { + let http = self.http.validate()?; + let dns = self.dns_policy.validate()?; + let allowed_ip_ranges = parse_ip_ranges(&self.allowed_ip_ranges)?; + + Ok(ValidatedNetworkPolicy { + allow_internet: self.allow_internet, + http, + allowed_ip_ranges, + dns_policy: dns, + limits: self.limits.clone(), + audit: self.audit.clone(), + }) + } +} + +/// HTTP-specific access policy. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HttpPolicy { + /// Allowed outbound hostnames or suffixes. + pub allowed_hosts: Vec, + /// Allowed URL schemes (https only in production). + pub allowed_schemes: Vec, + /// Allowed outbound TCP ports. + pub allowed_ports: Vec, +} + +impl Default for HttpPolicy { + fn default() -> Self { + Self { + allowed_hosts: Vec::new(), + allowed_schemes: vec![HttpScheme::Https], + allowed_ports: vec![443], + } + } +} + +impl HttpPolicy { + /// Development HTTP policy with relaxed defaults. + pub fn development() -> Self { + Self { + allowed_schemes: vec![HttpScheme::Https, HttpScheme::Http], + allowed_ports: vec![80, 443], + ..Default::default() + } + } + + fn validate(&self) -> Result { + let allowed_hosts = normalize_hosts(&self.allowed_hosts)?; + let allowed_ports = normalize_ports(&self.allowed_ports)?; + + Ok(ValidatedHttpPolicy { + allowed_hosts, + allowed_schemes: self.allowed_schemes.clone(), + allowed_ports, + }) + } +} + +/// Supported HTTP schemes for outbound requests. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum HttpScheme { + Http, + Https, +} + +/// DNS resolution policy for WASM network calls. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DnsPolicy { + /// Whether DNS resolution is allowed. + pub enabled: bool, + /// Allowed DNS hostnames or suffixes. + pub allowed_hosts: Vec, + /// Allowed DNS query types (A/AAAA/CNAME, etc.). + pub allowed_record_types: Vec, + /// Maximum DNS lookups per execution. + pub max_lookups: u32, + /// Cache TTL in seconds for deterministic resolution. + pub cache_ttl_secs: u64, + /// Whether to block private or loopback ranges. + pub block_private_ranges: bool, +} + +impl Default for DnsPolicy { + fn default() -> Self { + Self { + enabled: false, + allowed_hosts: Vec::new(), + allowed_record_types: vec![DnsRecordType::A, DnsRecordType::Aaaa], + max_lookups: 8, + cache_ttl_secs: 60, + block_private_ranges: true, + } + } +} + +impl DnsPolicy { + /// Development DNS policy. + pub fn development() -> Self { + Self { + enabled: true, + max_lookups: 32, + cache_ttl_secs: 10, + block_private_ranges: false, + ..Default::default() + } + } + + fn validate(&self) -> Result { + let allowed_hosts = normalize_hosts(&self.allowed_hosts)?; + + Ok(ValidatedDnsPolicy { + enabled: self.enabled, + allowed_hosts, + allowed_record_types: self.allowed_record_types.clone(), + max_lookups: self.max_lookups, + cache_ttl_secs: self.cache_ttl_secs, + block_private_ranges: self.block_private_ranges, + }) + } +} + +/// DNS record types permitted. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum DnsRecordType { + A, + Aaaa, + Cname, + Txt, +} + +/// Request/response limits enforced by the host. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct RequestLimits { + /// Maximum request body size in bytes. + pub max_request_bytes: u64, + /// Maximum response body size in bytes. + pub max_response_bytes: u64, + /// Maximum total headers size in bytes. + pub max_header_bytes: u64, + /// Per-request timeout in milliseconds. + pub timeout_ms: u64, + /// Maximum number of HTTP requests per execution. + pub max_requests: u32, + /// Maximum redirects permitted per request. + pub max_redirects: u8, +} + +impl Default for RequestLimits { + fn default() -> Self { + Self { + max_request_bytes: 256 * 1024, + max_response_bytes: 512 * 1024, + max_header_bytes: 32 * 1024, + timeout_ms: 5_000, + max_requests: 8, + max_redirects: 2, + } + } +} + +impl RequestLimits { + /// Development-friendly limits. + pub fn development() -> Self { + Self { + max_request_bytes: 1024 * 1024, + max_response_bytes: 2 * 1024 * 1024, + max_header_bytes: 64 * 1024, + timeout_ms: 15_000, + max_requests: 32, + max_redirects: 4, + } + } +} + +/// Audit logging configuration for network access. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AuditPolicy { + /// Whether to emit audit events. + pub enabled: bool, + /// Whether to include request headers in logs. + pub log_headers: bool, + /// Whether to include request/response bodies in logs. + pub log_bodies: bool, + /// Additional tags to attach to audit events. + pub tags: HashMap, +} + +impl Default for AuditPolicy { + fn default() -> Self { + Self { + enabled: true, + log_headers: false, + log_bodies: false, + tags: HashMap::new(), + } + } +} + +impl AuditPolicy { + /// Development audit policy. + pub fn development() -> Self { + Self { + enabled: true, + log_headers: true, + log_bodies: false, + tags: HashMap::new(), + } + } +} + +/// Normalized policy for runtime enforcement. +#[derive(Debug, Clone)] +pub struct ValidatedNetworkPolicy { + pub allow_internet: bool, + pub http: ValidatedHttpPolicy, + pub allowed_ip_ranges: Vec, + pub dns_policy: ValidatedDnsPolicy, + pub limits: RequestLimits, + pub audit: AuditPolicy, +} + +impl ValidatedNetworkPolicy { + /// Validate an outbound HTTP request against policy. + pub fn is_http_request_allowed(&self, url: &str) -> Result<(), NetworkPolicyError> { + if !self.allow_internet { + return Err(NetworkPolicyError::NetworkDisabled); + } + + let parsed = + url::Url::parse(url).map_err(|err| NetworkPolicyError::InvalidUrl(err.to_string()))?; + let scheme = match parsed.scheme() { + "http" => HttpScheme::Http, + "https" => HttpScheme::Https, + other => return Err(NetworkPolicyError::SchemeNotAllowed(other.to_string())), + }; + + if !self.http.allowed_schemes.is_empty() && !self.http.allowed_schemes.contains(&scheme) { + return Err(NetworkPolicyError::SchemeNotAllowed( + parsed.scheme().to_string(), + )); + } + + let host = parsed.host().ok_or(NetworkPolicyError::MissingHost)?; + let host_string = normalize_host_string(&host); + let port = parsed + .port_or_known_default() + .ok_or(NetworkPolicyError::MissingPort)?; + + if !self.http.allowed_ports.is_empty() && !self.http.allowed_ports.contains(&port) { + return Err(NetworkPolicyError::PortNotAllowed(port)); + } + + if !self.is_host_allowed(&host, &host_string) { + return Err(NetworkPolicyError::HostNotAllowed(host_string)); + } + + Ok(()) + } + + /// Validate a DNS lookup against policy. + pub fn is_dns_lookup_allowed( + &self, + hostname: &str, + record_type: DnsRecordType, + ) -> Result<(), NetworkPolicyError> { + if !self.allow_internet { + return Err(NetworkPolicyError::NetworkDisabled); + } + + if !self.dns_policy.enabled { + return Err(NetworkPolicyError::DnsDisabled); + } + + if !self.dns_policy.allowed_record_types.is_empty() + && !self.dns_policy.allowed_record_types.contains(&record_type) + { + return Err(NetworkPolicyError::DnsRecordTypeNotAllowed(record_type)); + } + + let host = url::Host::parse(hostname) + .map_err(|_| NetworkPolicyError::InvalidHost(hostname.to_string()))?; + let host_string = normalize_host_string(&host); + + if !self.dns_policy.allowed_hosts.is_empty() + && !self + .dns_policy + .allowed_hosts + .iter() + .any(|pattern| pattern.matches(&host_string)) + { + return Err(NetworkPolicyError::HostNotAllowed(host_string)); + } + + Ok(()) + } + + fn is_host_allowed>(&self, host: &url::Host, host_string: &str) -> bool { + let host_allowed = if self.http.allowed_hosts.is_empty() { + true + } else { + self.http + .allowed_hosts + .iter() + .any(|pattern| pattern.matches(host_string)) + }; + + match host { + url::Host::Ipv4(ip) => host_allowed || self.is_ip_allowed(IpAddr::V4(*ip)), + url::Host::Ipv6(ip) => host_allowed || self.is_ip_allowed(IpAddr::V6(*ip)), + url::Host::Domain(_) => host_allowed, + } + } + + fn is_ip_allowed(&self, ip: IpAddr) -> bool { + if self.allowed_ip_ranges.is_empty() { + return false; + } + + self.allowed_ip_ranges.iter().any(|net| net.contains(&ip)) + } +} + +/// Normalized HTTP policy for runtime enforcement. +#[derive(Debug, Clone)] +pub struct ValidatedHttpPolicy { + pub allowed_hosts: Vec, + pub allowed_schemes: Vec, + pub allowed_ports: Vec, +} + +/// Normalized DNS policy for runtime enforcement. +#[derive(Debug, Clone)] +pub struct ValidatedDnsPolicy { + pub enabled: bool, + pub allowed_hosts: Vec, + pub allowed_record_types: Vec, + pub max_lookups: u32, + pub cache_ttl_secs: u64, + pub block_private_ranges: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NormalizedHostPattern { + pattern: String, + match_subdomains: bool, +} + +impl NormalizedHostPattern { + fn matches(&self, host: &str) -> bool { + let host = host.trim_end_matches('.').to_lowercase(); + if self.match_subdomains { + host == self.pattern || host.ends_with(&format!(".{}", self.pattern)) + } else { + host == self.pattern + } + } +} + +/// Errors emitted when validating network policies. +#[derive(Debug, thiserror::Error)] +pub enum NetworkPolicyError { + #[error("network access disabled")] + NetworkDisabled, + #[error("dns access disabled")] + DnsDisabled, + #[error("invalid host pattern: {0}")] + InvalidHost(String), + #[error("invalid ip range: {0}")] + InvalidIpRange(String), + #[error("invalid url: {0}")] + InvalidUrl(String), + #[error("missing host in url")] + MissingHost, + #[error("missing port in url")] + MissingPort, + #[error("scheme not allowed: {0}")] + SchemeNotAllowed(String), + #[error("host not allowed: {0}")] + HostNotAllowed(String), + #[error("port not allowed: {0}")] + PortNotAllowed(u16), + #[error("dns record type not allowed: {0:?}")] + DnsRecordTypeNotAllowed(DnsRecordType), +} + +fn normalize_hosts( + allowed_hosts: &[String], +) -> Result, NetworkPolicyError> { + allowed_hosts + .iter() + .map(|host| normalize_host_pattern(host)) + .collect() +} + +fn normalize_host_pattern(host: &str) -> Result { + let trimmed = host.trim(); + if trimmed.is_empty() { + return Err(NetworkPolicyError::InvalidHost(host.to_string())); + } + + let (pattern, match_subdomains) = if let Some(stripped) = trimmed.strip_prefix("*.") { + (stripped, true) + } else if let Some(stripped) = trimmed.strip_prefix('.') { + (stripped, true) + } else { + (trimmed, false) + }; + + let normalized = pattern.trim_end_matches('.').to_lowercase(); + if normalized.is_empty() { + return Err(NetworkPolicyError::InvalidHost(host.to_string())); + } + + url::Host::parse(&normalized).map_err(|_| NetworkPolicyError::InvalidHost(host.to_string()))?; + + Ok(NormalizedHostPattern { + pattern: normalized, + match_subdomains, + }) +} + +fn normalize_ports(allowed_ports: &[u16]) -> Result, NetworkPolicyError> { + if allowed_ports.contains(&0) { + return Err(NetworkPolicyError::PortNotAllowed(0)); + } + + Ok(allowed_ports.to_vec()) +} + +fn parse_ip_ranges(ranges: &[String]) -> Result, NetworkPolicyError> { + ranges + .iter() + .map(|range| { + ipnet::IpNet::from_str(range) + .map_err(|_| NetworkPolicyError::InvalidIpRange(range.to_string())) + }) + .collect() +} + +fn normalize_host_string>(host: &url::Host) -> String { + match host { + url::Host::Domain(domain) => domain.as_ref().trim_end_matches('.').to_lowercase(), + url::Host::Ipv4(ip) => ip.to_string(), + url::Host::Ipv6(ip) => ip.to_string(), + } +} + +/// HTTP request description for WASM host calls. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HttpRequest { + pub method: HttpMethod, + pub url: String, + pub headers: HashMap, + pub body: Vec, +} + +/// HTTP GET request payload for host calls. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HttpGetRequest { + pub url: String, + pub headers: HashMap, +} + +/// HTTP POST request payload for host calls. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HttpPostRequest { + pub url: String, + pub headers: HashMap, + pub body: Vec, +} + +/// HTTP response returned to WASM. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct HttpResponse { + pub status: u16, + pub headers: HashMap, + pub body: Vec, +} + +/// Supported HTTP methods. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum HttpMethod { + Get, + Post, + Put, + Patch, + Delete, + Head, + Options, +} + +/// DNS resolution request. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DnsRequest { + pub hostname: String, + pub record_type: DnsRecordType, +} + +/// DNS resolution response. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DnsResponse { + pub records: Vec, +} + +/// Audit log entry for network operations. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct NetworkAuditEntry { + pub timestamp: chrono::DateTime, + pub challenge_id: String, + pub validator_id: String, + pub action: NetworkAuditAction, + pub metadata: HashMap, +} + +/// Specific network audit action. +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum NetworkAuditAction { + HttpRequest { url: String, method: HttpMethod }, + HttpResponse { status: u16, bytes: u64 }, + DnsLookup { hostname: String }, + PolicyDenied { reason: String }, +} + +/// Errors emitted by host networking operations. +#[derive(Debug, thiserror::Error, Serialize, Deserialize)] +pub enum NetworkError { + #[error("network access disabled")] + NetworkDisabled, + #[error("policy violation: {0}")] + PolicyViolation(String), + #[error("request limit exceeded: {0}")] + LimitExceeded(String), + #[error("dns resolution failed: {0}")] + DnsFailure(String), + #[error("http request failed: {0}")] + HttpFailure(String), + #[error("request timeout")] + Timeout, +} + +/// Hook for emitting audit events from the runtime. +pub trait NetworkAuditLogger: Send + Sync { + fn record(&self, entry: NetworkAuditEntry); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_http_policy_allows_https() { + let policy = NetworkPolicy::strict(vec!["example.com".to_string()]); + let validated = policy.validate().expect("policy should validate"); + + assert!(validated + .is_http_request_allowed("https://example.com/path") + .is_ok()); + assert!(validated + .is_http_request_allowed("http://example.com") + .is_err()); + } + + #[test] + fn test_http_policy_wildcard_hosts() { + let policy = NetworkPolicy::strict(vec!["*.example.com".to_string()]); + let validated = policy.validate().expect("policy should validate"); + + assert!(validated + .is_http_request_allowed("https://api.example.com") + .is_ok()); + assert!(validated + .is_http_request_allowed("https://example.com") + .is_ok()); + assert!(validated + .is_http_request_allowed("https://evil.com") + .is_err()); + } + + #[test] + fn test_http_policy_ports() { + let mut policy = NetworkPolicy::strict(vec!["example.com".to_string()]); + policy.http.allowed_ports = vec![443]; + let validated = policy.validate().expect("policy should validate"); + + assert!(validated + .is_http_request_allowed("https://example.com:443") + .is_ok()); + assert!(validated + .is_http_request_allowed("https://example.com:8443") + .is_err()); + } + + #[test] + fn test_dns_policy_allows_record() { + let mut policy = NetworkPolicy::strict(vec!["example.com".to_string()]); + policy.dns_policy.enabled = true; + policy.dns_policy.allowed_hosts = vec!["example.com".to_string()]; + let validated = policy.validate().expect("policy should validate"); + + assert!(validated + .is_dns_lookup_allowed("example.com", DnsRecordType::A) + .is_ok()); + assert!(validated + .is_dns_lookup_allowed("evil.com", DnsRecordType::A) + .is_err()); + } + + #[test] + fn test_invalid_host_rejected() { + let policy = NetworkPolicy::strict(vec!["bad host".to_string()]); + assert!(policy.validate().is_err()); + } +} diff --git a/crates/wasm-runtime-interface/src/llm.rs b/crates/wasm-runtime-interface/src/llm.rs new file mode 100644 index 000000000..074bacda3 --- /dev/null +++ b/crates/wasm-runtime-interface/src/llm.rs @@ -0,0 +1,480 @@ +//! LLM Host Functions for WASM Challenges +//! +//! Provides host functions that allow WASM code to perform LLM inference +//! via the Chutes API (llm.chutes.ai). Gated by `LlmPolicy`. +//! +//! # Host Functions +//! +//! - `llm_chat_completion(req_ptr, req_len, resp_ptr, resp_len) -> i32` โ€” Send chat completion request +//! - `llm_is_available() -> i32` โ€” Check if LLM inference is available (has API key) + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use bincode::Options; +use serde::{Deserialize, Serialize}; +use std::fmt; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +const MAX_CHAT_REQUEST_SIZE: u64 = 4 * 1024 * 1024; +const LLM_REQUEST_TIMEOUT_SECS: u64 = 60; + +pub const HOST_LLM_NAMESPACE: &str = "platform_llm"; +pub const HOST_LLM_CHAT_COMPLETION: &str = "llm_chat_completion"; +pub const HOST_LLM_IS_AVAILABLE: &str = "llm_is_available"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum LlmHostStatus { + Success = 0, + Disabled = -1, + InvalidRequest = -2, + ApiError = -3, + BufferTooSmall = -4, + RateLimited = -5, + InternalError = -100, +} + +impl LlmHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } +} + +#[derive(Clone, Serialize, Deserialize)] +pub struct LlmPolicy { + pub enabled: bool, + #[serde(skip)] + pub api_key: Option, + pub endpoint: String, + pub max_requests: u32, + pub allowed_models: Vec, +} + +impl fmt::Debug for LlmPolicy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("LlmPolicy") + .field("enabled", &self.enabled) + .field("api_key", &self.api_key.as_ref().map(|_| "[REDACTED]")) + .field("endpoint", &self.endpoint) + .field("max_requests", &self.max_requests) + .field("allowed_models", &self.allowed_models) + .finish() + } +} + +impl Default for LlmPolicy { + fn default() -> Self { + Self { + enabled: false, + api_key: None, + endpoint: "https://llm.chutes.ai/v1/chat/completions".to_string(), + max_requests: 10, + allowed_models: Vec::new(), + } + } +} + +impl LlmPolicy { + pub fn with_api_key(api_key: String) -> Self { + Self { + enabled: true, + api_key: Some(api_key), + ..Default::default() + } + } + + pub fn is_available(&self) -> bool { + self.enabled && self.api_key.is_some() + } +} + +pub struct LlmState { + pub policy: LlmPolicy, + pub requests_made: u32, +} + +impl LlmState { + pub fn new(policy: LlmPolicy) -> Self { + Self { + policy, + requests_made: 0, + } + } +} + +#[derive(Clone, Debug)] +pub struct LlmHostFunctions; + +impl LlmHostFunctions { + pub fn new() -> Self { + Self + } +} + +impl Default for LlmHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for LlmHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_LLM_NAMESPACE, + HOST_LLM_CHAT_COMPLETION, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_chat_completion(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_LLM_NAMESPACE, + HOST_LLM_IS_AVAILABLE, + |caller: Caller| -> i32 { handle_is_available(&caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +fn handle_is_available(caller: &Caller) -> i32 { + let state = &caller.data().llm_state; + if state.policy.is_available() { + 1 + } else { + 0 + } +} + +fn handle_chat_completion( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let policy_available; + let requests_made; + let max_requests; + { + let state = &caller.data().llm_state; + policy_available = state.policy.is_available(); + requests_made = state.requests_made; + max_requests = state.policy.max_requests; + } + + if !policy_available { + return LlmHostStatus::Disabled.to_i32(); + } + + if requests_made >= max_requests { + return LlmHostStatus::RateLimited.to_i32(); + } + + if req_ptr < 0 || req_len < 0 || resp_ptr < 0 || resp_len < 0 { + return LlmHostStatus::InvalidRequest.to_i32(); + } + + let request_bytes = match read_wasm_memory(caller, req_ptr, req_len as usize) { + Ok(b) => b, + Err(err) => { + warn!(error = %err, "llm_chat_completion: failed to read request from wasm memory"); + return LlmHostStatus::InternalError.to_i32(); + } + }; + + let api_key; + let endpoint; + { + let state = &caller.data().llm_state; + api_key = match &state.policy.api_key { + Some(k) => k.clone(), + None => return LlmHostStatus::Disabled.to_i32(), + }; + endpoint = state.policy.endpoint.clone(); + } + + #[derive(Deserialize)] + struct ChatRequest { + model: String, + messages: Vec, + max_tokens: u32, + temperature: f32, + } + + #[derive(Deserialize)] + struct ChatMessage { + role: String, + content: String, + } + + let chat_req: ChatRequest = match bincode::DefaultOptions::new() + .with_limit(MAX_CHAT_REQUEST_SIZE) + .with_fixint_encoding() + .allow_trailing_bytes() + .deserialize(&request_bytes) + { + Ok(r) => r, + Err(_) => return LlmHostStatus::InvalidRequest.to_i32(), + }; + + { + let state = &caller.data().llm_state; + let allowed = &state.policy.allowed_models; + if !allowed.is_empty() && !allowed.contains(&chat_req.model) { + warn!( + model = %chat_req.model, + "llm_chat_completion: model not in allowed list" + ); + return LlmHostStatus::InvalidRequest.to_i32(); + } + } + + #[derive(Serialize)] + struct OpenAiRequest { + model: String, + messages: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + max_tokens: Option, + #[serde(skip_serializing_if = "Option::is_none")] + temperature: Option, + } + + #[derive(Serialize)] + struct OpenAiMessage { + role: String, + content: String, + } + + let openai_req = OpenAiRequest { + model: chat_req.model, + messages: chat_req + .messages + .into_iter() + .map(|m| OpenAiMessage { + role: m.role, + content: m.content, + }) + .collect(), + max_tokens: Some(chat_req.max_tokens), + temperature: Some(chat_req.temperature), + }; + + let json_body = match serde_json::to_vec(&openai_req) { + Ok(b) => b, + Err(_) => return LlmHostStatus::InvalidRequest.to_i32(), + }; + + let client = reqwest::blocking::Client::new(); + let http_response = match client + .post(&endpoint) + .header("Content-Type", "application/json") + .header("Authorization", format!("Bearer {}", api_key)) + .body(json_body) + .timeout(std::time::Duration::from_secs(LLM_REQUEST_TIMEOUT_SECS)) + .send() + { + Ok(r) => r, + Err(err) => { + warn!(error = %err, "llm_chat_completion: HTTP request failed"); + return LlmHostStatus::ApiError.to_i32(); + } + }; + + let response_body = match http_response.bytes() { + Ok(b) => b.to_vec(), + Err(err) => { + warn!(error = %err, "llm_chat_completion: failed to read response body"); + return LlmHostStatus::ApiError.to_i32(); + } + }; + + #[derive(Deserialize)] + struct OpenAiResponse { + choices: Option>, + usage: Option, + } + + #[derive(Deserialize)] + struct OpenAiChoice { + message: Option, + } + + #[derive(Deserialize)] + struct OpenAiRespMessage { + content: Option, + } + + #[derive(Deserialize)] + struct OpenAiUsage { + prompt_tokens: Option, + completion_tokens: Option, + total_tokens: Option, + } + + let openai_resp: OpenAiResponse = match serde_json::from_slice(&response_body) { + Ok(r) => r, + Err(err) => { + warn!(error = %err, "llm_chat_completion: failed to parse OpenAI response"); + return LlmHostStatus::ApiError.to_i32(); + } + }; + + let content = openai_resp + .choices + .and_then(|mut c| c.pop()) + .and_then(|c| c.message) + .and_then(|m| m.content) + .unwrap_or_default(); + + #[derive(Serialize)] + struct LlmResponsePayload { + content: String, + usage: Option, + } + + #[derive(Serialize)] + struct LlmUsagePayload { + prompt_tokens: u32, + completion_tokens: u32, + total_tokens: u32, + } + + let usage = openai_resp.usage.map(|u| LlmUsagePayload { + prompt_tokens: u.prompt_tokens.unwrap_or(0), + completion_tokens: u.completion_tokens.unwrap_or(0), + total_tokens: u.total_tokens.unwrap_or(0), + }); + + let response_payload = LlmResponsePayload { content, usage }; + + let response_bytes = match bincode::serialize(&response_payload) { + Ok(b) => b, + Err(_) => return LlmHostStatus::InternalError.to_i32(), + }; + + if response_bytes.len() > resp_len as usize { + return LlmHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, resp_ptr, &response_bytes) { + warn!(error = %err, "llm_chat_completion: failed to write response to wasm memory"); + return LlmHostStatus::InternalError.to_i32(); + } + + caller.data_mut().llm_state.requests_made += 1; + + response_bytes.len() as i32 +} + +fn read_wasm_memory( + caller: &mut Caller, + ptr: i32, + len: usize, +) -> Result, String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data(caller); + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_wasm_memory( + caller: &mut Caller, + ptr: i32, + bytes: &[u8], +) -> Result<(), String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(bytes.len()) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data_mut(caller); + if end > data.len() { + return Err("memory write out of bounds".to_string()); + } + data[ptr..end].copy_from_slice(bytes); + Ok(()) +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_llm_host_status_values() { + assert_eq!(LlmHostStatus::Success.to_i32(), 0); + assert_eq!(LlmHostStatus::Disabled.to_i32(), -1); + assert_eq!(LlmHostStatus::InvalidRequest.to_i32(), -2); + assert_eq!(LlmHostStatus::ApiError.to_i32(), -3); + assert_eq!(LlmHostStatus::BufferTooSmall.to_i32(), -4); + assert_eq!(LlmHostStatus::RateLimited.to_i32(), -5); + assert_eq!(LlmHostStatus::InternalError.to_i32(), -100); + } + + #[test] + fn test_llm_policy_default() { + let policy = LlmPolicy::default(); + assert!(!policy.enabled); + assert!(policy.api_key.is_none()); + assert!(!policy.is_available()); + } + + #[test] + fn test_llm_policy_with_api_key() { + let policy = LlmPolicy::with_api_key("test-key".to_string()); + assert!(policy.enabled); + assert!(policy.is_available()); + assert_eq!(policy.api_key, Some("test-key".to_string())); + } + + #[test] + fn test_llm_state_creation() { + let state = LlmState::new(LlmPolicy::default()); + assert_eq!(state.requests_made, 0); + assert!(!state.policy.is_available()); + } + + #[test] + fn test_llm_policy_debug_redacts_api_key() { + let policy = LlmPolicy::with_api_key("super-secret-key-12345".to_string()); + let debug_output = format!("{:?}", policy); + assert!(!debug_output.contains("super-secret-key-12345")); + assert!(debug_output.contains("[REDACTED]")); + } + + #[test] + fn test_llm_policy_serialize_skips_api_key() { + let policy = LlmPolicy::with_api_key("secret-key".to_string()); + let serialized = bincode::serialize(&policy).unwrap(); + let deserialized: LlmPolicy = bincode::deserialize(&serialized).unwrap(); + assert!(deserialized.api_key.is_none()); + } +} diff --git a/crates/wasm-runtime-interface/src/network.rs b/crates/wasm-runtime-interface/src/network.rs new file mode 100644 index 000000000..02d4b7b4a --- /dev/null +++ b/crates/wasm-runtime-interface/src/network.rs @@ -0,0 +1,1369 @@ +use crate::{ + DnsRecordType, DnsRequest, DnsResponse, HostFunction, HttpGetRequest, HttpMethod, + HttpPostRequest, HttpRequest, HttpResponse, NetworkAuditAction, NetworkAuditEntry, + NetworkAuditLogger, NetworkError, NetworkPolicy, NetworkPolicyError, ValidatedNetworkPolicy, + HOST_DNS_RESOLVE, HOST_FUNCTION_NAMESPACE, HOST_HTTP_GET, HOST_HTTP_POST, HOST_HTTP_REQUEST, +}; +use reqwest::blocking::Client; +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; +use reqwest::redirect::Policy; +use std::collections::HashMap; +use std::hash::{Hash, Hasher}; +use std::io::Read; +use std::net::IpAddr; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tracing::{error, info, warn}; +use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; +use trust_dns_resolver::proto::rr::RecordType; +use trust_dns_resolver::Resolver; +use wasmtime::{Caller, Linker, Memory}; + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; + +pub const HOST_LOG_MESSAGE: &str = "log_message"; +pub const HOST_GET_TIMESTAMP: &str = "get_timestamp"; + +const DEFAULT_DNS_BUF_SIZE: i32 = 4096; + +#[derive(Debug, thiserror::Error)] +pub enum NetworkStateError { + #[error("network policy invalid: {0}")] + InvalidPolicy(#[from] NetworkPolicyError), + #[error("failed to initialize http client: {0}")] + HttpClient(String), + #[error("failed to initialize dns resolver: {0}")] + DnsResolver(String), +} + +#[derive(Clone, Debug)] +pub struct NetworkHostFunctions { + enabled: Vec, +} + +impl NetworkHostFunctions { + pub fn new(enabled: Vec) -> Self { + Self { enabled } + } + + pub fn all() -> Self { + Self { + enabled: vec![ + HostFunction::HttpRequest, + HostFunction::HttpGet, + HostFunction::HttpPost, + HostFunction::DnsResolve, + ], + } + } +} + +impl Default for NetworkHostFunctions { + fn default() -> Self { + Self::all() + } +} + +impl HostFunctionRegistrar for NetworkHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + if self.enabled.contains(&HostFunction::HttpRequest) { + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_HTTP_REQUEST, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_http_request(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + if self.enabled.contains(&HostFunction::HttpGet) { + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_HTTP_GET, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_http_get(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + if self.enabled.contains(&HostFunction::HttpPost) { + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_HTTP_POST, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, + _extra: i32| + -> i32 { + handle_http_post(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + if self.enabled.contains(&HostFunction::DnsResolve) { + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_DNS_RESOLVE, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32| + -> i32 { + handle_dns_request(&mut caller, req_ptr, req_len, resp_ptr) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_LOG_MESSAGE, + |mut caller: Caller, level: i32, msg_ptr: i32, msg_len: i32| { + handle_log_message(&mut caller, level, msg_ptr, msg_len); + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_FUNCTION_NAMESPACE, + HOST_GET_TIMESTAMP, + |caller: Caller| -> i64 { handle_get_timestamp(&caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +pub struct NetworkState { + policy: ValidatedNetworkPolicy, + audit_logger: Option>, + http_client: Client, + dns_resolver: Resolver, + dns_cache: HashMap, + requests_made: u32, + dns_lookups: u32, + request_timestamps: Vec, + challenge_id: String, + validator_id: String, +} + +impl NetworkState { + pub fn new( + policy: NetworkPolicy, + audit_logger: Option>, + challenge_id: String, + validator_id: String, + ) -> Result { + let validated = policy.validate()?; + + let redirect_policy = if validated.limits.max_redirects == 0 { + Policy::none() + } else { + Policy::limited(validated.limits.max_redirects as usize) + }; + + let http_client = Client::builder() + .timeout(Duration::from_millis(validated.limits.timeout_ms)) + .redirect(redirect_policy) + .build() + .map_err(|err| NetworkStateError::HttpClient(err.to_string()))?; + + let mut resolver_opts = ResolverOpts::default(); + resolver_opts.timeout = Duration::from_millis(validated.limits.timeout_ms); + resolver_opts.attempts = 1; + resolver_opts.cache_size = 0; + resolver_opts.use_hosts_file = false; + resolver_opts.num_concurrent_reqs = 1; + + if validated.dns_policy.cache_ttl_secs > 0 { + let ttl = Duration::from_secs(validated.dns_policy.cache_ttl_secs); + resolver_opts.positive_min_ttl = Some(ttl); + resolver_opts.positive_max_ttl = Some(ttl); + resolver_opts.negative_min_ttl = Some(ttl); + resolver_opts.negative_max_ttl = Some(ttl); + } + + let dns_resolver = Resolver::new(ResolverConfig::default(), resolver_opts) + .map_err(|err| NetworkStateError::DnsResolver(err.to_string()))?; + + Ok(Self { + policy: validated, + audit_logger, + http_client, + dns_resolver, + dns_cache: HashMap::new(), + requests_made: 0, + dns_lookups: 0, + request_timestamps: Vec::new(), + challenge_id, + validator_id, + }) + } + + pub fn requests_made(&self) -> u32 { + self.requests_made + } + + pub fn dns_lookups(&self) -> u32 { + self.dns_lookups + } + + pub fn reset_counters(&mut self) { + self.requests_made = 0; + self.dns_lookups = 0; + self.request_timestamps.clear(); + self.dns_cache.clear(); + } + + pub fn handle_http_request( + &mut self, + request: HttpRequest, + ) -> Result { + if !self.policy.allow_internet { + self.audit_denial("http_request denied: network disabled"); + return Err(NetworkError::NetworkDisabled); + } + + self.ensure_request_budget()?; + + if let Err(e) = self.validate_http_request(&request) { + self.audit_denial(&format!( + "http_request policy denied url={} reason={}", + request.url, e + )); + return Err(e); + } + + self.requests_made = self.requests_made.saturating_add(1); + self.request_timestamps.push(Instant::now()); + + self.audit(NetworkAuditAction::HttpRequest { + url: request.url.clone(), + method: request.method, + }); + + let _resolved_ip = self.resolve_and_validate_ip(&request.url)?; + + let method = to_reqwest_method(request.method); + let mut builder = self.http_client.request(method, &request.url); + let headers = to_header_map(&request.headers)?; + builder = builder.headers(headers); + + if !request.body.is_empty() { + builder = builder.body(request.body.clone()); + } + + let response = builder.send().map_err(map_reqwest_error)?; + let status = response.status().as_u16(); + let headers = collect_headers(response.headers())?; + + let body = read_response_body(response, self.policy.limits.max_response_bytes)?; + + self.ensure_header_limits(&headers)?; + + self.audit(NetworkAuditAction::HttpResponse { + status, + bytes: body.len() as u64, + }); + + info!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + url = %request.url, + status = status, + response_bytes = body.len(), + "http request completed" + ); + + Ok(HttpResponse { + status, + headers, + body, + }) + } + + pub fn handle_dns_request(&mut self, request: DnsRequest) -> Result { + if !self.policy.allow_internet { + self.audit_denial("dns_lookup denied: network disabled"); + return Err(NetworkError::NetworkDisabled); + } + + self.ensure_dns_budget()?; + + if let Err(e) = self + .policy + .is_dns_lookup_allowed(&request.hostname, request.record_type) + { + self.audit_denial(&format!( + "dns_lookup policy denied hostname={} type={:?} reason={}", + request.hostname, request.record_type, e + )); + return Err(map_policy_error(e)); + } + + self.dns_lookups = self.dns_lookups.saturating_add(1); + + let cache_key = DnsCacheKey::new(&request.hostname, request.record_type); + if let Some(entry) = self.dns_cache.get(&cache_key) { + if entry.expires_at > Instant::now() { + return Ok(DnsResponse { + records: entry.records.clone(), + }); + } + } + + self.audit(NetworkAuditAction::DnsLookup { + hostname: request.hostname.clone(), + }); + + let records = resolve_dns(&self.dns_resolver, &request, &self.policy)?; + if records.is_empty() { + return Err(NetworkError::DnsFailure("no records returned".to_string())); + } + + if self.policy.dns_policy.cache_ttl_secs > 0 { + let expires_at = + Instant::now() + Duration::from_secs(self.policy.dns_policy.cache_ttl_secs); + self.dns_cache.insert( + cache_key, + DnsCacheEntry { + records: records.clone(), + expires_at, + }, + ); + } + + info!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + hostname = %request.hostname, + record_count = records.len(), + "dns lookup completed" + ); + + Ok(DnsResponse { records }) + } + + fn ensure_request_budget(&self) -> Result<(), NetworkError> { + if self.policy.limits.max_requests == 0 { + return Err(NetworkError::LimitExceeded( + "http requests disabled".to_string(), + )); + } + + if self.requests_made >= self.policy.limits.max_requests { + self.audit_denial(&format!( + "http request limit exceeded: {}/{}", + self.requests_made, self.policy.limits.max_requests + )); + return Err(NetworkError::LimitExceeded( + "http request limit exceeded".to_string(), + )); + } + + Ok(()) + } + + fn ensure_dns_budget(&self) -> Result<(), NetworkError> { + if self.policy.dns_policy.max_lookups == 0 { + return Err(NetworkError::LimitExceeded( + "dns lookups disabled".to_string(), + )); + } + + if self.dns_lookups >= self.policy.dns_policy.max_lookups { + self.audit_denial(&format!( + "dns lookup limit exceeded: {}/{}", + self.dns_lookups, self.policy.dns_policy.max_lookups + )); + return Err(NetworkError::LimitExceeded( + "dns lookup limit exceeded".to_string(), + )); + } + + Ok(()) + } + + fn validate_http_request(&self, request: &HttpRequest) -> Result<(), NetworkError> { + if request.body.len() as u64 > self.policy.limits.max_request_bytes { + return Err(NetworkError::LimitExceeded(format!( + "request body too large: {} > {}", + request.body.len(), + self.policy.limits.max_request_bytes + ))); + } + + self.ensure_header_limits(&request.headers)?; + + self.policy + .is_http_request_allowed(&request.url) + .map_err(map_policy_error) + } + + fn resolve_and_validate_ip(&self, url: &str) -> Result, NetworkError> { + let parsed = url::Url::parse(url) + .map_err(|err| NetworkError::PolicyViolation(format!("invalid url: {err}")))?; + + let host_str = match parsed.host_str() { + Some(h) => h, + None => return Ok(None), + }; + + if let Ok(ip) = host_str.parse::() { + self.validate_ip_against_policy(ip)?; + return Ok(Some(ip)); + } + + let lookup = self.dns_resolver.lookup_ip(host_str).map_err(|err| { + NetworkError::DnsFailure(format!("pre-connect resolve failed: {err}")) + })?; + + for ip in lookup.iter() { + self.validate_ip_against_policy(ip)?; + } + + Ok(lookup.iter().next()) + } + + fn validate_ip_against_policy(&self, ip: IpAddr) -> Result<(), NetworkError> { + if self.policy.dns_policy.block_private_ranges && is_private_ip(ip) { + self.audit_denial(&format!("blocked private/reserved IP: {ip}")); + return Err(NetworkError::PolicyViolation(format!( + "connection to private/reserved IP blocked: {ip}" + ))); + } + + if !self.policy.allowed_ip_ranges.is_empty() + && !self + .policy + .allowed_ip_ranges + .iter() + .any(|net| net.contains(&ip)) + { + self.audit_denial(&format!("IP not in allowed ranges: {ip}")); + return Err(NetworkError::PolicyViolation(format!( + "IP not in allowed ranges: {ip}" + ))); + } + + Ok(()) + } + + fn ensure_header_limits(&self, headers: &HashMap) -> Result<(), NetworkError> { + let header_bytes = header_size(headers); + if header_bytes > self.policy.limits.max_header_bytes { + return Err(NetworkError::LimitExceeded(format!( + "header size exceeds limit: {} > {}", + header_bytes, self.policy.limits.max_header_bytes + ))); + } + + Ok(()) + } + + fn audit(&self, action: NetworkAuditAction) { + if !self.policy.audit.enabled { + return; + } + + if let Some(logger) = &self.audit_logger { + let entry = NetworkAuditEntry { + timestamp: chrono::Utc::now(), + challenge_id: self.challenge_id.clone(), + validator_id: self.validator_id.clone(), + action, + metadata: self.policy.audit.tags.clone(), + }; + logger.record(entry); + } + } + + fn audit_denial(&self, reason: &str) { + self.audit(NetworkAuditAction::PolicyDenied { + reason: reason.to_string(), + }); + + warn!( + challenge_id = %self.challenge_id, + validator_id = %self.validator_id, + reason = %reason, + "network policy denied" + ); + } +} + +#[derive(Clone, Debug)] +struct DnsCacheEntry { + records: Vec, + expires_at: Instant, +} + +#[derive(Clone, Debug, Eq)] +struct DnsCacheKey { + hostname: String, + record_type: DnsRecordType, +} + +impl DnsCacheKey { + fn new(hostname: &str, record_type: DnsRecordType) -> Self { + Self { + hostname: hostname.to_lowercase(), + record_type, + } + } +} + +impl PartialEq for DnsCacheKey { + fn eq(&self, other: &Self) -> bool { + self.hostname == other.hostname && self.record_type == other.record_type + } +} + +impl Hash for DnsCacheKey { + fn hash(&self, state: &mut H) { + self.hostname.hash(state); + self.record_type.hash(state); + } +} + +fn handle_http_request( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let enforcement = "http_request"; + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host memory read failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request decode failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(format!( + "invalid http request payload: {err}" + ))), + ); + } + }; + + let result = caller.data_mut().network_state.handle_http_request(request); + if let Err(ref err) = result { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request denied"); + } + write_result(caller, resp_ptr, resp_len, result) +} + +fn handle_http_get( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let enforcement = "http_get"; + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host memory read failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request decode failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(format!( + "invalid http get payload: {err}" + ))), + ); + } + }; + + let request = HttpRequest { + method: HttpMethod::Get, + url: request.url, + headers: request.headers, + body: Vec::new(), + }; + + let result = caller.data_mut().network_state.handle_http_request(request); + if let Err(ref err) = result { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request denied"); + } + write_result(caller, resp_ptr, resp_len, result) +} + +fn handle_http_post( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let enforcement = "http_post"; + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host memory read failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request decode failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::HttpFailure(format!( + "invalid http post payload: {err}" + ))), + ); + } + }; + + let request = HttpRequest { + method: HttpMethod::Post, + url: request.url, + headers: request.headers, + body: request.body, + }; + + let result = caller.data_mut().network_state.handle_http_request(request); + if let Err(ref err) = result { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request denied"); + } + write_result(caller, resp_ptr, resp_len, result) +} + +fn handle_dns_request( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, +) -> i32 { + let resp_len = DEFAULT_DNS_BUF_SIZE; + let enforcement = "dns_resolve"; + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host memory read failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::DnsFailure(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request decode failed"); + return write_result::( + caller, + resp_ptr, + resp_len, + Err(NetworkError::DnsFailure(format!( + "invalid dns request payload: {err}" + ))), + ); + } + }; + + let result = caller.data_mut().network_state.handle_dns_request(request); + if let Err(ref err) = result { + warn!(challenge_id = %caller.data().challenge_id, validator_id = %caller.data().validator_id, function = enforcement, error = %err, "host request denied"); + } + write_result(caller, resp_ptr, resp_len, result) +} + +fn handle_log_message(caller: &mut Caller, level: i32, msg_ptr: i32, msg_len: i32) { + let msg = match read_memory(caller, msg_ptr, msg_len) { + Ok(bytes) => String::from_utf8_lossy(&bytes).into_owned(), + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + error = %err, + "log_message: failed to read message from wasm memory" + ); + return; + } + }; + + let challenge_id = caller.data().challenge_id.clone(); + match level { + 0 => info!(challenge_id = %challenge_id, "[wasm] {}", msg), + 1 => warn!(challenge_id = %challenge_id, "[wasm] {}", msg), + _ => error!(challenge_id = %challenge_id, "[wasm] {}", msg), + } +} + +fn handle_get_timestamp(caller: &Caller) -> i64 { + if let Some(ts) = caller.data().fixed_timestamp_ms { + return ts; + } + chrono::Utc::now().timestamp_millis() +} + +fn resolve_dns( + resolver: &Resolver, + request: &DnsRequest, + policy: &ValidatedNetworkPolicy, +) -> Result, NetworkError> { + match request.record_type { + DnsRecordType::A | DnsRecordType::Aaaa => { + let lookup = resolver + .lookup_ip(request.hostname.as_str()) + .map_err(|err| NetworkError::DnsFailure(err.to_string()))?; + let records = lookup + .iter() + .filter(|ip| match request.record_type { + DnsRecordType::A => ip.is_ipv4(), + DnsRecordType::Aaaa => ip.is_ipv6(), + _ => false, + }) + .filter(|ip| { + if policy.dns_policy.block_private_ranges { + !is_private_ip(*ip) + } else { + true + } + }) + .filter(|ip| { + if policy.allowed_ip_ranges.is_empty() { + true + } else { + policy.allowed_ip_ranges.iter().any(|net| net.contains(ip)) + } + }) + .map(|ip| ip.to_string()) + .collect::>(); + Ok(records) + } + DnsRecordType::Cname => resolve_generic(resolver, request, RecordType::CNAME), + DnsRecordType::Txt => resolve_generic(resolver, request, RecordType::TXT), + } +} + +fn resolve_generic( + resolver: &Resolver, + request: &DnsRequest, + record_type: RecordType, +) -> Result, NetworkError> { + let lookup = resolver + .lookup(request.hostname.as_str(), record_type) + .map_err(|err| NetworkError::DnsFailure(err.to_string()))?; + + Ok(lookup.iter().map(|record| record.to_string()).collect()) +} + +fn read_response_body( + mut response: reqwest::blocking::Response, + max_response_bytes: u64, +) -> Result, NetworkError> { + let mut body = Vec::new(); + let mut buffer = [0u8; 8192]; + let mut total: u64 = 0; + let max_allowed = max_response_bytes; + + loop { + let bytes_read = response + .read(&mut buffer) + .map_err(|err| NetworkError::HttpFailure(err.to_string()))?; + if bytes_read == 0 { + break; + } + total = total.saturating_add(bytes_read as u64); + if total > max_allowed { + return Err(NetworkError::LimitExceeded(format!( + "response body too large: exceeded {max_allowed} bytes" + ))); + } + body.extend_from_slice(&buffer[..bytes_read]); + } + + Ok(body) +} + +fn to_reqwest_method(method: HttpMethod) -> reqwest::Method { + match method { + HttpMethod::Get => reqwest::Method::GET, + HttpMethod::Post => reqwest::Method::POST, + HttpMethod::Put => reqwest::Method::PUT, + HttpMethod::Patch => reqwest::Method::PATCH, + HttpMethod::Delete => reqwest::Method::DELETE, + HttpMethod::Head => reqwest::Method::HEAD, + HttpMethod::Options => reqwest::Method::OPTIONS, + } +} + +fn to_header_map(headers: &HashMap) -> Result { + let mut header_map = HeaderMap::new(); + for (key, value) in headers { + let name = HeaderName::from_bytes(key.as_bytes()) + .map_err(|err| NetworkError::HttpFailure(err.to_string()))?; + let header_value = HeaderValue::from_str(value) + .map_err(|err| NetworkError::HttpFailure(err.to_string()))?; + header_map.insert(name, header_value); + } + Ok(header_map) +} + +fn collect_headers(headers: &HeaderMap) -> Result, NetworkError> { + let mut result: HashMap = HashMap::new(); + for (name, value) in headers.iter() { + let value = value + .to_str() + .map_err(|err| NetworkError::HttpFailure(err.to_string()))?; + result + .entry(name.as_str().to_string()) + .and_modify(|existing| { + existing.push(','); + existing.push_str(value); + }) + .or_insert_with(|| value.to_string()); + } + Ok(result) +} + +fn header_size(headers: &HashMap) -> u64 { + headers + .iter() + .map(|(key, value)| (key.len() + value.len()) as u64) + .sum() +} + +fn map_policy_error(err: NetworkPolicyError) -> NetworkError { + match err { + NetworkPolicyError::NetworkDisabled => NetworkError::NetworkDisabled, + other => NetworkError::PolicyViolation(other.to_string()), + } +} + +fn map_reqwest_error(err: reqwest::Error) -> NetworkError { + if err.is_timeout() { + NetworkError::Timeout + } else { + NetworkError::HttpFailure(err.to_string()) + } +} + +fn is_private_ip(ip: IpAddr) -> bool { + match ip { + IpAddr::V4(addr) => { + addr.is_private() + || addr.is_loopback() + || addr.is_link_local() + || addr.is_broadcast() + || addr.is_unspecified() + || addr.is_multicast() + || is_cgnat(addr) + || is_documentation_v4(addr) + } + IpAddr::V6(addr) => { + addr.is_loopback() + || addr.is_unspecified() + || addr.is_unique_local() + || addr.is_unicast_link_local() + || addr.is_multicast() + } + } +} + +fn is_cgnat(addr: std::net::Ipv4Addr) -> bool { + let octets = addr.octets(); + octets[0] == 100 && (64..=127).contains(&octets[1]) +} + +fn is_documentation_v4(addr: std::net::Ipv4Addr) -> bool { + let octets = addr.octets(); + (octets[0] == 192 && octets[1] == 0 && octets[2] == 2) + || (octets[0] == 198 && octets[1] == 51 && octets[2] == 100) + || (octets[0] == 203 && octets[1] == 0 && octets[2] == 113) +} + +fn read_memory(caller: &mut Caller, ptr: i32, len: i32) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_result( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + result: Result, +) -> i32 { + let response_bytes = match bincode::serialize(&result) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "failed to serialize response"); + return -1; + } + }; + + write_bytes(caller, resp_ptr, resp_len, &response_bytes) +} + +fn write_bytes( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + bytes: &[u8], +) -> i32 { + if resp_ptr < 0 || resp_len < 0 { + return -1; + } + if bytes.len() > i32::MAX as usize { + return -1; + } + let resp_len = resp_len as usize; + if bytes.len() > resp_len { + return -(bytes.len() as i32); + } + + let memory = match get_memory(caller) { + Some(memory) => memory, + None => return -1, + }; + + let ptr = resp_ptr as usize; + let end = match ptr.checked_add(bytes.len()) { + Some(end) => end, + None => return -1, + }; + let data = memory.data_mut(caller); + if end > data.len() { + return -1; + } + data[ptr..end].copy_from_slice(bytes); + bytes.len() as i32 +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::DnsPolicy; + + fn test_policy_strict(hosts: Vec) -> NetworkPolicy { + NetworkPolicy::strict(hosts) + } + + fn test_policy_with_dns(hosts: Vec, dns_hosts: Vec) -> NetworkPolicy { + let mut policy = NetworkPolicy::strict(hosts); + policy.dns_policy = DnsPolicy { + enabled: true, + allowed_hosts: dns_hosts, + allowed_record_types: vec![DnsRecordType::A, DnsRecordType::Aaaa], + max_lookups: 8, + cache_ttl_secs: 60, + block_private_ranges: true, + }; + policy + } + + #[test] + fn test_network_state_creation() { + let policy = test_policy_strict(vec!["example.com".to_string()]); + let state = NetworkState::new( + policy, + None, + "test-challenge".into(), + "test-validator".into(), + ); + assert!(state.is_ok()); + let state = state.unwrap(); + assert_eq!(state.requests_made(), 0); + assert_eq!(state.dns_lookups(), 0); + } + + #[test] + fn test_request_budget_enforcement() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.limits.max_requests = 2; + let mut state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + assert!(state.ensure_request_budget().is_ok()); + state.requests_made = 2; + let err = state.ensure_request_budget().unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_dns_budget_enforcement() { + let mut policy = test_policy_with_dns( + vec!["example.com".to_string()], + vec!["example.com".to_string()], + ); + policy.dns_policy.max_lookups = 3; + let mut state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + assert!(state.ensure_dns_budget().is_ok()); + state.dns_lookups = 3; + let err = state.ensure_dns_budget().unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_request_budget_zero_disabled() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.limits.max_requests = 0; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let err = state.ensure_request_budget().unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_dns_budget_zero_disabled() { + let mut policy = test_policy_with_dns( + vec!["example.com".to_string()], + vec!["example.com".to_string()], + ); + policy.dns_policy.max_lookups = 0; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let err = state.ensure_dns_budget().unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_validate_http_request_body_too_large() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.limits.max_request_bytes = 10; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let request = HttpRequest { + method: HttpMethod::Get, + url: "https://example.com".to_string(), + headers: HashMap::new(), + body: vec![0u8; 100], + }; + + let err = state.validate_http_request(&request).unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_validate_http_request_headers_too_large() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.limits.max_header_bytes = 10; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let mut headers = HashMap::new(); + headers.insert("x-large".to_string(), "a".repeat(100)); + + let request = HttpRequest { + method: HttpMethod::Get, + url: "https://example.com".to_string(), + headers, + body: Vec::new(), + }; + + let err = state.validate_http_request(&request).unwrap_err(); + assert!(matches!(err, NetworkError::LimitExceeded(_))); + } + + #[test] + fn test_validate_http_request_url_policy() { + let policy = test_policy_strict(vec!["example.com".to_string()]); + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let request = HttpRequest { + method: HttpMethod::Get, + url: "https://evil.com".to_string(), + headers: HashMap::new(), + body: Vec::new(), + }; + + let err = state.validate_http_request(&request).unwrap_err(); + assert!(matches!(err, NetworkError::PolicyViolation(_))); + } + + #[test] + fn test_validate_http_request_allowed() { + let policy = test_policy_strict(vec!["example.com".to_string()]); + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let request = HttpRequest { + method: HttpMethod::Get, + url: "https://example.com/api".to_string(), + headers: HashMap::new(), + body: Vec::new(), + }; + + assert!(state.validate_http_request(&request).is_ok()); + } + + #[test] + fn test_is_private_ip_v4() { + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 127, 0, 0, 1 + )))); + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 10, 0, 0, 1 + )))); + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 192, 168, 1, 1 + )))); + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 172, 16, 0, 1 + )))); + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 169, 254, 1, 1 + )))); + assert!(is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 0, 0, 0, 0 + )))); + assert!(!is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 8, 8, 8, 8 + )))); + assert!(!is_private_ip(IpAddr::V4(std::net::Ipv4Addr::new( + 1, 1, 1, 1 + )))); + } + + #[test] + fn test_is_private_ip_v6() { + assert!(is_private_ip(IpAddr::V6(std::net::Ipv6Addr::LOCALHOST))); + assert!(is_private_ip(IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED))); + assert!(!is_private_ip(IpAddr::V6( + "2001:4860:4860::8888".parse().unwrap() + ))); + } + + #[test] + fn test_is_cgnat() { + assert!(is_cgnat(std::net::Ipv4Addr::new(100, 64, 0, 1))); + assert!(is_cgnat(std::net::Ipv4Addr::new(100, 127, 255, 254))); + assert!(!is_cgnat(std::net::Ipv4Addr::new(100, 128, 0, 1))); + assert!(!is_cgnat(std::net::Ipv4Addr::new(100, 63, 255, 255))); + } + + #[test] + fn test_is_documentation_v4() { + assert!(is_documentation_v4(std::net::Ipv4Addr::new(192, 0, 2, 1))); + assert!(is_documentation_v4(std::net::Ipv4Addr::new( + 198, 51, 100, 1 + ))); + assert!(is_documentation_v4(std::net::Ipv4Addr::new(203, 0, 113, 1))); + assert!(!is_documentation_v4(std::net::Ipv4Addr::new(8, 8, 8, 8))); + } + + #[test] + fn test_reset_counters() { + let policy = test_policy_strict(vec!["example.com".to_string()]); + let mut state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + state.requests_made = 5; + state.dns_lookups = 3; + state.request_timestamps.push(Instant::now()); + + state.reset_counters(); + + assert_eq!(state.requests_made(), 0); + assert_eq!(state.dns_lookups(), 0); + assert!(state.request_timestamps.is_empty()); + assert!(state.dns_cache.is_empty()); + } + + #[test] + fn test_validate_ip_against_policy_private_blocked() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.dns_policy.block_private_ranges = true; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let loopback = IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)); + let err = state.validate_ip_against_policy(loopback).unwrap_err(); + assert!(matches!(err, NetworkError::PolicyViolation(_))); + } + + #[test] + fn test_validate_ip_against_policy_public_allowed() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.dns_policy.block_private_ranges = true; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let public = IpAddr::V4(std::net::Ipv4Addr::new(8, 8, 8, 8)); + assert!(state.validate_ip_against_policy(public).is_ok()); + } + + #[test] + fn test_validate_ip_against_policy_ip_range_filter() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.dns_policy.block_private_ranges = false; + policy.allowed_ip_ranges = vec!["8.8.0.0/16".to_string()]; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let allowed = IpAddr::V4(std::net::Ipv4Addr::new(8, 8, 8, 8)); + assert!(state.validate_ip_against_policy(allowed).is_ok()); + + let denied = IpAddr::V4(std::net::Ipv4Addr::new(1, 1, 1, 1)); + let err = state.validate_ip_against_policy(denied).unwrap_err(); + assert!(matches!(err, NetworkError::PolicyViolation(_))); + } + + #[test] + fn test_validate_ip_against_policy_empty_ranges_no_block() { + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.dns_policy.block_private_ranges = false; + policy.allowed_ip_ranges = vec![]; + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let any_ip = IpAddr::V4(std::net::Ipv4Addr::new(8, 8, 8, 8)); + assert!(state.validate_ip_against_policy(any_ip).is_ok()); + } + + #[test] + fn test_audit_denial_logged() { + use std::sync::Mutex; + + struct TestLogger { + entries: Mutex>, + } + + impl NetworkAuditLogger for TestLogger { + fn record(&self, entry: NetworkAuditEntry) { + self.entries.lock().unwrap().push(entry); + } + } + + let logger = Arc::new(TestLogger { + entries: Mutex::new(Vec::new()), + }); + + let mut policy = test_policy_strict(vec!["example.com".to_string()]); + policy.audit.enabled = true; + let state = NetworkState::new( + policy, + Some(logger.clone()), + "chal-1".into(), + "val-1".into(), + ) + .unwrap(); + + state.audit_denial("test denial reason"); + + let entries = logger.entries.lock().unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].challenge_id, "chal-1"); + assert_eq!(entries[0].validator_id, "val-1"); + match &entries[0].action { + NetworkAuditAction::PolicyDenied { reason } => { + assert!(reason.contains("test denial reason")); + } + _ => panic!("expected PolicyDenied action"), + } + } + + #[test] + fn test_header_size_calculation() { + let mut headers = HashMap::new(); + headers.insert("key1".to_string(), "val1".to_string()); + headers.insert("key2".to_string(), "val2".to_string()); + assert_eq!(header_size(&headers), 16); + } + + #[test] + fn test_network_disabled_policy() { + let policy = NetworkPolicy::default(); + let state = NetworkState::new(policy, None, "test".into(), "test".into()).unwrap(); + + let request = HttpRequest { + method: HttpMethod::Get, + url: "https://example.com".to_string(), + headers: HashMap::new(), + body: Vec::new(), + }; + + let err = state.validate_http_request(&request).unwrap_err(); + assert!(matches!(err, NetworkError::NetworkDisabled)); + } + + #[test] + fn test_map_policy_error_network_disabled() { + let err = map_policy_error(NetworkPolicyError::NetworkDisabled); + assert!(matches!(err, NetworkError::NetworkDisabled)); + } + + #[test] + fn test_map_policy_error_other() { + let err = map_policy_error(NetworkPolicyError::HostNotAllowed("evil.com".into())); + assert!(matches!(err, NetworkError::PolicyViolation(_))); + } +} diff --git a/crates/wasm-runtime-interface/src/runtime.rs b/crates/wasm-runtime-interface/src/runtime.rs new file mode 100644 index 000000000..922be59e0 --- /dev/null +++ b/crates/wasm-runtime-interface/src/runtime.rs @@ -0,0 +1,679 @@ +use crate::bridge::{self, BridgeError, EvalRequest, EvalResponse}; +use crate::consensus::{ConsensusHostFunctions, ConsensusPolicy, ConsensusState}; +use crate::container::{ContainerHostFunctions, ContainerPolicy, ContainerState}; +use crate::data::{DataBackend, DataHostFunctions, DataPolicy, DataState, NoopDataBackend}; +use crate::exec::{ExecHostFunctions, ExecPolicy, ExecState}; +use crate::llm::{LlmHostFunctions, LlmPolicy, LlmState}; +use crate::sandbox::SandboxHostFunctions; +use crate::storage::{ + InMemoryStorageBackend, StorageBackend, StorageHostConfig, StorageHostFunctions, + StorageHostState, +}; +use crate::terminal::{TerminalHostFunctions, TerminalPolicy, TerminalState}; +use crate::time::{TimeHostFunctions, TimePolicy, TimeState}; +use crate::{NetworkAuditLogger, NetworkHostFunctions, NetworkPolicy, NetworkState, SandboxPolicy}; +use std::sync::Arc; +use std::time::Instant; +use thiserror::Error; +use tracing::info; +use wasmtime::{ + Config, Engine, Error as WasmtimeError, Func, Instance, Linker, Memory, Module, + ResourceLimiter, Store, StoreLimits, StoreLimitsBuilder, Val, +}; + +pub const DEFAULT_WASM_MEMORY_NAME: &str = "memory"; + +#[derive(Debug, Error)] +pub enum WasmRuntimeError { + #[error("module compile failed: {0}")] + Compile(String), + #[error("module instantiation failed: {0}")] + Instantiate(String), + #[error("host function registration failed: {0}")] + HostFunction(String), + #[error("missing export: {0}")] + MissingExport(String), + #[error("memory error: {0}")] + Memory(String), + #[error("execution error: {0}")] + Execution(String), + #[error("io error: {0}")] + Io(String), + #[error("fuel exhausted")] + FuelExhausted, + #[error("policy violation: {0}")] + PolicyViolation(String), + #[error("bridge error: {0}")] + Bridge(String), +} + +impl From for WasmRuntimeError { + fn from(err: WasmtimeError) -> Self { + let msg = err.to_string(); + if msg.contains("fuel") { + Self::FuelExhausted + } else { + Self::Execution(msg) + } + } +} + +impl From for WasmRuntimeError { + fn from(err: std::io::Error) -> Self { + Self::Io(err.to_string()) + } +} + +impl From for WasmRuntimeError { + fn from(err: BridgeError) -> Self { + Self::Bridge(err.to_string()) + } +} + +pub trait HostFunctionRegistrar: Send + Sync { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError>; +} + +#[derive(Clone)] +pub struct RuntimeConfig { + pub max_memory_bytes: u64, + pub max_instances: u32, + pub allow_fuel: bool, + pub fuel_limit: Option, +} + +impl Default for RuntimeConfig { + fn default() -> Self { + Self { + max_memory_bytes: 512 * 1024 * 1024, + max_instances: 32, + allow_fuel: false, + fuel_limit: None, + } + } +} + +#[derive(Clone)] +pub struct InstanceConfig { + /// Network policy enforced by host functions. + pub network_policy: NetworkPolicy, + /// Sandbox policy for challenge execution. + pub sandbox_policy: SandboxPolicy, + /// Exec policy enforced by host functions. + pub exec_policy: ExecPolicy, + /// Time policy enforced by host functions. + pub time_policy: TimePolicy, + /// Optional audit logger for network calls. + pub audit_logger: Option>, + /// Wasm memory export name. + pub memory_export: String, + /// Identifier used in audit logs. + pub challenge_id: String, + /// Validator identifier used in audit logs. + pub validator_id: String, + /// Restartable configuration identifier. + pub restart_id: String, + /// Configuration version for hot-restarts. + pub config_version: u64, + /// Storage host function configuration. + pub storage_host_config: StorageHostConfig, + /// Storage backend implementation. + pub storage_backend: Arc, + /// Fixed timestamp for deterministic consensus execution. + pub fixed_timestamp_ms: Option, + /// Consensus policy for WASM access to chain state. + pub consensus_policy: ConsensusPolicy, + /// Terminal policy for WASM access to terminal operations. + pub terminal_policy: TerminalPolicy, + /// Data policy for WASM access to challenge data. + pub data_policy: DataPolicy, + /// Data backend implementation. + pub data_backend: Arc, + /// Container policy for WASM access to container execution. + pub container_policy: ContainerPolicy, + /// LLM policy for WASM access to LLM inference. + pub llm_policy: LlmPolicy, +} + +impl Default for InstanceConfig { + fn default() -> Self { + Self { + network_policy: NetworkPolicy::default(), + sandbox_policy: SandboxPolicy::default(), + exec_policy: ExecPolicy::default(), + time_policy: TimePolicy::default(), + audit_logger: None, + memory_export: DEFAULT_WASM_MEMORY_NAME.to_string(), + challenge_id: "unknown".to_string(), + validator_id: "unknown".to_string(), + restart_id: String::new(), + config_version: 0, + storage_host_config: StorageHostConfig::default(), + storage_backend: Arc::new(InMemoryStorageBackend::new()), + fixed_timestamp_ms: None, + consensus_policy: ConsensusPolicy::default(), + terminal_policy: TerminalPolicy::default(), + data_policy: DataPolicy::default(), + data_backend: Arc::new(NoopDataBackend), + container_policy: ContainerPolicy::default(), + llm_policy: LlmPolicy::default(), + } + } +} + +pub struct RuntimeState { + /// Network policy available to host functions. + pub network_policy: NetworkPolicy, + /// Sandbox policy for challenge execution. + pub sandbox_policy: SandboxPolicy, + /// Mutable network state enforcing policy. + pub network_state: NetworkState, + /// Mutable exec state enforcing policy. + pub exec_state: ExecState, + /// Time state for deterministic or real timestamps. + pub time_state: TimeState, + /// Wasm memory export name. + pub memory_export: String, + /// Identifier used in audit logs. + pub challenge_id: String, + /// Validator identifier used in audit logs. + pub validator_id: String, + /// Restartable configuration identifier. + pub restart_id: String, + /// Configuration version for hot-restarts. + pub config_version: u64, + /// Storage host state for key-value operations. + pub storage_state: StorageHostState, + /// Fixed timestamp in milliseconds for deterministic consensus execution. + pub fixed_timestamp_ms: Option, + /// Consensus state for chain-level queries. + pub consensus_state: ConsensusState, + /// Terminal state for terminal host operations. + pub terminal_state: TerminalState, + /// Data state for challenge data host operations. + pub data_state: DataState, + /// Container state for container execution host operations. + pub container_state: ContainerState, + /// LLM state for LLM inference host operations. + pub llm_state: LlmState, + limits: StoreLimits, +} + +impl RuntimeState { + #[allow(clippy::too_many_arguments)] + pub fn new( + network_policy: NetworkPolicy, + sandbox_policy: SandboxPolicy, + network_state: NetworkState, + exec_state: ExecState, + time_state: TimeState, + consensus_state: ConsensusState, + terminal_state: TerminalState, + data_state: DataState, + container_state: ContainerState, + llm_state: LlmState, + memory_export: String, + challenge_id: String, + validator_id: String, + restart_id: String, + config_version: u64, + storage_state: StorageHostState, + fixed_timestamp_ms: Option, + limits: StoreLimits, + ) -> Self { + Self { + network_policy, + sandbox_policy, + network_state, + exec_state, + time_state, + consensus_state, + terminal_state, + data_state, + container_state, + llm_state, + memory_export, + challenge_id, + validator_id, + restart_id, + config_version, + storage_state, + fixed_timestamp_ms, + limits, + } + } + + pub fn reset_network_counters(&mut self) { + self.network_state.reset_counters(); + } + + pub fn reset_storage_counters(&mut self) { + self.storage_state.reset_counters(); + } + + pub fn reset_exec_counters(&mut self) { + self.exec_state.reset_counters(); + } + + pub fn reset_container_counters(&mut self) { + self.container_state.reset_counters(); + } + + pub fn reset_data_counters(&mut self) { + self.data_state.reset_counters(); + } +} + +impl ResourceLimiter for RuntimeState { + fn memory_growing( + &mut self, + current: usize, + desired: usize, + maximum: Option, + ) -> Result { + self.limits.memory_growing(current, desired, maximum) + } + + fn table_growing( + &mut self, + current: usize, + desired: usize, + maximum: Option, + ) -> Result { + self.limits.table_growing(current, desired, maximum) + } +} + +pub struct WasmRuntime { + engine: Engine, + config: RuntimeConfig, +} + +impl WasmRuntime { + pub fn new(config: RuntimeConfig) -> Result { + let mut engine_config = Config::new(); + if config.allow_fuel { + engine_config.consume_fuel(true); + } + let engine = Engine::new(&engine_config) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string()))?; + Ok(Self { engine, config }) + } + + pub fn from_engine(engine: Engine, config: RuntimeConfig) -> Self { + Self { engine, config } + } + + pub fn compile_module(&self, wasm: &[u8]) -> Result { + let module = Module::from_binary(&self.engine, wasm) + .map_err(|err: WasmtimeError| WasmRuntimeError::Compile(err.to_string()))?; + Ok(WasmModule { module }) + } + + pub fn instantiate( + &self, + module: &WasmModule, + instance_config: InstanceConfig, + registrar: Option>, + ) -> Result { + let mut limits = StoreLimitsBuilder::new(); + limits = limits.memory_size(self.config.max_memory_bytes as usize); + limits = limits.instances(self.config.max_instances as usize); + let network_state = NetworkState::new( + instance_config.network_policy.clone(), + instance_config.audit_logger.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + let storage_state = StorageHostState::new( + instance_config.challenge_id.clone(), + instance_config.storage_host_config.clone(), + Arc::clone(&instance_config.storage_backend), + ); + let exec_state = ExecState::new( + instance_config.exec_policy.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ); + let time_state = TimeState::new( + instance_config.time_policy.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ); + let consensus_state = ConsensusState::new( + instance_config.consensus_policy.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ); + let terminal_state = TerminalState::new( + instance_config.terminal_policy.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ); + let data_state = DataState::new( + instance_config.data_policy.clone(), + Arc::clone(&instance_config.data_backend), + instance_config.challenge_id.clone(), + ); + let container_state = ContainerState::new( + instance_config.container_policy.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + ); + let llm_state = LlmState::new(instance_config.llm_policy.clone()); + let runtime_state = RuntimeState::new( + instance_config.network_policy.clone(), + instance_config.sandbox_policy.clone(), + network_state, + exec_state, + time_state, + consensus_state, + terminal_state, + data_state, + container_state, + llm_state, + instance_config.memory_export.clone(), + instance_config.challenge_id.clone(), + instance_config.validator_id.clone(), + instance_config.restart_id.clone(), + instance_config.config_version, + storage_state, + instance_config.fixed_timestamp_ms, + limits.build(), + ); + let mut store = Store::new(&self.engine, runtime_state); + + if self.config.allow_fuel { + if let Some(limit) = self.config.fuel_limit { + store + .set_fuel(limit) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string()))?; + } + } + + store.limiter(|state| &mut state.limits); + + let mut linker = Linker::new(&self.engine); + + let network_host_fns = NetworkHostFunctions::all(); + network_host_fns.register(&mut linker)?; + + let storage_host_fns = StorageHostFunctions::new(); + storage_host_fns.register(&mut linker)?; + + let exec_host_fns = ExecHostFunctions::all(); + exec_host_fns.register(&mut linker)?; + + let time_host_fns = TimeHostFunctions::all(); + time_host_fns.register(&mut linker)?; + + let consensus_host_fns = ConsensusHostFunctions::new(); + consensus_host_fns.register(&mut linker)?; + + let terminal_host_fns = TerminalHostFunctions::new(); + terminal_host_fns.register(&mut linker)?; + + let data_host_fns = DataHostFunctions::new(); + data_host_fns.register(&mut linker)?; + + let container_host_fns = ContainerHostFunctions::new(); + container_host_fns.register(&mut linker)?; + + let llm_host_fns = LlmHostFunctions::new(); + llm_host_fns.register(&mut linker)?; + + let sandbox_host_fns = SandboxHostFunctions::all(); + sandbox_host_fns.register(&mut linker)?; + + if let Some(registrar) = registrar { + registrar.register(&mut linker)?; + } + + let instance = linker + .instantiate(&mut store, &module.module) + .map_err(|err: WasmtimeError| WasmRuntimeError::Instantiate(err.to_string()))?; + + let memory = instance + .get_memory(&mut store, &instance_config.memory_export) + .ok_or_else(|| { + WasmRuntimeError::MissingExport(instance_config.memory_export.clone()) + })?; + + info!( + challenge_id = %instance_config.challenge_id, + validator_id = %instance_config.validator_id, + max_memory = self.config.max_memory_bytes, + fuel_enabled = self.config.allow_fuel, + fuel_limit = ?self.config.fuel_limit, + "wasm challenge instance created" + ); + + Ok(ChallengeInstance { + store, + instance, + memory, + }) + } +} + +pub struct WasmModule { + module: Module, +} + +impl WasmModule { + pub fn module(&self) -> &Module { + &self.module + } +} + +pub struct ChallengeInstance { + store: Store, + instance: Instance, + memory: Memory, +} + +impl ChallengeInstance { + pub fn store(&self) -> &Store { + &self.store + } + + pub fn store_mut(&mut self) -> &mut Store { + &mut self.store + } + + pub fn memory(&self) -> &Memory { + &self.memory + } + + pub fn get_func(&mut self, name: &str) -> Result { + self.instance + .get_func(&mut self.store, name) + .ok_or_else(|| WasmRuntimeError::MissingExport(name.to_string())) + } + + pub fn call(&mut self, name: &str, params: &[Val]) -> Result, WasmRuntimeError> { + let func = self.get_func(name)?; + let ty = func.ty(&self.store); + let mut results = vec![Val::I32(0); ty.results().len()]; + func.call(&mut self.store, params, &mut results)?; + Ok(results) + } + + pub fn read_memory( + &mut self, + offset: usize, + length: usize, + ) -> Result, WasmRuntimeError> { + let data = self.memory.data(&self.store); + let end = offset.saturating_add(length); + if end > data.len() { + return Err(WasmRuntimeError::Memory("read out of bounds".to_string())); + } + Ok(data[offset..end].to_vec()) + } + + pub fn write_memory(&mut self, offset: usize, bytes: &[u8]) -> Result<(), WasmRuntimeError> { + let data = self.memory.data_mut(&mut self.store); + let end = offset.saturating_add(bytes.len()); + if end > data.len() { + return Err(WasmRuntimeError::Memory("write out of bounds".to_string())); + } + data[offset..end].copy_from_slice(bytes); + Ok(()) + } + + pub fn call_i32_i32_return_i64( + &mut self, + name: &str, + arg0: i32, + arg1: i32, + ) -> Result { + let func = self + .instance + .get_typed_func::<(i32, i32), i64>(&mut self.store, name) + .map_err(|_| WasmRuntimeError::MissingExport(name.to_string()))?; + func.call(&mut self.store, (arg0, arg1)) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string())) + } + + pub fn call_i32_i32_return_i32( + &mut self, + name: &str, + arg0: i32, + arg1: i32, + ) -> Result { + let func = self + .instance + .get_typed_func::<(i32, i32), i32>(&mut self.store, name) + .map_err(|_| WasmRuntimeError::MissingExport(name.to_string()))?; + func.call(&mut self.store, (arg0, arg1)) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string())) + } + + pub fn call_i32_return_i32(&mut self, name: &str, arg0: i32) -> Result { + let func = self + .instance + .get_typed_func::(&mut self.store, name) + .map_err(|_| WasmRuntimeError::MissingExport(name.to_string()))?; + func.call(&mut self.store, arg0) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string())) + } + + pub fn call_return_i32(&mut self, name: &str) -> Result { + let func = self + .instance + .get_typed_func::<(), i32>(&mut self.store, name) + .map_err(|_| WasmRuntimeError::MissingExport(name.to_string()))?; + func.call(&mut self.store, ()) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string())) + } + + pub fn call_return_i64(&mut self, name: &str) -> Result { + let func = self + .instance + .get_typed_func::<(), i64>(&mut self.store, name) + .map_err(|_| WasmRuntimeError::MissingExport(name.to_string()))?; + func.call(&mut self.store, ()) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string())) + } + + pub fn fuel_remaining(&self) -> Option { + self.store.get_fuel().ok() + } + + pub fn network_requests_made(&self) -> u32 { + self.store.data().network_state.requests_made() + } + + pub fn network_dns_lookups(&self) -> u32 { + self.store.data().network_state.dns_lookups() + } + + pub fn reset_network_state(&mut self) { + self.store.data_mut().reset_network_counters(); + } + + pub fn reset_storage_state(&mut self) { + self.store.data_mut().reset_storage_counters(); + } + + pub fn storage_bytes_read(&self) -> u64 { + self.store.data().storage_state.bytes_read + } + + pub fn storage_bytes_written(&self) -> u64 { + self.store.data().storage_state.bytes_written + } + + pub fn storage_operations_count(&self) -> u32 { + self.store.data().storage_state.operations_count + } + + pub fn challenge_id(&self) -> &str { + &self.store.data().challenge_id + } + + pub fn validator_id(&self) -> &str { + &self.store.data().validator_id + } + + pub fn exec_executions(&self) -> u32 { + self.store.data().exec_state.executions() + } + + pub fn reset_exec_state(&mut self) { + self.store.data_mut().reset_exec_counters(); + } + + pub fn evaluate_request(&mut self, req: EvalRequest) -> Result { + let start = Instant::now(); + let request_id = req.request_id.clone(); + let challenge_id = self.store.data().challenge_id.clone(); + + let input = bridge::request_to_input(&req, &challenge_id)?; + let input_bytes = bridge::input_to_bytes(&input)?; + + let alloc_func = self + .instance + .get_typed_func::(&mut self.store, "alloc") + .map_err(|_| WasmRuntimeError::MissingExport("alloc".to_string()))?; + + let ptr = alloc_func + .call(&mut self.store, input_bytes.len() as i32) + .map_err(|err: WasmtimeError| WasmRuntimeError::Execution(err.to_string()))?; + + if ptr == 0 { + return Err(WasmRuntimeError::Memory( + "alloc returned null pointer".to_string(), + )); + } + + self.write_memory(ptr as usize, &input_bytes)?; + + let packed = self.call_i32_i32_return_i64("evaluate", ptr, input_bytes.len() as i32)?; + + let out_len = (packed >> 32) as i32; + let out_ptr = (packed & 0xFFFF_FFFF) as i32; + + if out_ptr == 0 && out_len == 0 { + return Ok( + EvalResponse::error(&request_id, "WASM evaluate returned null") + .with_time(start.elapsed().as_millis() as i64), + ); + } + + let output_bytes = self.read_memory(out_ptr as usize, out_len as usize)?; + let output = bridge::bytes_to_output(&output_bytes)?; + + let elapsed_ms = start.elapsed().as_millis() as i64; + Ok(bridge::output_to_response(&output, &request_id, elapsed_ms)) + } + + pub fn with_state(&mut self, func: F) -> Result + where + F: FnOnce(&mut RuntimeState) -> Result, + { + func(self.store.data_mut()) + } +} diff --git a/crates/wasm-runtime-interface/src/sandbox.rs b/crates/wasm-runtime-interface/src/sandbox.rs new file mode 100644 index 000000000..4e3210bd2 --- /dev/null +++ b/crates/wasm-runtime-interface/src/sandbox.rs @@ -0,0 +1,762 @@ +//! Sandbox Host Functions for WASM Challenges +//! +//! This module provides host functions that allow WASM code to interact with +//! sandboxed command execution. All operations are gated by `SandboxPolicy`. +//! +//! # Host Functions +//! +//! - `sandbox_exec(cmd_ptr, cmd_len) -> i64` - Execute a sandboxed command +//! - `sandbox_get_tasks() -> i64` - Retrieve pending task list +//! - `sandbox_configure(cfg_ptr, cfg_len) -> i32` - Update sandbox configuration +//! - `sandbox_status() -> i32` - Query sandbox status +//! - `get_timestamp() -> i64` - Get current timestamp in milliseconds +//! - `log_message(level, msg_ptr, msg_len)` - Log a message from WASM + +#![allow(dead_code, unused_variables, unused_imports)] + +use crate::SandboxPolicy; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::process::Command; +use std::sync::{Arc, RwLock}; +use std::time::{Duration, Instant}; +use thiserror::Error; +use tracing::{debug, error, info, warn}; +use wasmtime::{Caller, Linker, Memory}; + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; + +pub const HOST_SANDBOX_NAMESPACE: &str = "platform_sandbox"; +pub const HOST_SANDBOX_EXEC: &str = "sandbox_exec"; +pub const HOST_SANDBOX_GET_TASKS: &str = "sandbox_get_tasks"; +pub const HOST_SANDBOX_CONFIGURE: &str = "sandbox_configure"; +pub const HOST_SANDBOX_STATUS: &str = "sandbox_status"; +pub const HOST_SANDBOX_GET_TIMESTAMP: &str = "get_timestamp"; +pub const HOST_SANDBOX_LOG_MESSAGE: &str = "log_message"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum SandboxHostStatus { + Success = 0, + Disabled = 1, + CommandNotAllowed = -1, + ExecutionTimeout = -2, + ExecutionFailed = -3, + InvalidConfig = -4, + InternalError = -100, +} + +impl SandboxHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } + + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::Disabled, + -1 => Self::CommandNotAllowed, + -2 => Self::ExecutionTimeout, + -3 => Self::ExecutionFailed, + -4 => Self::InvalidConfig, + _ => Self::InternalError, + } + } +} + +#[derive(Debug, Error)] +pub enum SandboxHostError { + #[error("sandbox disabled")] + Disabled, + + #[error("command not allowed: {0}")] + CommandNotAllowed(String), + + #[error("execution timeout after {0}s")] + ExecutionTimeout(u64), + + #[error("execution failed: {0}")] + ExecutionFailed(String), + + #[error("invalid configuration: {0}")] + InvalidConfig(String), + + #[error("memory error: {0}")] + MemoryError(String), + + #[error("internal error: {0}")] + InternalError(String), +} + +impl From for SandboxHostStatus { + fn from(err: SandboxHostError) -> Self { + match err { + SandboxHostError::Disabled => Self::Disabled, + SandboxHostError::CommandNotAllowed(_) => Self::CommandNotAllowed, + SandboxHostError::ExecutionTimeout(_) => Self::ExecutionTimeout, + SandboxHostError::ExecutionFailed(_) => Self::ExecutionFailed, + SandboxHostError::InvalidConfig(_) => Self::InvalidConfig, + SandboxHostError::MemoryError(_) => Self::InternalError, + SandboxHostError::InternalError(_) => Self::InternalError, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SandboxExecRequest { + pub command: String, + pub args: Vec, + pub env_vars: Vec<(String, String)>, + pub working_dir: Option, + pub stdin: Option>, + pub timeout_ms: u64, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SandboxExecResponse { + pub exit_code: i32, + pub stdout: Vec, + pub stderr: Vec, + pub duration_ms: u64, +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum SandboxExecError { + Disabled, + CommandNotAllowed(String), + ExecutionTimeout(u64), + ExecutionFailed(String), + MemoryError(String), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SandboxHostConfig { + pub policy: SandboxPolicy, + pub max_concurrent_tasks: usize, + pub max_output_bytes: usize, +} + +impl Default for SandboxHostConfig { + fn default() -> Self { + Self { + policy: SandboxPolicy::default(), + max_concurrent_tasks: 4, + max_output_bytes: 1024 * 1024, + } + } +} + +impl SandboxHostConfig { + pub fn permissive() -> Self { + Self { + policy: SandboxPolicy::development(), + max_concurrent_tasks: 16, + max_output_bytes: 10 * 1024 * 1024, + } + } + + pub fn is_command_allowed(&self, command: &str) -> bool { + if !self.policy.enable_sandbox { + return false; + } + self.policy + .allowed_commands + .iter() + .any(|c| c == "*" || c == command) + } +} + +pub struct SandboxHostState { + pub config: SandboxHostConfig, + pub challenge_id: String, + pub pending_results: HashMap>, + pub next_result_id: u32, + pub commands_executed: u32, +} + +impl SandboxHostState { + pub fn new(challenge_id: String, config: SandboxHostConfig) -> Self { + Self { + config, + challenge_id, + pending_results: HashMap::new(), + next_result_id: 1, + commands_executed: 0, + } + } + + pub fn store_result(&mut self, data: Vec) -> u32 { + let id = self.next_result_id; + self.next_result_id = self.next_result_id.wrapping_add(1); + self.pending_results.insert(id, data); + id + } + + pub fn take_result(&mut self, id: u32) -> Option> { + self.pending_results.remove(&id) + } + + pub fn reset_counters(&mut self) { + self.commands_executed = 0; + } +} + +pub struct SandboxHostFunctions; + +impl SandboxHostFunctions { + pub fn all() -> Self { + Self + } +} + +impl HostFunctionRegistrar for SandboxHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap(HOST_SANDBOX_NAMESPACE, HOST_SANDBOX_STATUS, || -> i32 { + SandboxHostStatus::Success.to_i32() + }) + .map_err(|e| { + WasmRuntimeError::HostFunction(format!( + "failed to register {}: {}", + HOST_SANDBOX_STATUS, e + )) + })?; + + linker + .func_wrap( + HOST_SANDBOX_NAMESPACE, + HOST_SANDBOX_EXEC, + |mut caller: Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32| + -> i32 { + handle_sandbox_exec(&mut caller, req_ptr, req_len, resp_ptr, resp_len) + }, + ) + .map_err(|e| { + WasmRuntimeError::HostFunction(format!( + "failed to register {}: {}", + HOST_SANDBOX_EXEC, e + )) + })?; + + linker + .func_wrap( + HOST_SANDBOX_NAMESPACE, + HOST_SANDBOX_GET_TIMESTAMP, + |caller: Caller| -> i64 { handle_get_timestamp(&caller) }, + ) + .map_err(|e| { + WasmRuntimeError::HostFunction(format!( + "failed to register {}: {}", + HOST_SANDBOX_GET_TIMESTAMP, e + )) + })?; + + linker + .func_wrap( + HOST_SANDBOX_NAMESPACE, + HOST_SANDBOX_LOG_MESSAGE, + |mut caller: Caller, level: i32, msg_ptr: i32, msg_len: i32| { + handle_log_message(&mut caller, level, msg_ptr, msg_len); + }, + ) + .map_err(|e| { + WasmRuntimeError::HostFunction(format!( + "failed to register {}: {}", + HOST_SANDBOX_LOG_MESSAGE, e + )) + })?; + + Ok(()) + } +} + +fn handle_sandbox_exec( + caller: &mut Caller, + req_ptr: i32, + req_len: i32, + resp_ptr: i32, + resp_len: i32, +) -> i32 { + let request_bytes = match read_memory(caller, req_ptr, req_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "sandbox_exec host memory read failed" + ); + return write_result( + caller, + resp_ptr, + resp_len, + Err::(SandboxExecError::MemoryError(err)), + ); + } + }; + + let request = match bincode::deserialize::(&request_bytes) { + Ok(req) => req, + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "sandbox_exec request decode failed" + ); + return write_result( + caller, + resp_ptr, + resp_len, + Err::(SandboxExecError::ExecutionFailed( + format!("invalid sandbox exec request: {err}"), + )), + ); + } + }; + + let policy = &caller.data().sandbox_policy; + + if !policy.enable_sandbox { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + command = %request.command, + "sandbox_exec denied: sandbox disabled" + ); + return write_result( + caller, + resp_ptr, + resp_len, + Err::(SandboxExecError::Disabled), + ); + } + + let command_allowed = policy + .allowed_commands + .iter() + .any(|c| c == "*" || c == &request.command); + + if !command_allowed { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + command = %request.command, + "sandbox_exec command not allowed" + ); + return write_result( + caller, + resp_ptr, + resp_len, + Err::(SandboxExecError::CommandNotAllowed( + request.command, + )), + ); + } + + let timeout_secs = caller.data().sandbox_policy.max_execution_time_secs; + let timeout_ms = if request.timeout_ms > 0 { + request.timeout_ms.min(timeout_secs.saturating_mul(1000)) + } else { + timeout_secs.saturating_mul(1000) + }; + let timeout = Duration::from_millis(timeout_ms); + + let result = execute_command(&request, timeout); + + let challenge_id = caller.data().challenge_id.clone(); + let validator_id = caller.data().validator_id.clone(); + + match &result { + Ok(resp) => { + info!( + challenge_id = %challenge_id, + validator_id = %validator_id, + command = %request.command, + exit_code = resp.exit_code, + stdout_bytes = resp.stdout.len(), + stderr_bytes = resp.stderr.len(), + duration_ms = resp.duration_ms, + "sandbox_exec command completed" + ); + } + Err(err) => { + warn!( + challenge_id = %challenge_id, + validator_id = %validator_id, + command = %request.command, + error = ?err, + "sandbox_exec command failed" + ); + } + } + + write_result(caller, resp_ptr, resp_len, result) +} + +fn execute_command( + request: &SandboxExecRequest, + timeout: Duration, +) -> Result { + let start = Instant::now(); + + let mut cmd = Command::new(&request.command); + cmd.args(&request.args); + cmd.env_clear(); + for (key, value) in &request.env_vars { + cmd.env(key, value); + } + + if let Some(ref dir) = request.working_dir { + cmd.current_dir(dir); + } + + let has_stdin = request.stdin.as_ref().is_some_and(|s| !s.is_empty()); + + if has_stdin { + cmd.stdin(std::process::Stdio::piped()); + } else { + cmd.stdin(std::process::Stdio::null()); + } + cmd.stdout(std::process::Stdio::piped()); + cmd.stderr(std::process::Stdio::piped()); + + let mut child = cmd + .spawn() + .map_err(|e| SandboxExecError::ExecutionFailed(e.to_string()))?; + + if has_stdin { + if let Some(ref stdin_data) = request.stdin { + if let Some(ref mut stdin) = child.stdin { + use std::io::Write; + let _ = stdin.write_all(stdin_data); + } + } + child.stdin.take(); + } + + let output = loop { + if start.elapsed() > timeout { + let _ = child.kill(); + return Err(SandboxExecError::ExecutionTimeout(timeout.as_secs())); + } + match child.try_wait() { + Ok(Some(_)) => { + break child + .wait_with_output() + .map_err(|e| SandboxExecError::ExecutionFailed(e.to_string()))? + } + Ok(None) => std::thread::sleep(Duration::from_millis(10)), + Err(e) => return Err(SandboxExecError::ExecutionFailed(e.to_string())), + } + }; + + let duration_ms = start.elapsed().as_millis() as u64; + + Ok(SandboxExecResponse { + exit_code: output.status.code().unwrap_or(-1), + stdout: output.stdout, + stderr: output.stderr, + duration_ms, + }) +} + +fn handle_get_timestamp(caller: &Caller) -> i64 { + if let Some(ts) = caller.data().fixed_timestamp_ms { + return ts; + } + chrono::Utc::now().timestamp_millis() +} + +fn handle_log_message(caller: &mut Caller, level: i32, msg_ptr: i32, msg_len: i32) { + let msg = match read_memory(caller, msg_ptr, msg_len) { + Ok(bytes) => String::from_utf8_lossy(&bytes).into_owned(), + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + error = %err, + "sandbox log_message: failed to read message from wasm memory" + ); + return; + } + }; + + let challenge_id = caller.data().challenge_id.clone(); + match level { + 0 => info!(challenge_id = %challenge_id, "[wasm-sandbox] {}", msg), + 1 => warn!(challenge_id = %challenge_id, "[wasm-sandbox] {}", msg), + _ => error!(challenge_id = %challenge_id, "[wasm-sandbox] {}", msg), + } +} + +fn read_memory(caller: &mut Caller, ptr: i32, len: i32) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_result( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + result: Result, +) -> i32 { + let response_bytes = match bincode::serialize(&result) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "failed to serialize sandbox exec response"); + return -1; + } + }; + + write_bytes(caller, resp_ptr, resp_len, &response_bytes) +} + +fn write_bytes( + caller: &mut Caller, + resp_ptr: i32, + resp_len: i32, + bytes: &[u8], +) -> i32 { + if resp_ptr < 0 || resp_len < 0 { + return -1; + } + if bytes.len() > i32::MAX as usize { + return -1; + } + let resp_len = resp_len as usize; + if bytes.len() > resp_len { + return -(bytes.len() as i32); + } + + let memory = match get_memory(caller) { + Some(memory) => memory, + None => return -1, + }; + + let ptr = resp_ptr as usize; + let end = match ptr.checked_add(bytes.len()) { + Some(end) => end, + None => return -1, + }; + let data = memory.data_mut(caller); + if end > data.len() { + return -1; + } + data[ptr..end].copy_from_slice(bytes); + bytes.len() as i32 +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sandbox_host_status_conversion() { + assert_eq!(SandboxHostStatus::Success.to_i32(), 0); + assert_eq!(SandboxHostStatus::Disabled.to_i32(), 1); + assert_eq!(SandboxHostStatus::CommandNotAllowed.to_i32(), -1); + assert_eq!(SandboxHostStatus::InternalError.to_i32(), -100); + + assert_eq!(SandboxHostStatus::from_i32(0), SandboxHostStatus::Success); + assert_eq!(SandboxHostStatus::from_i32(1), SandboxHostStatus::Disabled); + assert_eq!( + SandboxHostStatus::from_i32(-1), + SandboxHostStatus::CommandNotAllowed + ); + assert_eq!( + SandboxHostStatus::from_i32(-999), + SandboxHostStatus::InternalError + ); + } + + #[test] + fn test_sandbox_host_error_to_status() { + let err = SandboxHostError::Disabled; + assert_eq!(SandboxHostStatus::from(err), SandboxHostStatus::Disabled); + + let err = SandboxHostError::CommandNotAllowed("bash".to_string()); + assert_eq!( + SandboxHostStatus::from(err), + SandboxHostStatus::CommandNotAllowed + ); + + let err = SandboxHostError::ExecutionTimeout(30); + assert_eq!( + SandboxHostStatus::from(err), + SandboxHostStatus::ExecutionTimeout + ); + } + + #[test] + fn test_sandbox_host_config_command_check() { + let config = SandboxHostConfig::default(); + assert!(!config.is_command_allowed("bash")); + + let config = SandboxHostConfig::permissive(); + assert!(config.is_command_allowed("bash")); + assert!(config.is_command_allowed("anything")); + + let config = SandboxHostConfig { + policy: SandboxPolicy { + enable_sandbox: true, + allowed_commands: vec!["bash".to_string(), "sh".to_string()], + max_execution_time_secs: 30, + }, + ..Default::default() + }; + assert!(config.is_command_allowed("bash")); + assert!(config.is_command_allowed("sh")); + assert!(!config.is_command_allowed("python3")); + } + + #[test] + fn test_sandbox_host_state() { + let mut state = + SandboxHostState::new("challenge-1".to_string(), SandboxHostConfig::default()); + + let id1 = state.store_result(b"result1".to_vec()); + let id2 = state.store_result(b"result2".to_vec()); + + assert_ne!(id1, id2); + + let result1 = state.take_result(id1); + assert_eq!(result1, Some(b"result1".to_vec())); + + let result1_again = state.take_result(id1); + assert_eq!(result1_again, None); + + let result2 = state.take_result(id2); + assert_eq!(result2, Some(b"result2".to_vec())); + } + + #[test] + fn test_sandbox_policy_defaults() { + let policy = SandboxPolicy::default(); + assert!(!policy.enable_sandbox); + assert!(policy.allowed_commands.is_empty()); + assert_eq!(policy.max_execution_time_secs, 30); + } + + #[test] + fn test_sandbox_policy_default_challenge() { + let policy = SandboxPolicy::default_challenge(); + assert!(policy.enable_sandbox); + assert!(policy.allowed_commands.contains(&"bash".to_string())); + assert!(policy.allowed_commands.contains(&"python3".to_string())); + assert_eq!(policy.max_execution_time_secs, 60); + } + + #[test] + fn test_execute_command_echo() { + let request = SandboxExecRequest { + command: "echo".to_string(), + args: vec!["hello".to_string()], + env_vars: Vec::new(), + working_dir: None, + stdin: None, + timeout_ms: 5000, + }; + + let result = execute_command(&request, Duration::from_secs(5)); + assert!(result.is_ok()); + let resp = result.unwrap(); + assert_eq!(resp.exit_code, 0); + assert_eq!(String::from_utf8_lossy(&resp.stdout).trim(), "hello"); + } + + #[test] + fn test_execute_command_not_found() { + let request = SandboxExecRequest { + command: "nonexistent_command_12345".to_string(), + args: Vec::new(), + env_vars: Vec::new(), + working_dir: None, + stdin: None, + timeout_ms: 5000, + }; + + let result = execute_command(&request, Duration::from_secs(5)); + assert!(result.is_err()); + match result { + Err(SandboxExecError::ExecutionFailed(_)) => {} + other => panic!("expected ExecutionFailed, got {:?}", other), + } + } + + #[test] + fn test_execute_command_with_stdin() { + let request = SandboxExecRequest { + command: "cat".to_string(), + args: Vec::new(), + env_vars: Vec::new(), + working_dir: None, + stdin: Some(b"stdin data".to_vec()), + timeout_ms: 5000, + }; + + let result = execute_command(&request, Duration::from_secs(5)); + assert!(result.is_ok()); + let resp = result.unwrap(); + assert_eq!(resp.exit_code, 0); + assert_eq!(String::from_utf8_lossy(&resp.stdout), "stdin data"); + } + + #[test] + fn test_sandbox_exec_request_serialization() { + let request = SandboxExecRequest { + command: "echo".to_string(), + args: vec!["test".to_string()], + env_vars: vec![("KEY".to_string(), "VALUE".to_string())], + working_dir: None, + stdin: None, + timeout_ms: 5000, + }; + + let bytes = bincode::serialize(&request).unwrap(); + let deserialized: SandboxExecRequest = bincode::deserialize(&bytes).unwrap(); + assert_eq!(deserialized.command, "echo"); + assert_eq!(deserialized.args, vec!["test"]); + } + + #[test] + fn test_sandbox_exec_response_serialization() { + let response = SandboxExecResponse { + exit_code: 0, + stdout: b"output".to_vec(), + stderr: Vec::new(), + duration_ms: 42, + }; + + let result: Result = Ok(response); + let bytes = bincode::serialize(&result).unwrap(); + let deserialized: Result = + bincode::deserialize(&bytes).unwrap(); + assert!(deserialized.is_ok()); + let resp = deserialized.unwrap(); + assert_eq!(resp.exit_code, 0); + assert_eq!(resp.stdout, b"output"); + } +} diff --git a/crates/wasm-runtime-interface/src/storage.rs b/crates/wasm-runtime-interface/src/storage.rs new file mode 100644 index 000000000..51e6bab3f --- /dev/null +++ b/crates/wasm-runtime-interface/src/storage.rs @@ -0,0 +1,875 @@ +//! Storage Host Functions for WASM Challenges +//! +//! This module provides host functions that allow WASM code to interact with +//! validated storage. All write operations go through consensus to prevent abuse. +//! +//! # Host Functions +//! +//! - `storage_get(key_ptr, key_len, value_ptr) -> i32` - Read from storage +//! - `storage_set(key_ptr, key_len, value_ptr, value_len) -> i32` - Write to storage +//! - `storage_propose_write(key_ptr, key_len, value_ptr, value_len) -> i64` - Propose a write +//! - `storage_delete(key_ptr, key_len) -> i32` - Delete from storage (requires consensus) +//! +//! # Memory Layout +//! +//! Return values use a packed i64 format: +//! - High 32 bits: status code (0 = success, negative = error) +//! - Low 32 bits: result pointer or length +//! +//! For `storage_get`: +//! - Success: returns pointer to result buffer in WASM memory +//! - Not found: returns 0 +//! - Error: returns negative status code + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; +use thiserror::Error; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; + +pub const HOST_STORAGE_NAMESPACE: &str = "platform_storage"; +pub const HOST_STORAGE_GET: &str = "storage_get"; +pub const HOST_STORAGE_SET: &str = "storage_set"; +pub const HOST_STORAGE_PROPOSE_WRITE: &str = "storage_propose_write"; +pub const HOST_STORAGE_DELETE: &str = "storage_delete"; +pub const HOST_STORAGE_GET_RESULT: &str = "storage_get_result"; +pub const HOST_STORAGE_ALLOC: &str = "storage_alloc"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum StorageHostStatus { + Success = 0, + NotFound = 1, + KeyTooLarge = -1, + ValueTooLarge = -2, + InvalidKey = -3, + InvalidValue = -4, + StorageError = -5, + ConsensusRequired = -6, + PermissionDenied = -7, + QuotaExceeded = -8, + InternalError = -100, +} + +impl StorageHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } + + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::NotFound, + -1 => Self::KeyTooLarge, + -2 => Self::ValueTooLarge, + -3 => Self::InvalidKey, + -4 => Self::InvalidValue, + -5 => Self::StorageError, + -6 => Self::ConsensusRequired, + -7 => Self::PermissionDenied, + -8 => Self::QuotaExceeded, + _ => Self::InternalError, + } + } +} + +#[derive(Debug, Error)] +pub enum StorageHostError { + #[error("key too large: {0} bytes (max {1})")] + KeyTooLarge(usize, usize), + + #[error("value too large: {0} bytes (max {1})")] + ValueTooLarge(usize, usize), + + #[error("invalid key: {0}")] + InvalidKey(String), + + #[error("invalid value: {0}")] + InvalidValue(String), + + #[error("storage error: {0}")] + StorageError(String), + + #[error("consensus required for write")] + ConsensusRequired, + + #[error("permission denied: {0}")] + PermissionDenied(String), + + #[error("quota exceeded: {0}")] + QuotaExceeded(String), + + #[error("memory error: {0}")] + MemoryError(String), + + #[error("internal error: {0}")] + InternalError(String), +} + +impl From for StorageHostStatus { + fn from(err: StorageHostError) -> Self { + match err { + StorageHostError::KeyTooLarge(_, _) => Self::KeyTooLarge, + StorageHostError::ValueTooLarge(_, _) => Self::ValueTooLarge, + StorageHostError::InvalidKey(_) => Self::InvalidKey, + StorageHostError::InvalidValue(_) => Self::InvalidValue, + StorageHostError::StorageError(_) => Self::StorageError, + StorageHostError::ConsensusRequired => Self::ConsensusRequired, + StorageHostError::PermissionDenied(_) => Self::PermissionDenied, + StorageHostError::QuotaExceeded(_) => Self::QuotaExceeded, + StorageHostError::MemoryError(_) => Self::InternalError, + StorageHostError::InternalError(_) => Self::InternalError, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageHostConfig { + pub max_key_size: usize, + pub max_value_size: usize, + pub max_total_storage: usize, + pub max_keys_per_challenge: usize, + pub allow_direct_writes: bool, + pub require_consensus: bool, +} + +impl Default for StorageHostConfig { + fn default() -> Self { + Self { + max_key_size: 1024, + max_value_size: 1024 * 1024, + max_total_storage: 100 * 1024 * 1024, + max_keys_per_challenge: 10_000, + allow_direct_writes: false, + require_consensus: true, + } + } +} + +impl StorageHostConfig { + pub fn permissive() -> Self { + Self { + max_key_size: 4096, + max_value_size: 10 * 1024 * 1024, + max_total_storage: 1024 * 1024 * 1024, + max_keys_per_challenge: 100_000, + allow_direct_writes: true, + require_consensus: false, + } + } + + pub fn validate_key(&self, key: &[u8]) -> Result<(), StorageHostError> { + if key.is_empty() { + return Err(StorageHostError::InvalidKey( + "key cannot be empty".to_string(), + )); + } + if key.len() > self.max_key_size { + return Err(StorageHostError::KeyTooLarge(key.len(), self.max_key_size)); + } + Ok(()) + } + + pub fn validate_value(&self, value: &[u8]) -> Result<(), StorageHostError> { + if value.len() > self.max_value_size { + return Err(StorageHostError::ValueTooLarge( + value.len(), + self.max_value_size, + )); + } + Ok(()) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageGetRequest { + pub challenge_id: String, + pub key: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageGetResponse { + pub found: bool, + pub value: Option>, + pub version: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageProposeWriteRequest { + pub challenge_id: String, + pub key: Vec, + pub value: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageProposeWriteResponse { + pub proposal_id: [u8; 32], + pub status: i32, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageDeleteRequest { + pub challenge_id: String, + pub key: Vec, +} + +pub struct StorageHostState { + pub config: StorageHostConfig, + pub challenge_id: String, + pub backend: Arc, + pub pending_results: HashMap>, + pub next_result_id: u32, + pub bytes_read: u64, + pub bytes_written: u64, + pub operations_count: u32, +} + +impl StorageHostState { + pub fn new( + challenge_id: String, + config: StorageHostConfig, + backend: Arc, + ) -> Self { + Self { + config, + challenge_id, + backend, + pending_results: HashMap::new(), + next_result_id: 1, + bytes_read: 0, + bytes_written: 0, + operations_count: 0, + } + } + + pub fn store_result(&mut self, data: Vec) -> u32 { + let id = self.next_result_id; + self.next_result_id = self.next_result_id.wrapping_add(1); + self.pending_results.insert(id, data); + id + } + + pub fn take_result(&mut self, id: u32) -> Option> { + self.pending_results.remove(&id) + } + + pub fn reset_counters(&mut self) { + self.bytes_read = 0; + self.bytes_written = 0; + self.operations_count = 0; + } +} + +pub fn pack_result(status: StorageHostStatus, value: u32) -> i64 { + let status_bits = (status.to_i32() as i64) << 32; + let value_bits = value as i64; + status_bits | value_bits +} + +pub fn unpack_result(packed: i64) -> (StorageHostStatus, u32) { + let status = StorageHostStatus::from_i32((packed >> 32) as i32); + let value = (packed & 0xFFFFFFFF) as u32; + (status, value) +} + +pub trait StorageBackend: Send + Sync { + fn get(&self, challenge_id: &str, key: &[u8]) -> Result>, StorageHostError>; + + fn propose_write( + &self, + challenge_id: &str, + key: &[u8], + value: &[u8], + ) -> Result<[u8; 32], StorageHostError>; + + fn delete(&self, challenge_id: &str, key: &[u8]) -> Result; +} + +pub struct NoopStorageBackend; + +impl StorageBackend for NoopStorageBackend { + fn get(&self, _challenge_id: &str, _key: &[u8]) -> Result>, StorageHostError> { + Ok(None) + } + + fn propose_write( + &self, + _challenge_id: &str, + _key: &[u8], + _value: &[u8], + ) -> Result<[u8; 32], StorageHostError> { + Err(StorageHostError::ConsensusRequired) + } + + fn delete(&self, _challenge_id: &str, _key: &[u8]) -> Result { + Ok(false) + } +} + +type StorageMap = HashMap, Vec>>; + +pub struct InMemoryStorageBackend { + data: RwLock, +} + +impl InMemoryStorageBackend { + pub fn new() -> Self { + Self { + data: RwLock::new(HashMap::new()), + } + } +} + +impl Default for InMemoryStorageBackend { + fn default() -> Self { + Self::new() + } +} + +impl StorageBackend for InMemoryStorageBackend { + fn get(&self, challenge_id: &str, key: &[u8]) -> Result>, StorageHostError> { + let data = self + .data + .read() + .map_err(|e| StorageHostError::InternalError(format!("lock poisoned: {}", e)))?; + Ok(data + .get(challenge_id) + .and_then(|challenge_data: &HashMap, Vec>| { + challenge_data.get(key).cloned() + })) + } + + fn propose_write( + &self, + challenge_id: &str, + key: &[u8], + value: &[u8], + ) -> Result<[u8; 32], StorageHostError> { + let mut data = self + .data + .write() + .map_err(|e| StorageHostError::InternalError(format!("lock poisoned: {}", e)))?; + let challenge_data: &mut HashMap, Vec> = + data.entry(challenge_id.to_string()).or_default(); + challenge_data.insert(key.to_vec(), value.to_vec()); + + let mut hasher = Sha256::new(); + hasher.update(challenge_id.as_bytes()); + hasher.update(key); + hasher.update(value); + Ok(hasher.finalize().into()) + } + + fn delete(&self, challenge_id: &str, key: &[u8]) -> Result { + let mut data = self + .data + .write() + .map_err(|e| StorageHostError::InternalError(format!("lock poisoned: {}", e)))?; + if let Some(challenge_data) = data.get_mut(challenge_id) { + Ok(challenge_data.remove(key).is_some()) + } else { + Ok(false) + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StorageAuditEntry { + pub timestamp: chrono::DateTime, + pub challenge_id: String, + pub validator_id: String, + pub operation: StorageOperation, + pub key_hash: [u8; 32], + pub value_size: Option, + pub status: i32, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum StorageOperation { + Get, + Set, + ProposeWrite, + Delete, +} + +pub trait StorageAuditLogger: Send + Sync { + fn record(&self, entry: StorageAuditEntry); +} + +pub struct NoopStorageAuditLogger; + +impl StorageAuditLogger for NoopStorageAuditLogger { + fn record(&self, _entry: StorageAuditEntry) {} +} + +#[derive(Clone, Debug)] +pub struct StorageHostFunctions; + +impl StorageHostFunctions { + pub fn new() -> Self { + Self + } +} + +impl Default for StorageHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for StorageHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_STORAGE_NAMESPACE, + HOST_STORAGE_GET, + |mut caller: Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32| + -> i32 { + handle_storage_get(&mut caller, key_ptr, key_len, value_ptr) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_STORAGE_NAMESPACE, + HOST_STORAGE_SET, + |mut caller: Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32, + value_len: i32| + -> i32 { + handle_storage_set(&mut caller, key_ptr, key_len, value_ptr, value_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_STORAGE_NAMESPACE, + HOST_STORAGE_DELETE, + |mut caller: Caller, key_ptr: i32, key_len: i32| -> i32 { + handle_storage_delete(&mut caller, key_ptr, key_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_STORAGE_NAMESPACE, + HOST_STORAGE_PROPOSE_WRITE, + |mut caller: Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32, + value_len: i32| + -> i64 { + handle_storage_propose_write( + &mut caller, + key_ptr, + key_len, + value_ptr, + value_len, + ) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +fn handle_storage_get( + caller: &mut Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32, +) -> i32 { + let key = match read_wasm_memory(caller, key_ptr, key_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_get: failed to read key from wasm memory"); + return StorageHostStatus::InternalError.to_i32(); + } + }; + + let storage = &caller.data().storage_state; + if let Err(err) = storage.config.validate_key(&key) { + warn!(error = %err, "storage_get: key validation failed"); + return StorageHostStatus::from(err).to_i32(); + } + + let challenge_id = storage.challenge_id.clone(); + let backend = Arc::clone(&storage.backend); + + let value = match backend.get(&challenge_id, &key) { + Ok(Some(v)) => v, + Ok(None) => return 0, + Err(err) => { + warn!(error = %err, "storage_get: backend read failed"); + return StorageHostStatus::from(err).to_i32(); + } + }; + + caller.data_mut().storage_state.bytes_read += value.len() as u64; + caller.data_mut().storage_state.operations_count += 1; + + if let Err(err) = write_wasm_memory(caller, value_ptr, &value) { + warn!(error = %err, "storage_get: failed to write value to wasm memory"); + return StorageHostStatus::InternalError.to_i32(); + } + + value.len() as i32 +} + +fn handle_storage_set( + caller: &mut Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32, + value_len: i32, +) -> i32 { + let key = match read_wasm_memory(caller, key_ptr, key_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_set: failed to read key from wasm memory"); + return StorageHostStatus::InternalError.to_i32(); + } + }; + + let value = match read_wasm_memory(caller, value_ptr, value_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_set: failed to read value from wasm memory"); + return StorageHostStatus::InternalError.to_i32(); + } + }; + + let storage = &caller.data().storage_state; + if let Err(err) = storage.config.validate_key(&key) { + warn!(error = %err, "storage_set: key validation failed"); + return StorageHostStatus::from(err).to_i32(); + } + if let Err(err) = storage.config.validate_value(&value) { + warn!(error = %err, "storage_set: value validation failed"); + return StorageHostStatus::from(err).to_i32(); + } + + if storage.config.require_consensus && !storage.config.allow_direct_writes { + warn!("storage_set: direct writes require consensus or allow_direct_writes"); + return StorageHostStatus::ConsensusRequired.to_i32(); + } + + let challenge_id = storage.challenge_id.clone(); + let backend = Arc::clone(&storage.backend); + + match backend.propose_write(&challenge_id, &key, &value) { + Ok(_proposal_id) => { + caller.data_mut().storage_state.bytes_written += value.len() as u64; + caller.data_mut().storage_state.operations_count += 1; + StorageHostStatus::Success.to_i32() + } + Err(err) => { + warn!(error = %err, "storage_set: backend write failed"); + StorageHostStatus::from(err).to_i32() + } + } +} + +fn handle_storage_delete(caller: &mut Caller, key_ptr: i32, key_len: i32) -> i32 { + let key = match read_wasm_memory(caller, key_ptr, key_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_delete: failed to read key from wasm memory"); + return StorageHostStatus::InternalError.to_i32(); + } + }; + + let storage = &caller.data().storage_state; + if let Err(err) = storage.config.validate_key(&key) { + warn!(error = %err, "storage_delete: key validation failed"); + return StorageHostStatus::from(err).to_i32(); + } + + if storage.config.require_consensus && !storage.config.allow_direct_writes { + warn!("storage_delete: direct deletes require consensus or allow_direct_writes"); + return StorageHostStatus::ConsensusRequired.to_i32(); + } + + let challenge_id = storage.challenge_id.clone(); + let backend = Arc::clone(&storage.backend); + + match backend.delete(&challenge_id, &key) { + Ok(_deleted) => { + caller.data_mut().storage_state.operations_count += 1; + StorageHostStatus::Success.to_i32() + } + Err(err) => { + warn!(error = %err, "storage_delete: backend delete failed"); + StorageHostStatus::from(err).to_i32() + } + } +} + +fn handle_storage_propose_write( + caller: &mut Caller, + key_ptr: i32, + key_len: i32, + value_ptr: i32, + value_len: i32, +) -> i64 { + let key = match read_wasm_memory(caller, key_ptr, key_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_propose_write: failed to read key from wasm memory"); + return pack_result(StorageHostStatus::InternalError, 0); + } + }; + + let value = match read_wasm_memory(caller, value_ptr, value_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "storage_propose_write: failed to read value from wasm memory"); + return pack_result(StorageHostStatus::InternalError, 0); + } + }; + + let storage = &caller.data().storage_state; + if let Err(err) = storage.config.validate_key(&key) { + warn!(error = %err, "storage_propose_write: key validation failed"); + return pack_result(StorageHostStatus::from(err), 0); + } + if let Err(err) = storage.config.validate_value(&value) { + warn!(error = %err, "storage_propose_write: value validation failed"); + return pack_result(StorageHostStatus::from(err), 0); + } + + let challenge_id = storage.challenge_id.clone(); + let backend = Arc::clone(&storage.backend); + + match backend.propose_write(&challenge_id, &key, &value) { + Ok(proposal_id) => { + caller.data_mut().storage_state.bytes_written += value.len() as u64; + caller.data_mut().storage_state.operations_count += 1; + let result_id = caller + .data_mut() + .storage_state + .store_result(proposal_id.to_vec()); + pack_result(StorageHostStatus::Success, result_id) + } + Err(err) => { + warn!(error = %err, "storage_propose_write: backend write failed"); + pack_result(StorageHostStatus::from(err), 0) + } + } +} + +fn read_wasm_memory( + caller: &mut Caller, + ptr: i32, + len: i32, +) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_wasm_memory( + caller: &mut Caller, + ptr: i32, + bytes: &[u8], +) -> Result<(), String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(bytes.len()) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data_mut(caller); + if end > data.len() { + return Err("memory write out of bounds".to_string()); + } + data[ptr..end].copy_from_slice(bytes); + Ok(()) +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pack_unpack_result() { + let status = StorageHostStatus::Success; + let value = 12345u32; + let packed = pack_result(status, value); + let (unpacked_status, unpacked_value) = unpack_result(packed); + assert_eq!(unpacked_status, status); + assert_eq!(unpacked_value, value); + } + + #[test] + fn test_pack_unpack_error() { + let status = StorageHostStatus::KeyTooLarge; + let value = 0u32; + let packed = pack_result(status, value); + let (unpacked_status, unpacked_value) = unpack_result(packed); + assert_eq!(unpacked_status, status); + assert_eq!(unpacked_value, value); + } + + #[test] + fn test_storage_host_config_validate_key() { + let config = StorageHostConfig::default(); + + assert!(config.validate_key(b"valid-key").is_ok()); + assert!(config.validate_key(b"").is_err()); + + let large_key = vec![0u8; 2000]; + assert!(config.validate_key(&large_key).is_err()); + } + + #[test] + fn test_storage_host_config_validate_value() { + let config = StorageHostConfig::default(); + + assert!(config.validate_value(b"valid-value").is_ok()); + assert!(config.validate_value(b"").is_ok()); + + let large_value = vec![0u8; 2 * 1024 * 1024]; + assert!(config.validate_value(&large_value).is_err()); + } + + #[test] + fn test_storage_host_state() { + let backend = Arc::new(InMemoryStorageBackend::new()); + let mut state = StorageHostState::new( + "challenge-1".to_string(), + StorageHostConfig::default(), + backend, + ); + + let id1 = state.store_result(b"result1".to_vec()); + let id2 = state.store_result(b"result2".to_vec()); + + assert_ne!(id1, id2); + + let result1 = state.take_result(id1); + assert_eq!(result1, Some(b"result1".to_vec())); + + let result1_again = state.take_result(id1); + assert_eq!(result1_again, None); + + let result2 = state.take_result(id2); + assert_eq!(result2, Some(b"result2".to_vec())); + } + + #[test] + fn test_in_memory_storage_backend() { + let backend = InMemoryStorageBackend::new(); + + let result = backend.get("challenge-1", b"key1").unwrap(); + assert!(result.is_none()); + + let proposal_id = backend + .propose_write("challenge-1", b"key1", b"value1") + .unwrap(); + assert_ne!(proposal_id, [0u8; 32]); + + let result = backend.get("challenge-1", b"key1").unwrap(); + assert_eq!(result, Some(b"value1".to_vec())); + + let deleted = backend.delete("challenge-1", b"key1").unwrap(); + assert!(deleted); + + let result = backend.get("challenge-1", b"key1").unwrap(); + assert!(result.is_none()); + } + + #[test] + fn test_noop_storage_backend() { + let backend = NoopStorageBackend; + + let result = backend.get("challenge-1", b"key1").unwrap(); + assert!(result.is_none()); + + let result = backend.propose_write("challenge-1", b"key1", b"value1"); + assert!(matches!(result, Err(StorageHostError::ConsensusRequired))); + + let deleted = backend.delete("challenge-1", b"key1").unwrap(); + assert!(!deleted); + } + + #[test] + fn test_storage_host_status_conversion() { + assert_eq!(StorageHostStatus::Success.to_i32(), 0); + assert_eq!(StorageHostStatus::NotFound.to_i32(), 1); + assert_eq!(StorageHostStatus::KeyTooLarge.to_i32(), -1); + assert_eq!(StorageHostStatus::InternalError.to_i32(), -100); + + assert_eq!(StorageHostStatus::from_i32(0), StorageHostStatus::Success); + assert_eq!(StorageHostStatus::from_i32(1), StorageHostStatus::NotFound); + assert_eq!( + StorageHostStatus::from_i32(-1), + StorageHostStatus::KeyTooLarge + ); + assert_eq!( + StorageHostStatus::from_i32(-999), + StorageHostStatus::InternalError + ); + } + + #[test] + fn test_storage_host_error_to_status() { + let err = StorageHostError::KeyTooLarge(2000, 1024); + assert_eq!(StorageHostStatus::from(err), StorageHostStatus::KeyTooLarge); + + let err = StorageHostError::ValueTooLarge(10_000_000, 1_000_000); + assert_eq!( + StorageHostStatus::from(err), + StorageHostStatus::ValueTooLarge + ); + + let err = StorageHostError::ConsensusRequired; + assert_eq!( + StorageHostStatus::from(err), + StorageHostStatus::ConsensusRequired + ); + } + + #[test] + fn test_permissive_config() { + let config = StorageHostConfig::permissive(); + assert!(config.allow_direct_writes); + assert!(!config.require_consensus); + assert!(config.max_value_size > StorageHostConfig::default().max_value_size); + } +} diff --git a/crates/wasm-runtime-interface/src/terminal.rs b/crates/wasm-runtime-interface/src/terminal.rs new file mode 100644 index 000000000..88775bd12 --- /dev/null +++ b/crates/wasm-runtime-interface/src/terminal.rs @@ -0,0 +1,762 @@ +//! Terminal Host Functions for WASM Challenges +//! +//! This module provides host functions that allow WASM code to interact with +//! the host terminal environment. All operations are gated by `TerminalPolicy`. +//! +//! # Host Functions +//! +//! - `terminal_exec(cmd_ptr, cmd_len, result_ptr, result_len) -> i32` +//! - `terminal_read_file(path_ptr, path_len, buf_ptr, buf_len) -> i32` +//! - `terminal_write_file(path_ptr, path_len, data_ptr, data_len) -> i32` +//! - `terminal_list_dir(path_ptr, path_len, buf_ptr, buf_len) -> i32` +//! - `terminal_get_time() -> i64` +//! - `terminal_random_seed(buf_ptr, buf_len) -> i32` + +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use std::process::Command; +use std::time::Duration; +use tracing::warn; +use wasmtime::{Caller, Linker, Memory}; + +pub const HOST_TERMINAL_NAMESPACE: &str = "platform_terminal"; +pub const HOST_TERMINAL_EXEC: &str = "terminal_exec"; +pub const HOST_TERMINAL_READ_FILE: &str = "terminal_read_file"; +pub const HOST_TERMINAL_WRITE_FILE: &str = "terminal_write_file"; +pub const HOST_TERMINAL_LIST_DIR: &str = "terminal_list_dir"; +pub const HOST_TERMINAL_GET_TIME: &str = "terminal_get_time"; +pub const HOST_TERMINAL_RANDOM_SEED: &str = "terminal_random_seed"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum TerminalHostStatus { + Success = 0, + Disabled = 1, + CommandNotAllowed = -1, + PathNotAllowed = -2, + FileTooLarge = -3, + BufferTooSmall = -4, + IoError = -5, + LimitExceeded = -6, + Timeout = -7, + InternalError = -100, +} + +impl TerminalHostStatus { + pub fn to_i32(self) -> i32 { + self as i32 + } + + pub fn from_i32(code: i32) -> Self { + match code { + 0 => Self::Success, + 1 => Self::Disabled, + -1 => Self::CommandNotAllowed, + -2 => Self::PathNotAllowed, + -3 => Self::FileTooLarge, + -4 => Self::BufferTooSmall, + -5 => Self::IoError, + -6 => Self::LimitExceeded, + -7 => Self::Timeout, + _ => Self::InternalError, + } + } +} + +/// Policy controlling WASM access to terminal operations. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TerminalPolicy { + pub enabled: bool, + pub allowed_commands: Vec, + pub allowed_paths: Vec, + pub max_file_size: usize, + pub max_executions: u32, + pub max_output_bytes: usize, + pub timeout_ms: u64, +} + +impl Default for TerminalPolicy { + fn default() -> Self { + Self { + enabled: false, + allowed_commands: Vec::new(), + allowed_paths: Vec::new(), + max_file_size: 1024 * 1024, + max_executions: 0, + max_output_bytes: 512 * 1024, + timeout_ms: 5_000, + } + } +} + +impl TerminalPolicy { + pub fn development() -> Self { + Self { + enabled: true, + allowed_commands: vec![ + "bash".to_string(), + "sh".to_string(), + "echo".to_string(), + "cat".to_string(), + "ls".to_string(), + "python3".to_string(), + "node".to_string(), + ], + allowed_paths: vec!["/tmp".to_string(), "/workspace".to_string()], + max_file_size: 10 * 1024 * 1024, + max_executions: 64, + max_output_bytes: 2 * 1024 * 1024, + timeout_ms: 30_000, + } + } + + pub fn default_challenge() -> Self { + Self { + enabled: true, + allowed_commands: vec![ + "bash".to_string(), + "sh".to_string(), + "python3".to_string(), + "node".to_string(), + ], + allowed_paths: vec!["/tmp".to_string()], + max_file_size: 1024 * 1024, + max_executions: 32, + max_output_bytes: 1024 * 1024, + timeout_ms: 60_000, + } + } + + pub fn is_command_allowed(&self, command: &str) -> bool { + if !self.enabled { + return false; + } + self.allowed_commands + .iter() + .any(|c| c == "*" || c == command) + } + + pub fn is_path_allowed(&self, path: &str) -> bool { + if !self.enabled { + return false; + } + if path.contains("..") { + return false; + } + let normalized = std::path::Path::new(path).components().fold( + std::path::PathBuf::new(), + |mut acc, comp| { + match comp { + std::path::Component::ParentDir => { + acc.pop(); + } + std::path::Component::Normal(s) => acc.push(s), + std::path::Component::RootDir => acc.push("/"), + _ => {} + } + acc + }, + ); + let normalized_str = normalized.to_string_lossy(); + if self.allowed_paths.is_empty() { + return true; + } + self.allowed_paths + .iter() + .any(|p| normalized_str.starts_with(p)) + } +} + +/// Mutable terminal state for tracking per-instance usage. +pub struct TerminalState { + pub policy: TerminalPolicy, + pub challenge_id: String, + pub validator_id: String, + pub executions: u32, + pub bytes_read: u64, + pub bytes_written: u64, +} + +impl TerminalState { + pub fn new(policy: TerminalPolicy, challenge_id: String, validator_id: String) -> Self { + Self { + policy, + challenge_id, + validator_id, + executions: 0, + bytes_read: 0, + bytes_written: 0, + } + } + + pub fn reset_counters(&mut self) { + self.executions = 0; + self.bytes_read = 0; + self.bytes_written = 0; + } +} + +#[derive(Clone, Debug)] +pub struct TerminalHostFunctions; + +impl TerminalHostFunctions { + pub fn new() -> Self { + Self + } +} + +impl Default for TerminalHostFunctions { + fn default() -> Self { + Self::new() + } +} + +impl HostFunctionRegistrar for TerminalHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_EXEC, + |mut caller: Caller, + cmd_ptr: i32, + cmd_len: i32, + result_ptr: i32, + result_len: i32| + -> i32 { + handle_terminal_exec(&mut caller, cmd_ptr, cmd_len, result_ptr, result_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_READ_FILE, + |mut caller: Caller, + path_ptr: i32, + path_len: i32, + buf_ptr: i32, + buf_len: i32| + -> i32 { + handle_terminal_read_file(&mut caller, path_ptr, path_len, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_WRITE_FILE, + |mut caller: Caller, + path_ptr: i32, + path_len: i32, + data_ptr: i32, + data_len: i32| + -> i32 { + handle_terminal_write_file(&mut caller, path_ptr, path_len, data_ptr, data_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_LIST_DIR, + |mut caller: Caller, + path_ptr: i32, + path_len: i32, + buf_ptr: i32, + buf_len: i32| + -> i32 { + handle_terminal_list_dir(&mut caller, path_ptr, path_len, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_GET_TIME, + |caller: Caller| -> i64 { + if let Some(ts) = caller.data().fixed_timestamp_ms { + return ts; + } + chrono::Utc::now().timestamp_millis() + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + linker + .func_wrap( + HOST_TERMINAL_NAMESPACE, + HOST_TERMINAL_RANDOM_SEED, + |mut caller: Caller, buf_ptr: i32, buf_len: i32| -> i32 { + handle_terminal_random_seed(&mut caller, buf_ptr, buf_len) + }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + + Ok(()) + } +} + +fn handle_terminal_exec( + caller: &mut Caller, + cmd_ptr: i32, + cmd_len: i32, + result_ptr: i32, + result_len: i32, +) -> i32 { + let enabled = caller.data().terminal_state.policy.enabled; + if !enabled { + return TerminalHostStatus::Disabled.to_i32(); + } + + let cmd_bytes = match read_wasm_memory(caller, cmd_ptr, cmd_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "terminal_exec: failed to read command from wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + }; + + let cmd_str = match std::str::from_utf8(&cmd_bytes) { + Ok(s) => s.to_string(), + Err(_) => return TerminalHostStatus::InternalError.to_i32(), + }; + + let command_name = cmd_str.split_whitespace().next().unwrap_or("").to_string(); + + { + let state = &caller.data().terminal_state; + if !state.policy.is_command_allowed(&command_name) { + warn!( + challenge_id = %state.challenge_id, + command = %command_name, + "terminal_exec: command not allowed" + ); + return TerminalHostStatus::CommandNotAllowed.to_i32(); + } + if state.executions >= state.policy.max_executions { + return TerminalHostStatus::LimitExceeded.to_i32(); + } + } + + let timeout_ms = caller.data().terminal_state.policy.timeout_ms; + let max_output = caller.data().terminal_state.policy.max_output_bytes; + + let output = match Command::new("sh") + .arg("-c") + .arg(&cmd_str) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + { + Ok(child) => { + let start = std::time::Instant::now(); + let timeout = Duration::from_millis(timeout_ms); + match child.wait_with_output() { + Ok(out) => { + if start.elapsed() > timeout { + return TerminalHostStatus::Timeout.to_i32(); + } + out + } + Err(err) => { + warn!(error = %err, "terminal_exec: command wait failed"); + return TerminalHostStatus::IoError.to_i32(); + } + } + } + Err(err) => { + warn!(error = %err, "terminal_exec: command spawn failed"); + return TerminalHostStatus::IoError.to_i32(); + } + }; + + caller.data_mut().terminal_state.executions += 1; + + let mut result_data = output.stdout; + if result_data.len() > max_output { + result_data.truncate(max_output); + } + + if result_len < 0 || result_data.len() > result_len as usize { + return TerminalHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, result_ptr, &result_data) { + warn!(error = %err, "terminal_exec: failed to write result to wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + + result_data.len() as i32 +} + +fn handle_terminal_read_file( + caller: &mut Caller, + path_ptr: i32, + path_len: i32, + buf_ptr: i32, + buf_len: i32, +) -> i32 { + let enabled = caller.data().terminal_state.policy.enabled; + if !enabled { + return TerminalHostStatus::Disabled.to_i32(); + } + + let path_bytes = match read_wasm_memory(caller, path_ptr, path_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "terminal_read_file: failed to read path from wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + }; + + let path_str = match std::str::from_utf8(&path_bytes) { + Ok(s) => s.to_string(), + Err(_) => return TerminalHostStatus::InternalError.to_i32(), + }; + + if !caller + .data() + .terminal_state + .policy + .is_path_allowed(&path_str) + { + return TerminalHostStatus::PathNotAllowed.to_i32(); + } + + let max_file_size = caller.data().terminal_state.policy.max_file_size; + + let contents = match std::fs::read(&path_str) { + Ok(data) => data, + Err(err) => { + warn!(error = %err, path = %path_str, "terminal_read_file: read failed"); + return TerminalHostStatus::IoError.to_i32(); + } + }; + + if contents.len() > max_file_size { + return TerminalHostStatus::FileTooLarge.to_i32(); + } + + if buf_len < 0 || contents.len() > buf_len as usize { + return TerminalHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &contents) { + warn!(error = %err, "terminal_read_file: failed to write to wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + + caller.data_mut().terminal_state.bytes_read += contents.len() as u64; + + contents.len() as i32 +} + +fn handle_terminal_write_file( + caller: &mut Caller, + path_ptr: i32, + path_len: i32, + data_ptr: i32, + data_len: i32, +) -> i32 { + let enabled = caller.data().terminal_state.policy.enabled; + if !enabled { + return TerminalHostStatus::Disabled.to_i32(); + } + + let path_bytes = match read_wasm_memory(caller, path_ptr, path_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "terminal_write_file: failed to read path from wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + }; + + let path_str = match std::str::from_utf8(&path_bytes) { + Ok(s) => s.to_string(), + Err(_) => return TerminalHostStatus::InternalError.to_i32(), + }; + + if !caller + .data() + .terminal_state + .policy + .is_path_allowed(&path_str) + { + return TerminalHostStatus::PathNotAllowed.to_i32(); + } + + let data = match read_wasm_memory(caller, data_ptr, data_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "terminal_write_file: failed to read data from wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + }; + + let max_file_size = caller.data().terminal_state.policy.max_file_size; + if data.len() > max_file_size { + return TerminalHostStatus::FileTooLarge.to_i32(); + } + + if let Err(err) = std::fs::write(&path_str, &data) { + warn!(error = %err, path = %path_str, "terminal_write_file: write failed"); + return TerminalHostStatus::IoError.to_i32(); + } + + caller.data_mut().terminal_state.bytes_written += data.len() as u64; + + TerminalHostStatus::Success.to_i32() +} + +fn handle_terminal_list_dir( + caller: &mut Caller, + path_ptr: i32, + path_len: i32, + buf_ptr: i32, + buf_len: i32, +) -> i32 { + let enabled = caller.data().terminal_state.policy.enabled; + if !enabled { + return TerminalHostStatus::Disabled.to_i32(); + } + + let path_bytes = match read_wasm_memory(caller, path_ptr, path_len) { + Ok(bytes) => bytes, + Err(err) => { + warn!(error = %err, "terminal_list_dir: failed to read path from wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + }; + + let path_str = match std::str::from_utf8(&path_bytes) { + Ok(s) => s.to_string(), + Err(_) => return TerminalHostStatus::InternalError.to_i32(), + }; + + if !caller + .data() + .terminal_state + .policy + .is_path_allowed(&path_str) + { + return TerminalHostStatus::PathNotAllowed.to_i32(); + } + + let entries = match std::fs::read_dir(&path_str) { + Ok(rd) => rd, + Err(err) => { + warn!(error = %err, path = %path_str, "terminal_list_dir: read_dir failed"); + return TerminalHostStatus::IoError.to_i32(); + } + }; + + let mut names = Vec::new(); + for entry in entries { + match entry { + Ok(e) => { + if let Some(name) = e.file_name().to_str() { + names.push(name.to_string()); + } + } + Err(_) => continue, + } + } + + let result = names.join("\n"); + let result_bytes = result.as_bytes(); + + if buf_len < 0 || result_bytes.len() > buf_len as usize { + return TerminalHostStatus::BufferTooSmall.to_i32(); + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, result_bytes) { + warn!(error = %err, "terminal_list_dir: failed to write to wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + + result_bytes.len() as i32 +} + +fn handle_terminal_random_seed( + caller: &mut Caller, + buf_ptr: i32, + buf_len: i32, +) -> i32 { + if buf_len <= 0 { + return TerminalHostStatus::InternalError.to_i32(); + } + + let len = buf_len as usize; + let mut seed = vec![0u8; len]; + + // Use a deterministic seed based on challenge_id and timestamp for reproducibility + let challenge_id = caller.data().challenge_id.clone(); + let ts = caller + .data() + .fixed_timestamp_ms + .unwrap_or_else(|| chrono::Utc::now().timestamp_millis()); + + use sha2::{Digest, Sha256}; + let mut hasher = Sha256::new(); + hasher.update(challenge_id.as_bytes()); + hasher.update(ts.to_le_bytes()); + let hash = hasher.finalize(); + + for (i, byte) in seed.iter_mut().enumerate() { + *byte = hash[i % 32]; + } + + if let Err(err) = write_wasm_memory(caller, buf_ptr, &seed) { + warn!(error = %err, "terminal_random_seed: failed to write to wasm memory"); + return TerminalHostStatus::InternalError.to_i32(); + } + + TerminalHostStatus::Success.to_i32() +} + +fn read_wasm_memory( + caller: &mut Caller, + ptr: i32, + len: i32, +) -> Result, String> { + if ptr < 0 || len < 0 { + return Err("negative pointer/length".to_string()); + } + let ptr = ptr as usize; + let len = len as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let data = memory.data(caller); + let end = ptr + .checked_add(len) + .ok_or_else(|| "pointer overflow".to_string())?; + if end > data.len() { + return Err("memory read out of bounds".to_string()); + } + Ok(data[ptr..end].to_vec()) +} + +fn write_wasm_memory( + caller: &mut Caller, + ptr: i32, + bytes: &[u8], +) -> Result<(), String> { + if ptr < 0 { + return Err("negative pointer".to_string()); + } + let ptr = ptr as usize; + let memory = get_memory(caller).ok_or_else(|| "memory export not found".to_string())?; + let end = ptr + .checked_add(bytes.len()) + .ok_or_else(|| "pointer overflow".to_string())?; + let data = memory.data_mut(caller); + if end > data.len() { + return Err("memory write out of bounds".to_string()); + } + data[ptr..end].copy_from_slice(bytes); + Ok(()) +} + +fn get_memory(caller: &mut Caller) -> Option { + let memory_export = caller.data().memory_export.clone(); + caller + .get_export(&memory_export) + .and_then(|export| export.into_memory()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_terminal_host_status_conversion() { + assert_eq!(TerminalHostStatus::Success.to_i32(), 0); + assert_eq!(TerminalHostStatus::Disabled.to_i32(), 1); + assert_eq!(TerminalHostStatus::CommandNotAllowed.to_i32(), -1); + assert_eq!(TerminalHostStatus::InternalError.to_i32(), -100); + + assert_eq!(TerminalHostStatus::from_i32(0), TerminalHostStatus::Success); + assert_eq!( + TerminalHostStatus::from_i32(1), + TerminalHostStatus::Disabled + ); + assert_eq!( + TerminalHostStatus::from_i32(-999), + TerminalHostStatus::InternalError + ); + } + + #[test] + fn test_terminal_policy_default() { + let policy = TerminalPolicy::default(); + assert!(!policy.enabled); + assert!(policy.allowed_commands.is_empty()); + assert_eq!(policy.max_executions, 0); + } + + #[test] + fn test_terminal_policy_development() { + let policy = TerminalPolicy::development(); + assert!(policy.enabled); + assert!(policy.is_command_allowed("bash")); + assert!(policy.is_command_allowed("python3")); + assert!(!policy.is_command_allowed("rm")); + } + + #[test] + fn test_terminal_policy_path_check() { + let policy = TerminalPolicy::default_challenge(); + assert!(policy.is_path_allowed("/tmp/test.txt")); + assert!(!policy.is_path_allowed("/etc/passwd")); + } + + #[test] + fn test_terminal_policy_blocks_path_traversal() { + let policy = TerminalPolicy::default_challenge(); + assert!(!policy.is_path_allowed("/tmp/../../etc/passwd")); + assert!(!policy.is_path_allowed("/tmp/../etc/shadow")); + assert!(!policy.is_path_allowed("/tmp/safe/../../root/.ssh/id_rsa")); + assert!(!policy.is_path_allowed("/tmp/..")); + } + + #[test] + fn test_terminal_policy_disabled_blocks_all() { + let policy = TerminalPolicy::default(); + assert!(!policy.is_command_allowed("bash")); + assert!(!policy.is_path_allowed("/tmp")); + } + + #[test] + fn test_terminal_state_creation() { + let state = TerminalState::new( + TerminalPolicy::default(), + "test".to_string(), + "test".to_string(), + ); + assert_eq!(state.executions, 0); + assert_eq!(state.bytes_read, 0); + assert_eq!(state.bytes_written, 0); + } + + #[test] + fn test_terminal_state_reset() { + let mut state = TerminalState::new( + TerminalPolicy::default(), + "test".to_string(), + "test".to_string(), + ); + state.executions = 5; + state.bytes_read = 1000; + state.bytes_written = 500; + + state.reset_counters(); + + assert_eq!(state.executions, 0); + assert_eq!(state.bytes_read, 0); + assert_eq!(state.bytes_written, 0); + } +} diff --git a/crates/wasm-runtime-interface/src/time.rs b/crates/wasm-runtime-interface/src/time.rs new file mode 100644 index 000000000..756c1a41b --- /dev/null +++ b/crates/wasm-runtime-interface/src/time.rs @@ -0,0 +1,221 @@ +use crate::runtime::{HostFunctionRegistrar, RuntimeState, WasmRuntimeError}; +use serde::{Deserialize, Serialize}; +use tracing::warn; +use wasmtime::{Caller, Linker}; + +pub const HOST_TIME_NAMESPACE: &str = "platform_time"; +pub const HOST_GET_TIMESTAMP: &str = "get_timestamp"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum TimeHostFunction { + GetTimestamp, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum TimeMode { + Real, + #[default] + Deterministic, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TimePolicy { + pub enabled: bool, + pub mode: TimeMode, + pub fixed_timestamp_ms: u64, +} + +impl Default for TimePolicy { + fn default() -> Self { + Self { + enabled: true, + mode: TimeMode::Deterministic, + fixed_timestamp_ms: 1_700_000_000_000, + } + } +} + +impl TimePolicy { + pub fn real() -> Self { + Self { + enabled: true, + mode: TimeMode::Real, + fixed_timestamp_ms: 0, + } + } + + pub fn deterministic(timestamp_ms: u64) -> Self { + Self { + enabled: true, + mode: TimeMode::Deterministic, + fixed_timestamp_ms: timestamp_ms, + } + } + + pub fn development() -> Self { + Self::real() + } +} + +#[allow(dead_code)] +pub struct TimeState { + policy: TimePolicy, + challenge_id: String, + validator_id: String, +} + +impl TimeState { + pub fn new(policy: TimePolicy, challenge_id: String, validator_id: String) -> Self { + Self { + policy, + challenge_id, + validator_id, + } + } + + pub fn get_timestamp(&self) -> Result { + if !self.policy.enabled { + return Err(TimeError::Disabled); + } + + match self.policy.mode { + TimeMode::Real => { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_err(|e| TimeError::Failed(e.to_string()))?; + Ok(now.as_millis() as u64) + } + TimeMode::Deterministic => Ok(self.policy.fixed_timestamp_ms), + } + } +} + +#[derive(Debug, thiserror::Error, Serialize, Deserialize)] +pub enum TimeError { + #[error("time access disabled")] + Disabled, + #[error("time failed: {0}")] + Failed(String), +} + +#[derive(Clone, Debug)] +pub struct TimeHostFunctions { + enabled: Vec, +} + +impl TimeHostFunctions { + pub fn new(enabled: Vec) -> Self { + Self { enabled } + } + + pub fn all() -> Self { + Self { + enabled: vec![TimeHostFunction::GetTimestamp], + } + } +} + +impl Default for TimeHostFunctions { + fn default() -> Self { + Self::all() + } +} + +impl HostFunctionRegistrar for TimeHostFunctions { + fn register(&self, linker: &mut Linker) -> Result<(), WasmRuntimeError> { + if self.enabled.contains(&TimeHostFunction::GetTimestamp) { + linker + .func_wrap( + HOST_TIME_NAMESPACE, + HOST_GET_TIMESTAMP, + |mut caller: Caller| -> i64 { handle_get_timestamp(&mut caller) }, + ) + .map_err(|err| WasmRuntimeError::HostFunction(err.to_string()))?; + } + + Ok(()) + } +} + +fn handle_get_timestamp(caller: &mut Caller) -> i64 { + match caller.data().time_state.get_timestamp() { + Ok(ts) => ts as i64, + Err(err) => { + warn!( + challenge_id = %caller.data().challenge_id, + validator_id = %caller.data().validator_id, + error = %err, + "get_timestamp failed" + ); + -1 + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_time_policy_default() { + let policy = TimePolicy::default(); + assert!(policy.enabled); + assert_eq!(policy.mode, TimeMode::Deterministic); + assert_eq!(policy.fixed_timestamp_ms, 1_700_000_000_000); + } + + #[test] + fn test_time_policy_real() { + let policy = TimePolicy::real(); + assert!(policy.enabled); + assert_eq!(policy.mode, TimeMode::Real); + } + + #[test] + fn test_time_policy_deterministic() { + let ts = 1_234_567_890_000; + let policy = TimePolicy::deterministic(ts); + assert!(policy.enabled); + assert_eq!(policy.mode, TimeMode::Deterministic); + assert_eq!(policy.fixed_timestamp_ms, ts); + } + + #[test] + fn test_time_state_deterministic() { + let state = TimeState::new( + TimePolicy::deterministic(42_000), + "test".into(), + "test".into(), + ); + assert_eq!(state.get_timestamp().unwrap(), 42_000); + } + + #[test] + fn test_time_state_real() { + let state = TimeState::new(TimePolicy::real(), "test".into(), "test".into()); + let ts = state.get_timestamp().unwrap(); + assert!(ts > 1_700_000_000_000); + } + + #[test] + fn test_time_state_disabled() { + let state = TimeState::new( + TimePolicy { + enabled: false, + ..Default::default() + }, + "test".into(), + "test".into(), + ); + let err = state.get_timestamp().unwrap_err(); + assert!(matches!(err, TimeError::Disabled)); + } + + #[test] + fn test_time_host_functions_all() { + let funcs = TimeHostFunctions::all(); + assert!(funcs.enabled.contains(&TimeHostFunction::GetTimestamp)); + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..d77f11211 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,88 @@ +# ============================================================================= +# Platform Validator Configuration +# ============================================================================= +# Fully decentralized P2P architecture +# +# Usage: +# export VALIDATOR_SECRET_KEY="your-mnemonic-or-hex-key" +# docker compose up -d +# ============================================================================= + +services: + # ========================================================================== + # Platform Validator Node + # ========================================================================== + validator: + image: ghcr.io/platformnetwork/platform:latest + container_name: platform-validator + restart: unless-stopped + + # Enable Watchtower auto-update for this container only + labels: + - "com.centurylinklabs.watchtower.enable=true" + + ports: + - "9000:9000" # P2P libp2p port + - "8080:8080" # Local RPC API (optional) + + volumes: + - validator-data:/data + + environment: + - RUST_LOG=info,validator_node=debug,platform_p2p_consensus=debug + - DATA_DIR=/data + - VALIDATOR_SECRET_KEY=${VALIDATOR_SECRET_KEY} + # Subtensor endpoint (Bittensor mainnet) + - SUBTENSOR_ENDPOINT=wss://entrypoint-finney.opentensor.ai:443 + # Network UID for this subnet + - NETUID=100 + # P2P settings + - P2P_LISTEN_ADDR=/ip4/0.0.0.0/tcp/9000 + # Optional: Bootstrap peers (comma-separated multiaddrs) + # - BOOTSTRAP_PEERS=/ip4/x.x.x.x/tcp/9000/p2p/PEER_ID + + command: ["validator-node", "--data-dir", "/data", "--listen-addr", "/ip4/0.0.0.0/tcp/9000"] + + healthcheck: + test: ["CMD-SHELL", "test -e /data/distributed.db || exit 1"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + + networks: + - platform + + # ========================================================================== + # Watchtower - Auto-update platform container only + # ========================================================================== + watchtower: + image: nickfedor/watchtower@sha256:053e7ecba848b77eb5b966d236c2f4f2e1155e05007c9ef52418b4b7e255484b + container_name: platform-watchtower + restart: unless-stopped + + volumes: + - /var/run/docker.sock:/var/run/docker.sock + + environment: + # Only update containers with label "com.centurylinklabs.watchtower.enable=true" + - WATCHTOWER_LABEL_ENABLE=true + # Check at :00, :05, :10, :15... (all validators sync at same time) + - WATCHTOWER_SCHEDULE=0 */5 * * * * + # Remove old images after update + - WATCHTOWER_CLEANUP=true + # Include stopped containers + - WATCHTOWER_INCLUDE_STOPPED=false + # Logging + - WATCHTOWER_LOG_LEVEL=info + + networks: + - platform + +volumes: + validator-data: + driver: local + +networks: + platform: + driver: bridge diff --git a/docker/Dockerfile.challenge b/docker/Dockerfile.challenge new file mode 100644 index 000000000..06e7a6f1a --- /dev/null +++ b/docker/Dockerfile.challenge @@ -0,0 +1,92 @@ +# Challenge Container Dockerfile +# For Rust-based challenges that implement the Challenge trait +# +# Usage: +# docker build -f docker/Dockerfile.challenge \ +# --build-arg CHALLENGE_NAME=my-challenge \ +# --build-arg CHALLENGE_DIR=challenges/my-challenge \ +# -t cortexlm/challenge-my-challenge:v1.0.0 . + +# ===== Build Stage ===== +FROM rust:1.92-bookworm as builder + +ARG PLATFORM_NIGHTLY_RUSTFLAGS="" +ARG PLATFORM_LINKER_RUSTFLAGS="" +ARG INSTALL_FAST_LINKER=auto +ENV PLATFORM_NIGHTLY_RUSTFLAGS=${PLATFORM_NIGHTLY_RUSTFLAGS} +ENV PLATFORM_LINKER_RUSTFLAGS=${PLATFORM_LINKER_RUSTFLAGS} +ENV INSTALL_FAST_LINKER=${INSTALL_FAST_LINKER} + +ARG CHALLENGE_NAME=my-challenge +ARG CHALLENGE_DIR=challenges/my-challenge + +WORKDIR /app + +# Install dependencies +RUN apt-get update \ + && apt-get install -y \ + pkg-config \ + libssl-dev \ + protobuf-compiler \ + lld \ + && if [ "$INSTALL_FAST_LINKER" = "mold" ]; then \ + apt-get install -y mold; \ + fi \ + && rm -rf /var/lib/apt/lists/* + +# Copy mini-chain SDK (challenges depend on it) +COPY mini-chain/crates/challenge-sdk /app/mini-chain/crates/challenge-sdk +COPY mini-chain/crates/core /app/mini-chain/crates/core +COPY mini-chain/Cargo.toml /app/mini-chain/Cargo.toml + +# Copy challenge source +COPY ${CHALLENGE_DIR} /app/challenge + +WORKDIR /app/challenge + +# Build release binary +RUN cargo build --release + +# ===== Runtime Stage ===== +FROM debian:bookworm-slim + +ARG CHALLENGE_NAME=my-challenge + +# Install runtime dependencies +RUN apt-get update && apt-get install -y \ + ca-certificates \ + libssl3 \ + docker.io \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Create challenge user +RUN useradd -m -s /bin/bash challenge + +WORKDIR /app + +# Copy binary from builder +COPY --from=builder /app/challenge/target/release/${CHALLENGE_NAME} /app/challenge + +# Copy tasks directory if exists +COPY --from=builder /app/challenge/tasks /app/tasks 2>/dev/null || true + +# Create data directories +RUN mkdir -p /data /app/tasks && chown -R challenge:challenge /data /app + +# Environment variables +ENV RUST_LOG=info +ENV CHALLENGE_PORT=8080 +ENV DATA_DIR=/data +ENV TASKS_DIR=/app/tasks + +# Expose port +EXPOSE 8080 + +# Health check +HEALTHCHECK --interval=10s --timeout=5s --start-period=30s --retries=3 \ + CMD curl -f http://localhost:8080/health || exit 1 + +USER challenge + +ENTRYPOINT ["/app/challenge"] diff --git a/docker/Dockerfile.validator b/docker/Dockerfile.validator new file mode 100644 index 000000000..791dccf06 --- /dev/null +++ b/docker/Dockerfile.validator @@ -0,0 +1,87 @@ +# Validator Node Docker Image +# Multi-stage build for minimal runtime image + +# ===== Build Stage ===== +FROM rust:1.92-bookworm as builder +ARG RUSTUP_TOOLCHAIN=stable +ENV RUSTUP_TOOLCHAIN=${RUSTUP_TOOLCHAIN} + +ARG PLATFORM_NIGHTLY_RUSTFLAGS="" +ARG PLATFORM_LINKER_RUSTFLAGS="" +ARG PLATFORM_FAST_LINKER_RUSTFLAGS="" +ARG INSTALL_FAST_LINKER=auto +ENV PLATFORM_NIGHTLY_RUSTFLAGS=${PLATFORM_NIGHTLY_RUSTFLAGS} +ENV PLATFORM_LINKER_RUSTFLAGS=${PLATFORM_LINKER_RUSTFLAGS} +ENV PLATFORM_FAST_LINKER_RUSTFLAGS=${PLATFORM_FAST_LINKER_RUSTFLAGS} +ENV INSTALL_FAST_LINKER=${INSTALL_FAST_LINKER} + +WORKDIR /app + +# Install dependencies +RUN apt-get update \ + && apt-get install -y \ + pkg-config \ + libssl-dev \ + protobuf-compiler \ + lld \ + && if [ "$INSTALL_FAST_LINKER" = "mold" ]; then \ + apt-get install -y mold; \ + fi \ + && if [ "$RUSTUP_TOOLCHAIN" = "nightly" ]; then \ + rustup toolchain install nightly; \ + fi \ + && rm -rf /var/lib/apt/lists/* + +# Copy workspace files +COPY Cargo.toml Cargo.lock ./ +COPY crates ./crates +COPY bins ./bins +COPY tests ./tests + +# Build release binary +RUN cargo +${RUSTUP_TOOLCHAIN} build --release --bin validator-node + +# ===== Runtime Stage ===== +FROM debian:bookworm-slim + +# Install runtime dependencies +RUN apt-get update && apt-get install -y \ + ca-certificates \ + libssl3 \ + docker.io \ + && rm -rf /var/lib/apt/lists/* + +# Create non-root user +RUN useradd -m -s /bin/bash validator + +WORKDIR /app + +# Copy binary from builder +COPY --from=builder /app/target/release/validator-node /app/validator-node + +# Copy entrypoint script +COPY docker/entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +# Create data directory +RUN mkdir -p /data && chown validator:validator /data + +# Environment variables +ENV RUST_LOG=info +ENV DATA_DIR=/data + +# Expose ports +# P2P port +EXPOSE 9000 +# RPC port +EXPOSE 8080 + +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ + CMD curl -f http://localhost:8080/health || exit 1 + +# Run as validator user +USER validator + +ENTRYPOINT ["/app/entrypoint.sh"] +CMD ["validator-node"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 000000000..3ecb47b8d --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,70 @@ +# Platform Validator Docker Compose +# For local development and testing +# +# Challenges are maintained in separate repositories and import +# platform-challenge-sdk as a git dependency. Add your challenge +# service below following the example template. + +version: '3.8' + +services: + # ===== Core Validator ===== + validator: + build: + context: .. + dockerfile: docker/Dockerfile.validator + container_name: platform-validator + restart: unless-stopped + environment: + - VALIDATOR_SECRET_KEY=${VALIDATOR_SECRET_KEY} + - SUBTENSOR_ENDPOINT=${SUBTENSOR_ENDPOINT:-wss://entrypoint-finney.opentensor.ai:443} + - P2P_PORT=9000 + - RPC_PORT=8080 + - RUST_LOG=info + volumes: + - validator-data:/data + - /var/run/docker.sock:/var/run/docker.sock + ports: + - "9000:9000" # P2P + - "8080:8080" # RPC + networks: + - platform + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8080/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + + # ===== Example Challenge (uncomment and customise) ===== + # challenge-example: + # build: + # context: ../.. + # dockerfile: docker/Dockerfile.challenge + # args: + # CHALLENGE_NAME: my-challenge + # CHALLENGE_DIR: challenges/my-challenge + # image: your-org/challenge-example:latest + # container_name: challenge-example + # restart: unless-stopped + # environment: + # - RUST_LOG=info + # - CHALLENGE_PORT=8080 + # volumes: + # - challenge-data:/data + # networks: + # - platform + # healthcheck: + # test: ["CMD", "curl", "-f", "http://localhost:8080/health"] + # interval: 10s + # timeout: 5s + # retries: 3 + # start_period: 30s + +networks: + platform: + driver: bridge + name: platform + +volumes: + validator-data: diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 000000000..934115025 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,45 @@ +#!/bin/bash +set -e + +# Mini-Chain Validator Entrypoint + +echo "=== Mini-Chain Validator ===" +echo "Version: ${VERSION:-unknown}" +echo "P2P Port: ${P2P_PORT:-9000}" +echo "RPC Port: ${RPC_PORT:-8080}" +echo "" + +# Check for required environment variables +if [ -z "$VALIDATOR_SECRET_KEY" ]; then + echo "ERROR: VALIDATOR_SECRET_KEY is required" + exit 1 +fi + +# Build arguments +ARGS="--secret-key $VALIDATOR_SECRET_KEY" + +# Optional arguments +if [ -n "$P2P_PORT" ]; then + ARGS="$ARGS --p2p-port $P2P_PORT" +fi + +if [ -n "$RPC_PORT" ]; then + ARGS="$ARGS --rpc-port $RPC_PORT" +fi + +if [ -n "$SUBTENSOR_ENDPOINT" ]; then + ARGS="$ARGS --subtensor-endpoint $SUBTENSOR_ENDPOINT" +fi + +if [ -n "$BOOTSTRAP_PEERS" ]; then + for peer in $BOOTSTRAP_PEERS; do + ARGS="$ARGS --bootstrap-peer $peer" + done +fi + +if [ -n "$DATA_DIR" ]; then + ARGS="$ARGS --data-dir $DATA_DIR" +fi + +# Execute validator +exec /app/validator-node $ARGS "$@" diff --git a/docs/architecture.md b/docs/architecture.md index 321d891e7..413e45d65 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,363 +1,91 @@ -# Architecture Overview +# Architecture -This document describes the internal architecture of Term Challenge, including system components, the WASM module design, host function surface, P2P message types, and storage schema. +Platform is a **WASM-first, P2P validator network** for deterministic challenge evaluation on Bittensor. Validators exchange submissions, evaluations, and consensus votes directly over libp2p, then submit finalized weight matrices to the chain. ---- +## Core Components -## System Components +- **Validator Node (`validator-node`)**: P2P networking, consensus, evaluation, and weight submission. +- **Challenge Registry**: signed metadata for active challenges (WASM modules + runtime policies). +- **WASM Runtime Interface**: sandboxed execution with resource caps and audited host functions. +- **P2P Consensus Engine**: PBFT-style voting with stake-weighted validator set. +- **Distributed Storage (DHT)**: shared submissions, checkpoints, and consensus state. -```mermaid -flowchart TB - subgraph Miner - Agent[Python Agent] - CLI[term-cli TUI] - end - - subgraph Platform-v2 Validator - RPC[RPC Server] - P2P[P2P Consensus
libp2p gossipsub + DHT] - WR[WASM Runtime] - WASM[term-challenge.wasm] - Exec[term-executor] - BT[Bittensor Integration] - Store[Blockchain Storage] - end - - subgraph Bittensor - Chain[Bittensor Chain] - end - - Agent -->|ZIP submission| RPC - CLI -->|JSON-RPC| RPC - RPC --> WR - WR --> WASM - WASM -->|host_storage_get/set| Store - WASM -->|host_http_post| Exec - P2P <-->|Consensus messages| P2P - WR --> P2P - BT -->|Weights| Chain - P2P --> BT -``` - ---- - -## WASM Module Architecture +## System Context ```mermaid -flowchart TB - subgraph "term-challenge-wasm (no_std)" - Lib[lib.rs
Challenge trait impl] - Types[types.rs
Submission, TaskResult,
ChallengeParams, DecayParams] - Scoring[scoring.rs
Aggregate scoring,
decay, weight calc] - Tasks[tasks.rs
Active dataset
management] - Dataset[dataset.rs
Dataset selection
consensus logic] - Routes[routes.rs
Route definitions
for RPC] - Storage[agent_storage.rs
Code, hash, log
storage functions] - end - - Lib --> Types - Lib --> Scoring - Lib --> Storage - Lib --> Tasks - Tasks --> Dataset - Lib --> Routes - - subgraph "Host Functions (platform-v2)" - HStorage[host_storage_get/set] - HHttp[host_http_post] - HEpoch[host_consensus_get_epoch] - end - - Lib --> HStorage - Lib --> HHttp - Lib --> HEpoch -``` - -### Module Responsibilities - -| Module | Purpose | -| --- | --- | -| `lib.rs` | Implements the `Challenge` trait: `validate()`, `evaluate()`, `tasks()`, `configure()` | -| `types.rs` | All data structures: `Submission`, `TaskResult`, `ChallengeParams`, `DecayParams`, `AgentLogs`, `RouteDefinition` | -| `scoring.rs` | Score aggregation by difficulty, pass rate calculation, decay application, weight conversion | -| `tasks.rs` | Active dataset storage/retrieval, dataset history management | -| `dataset.rs` | P2P dataset consensus logic (reserved for future implementation) | -| `routes.rs` | Route definitions for challenge RPC endpoints | -| `agent_storage.rs` | Agent code, hash, and log storage with size limits | - ---- - -## Host Function Surface - -These are the host functions available to WASM challenge modules, provided by `platform-challenge-sdk-wasm`. Term Challenge uses a subset of these. - -### Network Functions (`platform_network`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_http_get` | `(request: &[u8]) โ†’ Result, i32>` | HTTP GET request | No | -| `host_http_post` | `(request: &[u8], body: &[u8]) โ†’ Result, i32>` | HTTP POST request | Yes (LLM judge) | -| `host_dns_resolve` | `(request: &[u8]) โ†’ Result, i32>` | DNS resolution | No | - -### Storage Functions (`platform_storage`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_storage_get` | `(key: &[u8]) โ†’ Result, i32>` | Read from blockchain storage | Yes | -| `host_storage_set` | `(key: &[u8], value: &[u8]) โ†’ Result<(), i32>` | Write to blockchain storage | Yes | - -### Terminal Functions (`platform_terminal`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_terminal_exec` | `(request: &[u8]) โ†’ Result, i32>` | Execute terminal command | No | -| `host_read_file` | `(path: &[u8]) โ†’ Result, i32>` | Read file contents | No | -| `host_write_file` | `(path: &[u8], data: &[u8]) โ†’ Result<(), i32>` | Write file contents | No | -| `host_list_dir` | `(path: &[u8]) โ†’ Result, i32>` | List directory contents | No | -| `host_get_time` | `() โ†’ i64` | Get current timestamp | No | -| `host_random_seed` | `(buf: &mut [u8]) โ†’ Result<(), i32>` | Fill buffer with random bytes | No | - -### Sandbox Functions (`platform_sandbox`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_sandbox_exec` | `(request: &[u8]) โ†’ Result, i32>` | Execute in sandbox | No | -| `host_get_timestamp` | `() โ†’ i64` | Get sandbox timestamp | No | -| `host_log` | `(level: u8, msg: &str) โ†’ ()` | Log a message | No | - -### LLM Functions (`platform_llm`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_llm_chat_completion` | `(request: &[u8]) โ†’ Result, i32>` | LLM chat completion | No (uses HTTP post instead) | -| `host_llm_is_available` | `() โ†’ bool` | Check LLM availability | No | - -### Consensus Functions (`platform_consensus`) - -| Function | Signature | Description | Used by Term Challenge | -| --- | --- | --- | --- | -| `host_consensus_get_epoch` | `() โ†’ i64` | Get current epoch number | Yes | -| `host_consensus_get_validators` | `() โ†’ Result, i32>` | Get validator list | No | -| `host_consensus_propose_weight` | `(uid: i32, weight: i32) โ†’ Result<(), i32>` | Propose a weight | No | -| `host_consensus_get_votes` | `() โ†’ Result, i32>` | Get consensus votes | No | -| `host_consensus_get_state_hash` | `() โ†’ Result<[u8; 32], i32>` | Get state hash | No | -| `host_consensus_get_submission_count` | `() โ†’ i32` | Get submission count | No | -| `host_consensus_get_block_height` | `() โ†’ i64` | Get block height | No | - ---- - -## WASM ABI Exports - -The `register_challenge!` macro exports these functions from the WASM module: - -| Export | Signature | Description | -| --- | --- | --- | -| `evaluate` | `(agent_ptr: i32, agent_len: i32) โ†’ i64` | Evaluate a submission, returns packed ptr+len | -| `validate` | `(agent_ptr: i32, agent_len: i32) โ†’ i32` | Validate a submission, returns 0 or 1 | -| `get_name` | `() โ†’ i32` | Return challenge name | -| `get_version` | `() โ†’ i32` | Return challenge version | -| `generate_task` | `(params_ptr: i32, params_len: i32) โ†’ i64` | Generate a task | -| `setup_environment` | `(config_ptr: i32, config_len: i32) โ†’ i32` | Set up environment | -| `get_tasks` | `() โ†’ i64` | Return active task definitions | -| `configure` | `(config_ptr: i32, config_len: i32) โ†’ i32` | Configure challenge with dataset | -| `get_routes` | `() โ†’ i64` | Return route definitions | -| `handle_route` | `(req_ptr: i32, req_len: i32) โ†’ i64` | Handle an incoming route request | -| `alloc` | `(size: usize) โ†’ *mut u8` | Allocate memory in WASM linear memory | - ---- - -## P2P Message Types - -These message types are used for inter-validator communication over libp2p gossipsub. Term Challenge interacts with these through platform-v2's consensus layer. - -### Consensus Messages - -| Message | Description | -| --- | --- | -| `Proposal` | Leader proposes a state transition (view, sequence, content, signature) | -| `PrePrepare` | Leader broadcasts after receiving proposal | -| `Prepare` | Validators acknowledge pre-prepare (2f+1 required) | -| `Commit` | Validators commit to the proposal | -| `ViewChange` | Request new leader election | -| `NewView` | New leader announces with collected view changes | - -### Challenge Evaluation Messages - -| Message | Description | -| --- | --- | -| `Submission` | Agent code submission for evaluation (submission_id, challenge_id, miner, agent_hash) | -| `Evaluation` | Evaluation result from a validator (score, metrics, execution_time) | -| `WeightVote` | Weight vote for epoch finalization (uid โ†’ weight vector) | - -### Challenge Lifecycle Messages - -| Message | Description | -| --- | --- | -| `JobClaim` | Validator claims evaluation work capacity | -| `JobAssignment` | Assigns a submission evaluation to a validator | -| `DataRequest` | Request challenge-related data from peers | -| `DataResponse` | Response containing requested challenge data | -| `TaskProgress` | Progress update during evaluation (task_index, total_tasks, progress_pct) | -| `TaskResult` | Result of a single task evaluation (passed, score, output) | -| `LeaderboardRequest` | Request leaderboard data with pagination | -| `LeaderboardResponse` | Response with serialized leaderboard entries | -| `ChallengeUpdate` | Update notification for challenge configuration | -| `StorageProposal` | Propose storing a key-value pair in consensus storage | -| `StorageVote` | Vote on a storage proposal (approve/reject) | - -### Review Messages - -| Message | Description | -| --- | --- | -| `ReviewAssignment` | Assigns review validators for a submission (3 LLM + 3 AST, deterministic seed) | -| `ReviewDecline` | Validator declines or times out on a review assignment | -| `ReviewResult` | Review result with score and details (review_type: Llm or Ast) | - -### Agent Log Messages - -| Message | Description | -| --- | --- | -| `AgentLogProposal` | Proposes agent evaluation logs for P2P consensus (logs_hash, logs_data โ‰ค 256KB) | - -### Network Maintenance Messages - -| Message | Description | -| --- | --- | -| `Heartbeat` | Validator presence signal (state_hash, sequence, stake) | -| `PeerAnnounce` | Peer discovery with multiaddresses and peer_id | - -### State Sync Messages - -| Message | Description | -| --- | --- | -| `StateRequest` | Request state synchronization from peers | -| `StateResponse` | Response with state data and optional Merkle proof | - ---- - -## Storage Key Schema - -Term Challenge uses the following storage keys via `host_storage_get` and `host_storage_set`: - -### Agent Storage Keys - -| Key Format | Content | Max Size | Module | -| --- | --- | --- | --- | -| `agent_code::` | Raw ZIP package bytes | 1 MB (1,048,576 bytes) | `agent_storage` | -| `agent_hash::` | Agent package hash string | Unbounded | `agent_storage` | -| `agent_logs::` | Serialized `AgentLogs` struct | 256 KB (262,144 bytes) | `agent_storage` | - -### Submission Tracking Keys - -| Key Format | Content | Size | Module | -| --- | --- | --- | --- | -| `last_submission:` | Last submission epoch (u64 LE) | 8 bytes | `lib` | - -### Dataset Keys - -| Key Format | Content | Size | Module | -| --- | --- | --- | --- | -| `active_dataset` | Serialized `Vec` | Variable | `tasks` | -| `dataset_history` | Serialized `Vec` (max 100 entries) | Variable | `tasks` | - -### Key Encoding - -- **Hotkey**: Raw bytes of the miner's hotkey string (`miner_hotkey.as_bytes()`) -- **Epoch**: Little-endian encoded `u64` (`epoch.to_le_bytes()`) -- **Separator**: ASCII colon (`:`, byte `0x3A`) - ---- - -## Data Types - -### Core Submission Types - -``` -Submission { - agent_hash: String, - miner_hotkey: String, - signature: Vec, - epoch: u64, - package_zip: Vec, - basilica_instance: String, - executor_url: String, - executor_token: String, - task_results: Vec, -} - -TaskResult { - task_id: String, - passed: bool, - score: f64, - execution_time_ms: u64, - test_output: String, - agent_output: String, - error: Option, -} +flowchart LR + Owner[Sudo Owner] -->|Signed challenge updates| Mesh[(libp2p Mesh)] + Mesh --> DHT[(DHT: submissions + checkpoints)] + Mesh --> V1[Validator 1] + Mesh --> V2[Validator 2] + Mesh --> VN[Validator N] + V1 -->|Evaluations + votes| Mesh + V2 -->|Evaluations + votes| Mesh + VN -->|Evaluations + votes| Mesh + V1 -->|Final weights| BT[Bittensor Chain] + V2 -->|Final weights| BT + VN -->|Final weights| BT ``` -### Configuration Types +## Consensus Flow (PBFT-style) +```mermaid +sequenceDiagram + participant L as Leader + participant V1 as Validator 1 + participant V2 as Validator 2 + participant Vn as Validator N + + L->>V1: Proposal(action, height) + L->>V2: Proposal(action, height) + L->>Vn: Proposal(action, height) + V1-->>L: Vote(approve/reject) + V2-->>L: Vote(approve/reject) + Vn-->>L: Vote(approve/reject) + L-->>V1: Commit(>=2f+1 approvals) + L-->>V2: Commit(>=2f+1 approvals) + L-->>Vn: Commit(>=2f+1 approvals) ``` -ChallengeParams { - tasks: Vec, - llm_judge_url: Option, - decay_params: Option, - active_dataset: Option>, -} -DecayParams { - grace_period_hours: u64, // default: 72 - half_life_hours: u64, // default: 24 - min_multiplier: f64, // default: 0.0 -} +## Data Flow -TaskDefinition { - id: String, - name: String, - repo: String, - base_commit: String, - difficulty: Difficulty, // Easy | Medium | Hard - timeout_secs: u64, -} +```mermaid +flowchart TD + Miner[Miners] -->|Submit payload| P2P[(libp2p gossipsub)] + P2P --> Validators[Validator Nodes] + Validators --> Runtime[WASM Sandbox] + Runtime --> Validators + Validators -->|Aggregate scores + consensus| DHT[(DHT + consensus state)] + Validators -->|Stake-weighted weights| Bittensor[Bittensor Chain] ``` -### Route Types +## Runtime Policy Boundary +```mermaid +flowchart LR + Validator[Validator Node] --> Runtime[WASM Runtime] + Runtime --> Policy[Runtime Policy] + Runtime --> HostFns[Whitelisted Host Functions] + Runtime --> Audit[Audit Logs] + Policy --> Runtime + HostFns --> Runtime + Runtime -->|Deterministic outputs| Validator ``` -WasmRouteRequest { - method: String, - path: String, - params: Vec<(String, String)>, - query: Vec<(String, String)>, - body: Vec, - auth_hotkey: Option, -} -WasmRouteResponse { - status: u16, - body: Vec, -} -``` +## Operational Boundaries ---- +- **WASM-first**: challenge execution runs in WASM in production. +- **Docker test-only**: Docker-backed harnesses are reserved for local/CI testing. +- **Consensus-driven changes**: challenge lifecycle events require PBFT approvals. -## Serialization +## Storage Model -- **WASM โ†” Host**: `bincode` with fixed-int encoding and size limits -- **P2P Messages**: `bincode` serialization (max 16 MB per message) -- **Storage Values**: `bincode` serialization -- **RPC**: JSON-RPC 2.0 over HTTP +- **DHT entries**: submissions, evaluation results, consensus checkpoints. +- **Local persistence**: validator state and audit logs under `data/`. -### Size Limits +## Related Documentation -| Context | Limit | Constant | -| --- | --- | --- | -| Submission deserialization | 64 MB | `MAX_SUBMISSION_SIZE` | -| Challenge params deserialization | 4 MB | `MAX_PARAMS_SIZE` | -| LLM response deserialization | 1 MB | `MAX_LLM_RESPONSE_SIZE` | -| P2P message | 16 MB | `MAX_P2P_MESSAGE_SIZE` | -| Agent package ZIP | 1 MB | `MAX_AGENT_PACKAGE_SIZE` | -| Agent logs | 256 KB | `MAX_LOG_SIZE` | -| Task output preview | 4 KB | `MAX_TASK_OUTPUT_PREVIEW` | -| Max tasks per submission | 256 | `MAX_TASKS` | +- [Security Model](security.md) +- [Challenges](challenges.md) +- [Challenge Integration Guide](challenge-integration.md) +- [Validator Operations](operations/validator.md) diff --git a/docs/challenge-integration.md b/docs/challenge-integration.md new file mode 100644 index 000000000..da05326e9 --- /dev/null +++ b/docs/challenge-integration.md @@ -0,0 +1,232 @@ +# Challenge Integration Guide + +This guide explains how to integrate challenge crates with the Platform validator network. All challenge execution is **WASM-only**. + +## Overview + +Platform uses a modular challenge architecture where each challenge: + +- Runs in a sandboxed WASM runtime (production). +- Communicates with validators via HTTP or WebSocket. +- Persists state through the shared checkpoint system. +- Supports hot-reload without losing evaluation progress. + +Challenge outputs feed directly into validator consensus. Validators exchange scores and weight commitments exclusively over libp2p with no centralized relays. + +## Integration Architecture + +```mermaid +flowchart TB + Validator[Platform Validator] --> Registry[Challenge Registry] + Validator --> StateMgr[State Manager] + Validator --> Checkpoints[Checkpoint System] + Registry --> Challenges[WASM Challenges] + StateMgr --> Challenges +``` + +## Challenge Lifecycle in Consensus + +```mermaid +sequenceDiagram + participant Owner as Sudo Owner + participant Registry as Challenge Registry + participant Validators as Validator Set + participant Runtime as WASM Runtime + + Owner->>Registry: Signed metadata update + Registry->>Validators: Broadcast metadata + Validators->>Runtime: Load WASM module + Runtime-->>Validators: Policy + sandbox ready + Validators-->>Owner: Consensus approval +``` + +## P2P Consensus Inputs + +Validators evaluate challenges locally and publish weight commitments over the P2P mesh. Each validatorโ€™s voting power is proportional to its Bittensor stake. + +1. **Stake-weighted validator set** derived from the metagraph. +2. **Commit phase**: broadcast commitments for weight vectors. +3. **Reveal phase**: reveal weight vectors matching commitments. +4. **Epoch aggregation**: stake-weighted sum yields canonical weights. +5. **Consensus finalization**: validators agree on the aggregated weights and state hash. + +## Weight Aggregation at Epoch Boundaries + +At each epoch boundary, validators aggregate revealed weights with stake weighting to produce the canonical weight matrix and deterministic state hash. Late or mismatched reveals are rejected to keep aggregation deterministic. The finalized aggregation is the only weight matrix submitted back to Bittensor for the epoch. + +## Creating a Challenge Crate + +### 1. Project Structure + +``` +my-challenge/ + Cargo.toml + src/ + lib.rs # Challenge implementation + evaluation.rs # Evaluation logic + scoring.rs # Scoring algorithm + README.md # Documentation +``` + +### 2. Dependencies + +Add Platform SDK to your `Cargo.toml`: + +```toml +[dependencies] +platform-challenge-sdk = { git = "https://github.com/PlatformNetwork/platform" } +``` + +### 3. Implement the Challenge Trait + +```rust +use platform_challenge_sdk::prelude::*; + +pub struct MyChallenge { + // Your challenge state +} + +#[async_trait] +impl ServerChallenge for MyChallenge { + fn challenge_id(&self) -> &str { + "my-challenge" + } + + fn name(&self) -> &str { + "My Challenge" + } + + fn version(&self) -> &str { + env!("CARGO_PKG_VERSION") + } + + async fn evaluate( + &self, + req: EvaluationRequest, + ) -> Result { + let score = self.evaluate_submission(&req.data)?; + + Ok(EvaluationResponse::success( + &req.request_id, + score, + json!({"details": "evaluation complete"}), + )) + } +} +``` + +### 4. Build WASM Artifact + +Build and optimize your challenge into a `.wasm` module: + +```bash +cargo build --release --target wasm32-unknown-unknown +``` + +## State Persistence + +### Checkpoint Integration + +Challenges automatically benefit from Platform's checkpoint system: + +1. **Periodic checkpoints**: every 5 minutes. +2. **Shutdown checkpoints**: on graceful shutdown. +3. **Crash recovery**: on restart, state is restored. + +### Custom State + +```rust +use platform_challenge_sdk::database::Database; + +impl MyChallenge { + pub fn save_state(&self, db: &Database) -> Result<()> { + db.set("my_state_key", &self.state)?; + Ok(()) + } + + pub fn load_state(&mut self, db: &Database) -> Result<()> { + if let Some(state) = db.get("my_state_key")? { + self.state = state; + } + Ok(()) + } +} +``` + +## Hot-Reload Support + +### Graceful Shutdown Signal + +When receiving SIGTERM, your challenge should: + +1. Stop accepting new evaluations. +2. Complete in-progress evaluations. +3. Persist any local state. +4. Exit cleanly. + +```rust +tokio::select! { + _ = tokio::signal::ctrl_c() => { + info!("Shutting down gracefully..."); + self.save_state(&db)?; + } +} +``` + +### Version Compatibility + +```rust +#[derive(Serialize, Deserialize)] +struct MyState { + #[serde(default)] + version: u32, +} + +impl MyState { + fn migrate(&mut self) { + if self.version < 2 { + self.version = 2; + } + } +} +``` + +## Health Checks + +Implement health check endpoints: + +- `GET /health` - Returns 200 if healthy. +- `GET /ready` - Returns 200 if ready for traffic. +- `GET /live` - Returns 200 if process is alive. + +## Registration + +### Local Development + +Add to workspace `Cargo.toml`: + +```toml +[workspace] +members = [ + "challenges/my-challenge", +] +``` + +### Production Deployment + +1. Build the WASM challenge artifact. +2. Register via sudo action (network operator only). +3. Validators load the WASM module into the sandboxed runtime. + +## Best Practices + +1. **Deterministic evaluation**: same input produces same output. +2. **Resource-aware design**: keep runtime usage within policy limits. +3. **Versioned outputs**: include version in responses for auditability. +4. **Clear scoring**: document scoring and mapping of outputs to weights. + +## References + +- [Challenges](challenges.md) +- [Security Model](security.md) +- [Architecture](architecture.md) diff --git a/docs/challenges.md b/docs/challenges.md new file mode 100644 index 000000000..8d829a9f2 --- /dev/null +++ b/docs/challenges.md @@ -0,0 +1,67 @@ +# Challenges + +Challenges define the evaluation logic for miners. Platform treats challenges as **WASM modules** with deterministic execution, explicit resource limits, and signed metadata distributed over the validator network. + +## Challenge Lifecycle + +```mermaid +sequenceDiagram + participant Owner as Sudo Owner + participant Registry as Challenge Registry + participant Validators as Validator Set + participant Runtime as WASM Runtime + + Owner->>Registry: Add/Update/Remove (signed) + Registry->>Validators: Broadcast metadata + Validators->>Runtime: Load WASM module + Runtime-->>Validators: Ready + policy enforcement + Validators-->>Owner: Consensus-approved state +``` + +## Challenge Execution Flow + +```mermaid +flowchart TD + Miner[Miners] -->|Submit payload| P2P[(libp2p gossipsub)] + P2P --> Validators[Validators] + Validators --> Runtime[WASM Sandbox] + Runtime --> Validators + Validators -->|Scores + votes| P2P +``` + +## Runtime Constraints + +- CPU, memory, and I/O quotas enforced per evaluation. +- Network access allowed only via explicit policy. +- Deterministic execution required for consensus reproducibility. + +## Challenge Metadata + +Each metadata bundle includes: + +- Challenge identifier + version. +- WASM module hash and entrypoint. +- Resource policy (CPU/memory/time limits). +- Network policy (allowed domains/IPs). +- Scoring configuration and mechanism mapping. + +## Challenge States + +```mermaid +stateDiagram-v2 + [*] --> Draft + Draft --> Active: Signed registration + Active --> Deprecated: New version released + Deprecated --> Retired: Removed via consensus + Active --> Retired: Emergency removal +``` + +## Docker Policy (Test-Only) + +Docker is used only for integration testing and local harnesses. Production challenge execution relies exclusively on WASM modules. + +## References + +- [Challenge Integration Guide](challenge-integration.md) +- [Security Model](security.md) +- [Architecture](architecture.md) diff --git a/docs/miner/how-to-mine.md b/docs/miner/how-to-mine.md deleted file mode 100644 index ea8c512c5..000000000 --- a/docs/miner/how-to-mine.md +++ /dev/null @@ -1,312 +0,0 @@ -# How to Mine on Term Challenge - -This guide walks you through building and submitting an AI agent to the Term Challenge subnet on Bittensor. - ---- - -## Overview - -```mermaid -flowchart LR - Dev[Develop Agent] --> Test[Test Locally] - Test --> Pack[Package as ZIP] - Pack --> Submit[Submit via CLI] - Submit --> RPC[Validator RPC] - RPC --> Review[LLM + AST Review] - Review --> Eval[SWE-bench Evaluation] - Eval --> Score[Score + Weight] - Score --> TAO[TAO Rewards] -``` - -Miners create Python agents that solve SWE-bench software engineering tasks. Agents run inside a sandboxed executor with access to a git repository, task description, and optional LLM APIs. The network evaluates your agent against 50 tasks per epoch and assigns a score based on pass rate. - ---- - -## Prerequisites - -| Requirement | Version | Purpose | -| --- | --- | --- | -| Python | 3.10+ | Agent runtime | -| Docker | 24.0+ | Local testing with term-executor | -| Rust | 1.90+ | Building term-cli from source (optional) | -| Git | 2.30+ | Repository operations | -| LLM API Key | โ€” | Agent LLM access via litellm (recommended) | - -### Bittensor Requirements - -- A registered hotkey on the Term Challenge subnet -- Sufficient TAO for registration fees -- `btcli` installed for key management - ---- - -## Installation - -### 1. Clone the Repository - -```bash -git clone https://github.com/PlatformNetwork/term-challenge.git -cd term-challenge -``` - -### 2. Install the CLI - -```bash -# Option A: Download pre-built binary -platform download term-challenge - -# Option B: Build from source -cargo build --release -p term-cli -``` - -### 3. Set Up Python Environment - -```bash -python3 -m venv venv -source venv/bin/activate -pip install litellm requests -``` - ---- - -## Agent Project Structure - -Your agent submission is a ZIP file containing at minimum: - -``` -my-agent/ -โ”œโ”€โ”€ agent.py # Entry point (required) -โ”œโ”€โ”€ requirements.txt # Python dependencies (required) -โ””โ”€โ”€ utils/ # Optional helper modules - โ””โ”€โ”€ helpers.py -``` - -### `agent.py` โ€” Entry Point - -The executor runs `python agent.py` inside the task repository. Your agent receives task context through environment variables and must produce a git patch that solves the issue. - -### `requirements.txt` โ€” Dependencies - -List all Python packages your agent needs. These are installed via `pip install -r requirements.txt` before execution. - ---- - -## Minimal Agent Example - -```python -"""Minimal Term Challenge agent using litellm.""" -import os -import subprocess - -TASK_ID = os.environ.get("TERM_TASK_ID", "") -REPO = os.environ.get("TERM_REPO", "") -BASE_COMMIT = os.environ.get("TERM_BASE_COMMIT", "") -ISSUE_TEXT = os.environ.get("TERM_ISSUE_TEXT", "") -HINTS = os.environ.get("TERM_HINTS", "") - -def run(cmd, **kwargs): - result = subprocess.run(cmd, shell=True, capture_output=True, text=True, **kwargs) - return result.stdout, result.stderr, result.returncode - -def solve(): - try: - from litellm import completion - except ImportError: - run("pip install litellm") - from litellm import completion - - repo_structure, _, _ = run("find . -type f -name '*.py' | head -50") - - response = completion( - model="gpt-4o", - messages=[ - { - "role": "system", - "content": "You are a software engineer. Generate a unified diff patch to fix the described issue.", - }, - { - "role": "user", - "content": ( - f"Repository: {REPO}\n" - f"Issue: {ISSUE_TEXT}\n" - f"Hints: {HINTS}\n" - f"Files:\n{repo_structure}\n\n" - "Provide ONLY a unified diff patch." - ), - }, - ], - ) - - patch = response.choices[0].message.content - with open("/tmp/fix.patch", "w") as f: - f.write(patch) - - run("git apply /tmp/fix.patch") - run("git add -A") - run('git commit -m "Fix issue"') - -if __name__ == "__main__": - solve() -``` - ---- - -## Environment Variables - -The executor sets these environment variables before running your agent: - -| Variable | Description | Example | -| --- | --- | --- | -| `TERM_TASK_ID` | Unique task identifier | `django__django-16527` | -| `TERM_REPO` | Repository name | `django/django` | -| `TERM_BASE_COMMIT` | Git commit to start from | `a1b2c3d4e5f6...` | -| `TERM_ISSUE_TEXT` | Full issue description text | *(multiline)* | -| `TERM_HINTS` | Optional hints for the task | *(may be empty)* | -| `TERM_TIMEOUT` | Execution timeout in seconds | `300` | -| `TERM_DIFFICULTY` | Task difficulty level | `Easy`, `Medium`, or `Hard` | -| `TERM_CHECKPOINT_DIR` | Directory for checkpoint files | `/tmp/checkpoints` | - ---- - -## Checkpoints - -Agents can save intermediate state to the checkpoint directory. This is useful for: - -- Resuming work if the agent is interrupted -- Storing intermediate analysis results -- Caching LLM responses to avoid redundant API calls - -```python -import os -import json - -CHECKPOINT_DIR = os.environ.get("TERM_CHECKPOINT_DIR", "/tmp/checkpoints") - -def save_checkpoint(name, data): - os.makedirs(CHECKPOINT_DIR, exist_ok=True) - path = os.path.join(CHECKPOINT_DIR, f"{name}.json") - with open(path, "w") as f: - json.dump(data, f) - -def load_checkpoint(name): - path = os.path.join(CHECKPOINT_DIR, f"{name}.json") - if os.path.exists(path): - with open(path) as f: - return json.load(f) - return None -``` - ---- - -## Testing Locally - -### 1. Run Against a Single Task - -```bash -# Set up a test task -export TERM_TASK_ID="test-task-001" -export TERM_REPO="my-org/my-repo" -export TERM_BASE_COMMIT="main" -export TERM_ISSUE_TEXT="Fix the bug in module X" -export TERM_TIMEOUT="300" - -# Clone the target repo -git clone https://github.com/$TERM_REPO /tmp/test-repo -cd /tmp/test-repo -git checkout $TERM_BASE_COMMIT - -# Run your agent -python /path/to/my-agent/agent.py -``` - -### 2. Verify the Patch - -```bash -# Check that changes were committed -git log --oneline -1 - -# View the diff -git diff HEAD~1 -``` - -### 3. Run Tests (if available) - -```bash -# Run the repository's test suite to verify the fix -python -m pytest tests/ -x -``` - ---- - -## Submitting via CLI - -### 1. Package Your Agent - -```bash -cd my-agent/ -zip -r ../my-agent.zip . -``` - -The ZIP file must be **โ‰ค 1 MB**. Keep your agent lean โ€” avoid bundling large model weights or datasets. - -### 2. Submit - -```bash -term-cli submit \ - --rpc-url http://chain.platform.network:9944 \ - --hotkey /path/to/hotkey \ - --agent-zip my-agent.zip \ - --name "my-agent" -``` - -### 3. Monitor Progress - -```bash -# Launch the TUI to watch evaluation progress -term-cli --rpc-url http://chain.platform.network:9944 --tab evaluation -``` - ---- - -## Scoring - -Your agent is scored based on: - -| Metric | Weight | Description | -| --- | --- | --- | -| Pass Rate | Primary | Percentage of SWE-bench tasks solved | -| Difficulty Bonus | Weighted | Hard tasks contribute more to score | -| LLM Judge Score | Modifier | Code quality assessed by LLM reviewers | -| Execution Time | Tiebreaker | Faster solutions preferred at equal scores | - -The final weight is calculated as `pass_rate ร— 10,000` (scaled to integer) and submitted to Bittensor. - ---- - -## Rate Limits - -- **1 submission per 3 epochs** per miner hotkey -- Submitting more frequently results in automatic rejection at the `validate()` stage -- Plan your submissions carefully โ€” iterate locally before submitting - ---- - -## Common Errors and Troubleshooting - -| Error | Cause | Solution | -| --- | --- | --- | -| `submission exceeds maximum task count` | Too many task results in submission | Ensure results match the active dataset (50 tasks) | -| `epoch rate limit` | Submitted too recently | Wait at least 3 epochs between submissions | -| `package_zip exceeds 1MB` | Agent ZIP too large | Remove unnecessary files, use `.gitignore` patterns | -| `invalid signature` | Wrong hotkey or corrupted signature | Verify your hotkey path and ensure it is registered | -| `empty agent_hash` | Missing agent hash in submission | Ensure the CLI computes the hash before submitting | -| `basilica_instance is empty` | Missing executor metadata | Check your CLI version and RPC connectivity | -| `failed to deserialize submission` | Malformed submission payload | Update to the latest CLI version | -| LLM API errors | API key invalid or rate limited | Verify `OPENAI_API_KEY` or equivalent is set correctly | - -### Debugging Tips - -1. **Check the leaderboard** โ€” Use `term-cli --tab leaderboard` to see if your submission was scored -2. **Review agent logs** โ€” Use `term-cli --tab evaluation` to see per-task results -3. **Test locally first** โ€” Always validate your agent against sample tasks before submitting -4. **Monitor network health** โ€” Use `term-cli --tab network` to verify validators are online diff --git a/docs/miner/submission.md b/docs/miner/submission.md deleted file mode 100644 index ec88c68e7..000000000 --- a/docs/miner/submission.md +++ /dev/null @@ -1,188 +0,0 @@ -# Submission Guide - -This document covers the submission lifecycle, naming conventions, versioning, and the security review process for Term Challenge. - ---- - -## Submission Lifecycle - -```mermaid -flowchart TB - Register[Register Submission Name] --> Version[Version Assignment] - Version --> Upload[Upload Agent ZIP] - Upload --> Validate[WASM Validation] - Validate --> Review[Security Review] - Review --> LLM[LLM Code Review ร—3] - Review --> AST[AST Structural Review ร—3] - LLM --> Aggregate[Score Aggregation] - AST --> Aggregate - Aggregate --> Evaluate[SWE-bench Evaluation] - Evaluate --> Score[Final Score] - Score --> Weight[Weight Submission to Bittensor] -``` - ---- - -## Submission Naming - -### First-Register-Owns - -Submission names follow a **first-register-owns** policy: - -- The first miner to submit with a given name **owns** that name permanently -- Subsequent submissions from the same hotkey under that name create new versions -- Other miners **cannot** use a name that is already registered to a different hotkey -- Names are case-sensitive and must be non-empty - -### Naming Conventions - -- Use lowercase alphanumeric characters and hyphens: `my-agent-v2` -- Avoid special characters or spaces -- Choose a descriptive, unique name for your agent - ---- - -## Versioning - -### Auto-Increment - -Each submission under a registered name automatically receives an incrementing version number: - -| Submission | Version | Notes | -| --- | --- | --- | -| First submission of `my-agent` | `1` | Name registered to your hotkey | -| Second submission of `my-agent` | `2` | Auto-incremented | -| Third submission of `my-agent` | `3` | Auto-incremented | - -### History Tracking - -The network maintains a complete version history for each submission name: - -- All previous versions remain stored on-chain -- Each version includes the agent hash, epoch, and evaluation results -- You can query historical versions via the CLI or RPC - -### Storage Keys - -| Key Format | Content | -| --- | --- | -| `agent_code::` | Raw ZIP package bytes (โ‰ค 1 MB) | -| `agent_hash::` | SHA256 hash of the agent package | -| `agent_logs::` | Serialized evaluation logs (โ‰ค 256 KB) | - ---- - -## Security Review Process - -Every submission undergoes a two-phase security review before evaluation. This ensures submitted agent code is safe to execute and structurally sound. - -### Phase 1: LLM Code Review - -```mermaid -flowchart LR - Sub[Submission] --> Assign1[Assign 3 LLM Reviewers] - Assign1 --> R1[Reviewer 1: LLM Analysis] - Assign1 --> R2[Reviewer 2: LLM Analysis] - Assign1 --> R3[Reviewer 3: LLM Analysis] - R1 --> Score1[Score 0.0โ€“1.0] - R2 --> Score2[Score 0.0โ€“1.0] - R3 --> Score3[Score 0.0โ€“1.0] - Score1 & Score2 & Score3 --> Avg[Average Score] -``` - -Three validators are deterministically selected to perform LLM-based code review: - -- The LLM analyzes the agent code for security issues, code quality, and correctness -- Each reviewer produces a score between 0.0 and 1.0 -- Scores below 0.5 flag the task result as failed -- The LLM judge URL is configured via `ChallengeParams.llm_judge_url` - -### Phase 2: AST Structural Review - -Three additional validators perform AST-based structural validation: - -- Parses the Python code into an abstract syntax tree -- Checks for prohibited patterns (e.g., network access outside allowed APIs, filesystem escape attempts) -- Validates the agent structure matches expected conventions -- Each reviewer produces a pass/fail score - -### Validator Selection - -Reviewers are selected deterministically using a seed derived from the submission ID: - -- **6 total reviewers**: 3 for LLM review + 3 for AST review -- Selection is based on the `ReviewAssignmentMessage` with a deterministic `seed` field -- All validators can independently verify the assignment is correct - -### Timeout and Replacement - -If a reviewer does not respond within the configured timeout: - -1. The reviewer's slot is marked as timed out -2. A `ReviewDeclineMessage` is broadcast (or inferred from timeout) -3. A replacement validator is selected from the remaining validator pool -4. The replacement validator performs the same review type - ---- - -## Review Stages and Timing - -| Stage | Duration | Description | -| --- | --- | --- | -| Submission | Immediate | Agent ZIP uploaded and broadcast to validators | -| Validation | ~1 block | WASM `validate()` checks signatures, rate limits, metadata | -| Consensus | ~2โ€“3 blocks | >50% validator approval required | -| LLM Review | ~30โ€“60s | 3 validators perform LLM code analysis | -| AST Review | ~10โ€“30s | 3 validators perform structural analysis | -| Evaluation | ~5โ€“15 min | Agent executed against 50 SWE-bench tasks | -| Scoring | ~1 block | Aggregate score computed and weight proposed | -| Weight Submission | Epoch boundary | Weights submitted to Bittensor chain | - ---- - -## Submission Constraints - -| Constraint | Value | Enforced By | -| --- | --- | --- | -| Package size | โ‰ค 1 MB (1,048,576 bytes) | `validate()` + `agent_storage` | -| Log size | โ‰ค 256 KB (262,144 bytes) | `agent_storage` | -| Task output preview | โ‰ค 4 KB (4,096 bytes) per task | `agent_storage::truncate_output` | -| Rate limit | 1 per 3 epochs | `validate()` via `last_submission` key | -| Max tasks | 256 | `validate()` + `evaluate()` | -| Signature | sr25519 (SS58 prefix 42) | `validate()` | -| Required fields | `agent_hash`, `miner_hotkey`, `signature`, `package_zip`, `basilica_instance`, `executor_url`, `executor_token` | `validate()` | - ---- - -## Troubleshooting Rejected Submissions - -### Validation Failures - -| Rejection Reason | Cause | Fix | -| --- | --- | --- | -| Empty `agent_hash` | Hash not computed | Ensure CLI computes hash before submission | -| Empty `miner_hotkey` | Hotkey not provided | Pass `--hotkey` flag to CLI | -| Empty `signature` | Signing failed | Check hotkey file permissions and format | -| Empty `package_zip` | ZIP file not loaded | Verify the ZIP file path exists | -| `package_zip` > 1 MB | Agent too large | Remove unnecessary files from the ZIP | -| Empty `basilica_instance` | Missing executor config | Update CLI to latest version | -| Empty `executor_url` | Missing executor config | Update CLI to latest version | -| Epoch rate limit | Submitted within 3 epochs | Wait for the cooldown period | -| Task count mismatch | Results don't match dataset | Ensure you solve all tasks in the active dataset | -| Invalid task result | Score out of range or empty ID | Validate scores are in [0.0, 1.0] range | - -### Review Failures - -| Issue | Cause | Fix | -| --- | --- | --- | -| Low LLM review score | Code quality concerns | Improve code structure, add comments, remove dead code | -| AST validation failure | Prohibited patterns detected | Remove disallowed imports or system calls | -| All reviewers timed out | Network congestion | Resubmit during lower traffic periods | - -### Post-Submission Issues - -| Issue | Cause | Fix | -| --- | --- | --- | -| Score is 0 | All tasks failed | Debug agent locally against SWE-bench tasks | -| Score decreasing | Decay mechanism active | Submit improved agent before grace period expires | -| Not on leaderboard | Submission not yet evaluated | Wait for evaluation to complete; check progress tab | diff --git a/docs/operations/validator.md b/docs/operations/validator.md new file mode 100644 index 000000000..b5c861c77 --- /dev/null +++ b/docs/operations/validator.md @@ -0,0 +1,173 @@ +# Validator Operations + +This guide covers validator deployment, configuration, monitoring, and lifecycle operations. Validators run directly on host systems with **WASM-first execution**; Docker is used only for integration tests. + +## Quick Start + +```bash +git clone https://github.com/PlatformNetwork/platform.git +cd platform +cp .env.example .env +# Edit .env: add your VALIDATOR_SECRET_KEY (BIP39 mnemonic) +mkdir -p data +cargo build --release --bin validator-node +./target/release/validator-node --data-dir ./data --secret-key "${VALIDATOR_SECRET_KEY}" +``` + +## Operational Topology + +```mermaid +flowchart LR + Validator[Validator Node] --> Mesh[(libp2p Mesh)] + Mesh --> DHT[(DHT + consensus state)] + Validator --> Runtime[WASM Runtime] + Validator --> Logs[Audit Logs] + Validator --> BT[Bittensor Chain] +``` + +## Requirements + +### Hardware + +| Resource | Minimum | Recommended | +| --- | --- | --- | +| CPU | 4 vCPU | 8 vCPU | +| RAM | 16 GB | 32 GB | +| Storage | 250 GB SSD | 500 GB NVMe | +| Network | 100 Mbps | 100 Mbps | + +### Network + +| Port | Protocol | Usage | Required | +| --- | --- | --- | --- | +| 9000/tcp | libp2p | Validator P2P mesh | Yes | +| 8545/tcp | HTTP | JSON-RPC API | Optional | + +### Bittensor + +- Minimum stake: 1000 TAO. +- Registered hotkey on subnet. +- BIP39 mnemonic or hex private key. + +## Configuration + +### Environment Variables + +| Variable | Description | Default | Required | +| --- | --- | --- | --- | +| `VALIDATOR_SECRET_KEY` | BIP39 mnemonic or hex private key | - | Yes | +| `SUBTENSOR_ENDPOINT` | Bittensor RPC endpoint | `wss://entrypoint-finney.opentensor.ai:443` | No | +| `NETUID` | Subnet UID | `100` | No | +| `RUST_LOG` | Log level | `info` | No | +| `P2P_LISTEN_ADDR` | libp2p listen address | `/ip4/0.0.0.0/tcp/9000` | No | +| `BOOTSTRAP_PEERS` | Bootstrap peers (comma-separated) | - | Yes | + +### Notes + +- `BOOTSTRAP_PEERS` is required; validators will refuse to start without peers. +- Secrets should be injected via environment variables or secret managers. + +## Consensus & Epoch Timeline + +```mermaid +sequenceDiagram + participant V as Validator + participant P2P as libp2p Mesh + participant BT as Bittensor + + V->>P2P: Commit(weight hash) + V->>P2P: Reveal(weights) + P2P-->>V: Aggregated weights + state hash + V->>BT: Submit weights +``` + +## Monitoring + +### Logs + +```bash +tail -f ./data/validator.log +``` + +### JSON-RPC Health Check + +```bash +curl -X POST http://localhost:8545/rpc \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc":"2.0","method":"system_health","id":1}' +``` + +Expected response: + +```json +{ + "jsonrpc": "2.0", + "result": { + "peers": 5, + "is_synced": true, + "block_height": 12345 + }, + "id": 1 +} +``` + +## Operations Playbooks + +### Upgrade + +```bash +cargo build --release --bin validator-node +./target/release/validator-node --data-dir ./data --secret-key "${VALIDATOR_SECRET_KEY}" +``` + +### Restart After Divergence + +```bash +rm -rf ./data/distributed-db +./target/release/validator-node --data-dir ./data --secret-key "${VALIDATOR_SECRET_KEY}" +``` + +### Stop + +```bash +rm -rf ./data/distributed-db +``` + +## Testing (Docker Harness Only) + +Docker is required only for integration tests. Use the comprehensive test harness if you need Docker-backed evaluation flows: + +```bash +./scripts/test-comprehensive.sh +``` + +## Troubleshooting + +### No Peers Connected + +- Verify port `9000/tcp` is reachable from the public internet. +- Ensure `BOOTSTRAP_PEERS` lists at least one valid peer multiaddr. + +### Bittensor Connection Issues + +```bash +curl -I wss://entrypoint-finney.opentensor.ai:443 +``` + +Use an alternative endpoint if needed: + +```bash +SUBTENSOR_ENDPOINT=wss://subtensor.api.opentensor.ai:443 +``` + +## Security Best Practices + +- Restrict `.env` permissions: `chmod 600 .env`. +- Use a firewall and limit ingress to required ports. +- Rotate keys and monitor logs for anomalies. + +## References + +- [Architecture](../architecture.md) +- [Security Model](../security.md) +- [Validator Guide](../validator.md) diff --git a/docs/security.md b/docs/security.md new file mode 100644 index 000000000..ff065be1e --- /dev/null +++ b/docs/security.md @@ -0,0 +1,76 @@ +# Security Model + +Platform prioritizes deterministic evaluation, validator integrity, and minimal trust assumptions. Security controls are enforced at the network, consensus, and runtime layers. + +## Security Pillars + +1. **Stake-weighted validator set**: validators must meet minimum stake requirements. +2. **Signed P2P messages**: every submission, evaluation, and vote is signed with the validator hotkey. +3. **PBFT-style consensus**: final state is accepted only with >= 2f + 1 approvals. +4. **WASM sandbox**: challenge execution is isolated with strict runtime policies. +5. **Auditability**: state changes and challenge updates are anchored to chain epochs. + +## Threat Model + +- **Byzantine validators**: mitigated by quorum thresholds and stake weighting. +- **Sybil attempts**: minimum stake requirements and metagraph verification. +- **Challenge tampering**: challenge metadata is signed and consensus-approved. +- **Resource exhaustion**: WASM runtime enforces CPU, memory, and I/O caps. + +## Security Architecture + +```mermaid +flowchart TB + subgraph Network + P2P[libp2p Mesh] + DHT[DHT] + P2P --> DHT + end + + subgraph Validator + Signed[Signed Messages] + Consensus[PBFT Consensus] + Runtime[WASM Runtime] + Audit[Audit Logs] + end + + P2P --> Signed + Signed --> Consensus + Consensus --> Runtime + Runtime --> Audit +``` + +## Secure Runtime (WASM) + +```mermaid +flowchart LR + Validator[Validator Node] --> Runtime[WASM Runtime] + Runtime --> Policy[Runtime Policy + Limits] + Runtime --> HostFns[Whitelisted Host Functions] + Runtime --> Audit[Audit Logs] + Policy --> Runtime + HostFns --> Runtime + Runtime -->|Deterministic outputs| Validator +``` + +## Security Controls Matrix + +| Layer | Control | Outcome | +| --- | --- | --- | +| Identity | Bittensor hotkey signatures | Non-repudiation & replay protection | +| Network | libp2p gossipsub + DHT | Decentralized data propagation | +| Consensus | PBFT-style approvals | Deterministic state finalization | +| Runtime | WASM sandbox + resource caps | Deterministic isolation | +| Operations | Key management + monitoring | Reduced operational risk | + +## Operational Controls + +- **Key management**: secrets via env vars or secret managers. +- **Network controls**: firewall rules limit ingress to required ports. +- **Monitoring**: health checks and log monitoring detect consensus drift. + +## References + +- [Architecture](architecture.md) +- [Validator Operations](operations/validator.md) +- [Challenges](challenges.md) diff --git a/docs/validator.md b/docs/validator.md new file mode 100644 index 000000000..d743bbbdc --- /dev/null +++ b/docs/validator.md @@ -0,0 +1,150 @@ +# Validator Guide + +This guide explains how to run a Platform validator node on the Bittensor network. Production validators execute challenges in the WASM runtime, while Docker is reserved for integration tests only. + +## Key Features + +- **No GPU required**: validators run on CPU servers. +- **No third-party APIs**: no external model keys needed. +- **WASM-first runtime**: deterministic challenge execution. +- **P2P-only consensus**: libp2p mesh for all validator traffic. + +## Validator Lifecycle + +```mermaid +flowchart LR + Start[Start validator] --> Sync[Sync metagraph + checkpoints] + Sync --> Evaluate[Run WASM challenges] + Evaluate --> Commit[Commit weights] + Commit --> Reveal[Reveal weights] + Reveal --> Submit[Submit weights to Bittensor] + Submit --> Observe[Monitor + audit] +``` + +## P2P Architecture + +Platform validators run as a fully peer-to-peer network with no centralized fallback services. All validator-to-validator traffic happens over libp2p on port 9000, and consensus data is exchanged directly between peers. + +- **Peer discovery**: validators connect to the libp2p mesh and maintain a live peer set. +- **State sync**: checkpoints, block proposals, and commits are shared only through the P2P network. +- **No central coordinator**: there are no HTTP relays or centralized aggregators for consensus. +- **Bittensor anchoring**: the metagraph provides stake and identity, but consensus payloads flow through P2P. + +## Weight-Based Consensus Flow + +Consensus is driven by validator weights derived from challenge evaluations. The validator set is stake-weighted, meaning higher-stake hotkeys carry more voting power when aggregating challenge results. + +1. **Stake-weighted validator set**: each validatorโ€™s voting power is proportional to its Bittensor stake in the metagraph. +2. **Challenge evaluation**: validators execute active challenges, producing raw scores. +3. **Commit-reveal weights**: validators commit weight vectors, then reveal them. +4. **Epoch aggregation**: stake-weighted aggregation produces canonical weights. +5. **Consensus agreement**: validators agree on the aggregated weights and state hash. +6. **Weight submission**: finalized weights are submitted back to Bittensor. + +```mermaid +sequenceDiagram + participant V as Validator + participant P2P as libp2p Mesh + participant BT as Bittensor + + V->>P2P: Commit(weight hash) + V->>P2P: Reveal(weights) + P2P-->>V: Aggregated weights + state hash + V->>BT: Submit weights +``` + +## Quick Start + +```bash +git clone https://github.com/PlatformNetwork/platform.git +cd platform +cp .env.example .env +# Edit .env: add your VALIDATOR_SECRET_KEY (BIP39 mnemonic) +mkdir -p data +cargo build --release --bin validator-node +./target/release/validator-node --data-dir ./data --secret-key "${VALIDATOR_SECRET_KEY}" +``` + +## Requirements + +### Hardware + +| Resource | Minimum | Recommended | +| --- | --- | --- | +| CPU | 4 vCPU | 8 vCPU | +| RAM | 16 GB | 32 GB | +| Storage | 250 GB SSD | 500 GB NVMe | +| Network | 100 Mbps | 100 Mbps | + +### Network + +**Port 9000/tcp must be open** for P2P communication. + +### Software + +- Linux (Ubuntu 22.04+ recommended) + +### Bittensor + +- **Minimum stake**: 1000 TAO. +- Registered hotkey on subnet. +- BIP39 mnemonic or hex private key. + +## Configuration Reference + +### Environment Variables + +| Variable | Description | Default | Required | +| --- | --- | --- | --- | +| `VALIDATOR_SECRET_KEY` | BIP39 mnemonic or hex private key | - | Yes | +| `SUBTENSOR_ENDPOINT` | Bittensor RPC endpoint | `wss://entrypoint-finney.opentensor.ai:443` | No | +| `NETUID` | Subnet UID | `100` | No | +| `RUST_LOG` | Log level (`debug`, `info`, `warn`, `error`) | `info` | No | + +### Network Ports + +| Port | Protocol | Usage | Required | +| --- | --- | --- | --- | +| 9000/tcp | libp2p | P2P validator communication | Yes | +| 8545/tcp | HTTP | JSON-RPC API | No | + +## Monitoring + +### Check Validator Status + +```bash +# View logs (if running directly) +tail -f ./data/validator.log +``` + +### JSON-RPC Health Check + +```bash +curl -X POST http://localhost:8545/rpc \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc":"2.0","method":"system_health","id":1}' +``` + +Expected response: + +```json +{ + "jsonrpc": "2.0", + "result": { + "peers": 5, + "is_synced": true, + "block_height": 12345 + }, + "id": 1 +} +``` + +## Docker Policy (Test-Only) + +Docker is required only for integration tests. Use `./scripts/test-comprehensive.sh` for Docker-backed evaluation flows. + +## References + +- [Validator Operations](operations/validator.md) +- [Architecture](architecture.md) +- [Security Model](security.md) diff --git a/docs/validator/setup.md b/docs/validator/setup.md deleted file mode 100644 index 3b2339068..000000000 --- a/docs/validator/setup.md +++ /dev/null @@ -1,356 +0,0 @@ -# Validator Setup Guide - -This guide covers setting up and operating a validator node for the Term Challenge subnet on the Platform-v2 network. - ---- - -## Hardware Requirements - -| Resource | Minimum | Recommended | Notes | -| --- | --- | --- | --- | -| CPU | 4 vCPU | 8 vCPU | WASM execution is CPU-bound | -| RAM | 16 GB | 32 GB | WASM runtime + P2P state | -| Storage | 250 GB SSD | 500 GB NVMe | Agent storage grows over time | -| Network | 100 Mbps | 100 Mbps | P2P mesh requires stable connectivity | -| OS | Ubuntu 22.04+ | Ubuntu 24.04 | Any Linux with glibc 2.35+ | - ---- - -## Software Prerequisites - -| Software | Version | Purpose | -| --- | --- | --- | -| Rust | 1.90+ | Building validator-node and WASM modules | -| Git | 2.30+ | Source code management | -| OpenSSL | 3.0+ | TLS for Bittensor RPC connections | -| `btcli` | Latest | Bittensor key management and registration | - -### Install Rust - -```bash -curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -source $HOME/.cargo/env -rustup target add wasm32-unknown-unknown -``` - -### Install btcli - -```bash -pip install bittensor -``` - ---- - -## Bittensor Prerequisites - -1. **Generate a hotkey** (if you don't have one): - ```bash - btcli wallet new_hotkey --wallet.name my_validator --wallet.hotkey default - ``` - -2. **Register on the subnet**: - ```bash - btcli subnet register --netuid --wallet.name my_validator --wallet.hotkey default - ``` - -3. **Stake TAO** (minimum 1000 TAO required): - ```bash - btcli stake add --wallet.name my_validator --wallet.hotkey default --amount 1000 - ``` - ---- - -## Installation - -### 1. Clone Platform-v2 - -```bash -git clone https://github.com/PlatformNetwork/platform-v2.git -cd platform-v2 -``` - -### 2. Configure Environment - -```bash -cp .env.example .env -``` - -Edit `.env` with your validator configuration: - -```bash -# REQUIRED: Your validator secret key (BIP39 mnemonic or hex-encoded 32 bytes) -VALIDATOR_SECRET_KEY=your_secret_key_here - -# Optional: Slack webhook for monitoring notifications -# SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxx/xxx/xxx -``` - -### 3. Build the Validator - -```bash -cargo build --release --bin validator-node -``` - -### 4. Create Data Directory - -```bash -mkdir -p data -``` - ---- - -## Configuration - -### Environment Variables - -| Variable | Description | Default | Required | -| --- | --- | --- | --- | -| `VALIDATOR_SECRET_KEY` | BIP39 mnemonic or hex private key | โ€” | Yes | -| `SUBTENSOR_ENDPOINT` | Bittensor RPC endpoint | `wss://entrypoint-finney.opentensor.ai:443` | No | -| `NETUID` | Subnet UID | `100` | No | -| `DATA_DIR` | Directory for validator state | `./data` | No | -| `RPC_PORT` | JSON-RPC API port | `8545` | No | -| `P2P_PORT` | libp2p mesh port | `9000` | No | -| `LOG_LEVEL` | Logging verbosity | `info` | No | -| `SLACK_WEBHOOK_URL` | Slack notifications webhook | โ€” | No | - -### Network Ports - -| Port | Protocol | Usage | Required | -| --- | --- | --- | --- | -| 9000/tcp | libp2p | Validator P2P mesh communication | Yes | -| 8545/tcp | HTTP | JSON-RPC API for CLI and miners | Optional | - -Ensure these ports are open in your firewall: - -```bash -# UFW example -sudo ufw allow 9000/tcp -sudo ufw allow 8545/tcp -``` - ---- - -## Running a Validator Node - -### Direct Execution - -```bash -./target/release/validator-node \ - --data-dir ./data \ - --secret-key "${VALIDATOR_SECRET_KEY}" -``` - -### With systemd (Recommended for Production) - -Create `/etc/systemd/system/platform-validator.service`: - -```ini -[Unit] -Description=Platform-v2 Validator Node -After=network-online.target -Wants=network-online.target - -[Service] -Type=simple -User=validator -Group=validator -WorkingDirectory=/opt/platform-v2 -ExecStart=/opt/platform-v2/target/release/validator-node --data-dir /opt/platform-v2/data --secret-key "${VALIDATOR_SECRET_KEY}" -Restart=always -RestartSec=10 -LimitNOFILE=65535 -EnvironmentFile=/opt/platform-v2/.env - -[Install] -WantedBy=multi-user.target -``` - -Enable and start: - -```bash -sudo systemctl daemon-reload -sudo systemctl enable platform-validator -sudo systemctl start platform-validator -``` - ---- - -## WASM Module Management - -The validator automatically loads WASM challenge modules. To update the term-challenge module: - -### Build the WASM Module - -```bash -# In the term-challenge repository -cargo build --release --target wasm32-unknown-unknown -p term-challenge-wasm - -# Copy to the validator's challenge directory -cp target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm \ - /opt/platform-v2/data/challenges/ -``` - -### Download via Platform CLI - -```bash -platform download term-challenge -``` - ---- - -## Monitoring and Health Checks - -### Health Endpoint - -```bash -curl http://localhost:8545/health -``` - -Expected response: - -```json -{ - "success": true, - "data": { - "status": "healthy", - "version": "0.1.0", - "uptime_secs": 86400 - } -} -``` - -### Status Endpoint - -```bash -curl http://localhost:8545/status -``` - -Returns current block height, epoch, validator count, and challenge count. - -### Epoch Information - -```bash -curl http://localhost:8545/epoch -``` - -Returns current epoch, phase (evaluation/commit/reveal), and blocks until next phase. - -### Using term-cli - -```bash -# Monitor network health -term-cli --rpc-url http://localhost:8545 --tab network - -# View leaderboard -term-cli --rpc-url http://localhost:8545 --tab leaderboard -``` - -### Log Monitoring - -```bash -# Follow validator logs -journalctl -u platform-validator -f - -# Filter for errors -journalctl -u platform-validator --since "1 hour ago" | grep -i error -``` - -### Key Metrics to Monitor - -| Metric | Healthy Range | Action if Unhealthy | -| --- | --- | --- | -| Uptime | > 99% | Check systemd restart logs | -| Peer count | โ‰ฅ 3 | Verify P2P port is open | -| Block height | Increasing | Check Bittensor RPC connectivity | -| Epoch progression | Advancing | Verify chain sync | -| Memory usage | < 80% of available | Increase RAM or check for leaks | -| Disk usage | < 80% of available | Prune old data or expand storage | - ---- - -## Validator Responsibilities - -As a Term Challenge validator, your node performs these duties: - -1. **Submission Validation** โ€” Run WASM `validate()` on incoming submissions -2. **Security Review** โ€” Perform LLM and AST reviews when assigned -3. **Agent Evaluation** โ€” Execute agents against SWE-bench tasks via term-executor -4. **Log Consensus** โ€” Propose and vote on agent evaluation logs -5. **Weight Submission** โ€” Submit consensus weights to Bittensor at epoch boundaries -6. **State Sync** โ€” Maintain synchronized state with other validators via P2P - ---- - -## Troubleshooting - -### Validator Not Connecting to Peers - -| Symptom | Cause | Solution | -| --- | --- | --- | -| 0 peers | Firewall blocking P2P port | Open port 9000/tcp | -| 0 peers | Incorrect boot nodes | Verify network configuration | -| Peers dropping | Unstable network | Check bandwidth and latency | -| Peers dropping | Clock skew | Sync system clock with NTP | - -### Bittensor Sync Issues - -| Symptom | Cause | Solution | -| --- | --- | --- | -| Block height not advancing | RPC endpoint down | Try alternate `SUBTENSOR_ENDPOINT` | -| Stake not detected | Registration not confirmed | Verify with `btcli wallet overview` | -| Weights not submitted | Insufficient stake | Ensure minimum 1000 TAO staked | - -### WASM Module Issues - -| Symptom | Cause | Solution | -| --- | --- | --- | -| Challenge not loading | Missing WASM file | Rebuild and copy the `.wasm` file | -| Evaluation failures | Outdated WASM module | Update to latest term-challenge version | -| High memory usage | Large submissions | Monitor and set memory limits | - -### Common Log Messages - -| Log Message | Meaning | Action | -| --- | --- | --- | -| `Validator sync complete` | Successfully synced from metagraph | Normal operation | -| `Submission validated` | A submission passed WASM validation | Normal operation | -| `Epoch transition` | New epoch started | Normal operation | -| `Weight submission failed` | Could not submit weights to chain | Check Bittensor connectivity | -| `Review assignment received` | Assigned to review a submission | Normal operation | -| `Review timeout` | Did not complete review in time | Check system resources | - ---- - -## Security Considerations - -- **Never share your `VALIDATOR_SECRET_KEY`** โ€” it controls your validator identity and stake -- **Keep the `.env` file permissions restricted**: `chmod 600 .env` -- **Run as a non-root user** โ€” create a dedicated `validator` user -- **Enable automatic updates** for OS security patches -- **Monitor for unauthorized access** to the RPC port (consider binding to localhost if not needed externally) -- **Back up your data directory** regularly โ€” it contains validator state and consensus data - ---- - -## Upgrading - -### Update Platform-v2 - -```bash -cd /opt/platform-v2 -git pull origin main -cargo build --release --bin validator-node -sudo systemctl restart platform-validator -``` - -### Update Term Challenge WASM - -```bash -cd /opt/term-challenge -git pull origin main -cargo build --release --target wasm32-unknown-unknown -p term-challenge-wasm -cp target/wasm32-unknown-unknown/release/term_challenge_wasm.wasm \ - /opt/platform-v2/data/challenges/ -sudo systemctl restart platform-validator -``` diff --git a/docs/validator_wasm_audit.md b/docs/validator_wasm_audit.md new file mode 100644 index 000000000..d391c35a7 --- /dev/null +++ b/docs/validator_wasm_audit.md @@ -0,0 +1,42 @@ +# Validator/Core/P2P/WASM Audit Notes + +## Scope + +Reviewed: `bins/validator-node`, `crates/core`, `crates/p2p-consensus`, `crates/challenge-registry`, `crates/wasm-runtime-interface`. + +## Key Findings + +### Validator Node +- `bins/validator-node` wires consensus + storage with WASM challenge execution integrated into the validator node runtime path. + +### Challenge Registry +- Registry entries store WASM module metadata as primary. `ChallengeEntry` includes WASM module hash, path, and network policy. +- `discovery` supports WASM module registry and signed P2P announcements for challenge distribution. + +### Core State +- `core::ChainState` includes WASM `wasm_challenge_configs` for challenge configuration. +- `core::ChallengeConfig` stores WASM module metadata (hash/path/entrypoint/policy) for WASM-only evaluation. + +### P2P Consensus +- `p2p-consensus::ChainState` stores `ChallengeConfig` with WASM module metadata and weight allocation. +- Consensus engine is PBFT-style and uses validator stake data from `ValidatorSet`. Stake is taken from heartbeats unless verified stake is set (metagraph refresh uses `set_verified_stake`), which is a potential gap if verified stakes are not enforced. + +### WASM Runtime Interface +- Runtime is strict and well-structured: `NetworkPolicy` with validation, explicit host functions, request limits, and audit log hooks. +- No apparent recursion; resource caps are enforced via wasmtime `StoreLimits` and request limits. +- Runtime interface is integrated into the validator execution path. + +## Cleanup / Follow-up Recommendations + +1. **Unify challenge configs** + - Ensure all challenge configs in `core::ChainState` and `p2p-consensus::ChainState` use WASM-only representations with `WasmChallengeConfig` metadata and network policy. +2. **Registry WASM-only** + - `ChallengeEntry` should store WASM module metadata as primary. + - `discovery` should focus on WASM module registry or signed P2P announcements. +3. **Consensus state challenge metadata** + - Ensure `p2p-consensus::ChallengeConfig` uses WASM module metadata (hash/path/entrypoint/policy) for WASM-only evaluation. + +## Suggested Next Steps + +- Align registry and core state to store WASM metadata only, with migration of existing state. +- Continue hardening WASM runtime execution policies and audit logging. diff --git a/pr_diff_full.txt b/pr_diff_full.txt new file mode 100644 index 000000000..e77711b01 --- /dev/null +++ b/pr_diff_full.txt @@ -0,0 +1,1191 @@ +diff --git a/Cargo.lock b/Cargo.lock +index 05e80ff..289dca5 100644 +--- a/Cargo.lock ++++ b/Cargo.lock +@@ -1930,6 +1930,27 @@ dependencies = [ + "dirs-sys-next", + ] + ++[[package]] ++name = "dirs" ++version = "5.0.1" ++source = "registry+https://github.com/rust-lang/crates.io-index" ++checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" ++dependencies = [ ++ "dirs-sys", ++] ++ ++[[package]] ++name = "dirs-sys" ++version = "0.4.1" ++source = "registry+https://github.com/rust-lang/crates.io-index" ++checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" ++dependencies = [ ++ "libc", ++ "option-ext", ++ "redox_users", ++ "windows-sys 0.48.0", ++] ++ + [[package]] + name = "dirs-sys-next" + version = "0.1.2" +@@ -4815,6 +4836,12 @@ dependencies = [ + "vcpkg", + ] + ++[[package]] ++name = "option-ext" ++version = "0.2.0" ++source = "registry+https://github.com/rust-lang/crates.io-index" ++checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" ++ + [[package]] + name = "parity-bip39" + version = "2.0.1" +@@ -5123,6 +5150,24 @@ dependencies = [ + "serde", + ] + ++[[package]] ++name = "platform-cli" ++version = "0.1.0" ++dependencies = [ ++ "anyhow", ++ "chrono", ++ "clap", ++ "dirs", ++ "reqwest 0.12.25", ++ "semver", ++ "serde", ++ "serde_json", ++ "tokio", ++ "toml 0.8.23", ++ "tracing", ++ "tracing-subscriber 0.3.22", ++] ++ + [[package]] + name = "platform-core" + version = "0.1.0" +diff --git a/Cargo.toml b/Cargo.toml +index 9ab2961..df08de4 100644 +--- a/Cargo.toml ++++ b/Cargo.toml +@@ -18,6 +18,7 @@ members = [ + "bins/validator-node", + "bins/utils", + "bins/mock-subtensor", ++ "bins/platform-cli", + "tests", + "challenges/term-challenge", + "challenges/term-challenge-wasm", +diff --git a/bins/platform-cli/Cargo.toml b/bins/platform-cli/Cargo.toml +new file mode 100644 +index 0000000..f7e94f0 +--- /dev/null ++++ b/bins/platform-cli/Cargo.toml +@@ -0,0 +1,23 @@ ++[package] ++name = "platform-cli" ++version.workspace = true ++edition.workspace = true ++description = "Platform CLI โ€” download and manage challenge CLIs" ++ ++[[bin]] ++name = "platform" ++path = "src/main.rs" ++ ++[dependencies] ++clap = { workspace = true } ++reqwest = { workspace = true, features = ["json"] } ++serde = { workspace = true } ++serde_json = { workspace = true } ++tokio = { workspace = true } ++anyhow = { workspace = true } ++tracing = { workspace = true } ++tracing-subscriber = { workspace = true } ++chrono = { workspace = true } ++toml = "0.8" ++dirs = "5" ++semver = { version = "1", features = ["serde"] } +diff --git a/bins/platform-cli/src/main.rs b/bins/platform-cli/src/main.rs +new file mode 100644 +index 0000000..d899b3c +--- /dev/null ++++ b/bins/platform-cli/src/main.rs +@@ -0,0 +1,633 @@ ++//! Platform CLI โ€” download and manage challenge CLIs ++//! ++//! Provides subcommands to download, update, list, run, and configure ++//! challenge CLI binaries from GitHub releases. ++ ++use anyhow::{Context, Result}; ++use chrono::{DateTime, Utc}; ++use clap::{Parser, Subcommand}; ++use serde::{Deserialize, Serialize}; ++use std::collections::HashMap; ++use std::path::{Path, PathBuf}; ++use tracing::{debug, info}; ++ ++// ==================== Constants ==================== ++ ++const PLATFORM_DIR_NAME: &str = ".platform"; ++const CONFIG_FILE_NAME: &str = "platform.toml"; ++const VERSIONS_FILE_NAME: &str = "versions.json"; ++const BIN_DIR_NAME: &str = "bin"; ++const GITHUB_API_BASE: &str = "https://api.github.com"; ++ ++// ==================== Config ==================== ++ ++#[derive(Debug, Serialize, Deserialize)] ++struct PlatformConfig { ++ network: NetworkConfig, ++ #[serde(default)] ++ challenges: HashMap, ++} ++ ++#[derive(Debug, Serialize, Deserialize)] ++struct NetworkConfig { ++ rpc_endpoint: String, ++ netuid: u16, ++} ++ ++#[derive(Debug, Clone, Serialize, Deserialize)] ++struct ChallengeConfig { ++ github_repo: String, ++ binary_name: String, ++ command_alias: String, ++ #[serde(default = "default_true")] ++ auto_update: bool, ++} ++ ++fn default_true() -> bool { ++ true ++} ++ ++impl Default for PlatformConfig { ++ fn default() -> Self { ++ let mut challenges = HashMap::new(); ++ challenges.insert( ++ "term-challenge".to_string(), ++ ChallengeConfig { ++ github_repo: "PlatformNetwork/term-challenge".to_string(), ++ binary_name: "term-cli".to_string(), ++ command_alias: "term".to_string(), ++ auto_update: true, ++ }, ++ ); ++ Self { ++ network: NetworkConfig { ++ rpc_endpoint: "wss://chain.platform.network".to_string(), ++ netuid: 100, ++ }, ++ challenges, ++ } ++ } ++} ++ ++// ==================== Version Tracking ==================== ++ ++#[derive(Debug, Serialize, Deserialize)] ++struct VersionInfo { ++ version: String, ++ binary_path: String, ++ installed_at: DateTime, ++ github_repo: String, ++} ++ ++type VersionStore = HashMap; ++ ++// ==================== GitHub API Types ==================== ++ ++#[derive(Debug, Deserialize)] ++struct GitHubRelease { ++ tag_name: String, ++ assets: Vec, ++} ++ ++#[derive(Debug, Deserialize)] ++struct GitHubAsset { ++ name: String, ++ browser_download_url: String, ++} ++ ++// ==================== CLI ==================== ++ ++#[derive(Parser)] ++#[command(name = "platform")] ++#[command(about = "Platform CLI โ€” download and manage challenge CLIs")] ++struct Cli { ++ #[command(subcommand)] ++ command: Commands, ++} ++ ++#[derive(Subcommand)] ++enum Commands { ++ /// Download a challenge CLI binary from GitHub releases ++ Download { ++ /// Name of the challenge to download ++ challenge_name: String, ++ }, ++ /// Check for and install updates for a challenge CLI ++ Update { ++ /// Name of the challenge to update ++ challenge_name: String, ++ }, ++ /// List installed challenge CLIs ++ List, ++ /// Run an installed challenge CLI ++ Run { ++ /// Name of the challenge to run (or a command alias) ++ challenge_name: String, ++ /// Arguments to forward to the challenge CLI ++ #[arg(trailing_var_arg = true, allow_hyphen_values = true)] ++ args: Vec, ++ }, ++ /// Show current platform.toml config ++ Config, ++} ++ ++// ==================== Path Helpers ==================== ++ ++fn platform_dir() -> Result { ++ let home = dirs::home_dir().context("Could not determine home directory")?; ++ Ok(home.join(PLATFORM_DIR_NAME)) ++} ++ ++fn config_path() -> Result { ++ Ok(platform_dir()?.join(CONFIG_FILE_NAME)) ++} ++ ++fn versions_path() -> Result { ++ Ok(platform_dir()?.join(VERSIONS_FILE_NAME)) ++} ++ ++fn bin_dir() -> Result { ++ Ok(platform_dir()?.join(BIN_DIR_NAME)) ++} ++ ++// ==================== Config I/O ==================== ++ ++fn load_config() -> Result { ++ let path = config_path()?; ++ if !path.exists() { ++ info!("Config not found at {}, creating default", path.display()); ++ let config = PlatformConfig::default(); ++ save_config(&config)?; ++ return Ok(config); ++ } ++ let contents = std::fs::read_to_string(&path) ++ .with_context(|| format!("Failed to read config from {}", path.display()))?; ++ let config: PlatformConfig = toml::from_str(&contents) ++ .with_context(|| format!("Failed to parse config at {}", path.display()))?; ++ Ok(config) ++} ++ ++fn save_config(config: &PlatformConfig) -> Result<()> { ++ let path = config_path()?; ++ if let Some(parent) = path.parent() { ++ std::fs::create_dir_all(parent) ++ .with_context(|| format!("Failed to create directory {}", parent.display()))?; ++ } ++ let contents = toml::to_string_pretty(config).context("Failed to serialize config")?; ++ std::fs::write(&path, contents) ++ .with_context(|| format!("Failed to write config to {}", path.display()))?; ++ debug!("Config saved to {}", path.display()); ++ Ok(()) ++} ++ ++// ==================== Version Store I/O ==================== ++ ++fn load_versions() -> Result { ++ let path = versions_path()?; ++ if !path.exists() { ++ return Ok(HashMap::new()); ++ } ++ let contents = std::fs::read_to_string(&path) ++ .with_context(|| format!("Failed to read versions from {}", path.display()))?; ++ let versions: VersionStore = serde_json::from_str(&contents) ++ .with_context(|| format!("Failed to parse versions at {}", path.display()))?; ++ Ok(versions) ++} ++ ++fn save_versions(versions: &VersionStore) -> Result<()> { ++ let path = versions_path()?; ++ if let Some(parent) = path.parent() { ++ std::fs::create_dir_all(parent) ++ .with_context(|| format!("Failed to create directory {}", parent.display()))?; ++ } ++ let contents = ++ serde_json::to_string_pretty(versions).context("Failed to serialize versions")?; ++ std::fs::write(&path, contents) ++ .with_context(|| format!("Failed to write versions to {}", path.display()))?; ++ debug!("Versions saved to {}", path.display()); ++ Ok(()) ++} ++ ++// ==================== Platform Detection ==================== ++ ++fn platform_identifier() -> String { ++ let os = match std::env::consts::OS { ++ "linux" => "linux", ++ "macos" => "darwin", ++ "windows" => "windows", ++ other => other, ++ }; ++ let arch = std::env::consts::ARCH; ++ format!("{}-{}", os, arch) ++} ++ ++fn find_matching_asset(assets: &[GitHubAsset]) -> Option<&GitHubAsset> { ++ let platform = platform_identifier(); ++ debug!("Looking for asset matching platform: {}", platform); ++ ++ assets ++ .iter() ++ .find(|asset| asset.name.contains(&platform)) ++ .or_else(|| { ++ let os = std::env::consts::OS; ++ let arch = std::env::consts::ARCH; ++ assets ++ .iter() ++ .find(|asset| asset.name.contains(os) && asset.name.contains(arch)) ++ }) ++} ++ ++// ==================== GitHub API ==================== ++ ++async fn fetch_latest_release( ++ client: &reqwest::Client, ++ github_repo: &str, ++) -> Result { ++ let url = format!("{}/repos/{}/releases/latest", GITHUB_API_BASE, github_repo); ++ debug!("Fetching latest release from {}", url); ++ ++ let response = client ++ .get(&url) ++ .header("User-Agent", "platform-cli") ++ .header("Accept", "application/vnd.github.v3+json") ++ .send() ++ .await ++ .with_context(|| format!("Failed to fetch releases from {}", url))?; ++ ++ if !response.status().is_success() { ++ let status = response.status(); ++ let body = response ++ .text() ++ .await ++ .unwrap_or_else(|_| "".to_string()); ++ anyhow::bail!( ++ "GitHub API returned {} for {}: {}", ++ status, ++ github_repo, ++ body ++ ); ++ } ++ ++ let release: GitHubRelease = response ++ .json() ++ .await ++ .context("Failed to parse GitHub release response")?; ++ ++ Ok(release) ++} ++ ++async fn download_binary(client: &reqwest::Client, url: &str, dest: &Path) -> Result<()> { ++ info!("Downloading binary from {}", url); ++ ++ let response = client ++ .get(url) ++ .header("User-Agent", "platform-cli") ++ .send() ++ .await ++ .with_context(|| format!("Failed to download from {}", url))?; ++ ++ if !response.status().is_success() { ++ let status = response.status(); ++ anyhow::bail!("Download failed with status {}", status); ++ } ++ ++ if let Some(parent) = dest.parent() { ++ std::fs::create_dir_all(parent) ++ .with_context(|| format!("Failed to create directory {}", parent.display()))?; ++ } ++ ++ let bytes = response ++ .bytes() ++ .await ++ .context("Failed to read download response body")?; ++ ++ std::fs::write(dest, &bytes) ++ .with_context(|| format!("Failed to write binary to {}", dest.display()))?; ++ ++ #[cfg(unix)] ++ { ++ use std::os::unix::fs::PermissionsExt; ++ let perms = std::fs::Permissions::from_mode(0o755); ++ std::fs::set_permissions(dest, perms).with_context(|| { ++ format!("Failed to set executable permissions on {}", dest.display()) ++ })?; ++ } ++ ++ info!("Binary saved to {}", dest.display()); ++ Ok(()) ++} ++ ++// ==================== Challenge Lookup ==================== ++ ++fn resolve_challenge_name( ++ config: &PlatformConfig, ++ name: &str, ++) -> Option<(String, ChallengeConfig)> { ++ if let Some(challenge) = config.challenges.get(name) { ++ return Some((name.to_string(), challenge.clone())); ++ } ++ ++ for (challenge_name, challenge) in &config.challenges { ++ if challenge.command_alias == name { ++ return Some((challenge_name.clone(), challenge.clone())); ++ } ++ } ++ ++ None ++} ++ ++// ==================== Subcommand Handlers ==================== ++ ++async fn cmd_download(challenge_name: &str) -> Result<()> { ++ let config = load_config()?; ++ let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) ++ .with_context(|| { ++ format!( ++ "Challenge '{}' not found in config. Add it to {} first.", ++ challenge_name, ++ config_path() ++ .map(|p| p.display().to_string()) ++ .unwrap_or_else(|_| "~/.platform/platform.toml".to_string()) ++ ) ++ })?; ++ ++ info!( ++ "Downloading challenge '{}' from {}", ++ canonical_name, challenge.github_repo ++ ); ++ ++ let client = reqwest::Client::new(); ++ let release = fetch_latest_release(&client, &challenge.github_repo).await?; ++ ++ let version = release.tag_name.trim_start_matches('v').to_string(); ++ info!("Latest release: v{}", version); ++ ++ let asset = find_matching_asset(&release.assets).with_context(|| { ++ let available: Vec<&str> = release.assets.iter().map(|a| a.name.as_str()).collect(); ++ format!( ++ "No binary found for platform '{}'. Available assets: {:?}", ++ platform_identifier(), ++ available ++ ) ++ })?; ++ ++ let dest = bin_dir()?.join(&challenge.binary_name); ++ download_binary(&client, &asset.browser_download_url, &dest).await?; ++ ++ let mut versions = load_versions()?; ++ versions.insert( ++ canonical_name.clone(), ++ VersionInfo { ++ version: version.clone(), ++ binary_path: dest.display().to_string(), ++ installed_at: Utc::now(), ++ github_repo: challenge.github_repo.clone(), ++ }, ++ ); ++ save_versions(&versions)?; ++ ++ info!( ++ "Successfully installed {} v{} to {}", ++ canonical_name, ++ version, ++ dest.display() ++ ); ++ println!( ++ "โœ“ {} v{} installed to {}", ++ canonical_name, ++ version, ++ dest.display() ++ ); ++ ++ Ok(()) ++} ++ ++async fn cmd_update(challenge_name: &str) -> Result<()> { ++ let config = load_config()?; ++ let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) ++ .with_context(|| format!("Challenge '{}' not found in config", challenge_name))?; ++ ++ let versions = load_versions()?; ++ let current_version = versions ++ .get(&canonical_name) ++ .map(|v| v.version.clone()) ++ .unwrap_or_default(); ++ ++ info!( ++ "Checking for updates to '{}' (current: {})", ++ canonical_name, ++ if current_version.is_empty() { ++ "not installed" ++ } else { ++ ¤t_version ++ } ++ ); ++ ++ let client = reqwest::Client::new(); ++ let release = fetch_latest_release(&client, &challenge.github_repo).await?; ++ let latest_version = release.tag_name.trim_start_matches('v').to_string(); ++ ++ if !current_version.is_empty() { ++ let current = semver::Version::parse(¤t_version); ++ let latest = semver::Version::parse(&latest_version); ++ ++ match (current, latest) { ++ (Ok(cur), Ok(lat)) if lat <= cur => { ++ println!( ++ "โœ“ {} is already up to date (v{})", ++ canonical_name, current_version ++ ); ++ return Ok(()); ++ } ++ _ => {} ++ } ++ } ++ ++ info!( ++ "Updating {} from v{} to v{}", ++ canonical_name, current_version, latest_version ++ ); ++ ++ let asset = find_matching_asset(&release.assets) ++ .with_context(|| format!("No binary found for platform '{}'", platform_identifier()))?; ++ ++ let dest = bin_dir()?.join(&challenge.binary_name); ++ download_binary(&client, &asset.browser_download_url, &dest).await?; ++ ++ let mut versions = load_versions()?; ++ versions.insert( ++ canonical_name.clone(), ++ VersionInfo { ++ version: latest_version.clone(), ++ binary_path: dest.display().to_string(), ++ installed_at: Utc::now(), ++ github_repo: challenge.github_repo.clone(), ++ }, ++ ); ++ save_versions(&versions)?; ++ ++ println!( ++ "โœ“ {} updated to v{} at {}", ++ canonical_name, ++ latest_version, ++ dest.display() ++ ); ++ ++ Ok(()) ++} ++ ++fn cmd_list() -> Result<()> { ++ let versions = load_versions()?; ++ ++ if versions.is_empty() { ++ println!("No challenge CLIs installed."); ++ println!("Use 'platform download ' to install one."); ++ return Ok(()); ++ } ++ ++ let header_installed = "INSTALLED"; ++ println!( ++ "{:<20} {:<12} {:<40} {}", ++ "CHALLENGE", "VERSION", "PATH", header_installed ++ ); ++ println!("{}", "-".repeat(90)); ++ ++ let mut entries: Vec<_> = versions.iter().collect(); ++ entries.sort_by_key(|(name, _)| (*name).clone()); ++ ++ for (name, info) in entries { ++ println!( ++ "{:<20} {:<12} {:<40} {}", ++ name, ++ info.version, ++ info.binary_path, ++ info.installed_at.format("%Y-%m-%d %H:%M:%S UTC") ++ ); ++ } ++ ++ Ok(()) ++} ++ ++async fn cmd_run(challenge_name: &str, args: &[String]) -> Result<()> { ++ let config = load_config()?; ++ let (canonical_name, challenge) = resolve_challenge_name(&config, challenge_name) ++ .with_context(|| format!("Challenge '{}' not found in config", challenge_name))?; ++ ++ let versions = load_versions()?; ++ let version_info = versions.get(&canonical_name).with_context(|| { ++ format!( ++ "Challenge '{}' is not installed. Run 'platform download {}' first.", ++ canonical_name, canonical_name ++ ) ++ })?; ++ ++ let binary_path = Path::new(&version_info.binary_path); ++ if !binary_path.exists() { ++ anyhow::bail!( ++ "Binary not found at {}. Run 'platform download {}' to reinstall.", ++ binary_path.display(), ++ canonical_name ++ ); ++ } ++ ++ if challenge.auto_update { ++ let repo = challenge.github_repo.clone(); ++ let current_version = version_info.version.clone(); ++ let name_for_log = canonical_name.clone(); ++ tokio::spawn(async move { ++ match check_for_update_quietly(&repo, ¤t_version).await { ++ Ok(Some(new_version)) => { ++ eprintln!( ++ "โ„น A new version of {} is available: v{} (current: v{}). Run 'platform update {}'", ++ name_for_log, new_version, current_version, name_for_log ++ ); ++ } ++ Ok(None) => {} ++ Err(e) => { ++ debug!("Auto-update check failed for {}: {}", name_for_log, e); ++ } ++ } ++ }); ++ } ++ ++ debug!("Running {} with args: {:?}", binary_path.display(), args); ++ ++ let status = std::process::Command::new(binary_path) ++ .args(args) ++ .stdin(std::process::Stdio::inherit()) ++ .stdout(std::process::Stdio::inherit()) ++ .stderr(std::process::Stdio::inherit()) ++ .status() ++ .with_context(|| format!("Failed to execute {}", binary_path.display()))?; ++ ++ if !status.success() { ++ let code = status.code().unwrap_or(1); ++ std::process::exit(code); ++ } ++ ++ Ok(()) ++} ++ ++async fn check_for_update_quietly( ++ github_repo: &str, ++ current_version: &str, ++) -> Result> { ++ let client = reqwest::Client::builder() ++ .timeout(std::time::Duration::from_secs(5)) ++ .build()?; ++ ++ let release = fetch_latest_release(&client, github_repo).await?; ++ let latest_version = release.tag_name.trim_start_matches('v').to_string(); ++ ++ let current = semver::Version::parse(current_version)?; ++ let latest = semver::Version::parse(&latest_version)?; ++ ++ if latest > current { ++ Ok(Some(latest_version)) ++ } else { ++ Ok(None) ++ } ++} ++ ++fn cmd_config() -> Result<()> { ++ let path = config_path()?; ++ if !path.exists() { ++ info!("No config found, creating default at {}", path.display()); ++ let config = PlatformConfig::default(); ++ save_config(&config)?; ++ } ++ ++ let contents = std::fs::read_to_string(&path) ++ .with_context(|| format!("Failed to read config from {}", path.display()))?; ++ ++ println!("# Config: {}", path.display()); ++ println!(); ++ print!("{}", contents); ++ ++ Ok(()) ++} ++ ++// ==================== Main ==================== ++ ++#[tokio::main] ++async fn main() -> Result<()> { ++ tracing_subscriber::fmt() ++ .with_env_filter( ++ tracing_subscriber::EnvFilter::try_from_default_env() ++ .unwrap_or_else(|_| "info,platform_cli=debug".into()), ++ ) ++ .init(); ++ ++ let cli = Cli::parse(); ++ ++ match cli.command { ++ Commands::Download { challenge_name } => cmd_download(&challenge_name).await, ++ Commands::Update { challenge_name } => cmd_update(&challenge_name).await, ++ Commands::List => cmd_list(), ++ Commands::Run { ++ challenge_name, ++ args, ++ } => cmd_run(&challenge_name, &args).await, ++ Commands::Config => cmd_config(), ++ } ++} +diff --git a/bins/validator-node/src/main.rs b/bins/validator-node/src/main.rs +index 2b4e798..46236a4 100644 +--- a/bins/validator-node/src/main.rs ++++ b/bins/validator-node/src/main.rs +@@ -1020,6 +1020,13 @@ async fn handle_network_event( + "Received review result" + ); + } ++ P2PMessage::AgentLogProposal(msg) => { ++ debug!( ++ submission_id = %msg.submission_id, ++ validator = %msg.validator_hotkey.to_hex(), ++ "Received agent log proposal" ++ ); ++ } + }, + NetworkEvent::PeerConnected(peer_id) => { + info!("Peer connected: {}", peer_id); +diff --git a/crates/core/src/message.rs b/crates/core/src/message.rs +index cea0fd5..270bd9b 100644 +--- a/crates/core/src/message.rs ++++ b/crates/core/src/message.rs +@@ -53,6 +53,9 @@ pub enum NetworkMessage { + /// Real-time task progress update (for evaluation tracking) + TaskProgress(TaskProgressMessage), + ++ /// Agent log proposal for consensus validation ++ AgentLogProposal(AgentLogProposalMessage), ++ + /// Version incompatible - disconnect + VersionMismatch { + our_version: String, +@@ -126,6 +129,18 @@ impl TaskProgressMessage { + } + } + ++/// Agent log proposal message ++#[derive(Clone, Debug, Serialize, Deserialize)] ++pub struct AgentLogProposalMessage { ++ pub submission_id: String, ++ pub challenge_id: String, ++ pub miner_hotkey: String, ++ pub logs_hash: [u8; 32], ++ pub logs_data: Vec, ++ pub validator_hotkey: String, ++ pub epoch: u64, ++} ++ + /// Challenge-specific network message + /// Contains serialized challenge P2P message that will be routed to the challenge handler + #[derive(Clone, Debug, Serialize, Deserialize)] +@@ -1236,6 +1251,19 @@ mod tests { + message_type: ChallengeMessageType::EvaluationResult, + }); + ++ // TaskProgress (already covered above via TaskProgress variant) ++ ++ // AgentLogProposal ++ let _ = NetworkMessage::AgentLogProposal(AgentLogProposalMessage { ++ submission_id: "sub-1".to_string(), ++ challenge_id: "challenge-1".to_string(), ++ miner_hotkey: "miner-1".to_string(), ++ logs_hash: [0u8; 32], ++ logs_data: vec![1, 2, 3], ++ validator_hotkey: "validator-1".to_string(), ++ epoch: 1, ++ }); ++ + // VersionMismatch + let _ = NetworkMessage::VersionMismatch { + our_version: "0.1.0".to_string(), +diff --git a/crates/p2p-consensus/src/messages.rs b/crates/p2p-consensus/src/messages.rs +index efcabd3..55acca2 100644 +--- a/crates/p2p-consensus/src/messages.rs ++++ b/crates/p2p-consensus/src/messages.rs +@@ -59,6 +59,9 @@ pub enum P2PMessage { + ReviewAssignment(ReviewAssignmentMessage), + ReviewDecline(ReviewDeclineMessage), + ReviewResult(ReviewResultMessage), ++ ++ /// Agent log proposal for consensus ++ AgentLogProposal(AgentLogProposalMessage), + } + + impl P2PMessage { +@@ -113,6 +116,7 @@ impl P2PMessage { + P2PMessage::ReviewAssignment(_) => "ReviewAssignment", + P2PMessage::ReviewDecline(_) => "ReviewDecline", + P2PMessage::ReviewResult(_) => "ReviewResult", ++ P2PMessage::AgentLogProposal(_) => "AgentLogProposal", + } + } + } +@@ -689,6 +693,31 @@ pub struct ReviewResultMessage { + pub signature: Vec, + } + ++// ============================================================================ ++// Agent Log Messages ++// ============================================================================ ++ ++/// Agent log proposal message for P2P consensus ++#[derive(Clone, Debug, Serialize, Deserialize)] ++pub struct AgentLogProposalMessage { ++ /// Submission ID this log belongs to ++ pub submission_id: String, ++ /// Challenge ID ++ pub challenge_id: String, ++ /// Miner hotkey ++ pub miner_hotkey: String, ++ /// SHA256 hash of the logs data ++ pub logs_hash: [u8; 32], ++ /// Serialized agent logs (max 256KB) ++ pub logs_data: Vec, ++ /// Validator proposing these logs ++ pub validator_hotkey: Hotkey, ++ /// Epoch when evaluation occurred ++ pub epoch: u64, ++ /// Timestamp ++ pub timestamp: i64, ++} ++ + // ============================================================================ + // Signed Message Wrapper + // ============================================================================ +diff --git a/crates/p2p-consensus/src/network.rs b/crates/p2p-consensus/src/network.rs +index 6de620f..5251cb7 100644 +--- a/crates/p2p-consensus/src/network.rs ++++ b/crates/p2p-consensus/src/network.rs +@@ -718,6 +718,7 @@ fn expected_signer(message: &P2PMessage) -> Option<&Hotkey> { + P2PMessage::ReviewAssignment(msg) => Some(&msg.assigner), + P2PMessage::ReviewDecline(msg) => Some(&msg.validator), + P2PMessage::ReviewResult(msg) => Some(&msg.validator), ++ P2PMessage::AgentLogProposal(msg) => Some(&msg.validator_hotkey), + } + } + +diff --git a/crates/p2p-consensus/src/state.rs b/crates/p2p-consensus/src/state.rs +index 8667cde..3d21549 100644 +--- a/crates/p2p-consensus/src/state.rs ++++ b/crates/p2p-consensus/src/state.rs +@@ -189,6 +189,15 @@ pub struct ChainState { + /// Review assignments per submission + #[serde(default)] + pub review_assignments: HashMap>, ++ /// Agent logs awaiting consensus (submission_id -> validator_hotkey -> serialized logs) ++ #[serde(default)] ++ pub agent_log_proposals: HashMap>>, ++ /// Consensus-validated agent logs (submission_id -> validated logs) ++ #[serde(default)] ++ pub validated_agent_logs: HashMap>, ++ /// Stored agent code registry (miner_hotkey -> latest agent code entry) ++ #[serde(default)] ++ pub agent_code_registry: HashMap, + } + + /// Record of a review assignment +@@ -209,6 +218,15 @@ pub struct ReviewResultEntry { + pub timestamp: i64, + } + ++/// Registry entry for stored agent code ++#[derive(Clone, Debug, Serialize, Deserialize)] ++pub struct AgentCodeEntry { ++ pub agent_hash: String, ++ pub code_size: u64, ++ pub epoch: u64, ++ pub stored_at: i64, ++} ++ + impl Default for ChainState { + fn default() -> Self { + Self { +@@ -231,6 +249,9 @@ impl Default for ChainState { + task_progress: HashMap::new(), + challenge_storage_roots: HashMap::new(), + review_assignments: HashMap::new(), ++ agent_log_proposals: HashMap::new(), ++ validated_agent_logs: HashMap::new(), ++ agent_code_registry: HashMap::new(), + } + } + } +@@ -766,6 +787,71 @@ impl ChainState { + pub fn get_review_status(&self, submission_id: &str) -> Option<&Vec> { + self.review_assignments.get(submission_id) + } ++ ++ /// Propose agent logs from a validator ++ pub fn propose_agent_logs( ++ &mut self, ++ submission_id: &str, ++ validator: Hotkey, ++ logs_data: Vec, ++ ) { ++ self.agent_log_proposals ++ .entry(submission_id.to_string()) ++ .or_default() ++ .insert(validator, logs_data); ++ self.update_hash(); ++ } ++ ++ /// Finalize agent logs by consensus (>50% agreement by hash) ++ pub fn finalize_agent_logs(&mut self, submission_id: &str) -> bool { ++ let proposals = match self.agent_log_proposals.get(submission_id) { ++ Some(p) if !p.is_empty() => p, ++ _ => return false, ++ }; ++ ++ let total_proposals = proposals.len(); ++ ++ let mut hash_counts: HashMap<[u8; 32], usize> = HashMap::new(); ++ let mut hash_to_data: HashMap<[u8; 32], &Vec> = HashMap::new(); ++ ++ for logs_data in proposals.values() { ++ let mut hasher = Sha256::new(); ++ hasher.update(logs_data); ++ let hash: [u8; 32] = hasher.finalize().into(); ++ ++ *hash_counts.entry(hash).or_default() += 1; ++ hash_to_data.entry(hash).or_insert(logs_data); ++ } ++ ++ let (best_hash, best_count) = hash_counts ++ .iter() ++ .max_by_key(|(_, count)| *count) ++ .map(|(h, c)| (*h, *c)) ++ .unwrap_or(([0u8; 32], 0)); ++ ++ if best_count > total_proposals / 2 { ++ if let Some(data) = hash_to_data.get(&best_hash) { ++ self.validated_agent_logs ++ .insert(submission_id.to_string(), (*data).clone()); ++ } ++ self.agent_log_proposals.remove(submission_id); ++ self.increment_sequence(); ++ true ++ } else { ++ false ++ } ++ } ++ ++ /// Register agent code entry ++ pub fn register_agent_code(&mut self, miner: Hotkey, entry: AgentCodeEntry) { ++ self.agent_code_registry.insert(miner, entry); ++ self.increment_sequence(); ++ } ++ ++ /// Get agent code entry for a miner ++ pub fn get_agent_code_entry(&self, miner: &Hotkey) -> Option<&AgentCodeEntry> { ++ self.agent_code_registry.get(miner) ++ } + } + + /// Thread-safe state manager +diff --git a/crates/rpc-server/src/jsonrpc.rs b/crates/rpc-server/src/jsonrpc.rs +index 225e0d8..7c8a8ab 100644 +--- a/crates/rpc-server/src/jsonrpc.rs ++++ b/crates/rpc-server/src/jsonrpc.rs +@@ -352,6 +352,17 @@ impl RpcHandler { + ["epoch", "current"] => self.epoch_current(req.id), + ["epoch", "getPhase"] => self.epoch_get_phase(req.id), + ++ // Leaderboard namespace ++ ["leaderboard", "get"] => self.leaderboard_get(req.id, req.params), ++ ++ // Evaluation namespace ++ ["evaluation", "getProgress"] => self.evaluation_get_progress(req.id, req.params), ++ ["evaluation", "getLogs"] => self.evaluation_get_logs(req.id, req.params), ++ ++ // Agent namespace ++ ["agent", "getCode"] => self.agent_get_code(req.id, req.params), ++ ["agent", "getLogs"] => self.agent_get_logs(req.id, req.params), ++ + // RPC info + ["rpc", "methods"] => self.rpc_methods(req.id), + +@@ -398,6 +409,12 @@ impl RpcHandler { + "job_list", "job_get", + // Epoch + "epoch_current", "epoch_getPhase", ++ // Leaderboard ++ "leaderboard_get", ++ // Evaluation ++ "evaluation_getProgress", "evaluation_getLogs", ++ // Agent ++ "agent_getCode", "agent_getLogs", + // RPC + "rpc_methods", + // Monitor +@@ -1322,6 +1339,150 @@ impl RpcHandler { + JsonRpcResponse::result(id, json!(phase)) + } + ++ // ==================== Leaderboard Namespace ==================== ++ ++ fn leaderboard_get(&self, id: Value, params: Value) -> JsonRpcResponse { ++ let challenge_id = match self.get_param_str(¶ms, 0, "challenge_id") { ++ Some(c) => c, ++ None => { ++ return JsonRpcResponse::error( ++ id, ++ INVALID_PARAMS, ++ "Missing 'challenge_id' parameter", ++ ) ++ } ++ }; ++ let limit = self ++ .get_param_u64(¶ms, 1, "limit") ++ .unwrap_or(100) ++ .min(1000); ++ let offset = self.get_param_u64(¶ms, 2, "offset").unwrap_or(0); ++ ++ let chain = self.chain_state.read(); ++ ++ let challenge_uuid = chain ++ .challenges ++ .values() ++ .find(|c| c.id.to_string() == challenge_id || c.name == challenge_id) ++ .map(|c| c.id); ++ ++ match challenge_uuid { ++ Some(_cid) => JsonRpcResponse::result( ++ id, ++ json!({ ++ "challengeId": challenge_id, ++ "entries": [], ++ "total": 0, ++ "limit": limit, ++ "offset": offset, ++ }), ++ ), ++ None => JsonRpcResponse::error( ++ id, ++ CHALLENGE_NOT_FOUND, ++ format!("Challenge '{}' not found", challenge_id), ++ ), ++ } ++ } ++ ++ // ==================== Evaluation Namespace ==================== ++ ++ fn evaluation_get_progress(&self, id: Value, params: Value) -> JsonRpcResponse { ++ let submission_id = match self.get_param_str(¶ms, 0, "submission_id") { ++ Some(s) => s, ++ None => { ++ return JsonRpcResponse::error( ++ id, ++ INVALID_PARAMS, ++ "Missing 'submission_id' parameter", ++ ) ++ } ++ }; ++ ++ JsonRpcResponse::result( ++ id, ++ json!({ ++ "submissionId": submission_id, ++ "progress": [], ++ "total": 0, ++ }), ++ ) ++ } ++ ++ fn evaluation_get_logs(&self, id: Value, params: Value) -> JsonRpcResponse { ++ let submission_id = match self.get_param_str(¶ms, 0, "submission_id") { ++ Some(s) => s, ++ None => { ++ return JsonRpcResponse::error( ++ id, ++ INVALID_PARAMS, ++ "Missing 'submission_id' parameter", ++ ) ++ } ++ }; ++ ++ JsonRpcResponse::result( ++ id, ++ json!({ ++ "submissionId": submission_id, ++ "logs": null, ++ "validated": false, ++ }), ++ ) ++ } ++ ++ // ==================== Agent Namespace ==================== ++ ++ fn agent_get_code(&self, id: Value, params: Value) -> JsonRpcResponse { ++ let miner_hotkey = match self.get_param_str(¶ms, 0, "miner_hotkey") { ++ Some(h) => h, ++ None => { ++ return JsonRpcResponse::error( ++ id, ++ INVALID_PARAMS, ++ "Missing 'miner_hotkey' parameter", ++ ) ++ } ++ }; ++ let _epoch = self.get_param_u64(¶ms, 1, "epoch"); ++ ++ let hk = match platform_core::Hotkey::from_hex(&miner_hotkey) { ++ Some(h) => h, ++ None => return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hotkey format"), ++ }; ++ ++ JsonRpcResponse::result( ++ id, ++ json!({ ++ "minerHotkey": miner_hotkey, ++ "entry": null, ++ }), ++ ) ++ } ++ ++ fn agent_get_logs(&self, id: Value, params: Value) -> JsonRpcResponse { ++ let miner_hotkey = match self.get_param_str(¶ms, 0, "miner_hotkey") { ++ Some(h) => h, ++ None => { ++ return JsonRpcResponse::error( ++ id, ++ INVALID_PARAMS, ++ "Missing 'miner_hotkey' parameter", ++ ) ++ } ++ }; ++ let _epoch = self.get_param_u64(¶ms, 1, "epoch"); ++ ++ JsonRpcResponse::result( ++ id, ++ json!({ ++ "minerHotkey": miner_hotkey, ++ "logs": [], ++ "total": 0, ++ }), ++ ) ++ } ++ + // ==================== Helper Methods ==================== + + fn get_param_str(&self, params: &Value, index: usize, name: &str) -> Option { diff --git a/release-please-config.json b/release-please-config.json index 95b091901..d69c2f762 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -3,17 +3,14 @@ "release-type": "simple", "include-component-in-tag": false, "include-v-in-tag": true, - "sequential-calls": true, "packages": { ".": { - "package-name": "term-challenge", + "package-name": "platform", "changelog-path": "CHANGELOG.md", "bump-minor-pre-major": true, - "bump-patch-for-minor-pre-major": true, - "extra-files": [] + "bump-patch-for-minor-pre-major": true } }, - "commit-search-depth": 50, "changelog-sections": [ {"type": "feat", "section": "Features"}, {"type": "fix", "section": "Bug Fixes"}, diff --git a/rust-toolchain-nightly.toml b/rust-toolchain-nightly.toml new file mode 100644 index 000000000..91a75b615 --- /dev/null +++ b/rust-toolchain-nightly.toml @@ -0,0 +1,6 @@ +[toolchain] +channel = "nightly" +profile = "default" + +[env] +PLATFORM_NIGHTLY_RUSTFLAGS = "-Z threads=0" diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 000000000..4259fa81a --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "stable" +profile = "default" \ No newline at end of file diff --git a/scripts/build-wasm.sh b/scripts/build-wasm.sh new file mode 100755 index 000000000..cafaa63d8 --- /dev/null +++ b/scripts/build-wasm.sh @@ -0,0 +1,109 @@ +#!/bin/bash +set -e + +# Ensure wasm32 target is installed +rustup target add wasm32-unknown-unknown 2>/dev/null || true + +COMPILED_DIR="challenges/compiled" + +# --------------------------------------------------------------------------- +# Build a challenge crate when a package name is supplied, otherwise build +# all challenge crates found under challenges/*/. +# --------------------------------------------------------------------------- + +build_challenge() { + local CRATE="$1" + echo "Building challenge crate: $CRATE ..." + + cargo build --release --target wasm32-unknown-unknown \ + -p "$CRATE" \ + --no-default-features + + # Derive the expected artefact name (hyphens become underscores) + ARTIFACT_NAME=$(echo "$CRATE" | tr '-' '_') + WASM_PATH="target/wasm32-unknown-unknown/release/${ARTIFACT_NAME}.wasm" + + if [ ! -f "$WASM_PATH" ]; then + echo "ERROR: WASM build failed โ€” expected $WASM_PATH" + return 1 + fi + + SIZE=$(du -h "$WASM_PATH" | cut -f1) + echo "WASM built successfully: $WASM_PATH ($SIZE)" + + # Strip debug info if wasm-strip is available + if command -v wasm-strip &> /dev/null; then + echo "Stripping WASM with wasm-strip..." + wasm-strip "$WASM_PATH" + STRIP_SIZE=$(du -h "$WASM_PATH" | cut -f1) + echo "Stripped WASM: $WASM_PATH ($STRIP_SIZE)" + fi + + # Optimize with wasm-opt if available + if command -v wasm-opt &> /dev/null; then + echo "Optimizing WASM with wasm-opt..." + wasm-opt -Oz -o "${WASM_PATH%.wasm}_optimized.wasm" "$WASM_PATH" + OPT_SIZE=$(du -h "${WASM_PATH%.wasm}_optimized.wasm" | cut -f1) + echo "Optimized WASM: ${WASM_PATH%.wasm}_optimized.wasm ($OPT_SIZE)" + else + echo "wasm-opt not found. Install with: cargo install wasm-opt" + fi + + # Copy to compiled output directory + mkdir -p "$COMPILED_DIR" + cp "$WASM_PATH" "$COMPILED_DIR/${ARTIFACT_NAME}.wasm" + echo "Copied to $COMPILED_DIR/${ARTIFACT_NAME}.wasm" + + # Compute and print SHA256 hash + if command -v sha256sum &> /dev/null; then + HASH=$(sha256sum "$COMPILED_DIR/${ARTIFACT_NAME}.wasm" | cut -d' ' -f1) + elif command -v shasum &> /dev/null; then + HASH=$(shasum -a 256 "$COMPILED_DIR/${ARTIFACT_NAME}.wasm" | cut -d' ' -f1) + else + HASH="(sha256sum not available)" + fi + echo "SHA256: $HASH" + echo "" +} + +if [ -n "$1" ]; then + build_challenge "$1" +else + # Build any challenge crates found under challenges/*/ + for dir in challenges/*/; do + if [ -f "${dir}Cargo.toml" ]; then + CRATE_NAME=$(grep '^name' "${dir}Cargo.toml" | head -1 | sed 's/.*"\(.*\)".*/\1/') + if [ -n "$CRATE_NAME" ]; then + build_challenge "$CRATE_NAME" + fi + fi + done + + # Fallback: build chain-runtime WASM if no challenges were found + if [ ! -d "$COMPILED_DIR" ] || [ -z "$(ls -A "$COMPILED_DIR" 2>/dev/null)" ]; then + echo "No challenge crates found. Building chain-runtime WASM..." + + cargo build --release --target wasm32-unknown-unknown \ + -p mini-chain-chain-runtime \ + --no-default-features + + WASM_PATH="target/wasm32-unknown-unknown/release/platform_chain_chain_runtime.wasm" + + if [ -f "$WASM_PATH" ]; then + SIZE=$(du -h "$WASM_PATH" | cut -f1) + echo "WASM built successfully: $WASM_PATH ($SIZE)" + + if command -v wasm-opt &> /dev/null; then + echo "Optimizing WASM with wasm-opt..." + wasm-opt -Oz -o "${WASM_PATH%.wasm}_optimized.wasm" "$WASM_PATH" + OPT_SIZE=$(du -h "${WASM_PATH%.wasm}_optimized.wasm" | cut -f1) + echo "Optimized WASM: ${WASM_PATH%.wasm}_optimized.wasm ($OPT_SIZE)" + else + echo "wasm-opt not found. Install with: cargo install wasm-opt" + fi + else + echo "ERROR: WASM build failed" + exit 1 + fi + fi +fi diff --git a/scripts/install-docker.sh b/scripts/install-docker.sh new file mode 100755 index 000000000..ca8d21c0b --- /dev/null +++ b/scripts/install-docker.sh @@ -0,0 +1,183 @@ +#!/bin/bash +set -euo pipefail + +# Test harness helper: installs Docker and Compose for local test runs only. + +log() { + echo "[docker-install] $1" +} + +has_docker() { + command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1 +} + +has_compose() { + if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then + return 0 + fi + + command -v docker-compose >/dev/null 2>&1 +} + +if has_docker && has_compose; then + log "Docker and Compose already available" + exit 0 +fi + +SUDO="" +if [ "$(id -u)" -ne 0 ]; then + if command -v sudo >/dev/null 2>&1; then + SUDO="sudo" + else + log "sudo is required to install Docker" + exit 1 + fi +fi + +start_docker() { + if command -v systemctl >/dev/null 2>&1; then + ${SUDO} systemctl enable --now docker || ${SUDO} systemctl start docker + return + fi + + if command -v service >/dev/null 2>&1; then + ${SUDO} service docker start + return + fi + + if command -v rc-service >/dev/null 2>&1; then + ${SUDO} rc-service docker start + fi +} + +install_debian() { + local repo_id + local codename + + repo_id="${ID}" + case "${ID}" in + linuxmint|pop|neon) + repo_id="ubuntu" + ;; + raspbian) + repo_id="debian" + ;; + esac + + ${SUDO} apt-get update -y + ${SUDO} apt-get install -y ca-certificates curl gnupg lsb-release + ${SUDO} install -m 0755 -d /etc/apt/keyrings + curl -fsSL "https://download.docker.com/linux/${repo_id}/gpg" | ${SUDO} gpg --dearmor -o /etc/apt/keyrings/docker.gpg + ${SUDO} chmod a+r /etc/apt/keyrings/docker.gpg + + codename="${VERSION_CODENAME:-}" + if [ -z "${codename}" ] && [ -n "${UBUNTU_CODENAME:-}" ]; then + codename="${UBUNTU_CODENAME}" + fi + if [ -z "${codename}" ]; then + codename="$(lsb_release -cs)" + fi + + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${repo_id} ${codename} stable" | ${SUDO} tee /etc/apt/sources.list.d/docker.list >/dev/null + ${SUDO} apt-get update -y + ${SUDO} apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin +} + +install_fedora() { + ${SUDO} dnf -y install dnf-plugins-core ca-certificates curl + ${SUDO} dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo + ${SUDO} dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin +} + +install_rhel() { + local pkg_mgr + + if command -v dnf >/dev/null 2>&1; then + pkg_mgr=dnf + else + pkg_mgr=yum + fi + + ${SUDO} ${pkg_mgr} -y install ca-certificates curl + ${SUDO} ${pkg_mgr} -y install dnf-plugins-core || true + ${SUDO} ${pkg_mgr} config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo + ${SUDO} ${pkg_mgr} -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin +} + +install_amzn() { + if command -v amazon-linux-extras >/dev/null 2>&1; then + ${SUDO} amazon-linux-extras install -y docker + else + ${SUDO} yum -y install docker + fi + ${SUDO} yum -y install docker-compose-plugin || ${SUDO} yum -y install docker-compose +} + +install_arch() { + ${SUDO} pacman -Sy --noconfirm docker docker-compose +} + +install_alpine() { + ${SUDO} apk add --no-cache docker docker-cli-compose docker-compose +} + +if [[ "${OSTYPE}" == "darwin"* ]]; then + if ! command -v brew >/dev/null 2>&1; then + log "Homebrew is required to install Docker Desktop" + exit 1 + fi + brew install --cask docker + exit 0 +fi + +if [ ! -f /etc/os-release ]; then + log "Unsupported OS: /etc/os-release not found" + exit 1 +fi + +. /etc/os-release + +case "${ID}" in + ubuntu|debian|linuxmint|raspbian|pop|neon) + install_debian + ;; + fedora) + install_fedora + ;; + centos|rhel|almalinux|rocky) + install_rhel + ;; + amzn) + install_amzn + ;; + arch|manjaro) + install_arch + ;; + alpine) + install_alpine + ;; + *) + if [[ "${ID_LIKE:-}" == *"debian"* ]]; then + install_debian + elif [[ "${ID_LIKE:-}" == *"rhel"* ]] || [[ "${ID_LIKE:-}" == *"fedora"* ]]; then + install_rhel + else + log "Unsupported Linux distribution: ${ID}" + exit 1 + fi + ;; + esac + +start_docker + +if ! has_docker; then + log "Docker daemon is not available after installation" + exit 1 +fi + +if ! has_compose; then + log "Docker Compose is not available after installation" + exit 1 +fi + +log "Docker installation complete" \ No newline at end of file diff --git a/scripts/setup-hooks.sh b/scripts/setup-hooks.sh new file mode 100755 index 000000000..164d5f84c --- /dev/null +++ b/scripts/setup-hooks.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Setup git hooks for platform-chain + +REPO_ROOT="$(git rev-parse --show-toplevel)" +git config core.hooksPath "$REPO_ROOT/.githooks" + +echo "Git hooks configured:" +echo " - pre-commit: Runs cargo fmt check, clippy, and cargo check" +echo " - pre-push: Runs all CI checks (fmt, clippy, check, tests)" diff --git a/scripts/test-all.sh b/scripts/test-all.sh new file mode 100755 index 000000000..27a80f5a6 --- /dev/null +++ b/scripts/test-all.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# ============================================================================= +# Platform Standard Test Suite +# ============================================================================= +# Entry point for local/unit test runs. Docker is not required. +# ============================================================================= + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=./test-harness.sh +source "${SCRIPT_DIR}/test-harness.sh" + +PASSED=0 +FAILED=0 +SKIPPED=0 + +platform_test_init +trap platform_cleanup_run_dir EXIT +log_info "Defaults: nightly toolchain uses -Z threads=0" +log_info "Defaults: fast linker flags opt-in via env" +log_info "Opt-out: PLATFORM_DISABLE_NIGHTLY=1" +log_info "Override: PLATFORM_RUST_NIGHTLY=1" +log_info "Opt-out: PLATFORM_DISABLE_FAST_LINKER=1" +log_info "Override: PLATFORM_FAST_LINKER_RUSTFLAGS/PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN" +log_info "Override: PLATFORM_LINKER_RUSTFLAGS/PLATFORM_LINKER_RUSTFLAGS_DARWIN" +if [ "${PLATFORM_DISABLE_NIGHTLY:-0}" = "1" ]; then + export PLATFORM_NIGHTLY_RUSTFLAGS="" + export RUSTUP_TOOLCHAIN="" + log_info "Nightly Rust disabled via opt-out" +elif [ "${PLATFORM_RUST_NIGHTLY:-0}" = "1" ] || [ "${RUSTUP_TOOLCHAIN:-}" = "nightly" ]; then + export RUSTUP_TOOLCHAIN="nightly" + export PLATFORM_NIGHTLY_RUSTFLAGS="${PLATFORM_NIGHTLY_RUSTFLAGS:--Z threads=0}" + log_info "Nightly Rust enabled (parallel rustc)" +else + export PLATFORM_NIGHTLY_RUSTFLAGS="" + log_info "Nightly Rust not requested; clearing nightly flags" +fi + +if [ "${PLATFORM_DISABLE_FAST_LINKER:-0}" = "1" ]; then + export PLATFORM_FAST_LINKER_RUSTFLAGS="" + export PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="" + export PLATFORM_LINKER_RUSTFLAGS="" + export PLATFORM_LINKER_RUSTFLAGS_DARWIN="" + log_info "Fast linker disabled via opt-out" +fi +log_info "=== Platform Test Suite ===" +log_info "Artifacts: ${PLATFORM_TEST_ARTIFACTS_DIR}" +log_info "Run dir: ${PLATFORM_TEST_RUN_DIR}" + +log_info "[1/2] Building workspace" +if cargo build --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/build.log"; then + log_success "Build completed" +else + log_failure "Build failed" + exit 1 +fi + +log_info "[2/2] Running unit tests" +if cargo test --workspace --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/unit-tests.log"; then + log_success "Unit tests completed" +else + log_failure "Unit tests failed" +fi + +log_info "Test summary" +log_info "Passed: ${PASSED}" +log_info "Failed: ${FAILED}" +log_info "Skipped: ${SKIPPED}" + +if [ "${FAILED}" -ne 0 ]; then + exit 1 +fi \ No newline at end of file diff --git a/scripts/test-comprehensive.sh b/scripts/test-comprehensive.sh new file mode 100755 index 000000000..4bee07337 --- /dev/null +++ b/scripts/test-comprehensive.sh @@ -0,0 +1,195 @@ +#!/bin/bash +# ============================================================================= +# Platform Comprehensive Test Suite +# ============================================================================= +# Runs unit, integration, docker, and multi-validator tests. +# Docker is required only for test harness phases 3 and 8; install via scripts/install-docker.sh. +# ============================================================================= + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=./test-harness.sh +source "${SCRIPT_DIR}/test-harness.sh" + +PASSED=0 +FAILED=0 +SKIPPED=0 + +platform_test_init +trap platform_cleanup_run_dir EXIT + +log_info "=============================================================================" +log_info " Platform Comprehensive Test Suite" +log_info "=============================================================================" +log_info "Artifacts: ${PLATFORM_TEST_ARTIFACTS_DIR}" +log_info "Run dir: ${PLATFORM_TEST_RUN_DIR}" +log_info "Defaults: nightly toolchain uses -Z threads=0" +log_info "Defaults: fast linker flags opt-in via env" +log_info "Opt-out: PLATFORM_DISABLE_NIGHTLY=1" +log_info "Override: PLATFORM_RUST_NIGHTLY=1" +log_info "Opt-out: PLATFORM_DISABLE_FAST_LINKER=1" +log_info "Override: PLATFORM_FAST_LINKER_RUSTFLAGS/PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN" +log_info "Override: PLATFORM_LINKER_RUSTFLAGS/PLATFORM_LINKER_RUSTFLAGS_DARWIN" +log_info "" + +if [ "${PLATFORM_DISABLE_NIGHTLY:-0}" = "1" ]; then + export PLATFORM_NIGHTLY_RUSTFLAGS="" + export RUSTUP_TOOLCHAIN="" + log_info "Nightly Rust disabled via opt-out" +elif [ "${PLATFORM_RUST_NIGHTLY:-0}" = "1" ] || [ "${RUSTUP_TOOLCHAIN:-}" = "nightly" ]; then + export RUSTUP_TOOLCHAIN="nightly" + export PLATFORM_NIGHTLY_RUSTFLAGS="${PLATFORM_NIGHTLY_RUSTFLAGS:--Z threads=0}" + log_info "Nightly Rust enabled (parallel rustc)" +else + export PLATFORM_NIGHTLY_RUSTFLAGS="" + log_info "Nightly Rust not requested; clearing nightly flags" +fi + +if [ "${PLATFORM_DISABLE_FAST_LINKER:-0}" = "1" ]; then + export PLATFORM_FAST_LINKER_RUSTFLAGS="" + export PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="" + export PLATFORM_LINKER_RUSTFLAGS="" + export PLATFORM_LINKER_RUSTFLAGS_DARWIN="" + log_info "Fast linker disabled via opt-out" +fi + +log_info "=============================================================================" +log_info "Phase 1: Build (cargo build --release)" +log_info "=============================================================================" +log_info "Building workspace..." +if cargo build --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/build.log"; then + log_success "Build completed successfully" +else + log_failure "Build failed" + exit 1 +fi + +log_info "=============================================================================" +log_info "Phase 2: Unit Tests (cargo test --workspace)" +log_info "=============================================================================" +log_info "Running unit tests..." +if cargo test --workspace --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/unit-tests.log"; then + log_success "Unit tests completed" +else + log_failure "Unit tests failed" +fi + +log_info "=============================================================================" +log_info "Phase 3: Docker Integration Tests" +log_info "=============================================================================" +if platform_should_run_docker; then + if platform_require_compose; then + platform_ensure_network + log_info "Running secure-container-runtime Docker tests..." + if cargo test -p secure-container-runtime --release -- --ignored 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/docker-secure-container.log"; then + log_success "Secure container runtime Docker tests passed" + else + log_failure "Secure container runtime Docker tests failed" + fi + + log_info "Challenge orchestrator Docker tests not configured in workspace" + log_skip "Challenge orchestrator crate unavailable; skipping" + else + log_skip "Docker Compose not available" + fi +else + log_skip "Docker not available, skipping Docker tests" +fi + +log_info "=============================================================================" +log_info "Phase 4: Bittensor Integration Tests" +log_info "=============================================================================" +log_info "Running Bittensor integration tests (requires network)..." +if timeout 120 cargo test -p platform-bittensor --release -- --ignored 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/bittensor.log"; then + log_success "Bittensor integration tests passed" +else + log_warning "Bittensor integration tests failed or timed out" +fi + +log_info "=============================================================================" +log_info "Phase 5: Security Policy Tests" +log_info "=============================================================================" +log_info "Verifying security policies..." + +log_info "Testing Docker socket mount blocking..." +if cargo test -p secure-container-runtime test_default_policy_blocks_docker_socket --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/policy-docker-socket.log"; then + log_success "Docker socket mount blocking verified" +else + log_failure "Docker socket mount blocking test failed" +fi + +log_info "Testing image whitelist enforcement..." +if cargo test -p secure-container-runtime test_strict_policy_blocks_non_whitelisted_images --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/policy-image-whitelist.log"; then + log_success "Image whitelist enforcement verified" +else + log_failure "Image whitelist enforcement test failed" +fi + +log_info "Testing resource limit enforcement..." +if cargo test -p secure-container-runtime test_policy_enforces_resource_limits --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/policy-resource-limits.log"; then + log_success "Resource limit enforcement verified" +else + log_failure "Resource limit enforcement test failed" +fi + +log_info "=============================================================================" +log_info "Phase 6: P2P Consensus Tests" +log_info "=============================================================================" +log_info "Running P2P consensus unit tests..." +if cargo test -p platform-p2p-consensus --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/p2p-tests.log"; then + log_success "P2P consensus tests completed" +else + log_failure "P2P consensus tests failed" +fi + +log_info "=============================================================================" +log_info "Phase 7: Storage Tests" +log_info "=============================================================================" +log_info "Running storage tests..." +if cargo test -p platform-storage --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/storage-tests.log"; then + log_success "Storage tests passed" +else + log_failure "Storage tests failed" +fi + +log_info "Running distributed storage tests..." +if cargo test -p platform-distributed-storage --release 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/distributed-storage-tests.log"; then + log_success "Distributed storage tests passed" +else + log_failure "Distributed storage tests failed" +fi + +log_info "=============================================================================" +log_info "Phase 8: Multi-validator Docker Compose" +log_info "=============================================================================" +if platform_should_run_docker; then + if platform_require_compose; then + platform_ensure_network + log_info "Running multi-validator docker test harness..." + if "${SCRIPT_DIR}/../tests/docker/test-multi-validator.sh" 2>&1 | tee "${PLATFORM_TEST_LOG_DIR}/multi-validator-docker.log"; then + log_success "Multi-validator docker test completed" + else + log_failure "Multi-validator docker test failed" + fi + else + log_skip "Docker Compose not available" + fi +else + log_skip "Docker not available, skipping compose tests" +fi + +log_info "=============================================================================" +log_info " Test Summary" +log_info "=============================================================================" +log_info "Passed: ${PASSED}" +log_info "Failed: ${FAILED}" +log_info "Skipped: ${SKIPPED}" + +if [ "${FAILED}" -eq 0 ]; then + log_success "All tests passed" + exit 0 +fi + +log_failure "Some tests failed" +exit 1 \ No newline at end of file diff --git a/scripts/test-harness.sh b/scripts/test-harness.sh new file mode 100644 index 000000000..8c387596a --- /dev/null +++ b/scripts/test-harness.sh @@ -0,0 +1,201 @@ +#!/bin/bash +# ============================================================================= +# Platform Test Harness Helpers +# ============================================================================= +# Shared environment defaults and preflight checks for test entrypoints. +# +# Environment variables: +# PLATFORM_TEST_ROOT Repo root (auto-detected) +# PLATFORM_TEST_ARTIFACTS_DIR Base artifacts directory +# PLATFORM_TEST_LOG_DIR Log output directory +# PLATFORM_TEST_TMP_BASE Base temp directory +# PLATFORM_TEST_RUN_DIR Specific run directory +# PLATFORM_TEST_COMPOSE_FILE Docker compose file path +# PLATFORM_TEST_COMPOSE_PROJECT Compose project name +# PLATFORM_TEST_NETWORK Docker network name +# PLATFORM_TEST_DOCKER_MODE auto|skip|required +# PLATFORM_TEST_PRESERVE_RUN_DIR true to skip cleanup +# ============================================================================= + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +log_info() { + echo -e "${BLUE}[INFO]${NC} $1" +} + +log_success() { + echo -e "${GREEN}[PASS]${NC} $1" + if [ -n "${PASSED+x}" ]; then + PASSED=$((PASSED + 1)) + fi +} + +log_failure() { + echo -e "${RED}[FAIL]${NC} $1" + if [ -n "${FAILED+x}" ]; then + FAILED=$((FAILED + 1)) + fi +} + +log_warning() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_skip() { + echo -e "${YELLOW}[SKIP]${NC} $1" + if [ -n "${SKIPPED+x}" ]; then + SKIPPED=$((SKIPPED + 1)) + fi +} + +platform_test_init() { + local script_dir + script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + + export PLATFORM_TEST_ROOT="${PLATFORM_TEST_ROOT:-$(cd "${script_dir}/.." && pwd)}" + export PLATFORM_TEST_ARTIFACTS_DIR="${PLATFORM_TEST_ARTIFACTS_DIR:-${PLATFORM_TEST_ROOT}/artifacts/tests}" + export PLATFORM_TEST_LOG_DIR="${PLATFORM_TEST_LOG_DIR:-${PLATFORM_TEST_ARTIFACTS_DIR}/logs}" + export PLATFORM_TEST_TMP_BASE="${PLATFORM_TEST_TMP_BASE:-/tmp/platform-tests}" + export PLATFORM_TEST_COMPOSE_FILE="${PLATFORM_TEST_COMPOSE_FILE:-${PLATFORM_TEST_ROOT}/tests/docker/docker-compose.multi-validator.yml}" + export PLATFORM_TEST_COMPOSE_PROJECT="${PLATFORM_TEST_COMPOSE_PROJECT:-platform-test}" + export PLATFORM_TEST_NETWORK="${PLATFORM_TEST_NETWORK:-platform-test}" + export PLATFORM_TEST_DOCKER_MODE="${PLATFORM_TEST_DOCKER_MODE:-auto}" + + mkdir -p "${PLATFORM_TEST_ARTIFACTS_DIR}" "${PLATFORM_TEST_LOG_DIR}" "${PLATFORM_TEST_TMP_BASE}" + + if [ -z "${PLATFORM_TEST_RUN_DIR:-}" ]; then + PLATFORM_TEST_RUN_DIR="$(mktemp -d "${PLATFORM_TEST_TMP_BASE}/run-XXXXXX")" + export PLATFORM_TEST_RUN_DIR + else + mkdir -p "${PLATFORM_TEST_RUN_DIR}" + fi + + if [ -z "${COMPOSE_PROJECT_NAME:-}" ]; then + export COMPOSE_PROJECT_NAME="${PLATFORM_TEST_COMPOSE_PROJECT}" + fi +} + +platform_cleanup_run_dir() { + if [ "${PLATFORM_TEST_PRESERVE_RUN_DIR:-false}" != "true" ] && [ -n "${PLATFORM_TEST_RUN_DIR:-}" ]; then + rm -rf "${PLATFORM_TEST_RUN_DIR}" 2>/dev/null || true + fi +} + +platform_require_command() { + local cmd="$1" + if ! command -v "${cmd}" >/dev/null 2>&1; then + log_failure "Required command not found: ${cmd}" + return 1 + fi +} + +platform_has_docker() { + command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1 +} + +platform_has_compose() { + if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then + return 0 + fi + + command -v docker-compose >/dev/null 2>&1 +} + +platform_install_docker_if_needed() { + if [ "${PLATFORM_TEST_DOCKER_MODE}" = "skip" ]; then + log_skip "Docker checks disabled (PLATFORM_TEST_DOCKER_MODE=skip)" + return 0 + fi + + if platform_has_docker && platform_has_compose; then + return 0 + fi + + local script_dir + script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + if [ ! -x "${script_dir}/install-docker.sh" ]; then + log_failure "scripts/install-docker.sh not found or not executable" + return 1 + fi + + log_info "Docker/Compose missing; attempting installation via scripts/install-docker.sh" + "${script_dir}/install-docker.sh" + + if ! platform_has_docker; then + log_failure "Docker daemon is still unavailable after installation" + return 1 + fi + + if ! platform_has_compose; then + log_failure "Docker Compose is still unavailable after installation" + return 1 + fi +} + +platform_require_docker() { + if ! platform_has_docker; then + log_failure "Docker daemon not available" + return 1 + fi +} + +platform_require_compose() { + if ! platform_has_compose; then + log_failure "Docker Compose not available" + return 1 + fi +} + +platform_compose() { + if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then + docker compose "$@" + return + fi + + if command -v docker-compose >/dev/null 2>&1; then + docker-compose "$@" + return + fi + + return 127 +} + +platform_should_run_docker() { + case "${PLATFORM_TEST_DOCKER_MODE}" in + skip) + return 1 + ;; + required) + platform_install_docker_if_needed || return 1 + platform_require_docker + ;; + auto) + if ! platform_has_docker || ! platform_has_compose; then + platform_install_docker_if_needed || return 1 + fi + platform_has_docker + ;; + *) + log_warning "Unknown PLATFORM_TEST_DOCKER_MODE=${PLATFORM_TEST_DOCKER_MODE}, defaulting to auto" + if ! platform_has_docker || ! platform_has_compose; then + platform_install_docker_if_needed || return 1 + fi + platform_has_docker + ;; + esac +} + +platform_ensure_network() { + if ! platform_has_docker; then + return 1 + fi + + if ! docker network inspect "${PLATFORM_TEST_NETWORK}" >/dev/null 2>&1; then + log_info "Creating docker network ${PLATFORM_TEST_NETWORK}" + docker network create "${PLATFORM_TEST_NETWORK}" >/dev/null + fi +} diff --git a/scripts/test-single-validator.sh b/scripts/test-single-validator.sh new file mode 100755 index 000000000..65ba616cf --- /dev/null +++ b/scripts/test-single-validator.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# Test a single validator node + +set -e + +echo "=== Single Validator Test ===" + +# Build +cargo build --release --bin validator-node + +# Create data directory +mkdir -p data/test + +# Run with debug logging +echo "" +echo "Starting validator..." +echo "Press Ctrl+C to stop" +echo "" + +RUST_LOG=debug,platform_chain=debug ./target/release/validator-node \ + --data-dir ./data/test \ + --stake 100 diff --git a/scripts/verify-nightly-config.sh b/scripts/verify-nightly-config.sh new file mode 100755 index 000000000..e44489cc6 --- /dev/null +++ b/scripts/verify-nightly-config.sh @@ -0,0 +1,224 @@ +#!/bin/bash +# ============================================================================= +# Nightly/Linker Config Verification +# ============================================================================= +# Verifies optional nightly + fast linker flags are applied without failing +# on stable toolchains. This is a lightweight check (dry-run build). +# ============================================================================= + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=./test-harness.sh +source "${SCRIPT_DIR}/test-harness.sh" + +platform_test_init +trap platform_cleanup_run_dir EXIT + +platform_require_command rg +platform_require_command cargo + +CARGO_CONFIG="${PLATFORM_TEST_ROOT}/.cargo/config.toml" +NIGHTLY_CONFIG="${PLATFORM_TEST_ROOT}/rust-toolchain-nightly.toml" + +log_info "Nightly config verification" +log_info "Defaults: build.jobs uses all cores" +log_info "Defaults: nightly toolchain uses -Z threads=0" +log_info "Defaults: fast linker flags from config when set" +log_info "Opt-out: PLATFORM_DISABLE_NIGHTLY=1" +log_info "Override: PLATFORM_RUST_NIGHTLY=1" +log_info "Opt-out: PLATFORM_DISABLE_FAST_LINKER=1" +log_info "Override: PLATFORM_FAST_LINKER_RUSTFLAGS/PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN" +log_info "Override: PLATFORM_LINKER_RUSTFLAGS/PLATFORM_LINKER_RUSTFLAGS_DARWIN" + +assert_config_contains() { + local file_path="$1" + local expected="$2" + + if rg -F --quiet "${expected}" "${file_path}"; then + log_success "Config contains: ${expected}" + else + log_failure "Missing config entry: ${expected}" + return 1 + fi +} + +verify_config_composition() { + log_info "Verifying config composition" + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_DISABLE_NIGHTLY = { value = "${PLATFORM_DISABLE_NIGHTLY}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_RUST_NIGHTLY = { value = "${PLATFORM_RUST_NIGHTLY}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_NIGHTLY_RUSTFLAGS = { value = "${PLATFORM_NIGHTLY_RUSTFLAGS}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_FAST_LINKER_RUSTFLAGS = { value = "${PLATFORM_FAST_LINKER_RUSTFLAGS}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN = { value = "${PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_LINKER_RUSTFLAGS = { value = "${PLATFORM_LINKER_RUSTFLAGS}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'PLATFORM_LINKER_RUSTFLAGS_DARWIN = { value = "${PLATFORM_LINKER_RUSTFLAGS_DARWIN}", force = false }' + assert_config_contains "${CARGO_CONFIG}" 'RUSTFLAGS = { value = "${RUSTFLAGS} ${PLATFORM_NIGHTLY_RUSTFLAGS} ${PLATFORM_FAST_LINKER_RUSTFLAGS} ${PLATFORM_LINKER_RUSTFLAGS}", force = true }' + assert_config_contains "${CARGO_CONFIG}" 'RUSTFLAGS = { value = "${RUSTFLAGS} ${PLATFORM_NIGHTLY_RUSTFLAGS} ${PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN} ${PLATFORM_LINKER_RUSTFLAGS_DARWIN}", force = true }' + assert_config_contains "${NIGHTLY_CONFIG}" 'PLATFORM_NIGHTLY_RUSTFLAGS = "-Z threads=0"' +} + +run_check() { + local label="$1" + local log_file="$2" + local expect_nightly="$3" + local expect_no_nightly="$4" + local expect_fast="$5" + local expect_no_fast="$6" + shift 6 + + PLATFORM_DISABLE_NIGHTLY=0 + PLATFORM_RUST_NIGHTLY=0 + RUSTUP_TOOLCHAIN="" + PLATFORM_NIGHTLY_RUSTFLAGS="" + PLATFORM_FAST_LINKER_RUSTFLAGS="" + PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="" + PLATFORM_LINKER_RUSTFLAGS="" + PLATFORM_LINKER_RUSTFLAGS_DARWIN="" + PLATFORM_DISABLE_FAST_LINKER=0 + + local fast_linker_test_flag="-C link-arg=-s" + local use_fast_linker=0 + local disable_fast_linker=0 + local label_safe="${label// /-}" + local cargo_target_dir="${PLATFORM_TEST_RUN_DIR}/target-${label_safe}" + + while [ "$#" -gt 0 ]; do + case "$1" in + --nightly) + PLATFORM_RUST_NIGHTLY=1 + ;; + --stable) + PLATFORM_DISABLE_NIGHTLY=1 + ;; + --fast-linker) + use_fast_linker=1 + ;; + --disable-fast-linker) + disable_fast_linker=1 + ;; + *) + log_failure "Unknown option: $1" + return 1 + ;; + esac + shift + done + + if [ "${use_fast_linker}" -eq 1 ]; then + PLATFORM_FAST_LINKER_RUSTFLAGS="${fast_linker_test_flag}" + PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="${fast_linker_test_flag}" + log_info "${label}: Fast linker override enabled" + fi + + if [ "${disable_fast_linker}" -eq 1 ]; then + PLATFORM_DISABLE_FAST_LINKER=1 + fi + + if [ "${PLATFORM_DISABLE_NIGHTLY:-0}" = "1" ]; then + PLATFORM_NIGHTLY_RUSTFLAGS="" + RUSTUP_TOOLCHAIN="" + log_info "${label}: Nightly Rust disabled via opt-out" + elif [ "${PLATFORM_RUST_NIGHTLY:-0}" = "1" ] || [ "${RUSTUP_TOOLCHAIN:-}" = "nightly" ]; then + RUSTUP_TOOLCHAIN="nightly" + PLATFORM_NIGHTLY_RUSTFLAGS="${PLATFORM_NIGHTLY_RUSTFLAGS:--Z threads=0}" + log_info "${label}: Nightly Rust enabled (parallel rustc)" + else + PLATFORM_NIGHTLY_RUSTFLAGS="" + log_info "${label}: Nightly Rust not requested; clearing nightly flags" + fi + + if [ "${PLATFORM_DISABLE_FAST_LINKER:-0}" = "1" ]; then + PLATFORM_FAST_LINKER_RUSTFLAGS="" + PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN="" + PLATFORM_LINKER_RUSTFLAGS="" + PLATFORM_LINKER_RUSTFLAGS_DARWIN="" + log_info "${label}: Fast linker disabled via opt-out" + fi + + if [ "${PLATFORM_DISABLE_NIGHTLY:-0}" = "1" ]; then + if [ -n "${PLATFORM_NIGHTLY_RUSTFLAGS}" ]; then + log_failure "${label}: Nightly rustflags should be empty when disabled" + return 1 + fi + fi + + log_info "${label}: Expected toolchain=${RUSTUP_TOOLCHAIN:-default}" + log_info "${label}: Expected nightly rustflags=${PLATFORM_NIGHTLY_RUSTFLAGS:-}" + log_info "${label}: Expected fast linker rustflags=${PLATFORM_FAST_LINKER_RUSTFLAGS:-}" + log_info "${label}: Expected fast linker rustflags darwin=${PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN:-}" + log_info "${label}: Expected linker rustflags=${PLATFORM_LINKER_RUSTFLAGS:-}" + log_info "${label}: Expected linker rustflags darwin=${PLATFORM_LINKER_RUSTFLAGS_DARWIN:-}" + + export PLATFORM_DISABLE_NIGHTLY + export PLATFORM_RUST_NIGHTLY + export RUSTUP_TOOLCHAIN + export PLATFORM_NIGHTLY_RUSTFLAGS + export PLATFORM_FAST_LINKER_RUSTFLAGS + export PLATFORM_FAST_LINKER_RUSTFLAGS_DARWIN + export PLATFORM_LINKER_RUSTFLAGS + export PLATFORM_LINKER_RUSTFLAGS_DARWIN + export PLATFORM_DISABLE_FAST_LINKER + + log_info "${label}: Running cargo check (dry-run build)" + export CARGO_TARGET_DIR="${cargo_target_dir}" + if RUSTFLAGS="${RUSTFLAGS:-} ${PLATFORM_NIGHTLY_RUSTFLAGS} ${PLATFORM_FAST_LINKER_RUSTFLAGS}" cargo check --workspace -v 2>&1 | tee "${log_file}"; then + log_success "${label}: Config verification completed" + else + log_failure "${label}: Config verification failed" + return 1 + fi + + if [ "${expect_nightly}" -eq 1 ]; then + if rg -F --quiet -- "-Z threads=0" "${log_file}"; then + log_success "${label}: Nightly rustflags detected" + else + log_failure "${label}: Nightly rustflags missing" + return 1 + fi + fi + + if [ "${expect_no_nightly}" -eq 1 ]; then + if rg -F --quiet -- "-Z threads=0" "${log_file}"; then + log_failure "${label}: Unexpected nightly rustflags detected" + return 1 + else + log_success "${label}: Nightly rustflags absent as expected" + fi + fi + + if [ "${expect_fast}" -eq 1 ]; then + if rg -F --quiet -- "${fast_linker_test_flag}" "${log_file}"; then + log_success "${label}: Fast linker rustflags detected" + else + log_failure "${label}: Fast linker rustflags missing" + return 1 + fi + fi + + if [ "${expect_no_fast}" -eq 1 ]; then + if rg -F --quiet -- "${fast_linker_test_flag}" "${log_file}"; then + log_failure "${label}: Unexpected fast linker rustflags detected" + return 1 + else + log_success "${label}: Fast linker rustflags absent as expected" + fi + fi +} + +verify_config_composition + +log_info "Stable verification (nightly opt-out)" +run_check "Stable" "${PLATFORM_TEST_LOG_DIR}/nightly-config-stable.log" 0 1 0 1 --stable + +log_info "Fast linker override verification" +run_check "Fast linker" "${PLATFORM_TEST_LOG_DIR}/nightly-config-fast-linker.log" 0 1 1 0 --stable --fast-linker + +log_info "Fast linker opt-out verification" +run_check "Fast linker opt-out" "${PLATFORM_TEST_LOG_DIR}/nightly-config-fast-linker-disabled.log" 0 1 0 1 --stable --fast-linker --disable-fast-linker + +if command -v rustup >/dev/null 2>&1 && rustup toolchain list 2>/dev/null | rg -q '^nightly'; then + log_info "Nightly verification (defaults apply)" + run_check "Nightly" "${PLATFORM_TEST_LOG_DIR}/nightly-config-nightly.log" 1 0 0 1 --nightly +else + log_skip "Nightly toolchain not installed; skipping nightly verification" +fi \ No newline at end of file diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 000000000..005306001 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,62 @@ +[package] +name = "platform-e2e-tests" +version = "0.1.0" +edition = "2021" +publish = false + +[[test]] +name = "e2e_tests" +path = "e2e_tests.rs" + +[[test]] +name = "error_cases" +path = "error_cases.rs" + +[[test]] +name = "rpc_server_tests" +path = "rpc_server_tests.rs" + +[[test]] +name = "storage_tests" +path = "storage_tests.rs" + +[[test]] +name = "bittensor_tests" +path = "bittensor_tests.rs" + +[[test]] +name = "epoch_tests" +path = "epoch_tests.rs" + +[[test]] +name = "sudo_action_tests" +path = "sudo_action_tests.rs" + +[[test]] +name = "blockchain_state_tests" +path = "blockchain_state_tests.rs" + +[[test]] +name = "checkpoint_tests" +path = "checkpoint_tests.rs" + +[dependencies] +platform-core = { path = "../crates/core" } +platform-storage = { path = "../crates/storage" } +platform-p2p-consensus = { path = "../crates/p2p-consensus" } +platform-bittensor = { path = "../crates/bittensor-integration" } +platform-challenge-sdk = { path = "../crates/challenge-sdk" } + +tokio = { version = "1.40", features = ["full", "test-util"] } +tempfile = "3.12" +serde_json = "1.0" +serde = { version = "1.0", features = ["derive"] } +uuid = { version = "1.10", features = ["v4"] } +parking_lot = "0.12" +chrono = { version = "0.4", features = ["serde"] } +rand = "0.8" +lz4_flex = "0.11" +hex = "0.4" +bincode = "1.3" +reqwest = { workspace = true } +sp-core = { workspace = true } \ No newline at end of file diff --git a/tests/bittensor_tests.rs b/tests/bittensor_tests.rs new file mode 100644 index 000000000..12b3e65fc --- /dev/null +++ b/tests/bittensor_tests.rs @@ -0,0 +1,730 @@ +//! Comprehensive Bittensor Integration Tests +//! +//! Tests for weight submission, block sync, and validator sync. + +#![allow(dead_code)] + +use platform_core::*; + +// ============================================================================ +// CONFIG TESTS +// ============================================================================ + +mod config { + + struct SubtensorConfig { + endpoint: String, + netuid: u16, + use_commit_reveal: bool, + version_key: u64, + } + + impl Default for SubtensorConfig { + fn default() -> Self { + Self { + endpoint: "wss://entrypoint-finney.opentensor.ai:443".to_string(), + netuid: 1, + use_commit_reveal: true, + version_key: 1, + } + } + } + + impl SubtensorConfig { + fn testnet(netuid: u16) -> Self { + Self { + endpoint: "wss://test.finney.opentensor.ai:443".to_string(), + netuid, + use_commit_reveal: true, + version_key: 1, + } + } + } + + #[test] + fn test_subtensor_config_default() { + let config = SubtensorConfig::default(); + assert!(!config.endpoint.is_empty()); + assert!(config.netuid > 0); + } + + #[test] + fn test_subtensor_config_testnet() { + let config = SubtensorConfig::testnet(123); + assert!(config.endpoint.contains("test")); + assert_eq!(config.netuid, 123); + } + + #[test] + fn test_subtensor_config_custom() { + let config = SubtensorConfig { + endpoint: "wss://custom.endpoint".to_string(), + netuid: 42, + use_commit_reveal: true, + version_key: 1000, + }; + + assert_eq!(config.netuid, 42); + assert!(config.use_commit_reveal); + } +} + +// ============================================================================ +// WEIGHT TYPES TESTS +// ============================================================================ + +mod weight_types { + + struct WeightAssignment { + uid: u16, + hotkey: String, + weight: f64, + } + + #[test] + fn test_weight_assignment_creation() { + let assignment = WeightAssignment { + uid: 1, + hotkey: "abc123".to_string(), + weight: 0.5, + }; + + assert_eq!(assignment.uid, 1); + assert!(assignment.weight >= 0.0 && assignment.weight <= 1.0); + } + + #[test] + fn test_weight_normalization() { + let weights = [ + WeightAssignment { + uid: 0, + hotkey: "a".to_string(), + weight: 0.3, + }, + WeightAssignment { + uid: 1, + hotkey: "b".to_string(), + weight: 0.3, + }, + WeightAssignment { + uid: 2, + hotkey: "c".to_string(), + weight: 0.4, + }, + ]; + + let sum: f64 = weights.iter().map(|w| w.weight).sum(); + assert!((sum - 1.0).abs() < 0.001); + } + + #[test] + fn test_weight_u16_conversion() { + let weight_f64 = 0.5; + let weight_u16 = (weight_f64 * 65535.0) as u16; + let back_f64 = weight_u16 as f64 / 65535.0; + + assert!((weight_f64 - back_f64).abs() < 0.001); + } + + #[test] + fn test_weight_edge_cases() { + // Zero weight + let w0 = WeightAssignment { + uid: 0, + hotkey: "a".to_string(), + weight: 0.0, + }; + assert_eq!(w0.weight, 0.0); + + // Full weight + let w1 = WeightAssignment { + uid: 1, + hotkey: "b".to_string(), + weight: 1.0, + }; + assert_eq!(w1.weight, 1.0); + } +} + +// ============================================================================ +// BLOCK SYNC TESTS +// ============================================================================ + +mod block_sync { + + struct BlockInfo { + number: u64, + hash: [u8; 32], + timestamp: chrono::DateTime, + } + + #[test] + fn test_block_info() { + let info = BlockInfo { + number: 1000, + hash: [0u8; 32], + timestamp: chrono::Utc::now(), + }; + + assert_eq!(info.number, 1000); + } + + #[test] + fn test_epoch_calculation() { + let tempo = 360; + let block = 1000; + let epoch = block / tempo; + assert_eq!(epoch, 2); + } + + #[test] + fn test_block_in_epoch() { + let tempo = 360; + let block = 1000; + let block_in_epoch = block % tempo; + assert_eq!(block_in_epoch, 280); + } + + #[test] + fn test_next_epoch_start() { + let tempo = 360; + let current_block = 1000; + let current_epoch = current_block / tempo; + let next_epoch_start = (current_epoch + 1) * tempo; + assert_eq!(next_epoch_start, 1080); + } +} + +// ============================================================================ +// VALIDATOR SYNC TESTS +// ============================================================================ + +mod validator_sync { + use super::*; + + struct ValidatorUpdate { + hotkey: Hotkey, + stake: Stake, + is_active: bool, + } + + struct MetagraphEntry { + uid: u16, + hotkey: String, + coldkey: String, + stake: u64, + rank: f64, + trust: f64, + consensus: f64, + incentive: f64, + dividends: f64, + emission: u64, + is_active: bool, + } + + #[test] + fn test_validator_update() { + let update = ValidatorUpdate { + hotkey: Keypair::generate().hotkey(), + stake: Stake::new(10_000_000_000), + is_active: true, + }; + + assert!(update.is_active); + assert!(update.stake.0 > 0); + } + + #[test] + fn test_metagraph_entry() { + let entry = MetagraphEntry { + uid: 1, + hotkey: "abc123".to_string(), + coldkey: "def456".to_string(), + stake: 1_000_000_000_000, + rank: 0.5, + trust: 0.8, + consensus: 0.9, + incentive: 0.7, + dividends: 0.1, + emission: 100, + is_active: true, + }; + + assert_eq!(entry.uid, 1); + assert!(entry.is_active); + } + + #[test] + fn test_stake_conversion() { + let stake_rao = 1_000_000_000; // 1 TAO + let stake_tao = stake_rao as f64 / 1_000_000_000.0; + assert_eq!(stake_tao, 1.0); + } + + #[test] + fn test_stake_threshold() { + let min_stake_tao = 1000.0; + let min_stake_rao = (min_stake_tao * 1_000_000_000.0) as u64; + + assert_eq!(min_stake_rao, 1_000_000_000_000); + } +} + +// ============================================================================ +// COMMIT-REVEAL TESTS +// ============================================================================ + +mod commit_reveal { + use super::*; + + #[test] + fn test_commitment_hash() { + let weights = vec![1u16, 2, 3]; + let salt = vec![0u16; 8]; + + // Simple hash simulation + let mut data = Vec::new(); + for w in &weights { + data.extend_from_slice(&w.to_le_bytes()); + } + for s in &salt { + data.extend_from_slice(&s.to_le_bytes()); + } + + let h = hash(&data); + assert_eq!(h.len(), 32); + } + + #[test] + fn test_salt_generation() { + use rand::Rng; + let salt: Vec = (0..8).map(|_| rand::thread_rng().gen()).collect(); + assert_eq!(salt.len(), 8); + } + + #[test] + fn test_commitment_verification() { + let weights = vec![100u16, 200, 300]; + let salt = vec![1u16, 2, 3, 4, 5, 6, 7, 8]; + + // Create commitment + let mut data = Vec::new(); + for w in &weights { + data.extend_from_slice(&w.to_le_bytes()); + } + for s in &salt { + data.extend_from_slice(&s.to_le_bytes()); + } + let commitment = hash(&data); + + // Verify same data produces same hash + let mut data2 = Vec::new(); + for w in &weights { + data2.extend_from_slice(&w.to_le_bytes()); + } + for s in &salt { + data2.extend_from_slice(&s.to_le_bytes()); + } + let commitment2 = hash(&data2); + + assert_eq!(commitment, commitment2); + } + + #[test] + fn test_different_weights_different_hash() { + let weights1 = vec![100u16, 200]; + let weights2 = vec![100u16, 201]; + let salt = vec![1u16; 8]; + + let hash1 = { + let mut data = Vec::new(); + for w in &weights1 { + data.extend_from_slice(&w.to_le_bytes()); + } + for s in &salt { + data.extend_from_slice(&s.to_le_bytes()); + } + hash(&data) + }; + + let hash2 = { + let mut data = Vec::new(); + for w in &weights2 { + data.extend_from_slice(&w.to_le_bytes()); + } + for s in &salt { + data.extend_from_slice(&s.to_le_bytes()); + } + hash(&data) + }; + + assert_ne!(hash1, hash2); + } +} + +// ============================================================================ +// MECHANISM WEIGHTS TESTS +// ============================================================================ + +mod mechanism_weights { + + struct MechanismWeightEntry { + mechanism_id: u16, + weight: f64, + } + + #[test] + fn test_mechanism_weight_entry() { + let entry = MechanismWeightEntry { + mechanism_id: 1, + weight: 0.5, + }; + + assert_eq!(entry.mechanism_id, 1); + assert!(entry.weight >= 0.0); + } + + #[test] + fn test_mechanism_weights_sum() { + let weights = [ + MechanismWeightEntry { + mechanism_id: 0, + weight: 0.3, + }, + MechanismWeightEntry { + mechanism_id: 1, + weight: 0.3, + }, + MechanismWeightEntry { + mechanism_id: 2, + weight: 0.4, + }, + ]; + + let sum: f64 = weights.iter().map(|w| w.weight).sum(); + assert!((sum - 1.0).abs() < 0.001); + } +} + +// ============================================================================ +// ERROR HANDLING TESTS +// ============================================================================ + +mod errors { + + #[derive(Debug)] + enum SubtensorError { + ConnectionFailed(String), + InvalidResponse(String), + Unauthorized, + } + + impl std::fmt::Display for SubtensorError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::ConnectionFailed(s) => write!(f, "Connection failed: {}", s), + Self::InvalidResponse(s) => write!(f, "Invalid response: {}", s), + Self::Unauthorized => write!(f, "Unauthorized"), + } + } + } + + #[derive(Debug)] + enum WeightError { + NoValidators, + CommitFailed(String), + RevealFailed(String), + } + + impl std::fmt::Display for WeightError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NoValidators => write!(f, "No validators"), + Self::CommitFailed(s) => write!(f, "Commit failed: {}", s), + Self::RevealFailed(s) => write!(f, "Reveal failed: {}", s), + } + } + } + + #[test] + fn test_subtensor_error_variants() { + let err = SubtensorError::ConnectionFailed("timeout".to_string()); + assert!(err.to_string().contains("timeout")); + + let err = SubtensorError::InvalidResponse("bad data".to_string()); + assert!(err.to_string().contains("bad data")); + + let err = SubtensorError::Unauthorized; + assert!(!err.to_string().is_empty()); + } + + #[test] + fn test_weight_error_variants() { + let err = WeightError::NoValidators; + assert!(!err.to_string().is_empty()); + + let err = WeightError::CommitFailed("reason".to_string()); + assert!(err.to_string().contains("reason")); + + let err = WeightError::RevealFailed("reason".to_string()); + assert!(err.to_string().contains("reason")); + } +} + +// ============================================================================ +// INTEGRATION TESTS +// ============================================================================ + +mod integration { + use platform_bittensor::{BittensorConfig, SubtensorClient, WeightSubmitter}; + use platform_challenge_sdk::WeightAssignment; + use serde::Deserialize; + use std::collections::HashMap; + use std::env; + use std::time::{Duration, SystemTime}; + use tokio::time::sleep; + + struct LocalWeightAssignment { + uid: u16, + hotkey: String, + weight: f64, + } + + #[test] + fn test_weight_submission_flow() { + let weights = [ + LocalWeightAssignment { + uid: 0, + hotkey: "a".to_string(), + weight: 0.5, + }, + LocalWeightAssignment { + uid: 1, + hotkey: "b".to_string(), + weight: 0.5, + }, + ]; + + let uids: Vec = weights.iter().map(|w| w.uid).collect(); + let values: Vec = weights + .iter() + .map(|w| (w.weight * 65535.0) as u16) + .collect(); + + assert_eq!(uids.len(), 2); + assert_eq!(values.len(), 2); + } + + #[test] + fn test_epoch_phase_calculation() { + let tempo = 360; + + let eval_block = 100; + let phase = if eval_block % tempo < (tempo * 3 / 4) { + "evaluation" + } else if eval_block % tempo < (tempo * 7 / 8) { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "evaluation"); + + let commit_block = 280; + let phase = if commit_block % tempo < (tempo * 3 / 4) { + "evaluation" + } else if commit_block % tempo < (tempo * 7 / 8) { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "commit"); + + let reveal_block = 330; + let phase = if reveal_block % tempo < (tempo * 3 / 4) { + "evaluation" + } else if reveal_block % tempo < (tempo * 7 / 8) { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "reveal"); + } + + #[derive(Debug, Deserialize)] + struct MockWeightsResponse { + pending: Vec, + revealed: Vec, + total_pending: usize, + total_revealed: usize, + } + + #[derive(Debug, Deserialize)] + struct MockPendingCommit { + hotkey: String, + netuid: u16, + uids: Vec, + commitment_hash: String, + commit_block: u64, + revealed: bool, + } + + #[derive(Debug, Deserialize)] + struct MockRevealedCommit { + hotkey: String, + netuid: u16, + uids: Vec, + weights: Option>, + reveal_block: Option, + revealed: bool, + } + + #[derive(Debug, Deserialize)] + struct JsonRpcResponse { + result: Option, + error: Option, + } + + fn test_endpoint() -> Option { + env::var("SUBTENSOR_ENDPOINT").ok() + } + + fn map_metagraph_hotkeys(metagraph: &platform_bittensor::Metagraph) -> HashMap { + use sp_core::crypto::Ss58Codec; + metagraph + .neurons + .iter() + .map(|(uid, neuron)| (neuron.hotkey.to_ss58check(), *uid as u16)) + .collect() + } + + async fn fetch_weights(endpoint: &str) -> MockWeightsResponse { + let base = endpoint + .replace("ws://", "http://") + .replace("wss://", "https://"); + let url = format!("{}/test/weights", base.trim_end_matches('/')); + reqwest::get(url) + .await + .expect("fetch weights") + .json::() + .await + .expect("parse weights") + } + + async fn wait_for_weight_change( + endpoint: &str, + expect_pending: usize, + expect_revealed: usize, + timeout: Duration, + ) -> MockWeightsResponse { + let start = SystemTime::now(); + loop { + let weights = fetch_weights(endpoint).await; + if weights.total_pending == expect_pending && weights.total_revealed == expect_revealed + { + return weights; + } + if start.elapsed().unwrap_or_default() > timeout { + return weights; + } + sleep(Duration::from_millis(200)).await; + } + } + + async fn reveal_with_mock_rpc( + endpoint: &str, + netuid: u16, + hotkey: &str, + uids: &[u16], + weights: &[u16], + salt: &str, + ) { + let base = endpoint + .replace("ws://", "http://") + .replace("wss://", "https://"); + let url = format!("{}/rpc", base.trim_end_matches('/')); + let payload = serde_json::json!({ + "jsonrpc": "2.0", + "method": "subtensor_revealWeights", + "params": [netuid, uids, weights, salt, hotkey], + "id": 1, + }); + let response = reqwest::Client::new() + .post(url) + .json(&payload) + .send() + .await + .expect("send reveal") + .json::() + .await + .expect("parse reveal"); + assert!(response.error.is_none()); + assert_eq!(response.result, Some(serde_json::Value::Bool(true))); + } + + #[tokio::test] + #[ignore] + async fn test_mock_subtensor_commit_reveal_flow() { + let endpoint = match test_endpoint() { + Some(endpoint) => endpoint, + None => return, + }; + + let netuid = 100; + let mut client = SubtensorClient::new(BittensorConfig { + endpoint: endpoint.clone(), + netuid, + use_commit_reveal: true, + version_key: 1, + }); + client.connect().await.expect("connect to mock subtensor"); + client.set_signer("//Alice").expect("set signer"); + + let metagraph = client.sync_metagraph().await.expect("sync metagraph"); + let uid_map = map_metagraph_hotkeys(metagraph); + let (hotkey, uid) = uid_map.iter().next().expect("hotkey mapping"); + assert!(client.get_uid_for_hotkey(hotkey).is_some()); + + let mut submitter = WeightSubmitter::new(client, None); + submitter.set_epoch(1); + + let weights = vec![WeightAssignment::new(hotkey.clone(), 1.0)]; + let commit_tx = submitter + .submit_weights(&weights) + .await + .expect("commit weights"); + assert!(!commit_tx.is_empty()); + + let pending = wait_for_weight_change(&endpoint, 1, 0, Duration::from_secs(10)).await; + assert_eq!(pending.total_pending, 1); + assert_eq!(pending.total_revealed, 0); + let pending_commit = pending.pending.first().expect("pending commit"); + assert_eq!(pending_commit.hotkey, *hotkey); + assert_eq!(pending_commit.netuid, netuid); + assert_eq!(pending_commit.uids, vec![*uid]); + assert!(!pending_commit.commitment_hash.is_empty()); + assert!(!pending_commit.revealed); + + let reveal_weights = vec![65535u16; pending_commit.uids.len()]; + reveal_with_mock_rpc( + &endpoint, + netuid, + hotkey, + &pending_commit.uids, + &reveal_weights, + &pending_commit.commitment_hash, + ) + .await; + + let revealed = wait_for_weight_change(&endpoint, 0, 1, Duration::from_secs(10)).await; + assert_eq!(revealed.total_pending, 0); + assert_eq!(revealed.total_revealed, 1); + let reveal_commit = revealed.revealed.first().expect("revealed commit"); + assert_eq!(reveal_commit.hotkey, *hotkey); + assert_eq!(reveal_commit.netuid, netuid); + assert_eq!(reveal_commit.uids, vec![*uid]); + assert!(reveal_commit.revealed); + assert!(reveal_commit.reveal_block.is_some()); + let weights = reveal_commit.weights.as_ref().expect("weights present"); + assert_eq!(weights, &reveal_weights); + } +} diff --git a/tests/blockchain_state_tests.rs b/tests/blockchain_state_tests.rs new file mode 100644 index 000000000..663a44944 --- /dev/null +++ b/tests/blockchain_state_tests.rs @@ -0,0 +1,615 @@ +//! Blockchain State Verification Tests +//! +//! Comprehensive tests for: +//! - Bittensor block linking +//! - Merkle proof computation and verification +//! - State hash integrity +//! - State serialization + +use platform_core::{ChallengeId, Hotkey, Keypair}; +use platform_p2p_consensus::{ + build_merkle_proof, compute_merkle_root, verify_merkle_proof, ChainState, ChallengeConfig, + EvaluationRecord, StateManager, +}; +use std::collections::HashMap; + +// ============================================================================ +// BITTENSOR BLOCK LINKING TESTS +// ============================================================================ + +#[test] +fn test_state_links_to_bittensor_block() { + let mut state = ChainState::new(100); + + // Initial state should have zero block + assert_eq!(state.linked_block(), 0); + assert_eq!(state.bittensor_block_hash, [0u8; 32]); + + // Link to a specific block + let block_number: u64 = 12345; + let block_hash: [u8; 32] = [0xAB; 32]; + state.link_to_bittensor_block(block_number, block_hash); + + // Verify linkage + assert_eq!(state.linked_block(), block_number); + assert_eq!(state.bittensor_block, block_number); + assert_eq!(state.bittensor_block_hash, block_hash); +} + +#[test] +fn test_link_updates_sequence_number() { + let mut state = ChainState::new(100); + let initial_sequence = state.sequence; + + // Link to block + state.link_to_bittensor_block(100, [0x01; 32]); + + // Sequence should have incremented + assert_eq!(state.sequence, initial_sequence + 1); +} + +#[test] +fn test_state_hash_changes_on_bittensor_link() { + let mut state = ChainState::new(100); + let hash_before = state.get_state_hash(); + + // Link to block + state.link_to_bittensor_block(100, [0x01; 32]); + let hash_after = state.get_state_hash(); + + // Hash must change + assert_ne!(hash_before, hash_after); +} + +#[test] +fn test_multiple_block_links_track_progression() { + let mut state = ChainState::new(100); + + // Link to first block + state.link_to_bittensor_block(1000, [0x01; 32]); + assert_eq!(state.linked_block(), 1000); + let seq_after_first = state.sequence; + + // Link to second block + state.link_to_bittensor_block(2000, [0x02; 32]); + assert_eq!(state.linked_block(), 2000); + assert_eq!(state.bittensor_block_hash, [0x02; 32]); + assert_eq!(state.sequence, seq_after_first + 1); + + // Link to third block + state.link_to_bittensor_block(3000, [0x03; 32]); + assert_eq!(state.linked_block(), 3000); + assert_eq!(state.bittensor_block_hash, [0x03; 32]); +} + +#[test] +fn test_bittensor_link_with_real_block_hash() { + let mut state = ChainState::new(100); + + // Simulate a real block hash (sha256 of block data) + let real_hash: [u8; 32] = [ + 0xDE, 0xAD, 0xBE, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11, 0x22, 0x33, + 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x01, 0x02, + 0x03, 0x04, + ]; + + state.link_to_bittensor_block(9999999, real_hash); + + assert_eq!(state.bittensor_block, 9999999); + assert_eq!(state.bittensor_block_hash, real_hash); +} + +// ============================================================================ +// MERKLE PROOF TESTS +// ============================================================================ + +#[test] +fn test_merkle_root_computation_deterministic() { + let leaves: Vec<[u8; 32]> = (0..4).map(|i| [i as u8; 32]).collect(); + + let root1 = compute_merkle_root(&leaves); + let root2 = compute_merkle_root(&leaves); + + // Same leaves should produce same root + assert_eq!(root1, root2); + assert_ne!(root1, [0u8; 32]); +} + +#[test] +fn test_merkle_root_changes_with_different_leaves() { + let leaves1: Vec<[u8; 32]> = (0..4).map(|i| [i as u8; 32]).collect(); + let leaves2: Vec<[u8; 32]> = (1..5).map(|i| [i as u8; 32]).collect(); + + let root1 = compute_merkle_root(&leaves1); + let root2 = compute_merkle_root(&leaves2); + + // Different leaves should produce different roots + assert_ne!(root1, root2); +} + +#[test] +fn test_merkle_proof_verification_valid() { + let leaves: Vec<[u8; 32]> = (0..8).map(|i| [i as u8; 32]).collect(); + + // Build and verify proof for each leaf + for (i, leaf) in leaves.iter().enumerate() { + let proof = build_merkle_proof(&leaves, i).expect("Failed to build proof"); + assert!( + verify_merkle_proof(leaf, &proof), + "Valid proof for leaf {} should verify", + i + ); + } +} + +#[test] +fn test_merkle_proof_verification_invalid() { + let leaves: Vec<[u8; 32]> = (0..4).map(|i| [i as u8; 32]).collect(); + + let proof = build_merkle_proof(&leaves, 0).expect("Failed to build proof"); + + // Tampered leaf should not verify + let tampered_leaf: [u8; 32] = [0xFF; 32]; + assert!( + !verify_merkle_proof(&tampered_leaf, &proof), + "Tampered leaf should not verify" + ); + + // Original leaf should still verify + assert!( + verify_merkle_proof(&leaves[0], &proof), + "Original leaf should verify" + ); +} + +#[test] +fn test_merkle_proof_single_leaf() { + let leaves: Vec<[u8; 32]> = vec![[0x42; 32]]; + + let root = compute_merkle_root(&leaves); + // Single leaf should be its own root + assert_eq!(root, leaves[0]); + + let proof = build_merkle_proof(&leaves, 0).expect("Failed to build proof for single leaf"); + assert!( + verify_merkle_proof(&leaves[0], &proof), + "Single leaf proof should verify" + ); +} + +#[test] +fn test_merkle_proof_empty_leaves() { + let leaves: Vec<[u8; 32]> = vec![]; + + let root = compute_merkle_root(&leaves); + // Empty tree should return zero hash + assert_eq!(root, [0u8; 32]); + + // Building proof for empty tree should return None + let proof = build_merkle_proof(&leaves, 0); + assert!( + proof.is_none(), + "Should not build proof for empty leaf array" + ); +} + +#[test] +fn test_merkle_proof_out_of_bounds() { + let leaves: Vec<[u8; 32]> = (0..4).map(|i| [i as u8; 32]).collect(); + + // Index out of bounds should return None + let proof = build_merkle_proof(&leaves, 10); + assert!(proof.is_none(), "Out of bounds index should return None"); +} + +#[test] +fn test_merkle_proof_odd_number_of_leaves() { + // Odd number of leaves tests edge case in tree construction + let leaves: Vec<[u8; 32]> = (0..5).map(|i| [i as u8; 32]).collect(); + + let root = compute_merkle_root(&leaves); + assert_ne!(root, [0u8; 32]); + + // All proofs should still verify + for (i, leaf) in leaves.iter().enumerate() { + let proof = build_merkle_proof(&leaves, i).expect("Failed to build proof"); + assert!( + verify_merkle_proof(leaf, &proof), + "Proof for leaf {} should verify", + i + ); + } +} + +#[test] +fn test_merkle_proof_power_of_two_leaves() { + // Test perfect binary tree + let leaves: Vec<[u8; 32]> = (0..16).map(|i| [i as u8; 32]).collect(); + + let root = compute_merkle_root(&leaves); + assert_ne!(root, [0u8; 32]); + + // Verify first, middle, and last + for &i in &[0, 7, 8, 15] { + let proof = build_merkle_proof(&leaves, i).expect("Failed to build proof"); + assert!(verify_merkle_proof(&leaves[i], &proof)); + } +} + +// ============================================================================ +// STATE VERIFICATION TESTS +// ============================================================================ + +#[test] +fn test_state_serialization_preserves_bittensor_link() { + let mut state = ChainState::new(100); + + // Link to bittensor block + let block_number: u64 = 54321; + let block_hash: [u8; 32] = [0xBE; 32]; + state.link_to_bittensor_block(block_number, block_hash); + + // Serialize and deserialize + let bytes = state.to_bytes().expect("Serialization failed"); + let recovered = ChainState::from_bytes(&bytes).expect("Deserialization failed"); + + // Verify block link preserved + assert_eq!(recovered.bittensor_block, block_number); + assert_eq!(recovered.bittensor_block_hash, block_hash); + assert_eq!(recovered.linked_block(), block_number); + assert_eq!(recovered.sequence, state.sequence); +} + +#[test] +fn test_state_hash_unique_per_modification() { + let mut state = ChainState::new(100); + let mut seen_hashes = std::collections::HashSet::new(); + + // Initial hash + seen_hashes.insert(state.get_state_hash()); + + // Add validator + let keypair1 = Keypair::generate(); + state.update_validator(keypair1.hotkey(), 1_000_000); + assert!( + seen_hashes.insert(state.get_state_hash()), + "Hash should be unique after adding validator" + ); + + // Link to block + state.link_to_bittensor_block(100, [0x01; 32]); + assert!( + seen_hashes.insert(state.get_state_hash()), + "Hash should be unique after linking block" + ); + + // Add challenge + let config = ChallengeConfig { + id: ChallengeId::new(), + name: "Test Challenge".to_string(), + weight: 50, + is_active: true, + creator: Hotkey([0u8; 32]), + created_at: chrono::Utc::now().timestamp_millis(), + }; + state.add_challenge(config); + assert!( + seen_hashes.insert(state.get_state_hash()), + "Hash should be unique after adding challenge" + ); + + // Transition epoch + state.next_epoch(); + assert!( + seen_hashes.insert(state.get_state_hash()), + "Hash should be unique after epoch transition" + ); + + // All 5 states had unique hashes + assert_eq!(seen_hashes.len(), 5); +} + +#[test] +fn test_epoch_transition_maintains_bittensor_link() { + let mut state = ChainState::new(100); + + // Link to block + let block_number: u64 = 12345; + let block_hash: [u8; 32] = [0xAB; 32]; + state.link_to_bittensor_block(block_number, block_hash); + + // Transition epoch + state.next_epoch(); + + // Block link should be preserved + assert_eq!(state.linked_block(), block_number); + assert_eq!(state.bittensor_block_hash, block_hash); + assert_eq!(state.epoch, 1); +} + +#[test] +fn test_validator_update_changes_hash_preserves_block() { + let mut state = ChainState::new(100); + + // Link to block + let block_number: u64 = 10000; + let block_hash: [u8; 32] = [0xCD; 32]; + state.link_to_bittensor_block(block_number, block_hash); + + let hash_before = state.get_state_hash(); + + // Add validators + let keypair1 = Keypair::generate(); + let keypair2 = Keypair::generate(); + state.update_validator(keypair1.hotkey(), 1_000_000); + state.update_validator(keypair2.hotkey(), 2_000_000); + + let hash_after = state.get_state_hash(); + + // Hash should change + assert_ne!(hash_before, hash_after); + + // Block link should be preserved + assert_eq!(state.linked_block(), block_number); + assert_eq!(state.bittensor_block_hash, block_hash); +} + +#[test] +fn test_state_hash_hex_format() { + let state = ChainState::new(100); + let hex = state.hash_hex(); + + // Should be 64 hex characters (32 bytes) + assert_eq!(hex.len(), 64); + assert!(hex.chars().all(|c| c.is_ascii_hexdigit())); +} + +#[test] +fn test_block_height_tracks_sequence() { + let mut state = ChainState::new(100); + + assert_eq!(state.block_height(), 0); + + state.increment_sequence(); + assert_eq!(state.block_height(), 1); + + state.link_to_bittensor_block(100, [0x01; 32]); + assert_eq!(state.block_height(), 2); + + let keypair = Keypair::generate(); + state.update_validator(keypair.hotkey(), 1_000_000); + assert_eq!(state.block_height(), 3); +} + +// ============================================================================ +// INTEGRATION TESTS +// ============================================================================ + +#[test] +fn test_full_state_lifecycle_with_block_linking() { + // Create state with custom sudo key for testing + let sudo_keypair = Keypair::generate(); + let mut state = ChainState::with_sudo(sudo_keypair.hotkey(), 100); + + // Initial state + assert_eq!(state.epoch, 0); + assert_eq!(state.linked_block(), 0); + + // Link to genesis block + state.link_to_bittensor_block(1, [0x01; 32]); + assert_eq!(state.linked_block(), 1); + let seq_after_genesis = state.sequence; + + // Add validators + let validator1 = Keypair::generate(); + let validator2 = Keypair::generate(); + state.update_validator(validator1.hotkey(), 1_000_000); + state.update_validator(validator2.hotkey(), 2_000_000); + assert_eq!(state.validators.len(), 2); + assert!(state.sequence > seq_after_genesis); + + // Add challenge + let challenge = ChallengeConfig { + id: ChallengeId::new(), + name: "Integration Test Challenge".to_string(), + weight: 100, + is_active: true, + creator: sudo_keypair.hotkey(), + created_at: chrono::Utc::now().timestamp_millis(), + }; + state.add_challenge(challenge); + assert_eq!(state.challenges.len(), 1); + + // Link to new block (simulating progression) + let hash_before_new_block = state.get_state_hash(); + state.link_to_bittensor_block(1000, [0x10; 32]); + let hash_after_new_block = state.get_state_hash(); + + // Verify state integrity + assert_ne!(hash_before_new_block, hash_after_new_block); + assert_eq!(state.linked_block(), 1000); + assert_eq!(state.validators.len(), 2); + assert_eq!(state.challenges.len(), 1); + + // Serialize and verify + let bytes = state.to_bytes().expect("Serialization failed"); + let recovered = ChainState::from_bytes(&bytes).expect("Deserialization failed"); + + assert_eq!(recovered.linked_block(), 1000); + assert_eq!(recovered.validators.len(), 2); + assert_eq!(recovered.challenges.len(), 1); +} + +#[test] +fn test_evaluation_finalization_with_merkle_proof() { + // Create state + let mut state = ChainState::new(100); + + // Add validators + let validator1 = Keypair::generate(); + let validator2 = Keypair::generate(); + state.update_validator(validator1.hotkey(), 1_000_000); + state.update_validator(validator2.hotkey(), 2_000_000); + + // Create evaluation records with unique hashes + let evaluation_hashes: Vec<[u8; 32]> = (0..4) + .map(|i| { + let mut hash = [0u8; 32]; + hash[0] = i as u8; + hash[31] = i as u8; + hash + }) + .collect(); + + // Compute merkle root for evaluations + let root = compute_merkle_root(&evaluation_hashes); + assert_ne!(root, [0u8; 32]); + + // Build and verify proof for each evaluation + for (i, hash) in evaluation_hashes.iter().enumerate() { + let proof = build_merkle_proof(&evaluation_hashes, i).expect("Failed to build proof"); + + // Proof should verify + assert!( + verify_merkle_proof(hash, &proof), + "Proof for evaluation {} should verify", + i + ); + + // Proof root should match computed root + assert_eq!(proof.root, root); + } +} + +#[test] +fn test_state_manager_with_block_linking() { + let manager = StateManager::for_netuid(100); + + // Link to block via manager + manager.apply(|state| { + state.link_to_bittensor_block(500, [0x50; 32]); + }); + + // Verify via snapshot + let snapshot = manager.snapshot(); + assert_eq!(snapshot.linked_block(), 500); + assert_eq!(snapshot.bittensor_block_hash, [0x50; 32]); + assert_eq!(manager.sequence(), 1); +} + +#[test] +fn test_state_sync_preserves_bittensor_link() { + let manager = StateManager::for_netuid(100); + + // Create a new state with higher sequence + let mut new_state = ChainState::new(100); + new_state.link_to_bittensor_block(9999, [0x99; 32]); + new_state.increment_sequence(); // Sequence = 2 + + // Apply sync + manager + .apply_sync_state(new_state.clone()) + .expect("Sync should succeed"); + + // Verify synced state + let snapshot = manager.snapshot(); + assert_eq!(snapshot.linked_block(), 9999); + assert_eq!(snapshot.bittensor_block_hash, [0x99; 32]); +} + +#[test] +fn test_merkle_proof_for_validator_hashes() { + // Simulate validator hotkeys as merkle leaves + let validators: Vec = (0..8).map(|_| Keypair::generate()).collect(); + let leaves: Vec<[u8; 32]> = validators.iter().map(|k| k.hotkey().0).collect(); + + let root = compute_merkle_root(&leaves); + assert_ne!(root, [0u8; 32], "Merkle root should not be empty"); + + // Verify each validator's inclusion + for (i, validator) in validators.iter().enumerate() { + let leaf = validator.hotkey().0; + let proof = build_merkle_proof(&leaves, i).expect("Failed to build proof"); + assert!( + verify_merkle_proof(&leaf, &proof), + "Validator {} should be provable in merkle tree", + i + ); + } +} + +#[test] +fn test_state_determinism() { + // Two states with same operations should have same hash + let mut state1 = ChainState::new(100); + let mut state2 = ChainState::new(100); + + // Both start with same hash + let initial_hash1 = state1.get_state_hash(); + let initial_hash2 = state2.get_state_hash(); + assert_eq!(initial_hash1, initial_hash2); + + // Same operations in same order + let hotkey = Hotkey([0x42; 32]); + state1.update_validator(hotkey.clone(), 1_000_000); + state2.update_validator(hotkey.clone(), 1_000_000); + + // Should still have same hash (sequence-based) + // Note: Hash is based on counts and sequence, so adding same validator + // at same sequence should produce same hash + assert_eq!(state1.sequence, state2.sequence); +} + +#[test] +fn test_challenge_removal_changes_hash() { + let mut state = ChainState::new(100); + + let challenge_id = ChallengeId::new(); + let config = ChallengeConfig { + id: challenge_id, + name: "Removable Challenge".to_string(), + weight: 50, + is_active: true, + creator: Hotkey([0u8; 32]), + created_at: chrono::Utc::now().timestamp_millis(), + }; + + state.add_challenge(config); + let hash_with_challenge = state.get_state_hash(); + + state.remove_challenge(&challenge_id); + let hash_without_challenge = state.get_state_hash(); + + assert_ne!(hash_with_challenge, hash_without_challenge); +} + +#[test] +fn test_evaluation_record_lifecycle() { + let mut state = ChainState::new(100); + + // Add validator + let validator_keypair = Keypair::generate(); + state.update_validator(validator_keypair.hotkey(), 1_000_000); + + // Create evaluation record + let record = EvaluationRecord { + submission_id: "test_submission_001".to_string(), + challenge_id: ChallengeId::new(), + miner: Keypair::generate().hotkey(), + agent_hash: "agent_hash_123".to_string(), + evaluations: HashMap::new(), + aggregated_score: None, + finalized: false, + created_at: chrono::Utc::now().timestamp_millis(), + finalized_at: None, + }; + + let hash_before = state.get_state_hash(); + state.add_evaluation(record); + let hash_after = state.get_state_hash(); + + assert_ne!(hash_before, hash_after); + assert!(state + .pending_evaluations + .contains_key("test_submission_001")); +} diff --git a/tests/checkpoint_tests.rs b/tests/checkpoint_tests.rs new file mode 100644 index 000000000..1bccf51e9 --- /dev/null +++ b/tests/checkpoint_tests.rs @@ -0,0 +1,524 @@ +//! Integration tests for checkpoint and restoration system +//! +//! Tests for verifying the checkpoint/restoration system works correctly end-to-end. + +use platform_core::{ + ChallengeId, CheckpointData, CheckpointManager, CompletedEvaluationState, Hotkey, + PendingEvaluationState, RestorationManager, RestorationOptions, WeightVoteState, +}; +use std::collections::HashMap; +use tempfile::tempdir; + +// ============================================================================ +// TEST HELPERS +// ============================================================================ + +/// Create test checkpoint data with realistic content +fn create_test_data() -> CheckpointData { + let mut data = CheckpointData::new(100, 5, 100); + + // Add pending evaluations + for i in 0..5 { + data.pending_evaluations.push(PendingEvaluationState { + submission_id: format!("submission_{}", i), + challenge_id: ChallengeId::new(), + miner: Hotkey([i as u8; 32]), + submission_hash: format!("hash_{}", i), + scores: { + let mut scores = HashMap::new(); + scores.insert(Hotkey([1u8; 32]), 0.85); + scores.insert(Hotkey([2u8; 32]), 0.90); + scores + }, + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + } + + // Add completed evaluations + for i in 0..3 { + data.completed_evaluations.push(CompletedEvaluationState { + submission_id: format!("completed_{}", i), + challenge_id: ChallengeId::new(), + final_score: 0.87 + (i as f64 * 0.01), + epoch: 5, + completed_at: chrono::Utc::now().timestamp_millis(), + }); + } + + // Add weight votes + data.weight_votes = Some(WeightVoteState { + epoch: 5, + netuid: 100, + votes: { + let mut votes = HashMap::new(); + votes.insert(Hotkey([1u8; 32]), vec![(0, 1000), (1, 2000)]); + votes.insert(Hotkey([2u8; 32]), vec![(0, 1500), (1, 1500)]); + votes + }, + finalized: false, + final_weights: None, + }); + + data.bittensor_block = 12345; + data +} + +// ============================================================================ +// CHECKPOINT ROUNDTRIP TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_roundtrip() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 10).expect("Failed to create manager"); + + let original_data = create_test_data(); + + // Create checkpoint + let path = manager + .create_checkpoint(&original_data) + .expect("Failed to create checkpoint"); + assert!(path.exists()); + + // Load checkpoint + let (header, loaded_data) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint found"); + + // Verify data integrity + assert_eq!(loaded_data.sequence, original_data.sequence); + assert_eq!(loaded_data.epoch, original_data.epoch); + assert_eq!(loaded_data.netuid, original_data.netuid); + assert_eq!( + loaded_data.pending_evaluations.len(), + original_data.pending_evaluations.len() + ); + assert_eq!( + loaded_data.completed_evaluations.len(), + original_data.completed_evaluations.len() + ); + assert!(loaded_data.weight_votes.is_some()); + assert_eq!(loaded_data.bittensor_block, original_data.bittensor_block); + + // Verify header has correct sequence + assert_eq!(header.sequence, 1); +} + +// ============================================================================ +// MULTIPLE CHECKPOINTS TESTS +// ============================================================================ + +#[test] +fn test_multiple_checkpoints() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + // Create multiple checkpoints + for i in 0..10 { + let mut data = CheckpointData::new(i, i / 2, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: format!("sub_{}", i), + challenge_id: ChallengeId::new(), + miner: Hotkey([i as u8; 32]), + submission_hash: format!("hash_{}", i), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + manager + .create_checkpoint(&data) + .expect("Failed to create checkpoint"); + } + + // Should only keep 5 checkpoints + let checkpoints = manager.list_checkpoints().expect("Failed to list"); + assert_eq!(checkpoints.len(), 5); + + // Latest should be sequence 10 + let (header, latest) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + assert_eq!(latest.sequence, 9); + assert_eq!(header.sequence, 10); +} + +// ============================================================================ +// RESTORATION TESTS +// ============================================================================ + +#[test] +fn test_restoration_with_options() { + let dir = tempdir().expect("Failed to create temp dir"); + + // Create checkpoint + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + let data = create_test_data(); + manager + .create_checkpoint(&data) + .expect("Failed to create checkpoint"); + + // Restore with options + let options = RestorationOptions::new() + .without_max_age() + .with_validation(true); + + let restoration = + RestorationManager::new(dir.path(), options).expect("Failed to create restoration manager"); + + let result = restoration.restore_latest().expect("Failed to restore"); + assert!(result.is_some()); + + let (res, restored_data) = result.unwrap(); + assert!(res.success); + assert_eq!(restored_data.pending_evaluations.len(), 5); + assert_eq!(restored_data.completed_evaluations.len(), 3); +} + +#[test] +fn test_restoration_empty() { + let dir = tempdir().expect("Failed to create temp dir"); + + let restoration = RestorationManager::with_defaults(dir.path()).expect("Failed to create"); + let result = restoration.restore_latest().expect("Failed to restore"); + + assert!(result.is_none()); +} + +// ============================================================================ +// HASH VERIFICATION TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_hash_verification() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + let data = create_test_data(); + let path = manager.create_checkpoint(&data).expect("Failed to create"); + + // Corrupt the file + let mut content = std::fs::read(&path).expect("Failed to read"); + if content.len() > 100 { + content[100] ^= 0xFF; // Flip bits + } + std::fs::write(&path, content).expect("Failed to write"); + + // Loading should fail due to hash mismatch + let result = manager.load_checkpoint(1); + assert!(result.is_err()); +} + +// ============================================================================ +// WEIGHT VOTES TESTS +// ============================================================================ + +#[test] +fn test_weight_votes_persistence() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + let mut data = CheckpointData::new(1, 5, 100); + data.weight_votes = Some(WeightVoteState { + epoch: 5, + netuid: 100, + votes: { + let mut v = HashMap::new(); + v.insert(Hotkey([1u8; 32]), vec![(0, 1000), (1, 2000), (2, 3000)]); + v.insert(Hotkey([2u8; 32]), vec![(0, 1500), (1, 2500), (2, 2000)]); + v.insert(Hotkey([3u8; 32]), vec![(0, 2000), (1, 2000), (2, 2000)]); + v + }, + finalized: true, + final_weights: Some(vec![(0, 4500), (1, 6500), (2, 7000)]), + }); + + manager.create_checkpoint(&data).expect("Failed to create"); + + let (_, loaded) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + + let votes = loaded.weight_votes.expect("No weight votes"); + assert!(votes.finalized); + assert_eq!(votes.votes.len(), 3); + assert_eq!(votes.final_weights.as_ref().unwrap().len(), 3); +} + +// ============================================================================ +// CHECKPOINT INFO TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_info() { + let dir = tempdir().expect("Failed to create temp dir"); + + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + let data = create_test_data(); + manager.create_checkpoint(&data).expect("Failed to create"); + + let restoration = RestorationManager::with_defaults(dir.path()).expect("Failed to create"); + let infos = restoration.list_available().expect("Failed to list"); + + assert_eq!(infos.len(), 1); + assert_eq!(infos[0].epoch, 5); + assert_eq!(infos[0].netuid, 100); + assert_eq!(infos[0].pending_count, 5); + assert_eq!(infos[0].completed_count, 3); + assert!(infos[0].has_weight_votes); + assert_eq!(infos[0].bittensor_block, 12345); +} + +// ============================================================================ +// SCORING PERSISTENCE TESTS +// ============================================================================ + +#[test] +fn test_pending_evaluation_scores_persistence() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + let mut data = CheckpointData::new(1, 5, 100); + let mut scores = HashMap::new(); + scores.insert(Hotkey([10u8; 32]), 0.95); + scores.insert(Hotkey([20u8; 32]), 0.87); + scores.insert(Hotkey([30u8; 32]), 0.92); + + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "scored_submission".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([5u8; 32]), + submission_hash: "hash_scored".to_string(), + scores, + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: true, + }); + + manager.create_checkpoint(&data).expect("Failed to create"); + + let (_, loaded) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + + let pending = &loaded.pending_evaluations[0]; + assert_eq!(pending.scores.len(), 3); + assert_eq!(pending.scores.get(&Hotkey([10u8; 32])), Some(&0.95)); + assert_eq!(pending.scores.get(&Hotkey([20u8; 32])), Some(&0.87)); + assert_eq!(pending.scores.get(&Hotkey([30u8; 32])), Some(&0.92)); + assert!(pending.finalizing); +} + +// ============================================================================ +// SEQUENCE MANAGEMENT TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_sequence_resume() { + let dir = tempdir().expect("Failed to create temp dir"); + + // First manager creates checkpoints + { + let mut manager = CheckpointManager::new(dir.path(), 10).expect("Failed to create manager"); + for i in 0..5 { + let data = CheckpointData::new(i, i, 100); + manager.create_checkpoint(&data).expect("Failed to create"); + } + assert_eq!(manager.current_sequence(), 5); + } + + // New manager should resume from the latest sequence + { + let manager = CheckpointManager::new(dir.path(), 10).expect("Failed to create manager"); + assert_eq!(manager.current_sequence(), 5); + } +} + +#[test] +fn test_load_specific_checkpoint() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 10).expect("Failed to create manager"); + + // Create 3 checkpoints with different epochs + for i in 0..3 { + let mut data = CheckpointData::new(i, i * 10, 100); + data.metadata + .insert("marker".to_string(), format!("checkpoint_{}", i)); + manager.create_checkpoint(&data).expect("Failed to create"); + } + + // Load specific checkpoint (sequence 2) + let (header, data) = manager + .load_checkpoint(2) + .expect("Failed to load") + .expect("Not found"); + assert_eq!(header.sequence, 2); + assert_eq!(data.epoch, 10); + assert_eq!( + data.metadata.get("marker"), + Some(&"checkpoint_1".to_string()) + ); +} + +// ============================================================================ +// METADATA TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_metadata_persistence() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + let mut data = CheckpointData::new(1, 5, 100); + data.metadata + .insert("version".to_string(), "1.0.0".to_string()); + data.metadata + .insert("node_id".to_string(), "validator_1".to_string()); + data.metadata + .insert("custom_key".to_string(), "custom_value".to_string()); + + manager.create_checkpoint(&data).expect("Failed to create"); + + let (_, loaded) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + + assert_eq!(loaded.metadata.len(), 3); + assert_eq!(loaded.metadata.get("version"), Some(&"1.0.0".to_string())); + assert_eq!( + loaded.metadata.get("node_id"), + Some(&"validator_1".to_string()) + ); + assert_eq!( + loaded.metadata.get("custom_key"), + Some(&"custom_value".to_string()) + ); +} + +// ============================================================================ +// COMPLETED EVALUATION TESTS +// ============================================================================ + +#[test] +fn test_completed_evaluations_persistence() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + let challenge_id = ChallengeId::new(); + let mut data = CheckpointData::new(1, 5, 100); + + for i in 0..5 { + data.completed_evaluations.push(CompletedEvaluationState { + submission_id: format!("completed_{}", i), + challenge_id, + final_score: 0.80 + (i as f64 * 0.04), + epoch: 5, + completed_at: chrono::Utc::now().timestamp_millis(), + }); + } + + manager.create_checkpoint(&data).expect("Failed to create"); + + let (_, loaded) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + + assert_eq!(loaded.completed_evaluations.len(), 5); + + // Verify score ordering is preserved + for (i, eval) in loaded.completed_evaluations.iter().enumerate() { + let expected_score = 0.80 + (i as f64 * 0.04); + assert!((eval.final_score - expected_score).abs() < 0.001); + assert_eq!(eval.challenge_id, challenge_id); + } +} + +// ============================================================================ +// EMPTY STATE TESTS +// ============================================================================ + +#[test] +fn test_checkpoint_with_empty_state() { + let dir = tempdir().expect("Failed to create temp dir"); + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + + // Empty checkpoint data + let data = CheckpointData::new(0, 0, 100); + + manager.create_checkpoint(&data).expect("Failed to create"); + + let (_, loaded) = manager + .load_latest() + .expect("Failed to load") + .expect("No checkpoint"); + + assert_eq!(loaded.sequence, 0); + assert_eq!(loaded.epoch, 0); + assert!(loaded.pending_evaluations.is_empty()); + assert!(loaded.completed_evaluations.is_empty()); + assert!(loaded.weight_votes.is_none()); + assert!(loaded.metadata.is_empty()); +} + +// ============================================================================ +// RESTORATION VALIDATION TESTS +// ============================================================================ + +#[test] +fn test_restoration_validates_epoch() { + let dir = tempdir().expect("Failed to create temp dir"); + + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + let mut data = CheckpointData::new(1, 2_000_000, 100); // Unreasonably high epoch + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "test".to_string(), + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "hash".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + manager.create_checkpoint(&data).expect("Failed to create"); + + // With validation enabled, this should fail + let options = RestorationOptions::new() + .without_max_age() + .with_validation(true); + + let restoration = RestorationManager::new(dir.path(), options).expect("Failed to create"); + let result = restoration.restore_latest(); + assert!(result.is_err()); +} + +#[test] +fn test_restoration_validates_submission_id() { + let dir = tempdir().expect("Failed to create temp dir"); + + let mut manager = CheckpointManager::new(dir.path(), 5).expect("Failed to create manager"); + let mut data = CheckpointData::new(1, 5, 100); + data.pending_evaluations.push(PendingEvaluationState { + submission_id: "".to_string(), // Empty submission_id is invalid + challenge_id: ChallengeId::new(), + miner: Hotkey([1u8; 32]), + submission_hash: "hash".to_string(), + scores: HashMap::new(), + created_at: chrono::Utc::now().timestamp_millis(), + finalizing: false, + }); + manager.create_checkpoint(&data).expect("Failed to create"); + + // With validation enabled, this should fail + let options = RestorationOptions::new() + .without_max_age() + .with_validation(true); + + let restoration = RestorationManager::new(dir.path(), options).expect("Failed to create"); + let result = restoration.restore_latest(); + assert!(result.is_err()); +} diff --git a/tests/e2e_tests.rs b/tests/e2e_tests.rs new file mode 100644 index 000000000..ab76bb076 --- /dev/null +++ b/tests/e2e_tests.rs @@ -0,0 +1,266 @@ +//! End-to-End Integration Tests for Platform +//! +//! These tests verify the complete flow of the validator system. + +use parking_lot::RwLock; +use platform_core::*; +use platform_storage::*; +use std::sync::Arc; +use tempfile::tempdir; + +// ============================================================================ +// E2E: STORAGE FLOW +// ============================================================================ + +#[test] +fn test_e2e_storage_state_persistence() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + // Add validators + for _ in 0..4 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + state.add_validator(info).unwrap(); + } + + state.increment_block(); + state.increment_block(); + + // Save + storage.save_state(&state).unwrap(); + + // Load and verify + let loaded = storage.load_state().unwrap().unwrap(); + assert_eq!(loaded.block_height, 2); + assert_eq!(loaded.validators.len(), 4); +} + +#[test] +fn test_e2e_storage_challenge_lifecycle() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + // Create challenge + let challenge = Challenge::new( + "Test Challenge".into(), + "A test challenge".into(), + b"print('hello')".to_vec(), + sudo.hotkey(), + ChallengeConfig::default(), + ); + + let challenge_id = challenge.id; + + // Add to state + state.add_challenge(challenge.clone()); + assert!(state.get_challenge(&challenge_id).is_some()); + + // Save to storage + storage.save_challenge(&challenge).unwrap(); + + // Load + let loaded = storage.load_challenge(&challenge_id).unwrap().unwrap(); + assert_eq!(loaded.name, "Test Challenge"); + + // Remove + state.remove_challenge(&challenge_id); + assert!(state.get_challenge(&challenge_id).is_none()); +} + +#[test] +fn test_e2e_validator_registration() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let validators: Vec<_> = (0..8) + .map(|i| { + let kp = Keypair::generate(); + let stake = Stake::new((i + 1) as u64 * 1_000_000_000); + (kp, stake) + }) + .collect(); + + for (kp, stake) in &validators { + let info = ValidatorInfo::new(kp.hotkey(), *stake); + state.add_validator(info).unwrap(); + } + + assert_eq!(state.validators.len(), 8); + assert_eq!(state.active_validators().len(), 8); + + let expected_stake: u64 = (1..=8).map(|i| i * 1_000_000_000).sum(); + assert_eq!(state.total_stake().0, expected_stake); + + // Consensus threshold (50% of 8 = 4) + assert_eq!(state.consensus_threshold(), 4); +} + +#[test] +fn test_e2e_job_queue() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let challenge_id = ChallengeId::new(); + + // Add jobs + for i in 0..5 { + let job = Job::new(challenge_id, format!("agent_{}", i)); + state.add_job(job); + } + + assert_eq!(state.pending_jobs.len(), 5); + + // Claim jobs + let validator = Keypair::generate(); + for i in 0..5 { + let job = state.claim_job(&validator.hotkey()); + assert!(job.is_some()); + assert_eq!(job.unwrap().agent_hash, format!("agent_{}", i)); + } + + assert!(state.claim_job(&validator.hotkey()).is_none()); +} + +// ============================================================================ +// E2E: CRYPTO VERIFICATION +// ============================================================================ + +#[test] +fn test_e2e_signed_message_chain() { + let validators: Vec<_> = (0..4).map(|_| Keypair::generate()).collect(); + + let original_message = b"Important network message"; + + let signed_messages: Vec<_> = validators + .iter() + .map(|v| v.sign(original_message)) + .collect(); + + for signed in &signed_messages { + assert!(signed.verify().unwrap()); + } + + // Tampering should fail + let mut tampered = signed_messages[0].clone(); + tampered.message.push(0); + assert!(!tampered.verify().unwrap()); +} + +#[test] +fn test_e2e_state_hash_consistency() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let mut hashes = Vec::new(); + + hashes.push(state.state_hash); + + state.increment_block(); + hashes.push(state.state_hash); + + let kp = Keypair::generate(); + state + .add_validator(ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000))) + .unwrap(); + hashes.push(state.state_hash); + + let challenge = Challenge::new( + "Test".into(), + "Desc".into(), + vec![], + sudo.hotkey(), + ChallengeConfig::default(), + ); + state.add_challenge(challenge); + hashes.push(state.state_hash); + + // All hashes should be unique + for i in 0..hashes.len() { + for j in i + 1..hashes.len() { + assert_ne!(hashes[i], hashes[j], "Hash collision at {} and {}", i, j); + } + } +} + +// ============================================================================ +// E2E: FULL VALIDATOR FLOW +// ============================================================================ + +#[test] +fn test_e2e_full_validator_flow() { + let sudo = Keypair::generate(); + let dir = tempdir().unwrap(); + + let state = Arc::new(RwLock::new(ChainState::new( + sudo.hotkey(), + NetworkConfig::default(), + ))); + + let storage = Storage::open(dir.path()).unwrap(); + + // Add validators + let validators: Vec<_> = (0..4).map(|_| Keypair::generate()).collect(); + for v in &validators { + let info = ValidatorInfo::new(v.hotkey(), Stake::new(10_000_000_000)); + state.write().add_validator(info.clone()).unwrap(); + storage.save_validator(&info).unwrap(); + } + + // 1. Add a challenge + let challenge = Challenge::new( + "Terminal Benchmark".into(), + "Terminal AI benchmark challenge".into(), + b"challenge code".to_vec(), + sudo.hotkey(), + ChallengeConfig::default(), + ); + + state.write().add_challenge(challenge.clone()); + storage.save_challenge(&challenge).unwrap(); + + // 2. Add jobs + let job = Job::new(challenge.id, "agent_abc123".into()); + state.write().add_job(job); + + // 3. Claim job + let claimed = state.write().claim_job(&validators[0].hotkey()); + assert!(claimed.is_some()); + + // 4. Save state + storage.save_state(&state.read().clone()).unwrap(); + + // 5. Load and verify + let loaded_state = storage.load_state().unwrap().unwrap(); + assert_eq!(loaded_state.validators.len(), 4); + assert_eq!(loaded_state.challenges.len(), 1); +} + +// ============================================================================ +// E2E: EPOCH SIMULATION +// ============================================================================ + +#[test] +fn test_e2e_epoch_simulation() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + for epoch in 0..10 { + state.epoch = epoch; + + for _ in 0..100 { + state.increment_block(); + } + + assert_eq!(state.block_height, (epoch + 1) * 100); + } + + assert_eq!(state.epoch, 9); + assert_eq!(state.block_height, 1000); +} diff --git a/tests/epoch_tests.rs b/tests/epoch_tests.rs new file mode 100644 index 000000000..6419df991 --- /dev/null +++ b/tests/epoch_tests.rs @@ -0,0 +1,938 @@ +//! Comprehensive Epoch Module Tests +//! +//! Tests for epoch management, commit-reveal, and weight aggregation. + +#![allow(dead_code, clippy::type_complexity)] + +use parking_lot::RwLock; +use platform_core::*; +use std::collections::HashMap; +use std::sync::Arc; + +// ============================================================================ +// EPOCH MANAGER TESTS +// ============================================================================ + +mod epoch_manager { + use super::*; + + #[test] + fn test_epoch_config_default() { + let config = EpochConfig::default(); + assert!(config.blocks_per_epoch > 0); + assert!(config.evaluation_phase_blocks > 0); + assert!(config.commit_phase_blocks > 0); + assert!(config.reveal_phase_blocks > 0); + } + + #[test] + fn test_epoch_config_phases_sum() { + let config = EpochConfig::default(); + let total = config.evaluation_phase_blocks + + config.commit_phase_blocks + + config.reveal_phase_blocks; + assert_eq!(total, config.blocks_per_epoch); + } + + #[test] + fn test_epoch_phase_evaluation() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + for block in 0..75 { + let phase = config.get_phase(block); + assert_eq!(phase, EpochPhase::Evaluation); + } + } + + #[test] + fn test_epoch_phase_commit() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + for block in 75..88 { + let phase = config.get_phase(block); + assert_eq!(phase, EpochPhase::Commit); + } + } + + #[test] + fn test_epoch_phase_reveal() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + for block in 88..100 { + let phase = config.get_phase(block); + assert_eq!(phase, EpochPhase::Reveal); + } + } + + #[test] + fn test_epoch_calculation() { + let config = EpochConfig { + blocks_per_epoch: 100, + ..Default::default() + }; + + assert_eq!(config.get_epoch(0), 0); + assert_eq!(config.get_epoch(99), 0); + assert_eq!(config.get_epoch(100), 1); + assert_eq!(config.get_epoch(250), 2); + } + + #[test] + fn test_blocks_until_next_phase() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + // At block 50 (evaluation), 25 blocks until commit + assert_eq!(config.blocks_until_next_phase(50), 25); + + // At block 80 (commit), 8 blocks until reveal + assert_eq!(config.blocks_until_next_phase(80), 8); + + // At block 95 (reveal), 5 blocks until next epoch + assert_eq!(config.blocks_until_next_phase(95), 5); + } + + #[test] + fn test_epoch_manager_new() { + let sudo = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + sudo.hotkey(), + NetworkConfig::default(), + ))); + + let config = EpochConfig::default(); + let manager = EpochManager::new(state, config); + + assert_eq!(manager.current_epoch(), 0); + } + + #[test] + fn test_epoch_manager_current_phase() { + let sudo = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + sudo.hotkey(), + NetworkConfig::default(), + ))); + + let config = EpochConfig::default(); + let manager = EpochManager::new(state, config); + + let phase = manager.current_phase(); + assert!(matches!( + phase, + EpochPhase::Evaluation | EpochPhase::Commit | EpochPhase::Reveal + )); + } +} + +// ============================================================================ +// COMMIT-REVEAL TESTS +// ============================================================================ + +mod commit_reveal { + use super::*; + + #[test] + fn test_commitment_creation() { + let validator = Keypair::generate(); + let challenge_id = "challenge1".to_string(); + let epoch = 10u64; + + let weights = vec![ + (Keypair::generate().hotkey(), 0.5f64), + (Keypair::generate().hotkey(), 0.5f64), + ]; + + let commitment = Commitment::new( + validator.hotkey(), + challenge_id.clone(), + epoch, + weights.clone(), + ); + + assert_eq!(commitment.validator, validator.hotkey()); + assert_eq!(commitment.challenge_id, challenge_id); + assert_eq!(commitment.epoch, epoch); + assert!(!commitment.commitment_hash.is_empty()); + } + + #[test] + fn test_commitment_verification() { + let validator = Keypair::generate(); + let weights = vec![ + (Keypair::generate().hotkey(), 0.5f64), + (Keypair::generate().hotkey(), 0.5f64), + ]; + + let commitment = Commitment::new( + validator.hotkey(), + "challenge1".to_string(), + 10, + weights.clone(), + ); + + // Reveal with same data should verify + let reveal = Reveal { + validator: validator.hotkey(), + challenge_id: "challenge1".to_string(), + epoch: 10, + weights: weights.clone(), + salt: commitment.salt.clone(), + }; + + assert!(commitment.verify(&reveal)); + } + + #[test] + fn test_commitment_verification_failure() { + let validator = Keypair::generate(); + let weights = vec![ + (Keypair::generate().hotkey(), 0.5f64), + (Keypair::generate().hotkey(), 0.5f64), + ]; + + let commitment = Commitment::new( + validator.hotkey(), + "challenge1".to_string(), + 10, + weights.clone(), + ); + + // Reveal with different weights should fail + let different_weights = vec![ + (Keypair::generate().hotkey(), 0.3f64), + (Keypair::generate().hotkey(), 0.7f64), + ]; + + let reveal = Reveal { + validator: validator.hotkey(), + challenge_id: "challenge1".to_string(), + epoch: 10, + weights: different_weights, + salt: commitment.salt.clone(), + }; + + assert!(!commitment.verify(&reveal)); + } + + #[test] + fn test_commit_reveal_manager() { + let manager = CommitRevealManager::new(); + assert_eq!(manager.pending_commits_count(), 0); + } + + #[test] + fn test_add_commitment() { + let manager = CommitRevealManager::new(); + let validator = Keypair::generate(); + + let commitment = Commitment::new(validator.hotkey(), "challenge1".to_string(), 10, vec![]); + + manager.add_commitment(commitment); + assert_eq!(manager.pending_commits_count(), 1); + } + + #[test] + fn test_add_reveal() { + let manager = CommitRevealManager::new(); + let validator = Keypair::generate(); + + let weights = vec![ + (Keypair::generate().hotkey(), 0.5f64), + (Keypair::generate().hotkey(), 0.5f64), + ]; + + let commitment = Commitment::new( + validator.hotkey(), + "challenge1".to_string(), + 10, + weights.clone(), + ); + + manager.add_commitment(commitment.clone()); + + let reveal = Reveal { + validator: validator.hotkey(), + challenge_id: "challenge1".to_string(), + epoch: 10, + weights, + salt: commitment.salt.clone(), + }; + + let result = manager.add_reveal(reveal); + assert!(result.is_ok()); + } + + #[test] + fn test_reveal_without_commitment() { + let manager = CommitRevealManager::new(); + let validator = Keypair::generate(); + + let reveal = Reveal { + validator: validator.hotkey(), + challenge_id: "challenge1".to_string(), + epoch: 10, + weights: vec![], + salt: vec![0u8; 32], + }; + + let result = manager.add_reveal(reveal); + assert!(result.is_err()); + } +} + +// ============================================================================ +// WEIGHT AGGREGATION TESTS +// ============================================================================ + +mod aggregation { + use super::*; + + #[test] + fn test_weight_aggregator_new() { + let aggregator = WeightAggregator::new(AggregationConfig::default()); + assert!(aggregator.is_empty()); + } + + #[test] + fn test_add_validator_weights() { + let aggregator = WeightAggregator::new(AggregationConfig::default()); + let validator = Keypair::generate(); + let miner = Keypair::generate(); + + let weights = vec![(miner.hotkey(), 0.8f64)]; + + aggregator.add_weights(validator.hotkey(), Stake::new(10_000_000_000), weights); + + assert!(!aggregator.is_empty()); + } + + #[test] + fn test_aggregate_simple() { + let aggregator = WeightAggregator::new(AggregationConfig::default()); + + let miner = Keypair::generate(); + + // Two validators with equal stake + for _ in 0..2 { + let validator = Keypair::generate(); + aggregator.add_weights( + validator.hotkey(), + Stake::new(10_000_000_000), + vec![(miner.hotkey(), 0.5f64)], + ); + } + + let result = aggregator.aggregate(); + assert_eq!(result.len(), 1); + assert!((result[0].1 - 0.5).abs() < 0.01); + } + + #[test] + fn test_aggregate_stake_weighted() { + let config = AggregationConfig { + stake_weighted: true, + ..Default::default() + }; + let aggregator = WeightAggregator::new(config); + + let miner = Keypair::generate(); + + // Validator with 10x stake gives weight 0.8 + let v1 = Keypair::generate(); + aggregator.add_weights( + v1.hotkey(), + Stake::new(100_000_000_000), + vec![(miner.hotkey(), 0.8f64)], + ); + + // Validator with 1x stake gives weight 0.2 + let v2 = Keypair::generate(); + aggregator.add_weights( + v2.hotkey(), + Stake::new(10_000_000_000), + vec![(miner.hotkey(), 0.2f64)], + ); + + let result = aggregator.aggregate(); + assert_eq!(result.len(), 1); + + // Result should be closer to 0.8 due to stake weighting + assert!(result[0].1 > 0.5); + } + + #[test] + fn test_aggregate_outlier_removal() { + let config = AggregationConfig { + remove_outliers: true, + outlier_threshold: 2.0, + ..Default::default() + }; + let aggregator = WeightAggregator::new(config); + + let miner = Keypair::generate(); + + // 4 validators agree on ~0.5 + for _ in 0..4 { + let v = Keypair::generate(); + aggregator.add_weights( + v.hotkey(), + Stake::new(10_000_000_000), + vec![(miner.hotkey(), 0.5f64)], + ); + } + + // 1 outlier gives 0.0 + let outlier = Keypair::generate(); + aggregator.add_weights( + outlier.hotkey(), + Stake::new(10_000_000_000), + vec![(miner.hotkey(), 0.0f64)], + ); + + let result = aggregator.aggregate(); + assert_eq!(result.len(), 1); + + // Outlier should be removed, result should be ~0.5 + assert!((result[0].1 - 0.5).abs() < 0.1); + } + + #[test] + fn test_minimum_validators() { + let config = AggregationConfig { + minimum_validators: 3, + ..Default::default() + }; + let aggregator = WeightAggregator::new(config); + + let miner = Keypair::generate(); + + // Only 2 validators - not enough + for _ in 0..2 { + let v = Keypair::generate(); + aggregator.add_weights( + v.hotkey(), + Stake::new(10_000_000_000), + vec![(miner.hotkey(), 0.5f64)], + ); + } + + let result = aggregator.aggregate(); + assert!(result.is_empty()); + } +} + +// ============================================================================ +// MECHANISM WEIGHTS TESTS +// ============================================================================ + +mod mechanism_weights { + use super::*; + + #[test] + fn test_mechanism_weight() { + let mw = MechanismWeight { + mechanism_id: 1, + weight: 0.5, + }; + + assert_eq!(mw.mechanism_id, 1); + assert_eq!(mw.weight, 0.5); + } + + #[test] + fn test_mechanism_weights_sum() { + let weights = [ + MechanismWeight { + mechanism_id: 0, + weight: 0.3, + }, + MechanismWeight { + mechanism_id: 1, + weight: 0.3, + }, + MechanismWeight { + mechanism_id: 2, + weight: 0.4, + }, + ]; + + let sum: f64 = weights.iter().map(|w| w.weight).sum(); + assert!((sum - 1.0).abs() < 0.001); + } + + #[test] + fn test_mechanism_manager() { + let manager = MechanismWeightManager::new(); + assert!(manager.mechanisms().is_empty()); + } + + #[test] + fn test_register_mechanism() { + let manager = MechanismWeightManager::new(); + + manager.register_mechanism(0, "challenge_a".to_string()); + manager.register_mechanism(1, "challenge_b".to_string()); + + assert_eq!(manager.mechanisms().len(), 2); + } + + #[test] + fn test_set_mechanism_weight() { + let manager = MechanismWeightManager::new(); + + manager.register_mechanism(0, "challenge_a".to_string()); + manager.set_weight(0, 0.5); + + let weights = manager.get_weights(); + assert_eq!(weights.len(), 1); + assert_eq!(weights[0].weight, 0.5); + } +} + +// ============================================================================ +// EPOCH TRANSITION TESTS +// ============================================================================ + +mod transition { + use super::*; + + #[test] + fn test_epoch_transition_trigger() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + // Block 99 is last block of epoch 0 + assert_eq!(config.get_epoch(99), 0); + + // Block 100 is first block of epoch 1 + assert_eq!(config.get_epoch(100), 1); + } + + #[test] + fn test_phase_transition() { + let config = EpochConfig { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + }; + + // Block 74 is last evaluation block + assert_eq!(config.get_phase(74), EpochPhase::Evaluation); + + // Block 75 is first commit block + assert_eq!(config.get_phase(75), EpochPhase::Commit); + + // Block 87 is last commit block + assert_eq!(config.get_phase(87), EpochPhase::Commit); + + // Block 88 is first reveal block + assert_eq!(config.get_phase(88), EpochPhase::Reveal); + } +} + +// ============================================================================ +// INTEGRATION TESTS +// ============================================================================ + +mod integration { + use super::*; + + #[test] + fn test_full_epoch_cycle() { + let sudo = Keypair::generate(); + let state = Arc::new(RwLock::new(ChainState::new( + sudo.hotkey(), + NetworkConfig::default(), + ))); + + let config = EpochConfig { + blocks_per_epoch: 10, + evaluation_phase_blocks: 7, + commit_phase_blocks: 2, + reveal_phase_blocks: 1, + }; + + let manager = EpochManager::new(state.clone(), config); + + // Simulate blocks + for _ in 0..10 { + state.write().increment_block(); + } + + // Should be in epoch 1 now + assert_eq!(manager.current_epoch(), 1); + } + + #[test] + fn test_commit_reveal_flow() { + let manager = CommitRevealManager::new(); + let validator = Keypair::generate(); + let miner = Keypair::generate(); + + // 1. Create weights + let weights = vec![(miner.hotkey(), 0.8f64)]; + + // 2. Create and submit commitment + let commitment = Commitment::new( + validator.hotkey(), + "challenge1".to_string(), + 1, + weights.clone(), + ); + let salt = commitment.salt.clone(); + manager.add_commitment(commitment); + + // 3. Create and submit reveal + let reveal = Reveal { + validator: validator.hotkey(), + challenge_id: "challenge1".to_string(), + epoch: 1, + weights, + salt, + }; + + let result = manager.add_reveal(reveal); + assert!(result.is_ok()); + + // 4. Get revealed weights + let revealed = manager.get_revealed_weights("challenge1", 1); + assert_eq!(revealed.len(), 1); + } +} + +// ============================================================================ +// HELPER TYPES (simplified for tests) +// ============================================================================ + +#[derive(Debug, Clone, PartialEq)] +enum EpochPhase { + Evaluation, + Commit, + Reveal, +} + +#[derive(Debug, Clone)] +struct EpochConfig { + blocks_per_epoch: u64, + evaluation_phase_blocks: u64, + commit_phase_blocks: u64, + reveal_phase_blocks: u64, +} + +impl Default for EpochConfig { + fn default() -> Self { + Self { + blocks_per_epoch: 100, + evaluation_phase_blocks: 75, + commit_phase_blocks: 13, + reveal_phase_blocks: 12, + } + } +} + +impl EpochConfig { + fn get_epoch(&self, block: u64) -> u64 { + block / self.blocks_per_epoch + } + + fn get_phase(&self, block: u64) -> EpochPhase { + let block_in_epoch = block % self.blocks_per_epoch; + if block_in_epoch < self.evaluation_phase_blocks { + EpochPhase::Evaluation + } else if block_in_epoch < self.evaluation_phase_blocks + self.commit_phase_blocks { + EpochPhase::Commit + } else { + EpochPhase::Reveal + } + } + + fn blocks_until_next_phase(&self, block: u64) -> u64 { + let block_in_epoch = block % self.blocks_per_epoch; + if block_in_epoch < self.evaluation_phase_blocks { + self.evaluation_phase_blocks - block_in_epoch + } else if block_in_epoch < self.evaluation_phase_blocks + self.commit_phase_blocks { + self.evaluation_phase_blocks + self.commit_phase_blocks - block_in_epoch + } else { + self.blocks_per_epoch - block_in_epoch + } + } +} + +struct EpochManager { + state: Arc>, + config: EpochConfig, +} + +impl EpochManager { + fn new(state: Arc>, config: EpochConfig) -> Self { + Self { state, config } + } + + fn current_epoch(&self) -> u64 { + self.config.get_epoch(self.state.read().block_height) + } + + fn current_phase(&self) -> EpochPhase { + self.config.get_phase(self.state.read().block_height) + } +} + +#[derive(Clone)] +struct Commitment { + validator: Hotkey, + challenge_id: String, + epoch: u64, + commitment_hash: Vec, + salt: Vec, +} + +impl Commitment { + fn new( + validator: Hotkey, + challenge_id: String, + epoch: u64, + weights: Vec<(Hotkey, f64)>, + ) -> Self { + let salt: Vec = (0..32).map(|_| rand::random()).collect(); + + let mut data = Vec::new(); + for (h, w) in &weights { + data.extend_from_slice(h.as_bytes()); + data.extend_from_slice(&w.to_le_bytes()); + } + data.extend_from_slice(&salt); + + let commitment_hash = hash(&data).to_vec(); + + Self { + validator, + challenge_id, + epoch, + commitment_hash, + salt, + } + } + + fn verify(&self, reveal: &Reveal) -> bool { + let mut data = Vec::new(); + for (h, w) in &reveal.weights { + data.extend_from_slice(h.as_bytes()); + data.extend_from_slice(&w.to_le_bytes()); + } + data.extend_from_slice(&reveal.salt); + + let reveal_hash = hash(&data); + reveal_hash.to_vec() == self.commitment_hash + } +} + +struct Reveal { + validator: Hotkey, + challenge_id: String, + epoch: u64, + weights: Vec<(Hotkey, f64)>, + salt: Vec, +} + +struct CommitRevealManager { + commitments: RwLock>, + reveals: RwLock>>, +} + +impl CommitRevealManager { + fn new() -> Self { + Self { + commitments: RwLock::new(HashMap::new()), + reveals: RwLock::new(HashMap::new()), + } + } + + fn pending_commits_count(&self) -> usize { + self.commitments.read().len() + } + + fn add_commitment(&self, commitment: Commitment) { + let key = ( + commitment.validator.clone(), + commitment.challenge_id.clone(), + commitment.epoch, + ); + self.commitments.write().insert(key, commitment); + } + + fn add_reveal(&self, reveal: Reveal) -> std::result::Result<(), String> { + let key = ( + reveal.validator.clone(), + reveal.challenge_id.clone(), + reveal.epoch, + ); + + let commitment = self.commitments.read().get(&key).cloned(); + + match commitment { + Some(c) if c.verify(&reveal) => { + self.reveals.write().insert(key, reveal.weights); + Ok(()) + } + Some(_) => Err("Reveal doesn't match commitment".to_string()), + None => Err("No commitment found".to_string()), + } + } + + fn get_revealed_weights( + &self, + challenge_id: &str, + epoch: u64, + ) -> Vec<(Hotkey, Vec<(Hotkey, f64)>)> { + self.reveals + .read() + .iter() + .filter(|((_, c, e), _)| c == challenge_id && *e == epoch) + .map(|((v, _, _), w)| (v.clone(), w.clone())) + .collect() + } +} + +#[derive(Default)] +struct AggregationConfig { + stake_weighted: bool, + remove_outliers: bool, + outlier_threshold: f64, + minimum_validators: usize, +} + +struct WeightAggregator { + config: AggregationConfig, + weights: RwLock)>>, +} + +impl WeightAggregator { + fn new(config: AggregationConfig) -> Self { + Self { + config, + weights: RwLock::new(Vec::new()), + } + } + + fn is_empty(&self) -> bool { + self.weights.read().is_empty() + } + + fn add_weights(&self, validator: Hotkey, stake: Stake, weights: Vec<(Hotkey, f64)>) { + self.weights.write().push((validator, stake.0, weights)); + } + + fn aggregate(&self) -> Vec<(Hotkey, f64)> { + let weights = self.weights.read(); + + if weights.len() < self.config.minimum_validators { + return vec![]; + } + + // Collect all miners + let mut miner_weights: HashMap> = HashMap::new(); + + for (_, stake, ws) in weights.iter() { + for (miner, weight) in ws { + miner_weights + .entry(miner.clone()) + .or_default() + .push((*stake, *weight)); + } + } + + // Aggregate + let mut result = Vec::new(); + for (miner, stake_weights) in miner_weights { + let avg = if self.config.stake_weighted { + let total_stake: u64 = stake_weights.iter().map(|(s, _)| s).sum(); + stake_weights + .iter() + .map(|(s, w)| (*s as f64) * w) + .sum::() + / total_stake as f64 + } else { + stake_weights.iter().map(|(_, w)| w).sum::() / stake_weights.len() as f64 + }; + result.push((miner, avg)); + } + + result + } +} + +struct MechanismWeight { + mechanism_id: u16, + weight: f64, +} + +struct MechanismWeightManager { + mechanisms: RwLock>, +} + +impl MechanismWeightManager { + fn new() -> Self { + Self { + mechanisms: RwLock::new(HashMap::new()), + } + } + + fn mechanisms(&self) -> Vec<(u16, String)> { + self.mechanisms + .read() + .iter() + .map(|(id, (name, _))| (*id, name.clone())) + .collect() + } + + fn register_mechanism(&self, id: u16, name: String) { + self.mechanisms.write().insert(id, (name, 0.0)); + } + + fn set_weight(&self, id: u16, weight: f64) { + // Get the name first, then release the read lock before acquiring write lock + let name = { + let mechanisms = self.mechanisms.read(); + mechanisms.get(&id).map(|(n, _)| n.clone()) + }; + if let Some(name) = name { + self.mechanisms.write().insert(id, (name, weight)); + } + } + + fn get_weights(&self) -> Vec { + self.mechanisms + .read() + .iter() + .map(|(id, (_, w))| MechanismWeight { + mechanism_id: *id, + weight: *w, + }) + .collect() + } +} diff --git a/tests/error_cases.rs b/tests/error_cases.rs new file mode 100644 index 000000000..8082a99a7 --- /dev/null +++ b/tests/error_cases.rs @@ -0,0 +1,410 @@ +//! Exhaustive Error Case Tests +//! +//! These tests verify that all error conditions are properly handled. + +#![allow(clippy::field_reassign_with_default)] + +use platform_core::*; +use platform_storage::*; +use tempfile::tempdir; + +// ============================================================================ +// CORE ERROR CASES +// ============================================================================ + +mod core_errors { + use super::*; + + #[test] + fn test_hotkey_from_bytes_too_short() { + let short = vec![1u8; 16]; + assert!(Hotkey::from_bytes(&short).is_none()); + } + + #[test] + fn test_hotkey_from_bytes_too_long() { + let long = vec![1u8; 64]; + assert!(Hotkey::from_bytes(&long).is_none()); + } + + #[test] + fn test_hotkey_from_bytes_empty() { + assert!(Hotkey::from_bytes(&[]).is_none()); + } + + #[test] + fn test_hotkey_from_hex_invalid() { + assert!(Hotkey::from_hex("not_valid_hex").is_none()); + assert!(Hotkey::from_hex("").is_none()); + assert!(Hotkey::from_hex("zzzz").is_none()); + } + + #[test] + fn test_hotkey_from_hex_wrong_length() { + assert!(Hotkey::from_hex("0102030405").is_none()); + } + + #[test] + fn test_signature_invalid_length() { + let signed = SignedMessage { + message: b"test".to_vec(), + signature: vec![0u8; 32], // Wrong length + signer: Hotkey([0u8; 32]), + }; + assert!(signed.verify().is_err()); + } + + #[test] + fn test_signature_corrupted() { + let kp = Keypair::generate(); + let mut signed = kp.sign(b"test message"); + signed.signature[0] ^= 0xFF; + signed.signature[32] ^= 0xFF; + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_signature_wrong_message() { + let kp = Keypair::generate(); + let mut signed = kp.sign(b"original message"); + signed.message = b"different message".to_vec(); + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_score_negative_values() { + let score = Score::new(-1.0, -1.0); + assert_eq!(score.value, 0.0); + assert_eq!(score.weight, 0.0); + } + + #[test] + fn test_score_overflow_values() { + let score = Score::new(1000.0, 1000.0); + assert_eq!(score.value, 1.0); + assert_eq!(score.weight, 1.0); + } + + #[test] + fn test_challenge_id_from_invalid_uuid() { + let id1 = ChallengeId::from_string("not-a-uuid"); + let id2 = ChallengeId::from_string("not-a-uuid"); + assert_eq!(id1, id2); + } +} + +// ============================================================================ +// STATE ERROR CASES +// ============================================================================ + +mod state_errors { + use super::*; + + #[test] + fn test_add_validator_max_reached() { + let sudo = Keypair::generate(); + let mut config = NetworkConfig::default(); + config.max_validators = 2; + + let mut state = ChainState::new(sudo.hotkey(), config); + + for _ in 0..2 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + assert!(state.add_validator(info).is_ok()); + } + + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + assert!(state.add_validator(info).is_err()); + } + + #[test] + fn test_add_validator_insufficient_stake() { + let sudo = Keypair::generate(); + let mut config = NetworkConfig::default(); + config.min_stake = Stake::new(1_000_000_000_000); + + let mut state = ChainState::new(sudo.hotkey(), config); + + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(100)); + assert!(state.add_validator(info).is_err()); + } + + #[test] + fn test_remove_nonexistent_validator() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let kp = Keypair::generate(); + let removed = state.remove_validator(&kp.hotkey()); + assert!(removed.is_none()); + } + + #[test] + fn test_get_nonexistent_validator() { + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let kp = Keypair::generate(); + assert!(state.get_validator(&kp.hotkey()).is_none()); + } + + #[test] + fn test_get_nonexistent_challenge() { + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + assert!(state.get_challenge(&ChallengeId::new()).is_none()); + } + + #[test] + fn test_remove_nonexistent_challenge() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + let removed = state.remove_challenge(&ChallengeId::new()); + assert!(removed.is_none()); + } + + #[test] + fn test_claim_job_empty_queue() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let validator = Keypair::generate(); + assert!(state.claim_job(&validator.hotkey()).is_none()); + } + + #[test] + fn test_is_sudo_wrong_key() { + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + let not_sudo = Keypair::generate(); + assert!(!state.is_sudo(¬_sudo.hotkey())); + } + + #[test] + fn test_consensus_threshold_zero_validators() { + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + assert_eq!(state.consensus_threshold(), 0); + } + + #[test] + fn test_total_stake_empty() { + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + assert_eq!(state.total_stake().0, 0); + } +} + +// ============================================================================ +// STORAGE ERROR CASES +// ============================================================================ + +mod storage_errors { + use super::*; + + #[test] + fn test_load_nonexistent_state() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let loaded = storage.load_state().unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_load_nonexistent_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let loaded = storage.load_challenge(&ChallengeId::new()).unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_load_nonexistent_validator() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let kp = Keypair::generate(); + let loaded = storage.load_validator(&kp.hotkey()).unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_delete_nonexistent_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let deleted = storage.delete_challenge(&ChallengeId::new()).unwrap(); + assert!(!deleted); + } + + #[test] + fn test_list_challenges_empty() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let challenges = storage.list_challenges().unwrap(); + assert!(challenges.is_empty()); + } +} + +// ============================================================================ +// MESSAGE ERROR CASES +// ============================================================================ + +mod message_errors { + use super::*; + + #[test] + fn test_signed_message_tampering() { + let kp = Keypair::generate(); + let mut signed = kp.sign(b"original message"); + signed.message = b"tampered message".to_vec(); + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_signed_message_signature_tampering() { + let kp = Keypair::generate(); + let mut signed = kp.sign(b"test message"); + // SignedMessage has signature as Vec + if !signed.signature.is_empty() { + signed.signature[0] ^= 0xFF; + } + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_signed_message_signer_tampering() { + let kp1 = Keypair::generate(); + let kp2 = Keypair::generate(); + let mut signed = kp1.sign(b"test"); + signed.signer = kp2.hotkey(); + assert!(!signed.verify().unwrap()); + } +} + +// ============================================================================ +// CRYPTO ERROR CASES +// ============================================================================ + +mod crypto_errors { + use super::*; + + #[test] + fn test_hash_empty_data() { + let h1 = hash(b""); + let h2 = hash(b""); + assert_eq!(h1, h2); + assert_eq!(h1.len(), 32); + } + + #[test] + fn test_signature_empty_message() { + let kp = Keypair::generate(); + let signed = kp.sign(b""); + assert!(signed.verify().unwrap()); + } + + #[test] + fn test_signature_large_message() { + let kp = Keypair::generate(); + let large = vec![0xAB; 1024 * 1024]; + let signed = kp.sign(&large); + assert!(signed.verify().unwrap()); + } +} + +// ============================================================================ +// EDGE CASES +// ============================================================================ + +mod edge_cases { + use super::*; + + #[test] + fn test_stake_zero() { + let stake = Stake::new(0); + assert_eq!(stake.as_tao(), 0.0); + } + + #[test] + fn test_stake_max() { + let stake = Stake::new(u64::MAX); + assert!(stake.as_tao() > 0.0); + } + + #[test] + fn test_block_height_max() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + state.block_height = u64::MAX - 1; + state.increment_block(); + assert_eq!(state.block_height, u64::MAX); + } + + #[test] + fn test_epoch_max() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + state.epoch = u64::MAX; + let _ = state.snapshot(); // Should not panic + } + + #[test] + fn test_challenge_empty_code() { + let kp = Keypair::generate(); + let challenge = Challenge::new( + "Empty".into(), + "Description".into(), + vec![], + kp.hotkey(), + ChallengeConfig::default(), + ); + assert!(!challenge.code_hash.is_empty()); + } + + #[test] + fn test_job_status_transitions() { + let challenge_id = ChallengeId::new(); + let mut job = Job::new(challenge_id, "agent1".into()); + + assert_eq!(job.status, JobStatus::Pending); + + job.status = JobStatus::Running; + assert_eq!(job.status, JobStatus::Running); + + job.status = JobStatus::Completed; + job.result = Some(Score::new(0.95, 1.0)); + assert_eq!(job.status, JobStatus::Completed); + + let mut failed_job = Job::new(challenge_id, "agent2".into()); + failed_job.status = JobStatus::Failed; + assert_eq!(failed_job.status, JobStatus::Failed); + + let mut timeout_job = Job::new(challenge_id, "agent3".into()); + timeout_job.status = JobStatus::Timeout; + assert_eq!(timeout_job.status, JobStatus::Timeout); + } + + #[test] + fn test_validator_inactive_not_counted() { + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + + // Add active validator + let kp1 = Keypair::generate(); + state + .add_validator(ValidatorInfo::new(kp1.hotkey(), Stake::new(10_000_000_000))) + .unwrap(); + + // Add inactive validator + let kp2 = Keypair::generate(); + let mut info2 = ValidatorInfo::new(kp2.hotkey(), Stake::new(10_000_000_000)); + info2.is_active = false; + state.validators.insert(kp2.hotkey(), info2); + + assert_eq!(state.validators.len(), 2); + assert_eq!(state.active_validators().len(), 1); + } +} diff --git a/tests/rpc_server_tests.rs b/tests/rpc_server_tests.rs new file mode 100644 index 000000000..dd9b1c803 --- /dev/null +++ b/tests/rpc_server_tests.rs @@ -0,0 +1,413 @@ +//! Comprehensive RPC Server Tests + +#![allow(dead_code, unused_variables)] + +use parking_lot::RwLock; +use platform_core::*; +use std::sync::Arc; + +// ============================================================================ +// HELPER FUNCTIONS +// ============================================================================ + +fn create_chain_state() -> Arc> { + let sudo = Keypair::generate(); + Arc::new(RwLock::new(ChainState::new( + sudo.hotkey(), + NetworkConfig::default(), + ))) +} + +fn create_populated_chain_state() -> Arc> { + let state = create_chain_state(); + + // Add validators + for i in 0..5 { + let kp = Keypair::generate(); + let mut info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000 * (i + 1))); + info.is_active = i < 4; + state.write().validators.insert(kp.hotkey(), info); + } + + // Add challenges + let creator = Keypair::generate(); + for i in 0..3 { + let challenge = Challenge::new( + format!("Challenge {}", i), + format!("Description {}", i), + format!("code{}", i).into_bytes(), + creator.hotkey(), + ChallengeConfig::default(), + ); + state.write().add_challenge(challenge); + } + + // Add jobs + let challenge_id = state.read().challenges.values().next().unwrap().id; + for i in 0..10 { + let job = Job::new(challenge_id, format!("agent_{}", i)); + state.write().add_job(job); + } + + state +} + +// ============================================================================ +// CHAIN STATE TESTS +// ============================================================================ + +#[test] +fn test_chain_state_creation() { + let state = create_chain_state(); + assert_eq!(state.read().validators.len(), 0); + assert_eq!(state.read().challenges.len(), 0); + assert_eq!(state.read().pending_jobs.len(), 0); +} + +#[test] +fn test_populated_chain_state() { + let state = create_populated_chain_state(); + assert_eq!(state.read().validators.len(), 5); + assert_eq!(state.read().challenges.len(), 3); + assert_eq!(state.read().pending_jobs.len(), 10); +} + +#[test] +fn test_active_validators_count() { + let state = create_populated_chain_state(); + let active_count = state.read().active_validators().len(); + assert_eq!(active_count, 4); +} + +// ============================================================================ +// RESPONSE STRUCTURE TESTS +// ============================================================================ + +mod response_types { + + struct HealthResponse { + status: String, + version: String, + uptime_secs: u64, + } + + struct StatusResponse { + netuid: u16, + name: String, + version: String, + block_height: u64, + epoch: u64, + validators_count: usize, + challenges_count: usize, + pending_jobs: usize, + is_paused: bool, + } + + struct ValidatorResponse { + hotkey: String, + stake: u64, + stake_tao: f64, + is_active: bool, + } + + struct ChallengeResponse { + id: String, + name: String, + description: String, + is_active: bool, + emission_weight: f64, + timeout_secs: u64, + } + + struct JobResponse { + id: String, + challenge_id: String, + agent_hash: String, + status: String, + } + + struct EpochResponse { + current_epoch: u64, + current_block: u64, + blocks_per_epoch: u64, + phase: String, + phase_progress: f64, + blocks_until_next_phase: u64, + } + + #[test] + fn test_health_response() { + let response = HealthResponse { + status: "healthy".to_string(), + version: "1.0.0".to_string(), + uptime_secs: 3600, + }; + assert_eq!(response.status, "healthy"); + assert_eq!(response.uptime_secs, 3600); + } + + #[test] + fn test_status_response() { + let response = StatusResponse { + netuid: 1, + name: "test".to_string(), + version: "1.0.0".to_string(), + block_height: 1000, + epoch: 10, + validators_count: 5, + challenges_count: 3, + pending_jobs: 10, + is_paused: false, + }; + assert_eq!(response.block_height, 1000); + assert_eq!(response.epoch, 10); + } + + #[test] + fn test_validator_response() { + let response = ValidatorResponse { + hotkey: "abc123".to_string(), + stake: 1_000_000_000, + stake_tao: 1.0, + is_active: true, + }; + assert!(response.is_active); + } + + #[test] + fn test_challenge_response() { + let response = ChallengeResponse { + id: "uuid".to_string(), + name: "Test Challenge".to_string(), + description: "A test".to_string(), + is_active: true, + emission_weight: 1.0, + timeout_secs: 300, + }; + assert!(response.is_active); + assert_eq!(response.timeout_secs, 300); + } + + #[test] + fn test_job_response() { + let response = JobResponse { + id: "job-uuid".to_string(), + challenge_id: "challenge-uuid".to_string(), + agent_hash: "agent123".to_string(), + status: "Pending".to_string(), + }; + assert_eq!(response.status, "Pending"); + } + + #[test] + fn test_epoch_response() { + let response = EpochResponse { + current_epoch: 10, + current_block: 1050, + blocks_per_epoch: 100, + phase: "evaluation".to_string(), + phase_progress: 0.5, + blocks_until_next_phase: 25, + }; + assert_eq!(response.phase, "evaluation"); + assert!(response.phase_progress >= 0.0 && response.phase_progress <= 1.0); + } +} + +// ============================================================================ +// HANDLER LOGIC TESTS +// ============================================================================ + +mod handler_logic { + use super::*; + + #[test] + fn test_status_logic() { + let state = create_populated_chain_state(); + let chain = state.read(); + + assert_eq!(chain.validators.len(), 5); + assert_eq!(chain.challenges.len(), 3); + assert_eq!(chain.pending_jobs.len(), 10); + } + + #[test] + fn test_validators_pagination() { + let state = create_populated_chain_state(); + let chain = state.read(); + + let offset = 0; + let limit = 3; + + let validators: Vec<_> = chain.validators.values().skip(offset).take(limit).collect(); + + assert_eq!(validators.len(), 3); + } + + #[test] + fn test_validators_pagination_offset() { + let state = create_populated_chain_state(); + let chain = state.read(); + + let offset = 2; + let limit = 10; + + let validators: Vec<_> = chain.validators.values().skip(offset).take(limit).collect(); + + assert_eq!(validators.len(), 3); + } + + #[test] + fn test_challenges_list() { + let state = create_populated_chain_state(); + let chain = state.read(); + + let challenges: Vec<_> = chain.challenges.values().collect(); + assert_eq!(challenges.len(), 3); + assert!(challenges.iter().all(|c| c.is_active)); + } + + #[test] + fn test_jobs_list() { + let state = create_populated_chain_state(); + let chain = state.read(); + + let offset = 0; + let limit = 5; + + let jobs: Vec<_> = chain.pending_jobs.iter().skip(offset).take(limit).collect(); + + assert_eq!(jobs.len(), 5); + assert!(jobs.iter().all(|j| j.status == JobStatus::Pending)); + } + + #[test] + fn test_epoch_phase_calculation() { + let blocks_per_epoch = 100u64; + + // Evaluation phase (0-74) + let block_in_epoch = 50u64; + let phase = if block_in_epoch < 75 { + "evaluation" + } else if block_in_epoch < 88 { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "evaluation"); + + // Commit phase (75-87) + let block_in_epoch = 80u64; + let phase = if block_in_epoch < 75 { + "evaluation" + } else if block_in_epoch < 88 { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "commit"); + + // Reveal phase (88-99) + let block_in_epoch = 95u64; + let phase = if block_in_epoch < 75 { + "evaluation" + } else if block_in_epoch < 88 { + "commit" + } else { + "reveal" + }; + assert_eq!(phase, "reveal"); + } +} + +// ============================================================================ +// AUTHENTICATION TESTS +// ============================================================================ + +mod auth { + use super::*; + + #[test] + fn test_signature_verification() { + let kp = Keypair::generate(); + let message = b"test message"; + + let signed = kp.sign(message); + assert!(signed.verify().unwrap()); + } + + #[test] + fn test_signature_verification_tampering() { + let kp = Keypair::generate(); + let message = b"test message"; + + let mut signed = kp.sign(message); + signed.message = b"tampered".to_vec(); + assert!(!signed.verify().unwrap()); + } + + #[test] + fn test_hotkey_hex_conversion() { + let kp = Keypair::generate(); + let hex = kp.hotkey().to_hex(); + let parsed = Hotkey::from_hex(&hex); + assert!(parsed.is_some()); + assert_eq!(parsed.unwrap(), kp.hotkey()); + } + + #[test] + fn test_hotkey_invalid_hex() { + let parsed = Hotkey::from_hex("not_valid_hex"); + assert!(parsed.is_none()); + } +} + +// ============================================================================ +// EDGE CASES +// ============================================================================ + +mod edge_cases { + use super::*; + + #[test] + fn test_empty_validators() { + let state = create_chain_state(); + let chain = state.read(); + assert_eq!(chain.validators.len(), 0); + } + + #[test] + fn test_empty_challenges() { + let state = create_chain_state(); + let chain = state.read(); + assert_eq!(chain.challenges.len(), 0); + } + + #[test] + fn test_empty_jobs() { + let state = create_chain_state(); + let chain = state.read(); + assert_eq!(chain.pending_jobs.len(), 0); + } + + #[test] + fn test_pagination_boundary() { + let state = create_populated_chain_state(); + let chain = state.read(); + + // Request more than available + let offset = 100; + let limit = 100; + + let validators: Vec<_> = chain.validators.values().skip(offset).take(limit).collect(); + + assert_eq!(validators.len(), 0); + } + + #[test] + fn test_large_limit_capped() { + let requested_limit = 5000usize; + let capped_limit = requested_limit.min(1000); + assert_eq!(capped_limit, 1000); + } +} diff --git a/tests/storage_tests.rs b/tests/storage_tests.rs new file mode 100644 index 000000000..72735f208 --- /dev/null +++ b/tests/storage_tests.rs @@ -0,0 +1,414 @@ +//! Comprehensive Storage Module Tests +//! +//! Tests for storage operations and data management. + +use platform_core::*; +use platform_storage::*; +use tempfile::tempdir; + +// ============================================================================ +// MAIN STORAGE TESTS +// ============================================================================ + +mod main_storage { + use super::*; + + #[test] + fn test_storage_open() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + assert!(storage.load_state().unwrap().is_none()); + } + + #[test] + fn test_storage_save_load_state() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + let mut state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + state.increment_block(); + state.epoch = 5; + + storage.save_state(&state).unwrap(); + + let loaded = storage.load_state().unwrap().unwrap(); + assert_eq!(loaded.block_height, 1); + assert_eq!(loaded.epoch, 5); + } + + #[test] + fn test_storage_challenge_operations() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let creator = Keypair::generate(); + let challenge = Challenge::new( + "Test".into(), + "Description".into(), + b"code".to_vec(), + creator.hotkey(), + ChallengeConfig::default(), + ); + + let id = challenge.id; + + // Save + storage.save_challenge(&challenge).unwrap(); + + // Load + let loaded = storage.load_challenge(&id).unwrap().unwrap(); + assert_eq!(loaded.name, "Test"); + + // List + let challenges = storage.list_challenges().unwrap(); + assert_eq!(challenges.len(), 1); + + // Delete + assert!(storage.delete_challenge(&id).unwrap()); + assert!(storage.load_challenge(&id).unwrap().is_none()); + } + + #[test] + fn test_storage_validator_operations() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)); + + // Save + storage.save_validator(&info).unwrap(); + + // Load + let loaded = storage.load_validator(&kp.hotkey()).unwrap().unwrap(); + assert_eq!(loaded.stake.0, 10_000_000_000); + } + + #[test] + fn test_storage_multiple_challenges() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let creator = Keypair::generate(); + + for i in 0..5 { + let challenge = Challenge::new( + format!("Challenge {}", i), + format!("Description {}", i), + format!("code{}", i).into_bytes(), + creator.hotkey(), + ChallengeConfig::default(), + ); + storage.save_challenge(&challenge).unwrap(); + } + + let challenges = storage.list_challenges().unwrap(); + assert_eq!(challenges.len(), 5); + } + + #[test] + fn test_storage_persistence() { + let dir = tempdir().unwrap(); + + // Create and save + { + let storage = Storage::open(dir.path()).unwrap(); + let sudo = Keypair::generate(); + let state = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + storage.save_state(&state).unwrap(); + } + + // Reopen and verify + { + let storage = Storage::open(dir.path()).unwrap(); + let loaded = storage.load_state().unwrap(); + assert!(loaded.is_some()); + } + } +} + +// ============================================================================ +// STORAGE ERROR TESTS +// ============================================================================ + +mod storage_errors { + use super::*; + + #[test] + fn test_load_nonexistent_state() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + assert!(storage.load_state().unwrap().is_none()); + } + + #[test] + fn test_load_nonexistent_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let id = ChallengeId::new(); + assert!(storage.load_challenge(&id).unwrap().is_none()); + } + + #[test] + fn test_load_nonexistent_validator() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let kp = Keypair::generate(); + let loaded = storage.load_validator(&kp.hotkey()).unwrap(); + assert!(loaded.is_none()); + } + + #[test] + fn test_delete_nonexistent_challenge() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let id = ChallengeId::new(); + assert!(!storage.delete_challenge(&id).unwrap()); + } + + #[test] + fn test_list_challenges_empty() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + let challenges = storage.list_challenges().unwrap(); + assert!(challenges.is_empty()); + } +} + +// ============================================================================ +// DISTRIBUTED STORAGE CONSTANTS TESTS +// ============================================================================ + +mod distributed_constants { + use super::*; + + #[test] + fn test_consensus_threshold() { + assert_eq!(CONSENSUS_THRESHOLD, 0.50); + } + + #[test] + fn test_max_sizes() { + assert_eq!(MAX_RAW_SIZE, 10 * 1024 * 1024); + assert_eq!(MAX_COMPRESSED_SIZE, 5 * 1024 * 1024); + } + + #[test] + fn test_max_entries() { + assert_eq!(MAX_ENTRIES_PER_CATEGORY, 100_000); + } + + #[test] + fn test_no_ttl() { + assert_eq!(NO_TTL, 0); + } +} + +// ============================================================================ +// CATEGORY TESTS +// ============================================================================ + +mod category_tests { + use super::*; + + #[test] + fn test_category_variants() { + let _ = Category::Submission; + let _ = Category::Agent; + let _ = Category::Evaluation; + let _ = Category::Consensus; + let _ = Category::Log; + let _ = Category::Index; + let _ = Category::Meta; + } + + #[test] + fn test_category_equality() { + assert_eq!(Category::Agent, Category::Agent); + assert_ne!(Category::Agent, Category::Log); + } +} + +// ============================================================================ +// WRITE VALIDATION TESTS +// ============================================================================ + +mod write_validation { + use super::*; + + #[test] + fn test_write_validation_result_accept() { + let result = WriteValidationResult::Accept; + assert!(matches!(result, WriteValidationResult::Accept)); + } + + #[test] + fn test_write_validation_result_reject() { + let result = WriteValidationResult::Reject("reason".to_string()); + if let WriteValidationResult::Reject(reason) = result { + assert_eq!(reason, "reason"); + } else { + panic!("Expected Reject"); + } + } +} + +// ============================================================================ +// COMPRESSION TESTS +// ============================================================================ + +mod compression { + #[test] + fn test_lz4_compression_roundtrip() { + let data = b"Hello, World! This is test data that should be compressed."; + let compressed = lz4_flex::compress_prepend_size(data); + let decompressed = lz4_flex::decompress_size_prepended(&compressed).unwrap(); + assert_eq!(data.as_slice(), decompressed.as_slice()); + } + + #[test] + fn test_lz4_compression_large() { + let data: Vec = (0..10000).map(|i| (i % 256) as u8).collect(); + let compressed = lz4_flex::compress_prepend_size(&data); + let decompressed = lz4_flex::decompress_size_prepended(&compressed).unwrap(); + assert_eq!(data, decompressed); + // Compression should reduce size for repetitive data + assert!(compressed.len() < data.len()); + } + + #[test] + fn test_lz4_compression_empty() { + let data: &[u8] = &[]; + let compressed = lz4_flex::compress_prepend_size(data); + let decompressed = lz4_flex::decompress_size_prepended(&compressed).unwrap(); + assert!(decompressed.is_empty()); + } + + #[test] + fn test_lz4_compression_random() { + use rand::Rng; + let mut rng = rand::thread_rng(); + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + let compressed = lz4_flex::compress_prepend_size(&data); + let decompressed = lz4_flex::decompress_size_prepended(&compressed).unwrap(); + assert_eq!(data, decompressed); + } +} + +// ============================================================================ +// EDGE CASES +// ============================================================================ + +mod edge_cases { + use super::*; + + #[test] + fn test_empty_storage() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + assert!(storage.load_state().unwrap().is_none()); + assert!(storage.list_challenges().unwrap().is_empty()); + } + + #[test] + fn test_overwrite_state() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let sudo = Keypair::generate(); + + // Save first state + let mut state1 = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + state1.epoch = 1; + storage.save_state(&state1).unwrap(); + + // Overwrite with second state + let mut state2 = ChainState::new(sudo.hotkey(), NetworkConfig::default()); + state2.epoch = 2; + storage.save_state(&state2).unwrap(); + + // Verify second state + let loaded = storage.load_state().unwrap().unwrap(); + assert_eq!(loaded.epoch, 2); + } + + #[test] + fn test_large_challenge_code() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let creator = Keypair::generate(); + let large_code = vec![0xAB; 100_000]; // 100KB + + let challenge = Challenge::new( + "Large".into(), + "Desc".into(), + large_code.clone(), + creator.hotkey(), + ChallengeConfig::default(), + ); + + storage.save_challenge(&challenge).unwrap(); + + let loaded = storage.load_challenge(&challenge.id).unwrap().unwrap(); + assert_eq!(loaded.wasm_code.len(), 100_000); + } + + #[test] + fn test_many_validators() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + for i in 0..100 { + let kp = Keypair::generate(); + let info = ValidatorInfo::new(kp.hotkey(), Stake::new(i * 1_000_000_000)); + storage.save_validator(&info).unwrap(); + } + + // Verify we can save many validators + // (Storage doesn't have a list_validators method, so we verify by saving) + } + + #[test] + fn test_challenge_with_unicode_name() { + let dir = tempdir().unwrap(); + let storage = Storage::open(dir.path()).unwrap(); + + let creator = Keypair::generate(); + let challenge = Challenge::new( + "ใƒ†ใ‚นใƒˆ ใƒใƒฃใƒฌใƒณใ‚ธ ๐Ÿš€".into(), + "Description with รฉmojis ๐ŸŽ‰".into(), + b"code".to_vec(), + creator.hotkey(), + ChallengeConfig::default(), + ); + + storage.save_challenge(&challenge).unwrap(); + + let loaded = storage.load_challenge(&challenge.id).unwrap().unwrap(); + assert_eq!(loaded.name, "ใƒ†ใ‚นใƒˆ ใƒใƒฃใƒฌใƒณใ‚ธ ๐Ÿš€"); + } +} + +// ============================================================================ +// STORAGE STATS TESTS +// ============================================================================ + +mod stats_tests { + #[test] + fn test_storage_compression_ratio() { + let raw_size = 1000usize; + let compressed_size = 500usize; + let ratio = compressed_size as f64 / raw_size as f64; + assert!(ratio > 0.0 && ratio <= 1.0); + } + + #[test] + fn test_storage_size_calculation() { + let entries = [100, 200, 300, 400]; + let total: usize = entries.iter().sum(); + assert_eq!(total, 1000); + } +} diff --git a/tests/sudo_action_tests.rs b/tests/sudo_action_tests.rs new file mode 100644 index 000000000..63e7cb626 --- /dev/null +++ b/tests/sudo_action_tests.rs @@ -0,0 +1,588 @@ +//! Tests for SudoAction operations and blockchain halt functionality. +//! +//! These tests verify: +//! - Emergency controls (EmergencyPause, Resume) +//! - Signature verification for sudo operations +//! - Version management +//! - Weight and validator management + +use platform_core::{ + is_production_sudo, production_sudo_key, ChainState, ChallengeId, Hotkey, Keypair, + NetworkConfig, NetworkMessage, ProposalAction, SignedNetworkMessage, Stake, SudoAction, + ValidatorInfo, SUDO_KEY_BYTES, +}; + +// ============================================================================ +// HELPER FUNCTIONS +// ============================================================================ + +/// Create a test ChainState with a custom sudo key +fn create_test_state_with_sudo(sudo_keypair: &Keypair) -> ChainState { + ChainState::new(sudo_keypair.hotkey(), NetworkConfig::default()) +} + +/// Create a test ChainState with production sudo key +fn create_production_state() -> ChainState { + ChainState::new_production(NetworkConfig::default()) +} + +// ============================================================================ +// SUDO KEY AUTHORIZATION TESTS +// ============================================================================ + +#[test] +fn test_only_sudo_can_perform_actions() { + let sudo_kp = Keypair::generate(); + let non_sudo_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + // Verify sudo key is recognized + assert!( + state.is_sudo(&sudo_kp.hotkey()), + "Sudo keypair should be recognized as sudo" + ); + + // Verify non-sudo key is rejected + assert!( + !state.is_sudo(&non_sudo_kp.hotkey()), + "Non-sudo keypair should not be recognized as sudo" + ); + + // The actual authorization check is done at the network layer + // where SignedNetworkMessage.signer() is checked against state.sudo_key +} + +// ============================================================================ +// EMERGENCY CONTROLS TESTS +// ============================================================================ + +/// Simulated pause state for testing +struct PausedState { + is_paused: bool, + pause_reason: Option, +} + +impl PausedState { + fn new() -> Self { + Self { + is_paused: false, + pause_reason: None, + } + } + + fn pause(&mut self, reason: String) { + self.is_paused = true; + self.pause_reason = Some(reason); + } + + fn resume(&mut self) { + self.is_paused = false; + self.pause_reason = None; + } +} + +#[test] +fn test_emergency_pause_halts_operations() { + let mut paused_state = PausedState::new(); + + // Initially not paused + assert!(!paused_state.is_paused); + + // Create EmergencyPause action + let action = SudoAction::EmergencyPause { + reason: "Security vulnerability detected".to_string(), + }; + + // Apply pause + if let SudoAction::EmergencyPause { reason } = action { + paused_state.pause(reason); + } + + // Verify paused + assert!(paused_state.is_paused); + assert_eq!( + paused_state.pause_reason, + Some("Security vulnerability detected".to_string()) + ); +} + +#[test] +fn test_resume_restores_operations() { + let mut paused_state = PausedState::new(); + + // First pause + paused_state.pause("Test pause".to_string()); + assert!(paused_state.is_paused); + + // Create Resume action + let action = SudoAction::Resume; + + // Apply resume + if let SudoAction::Resume = action { + paused_state.resume(); + } + + // Verify resumed + assert!(!paused_state.is_paused); + assert!(paused_state.pause_reason.is_none()); +} + +#[test] +fn test_emergency_pause_requires_sudo() { + let sudo_kp = Keypair::generate(); + let non_sudo_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + // Create signed EmergencyPause message from sudo key + let pause_action = SudoAction::EmergencyPause { + reason: "Test".to_string(), + }; + let msg = NetworkMessage::SudoAction(pause_action); + let signed_sudo = + SignedNetworkMessage::new(msg.clone(), &sudo_kp).expect("Should sign message"); + + // Verify signature is valid + assert!(signed_sudo.verify().unwrap()); + + // Verify signer is sudo + assert!(state.is_sudo(signed_sudo.signer())); + + // Create signed message from non-sudo key + let non_sudo_msg = NetworkMessage::SudoAction(SudoAction::EmergencyPause { + reason: "Unauthorized".to_string(), + }); + let signed_non_sudo = + SignedNetworkMessage::new(non_sudo_msg, &non_sudo_kp).expect("Should sign message"); + + // Signature is valid but signer is not sudo + assert!(signed_non_sudo.verify().unwrap()); + assert!(!state.is_sudo(signed_non_sudo.signer())); +} + +#[test] +fn test_pause_reason_is_recorded() { + let action = SudoAction::EmergencyPause { + reason: "Critical bug in scoring algorithm".to_string(), + }; + + match action { + SudoAction::EmergencyPause { reason } => { + assert_eq!(reason, "Critical bug in scoring algorithm"); + assert!(!reason.is_empty()); + } + _ => panic!("Expected EmergencyPause action"), + } +} + +// ============================================================================ +// SIGNATURE VERIFICATION TESTS +// ============================================================================ + +#[test] +fn test_sudo_action_signed_by_sudo_key_accepted() { + let sudo_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + // Create sudo action + let action = SudoAction::UpdateConfig { + config: NetworkConfig::default(), + }; + let msg = NetworkMessage::SudoAction(action); + + // Sign with sudo key + let signed = SignedNetworkMessage::new(msg, &sudo_kp).expect("Should sign"); + + // Verify signature + assert!(signed.verify().unwrap(), "Signature should be valid"); + + // Verify signer is sudo + assert!( + state.is_sudo(signed.signer()), + "Signer should be recognized as sudo" + ); +} + +#[test] +fn test_sudo_action_signed_by_non_sudo_rejected() { + let sudo_kp = Keypair::generate(); + let non_sudo_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + // Create sudo action + let action = SudoAction::AddValidator { + info: ValidatorInfo::new(non_sudo_kp.hotkey(), Stake::new(10_000_000_000)), + }; + let msg = NetworkMessage::SudoAction(action); + + // Sign with non-sudo key + let signed = SignedNetworkMessage::new(msg, &non_sudo_kp).expect("Should sign"); + + // Signature is technically valid + assert!(signed.verify().unwrap(), "Signature should be valid"); + + // But signer is NOT sudo + assert!( + !state.is_sudo(signed.signer()), + "Non-sudo signer should be rejected" + ); +} + +#[test] +fn test_sudo_action_invalid_signature_rejected() { + let sudo_kp = Keypair::generate(); + + // Create sudo action and sign it + let action = SudoAction::Resume; + let msg = NetworkMessage::SudoAction(action); + let mut signed = SignedNetworkMessage::new(msg, &sudo_kp).expect("Should sign"); + + // Tamper with the signature + if !signed.signature.signature.is_empty() { + signed.signature.signature[0] ^= 0xFF; // Flip bits + } + + // Verification should fail + let result = signed.verify(); + // After tampering, verify should return false or error + if let Ok(valid) = result { + assert!(!valid, "Tampered signature should not verify"); + } +} + +#[test] +fn test_production_sudo_key_recognized() { + let state = create_production_state(); + + // Get production sudo key + let prod_sudo = production_sudo_key(); + + // Verify it's recognized + assert!(state.is_sudo(&prod_sudo)); + assert!(is_production_sudo(&prod_sudo)); + + // Verify raw bytes match + assert_eq!(prod_sudo.0, SUDO_KEY_BYTES); +} + +#[test] +fn test_proposal_action_sudo_variant() { + let sudo_action = SudoAction::EmergencyPause { + reason: "Testing proposal".to_string(), + }; + + let proposal = ProposalAction::Sudo(sudo_action); + + match proposal { + ProposalAction::Sudo(SudoAction::EmergencyPause { reason }) => { + assert_eq!(reason, "Testing proposal"); + } + _ => panic!("Expected Sudo(EmergencyPause)"), + } +} + +// ============================================================================ +// SUDO ACTION SERIALIZATION TESTS +// ============================================================================ + +#[test] +fn test_sudo_action_serialization_roundtrip() { + let actions = vec![ + SudoAction::EmergencyPause { + reason: "Test pause".to_string(), + }, + SudoAction::Resume, + SudoAction::SetChallengeWeight { + challenge_id: ChallengeId::new(), + mechanism_id: 1, + weight_ratio: 0.5, + }, + ]; + + for action in actions { + // Serialize + let bytes = bincode::serialize(&action).expect("Should serialize"); + + // Deserialize + let recovered: SudoAction = bincode::deserialize(&bytes).expect("Should deserialize"); + + // Verify match + match (&action, &recovered) { + ( + SudoAction::EmergencyPause { reason: r1 }, + SudoAction::EmergencyPause { reason: r2 }, + ) => assert_eq!(r1, r2), + (SudoAction::Resume, SudoAction::Resume) => {} + ( + SudoAction::SetChallengeWeight { + challenge_id: c1, + mechanism_id: m1, + weight_ratio: w1, + }, + SudoAction::SetChallengeWeight { + challenge_id: c2, + mechanism_id: m2, + weight_ratio: w2, + }, + ) => { + assert_eq!(c1, c2); + assert_eq!(m1, m2); + assert_eq!(w1, w2); + } + _ => panic!("Serialization roundtrip produced different variant"), + } + } +} + +// ============================================================================ +// SIGNED NETWORK MESSAGE TESTS FOR SUDO +// ============================================================================ + +#[test] +fn test_signed_sudo_message_contains_signer_info() { + let sudo_kp = Keypair::generate(); + + let action = SudoAction::EmergencyPause { + reason: "Signed Challenge".to_string(), + }; + let msg = NetworkMessage::SudoAction(action); + let signed = SignedNetworkMessage::new(msg, &sudo_kp).expect("Should sign"); + + // Verify signer is captured + assert_eq!(signed.signer(), &sudo_kp.hotkey()); + + // Verify message is accessible + match &signed.message { + NetworkMessage::SudoAction(SudoAction::EmergencyPause { reason }) => { + assert_eq!(reason, "Signed Challenge"); + } + _ => panic!("Expected SudoAction(EmergencyPause)"), + } +} + +#[test] +fn test_all_sudo_action_variants_can_be_signed() { + let kp = Keypair::generate(); + + let actions: Vec = vec![ + SudoAction::UpdateConfig { + config: NetworkConfig::default(), + }, + SudoAction::SetChallengeWeight { + challenge_id: ChallengeId::new(), + mechanism_id: 0, + weight_ratio: 0.5, + }, + SudoAction::SetMechanismBurnRate { + mechanism_id: 0, + burn_rate: 0.1, + }, + SudoAction::SetMechanismConfig { + mechanism_id: 0, + config: platform_core::MechanismWeightConfig::new(0), + }, + SudoAction::SetRequiredVersion { + min_version: "0.1.0".to_string(), + recommended_version: "0.2.0".to_string(), + mandatory: false, + deadline_block: None, + release_notes: None, + }, + SudoAction::AddValidator { + info: ValidatorInfo::new(kp.hotkey(), Stake::new(10_000_000_000)), + }, + SudoAction::RemoveValidator { + hotkey: kp.hotkey(), + }, + SudoAction::EmergencyPause { + reason: "Test".to_string(), + }, + SudoAction::Resume, + SudoAction::ForceStateUpdate { + state: ChainState::default(), + }, + ]; + + for action in actions { + let msg = NetworkMessage::SudoAction(action); + let signed = SignedNetworkMessage::new(msg, &kp); + assert!(signed.is_ok(), "Failed to sign SudoAction variant"); + + let signed = signed.unwrap(); + assert!(signed.verify().unwrap(), "Signature verification failed"); + } +} + +// ============================================================================ +// CHAIN STATE SUDO KEY TESTS +// ============================================================================ + +#[test] +fn test_chain_state_is_sudo_method() { + let sudo_kp = Keypair::generate(); + let other_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + assert!(state.is_sudo(&sudo_kp.hotkey())); + assert!(!state.is_sudo(&other_kp.hotkey())); + assert!(!state.is_sudo(&Hotkey([0u8; 32]))); +} + +#[test] +fn test_chain_state_sudo_key_immutable() { + let sudo_kp = Keypair::generate(); + let state = create_test_state_with_sudo(&sudo_kp); + + // Sudo key should remain constant + let original_sudo = state.sudo_key.clone(); + + // After various operations + let mut state = state; + state.increment_block(); + state.update_hash(); + + // Sudo key unchanged + assert_eq!(state.sudo_key, original_sudo); +} + +// ============================================================================ +// FORCE STATE UPDATE TESTS +// ============================================================================ + +#[test] +fn test_force_state_update_action() { + let sudo_kp = Keypair::generate(); + let mut recovery_state = ChainState::new(sudo_kp.hotkey(), NetworkConfig::default()); + recovery_state.block_height = 1000; + + let action = SudoAction::ForceStateUpdate { + state: recovery_state.clone(), + }; + + match action { + SudoAction::ForceStateUpdate { state } => { + assert_eq!(state.block_height, 1000); + assert!(state.is_sudo(&sudo_kp.hotkey())); + } + _ => panic!("Expected ForceStateUpdate"), + } +} + +// ============================================================================ +// CHALLENGE WEIGHT ALLOCATION TESTS +// ============================================================================ + +#[test] +fn test_set_challenge_weight_action() { + let challenge_id = ChallengeId::new(); + + let action = SudoAction::SetChallengeWeight { + challenge_id, + mechanism_id: 0, + weight_ratio: 0.75, + }; + + match action { + SudoAction::SetChallengeWeight { + challenge_id: cid, + mechanism_id, + weight_ratio, + } => { + assert_eq!(cid, challenge_id); + assert_eq!(mechanism_id, 0); + assert_eq!(weight_ratio, 0.75); + } + _ => panic!("Expected SetChallengeWeight"), + } +} + +#[test] +fn test_set_mechanism_burn_rate_action() { + let action = SudoAction::SetMechanismBurnRate { + mechanism_id: 1, + burn_rate: 0.15, + }; + + match action { + SudoAction::SetMechanismBurnRate { + mechanism_id, + burn_rate, + } => { + assert_eq!(mechanism_id, 1); + assert_eq!(burn_rate, 0.15); + } + _ => panic!("Expected SetMechanismBurnRate"), + } +} + +// ============================================================================ +// VERSION MANAGEMENT TESTS +// ============================================================================ + +#[test] +fn test_set_required_version_action() { + let action = SudoAction::SetRequiredVersion { + min_version: "0.2.0".to_string(), + recommended_version: "0.3.0".to_string(), + mandatory: true, + deadline_block: Some(50000), + release_notes: Some("Security patches and performance improvements".to_string()), + }; + + match action { + SudoAction::SetRequiredVersion { + min_version, + recommended_version, + mandatory, + deadline_block, + release_notes, + } => { + assert_eq!(min_version, "0.2.0"); + assert_eq!(recommended_version, "0.3.0"); + assert!(mandatory); + assert_eq!(deadline_block, Some(50000)); + assert!(release_notes.is_some()); + } + _ => panic!("Expected SetRequiredVersion"), + } +} + +#[test] +fn test_sudo_remove_validator_action() { + let validator_kp = Keypair::generate(); + + let action = SudoAction::RemoveValidator { + hotkey: validator_kp.hotkey(), + }; + + match action { + SudoAction::RemoveValidator { hotkey } => { + assert_eq!(hotkey, validator_kp.hotkey()); + } + _ => panic!("Expected RemoveValidator"), + } +} + +// ============================================================================ +// CONFIG UPDATE TESTS +// ============================================================================ + +#[test] +fn test_sudo_update_config_action() { + let config = NetworkConfig { + max_validators: 64, + consensus_threshold: 0.67, + ..Default::default() + }; + + let action = SudoAction::UpdateConfig { + config: config.clone(), + }; + + match action { + SudoAction::UpdateConfig { config: updated } => { + assert_eq!(updated.max_validators, 64); + assert_eq!(updated.consensus_threshold, 0.67); + } + _ => panic!("Expected UpdateConfig"), + } +} diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml deleted file mode 100644 index 8e31c0e34..000000000 --- a/wasm/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "term-challenge-wasm" -version = "0.1.0" -edition = "2021" -description = "Terminal Benchmark Challenge โ€“ WASM evaluation module" - -[lib] -crate-type = ["cdylib", "rlib"] - -[dependencies] -platform-challenge-sdk-wasm = { git = "https://github.com/PlatformNetwork/platform-v2", branch = "main" } -serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } -bincode = { version = "1.3", default-features = false } diff --git a/wasm/src/agent_storage.rs b/wasm/src/agent_storage.rs deleted file mode 100644 index 65df03f0c..000000000 --- a/wasm/src/agent_storage.rs +++ /dev/null @@ -1,88 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{host_storage_get, host_storage_set}; - -use crate::types::{AgentLogs, EvaluationStatus}; - -pub const MAX_AGENT_PACKAGE_SIZE: usize = 1_048_576; -const MAX_LOG_SIZE: usize = 262_144; -pub const MAX_TASK_OUTPUT_PREVIEW: usize = 4_096; - -fn make_key(prefix: &[u8], miner_hotkey: &str, epoch: u64) -> Vec { - let mut key = Vec::from(prefix); - key.extend_from_slice(miner_hotkey.as_bytes()); - key.push(b':'); - key.extend_from_slice(&epoch.to_le_bytes()); - key -} - -pub fn store_agent_code(miner_hotkey: &str, epoch: u64, package_zip: &[u8]) -> bool { - if package_zip.len() > MAX_AGENT_PACKAGE_SIZE { - return false; - } - let key = make_key(b"agent_code:", miner_hotkey, epoch); - host_storage_set(&key, package_zip).is_ok() -} - -pub fn store_agent_hash(miner_hotkey: &str, epoch: u64, agent_hash: &str) -> bool { - let key = make_key(b"agent_hash:", miner_hotkey, epoch); - host_storage_set(&key, agent_hash.as_bytes()).is_ok() -} - -pub fn store_agent_logs(miner_hotkey: &str, epoch: u64, logs: &AgentLogs) -> bool { - let data = match bincode::serialize(logs) { - Ok(d) => d, - Err(_) => return false, - }; - if data.len() > MAX_LOG_SIZE { - return false; - } - let key = make_key(b"agent_logs:", miner_hotkey, epoch); - host_storage_set(&key, &data).is_ok() -} - -pub fn get_agent_code(miner_hotkey: &str, epoch: u64) -> Option> { - let key = make_key(b"agent_code:", miner_hotkey, epoch); - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - Some(data) -} - -pub fn get_agent_logs(miner_hotkey: &str, epoch: u64) -> Option { - let key = make_key(b"agent_logs:", miner_hotkey, epoch); - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} - -pub fn truncate_output(output: &str, max_len: usize) -> String { - if output.len() <= max_len { - return String::from(output); - } - let mut end = max_len; - while end > 0 && !output.is_char_boundary(end) { - end -= 1; - } - String::from(&output[..end]) -} - -pub fn store_evaluation_status(miner_hotkey: &str, epoch: u64, status: EvaluationStatus) -> bool { - let key = make_key(b"eval_status:", miner_hotkey, epoch); - if let Ok(data) = bincode::serialize(&status) { - return host_storage_set(&key, &data).is_ok(); - } - false -} - -pub fn get_evaluation_status(miner_hotkey: &str, epoch: u64) -> Option { - let key = make_key(b"eval_status:", miner_hotkey, epoch); - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} diff --git a/wasm/src/ast_validation.rs b/wasm/src/ast_validation.rs deleted file mode 100644 index 280b46809..000000000 --- a/wasm/src/ast_validation.rs +++ /dev/null @@ -1,134 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{host_storage_get, host_storage_set}; - -use crate::types::{AstReviewResult, WhitelistConfig}; - -pub fn get_whitelist_config() -> WhitelistConfig { - host_storage_get(b"ast_whitelist_config") - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default() -} - -pub fn set_whitelist_config(config: &WhitelistConfig) -> bool { - if let Ok(data) = bincode::serialize(config) { - return host_storage_set(b"ast_whitelist_config", &data).is_ok(); - } - false -} - -pub fn validate_python_code(code: &str, config: &WhitelistConfig) -> AstReviewResult { - let mut violations = Vec::new(); - - if code.len() > config.max_code_size { - violations.push(String::from("Code exceeds maximum allowed size")); - } - - for builtin in &config.forbidden_builtins { - let mut pattern = String::from(builtin.as_str()); - pattern.push('('); - if code.contains(pattern.as_str()) { - let mut msg = String::from("Forbidden builtin: "); - msg.push_str(builtin); - violations.push(msg); - } - } - - check_dangerous_patterns(code, &mut violations); - check_imports(code, config, &mut violations); - - AstReviewResult { - passed: violations.is_empty(), - violations, - reviewer_validators: Vec::new(), - } -} - -fn check_dangerous_patterns(code: &str, violations: &mut Vec) { - let dangerous = [ - ("os.system(", "Direct OS command execution"), - ("os.popen(", "OS pipe execution"), - ("subprocess.call(", "Subprocess execution"), - ("subprocess.Popen(", "Subprocess execution"), - ("subprocess.run(", "Subprocess execution"), - ("socket.socket(", "Raw socket access"), - ("__import__(", "Dynamic import"), - ]; - - for (pattern, desc) in &dangerous { - if code.contains(pattern) { - let mut msg = String::from("Dangerous pattern: "); - msg.push_str(desc); - msg.push_str(" ("); - msg.push_str(pattern); - msg.push(')'); - violations.push(msg); - } - } -} - -fn check_imports(code: &str, config: &WhitelistConfig, violations: &mut Vec) { - for line in code.lines() { - let trimmed = line.trim(); - - if let Some(rest) = trimmed.strip_prefix("import ") { - let modules_part = if let Some(idx) = rest.find(" as ") { - &rest[..idx] - } else { - rest - }; - for module in modules_part.split(',') { - let module = module.trim(); - let root = module.split('.').next().unwrap_or(module).trim(); - if !root.is_empty() && !is_module_allowed(root, config) { - let mut msg = String::from("Disallowed module: "); - msg.push_str(root); - violations.push(msg); - } - } - } - - if let Some(rest) = trimmed.strip_prefix("from ") { - if let Some(import_idx) = rest.find(" import ") { - let module = rest[..import_idx].trim(); - let root = module.split('.').next().unwrap_or(module).trim(); - if !root.is_empty() && !is_module_allowed(root, config) { - let mut msg = String::from("Disallowed module: "); - msg.push_str(root); - violations.push(msg); - } - } - } - } -} - -fn is_module_allowed(module: &str, config: &WhitelistConfig) -> bool { - config.allowed_stdlib.iter().any(|s| s == module) - || config.allowed_third_party.iter().any(|s| s == module) -} - -pub fn store_ast_result(submission_id: &str, result: &AstReviewResult) -> bool { - let mut key = Vec::from(b"ast_review:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - if let Ok(data) = bincode::serialize(result) { - return host_storage_set(&key, &data).is_ok(); - } - false -} - -pub fn get_ast_result(submission_id: &str) -> Option { - let mut key = Vec::from(b"ast_review:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} diff --git a/wasm/src/dataset.rs b/wasm/src/dataset.rs deleted file mode 100644 index 4ee05fe69..000000000 --- a/wasm/src/dataset.rs +++ /dev/null @@ -1,115 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{ - host_random_seed, host_storage_get, host_storage_set, -}; - -use crate::types::DatasetSelection; - -const DATASET_PROPOSALS_KEY: &[u8] = b"dataset_proposals"; - -pub fn propose_task_indices(validator_id: &str, indices: &[u32]) -> bool { - let mut proposals: Vec<(String, Vec)> = host_storage_get(DATASET_PROPOSALS_KEY) - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default(); - - if let Some(pos) = proposals.iter().position(|(v, _)| v == validator_id) { - proposals[pos].1 = indices.to_vec(); - } else { - proposals.push((String::from(validator_id), indices.to_vec())); - } - - if let Ok(data) = bincode::serialize(&proposals) { - return host_storage_set(DATASET_PROPOSALS_KEY, &data).is_ok(); - } - false -} - -pub fn check_dataset_consensus() -> Option> { - let proposals: Vec<(String, Vec)> = host_storage_get(DATASET_PROPOSALS_KEY) - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default(); - - if proposals.is_empty() { - return None; - } - - let validator_count = proposals.len(); - let threshold = (validator_count / 2) + 1; - - let mut counts: Vec<(Vec, usize)> = Vec::new(); - for (_, indices) in &proposals { - let mut sorted = indices.clone(); - sorted.sort_unstable(); - if let Some(entry) = counts.iter_mut().find(|(k, _)| *k == sorted) { - entry.1 += 1; - } else { - counts.push((sorted, 1)); - } - } - - for (indices, count) in counts { - if count >= threshold { - return Some(indices); - } - } - None -} - -pub fn generate_random_indices(total_tasks: u32, select_count: u32) -> Vec { - let mut seed = [0u8; 32]; - let _ = host_random_seed(&mut seed); - - let count = select_count.min(total_tasks) as usize; - let mut indices = Vec::with_capacity(count); - let mut used = Vec::new(); - - for i in 0..count { - let idx_bytes = if i * 4 + 4 <= seed.len() { - let mut buf = [0u8; 4]; - buf.copy_from_slice(&seed[i * 4..i * 4 + 4]); - u32::from_le_bytes(buf) - } else { - seed[i % seed.len()] as u32 - }; - - let mut idx = idx_bytes % total_tasks; - let mut attempts = 0; - while used.contains(&idx) && attempts < total_tasks { - idx = (idx + 1) % total_tasks; - attempts += 1; - } - if !used.contains(&idx) { - used.push(idx); - indices.push(idx); - } - } - indices -} - -pub fn get_dataset_history() -> Vec { - host_storage_get(b"dataset_history") - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default() -} diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs deleted file mode 100644 index b5336936a..000000000 --- a/wasm/src/lib.rs +++ /dev/null @@ -1,454 +0,0 @@ -#![no_std] - -extern crate alloc; - -mod agent_storage; -mod ast_validation; -mod dataset; -mod llm_review; -mod routes; -mod scoring; -mod submission; -mod tasks; -mod timeout_handler; -mod types; - -use alloc::string::String; -use alloc::vec::Vec; -use bincode::Options; -use platform_challenge_sdk_wasm::host_functions::{ - host_consensus_get_epoch, host_http_post, host_storage_get, host_storage_set, -}; -use platform_challenge_sdk_wasm::{Challenge, EvaluationInput, EvaluationOutput}; - -use crate::scoring::{calculate_aggregate, format_summary, to_weight}; -use crate::types::{ - AgentLogEntry, AgentLogs, ChallengeParams, DatasetSelection, EvaluationStatus, LlmJudgeRequest, - LlmJudgeResponse, Submission, TaskResult, WasmRouteRequest, -}; - -const MAX_SUBMISSION_SIZE: u64 = 64 * 1024 * 1024; -const MAX_PARAMS_SIZE: u64 = 4 * 1024 * 1024; -const MAX_LLM_RESPONSE_SIZE: u64 = 1024 * 1024; -const MAX_ROUTE_REQUEST_SIZE: u64 = 1024 * 1024; -const MAX_TASKS: usize = 256; -const EPOCH_RATE_LIMIT: u64 = 3; - -fn bincode_options_submission() -> impl Options { - bincode::DefaultOptions::new() - .with_limit(MAX_SUBMISSION_SIZE) - .with_fixint_encoding() - .allow_trailing_bytes() -} - -fn bincode_options_params() -> impl Options { - bincode::DefaultOptions::new() - .with_limit(MAX_PARAMS_SIZE) - .with_fixint_encoding() - .allow_trailing_bytes() -} - -fn bincode_options_llm() -> impl Options { - bincode::DefaultOptions::new() - .with_limit(MAX_LLM_RESPONSE_SIZE) - .with_fixint_encoding() - .allow_trailing_bytes() -} - -fn bincode_options_route_request() -> impl Options { - bincode::DefaultOptions::new() - .with_limit(MAX_ROUTE_REQUEST_SIZE) - .with_fixint_encoding() - .allow_trailing_bytes() -} - -fn validate_task_result(result: &TaskResult) -> bool { - if result.task_id.is_empty() { - return false; - } - if !result.score.is_finite() || !(0.0..=1.0).contains(&result.score) { - return false; - } - true -} - -fn last_submission_key(miner_hotkey: &str) -> Vec { - let mut key = Vec::from(b"last_submission:" as &[u8]); - key.extend_from_slice(miner_hotkey.as_bytes()); - key -} - -fn get_last_submission_epoch(miner_hotkey: &str) -> Option { - let key = last_submission_key(miner_hotkey); - let data = host_storage_get(&key).ok()?; - if data.len() < 8 { - return None; - } - let mut buf = [0u8; 8]; - buf.copy_from_slice(&data[..8]); - Some(u64::from_le_bytes(buf)) -} - -fn set_last_submission_epoch(miner_hotkey: &str, epoch: u64) { - let key = last_submission_key(miner_hotkey); - let _ = host_storage_set(&key, &epoch.to_le_bytes()); -} - -fn store_score(hotkey: &str, score: f64) { - let mut key = Vec::from(b"score:" as &[u8]); - key.extend_from_slice(hotkey.as_bytes()); - let _ = host_storage_set(&key, &score.to_le_bytes()); -} - -fn store_submission_record(hotkey: &str, epoch: u64, agent_hash: &str) { - let mut key = Vec::from(b"submission:" as &[u8]); - key.extend_from_slice(hotkey.as_bytes()); - key.push(b':'); - key.extend_from_slice(&epoch.to_le_bytes()); - let _ = host_storage_set(&key, agent_hash.as_bytes()); -} - -pub struct TermChallengeWasm; - -impl Default for TermChallengeWasm { - fn default() -> Self { - Self - } -} - -impl TermChallengeWasm { - pub const fn new() -> Self { - Self - } - - fn try_llm_judge(url: &str, result: &TaskResult, instruction: &str) -> Option { - let request = LlmJudgeRequest { - task_id: result.task_id.clone(), - instruction: String::from(instruction), - agent_output: result.agent_output.clone(), - test_output: result.test_output.clone(), - }; - - let url_bytes = url.as_bytes(); - let body = match bincode::serialize(&request) { - Ok(b) => b, - Err(_) => return None, - }; - - let response_bytes = match host_http_post(url_bytes, &body) { - Ok(b) => b, - Err(_) => return None, - }; - - let judge_resp: LlmJudgeResponse = match bincode_options_llm().deserialize(&response_bytes) - { - Ok(r) => r, - Err(_) => return None, - }; - - if !judge_resp.score.is_finite() { - return None; - } - - Some(judge_resp.score.clamp(0.0, 1.0)) - } - - pub fn routes(&self) -> Vec { - let defs = routes::get_route_definitions(); - bincode::serialize(&defs).unwrap_or_default() - } - - pub fn handle_route(&self, request_data: &[u8]) -> Vec { - let request: WasmRouteRequest = - match bincode_options_route_request().deserialize(request_data) { - Ok(r) => r, - Err(_) => return Vec::new(), - }; - routes::handle_route_request(&request) - } -} - -impl Challenge for TermChallengeWasm { - fn name(&self) -> &'static str { - "term-challenge" - } - - fn version(&self) -> &'static str { - "4.0.0" - } - - fn evaluate(&self, input: EvaluationInput) -> EvaluationOutput { - let submission_data: Submission = - match bincode_options_submission().deserialize(&input.agent_data) { - Ok(s) => s, - Err(_) => return EvaluationOutput::failure("failed to deserialize submission"), - }; - - let params: ChallengeParams = match bincode_options_params().deserialize(&input.params) { - Ok(p) => p, - Err(_) => return EvaluationOutput::failure("failed to deserialize challenge params"), - }; - - if submission_data.task_results.is_empty() { - return EvaluationOutput::failure("submission contains no task results"); - } - - if submission_data.task_results.len() > MAX_TASKS { - return EvaluationOutput::failure("submission exceeds maximum task count"); - } - - if submission_data.task_results.len() != params.tasks.len() { - return EvaluationOutput::failure("task result count does not match task definitions"); - } - - for result in &submission_data.task_results { - if !validate_task_result(result) { - return EvaluationOutput::failure( - "invalid task result: bad score or empty task_id", - ); - } - } - - let miner_hotkey = submission_data.miner_hotkey; - let epoch = submission_data.epoch; - let agent_hash = submission_data.agent_hash; - let package_zip = submission_data.package_zip; - let mut results: Vec = submission_data.task_results; - - let _ = - agent_storage::store_evaluation_status(&miner_hotkey, epoch, EvaluationStatus::Pending); - - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::AstReview, - ); - let whitelist_config = ast_validation::get_whitelist_config(); - let code_str = core::str::from_utf8(&package_zip).unwrap_or(""); - let ast_result = ast_validation::validate_python_code(code_str, &whitelist_config); - let _ = ast_validation::store_ast_result(&agent_hash, &ast_result); - if !ast_result.passed { - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::Failed, - ); - return EvaluationOutput::failure("AST validation failed"); - } - - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::LlmReview, - ); - if let Some(ref url) = params.llm_judge_url { - if let Some(review_result) = llm_review::run_llm_review(code_str, url) { - let _ = llm_review::store_review_result(&agent_hash, &review_result); - if !review_result.approved { - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::Failed, - ); - return EvaluationOutput::failure("LLM review rejected submission"); - } - } - } - - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::Evaluating, - ); - - let _ = submission::submit_versioned(&miner_hotkey, &miner_hotkey, &agent_hash, epoch); - - if let Some(ref url) = params.llm_judge_url { - for (result, task) in results.iter_mut().zip(params.tasks.iter()) { - if !result.passed { - continue; - } - if let Some(llm_score) = Self::try_llm_judge(url, result, &task.name) { - result.score = llm_score; - if llm_score < 0.5 { - result.passed = false; - } - } - } - } - - let aggregate = calculate_aggregate(¶ms.tasks, &results); - let weight = to_weight(&aggregate); - - let final_weight = if let Some(ref decay_params) = params.decay_params { - scoring::apply_epoch_decay(weight, decay_params) - } else { - weight - }; - - let score = (final_weight * 10_000.0) as i64; - let message = format_summary(&aggregate); - - let _ = agent_storage::store_agent_code(&miner_hotkey, epoch, &package_zip); - let _ = agent_storage::store_agent_hash(&miner_hotkey, epoch, &agent_hash); - - let _ = scoring::update_top_agent_state(&agent_hash, final_weight, epoch); - - store_score(&miner_hotkey, final_weight); - store_submission_record(&miner_hotkey, epoch, &agent_hash); - - let mut entries = Vec::with_capacity(results.len()); - let mut total_size_bytes: u64 = 0; - for r in &results { - let output_preview = agent_storage::truncate_output( - &r.agent_output, - agent_storage::MAX_TASK_OUTPUT_PREVIEW, - ); - total_size_bytes = total_size_bytes.saturating_add(output_preview.len() as u64); - entries.push(AgentLogEntry { - task_id: r.task_id.clone(), - passed: r.passed, - score: r.score, - execution_time_ms: r.execution_time_ms, - output_preview, - error: r.error.clone(), - }); - } - - let logs = AgentLogs { - miner_hotkey: miner_hotkey.clone(), - epoch, - agent_hash: agent_hash.clone(), - entries, - total_size_bytes, - }; - let _ = agent_storage::store_agent_logs(&miner_hotkey, epoch, &logs); - - set_last_submission_epoch(&miner_hotkey, epoch); - - let _ = agent_storage::store_evaluation_status( - &miner_hotkey, - epoch, - EvaluationStatus::Completed, - ); - - EvaluationOutput::success(score, &message) - } - - fn validate(&self, input: EvaluationInput) -> bool { - let submission_data: Submission = - match bincode_options_submission().deserialize(&input.agent_data) { - Ok(s) => s, - Err(_) => return false, - }; - - let params: ChallengeParams = match bincode_options_params().deserialize(&input.params) { - Ok(p) => p, - Err(_) => return false, - }; - - if submission_data.agent_hash.is_empty() || submission_data.miner_hotkey.is_empty() { - return false; - } - - if submission_data.signature.is_empty() { - return false; - } - - if submission_data.package_zip.is_empty() { - return false; - } - - if submission_data.package_zip.len() > 1_048_576 { - return false; - } - - if submission_data.basilica_instance.is_empty() - || submission_data.executor_url.is_empty() - || submission_data.executor_token.is_empty() - { - return false; - } - - let current_epoch = host_consensus_get_epoch(); - if current_epoch >= 0 { - if let Some(last_epoch) = get_last_submission_epoch(&submission_data.miner_hotkey) { - let current = current_epoch as u64; - if current < last_epoch.saturating_add(EPOCH_RATE_LIMIT) { - return false; - } - } - } - - if submission_data.task_results.is_empty() { - return false; - } - - if submission_data.task_results.len() > MAX_TASKS { - return false; - } - - if submission_data.task_results.len() != params.tasks.len() { - return false; - } - - for result in &submission_data.task_results { - if !validate_task_result(result) { - return false; - } - } - - true - } - - fn tasks(&self) -> Vec { - let dataset = tasks::get_active_dataset(); - match dataset { - Some(task_defs) => bincode::serialize(&task_defs).unwrap_or_default(), - None => Vec::new(), - } - } - - fn configure(&self, config: &[u8]) { - if let Ok(selection) = bincode::deserialize::(config) { - tasks::store_dataset(&selection); - } - } -} - -platform_challenge_sdk_wasm::register_challenge!(TermChallengeWasm, TermChallengeWasm::new()); - -#[no_mangle] -pub extern "C" fn get_routes() -> i64 { - let challenge = TermChallengeWasm::new(); - let output = challenge.routes(); - if output.is_empty() { - return platform_challenge_sdk_wasm::pack_ptr_len(0, 0); - } - let ptr = platform_challenge_sdk_wasm::alloc_impl::sdk_alloc(output.len()); - if ptr.is_null() { - return platform_challenge_sdk_wasm::pack_ptr_len(0, 0); - } - unsafe { - core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); - } - platform_challenge_sdk_wasm::pack_ptr_len(ptr as i32, output.len() as i32) -} - -#[no_mangle] -pub extern "C" fn handle_route(req_ptr: i32, req_len: i32) -> i64 { - let slice = unsafe { core::slice::from_raw_parts(req_ptr as *const u8, req_len as usize) }; - let challenge = TermChallengeWasm::new(); - let output = challenge.handle_route(slice); - if output.is_empty() { - return platform_challenge_sdk_wasm::pack_ptr_len(0, 0); - } - let ptr = platform_challenge_sdk_wasm::alloc_impl::sdk_alloc(output.len()); - if ptr.is_null() { - return platform_challenge_sdk_wasm::pack_ptr_len(0, 0); - } - unsafe { - core::ptr::copy_nonoverlapping(output.as_ptr(), ptr, output.len()); - } - platform_challenge_sdk_wasm::pack_ptr_len(ptr as i32, output.len() as i32) -} diff --git a/wasm/src/llm_review.rs b/wasm/src/llm_review.rs deleted file mode 100644 index ad69b0dd8..000000000 --- a/wasm/src/llm_review.rs +++ /dev/null @@ -1,356 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use core::fmt::Write as _; -use platform_challenge_sdk_wasm::host_functions::{ - host_http_post, host_random_seed, host_storage_get, host_storage_set, -}; - -use crate::types::{LlmMessage, LlmRequest, LlmResponse, LlmReviewResult}; - -const DEFAULT_LLM_MODEL: &str = "moonshotai/Kimi-K2.5-TEE"; -const MAX_LLM_CODE_SIZE: usize = 50_000; - -const DEFAULT_SYSTEM_PROMPT: &str = "You are a strict security code reviewer for a terminal-based AI agent challenge.\n\nYour task is to analyze Python agent code and determine if it complies with the validation rules.\n\nRules:\n1. No hardcoded API keys or secrets\n2. No malicious code patterns\n3. No attempts to exploit the evaluation environment\n4. Code must be original (no plagiarism)\n\nRespond with a JSON object: {\"approved\": true/false, \"reason\": \"...\", \"violations\": []}"; - -pub fn is_llm_available() -> bool { - host_storage_get(b"llm_enabled") - .ok() - .is_some_and(|d| !d.is_empty() && d[0] == 1) -} - -pub fn select_reviewers(validators_json: &[u8], submission_hash: &[u8], offset: u8) -> Vec { - let validators: Vec = match bincode::deserialize(validators_json) { - Ok(v) => v, - Err(_) => return Vec::new(), - }; - - if validators.len() < 3 { - return validators; - } - - let mut seed = [0u8; 32]; - let _ = host_random_seed(&mut seed); - for (i, b) in submission_hash.iter().enumerate() { - if i < 32 { - seed[i] ^= b; - } - } - if !seed.is_empty() { - seed[0] = seed[0].wrapping_add(offset); - } - - let n = validators.len(); - let mut selected = Vec::with_capacity(3); - let mut used = Vec::new(); - - for i in 0..3 { - let idx_bytes = if i * 4 + 4 <= seed.len() { - let mut buf = [0u8; 4]; - buf.copy_from_slice(&seed[i * 4..i * 4 + 4]); - u32::from_le_bytes(buf) as usize - } else { - (seed[i % seed.len()] as usize).wrapping_mul(i + 1) - }; - - let mut idx = idx_bytes % n; - let mut attempts = 0; - while used.contains(&idx) && attempts < n { - idx = (idx + 1) % n; - attempts += 1; - } - if !used.contains(&idx) { - used.push(idx); - selected.push(validators[idx].clone()); - } - } - selected -} - -pub fn run_llm_review(agent_code: &str, llm_url: &str) -> Option { - if !is_llm_available() { - return None; - } - - let redacted_code = redact_api_keys(agent_code); - - let mut prompt = String::new(); - let _ = write!( - prompt, - "Review the following Python agent code:\n\n```python\n{}\n```\n\nProvide your verdict as JSON: {{\"approved\": true/false, \"reason\": \"...\", \"violations\": []}}", - redacted_code - ); - - let request = LlmRequest { - model: String::from(DEFAULT_LLM_MODEL), - messages: alloc::vec![ - LlmMessage { - role: String::from("system"), - content: String::from(DEFAULT_SYSTEM_PROMPT), - }, - LlmMessage { - role: String::from("user"), - content: prompt, - }, - ], - max_tokens: 2048, - temperature: 0.1, - }; - - let request_bytes = bincode::serialize(&request).ok()?; - let response_bytes = host_http_post(llm_url.as_bytes(), &request_bytes).ok()?; - let response: LlmResponse = bincode::deserialize(&response_bytes).ok()?; - - parse_llm_verdict(&response.content) -} - -fn parse_llm_verdict(content: &str) -> Option { - let json_start = content.find('{')?; - let json_end = content.rfind('}')? + 1; - if json_start >= json_end { - return None; - } - let json_str = &content[json_start..json_end]; - - let approved = - json_str.contains("\"approved\": true") || json_str.contains("\"approved\":true"); - - let reason = extract_json_string(json_str, "reason").unwrap_or_default(); - - Some(LlmReviewResult { - approved, - reason, - violations: Vec::new(), - reviewer_validators: Vec::new(), - scores: Vec::new(), - }) -} - -fn extract_json_string(json: &str, key: &str) -> Option { - let mut search = String::from("\""); - search.push_str(key); - search.push_str("\": \""); - let start = json.find(search.as_str())? + search.len(); - let rest = &json[start..]; - let end = rest.find('"')?; - Some(String::from(&rest[..end])) -} - -const REDACTED_MARKER: &str = "[REDACTED]"; -const MIN_TOKEN_LEN: usize = 12; -const MIN_QUOTED_SECRET_LEN: usize = 16; -const SECRET_CONTEXT_WINDOW: usize = 80; - -fn redact_api_keys(code: &str) -> String { - let src = if code.len() > MAX_LLM_CODE_SIZE { - let boundary = find_char_boundary(code, MAX_LLM_CODE_SIZE); - &code[..boundary] - } else { - code - }; - - let bytes = src.as_bytes(); - let len = bytes.len(); - let mut result = String::with_capacity(len); - let mut i = 0; - - while i < len { - if let Some(end) = try_match_known_prefix(bytes, i) { - result.push_str(REDACTED_MARKER); - i = end; - continue; - } - - if let Some(end) = try_match_quoted_secret(bytes, i) { - result.push_str(REDACTED_MARKER); - i = end; - continue; - } - - result.push(bytes[i] as char); - i += 1; - } - - if code.len() > MAX_LLM_CODE_SIZE { - result.push_str("\n... [truncated]"); - } - result -} - -fn find_char_boundary(s: &str, max: usize) -> usize { - if max >= s.len() { - return s.len(); - } - let mut boundary = max; - while boundary > 0 && !s.is_char_boundary(boundary) { - boundary -= 1; - } - boundary -} - -fn try_match_known_prefix(bytes: &[u8], start: usize) -> Option { - const PREFIXES: &[&[u8]] = &[ - b"sk-", - b"sk_live_", - b"sk_test_", - b"pk_live_", - b"pk_test_", - b"AKIA", - b"ghp_", - b"gho_", - b"github_pat_", - b"glpat-", - b"xoxb-", - b"xoxp-", - b"xapp-", - ]; - - for prefix in PREFIXES { - let plen = prefix.len(); - if start + plen > bytes.len() { - continue; - } - if &bytes[start..start + plen] == *prefix { - let token_end = scan_token_end(bytes, start + plen); - if token_end - start >= MIN_TOKEN_LEN { - return Some(token_end); - } - } - } - None -} - -fn try_match_quoted_secret(bytes: &[u8], start: usize) -> Option { - let quote = bytes[start]; - if quote != b'"' && quote != b'\'' { - return None; - } - - if !is_preceded_by_secret_keyword(bytes, start) { - return None; - } - - let content_start = start + 1; - let mut end = content_start; - while end < bytes.len() && bytes[end] != quote && bytes[end] != b'\n' { - end += 1; - } - - let content_len = end - content_start; - if content_len < MIN_QUOTED_SECRET_LEN { - return None; - } - - let all_token = bytes[content_start..end] - .iter() - .all(|&b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.'); - if !all_token { - return None; - } - - if end < bytes.len() && bytes[end] == quote { - end += 1; - } - Some(end) -} - -fn is_preceded_by_secret_keyword(bytes: &[u8], quote_pos: usize) -> bool { - let search_start = quote_pos.saturating_sub(SECRET_CONTEXT_WINDOW); - - let line_start = match bytes[search_start..quote_pos] - .iter() - .rposition(|&b| b == b'\n') - { - Some(pos) => search_start + pos + 1, - None => search_start, - }; - - let before = &bytes[line_start..quote_pos]; - let mut lower_buf = alloc::vec::Vec::with_capacity(before.len()); - for &b in before { - lower_buf.push(b.to_ascii_lowercase()); - } - let lower_str = core::str::from_utf8(&lower_buf).unwrap_or(""); - - const SECRET_KEYWORDS: &[&str] = &[ - "api_key", - "apikey", - "api-key", - "secret", - "token", - "password", - "passwd", - "credential", - "auth_key", - "access_key", - "private_key", - "openai_api", - "anthropic_api", - ]; - - for keyword in SECRET_KEYWORDS { - if lower_str.contains(keyword) { - return true; - } - } - false -} - -fn scan_token_end(bytes: &[u8], start: usize) -> usize { - let mut i = start; - while i < bytes.len() - && (bytes[i].is_ascii_alphanumeric() - || bytes[i] == b'-' - || bytes[i] == b'_' - || bytes[i] == b'.') - { - i += 1; - } - i -} - -pub fn store_review_result(submission_id: &str, result: &LlmReviewResult) -> bool { - let mut key = Vec::from(b"llm_review:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - if let Ok(data) = bincode::serialize(result) { - return host_storage_set(&key, &data).is_ok(); - } - false -} - -pub fn get_review_result(submission_id: &str) -> Option { - let mut key = Vec::from(b"llm_review:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} - -pub fn aggregate_reviews(results: &[LlmReviewResult]) -> LlmReviewResult { - let approved_count = results.iter().filter(|r| r.approved).count(); - let total = results.len(); - let approved = total > 0 && approved_count * 2 > total; - - let mut all_violations = Vec::new(); - let mut all_validators = Vec::new(); - let mut all_scores = Vec::new(); - let mut reason = String::new(); - - for r in results { - all_violations.extend(r.violations.iter().cloned()); - all_validators.extend(r.reviewer_validators.iter().cloned()); - all_scores.extend(r.scores.iter().copied()); - if !r.reason.is_empty() && reason.is_empty() { - reason = r.reason.clone(); - } - } - - LlmReviewResult { - approved, - reason, - violations: all_violations, - reviewer_validators: all_validators, - scores: all_scores, - } -} diff --git a/wasm/src/routes.rs b/wasm/src/routes.rs deleted file mode 100644 index 17c4ff54d..000000000 --- a/wasm/src/routes.rs +++ /dev/null @@ -1,530 +0,0 @@ -use alloc::string::String; -use alloc::vec; -use alloc::vec::Vec; -use bincode::Options; -use platform_challenge_sdk_wasm::host_functions::{ - host_consensus_get_epoch, host_consensus_get_submission_count, host_storage_get, -}; - -use crate::types::{ - LeaderboardEntry, RouteDefinition, StatsResponse, TimeoutConfig, TopAgentState, - WasmRouteRequest, WhitelistConfig, -}; -use crate::{ - agent_storage, ast_validation, dataset, llm_review, scoring, submission, timeout_handler, -}; - -const MAX_ROUTE_BODY_SIZE: usize = 1_048_576; - -fn bincode_options_route_body() -> impl Options { - bincode::DefaultOptions::new() - .with_limit(MAX_ROUTE_BODY_SIZE as u64) - .with_fixint_encoding() - .allow_trailing_bytes() -} - -fn is_authenticated(request: &WasmRouteRequest) -> bool { - request - .auth_hotkey - .as_ref() - .map(|k| !k.is_empty()) - .unwrap_or(false) -} - -fn unauthorized_response() -> Vec { - bincode::serialize(&false).unwrap_or_default() -} - -pub fn get_route_definitions() -> Vec { - vec![ - RouteDefinition { - method: String::from("GET"), - path: String::from("/leaderboard"), - description: String::from( - "Returns current leaderboard with scores, miner hotkeys, and ranks", - ), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/submissions"), - description: String::from("Returns pending submissions awaiting evaluation"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/submissions/:id"), - description: String::from("Returns specific submission status"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/dataset"), - description: String::from("Returns current active dataset of 50 SWE-bench tasks"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/dataset/history"), - description: String::from("Returns historical dataset selections"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/submit"), - description: String::from("Submission endpoint: receives zip package and metadata"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/decay"), - description: String::from("Returns current decay status for top agents"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/stats"), - description: String::from("Challenge statistics: total submissions, active miners"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/agent/:hotkey/code"), - description: String::from("Returns stored agent code package for a miner"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/agent/:hotkey/logs"), - description: String::from("Returns evaluation logs for a miner"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/agent/:hotkey/journey"), - description: String::from("Returns evaluation status journey for a miner"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/review/:id"), - description: String::from("Returns LLM review result for a submission"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/ast/:id"), - description: String::from("Returns AST validation result for a submission"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/submission/:name"), - description: String::from("Returns submission info by name"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/config/timeout"), - description: String::from("Returns current timeout configuration"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/config/timeout"), - description: String::from("Updates timeout configuration (requires auth)"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/config/whitelist"), - description: String::from("Returns current AST whitelist configuration"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/config/whitelist"), - description: String::from("Updates AST whitelist configuration (requires auth)"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/dataset/propose"), - description: String::from("Propose task indices for dataset consensus (requires auth)"), - }, - RouteDefinition { - method: String::from("GET"), - path: String::from("/dataset/consensus"), - description: String::from("Check dataset consensus status"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/review/select"), - description: String::from("Select reviewers for a submission (requires auth)"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/review/aggregate"), - description: String::from("Aggregate multiple review results (requires auth)"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/timeout/record"), - description: String::from( - "Record a review assignment for timeout tracking (requires auth)", - ), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/timeout/check"), - description: String::from("Check if a review assignment has timed out (requires auth)"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/dataset/random"), - description: String::from("Generate random task indices (requires auth)"), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/timeout/replace"), - description: String::from( - "Select a replacement validator for a timed-out review (requires auth)", - ), - }, - RouteDefinition { - method: String::from("POST"), - path: String::from("/timeout/mark"), - description: String::from("Mark a review assignment as timed out (requires auth)"), - }, - ] -} - -pub fn handle_route_request(request: &WasmRouteRequest) -> Vec { - let path = request.path.as_str(); - let method = request.method.as_str(); - - match (method, path) { - ("GET", "/leaderboard") => handle_leaderboard(), - ("GET", "/stats") => handle_stats(), - ("GET", "/decay") => handle_decay(), - ("GET", "/dataset/history") => handle_dataset_history(), - ("GET", "/dataset/consensus") => handle_dataset_consensus(), - ("GET", "/config/timeout") => handle_get_timeout_config(), - ("GET", "/config/whitelist") => handle_get_whitelist_config(), - ("POST", "/config/timeout") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_set_timeout_config(&request.body) - } - ("POST", "/config/whitelist") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_set_whitelist_config(&request.body) - } - ("POST", "/dataset/propose") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_dataset_propose(&request.body) - } - ("POST", "/dataset/random") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_dataset_random(&request.body) - } - ("POST", "/review/select") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_review_select(&request.body) - } - ("POST", "/review/aggregate") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_review_aggregate(&request.body) - } - ("POST", "/timeout/record") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_timeout_record(&request.body) - } - ("POST", "/timeout/check") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_timeout_check(&request.body) - } - ("POST", "/timeout/replace") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_timeout_replace(&request.body) - } - ("POST", "/timeout/mark") => { - if !is_authenticated(request) { - return unauthorized_response(); - } - handle_timeout_mark(&request.body) - } - _ => { - if method == "GET" { - if let Some(id) = path.strip_prefix("/review/") { - return handle_review(id); - } - if let Some(id) = path.strip_prefix("/ast/") { - return handle_ast(id); - } - if let Some(name) = path.strip_prefix("/submission/") { - return handle_submission_by_name(name); - } - if let Some(rest) = path.strip_prefix("/agent/") { - if let Some(hotkey) = rest.strip_suffix("/journey") { - return handle_journey(hotkey); - } - if let Some(hotkey) = rest.strip_suffix("/logs") { - return handle_logs(hotkey); - } - if let Some(hotkey) = rest.strip_suffix("/code") { - return handle_code(hotkey); - } - } - } - Vec::new() - } - } -} - -fn handle_leaderboard() -> Vec { - let entries: Vec = host_storage_get(b"leaderboard") - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default(); - bincode::serialize(&entries).unwrap_or_default() -} - -fn handle_stats() -> Vec { - let total_submissions = host_consensus_get_submission_count() as u64; - let epoch = host_consensus_get_epoch(); - let active_miners = host_storage_get(b"active_miner_count") - .ok() - .and_then(|d| { - if d.len() >= 8 { - let mut buf = [0u8; 8]; - buf.copy_from_slice(&d[..8]); - Some(u64::from_le_bytes(buf)) - } else { - None - } - }) - .unwrap_or(0); - let validator_count = host_storage_get(b"validator_count") - .ok() - .and_then(|d| { - if d.len() >= 8 { - let mut buf = [0u8; 8]; - buf.copy_from_slice(&d[..8]); - Some(u64::from_le_bytes(buf)) - } else { - None - } - }) - .unwrap_or(0); - - let stats = StatsResponse { - total_submissions, - active_miners, - validator_count, - }; - let _ = epoch; - bincode::serialize(&stats).unwrap_or_default() -} - -fn handle_decay() -> Vec { - let state: Option = scoring::get_top_agent_state(); - bincode::serialize(&state).unwrap_or_default() -} - -fn handle_dataset_history() -> Vec { - let history = dataset::get_dataset_history(); - bincode::serialize(&history).unwrap_or_default() -} - -fn handle_review(id: &str) -> Vec { - let result = llm_review::get_review_result(id); - bincode::serialize(&result).unwrap_or_default() -} - -fn handle_ast(id: &str) -> Vec { - let result = ast_validation::get_ast_result(id); - bincode::serialize(&result).unwrap_or_default() -} - -fn handle_submission_by_name(name: &str) -> Vec { - let result = submission::get_submission_by_name(name); - bincode::serialize(&result).unwrap_or_default() -} - -fn handle_journey(hotkey: &str) -> Vec { - let epoch = host_consensus_get_epoch(); - let current_epoch = if epoch >= 0 { epoch as u64 } else { 0 }; - let status = agent_storage::get_evaluation_status(hotkey, current_epoch); - bincode::serialize(&status).unwrap_or_default() -} - -fn handle_logs(hotkey: &str) -> Vec { - let epoch = host_consensus_get_epoch(); - let current_epoch = if epoch >= 0 { epoch as u64 } else { 0 }; - let logs = agent_storage::get_agent_logs(hotkey, current_epoch); - bincode::serialize(&logs).unwrap_or_default() -} - -fn handle_code(hotkey: &str) -> Vec { - let epoch = host_consensus_get_epoch(); - let current_epoch = if epoch >= 0 { epoch as u64 } else { 0 }; - agent_storage::get_agent_code(hotkey, current_epoch).unwrap_or_default() -} - -fn handle_get_timeout_config() -> Vec { - let config = timeout_handler::get_timeout_config(); - bincode::serialize(&config).unwrap_or_default() -} - -fn handle_set_timeout_config(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok(config) = bincode_options_route_body().deserialize::(body) { - let ok = timeout_handler::set_timeout_config(&config); - bincode::serialize(&ok).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} - -fn handle_get_whitelist_config() -> Vec { - let config = ast_validation::get_whitelist_config(); - bincode::serialize(&config).unwrap_or_default() -} - -fn handle_set_whitelist_config(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok(config) = bincode_options_route_body().deserialize::(body) { - let ok = ast_validation::set_whitelist_config(&config); - bincode::serialize(&ok).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} - -fn handle_dataset_consensus() -> Vec { - let result = dataset::check_dataset_consensus(); - bincode::serialize(&result).unwrap_or_default() -} - -fn handle_dataset_propose(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok((validator_id, indices)) = - bincode_options_route_body().deserialize::<(String, Vec)>(body) - { - let ok = dataset::propose_task_indices(&validator_id, &indices); - bincode::serialize(&ok).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} - -fn handle_dataset_random(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return Vec::new(); - } - if let Ok((total_tasks, select_count)) = - bincode_options_route_body().deserialize::<(u32, u32)>(body) - { - let indices = dataset::generate_random_indices(total_tasks, select_count); - bincode::serialize(&indices).unwrap_or_default() - } else { - Vec::new() - } -} - -fn handle_review_select(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return Vec::new(); - } - if let Ok((validators_json, submission_hash, offset)) = - bincode_options_route_body().deserialize::<(Vec, Vec, u8)>(body) - { - let reviewers = llm_review::select_reviewers(&validators_json, &submission_hash, offset); - bincode::serialize(&reviewers).unwrap_or_default() - } else { - Vec::new() - } -} - -fn handle_review_aggregate(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return Vec::new(); - } - if let Ok(results) = - bincode_options_route_body().deserialize::>(body) - { - let aggregated = llm_review::aggregate_reviews(&results); - bincode::serialize(&aggregated).unwrap_or_default() - } else { - Vec::new() - } -} - -fn handle_timeout_record(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok((submission_id, validator, review_type)) = - bincode_options_route_body().deserialize::<(String, String, String)>(body) - { - let ok = timeout_handler::record_assignment(&submission_id, &validator, &review_type); - bincode::serialize(&ok).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} - -fn handle_timeout_check(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok((submission_id, validator, review_type, timeout_ms)) = - bincode_options_route_body().deserialize::<(String, String, String, u64)>(body) - { - let timed_out = - timeout_handler::check_timeout(&submission_id, &validator, &review_type, timeout_ms); - bincode::serialize(&timed_out).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} - -fn handle_timeout_replace(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&Option::::None).unwrap_or_default(); - } - if let Ok((validators, excluded, seed)) = - bincode_options_route_body().deserialize::<(Vec, Vec, Vec)>(body) - { - let replacement = timeout_handler::select_replacement(&validators, &excluded, &seed); - bincode::serialize(&replacement).unwrap_or_default() - } else { - bincode::serialize(&Option::::None).unwrap_or_default() - } -} - -fn handle_timeout_mark(body: &[u8]) -> Vec { - if body.len() > MAX_ROUTE_BODY_SIZE { - return bincode::serialize(&false).unwrap_or_default(); - } - if let Ok((submission_id, validator, review_type)) = - bincode_options_route_body().deserialize::<(String, String, String)>(body) - { - let ok = timeout_handler::mark_timed_out(&submission_id, &validator, &review_type); - bincode::serialize(&ok).unwrap_or_default() - } else { - bincode::serialize(&false).unwrap_or_default() - } -} diff --git a/wasm/src/scoring.rs b/wasm/src/scoring.rs deleted file mode 100644 index 3c67fa421..000000000 --- a/wasm/src/scoring.rs +++ /dev/null @@ -1,183 +0,0 @@ -use alloc::string::String; -use core::fmt::Write as _; -use platform_challenge_sdk_wasm::host_functions::{ - host_consensus_get_epoch, host_storage_get, host_storage_set, -}; - -use crate::types::{ - DecayParams, Difficulty, DifficultyStats, TaskDefinition, TaskResult, TopAgentState, -}; - -const TOP_AGENT_KEY: &[u8] = b"top_agent_state"; -const GRACE_EPOCHS: u64 = 60; -const HALF_LIFE_EPOCHS: f64 = 20.0; - -pub struct AggregateScore { - pub tasks_passed: u32, - pub tasks_failed: u32, - pub pass_rate: f64, - pub total_execution_time_ms: u64, - pub easy_stats: DifficultyStats, - pub medium_stats: DifficultyStats, - pub hard_stats: DifficultyStats, -} - -impl AggregateScore { - pub fn total_tasks(&self) -> u32 { - self.tasks_passed.saturating_add(self.tasks_failed) - } -} - -/// Calculate aggregate scoring statistics from task definitions and results. -pub fn calculate_aggregate(tasks: &[TaskDefinition], results: &[TaskResult]) -> AggregateScore { - let mut passed: u32 = 0; - let mut failed: u32 = 0; - let mut total_execution_time_ms: u64 = 0; - let mut easy = DifficultyStats { - total: 0, - passed: 0, - }; - let mut medium = DifficultyStats { - total: 0, - passed: 0, - }; - let mut hard = DifficultyStats { - total: 0, - passed: 0, - }; - - for (task, result) in tasks.iter().zip(results.iter()) { - if result.passed { - passed += 1; - } else { - failed += 1; - } - - total_execution_time_ms = total_execution_time_ms.saturating_add(result.execution_time_ms); - - let stats = match task.difficulty { - Difficulty::Easy => &mut easy, - Difficulty::Medium => &mut medium, - Difficulty::Hard => &mut hard, - }; - stats.total += 1; - if result.passed { - stats.passed += 1; - } - } - - let total = passed + failed; - let pass_rate = if total > 0 { - passed as f64 / total as f64 - } else { - 0.0 - }; - - AggregateScore { - tasks_passed: passed, - tasks_failed: failed, - pass_rate, - total_execution_time_ms, - easy_stats: easy, - medium_stats: medium, - hard_stats: hard, - } -} - -/// Convert aggregate score to weight (normalized 0.0-1.0). -pub fn to_weight(score: &AggregateScore) -> f64 { - score.pass_rate.clamp(0.0, 1.0) -} - -/// Format a human-readable summary of aggregate scoring results. -pub fn format_summary(score: &AggregateScore) -> String { - let mut msg = String::new(); - let _ = write!( - msg, - "passed={}/{} rate={:.2}%", - score.tasks_passed, - score.total_tasks(), - score.pass_rate * 100.0, - ); - if score.easy_stats.total > 0 { - let _ = write!( - msg, - " easy={}/{}", - score.easy_stats.passed, score.easy_stats.total, - ); - } - if score.medium_stats.total > 0 { - let _ = write!( - msg, - " med={}/{}", - score.medium_stats.passed, score.medium_stats.total, - ); - } - if score.hard_stats.total > 0 { - let _ = write!( - msg, - " hard={}/{}", - score.hard_stats.passed, score.hard_stats.total, - ); - } - let _ = write!(msg, " time={}ms", score.total_execution_time_ms); - msg -} - -/// Retrieve the current top agent state from storage. -pub fn get_top_agent_state() -> Option { - let data = host_storage_get(TOP_AGENT_KEY).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} - -/// Update the top agent state if the new score is higher, or refresh staleness. -pub fn update_top_agent_state(agent_hash: &str, score: f64, epoch: u64) -> bool { - let current = get_top_agent_state(); - let should_update = match ¤t { - Some(state) => score > state.score, - None => true, - }; - - if should_update { - let state = TopAgentState { - agent_hash: String::from(agent_hash), - score, - achieved_epoch: epoch, - epochs_stale: 0, - decay_active: false, - current_burn_percent: 0.0, - }; - if let Ok(data) = bincode::serialize(&state) { - return host_storage_set(TOP_AGENT_KEY, &data).is_ok(); - } - } else if let Some(mut state) = current { - let current_epoch = host_consensus_get_epoch(); - if current_epoch >= 0 { - state.epochs_stale = (current_epoch as u64).saturating_sub(state.achieved_epoch); - state.decay_active = state.epochs_stale > GRACE_EPOCHS; - if state.decay_active { - let decay_epochs = state.epochs_stale.saturating_sub(GRACE_EPOCHS); - let multiplier = 0.5f64.powf(decay_epochs as f64 / HALF_LIFE_EPOCHS); - state.current_burn_percent = (1.0 - multiplier) * 100.0; - } - if let Ok(data) = bincode::serialize(&state) { - let _ = host_storage_set(TOP_AGENT_KEY, &data); - } - } - } - false -} - -/// Apply epoch-based decay using the stored top agent staleness state. -pub fn apply_epoch_decay(weight: f64, params: &DecayParams) -> f64 { - if let Some(state) = get_top_agent_state() { - if state.decay_active { - let multiplier = 1.0 - (state.current_burn_percent / 100.0); - return weight * multiplier.max(params.min_multiplier); - } - } - weight -} diff --git a/wasm/src/submission.rs b/wasm/src/submission.rs deleted file mode 100644 index aa14d2ccf..000000000 --- a/wasm/src/submission.rs +++ /dev/null @@ -1,102 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{ - host_consensus_get_epoch, host_storage_get, host_storage_set, -}; - -use crate::types::{SubmissionName, SubmissionVersion}; - -pub fn register_submission_name(name: &str, hotkey: &str) -> bool { - let mut key = Vec::from(b"name_registry:" as &[u8]); - key.extend_from_slice(name.as_bytes()); - - if let Ok(data) = host_storage_get(&key) { - if !data.is_empty() { - if let Ok(existing) = bincode::deserialize::(&data) { - return existing.owner_hotkey == hotkey; - } - return false; - } - } - - let epoch = host_consensus_get_epoch(); - let entry = SubmissionName { - name: String::from(name), - owner_hotkey: String::from(hotkey), - registered_epoch: if epoch >= 0 { epoch as u64 } else { 0 }, - }; - if let Ok(data) = bincode::serialize(&entry) { - return host_storage_set(&key, &data).is_ok(); - } - false -} - -pub fn submit_versioned(name: &str, hotkey: &str, agent_hash: &str, epoch: u64) -> Option { - if !register_submission_name(name, hotkey) { - return None; - } - - let mut key = Vec::from(b"submission_versions:" as &[u8]); - key.extend_from_slice(hotkey.as_bytes()); - key.push(b':'); - key.extend_from_slice(name.as_bytes()); - - let mut versions: Vec = host_storage_get(&key) - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default(); - - let next_version = versions.last().map(|v| v.version + 1).unwrap_or(1); - versions.push(SubmissionVersion { - version: next_version, - agent_hash: String::from(agent_hash), - epoch, - score: None, - }); - - if let Ok(data) = bincode::serialize(&versions) { - if host_storage_set(&key, &data).is_ok() { - return Some(next_version); - } - } - None -} - -pub fn get_submission_history(hotkey: &str, name: &str) -> Vec { - let mut key = Vec::from(b"submission_versions:" as &[u8]); - key.extend_from_slice(hotkey.as_bytes()); - key.push(b':'); - key.extend_from_slice(name.as_bytes()); - - host_storage_get(&key) - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default() -} - -pub fn get_submission_by_name(name: &str) -> Option<(String, SubmissionVersion)> { - let mut key = Vec::from(b"name_registry:" as &[u8]); - key.extend_from_slice(name.as_bytes()); - - let data = host_storage_get(&key).ok()?; - if data.is_empty() { - return None; - } - let entry: SubmissionName = bincode::deserialize(&data).ok()?; - - let versions = get_submission_history(&entry.owner_hotkey, name); - let latest = versions.last()?.clone(); - Some((entry.owner_hotkey, latest)) -} diff --git a/wasm/src/tasks.rs b/wasm/src/tasks.rs deleted file mode 100644 index d7bd9bcd7..000000000 --- a/wasm/src/tasks.rs +++ /dev/null @@ -1,53 +0,0 @@ -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{host_storage_get, host_storage_set}; - -use crate::types::{DatasetSelection, TaskDefinition}; - -const ACTIVE_DATASET_KEY: &[u8] = b"active_dataset"; -const DATASET_HISTORY_KEY: &[u8] = b"dataset_history"; -const MAX_DATASET_HISTORY: usize = 100; - -pub fn get_active_dataset() -> Option> { - let data = host_storage_get(ACTIVE_DATASET_KEY).ok()?; - if data.is_empty() { - return None; - } - bincode::deserialize(&data).ok() -} - -pub fn store_dataset(selection: &DatasetSelection) -> bool { - let data = match bincode::serialize(selection) { - Ok(d) => d, - Err(_) => return false, - }; - if host_storage_set(ACTIVE_DATASET_KEY, &data).is_err() { - return false; - } - let _ = append_dataset_history(selection); - true -} - -fn append_dataset_history(selection: &DatasetSelection) -> bool { - let mut history: Vec = host_storage_get(DATASET_HISTORY_KEY) - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default(); - - history.push(selection.clone()); - - if history.len() > MAX_DATASET_HISTORY { - history.drain(0..history.len() - MAX_DATASET_HISTORY); - } - - let data = match bincode::serialize(&history) { - Ok(d) => d, - Err(_) => return false, - }; - host_storage_set(DATASET_HISTORY_KEY, &data).is_ok() -} diff --git a/wasm/src/timeout_handler.rs b/wasm/src/timeout_handler.rs deleted file mode 100644 index b98e64458..000000000 --- a/wasm/src/timeout_handler.rs +++ /dev/null @@ -1,102 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use platform_challenge_sdk_wasm::host_functions::{ - host_get_timestamp, host_storage_get, host_storage_set, -}; - -use crate::types::TimeoutConfig; - -pub fn get_timeout_config() -> TimeoutConfig { - host_storage_get(b"timeout_config") - .ok() - .and_then(|d| { - if d.is_empty() { - None - } else { - bincode::deserialize(&d).ok() - } - }) - .unwrap_or_default() -} - -pub fn set_timeout_config(config: &TimeoutConfig) -> bool { - if let Ok(data) = bincode::serialize(config) { - return host_storage_set(b"timeout_config", &data).is_ok(); - } - false -} - -pub fn record_assignment(submission_id: &str, validator: &str, review_type: &str) -> bool { - let mut key = Vec::from(b"review_assignment:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - key.push(b':'); - key.extend_from_slice(review_type.as_bytes()); - key.push(b':'); - key.extend_from_slice(validator.as_bytes()); - - let timestamp = host_get_timestamp(); - host_storage_set(&key, ×tamp.to_le_bytes()).is_ok() -} - -pub fn check_timeout( - submission_id: &str, - validator: &str, - review_type: &str, - timeout_ms: u64, -) -> bool { - let mut key = Vec::from(b"review_assignment:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - key.push(b':'); - key.extend_from_slice(review_type.as_bytes()); - key.push(b':'); - key.extend_from_slice(validator.as_bytes()); - - if let Ok(data) = host_storage_get(&key) { - if data.len() >= 8 { - let mut buf = [0u8; 8]; - buf.copy_from_slice(&data[..8]); - let assigned_time = i64::from_le_bytes(buf); - let current_time = host_get_timestamp(); - let elapsed = (current_time - assigned_time) as u64; - return elapsed > timeout_ms; - } - } - false -} - -pub fn select_replacement( - validators: &[String], - excluded: &[String], - seed: &[u8], -) -> Option { - let available: Vec<&String> = validators - .iter() - .filter(|v| !excluded.iter().any(|e| e == *v)) - .collect(); - - if available.is_empty() { - return None; - } - - let idx = if seed.len() >= 4 { - let mut buf = [0u8; 4]; - buf.copy_from_slice(&seed[..4]); - u32::from_le_bytes(buf) as usize % available.len() - } else { - 0 - }; - - Some(available[idx].clone()) -} - -pub fn mark_timed_out(submission_id: &str, validator: &str, review_type: &str) -> bool { - let mut key = Vec::from(b"review_timeout:" as &[u8]); - key.extend_from_slice(submission_id.as_bytes()); - key.push(b':'); - key.extend_from_slice(review_type.as_bytes()); - key.push(b':'); - key.extend_from_slice(validator.as_bytes()); - - let timestamp = host_get_timestamp(); - host_storage_set(&key, ×tamp.to_le_bytes()).is_ok() -} diff --git a/wasm/src/types.rs b/wasm/src/types.rs deleted file mode 100644 index d12fd2711..000000000 --- a/wasm/src/types.rs +++ /dev/null @@ -1,331 +0,0 @@ -use alloc::string::String; -use alloc::vec::Vec; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum Difficulty { - Easy, - Medium, - Hard, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TaskDefinition { - pub id: String, - pub name: String, - pub repo: String, - pub base_commit: String, - pub difficulty: Difficulty, - pub timeout_secs: u64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TaskResult { - pub task_id: String, - pub passed: bool, - pub score: f64, - pub execution_time_ms: u64, - pub test_output: String, - pub agent_output: String, - pub error: Option, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ChallengeParams { - pub tasks: Vec, - pub llm_judge_url: Option, - pub decay_params: Option, - pub active_dataset: Option>, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Submission { - pub agent_hash: String, - pub miner_hotkey: String, - pub signature: Vec, - pub epoch: u64, - pub package_zip: Vec, - pub basilica_instance: String, - pub executor_url: String, - pub executor_token: String, - pub task_results: Vec, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct DifficultyStats { - pub total: u32, - pub passed: u32, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmJudgeRequest { - pub task_id: String, - pub instruction: String, - pub agent_output: String, - pub test_output: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmJudgeResponse { - pub score: f64, - pub reasoning: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct DecayParams { - pub grace_period_hours: u64, - pub half_life_hours: u64, - pub min_multiplier: f64, -} - -impl Default for DecayParams { - fn default() -> Self { - Self { - grace_period_hours: 72, - half_life_hours: 24, - min_multiplier: 0.0, - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct DatasetSelection { - pub tasks: Vec, - pub selected_at_epoch: u64, - pub dataset_hash: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct AgentLogEntry { - pub task_id: String, - pub passed: bool, - pub score: f64, - pub execution_time_ms: u64, - pub output_preview: String, - pub error: Option, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct AgentLogs { - pub miner_hotkey: String, - pub epoch: u64, - pub agent_hash: String, - pub entries: Vec, - pub total_size_bytes: u64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct RouteDefinition { - pub method: String, - pub path: String, - pub description: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct SubmissionName { - pub name: String, - pub owner_hotkey: String, - pub registered_epoch: u64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct SubmissionVersion { - pub version: u32, - pub agent_hash: String, - pub epoch: u64, - pub score: Option, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmReviewResult { - pub approved: bool, - pub reason: String, - pub violations: Vec, - pub reviewer_validators: Vec, - pub scores: Vec, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct AstReviewResult { - pub passed: bool, - pub violations: Vec, - pub reviewer_validators: Vec, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum EvaluationStatus { - Pending, - LlmReview, - AstReview, - Evaluating, - Completed, - Failed, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TopAgentState { - pub agent_hash: String, - pub score: f64, - pub achieved_epoch: u64, - pub epochs_stale: u64, - pub decay_active: bool, - pub current_burn_percent: f64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LeaderboardEntry { - pub rank: u32, - pub hotkey: String, - pub score: f64, - pub pass_rate: f64, - pub submissions: u32, - pub last_epoch: u64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct StatsResponse { - pub total_submissions: u64, - pub active_miners: u64, - pub validator_count: u64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TimeoutConfig { - pub evaluation_timeout_ms: u64, - pub llm_review_timeout_ms: u64, - pub ast_review_timeout_ms: u64, -} - -impl Default for TimeoutConfig { - fn default() -> Self { - Self { - evaluation_timeout_ms: 6 * 60 * 60 * 1000, - llm_review_timeout_ms: 3 * 60 * 1000, - ast_review_timeout_ms: 60 * 1000, - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct WhitelistConfig { - pub allowed_stdlib: Vec, - pub allowed_third_party: Vec, - pub forbidden_builtins: Vec, - pub max_code_size: usize, -} - -impl Default for WhitelistConfig { - fn default() -> Self { - use alloc::string::ToString; - Self { - allowed_stdlib: [ - "json", - "re", - "math", - "random", - "collections", - "itertools", - "functools", - "operator", - "string", - "textwrap", - "datetime", - "time", - "copy", - "pprint", - "typing", - "dataclasses", - "enum", - "abc", - "contextlib", - "warnings", - "bisect", - "heapq", - "array", - "types", - "decimal", - "fractions", - "statistics", - "hashlib", - "hmac", - "secrets", - "base64", - "binascii", - "struct", - "codecs", - "io", - "pathlib", - "argparse", - "logging", - "traceback", - "difflib", - "uuid", - "html", - "csv", - "os", - "sys", - "shutil", - "glob", - "subprocess", - ] - .iter() - .map(|s| s.to_string()) - .collect(), - allowed_third_party: [ - "term_sdk", - "numpy", - "pandas", - "scipy", - "sklearn", - "torch", - "tensorflow", - "transformers", - "openai", - "anthropic", - "httpx", - "aiohttp", - "requests", - "pydantic", - "rich", - "tqdm", - "litellm", - ] - .iter() - .map(|s| s.to_string()) - .collect(), - forbidden_builtins: ["exec", "eval", "compile", "__import__"] - .iter() - .map(|s| s.to_string()) - .collect(), - max_code_size: 1_048_576, - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmMessage { - pub role: String, - pub content: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmRequest { - pub model: String, - pub messages: Vec, - pub max_tokens: u32, - pub temperature: f64, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct LlmResponse { - pub content: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct WasmRouteRequest { - pub method: String, - pub path: String, - pub body: Vec, - #[serde(default)] - pub auth_hotkey: Option, -}