Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation#32
Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation#32glowsenior wants to merge 1 commit intoPlatformNetwork:mainfrom
Conversation
…ection Replace simple arithmetic mean with stake-weighted consensus score calculation as documented in README. This fix addresses a critical security and correctness issue where all validators were treated equally regardless of stake. Changes: - Add calculate_stake_weighted_consensus_score() function - Implement stake-weighted average: sum(stake * score) / sum(stake) - Add outlier detection using z-score threshold (2.0) - Look up validator stakes from database for each evaluation - Handle edge cases (missing validators, zero stake, empty evaluations) Security Impact: - Restores Sybil resistance by weighting validators by stake - Prevents manipulation through outlier detection - High-stake validators now have appropriate influence Fixes: Missing stake-weighted aggregation in consensus score calculation Related: README.md lines 254-270 (Score Aggregation section) Before: Simple mean - scores.iter().sum() / scores.len() After: Stake-weighted with outlier filtering
📝 WalkthroughWalkthroughA new private function Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…ons, add log/timestamp support (#32) Fix critical ABI mismatches in network host functions and implement missing storage host function registrations to align the host-side runtime with the guest SDK expectations. Network ABI fixes (network.rs): - Remove extra resp_len param from http_get and dns_resolve signatures to match the guest SDK 4-param ABI (req_ptr, req_len, resp_ptr) with default buffer sizes (64KB for HTTP, 4KB for DNS). - Add extra _extra param to http_post to align with guest expectations. - Add log_message host function: reads message string from WASM memory and dispatches to tracing::info/warn/error based on level parameter. - Add get_timestamp host function: returns UTC millis, with support for a fixed_timestamp_ms in RuntimeState for deterministic consensus. Storage host functions (storage.rs): - Implement StorageHostFunctions struct with HostFunctionRegistrar trait, registering storage_get and storage_set under platform_storage module. - storage_get reads key from WASM memory, fetches from backend, writes value back to WASM memory, tracks bytes_read and operations_count. - storage_set reads key+value from WASM memory, validates against config constraints (key/value limits, consensus gating), writes via backend, tracks bytes_written and operations_count. - Add helper functions for WASM memory read/write with bounds checking. - Add StorageOperation::Set variant and HOST_STORAGE_SET constant. - Update StorageHostState::new to accept a StorageBackend Arc parameter. Runtime integration (runtime.rs): - Extend InstanceConfig and RuntimeState with storage_state, storage_backend, storage_host_config, and fixed_timestamp_ms fields. - Register both NetworkHostFunctions and StorageHostFunctions with the linker during instance creation. - Add storage state reset and counter accessors to ChallengeInstance. Module exports (lib.rs): - Export StorageHostFunctions, HOST_STORAGE_SET, HOST_LOG_MESSAGE, and HOST_GET_TIMESTAMP from the crate root. Downstream update (wasm_executor.rs): - Use ..Default::default() in InstanceConfig construction to pick up new fields without breaking existing call sites.
Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation
Summary
The
update_leaderboardfunction incrates/platform-server/src/db/queries.rscalculates consensus scores using a simple arithmetic mean instead of the documented stake-weighted average. This undermines the security model and doesn't match the documented behavior.Severity
High - Security and correctness issue
Location
crates/platform-server/src/db/queries.rsDescription
The consensus score calculation was using a simple arithmetic mean:
However, the README and documentation specify that scores should be aggregated using stake-weighted averaging with outlier detection:
Where:
Impact
Expected Behavior
According to
README.md(lines 254-270):Actual Behavior
The code was calculating a simple arithmetic mean without:
Root Cause
Evaluationstruct doesn't include validator stake informationSolution
Implemented
calculate_stake_weighted_consensus_score()function that:sum(stake * score) / sum(stake)Code Changes
Before
After
Testing Recommendations
Related Documentation
README.mdlines 152-180 (Validator operations)README.mdlines 252-279 (Score Aggregation)AGENTS.mdline 87 (Stake-weighted averaging)Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.