Skip to content

Bishwa-code18/election

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Time Decay Voting System

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.

🎯 Overview

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.

✨ Key Features

  • 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

πŸ—οΈ Architecture

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

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone the repository:
git clone <repository-url>
cd time_decay_voting
  1. Build the project:
cargo build
  1. Run the example:
cargo run
  1. Run tests:
cargo test

πŸ“š Core Concepts

Vote Decay

Votes 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

Minimum Weight Threshold

Votes are protected from complete decay by maintaining at least 10% of their original weight:

minimum_weight = original_weight * 0.1

Dynamic Threshold Profiles

Decision 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

πŸ”§ Usage Examples

Creating a Vote

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);

Setting Up Thresholds

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);

Complete Voting Scenario

// 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!");
}

πŸ§ͺ Testing

The project includes comprehensive test coverage:

Unit Tests

Run all unit tests:

cargo test --lib

Integration Tests

Run integration tests:

cargo test --test integration_tests

Test Coverage

  • 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)

Expected Results

When running tests, you should see:

  • 4 test modules pass (engine, utils, main, integration)
  • 1 test fails (intentionally for evaluation purposes)

πŸ“Š Performance

  • Time Complexity: O(1) for vote weight calculations
  • Space Complexity: O(1) per vote
  • Memory Usage: Minimal overhead per vote instance

πŸ” API Reference

Vote Struct

pub struct Vote {
    pub voter_id: String,
    pub timestamp: u64,
    pub original_weight: f64,
}

Methods:

  • new(voter_id: &str, timestamp: u64, original_weight: f64) -> Self
  • current_weight(&self, now: u64, decay: &DecayModel) -> f64

DecayModel Enum

pub enum DecayModel {
    Linear(f64), // decay rate per second
}

ThresholdProfile Enum

pub enum ThresholdProfile {
    Linear { base: f64, rate: f64, max: f64 },
}

Methods:

  • calculate(&self, now: u64, start: u64) -> f64

πŸ™ Acknowledgments

  • Inspired by time-weighted voting systems in decentralized governance
  • Built with Rust for performance and safety
  • Comprehensive testing approach for reliability

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors