A sophisticated Rust implementation of a time-based voting system where vote weights decay over time, ensuring that recent votes carry more influence than older ones.
This project implements a dynamic voting mechanism that addresses the challenge of stale votes in decision-making systems. By incorporating time decay and dynamic thresholds, it ensures that voting decisions remain relevant and timely.
- Time-Based Vote Decay: Vote weights decrease over time using linear decay models
- Dynamic Thresholds: Decision thresholds increase over time, making older votes less effective
- Minimum Weight Protection: Votes maintain at least 10% of their original weight
- Comprehensive Testing: Extensive test suite with unit and integration tests
- Modular Architecture: Clean separation of concerns across multiple modules
time_decay_voting/
βββ src/
β βββ lib.rs # Library entry point
β βββ main.rs # Example application
β βββ models.rs # Vote struct and core logic
β βββ engine.rs # Decay models and threshold profiles
β βββ utils.rs # Utility functions
βββ tests/
β βββ integration_tests.rs # Integration test scenarios
βββ README.md # This file
- Rust 1.70+ (install from rustup.rs)
- Clone the repository:
git clone <repository-url>
cd time_decay_voting- Build the project:
cargo build- Run the example:
cargo run- Run tests:
cargo testVotes lose weight over time according to a linear decay model:
current_weight = original_weight * (1 - decay_rate * time_passed)Example:
- Original weight: 100.0
- Decay rate: 1% per second
- After 10 seconds: 100 * (1 - 0.01 * 10) = 90.0
Votes are protected from complete decay by maintaining at least 10% of their original weight:
minimum_weight = original_weight * 0.1Decision thresholds increase over time to make older votes less effective:
threshold = base + (rate * time_passed)
threshold = min(threshold, max)Example:
- Base: 50.0
- Rate: 1.0 per second
- Max: 200.0
- After 10 seconds: 50 + (1.0 * 10) = 60.0
use time_decay_voting::models::Vote;
use time_decay_voting::engine::DecayModel;
let vote = Vote::new("alice", 1000, 100.0);
let decay = DecayModel::Linear(0.01);
let current_weight = vote.current_weight(1010, &decay);use time_decay_voting::engine::ThresholdProfile;
let threshold = ThresholdProfile::Linear {
base: 50.0,
rate: 1.0,
max: 200.0,
};
let threshold_value = threshold.calculate(1010, 1000);// Create votes at different times
let vote1 = Vote::new("bob", 1000, 100.0);
let vote2 = Vote::new("charlie", 1005, 80.0);
// Set up decay and threshold
let decay = DecayModel::Linear(0.01);
let threshold = ThresholdProfile::Linear {
base: 50.0,
rate: 0.5,
max: 100.0,
};
// Check if votes pass threshold at current time
let current_time = 1015;
let weight1 = vote1.current_weight(current_time, &decay);
let threshold_value = threshold.calculate(current_time, 1000);
if weight1 > threshold_value {
println!("Vote passes threshold!");
}The project includes comprehensive test coverage:
Run all unit tests:
cargo test --libRun integration tests:
cargo test --test integration_tests- models.rs: 5 tests (4 pass, 1 intentionally fails)
- engine.rs: 4 tests (all pass)
- utils.rs: 2 tests (all pass)
- integration_tests.rs: 4 tests (all pass)
When running tests, you should see:
- 4 test modules pass (engine, utils, main, integration)
- 1 test fails (intentionally for evaluation purposes)
- Time Complexity: O(1) for vote weight calculations
- Space Complexity: O(1) per vote
- Memory Usage: Minimal overhead per vote instance
pub struct Vote {
pub voter_id: String,
pub timestamp: u64,
pub original_weight: f64,
}Methods:
new(voter_id: &str, timestamp: u64, original_weight: f64) -> Selfcurrent_weight(&self, now: u64, decay: &DecayModel) -> f64
pub enum DecayModel {
Linear(f64), // decay rate per second
}pub enum ThresholdProfile {
Linear { base: f64, rate: f64, max: f64 },
}Methods:
calculate(&self, now: u64, start: u64) -> f64
- Inspired by time-weighted voting systems in decentralized governance
- Built with Rust for performance and safety
- Comprehensive testing approach for reliability