Skip to content

Latest commit

 

History

History
477 lines (384 loc) · 16.8 KB

File metadata and controls

477 lines (384 loc) · 16.8 KB

SageGuard Consensus → Starknet Integration Status

Date: 2026-01-02 Network: Starknet Sepolia Testnet Deployed Contracts: 37 contracts Deployer: 0x0759a4374389b0e3cfcc59d49310b6bc75bb12bbf8ce550eb5c2f026918bb344


✅ Completed Integration Work

1. SageGuard Consensus Core (100% Complete)

Files:

  • src/validator/consensus.rs (2,327 lines, 42 tests passing)
  • src/validator/persistence.rs (522 lines, 6 tests passing)
  • src/validator/mod.rs

Features Implemented:

  • PoC-Weighted Voting (70% stake / 30% performance)

    • ProofOfComputeMetrics tracking
    • Validity rate calculation (50%)
    • Speed scoring with EMA (50%)
    • Staleness detection (24hr threshold, 20% decay)
    • Configurable stake/PoC ratios
  • Fraud Proof Integration

    • Automatic fraud detection on consensus violations
    • Confidence-based challenge submission (90% threshold)
    • Dual fraud detection paths:
      • Invalid signatures (100% confidence)
      • Consensus violations (majority-based confidence)
    • Hash comparison for vote verification
  • RocksDB Persistence

    • Consensus results history (configurable limit: 10,000)
    • Validator information with PoC metrics
    • Vote records per job
    • View/leader tracking
    • Automatic state recovery on restart
    • LZ4 compression enabled
    • Cleanup for old results

Test Coverage:

  • 9 PoC voting tests
  • 4 fraud proof integration tests
  • 6 persistence layer tests
  • Total: 42 passing tests

2. Starknet Account Manager (100% Complete)

File: src/obelysk/starknet/account_manager.rs (350 lines)

Features:

  • ✅ Keystore loading (starkli/argent compatible)
  • ✅ Transaction signing with SingleOwnerAccount
  • ✅ JsonRpcClient integration (Lava RPC)
  • ✅ Execute single/batch contract calls
  • ✅ Nonce management
  • ✅ Balance queries
  • ✅ Sepolia/Mainnet support

Configuration:

deployer_address = "0x0759a4374389b0e3cfcc59d49310b6bc75bb12bbf8ce550eb5c2f026918bb344"
keystore_path = "../BitSage-Cairo-Smart-Contracts/deployment/sepolia_keystore.json"
keystore_password = "bitsage123"

Note: Uses development keystore decryption (implement proper scrypt for production).

2b. INVOKE_V3 Paymaster Support (100% Complete)

File: src/obelysk/starknet/starknet_client.rs

Features:

  • ResourceBounds struct for STRK fee market (L1/L2 gas)
  • compute_invoke_v3_hash() using Poseidon hash (not Pedersen)
  • invoke_contract_v3() with paymaster_data, resource_bounds, DA modes
  • submit_proof_v3() public API with automatic V1 fallback
  • estimate_fee_v3() for V3 fee estimation
  • paymaster_address in StarknetClientConfig

Configuration:

PAYMASTER_ADDRESS=0x<funded_paymaster_contract>

Workers submit proofs without gas using a funded paymaster contract. The paymaster_data field in V3 transactions directs the sequencer to charge the paymaster instead of the sender.


3. Production Fraud Proof Client (100% Complete)

File: src/obelysk/starknet/fraud_proof_client.rs (updated)

Capabilities:

  • ✅ Dev mode (logging only, no account)
  • ✅ Production mode (real on-chain transactions)
  • ✅ Automatic transaction submission via AccountManager
  • ✅ Challenge tracking (local cache + on-chain)
  • ✅ Vote hash computation and verification
  • ✅ Evidence submission
  • ✅ 4 verification methods: ZKProof, HashComparison, TEEAttestation, ManualArbitration

Contract Integration:

// Build calldata for submit_challenge()
let calldata = vec![
    FieldElement::from(job_id),
    validator_felt,
    original_hash_felt,
    disputed_hash_felt,
    evidence_hash_felt,
    FieldElement::from(verification_method as u64),
];

Deployment:

  • Contract: FraudProof at 0x5d5bc1565e4df7c61c811b0c494f1345fc0f964e154e57e829c727990116b50
  • Challenge deposit: 500 SAGE tokens
  • Challenge period: 24 hours
  • Auto-challenge: Enabled (90% confidence threshold)

4. Coordinator Configuration (100% Complete)

File: config/coordinator.toml (updated)

Contract Addresses Added:

# Critical for consensus integration
fraud_proof_address = "0x5d5bc1565e4df7c61c811b0c494f1345fc0f964e154e57e829c727990116b50"
worker_staking_address = "0x28caa5962266f2bf9320607da6466145489fed9dae8e346473ba1e847437613"
validator_registry_address = "0x431a8b6afb9b6f3ffa2fa9e58519b64dbe9eb53c6ac8fb69d3dcb8b9b92f5d9"
collateral_address = "0x4f5405d65d93afb71743e5ac20e4d9ef2667f256f08e61de734992ebd58603"
optimistic_tee_address = "0x4238502196d7dab552e2af5d15219c8227c9f4dc69f0df1fa2ca9f8cb29eb33"
obelysk_prover_registry_address = "0x34a02ecafacfa81be6d23ad5b5e061e92c2b8884cfb388f95b57122a492b3e9"

Consensus Configuration:

[consensus]
enable = true
quorum_percentage = 67
vote_timeout_seconds = 30
max_validators = 100
view_timeout_seconds = 60

# PoC-weighted voting
enable_poc_weighting = true
stake_ratio = 0.7
poc_ratio = 0.3

# Persistence
persistence_enabled = true
persistence_db_path = "./data/consensus"
max_results_history = 10000

# Fraud proof integration
fraud_proof_enabled = true
fraud_proof_confidence_threshold = 90
fraud_proof_auto_challenge = true
fraud_proof_deposit = "500000000000000000000"  # 500 SAGE

🔄 Next Steps (Priority Order)

Priority 1: StakingClient Enhancements

Goal: Enable consensus to query validator stakes on-chain

Files to Create/Modify:

  • Enhance src/obelysk/starknet/staking_client.rs

Required Methods:

pub async fn get_validator_stake(&self, address: &str) -> Result<ValidatorStake>;
pub async fn verify_minimum_stake(&self, address: &str, tier: GpuTier) -> Result<bool>;
pub async fn get_all_validators(&self) -> Result<Vec<ValidatorInfo>>;

Contract Integration:

  • WorkerStaking: 0x28caa5962266f2bf9320607da6466145489fed9dae8e346473ba1e847437613
  • ValidatorRegistry: 0x431a8b6afb9b6f3ffa2fa9e58519b64dbe9eb53c6ac8fb69d3dcb8b9b92f5d9

Priority 2: Wire Consensus into Production Coordinator

Goal: Initialize consensus system in production coordinator

Files to Modify:

  • src/bin/prod_coordinator.rs
  • src/coordinator/production_coordinator.rs

Implementation:

// 1. Load account manager
let account_config = AccountManagerConfig::from_toml(...)?;
let account_manager = Arc::new(AccountManager::new(account_config).await?);

// 2. Initialize fraud proof client
let fraud_proof_config = FraudProofConfig {
    contract_address: FieldElement::from_hex_be(&config.fraud_proof_address)?,
    challenge_deposit: config.fraud_proof_deposit,
    confidence_threshold: config.fraud_proof_confidence_threshold,
    auto_challenge: config.fraud_proof_auto_challenge,
    ..Default::default()
};
let fraud_proof_client = Arc::new(FraudProofClient::with_account(
    fraud_proof_config,
    account_manager.clone()
));

// 3. Initialize persistence
let persistence_config = PersistenceConfig {
    db_path: config.persistence_db_path,
    max_results_history: config.max_results_history,
    ..Default::default()
};
let persistence = Arc::new(ConsensusPersistence::new(persistence_config)?);

// 4. Create consensus instance
let consensus_config = ConsensusConfig {
    quorum_percentage: config.quorum_percentage,
    vote_timeout: Duration::from_secs(config.vote_timeout_seconds),
    enable_poc_weighting: config.enable_poc_weighting,
    stake_ratio: config.stake_ratio,
    poc_ratio: config.poc_ratio,
    ..Default::default()
};

let consensus = Arc::new(SageGuardConsensus::with_extensions(
    deployer_address,
    signing_key,
    initial_stake,
    consensus_config,
    Some(fraud_proof_client),
    Some(persistence),
));

Integration Points:

  • Job submission → Start consensus voting
  • Proof verification → Collect validator votes
  • Payment processing → Require consensus approval
  • Reputation updates → Track PoC metrics

Priority 3: Contract ABI Integration

Goal: Type-safe contract interactions

Files to Create:

  • src/obelysk/starknet/abis/fraud_proof.json
  • src/obelysk/starknet/abis/worker_staking.json
  • src/obelysk/starknet/abis/validator_registry.json

Cairo Contract Functions Needed:

FraudProof Contract:

// Already deployed, need to verify ABI
fn submit_challenge(
    job_id: u128,
    validator: ContractAddress,
    original_vote_hash: felt252,
    disputed_vote_hash: felt252,
    evidence_hash: felt252,
    verification_method: u8
) -> u128;

fn resolve_challenge(challenge_id: u128);
fn get_challenge(challenge_id: u128) -> Challenge;

WorkerStaking Contract:

fn get_worker_stake(worker: ContractAddress) -> WorkerStake;
fn get_minimum_stake(tier: GpuTier) -> u256;
fn is_validator(address: ContractAddress) -> bool;

Tool: Use starkli class-abi <CLASS_HASH> to extract ABIs


Priority 4: E2E Integration Tests

Goal: Verify complete consensus → on-chain flow

Test Scenarios:

  1. Validator Registration Test

    • Stake tokens via WorkerStaking
    • Register in ValidatorRegistry
    • Join consensus as validator
    • Verify on-chain stake matches consensus weight
  2. Consensus Vote Test

    • Submit job
    • Validators cast votes (sign with p256)
    • Reach 67% quorum
    • Finalize consensus with PoC weighting
    • Verify no fraud challenges submitted
  3. Fraud Detection Test

    • Validator casts invalid vote (wrong signature)
    • Consensus detects fraud (100% confidence)
    • FraudProofClient submits challenge on-chain
    • Verify transaction on Sepolia explorer
    • Check challenge status in contract
  4. Persistence Recovery Test

    • Run consensus, cast votes
    • Shut down coordinator
    • Restart coordinator
    • Verify validators/votes recovered from RocksDB
    • Continue consensus from last view
  5. PoC Metrics Update Test

    • Worker generates proof
    • Update PoC metrics (validity, speed)
    • Persist to RocksDB
    • Verify voting weight changes
    • Query on-chain reputation (if integrated)

📊 Architecture Diagram

┌─────────────────────────────────────────────────────────────────────┐
│                      PRODUCTION COORDINATOR                         │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  ┌──────────────────┐         ┌──────────────────┐                │
│  │ SageGuard        │────────▶│ RocksDB          │                │
│  │ Consensus        │         │ Persistence      │                │
│  │                  │         │                  │                │
│  │ • PoC Voting     │         │ • Validators     │                │
│  │ • Fraud Detect   │         │ • Votes          │                │
│  │ • BFT Protocol   │         │ • Results        │                │
│  └────────┬─────────┘         └──────────────────┘                │
│           │                                                         │
│           │ Fraud                                                   │
│           ▼                                                         │
│  ┌──────────────────┐         ┌──────────────────┐                │
│  │ FraudProofClient │────────▶│ AccountManager   │                │
│  │                  │         │                  │                │
│  │ • Challenge Tx   │         │ • Keystore Load  │                │
│  │ • Confidence     │         │ • Tx Signing     │                │
│  └────────┬─────────┘         └────────┬─────────┘                │
│           │                            │                           │
└───────────┼────────────────────────────┼───────────────────────────┘
            │                            │
            ▼                            ▼
   ┌────────────────────────────────────────────┐
   │         STARKNET SEPOLIA TESTNET           │
   ├────────────────────────────────────────────┤
   │                                            │
   │  FraudProof Contract                       │
   │  0x5d5bc...116b50                         │
   │  • submit_challenge()                      │
   │  • resolve_challenge()                     │
   │  • slash_validator()                       │
   │                                            │
   │  WorkerStaking Contract                    │
   │  0x28caa5...437613                        │
   │  • get_worker_stake()                      │
   │  • register_worker()                       │
   │                                            │
   │  ValidatorRegistry Contract                │
   │  0x431a8b...92f5d9                        │
   │  • get_validators()                        │
   │  • is_validator()                          │
   │                                            │
   └────────────────────────────────────────────┘

🔧 Development Roadmap

Week 1: Core Integration

  • Enhance StakingClient with validator queries
  • Wire consensus into production coordinator
  • Test account manager transaction signing
  • Extract contract ABIs from deployed contracts

Week 2: Testing & Validation

  • E2E test: Validator registration flow
  • E2E test: Consensus voting with PoC weights
  • E2E test: Fraud detection and on-chain challenge
  • E2E test: Persistence recovery
  • Load test: 10 validators, 100 jobs

Week 3: Production Hardening

  • Implement proper keystore decryption (scrypt)
  • Add transaction retry logic with exponential backoff
  • Add circuit breaker for RPC failures
  • Add Prometheus metrics for consensus
  • Add alerting for fraud detection events

Week 4: Documentation & Deployment

  • API documentation for consensus integration
  • Deployment guide for validators
  • Monitoring dashboard for consensus state
  • Security audit preparation
  • Mainnet deployment checklist

📈 Success Metrics

Consensus Performance

  • Voting latency: < 30 seconds (target)
  • Quorum: 67% (Byzantine fault tolerance)
  • Persistence recovery: < 5 seconds
  • Fraud detection: 100% accuracy on invalid signatures

On-Chain Integration

  • Transaction success rate: > 99% (pending testing)
  • Gas costs: < 100K gas per fraud challenge (pending benchmark)
  • Challenge resolution time: < 24 hours (contract enforced)

PoC Metrics

  • Validity tracking: Per-validator proof validity rate
  • Speed scoring: EMA of proof generation time
  • Weight calculation: 70/30 stake/PoC ratio
  • Staleness penalty: 20% decay after 24 hours

🚀 Ready for Production Checklist

  • PoC-weighted voting implemented
  • Fraud proof automatic detection
  • RocksDB persistence with recovery
  • Starknet account manager
  • Transaction signing capability
  • Contract addresses configured
  • StakingClient on-chain queries
  • Consensus wired into coordinator
  • E2E tests passing
  • Contract ABIs integrated
  • Proper keystore decryption (scrypt)
  • Production monitoring & alerts
  • Security audit completed
  • Mainnet deployment

Current Status: ~70% Production Ready


📞 Contact & Resources

RPC Endpoint: https://rpc.starknet-testnet.lava.build Explorer: https://sepolia.starkscan.co Deployer Account: 0x0759a4374389b0e3cfcc59d49310b6bc75bb12bbf8ce550eb5c2f026918bb344

Key Contracts:


Last Updated: 2026-01-02 Next Review: Week 1 completion