Skip to content
Open
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
60 changes: 60 additions & 0 deletions contracts/predictify-hybrid/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,15 @@ pub struct DisputeTimeoutOutcome {
pub reason: String,
}

/// Configuration for dispute collusion detection.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CollusionDetectorConfig {
pub stake_delta_threshold: i128,
pub time_delta_threshold: u64,
pub window_size: u32,
}

/// Aggregate statistics about dispute timeouts across all markets.
///
/// Returned by timeout analytics queries; useful for governance dashboards
Expand Down Expand Up @@ -786,6 +795,27 @@ impl DisputeManager {
env.storage().persistent().get(&key)
}

/// Sets the collusion detector configuration.
pub fn set_collusion_detector_config(env: &Env, admin: Address, config: CollusionDetectorConfig) -> Result<(), Error> {
admin.require_auth();
DisputeValidator::validate_admin_permissions(env, &admin)?;

let key = DataKey::CollusionDetectorConfig;
env.storage().persistent().set(&key, &config);
env.storage().persistent().extend_ttl(&key, 535680, 535680);
Ok(())
}

/// Retrieves the collusion detector configuration.
pub fn get_collusion_detector_config(env: &Env) -> CollusionDetectorConfig {
let key = DataKey::CollusionDetectorConfig;
env.storage().persistent().get(&key).unwrap_or(CollusionDetectorConfig {
stake_delta_threshold: 1_000_000,
time_delta_threshold: 600, // 10 minutes
window_size: 8,
})
}

/// Evicts the oldest resolved/expired disputes if history size exceeds the cap.
pub fn apply_eviction(
env: &Env,
Expand Down Expand Up @@ -990,6 +1020,36 @@ impl DisputeManager {
None,
);

// --- Collusion Detector ---
let config = Self::get_collusion_detector_config(env);
let window_size = config.window_size;
let start_idx = if history.len() > window_size {
history.len() - window_size
} else {
0
};

for i in start_idx..history.len().saturating_sub(1) {
if let Some(prev_dispute) = history.get(i) {
if prev_dispute.user != user {
let stake_diff = if prev_dispute.stake > stake { prev_dispute.stake - stake } else { stake - prev_dispute.stake };
let time_diff = if prev_dispute.timestamp > dispute.timestamp { prev_dispute.timestamp - dispute.timestamp } else { dispute.timestamp - prev_dispute.timestamp };

if stake_diff <= config.stake_delta_threshold && time_diff <= config.time_delta_threshold {
crate::events::EventEmitter::emit_suspected_collusion_flag(
env,
&market_id,
&user,
&prev_dispute.user,
stake_diff,
time_diff,
);
}
}
}
}
// --------------------------

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/predictify-hybrid/src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub enum Error {
/// The upgrade chain predecessor hash does not match the expected value.
UpgradeChainMismatch = 525,
/// An admin override nonce was replayed; reject to prevent replay attacks.
ReplayedOverride = 526,
ReplayedAdminOverride = 526,
/// Oracle quote is an outlier relative to the rolling median history.
OracleQuoteOutlier = 527,
}
Expand Down
Loading
Loading