daa-rules 0.2.1

Rules engine for DAA system providing policy enforcement and decision automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Rule definitions and built-in rules

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fmt;

use crate::context::StateContext;
use crate::error::{Result, RuleError};

/// A rule violation with details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleViolation {
    pub rule_id: String,
    pub message: String,
    pub severity: ViolationSeverity,
    pub timestamp: DateTime<Utc>,
    pub context: serde_json::Value,
}

/// Severity levels for violations
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ViolationSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// A rule that can be evaluated against a state context
pub trait Rule: Send + Sync {
    /// Unique identifier for the rule
    fn id(&self) -> &str;
    
    /// Human-readable description of the rule
    fn description(&self) -> &str;
    
    /// Evaluate the rule against the given context
    fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>>;
    
    /// Check if the rule is enabled
    fn is_enabled(&self) -> bool {
        true
    }
}

/// Built-in rules module
pub mod builtin {
    use super::*;

    /// Rule that limits maximum daily spending
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct MaxDailySpendingRule {
        pub max_amount: u128,
        pub enabled: bool,
    }

    impl MaxDailySpendingRule {
        pub fn new(max_amount: u128) -> Self {
            Self {
                max_amount,
                enabled: true,
            }
        }
    }

    impl Rule for MaxDailySpendingRule {
        fn id(&self) -> &str {
            "max_daily_spending"
        }

        fn description(&self) -> &str {
            "Limits maximum daily spending amount"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            let today = context.timestamp.format("%Y-%m-%d").to_string();
            let daily_total = context.get_daily_spending(&today);

            if daily_total > self.max_amount {
                Ok(Some(RuleViolation {
                    rule_id: self.id().to_string(),
                    message: format!(
                        "Daily spending limit exceeded: {} > {}",
                        daily_total, self.max_amount
                    ),
                    severity: ViolationSeverity::Error,
                    timestamp: Utc::now(),
                    context: serde_json::json!({
                        "daily_total": daily_total,
                        "max_amount": self.max_amount,
                        "date": today
                    }),
                }))
            } else {
                Ok(None)
            }
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }

    /// Rule that enforces minimum balance requirements
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct MinimumBalanceRule {
        pub address: String,
        pub min_balance: u128,
        pub enabled: bool,
    }

    impl MinimumBalanceRule {
        pub fn new(address: String, min_balance: u128) -> Self {
            Self {
                address,
                min_balance,
                enabled: true,
            }
        }
    }

    impl Rule for MinimumBalanceRule {
        fn id(&self) -> &str {
            "minimum_balance"
        }

        fn description(&self) -> &str {
            "Ensures minimum balance is maintained"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            let balance = context.get_balance(&self.address);

            if balance < self.min_balance {
                Ok(Some(RuleViolation {
                    rule_id: self.id().to_string(),
                    message: format!(
                        "Minimum balance requirement not met for {}: {} < {}",
                        self.address, balance, self.min_balance
                    ),
                    severity: ViolationSeverity::Warning,
                    timestamp: Utc::now(),
                    context: serde_json::json!({
                        "address": self.address,
                        "current_balance": balance,
                        "min_balance": self.min_balance
                    }),
                }))
            } else {
                Ok(None)
            }
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }

    /// Rule that limits maximum transaction amount
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct MaxTransactionAmountRule {
        pub max_amount: u128,
        pub enabled: bool,
    }

    impl MaxTransactionAmountRule {
        pub fn new(max_amount: u128) -> Self {
            Self {
                max_amount,
                enabled: true,
            }
        }
    }

    impl Rule for MaxTransactionAmountRule {
        fn id(&self) -> &str {
            "max_transaction_amount"
        }

        fn description(&self) -> &str {
            "Limits maximum single transaction amount"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            for &amount in &context.transaction_amounts {
                if amount > self.max_amount {
                    return Ok(Some(RuleViolation {
                        rule_id: self.id().to_string(),
                        message: format!(
                            "Transaction amount exceeds limit: {} > {}",
                            amount, self.max_amount
                        ),
                        severity: ViolationSeverity::Error,
                        timestamp: Utc::now(),
                        context: serde_json::json!({
                            "transaction_amount": amount,
                            "max_amount": self.max_amount
                        }),
                    }));
                }
            }

            Ok(None)
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }

    /// Rule that enforces operational hours
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct OperationalHoursRule {
        pub start_hour: u32,
        pub end_hour: u32,
        pub enabled: bool,
    }

    impl OperationalHoursRule {
        pub fn new(start_hour: u32, end_hour: u32) -> Self {
            Self {
                start_hour,
                end_hour,
                enabled: true,
            }
        }

        pub fn business_hours() -> Self {
            Self::new(9, 17) // 9 AM to 5 PM
        }
    }

    impl Rule for OperationalHoursRule {
        fn id(&self) -> &str {
            "operational_hours"
        }

        fn description(&self) -> &str {
            "Restricts operations to specific hours"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            let current_hour = context.timestamp.hour();

            if current_hour < self.start_hour || current_hour >= self.end_hour {
                Ok(Some(RuleViolation {
                    rule_id: self.id().to_string(),
                    message: format!(
                        "Operation outside allowed hours: {} (allowed: {}-{})",
                        current_hour, self.start_hour, self.end_hour
                    ),
                    severity: ViolationSeverity::Warning,
                    timestamp: Utc::now(),
                    context: serde_json::json!({
                        "current_hour": current_hour,
                        "start_hour": self.start_hour,
                        "end_hour": self.end_hour
                    }),
                }))
            } else {
                Ok(None)
            }
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }

    /// Rule that implements rate limiting
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct RateLimitRule {
        pub key: String,
        pub max_requests: u32,
        pub window_seconds: u64,
        pub enabled: bool,
    }

    impl RateLimitRule {
        pub fn new(key: String, max_requests: u32, window_seconds: u64) -> Self {
            Self {
                key,
                max_requests,
                window_seconds,
                enabled: true,
            }
        }
    }

    impl Rule for RateLimitRule {
        fn id(&self) -> &str {
            "rate_limit"
        }

        fn description(&self) -> &str {
            "Implements rate limiting for operations"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            let current_count = context.get_rate_limit(&self.key);

            if current_count > self.max_requests {
                Ok(Some(RuleViolation {
                    rule_id: self.id().to_string(),
                    message: format!(
                        "Rate limit exceeded for {}: {} > {}",
                        self.key, current_count, self.max_requests
                    ),
                    severity: ViolationSeverity::Error,
                    timestamp: Utc::now(),
                    context: serde_json::json!({
                        "key": self.key,
                        "current_count": current_count,
                        "max_requests": self.max_requests,
                        "window_seconds": self.window_seconds
                    }),
                }))
            } else {
                Ok(None)
            }
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }

    /// Rule that enforces risk thresholds
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct RiskThresholdRule {
        pub max_risk_score: f64,
        pub enabled: bool,
    }

    impl RiskThresholdRule {
        pub fn new(max_risk_score: f64) -> Self {
            Self {
                max_risk_score,
                enabled: true,
            }
        }

        fn calculate_risk_score(&self, context: &StateContext) -> f64 {
            // Simple risk calculation based on transaction amounts
            let total_amount: u128 = context.transaction_amounts.iter().sum();
            let avg_amount = if context.transaction_amounts.is_empty() {
                0.0
            } else {
                total_amount as f64 / context.transaction_amounts.len() as f64
            };

            // Risk increases with higher amounts and more transactions
            let amount_risk = (avg_amount / 10000.0).min(1.0);
            let frequency_risk = (context.transaction_amounts.len() as f64 / 100.0).min(1.0);
            
            (amount_risk + frequency_risk) / 2.0
        }
    }

    impl Rule for RiskThresholdRule {
        fn id(&self) -> &str {
            "risk_threshold"
        }

        fn description(&self) -> &str {
            "Monitors risk score and alerts on threshold breach"
        }

        fn evaluate(&self, context: &StateContext) -> Result<Option<RuleViolation>> {
            if !self.enabled {
                return Ok(None);
            }

            let risk_score = self.calculate_risk_score(context);

            if risk_score > self.max_risk_score {
                Ok(Some(RuleViolation {
                    rule_id: self.id().to_string(),
                    message: format!(
                        "Risk threshold exceeded: {:.2} > {:.2}",
                        risk_score, self.max_risk_score
                    ),
                    severity: ViolationSeverity::Critical,
                    timestamp: Utc::now(),
                    context: serde_json::json!({
                        "risk_score": risk_score,
                        "max_risk_score": self.max_risk_score,
                        "transaction_count": context.transaction_amounts.len()
                    }),
                }))
            } else {
                Ok(None)
            }
        }

        fn is_enabled(&self) -> bool {
            self.enabled
        }
    }
}

impl fmt::Display for ViolationSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ViolationSeverity::Info => write!(f, "INFO"),
            ViolationSeverity::Warning => write!(f, "WARNING"),
            ViolationSeverity::Error => write!(f, "ERROR"),
            ViolationSeverity::Critical => write!(f, "CRITICAL"),
        }
    }
}

impl fmt::Display for RuleViolation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}] {}: {}",
            self.severity, self.rule_id, self.message
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::builtin::*;

    #[test]
    fn test_max_daily_spending_rule() {
        let rule = MaxDailySpendingRule::new(1000);
        let mut context = StateContext::new();
        
        // No violation initially
        assert!(rule.evaluate(&context).unwrap().is_none());
        
        // Add spending that exceeds limit
        let today = context.timestamp.format("%Y-%m-%d").to_string();
        context.add_daily_spending(today, 1500);
        
        let violation = rule.evaluate(&context).unwrap();
        assert!(violation.is_some());
        assert_eq!(violation.unwrap().severity, ViolationSeverity::Error);
    }

    #[test]
    fn test_minimum_balance_rule() {
        let rule = MinimumBalanceRule::new("addr1".to_string(), 100);
        let mut context = StateContext::new();
        
        // Violation when balance is below minimum
        let violation = rule.evaluate(&context).unwrap();
        assert!(violation.is_some());
        
        // No violation when balance meets minimum
        context.set_balance("addr1".to_string(), 150);
        assert!(rule.evaluate(&context).unwrap().is_none());
    }
}