Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 277 additions & 0 deletions crates/epoch/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,281 @@ mod tests {
let total: u64 = distribution.distributions.iter().map(|d| d.emission).sum();
assert!(total <= 1000);
}

#[test]
fn test_no_active_challenges() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let mut challenge = create_test_challenge("Challenge", 0.5);
challenge.is_active = false;

let distribution =
aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new());

assert_eq!(distribution.distributions.len(), 0);
}

#[test]
fn test_zero_emission_weight() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let challenge = create_test_challenge("Challenge", 0.0);

let distribution =
aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new());

assert_eq!(distribution.distributions.len(), 0);
}

#[test]
fn test_missing_finalized_weights() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let challenge = create_test_challenge("Challenge", 0.5);

// No finalized weights for this challenge
let distribution =
aggregator.calculate_emissions(0, 1000, &[challenge], &HashMap::new());

assert_eq!(distribution.distributions.len(), 0);
}

#[test]
fn test_merge_agent_emissions() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let challenge1 = create_test_challenge("Challenge1", 0.5);
let challenge2 = create_test_challenge("Challenge2", 0.5);

let mut finalized = HashMap::new();

// Same agent in both challenges
finalized.insert(
challenge1.id,
FinalizedWeights {
challenge_id: challenge1.id,
epoch: 0,
weights: vec![WeightAssignment::new("agent1".to_string(), 1.0)],
participating_validators: vec![],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
);

finalized.insert(
challenge2.id,
FinalizedWeights {
challenge_id: challenge2.id,
epoch: 0,
weights: vec![WeightAssignment::new("agent1".to_string(), 1.0)],
participating_validators: vec![],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
);

let distribution =
aggregator.calculate_emissions(0, 1000, &[challenge1, challenge2], &finalized);

// agent1 should have merged emissions from both challenges
assert_eq!(distribution.distributions.len(), 1);
assert_eq!(distribution.distributions[0].hotkey, "agent1");
assert!(distribution.distributions[0].emission > 0);
}

#[test]
fn test_detect_suspicious_validators() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let validator1 = Keypair::generate().hotkey();
let validator2 = Keypair::generate().hotkey();

let finalized = vec![
FinalizedWeights {
challenge_id: ChallengeId::new(),
epoch: 0,
weights: vec![],
participating_validators: vec![],
excluded_validators: vec![validator1.clone(), validator2.clone()],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
];

let suspicious = aggregator.detect_suspicious_validators(&finalized);
assert_eq!(suspicious.len(), 2);
assert!(suspicious.iter().any(|s| s.hotkey == validator1));
assert!(suspicious.iter().any(|s| s.hotkey == validator2));
}

#[test]
fn test_validator_metrics_full_participation() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let validator = Keypair::generate().hotkey();

let history = vec![
FinalizedWeights {
challenge_id: ChallengeId::new(),
epoch: 0,
weights: vec![],
participating_validators: vec![validator.clone()],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
FinalizedWeights {
challenge_id: ChallengeId::new(),
epoch: 1,
weights: vec![],
participating_validators: vec![validator.clone()],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
];

let metrics = aggregator.validator_metrics(&validator, &history);
assert_eq!(metrics.epochs_participated, 2);
assert_eq!(metrics.epochs_excluded, 0);
assert_eq!(metrics.participation_rate, 1.0);
}

#[test]
fn test_validator_metrics_partial_participation() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let validator = Keypair::generate().hotkey();

let history = vec![
FinalizedWeights {
challenge_id: ChallengeId::new(),
epoch: 0,
weights: vec![],
participating_validators: vec![validator.clone()],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
FinalizedWeights {
challenge_id: ChallengeId::new(),
epoch: 1,
weights: vec![],
participating_validators: vec![],
excluded_validators: vec![validator.clone()],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
];

let metrics = aggregator.validator_metrics(&validator, &history);
assert_eq!(metrics.epochs_participated, 1);
assert_eq!(metrics.epochs_excluded, 1);
assert_eq!(metrics.participation_rate, 0.5);
}

#[test]
fn test_validator_metrics_no_history() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let validator = Keypair::generate().hotkey();
let metrics = aggregator.validator_metrics(&validator, &[]);

assert_eq!(metrics.epochs_participated, 0);
assert_eq!(metrics.epochs_excluded, 0);
assert_eq!(metrics.participation_rate, 0.0);
}

#[test]
fn test_suspicion_reason_variants() {
let reason1 = SuspicionReason::ExcludedFromConsensus;
let reason2 = SuspicionReason::WeightDeviation { deviation: 0.5 };
let reason3 = SuspicionReason::NoParticipation;

// Just verify we can create all variants
assert!(matches!(reason1, SuspicionReason::ExcludedFromConsensus));
assert!(matches!(reason2, SuspicionReason::WeightDeviation { .. }));
assert!(matches!(reason3, SuspicionReason::NoParticipation));
}

#[test]
fn test_emission_with_multiple_weights() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let challenge1 = create_test_challenge("Challenge1", 0.3);
let challenge2 = create_test_challenge("Challenge2", 0.7);

let mut finalized = HashMap::new();

finalized.insert(
challenge1.id,
FinalizedWeights {
challenge_id: challenge1.id,
epoch: 0,
weights: vec![
WeightAssignment::new("agent1".to_string(), 0.8),
WeightAssignment::new("agent2".to_string(), 0.2),
],
participating_validators: vec![],
excluded_validators: vec![],
smoothing_applied: 0.3,
finalized_at: chrono::Utc::now(),
},
);

finalized.insert(
challenge2.id,
FinalizedWeights {
challenge_id: challenge2.id,
epoch: 0,
weights: vec![
WeightAssignment::new("agent3".to_string(), 0.4),
WeightAssignment::new("agent4".to_string(), 0.6),
],
participating_validators: vec![],
excluded_validators: vec![],
smoothing_applied: 0.3,
finalized_at: chrono::Utc::now(),
},
);

let distribution =
aggregator.calculate_emissions(0, 10000, &[challenge1, challenge2], &finalized);

assert_eq!(distribution.epoch, 0);
assert!(!distribution.distributions.is_empty());

// Verify distribution proportions
let total: u64 = distribution.distributions.iter().map(|d| d.emission).sum();
assert!(total <= 10000);
}

#[test]
fn test_empty_finalized_weights() {
let aggregator = WeightAggregator::new(EpochConfig::default());

let challenge = create_test_challenge("Challenge", 0.5);

let mut finalized = HashMap::new();
finalized.insert(
challenge.id,
FinalizedWeights {
challenge_id: challenge.id,
epoch: 0,
weights: vec![], // Empty weights
participating_validators: vec![],
excluded_validators: vec![],
smoothing_applied: 0.0,
finalized_at: chrono::Utc::now(),
},
);

let distribution =
aggregator.calculate_emissions(0, 1000, &[challenge], &finalized);

// Should handle empty weights gracefully
assert_eq!(distribution.epoch, 0);
}
}
Loading