lineage-rs 0.2.2

Software identity preserved through irreversible change
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Advanced Features - Blockchain integration, evolutionary AI, governance
//!
//! These features position Lineage Finance as a groundbreaking platform:
//! - Blockchain hooks for on-chain deployment
//! - Evolutionary AI integration framework
//! - Real-time market adaptation
//! - Community governance with permanent decisions

use serde::{Deserialize, Serialize};

/// Blockchain platform integration hook
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BlockchainHook {
    /// Ethereum integration
    Ethereum {
        contract_address: String,
        network: String,
    },
    
    /// Solana integration
    Solana {
        program_id: String,
        cluster: String,
    },
    
    /// Polygon integration
    Polygon {
        contract_address: String,
    },
    
    /// Generic EVM chain
    EVM {
        rpc_endpoint: String,
        contract_address: String,
    },
}

impl BlockchainHook {
    /// Get deployment requirements
    pub fn deployment_requirements(&self) -> Vec<String> {
        match self {
            BlockchainHook::Ethereum { .. } => vec![
                "agent_state_hash".to_string(),
                "scar_history".to_string(),
                "trade_log".to_string(),
            ],
            BlockchainHook::Solana { .. } => vec![
                "agent_pubkey".to_string(),
                "state_account".to_string(),
            ],
            BlockchainHook::Polygon { .. } => vec![
                "agent_state_hash".to_string(),
            ],
            BlockchainHook::EVM { .. } => vec![
                "agent_state_hash".to_string(),
            ],
        }
    }
    
    /// Enable trustless global trading
    pub fn enable_trustless_trading(&self) -> bool {
        true // All blockchains support this
    }
}

/// Evolutionary AI strategy integration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvolutionaryStrategy {
    /// ML framework (PyTorch, TensorFlow, etc.)
    pub ml_framework: String,
    
    /// Model type (Neural Net, Decision Tree, etc.)
    pub model_type: String,
    
    /// Genetic algorithm mutation rate
    pub mutation_rate: f32,
    
    /// Population size for evolution
    pub population_size: u32,
    
    /// Generation count
    pub generation: u32,
    
    /// Fitness function (trading performance)
    pub fitness_metric: String,
}

impl EvolutionaryStrategy {
    /// Create new evolutionary strategy
    pub fn new(
        ml_framework: String,
        model_type: String,
        population_size: u32,
    ) -> Self {
        EvolutionaryStrategy {
            ml_framework,
            model_type,
            mutation_rate: 0.1,
            population_size,
            generation: 0,
            fitness_metric: "total_pnl".to_string(),
        }
    }
    
    /// Simulate evolution round
    pub fn evolve_generation(&mut self) -> Vec<EvolvedAgent> {
        // In real implementation, would use genetic algorithm
        // For now, return placeholder evolved agents
        let mut evolved = Vec::new();
        
        for i in 0..self.population_size {
            evolved.push(EvolvedAgent {
                id: format!("evolved_{}_{}", self.generation, i),
                fitness_score: 50.0 + (i as f32 * 0.1),
                mutation_count: 1,
            });
        }
        
        self.generation += 1;
        evolved
    }
}

/// An evolved agent from the evolutionary process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvolvedAgent {
    pub id: String,
    pub fitness_score: f32,
    pub mutation_count: u32,
}

/// Real-time market adaptation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RealTimeAdaptation {
    /// Oracle source (Chainlink, Pyth, etc.)
    pub oracle_source: String,
    
    /// Update frequency (milliseconds)
    pub update_frequency_ms: u64,
    
    /// Black swan detection enabled
    pub black_swan_detection: bool,
    
    /// Flash crash protection
    pub flash_crash_protection: bool,
    
    /// Volatility adaptation
    pub volatility_adaptation: bool,
}

impl Default for RealTimeAdaptation {
    fn default() -> Self {
        RealTimeAdaptation {
            oracle_source: "Chainlink".to_string(),
            update_frequency_ms: 100,
            black_swan_detection: true,
            flash_crash_protection: true,
            volatility_adaptation: true,
        }
    }
}

impl RealTimeAdaptation {
    /// Process market event and adapt strategy
    pub fn process_event(&self, event_type: MarketEvent) -> AdaptationResponse {
        match event_type {
            MarketEvent::BlackSwan { volatility_increase } => {
                AdaptationResponse::ReduceExposure {
                    reduction_percentage: volatility_increase.min(50.0),
                    reason: "Black swan event detected".to_string(),
                }
            }
            MarketEvent::FlashCrash { severity } => {
                AdaptationResponse::EmergencyHalt {
                    reason: format!("Flash crash severity: {}", severity),
                }
            }
            MarketEvent::VolatilitySpike { new_vix } => {
                AdaptationResponse::AdjustLeverage {
                    new_leverage: 5.0 / (new_vix / 20.0).max(1.0),
                    reason: format!("Volatility spike: {}", new_vix),
                }
            }
            MarketEvent::TrendChange { direction } => {
                AdaptationResponse::RebalancePortfolio {
                    direction,
                    intensity: 0.7,
                }
            }
        }
    }
}

/// Market events that trigger adaptation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MarketEvent {
    /// Black swan event
    BlackSwan { volatility_increase: f32 },
    
    /// Flash crash
    FlashCrash { severity: f32 },
    
    /// Sudden volatility change
    VolatilitySpike { new_vix: f32 },
    
    /// Trend reversal
    TrendChange { direction: String },
}

/// Adaptation response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AdaptationResponse {
    /// Reduce position size
    ReduceExposure { reduction_percentage: f32, reason: String },
    
    /// Stop trading immediately
    EmergencyHalt { reason: String },
    
    /// Change leverage
    AdjustLeverage { new_leverage: f32, reason: String },
    
    /// Rebalance positions
    RebalancePortfolio { direction: String, intensity: f32 },
}

/// Governance vote (permanent, immutable)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GovernanceVote {
    /// Proposal ID
    pub proposal_id: String,
    
    /// Voter (agent ID)
    pub voter_id: String,
    
    /// Vote choice (yes/no/abstain)
    pub choice: VoteChoice,
    
    /// Voting power (based on trust score)
    pub voting_power: f32,
    
    /// Reasoning (required)
    pub rationale: String,
    
    /// Timestamp (permanent record)
    pub timestamp: u64,
    
    /// Signature (cryptographic proof)
    pub signature: String,
}

/// Vote choice
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VoteChoice {
    Yes,
    No,
    Abstain,
}

/// Governance proposal
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GovernanceProposal {
    /// Proposal ID
    pub id: String,
    
    /// Title
    pub title: String,
    
    /// Description
    pub description: String,
    
    /// Type of change
    pub change_type: ProposalType,
    
    /// Required voting power to pass (0-100)
    pub threshold_percentage: f32,
    
    /// Votes received
    pub votes: Vec<GovernanceVote>,
    
    /// Status
    pub status: ProposalStatus,
    
    /// Execution is irreversible (note)
    pub is_irreversible: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProposalType {
    FeeChange,
    LeverageLimit,
    SpawningRequirements,
    TrustScoringFormula,
    AssetListChange,
    ProtocolUpgrade,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProposalStatus {
    Draft,
    Voting,
    Passed,
    Rejected,
    Executed,
}

impl GovernanceProposal {
    /// Create new proposal
    pub fn new(
        id: String,
        title: String,
        description: String,
        change_type: ProposalType,
    ) -> Self {
        GovernanceProposal {
            id,
            title,
            description,
            change_type,
            threshold_percentage: 66.0,
            votes: Vec::new(),
            status: ProposalStatus::Draft,
            is_irreversible: true,
        }
    }
    
    /// Cast a vote
    pub fn vote(&mut self, vote: GovernanceVote) {
        self.votes.push(vote);
    }
    
    /// Calculate voting result
    pub fn calculate_result(&self) -> GovernanceResult {
        let total_power: f32 = self.votes.iter().map(|v| v.voting_power).sum();
        let yes_power: f32 = self.votes.iter()
            .filter(|v| v.choice == VoteChoice::Yes)
            .map(|v| v.voting_power)
            .sum();
        
        let yes_percentage = if total_power > 0.0 {
            (yes_power / total_power) * 100.0
        } else {
            0.0
        };
        
        let passed = yes_percentage >= self.threshold_percentage;
        
        GovernanceResult {
            total_votes: self.votes.len() as u64,
            total_voting_power: total_power,
            yes_percentage,
            passed,
        }
    }
}

/// Permadeath Economy - Resurrection Mechanic
/// 
/// Rare, scar-heavy revival system that enables speculative markets
/// around agent resurrection. This creates a meta-game where:
/// - Dead agents can be resurrected (very rare probability)
/// - Resurrection is costly in scars (increases future operational costs)
/// - Resurrected agents start with reduced capital
/// - Resurrection events create market opportunities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResurrectionMechanic {
    /// Probability of resurrection when agent dies (0.0-1.0)
    pub resurrection_probability: f32,
    
    /// Capital multiplier on resurrection (e.g., 0.5 = 50% of original)
    pub capital_recovery_rate: f32,
    
    /// Number of scars added on resurrection
    pub resurrection_scar_cost: u32,
    
    /// Scar severity for resurrection (makes future trades more expensive)
    pub scar_severity: String,
    
    /// Cooldown rounds before next resurrection is possible
    pub resurrection_cooldown: u32,
    
    /// Cost in resources (e.g., community staking votes needed)
    pub resurrection_cost: u64,
    
    /// Total resurrections in system (for metrics)
    pub total_resurrections: u32,
}

impl ResurrectionMechanic {
    /// Create default resurrection mechanic (1% chance, scar-heavy)
    pub fn new() -> Self {
        ResurrectionMechanic {
            resurrection_probability: 0.01,           // 1% chance
            capital_recovery_rate: 0.5,               // 50% capital recovery
            resurrection_scar_cost: 3,                // 3 additional scars
            scar_severity: "Major".to_string(),       // Expensive scars
            resurrection_cooldown: 20,                // Can't be resurrected every round
            resurrection_cost: 5000,                  // Community staking cost
            total_resurrections: 0,
        }
    }
    
    /// Create permissive resurrection (testing/demo purposes)
    pub fn permissive() -> Self {
        ResurrectionMechanic {
            resurrection_probability: 0.05,           // 5% chance
            capital_recovery_rate: 0.75,              // 75% capital recovery
            resurrection_scar_cost: 2,                // 2 scars
            scar_severity: "Moderate".to_string(),
            resurrection_cooldown: 10,
            resurrection_cost: 1000,
            total_resurrections: 0,
        }
    }
    
    /// Check if resurrection should occur (probability-based)
    pub fn should_resurrect(&self) -> bool {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        rng.gen_range(0.0..1.0) < self.resurrection_probability
    }
    
    /// Calculate resurrected capital amount
    pub fn calculate_resurrected_capital(&self, original_capital: u64) -> u64 {
        (original_capital as f32 * self.capital_recovery_rate) as u64
    }
    
    /// Get resurrection cost message
    pub fn get_resurrection_cost_message(&self) -> String {
        format!(
            "Resurrection activated! Cost: {} scars ({}), {} capital staked, {} round cooldown",
            self.resurrection_scar_cost,
            self.scar_severity,
            self.resurrection_cost,
            self.resurrection_cooldown
        )
    }
}

impl Default for ResurrectionMechanic {
    fn default() -> Self {
        Self::new()
    }
}

/// Track resurrection events for metrics and speculation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResurrectionRecord {
    /// Agent ID that was resurrected
    pub agent_id: String,
    
    /// Round number when resurrection occurred
    pub resurrection_round: u32,
    
    /// Capital before death
    pub capital_before_death: u64,
    
    /// Capital after resurrection
    pub capital_after_resurrection: u64,
    
    /// Scars accumulated from resurrection
    pub resurrection_scars: u32,
    
    /// Previous death count for this agent
    pub death_count: u32,
    
    /// Time cost (rounds until eligible for another resurrection)
    pub cooldown_rounds_remaining: u32,
}

impl ResurrectionRecord {
    /// Create new resurrection record
    pub fn new(
        agent_id: String,
        round: u32,
        capital_before: u64,
        capital_after: u64,
        scars: u32,
        death_count: u32,
        cooldown: u32,
    ) -> Self {
        ResurrectionRecord {
            agent_id,
            resurrection_round: round,
            capital_before_death: capital_before,
            capital_after_resurrection: capital_after,
            resurrection_scars: scars,
            death_count,
            cooldown_rounds_remaining: cooldown,
        }
    }
    
    /// Calculate capital loss from resurrection
    pub fn capital_loss(&self) -> u64 {
        self.capital_before_death.saturating_sub(self.capital_after_resurrection)
    }
    
    /// Get resurrection story/narrative
    pub fn narrate(&self) -> String {
        format!(
            "Agent {} rose from the dead at round {} | \
             Lost: ${} | Resurrected with: ${} | \
             Scars incurred: {} | Death #{} | \
             Cooldown: {} rounds",
            self.agent_id,
            self.resurrection_round,
            self.capital_loss(),
            self.capital_after_resurrection,
            self.resurrection_scars,
            self.death_count,
            self.cooldown_rounds_remaining,
        )
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GovernanceResult {
    pub total_votes: u64,
    pub total_voting_power: f32,
    pub yes_percentage: f32,
    pub passed: bool,
}

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

    #[test]
    fn test_blockchain_integration() {
        let hook = BlockchainHook::Ethereum {
            contract_address: "0x123".to_string(),
            network: "mainnet".to_string(),
        };
        assert!(hook.enable_trustless_trading());
    }

    #[test]
    fn test_evolutionary_strategy() {
        let mut strategy = EvolutionaryStrategy::new(
            "PyTorch".to_string(),
            "NeuralNet".to_string(),
            100,
        );
        let evolved = strategy.evolve_generation();
        assert_eq!(evolved.len(), 100);
    }
    
    #[test]
    fn test_resurrection_mechanic() {
        let resurrection = ResurrectionMechanic::new();
        assert_eq!(resurrection.resurrection_probability, 0.01);
        assert_eq!(resurrection.capital_recovery_rate, 0.5);
        
        let resurrected_capital = resurrection.calculate_resurrected_capital(100_000);
        assert_eq!(resurrected_capital, 50_000);
    }
    
    #[test]
    fn test_resurrection_record() {
        let record = ResurrectionRecord::new(
            "Agent_1".to_string(),
            50,
            100_000,
            50_000,
            3,
            1,
            20,
        );
        assert_eq!(record.capital_loss(), 50_000);
        println!("{}", record.narrate());
    }
}