This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Rust-based threshold cryptography system for Bitcoin operations using FROST (Flexible Round-Optimized Schnorr Threshold signatures). The system implements a distributed key generation protocol with P2P networking for secure multi-party Bitcoin transactions.
This project requires Rust 1.87:
rustup default 1.87# Build all crates
cargo build --all
# Run all tests
cargo test --all
# Run tests for specific crate
cargo test -p abci
cargo test -p node
# Run single test
cargo test test_name
# Format check (required for CI)
cargo fmt --all -- --check
# Linting (CI fails on warnings)
cargo clippy -- -D warnings
# Build specific binary
cargo build --bin cli
# Code coverage analysis
cargo tarpaulin --out html# Generate 5-node testnet configuration
KEY_PASSWORD=supersecret ./bootstrap.sh
# Setup individual node
cargo run --bin cli setup
# Start node
cargo run --bin cli run# Run 5-node testnet cluster
docker-compose up -d --build
# View logs from all nodes
docker-compose logs
# Stop the cluster
docker-compose down
# Build container
docker build -t vault-node .
# Multi-node DKG testing workflow
# 1. Start n nodes using setup script (max 13 nodes for DKG to work)
./setup_nodes.sh <n>
# 2. Run DKG integration test (note: port range is 50057 to 50057+n-1)
cargo run --bin integration-tests check-dkg --port-range 50057-50063 # for 7 nodes
cargo run --bin integration-tests check-dkg --port-range 50057-50069 # for 13 nodes (max working)
# 3. Clean up containers
docker ps -q | xargs docker stop
# DKG Scalability Limits:
# ✅ Works: 7-13 nodes
# ❌ Shaky: 14+ nodes (DKG process doesn't complete)
# Note: The limit appears to be around 13 nodes but should be 256# End-to-end deposit test (requires docker cluster)
cargo run --bin integration-tests -- end-to-end-test 5000
# Deposit test only
cargo run --bin integration-tests -- deposit-test 1000
# Withdrawal test only
cargo run --bin integration-tests -- withdrawal-test 1000
# Consensus test with multiple deposits
cargo run --bin integration-tests -- consensus-test --amount 1000 --num-deposits 3
# Single node endpoint test
cargo run --bin integration-tests -- deposit-test 1000 --endpoint http://127.0.0.1:50051cd website/
yarn dev # Development server
yarn build # Production build
yarn format # Prettier formattingcrates/node/- Main node with P2P networking, gRPC server, and handlerscrates/abci/- Application Blockchain Interface for chain state managementcrates/protocol/- Core protocol logic and blockchain definitionscrates/types/- Shared type definitions and error handlingcrates/oracle/- Bitcoin blockchain oracle (Esplora client + mock)
- FROST Protocol: Threshold signature implementation using
frost-secp256k1 - P2P Networking: libp2p with Gossipsub, mDNS discovery, TCP/QUIC transports
- gRPC API: Tonic-based server for deposit/withdrawal operations, includes dev endpoints for chain inspection
- Bitcoin Integration: Taproot wallet with Esplora API client
- ABCI Layer: Manages chain state, database operations, and transaction execution
The ABCI crate implements a clean separation of concerns:
ChainInterface: Main trait for blockchain operations (deposit intents, transaction execution, account management)ChainState: In-memory state management with serialization supportTransactionExecutor: Stack-based virtual machine for executing protocol transactionsDbtrait: Database abstraction with RocksDB implementation
tests/- Separate crate with integration tests using mock implementationscrates/abci/src/tests/- Organized unit tests by module:chain_state.rs- ChainState and Account testsexecutor.rs- TransactionExecutor and VM testsdb.rs- Database operation testslib_tests.rs- ChainInterface integration tests
- MockNodeCluster: Multi-node test harness with in-memory databases
- MockOracle: Simulated blockchain for testing
Code is generic over trait types (Network, Db, Oracle, ChainInterface) to enable mocking and testing. Always maintain this abstraction when adding new functionality.
The system uses an actor-like pattern where operations are handled by specific handlers:
- DepositState: Manages deposit intents and address derivation
- WithdrowlState: Handles two-phase withdrawals (challenge + confirm)
- BalanceState: Tracks user account balances via gRPC CheckBalance RPC, also handles dev endpoints (GetChainInfo, GetLatestBlocks)
- DKGState: Manages distributed key generation
- ConsensusState: Handles FROST consensus protocol, includes TriggerConsensusRound dev endpoint
The ABCI transaction executor uses a stack-based virtual machine:
- Operations:
OpPush,OpCheckOracle,OpIncrementBalance,OpDecrementBalance - Stack Management: LIFO operations with type-safe data handling
- Allowance System: Oracle validation creates allowances for balance increments
- Error Handling: Errors push zero to stack and stop execution
Functions like update_user_balance track processed transaction IDs to prevent double-processing. Maintain this pattern for all state-modifying operations.
Each test creates its own isolated state. Tests are structured as:
- Unit tests: Test individual components in isolation (
crates/*/src/tests/) - Integration tests: Test component interactions (
tests/src/) - End-to-end tests: Full system workflows via gRPC
KEY_PASSWORD: Required for encrypted key operations and bootstrap scriptIS_TESTNET: Toggle between development/production modesMNEMONIC: Test wallet seed phrases for development
The GitHub Actions pipeline enforces:
- Code formatting (
cargo fmt --all -- --check) - Build success (
cargo build --all) - Test passing (
cargo test --all) - Clippy linting with zero warnings (
cargo clippy -- -D warnings)
crates/node/src/lib.rs- DefinesNodeState<N,D,O>generics and registers handlerscrates/abci/src/lib.rs- ChainInterface trait and implementationcrates/abci/src/executor.rs- Transaction execution virtual machinecrates/abci/src/chain_state.rs- In-memory state management
crates/node/src/utils/swarm_manager.rs- P2P message definitions and gossipsub logiccrates/abci/src/db/rocksdb.rs- Persistent database implementationtests/src/mocks/- Mock implementations for testing
tests/src/mocks/network.rs- MockNodeCluster for multi-node teststests/src/mocks/abci.rs- Mock ChainInterface for testingcrates/abci/src/tests/- Organized unit test modulestests/src/consensus/- Block consensus unit teststests/src/bin/integration-tests/- End-to-end integration test binary
- Implement core logic in appropriate crate (usually
crates/node/orcrates/abci/) - Add trait abstractions for external dependencies to maintain generic design
- Create mock implementations in
tests/src/mocks/for testing - Add gRPC endpoints in protobuf definitions if needed
- Write comprehensive tests:
- Unit tests in the same crate (
src/tests/) - Integration tests in
tests/src/ - Use
MockNodeClusterfor multi-node scenarios
- Unit tests in the same crate (
- Ensure idempotent behavior for state modifications
- Run code coverage with
cargo tarpaulinto verify test completeness
When working with the ABCI crate:
- Field visibility: Some struct fields are
pub(crate)for testing access - Error handling: Always use
NodeErrorfor consistent error propagation - State persistence: Changes to
ChainStatemust be flushed to database - Transaction safety: Use allowance system for balance increments via oracle validation
- Test organization: Unit tests are organized by module in
src/tests/directory
- you don't need comments for things you remove or to clarify things. keep comments to a minimum, and don't use them whenever possible
- ensure that you always try to modualarize and try to be DRY as possible. dont modualarize unecessarily but ensure that you can reuse code when possible
The system includes gRPC development endpoints for testing and debugging:
- GetChainInfo: Returns chain state information (latest height, pending transactions, etc.)
- TriggerConsensusRound: Manually trigger a consensus round (useful for testing)
- GetLatestBlocks: Retrieve recent block information
These endpoints are implemented in existing handlers:
GetChainInfoandGetLatestBlocksinBalanceStatehandlerTriggerConsensusRoundinConsensusStatehandler
The integration test binary (tests/src/bin/integration-tests/main.rs) provides comprehensive testing:
- Deposit Test: Creates deposit intents and verifies balance updates
- Withdrawal Test: Tests two-phase withdrawal (propose + confirm)
- End-to-End Test: Combines deposit and withdrawal flows
- Consensus Test: Multi-node consensus verification with multiple deposits
Tests work with both:
- Mock Oracle: For unit/integration testing (instant processing)
- Real Testnet: For end-to-end validation (requires actual Bitcoin transactions)
The consensus test specifically verifies:
- State synchronization across all nodes
- Deposit intent propagation via gossipsub
- Block creation and finalization
- Chain state consistency in RocksDB
The system supports multi-node testing via Docker Compose:
- 5-node cluster configuration in
docker-compose.yml - Automated key generation via
bootstrap.sh - gRPC endpoints exposed on ports 50051-50055
- Logs accessible via
docker-compose logs
Integration tests can target specific nodes or test across the entire cluster for consensus verification.
-
Dev gRPC Endpoints - ✅ WORKING
GetChainInfo: Returns chain height, pending transactions, total blocksTriggerConsensusRound: Manually triggers consensus roundsGetLatestBlocks: Retrieves recent block information- All endpoints properly implemented in existing handlers
-
Enhanced Integration Tests - ✅ WORKING
- Restructured CLI with main
testcommand and subcommands - Comprehensive consensus test using all 5 nodes
- Dev endpoint integration for chain state verification
- Multi-phase testing (state sync, block creation, execution)
- Restructured CLI with main
-
Consensus Improvements - ✅ WORKING
- Fixed block validation logic (compare transactions, not entire blocks)
- Fixed 2/3+ threshold calculation for voting
- Consensus rounds triggering correctly
- Prevotes and precommits working
-
Mock Oracle Transaction Processing - ❌ NEEDS FIX
- Mock oracle creates dummy transactions but they don't update balances
- Consensus can process blocks but transaction execution not completing
- Even basic deposit tests failing due to balance not updating
-
Consensus Finalization -
⚠️ PARTIAL- Getting 3/5 precommits but need 4/5 for finalization
- Block proposals working, voting working, but not reaching final threshold
- Fix mock oracle transaction processing to properly update balances
- Investigate why not all 5 nodes are voting in consensus
- Ensure finalized blocks actually execute transactions and update chain state
- when you make changes you have to docker compose down and then docker compose up -d --build to have the latest version of the code running in the test node network