pot-o-validator 0.10.0

PoT-O (Proof of Tensor Optimizations) Validator Service
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Pool Coordinator: Manages batch proof submission and coordination with primary validator.
//!
//! The coordinator accumulates proofs from miners, batches them deterministically,
//! and submits to either the local on-chain bridge (if primary) or relays to the
//! primary validator for submission. Supports Solo, Proportional, and PPLNS strategies.

use pot_o_core::{TribeError, TribeResult};
use pot_o_extensions::pool_strategy::ProofRecord;
use sha2::{Digest, Sha256};
use std::time::{Duration, SystemTime};

// ---------------------------------------------------------------------------
// PoolCoordinator Struct
// ---------------------------------------------------------------------------

/// Manages proof batching and submission coordination for pool mining.
///
/// Accumulates proofs locally, deterministically orders them, and submits
/// batches to the primary validator (or locally if this is the primary).
#[derive(Debug, Clone)]
pub struct PoolCoordinator {
    /// This validator's unique ID
    node_id: String,
    /// Pool strategy: "solo", "proportional", or "pplns"
    pool_strategy: String,
    /// URL of primary validator for delegation (None = this is primary)
    primary_validator_url: Option<String>,
    /// Minimum proofs required to trigger batch submission
    min_batch_size: usize,
    /// Maximum age in seconds for batch before forced submission
    max_batch_age_secs: u64,
    /// Accumulated proofs waiting for batch submission
    pending_proofs: Vec<ProofRecord>,
    /// Timestamp when current batch started (None = no batch accumulating)
    batch_start_time: Option<SystemTime>,
}

impl PoolCoordinator {
    /// Create a new Pool Coordinator with given configuration.
    ///
    /// # Arguments
    /// * `node_id` - Unique identifier for this validator
    /// * `pool_strategy` - Pool mode: "solo", "proportional", or "pplns"
    /// * `primary_validator_url` - URL of primary validator (None = this is primary)
    /// * `min_batch_size` - Minimum proofs before batch submission
    /// * `max_batch_age_secs` - Maximum age before forced batch submission
    ///
    /// # Errors
    /// Returns TribeResult::Err if configuration is invalid.
    pub fn new(
        node_id: String,
        pool_strategy: String,
        primary_validator_url: Option<String>,
        min_batch_size: usize,
        max_batch_age_secs: u64,
    ) -> TribeResult<Self> {
        // Validate pool strategy
        match pool_strategy.as_str() {
            "solo" | "proportional" | "pplns" => {}
            _ => {
                return Err(TribeError::InvalidOperation(format!(
                    "invalid pool strategy: {}. must be 'solo', 'proportional', or 'pplns'",
                    pool_strategy
                )));
            }
        }

        // Validate batch parameters
        if min_batch_size == 0 {
            return Err(TribeError::InvalidOperation(
                "min_batch_size must be > 0".to_string(),
            ));
        }
        if max_batch_age_secs == 0 {
            return Err(TribeError::InvalidOperation(
                "max_batch_age_secs must be > 0".to_string(),
            ));
        }

        Ok(Self {
            node_id,
            pool_strategy,
            primary_validator_url,
            min_batch_size,
            max_batch_age_secs,
            pending_proofs: Vec::new(),
            batch_start_time: None,
        })
    }

    /// Add a proof to the current batch.
    ///
    /// Initializes batch timing on first proof added. Does not check if batch
    /// is ready for submission - caller should check `batch_ready()` after adding.
    ///
    /// # Arguments
    /// * `proof` - Proof record to add to pending batch
    ///
    /// # Returns
    /// Ok if proof added successfully
    pub fn add_proof(&mut self, proof: ProofRecord) -> TribeResult<()> {
        // Initialize batch timing if first proof
        if self.pending_proofs.is_empty() && self.batch_start_time.is_none() {
            self.batch_start_time = Some(SystemTime::now());
        }

        self.pending_proofs.push(proof);
        Ok(())
    }

    /// Get list of pending proofs waiting for submission.
    ///
    /// # Returns
    /// Vec of ProofRecord currently accumulated in batch
    pub fn get_pending_proofs(&self) -> Vec<ProofRecord> {
        self.pending_proofs.clone()
    }

    /// Check if current batch is ready for submission.
    ///
    /// Batch is ready when:
    /// - Size >= min_batch_size, OR
    /// - Time since batch start >= max_batch_age_secs
    ///
    /// # Returns
    /// true if batch should be submitted now
    pub fn batch_ready(&self) -> bool {
        // Check size
        if self.pending_proofs.len() >= self.min_batch_size {
            return true;
        }

        // Check age
        if let Some(start_time) = self.batch_start_time {
            if let Ok(elapsed) = start_time.elapsed() {
                if elapsed >= Duration::from_secs(self.max_batch_age_secs) {
                    return true;
                }
            }
        }

        false
    }

    /// Check if this validator is the primary (receives proofs from others).
    ///
    /// Primary detection heuristic:
    /// - primary_validator_url is None, OR
    /// - primary_validator_url contains "localhost" or "127.0.0.1"
    ///
    /// # Returns
    /// true if this is the primary validator
    pub fn is_primary_validator(&self) -> bool {
        self.primary_validator_url.is_none()
            || self
                .primary_validator_url
                .as_ref()
                .is_some_and(|url| url.contains("localhost") || url.contains("127.0.0.1"))
    }

    /// Submit a batch of proofs for on-chain processing.
    ///
    /// If this is the primary validator, submits locally (caller should
    /// handle on-chain submission). Otherwise, relays to primary validator URL.
    ///
    /// # Arguments
    /// * `batch` - Proofs to submit (will be sorted deterministically)
    ///
    /// # Returns
    /// Result containing submission ID/hash or error
    pub async fn submit_batch_to_primary(
        &mut self,
        mut batch: Vec<ProofRecord>,
    ) -> TribeResult<String> {
        // Sort deterministically for consistent ordering
        self.sort_proofs_deterministically(&mut batch);

        // If primary, submit locally
        if self.is_primary_validator() {
            return self.submit_batch_local(&batch).await;
        }

        // Otherwise relay to primary validator
        if let Some(ref primary_url) = self.primary_validator_url {
            return self.submit_batch_to_url(primary_url, &batch).await;
        }

        Err(TribeError::NetworkError(
            "no primary validator URL configured".to_string(),
        ))
    }

    /// Clear all pending proofs from the batch.
    ///
    /// Called after successful batch submission to reset for next batch.
    pub fn clear_batch(&mut self) {
        self.pending_proofs.clear();
        self.batch_start_time = None;
    }

    // -----------------------------------------------------------------------
    // Private Helpers
    // -----------------------------------------------------------------------

    /// Sort proofs deterministically by (miner_pubkey, timestamp).
    ///
    /// Ensures all validators produce same batch order for consistency.
    fn sort_proofs_deterministically(&self, proofs: &mut [ProofRecord]) {
        proofs.sort_by(|a, b| match a.miner_pubkey.cmp(&b.miner_pubkey) {
            std::cmp::Ordering::Equal => a.timestamp.cmp(&b.timestamp),
            other => other,
        });
    }

    /// Submit batch locally (primary validator path).
    ///
    /// Generates a deterministic hash of the batch to serve as the submission ID.
    /// Actual on-chain proof submission occurs via POST /submit (one proof at a time);
    /// this method records the batch for pool reward accounting.
    async fn submit_batch_local(&self, batch: &[ProofRecord]) -> TribeResult<String> {
        let mut hasher = Sha256::new();
        hasher.update(b"pot-o-batch:");
        hasher.update(self.node_id.as_bytes());
        hasher.update(b":");
        for record in batch {
            hasher.update(record.miner_pubkey.as_bytes());
            hasher.update(b":");
            hasher.update(record.challenge_id.as_bytes());
            hasher.update(b":");
            hasher.update(record.reward.to_le_bytes());
            hasher.update(b":");
            hasher.update(record.timestamp.timestamp().to_le_bytes());
            hasher.update(b"|");
        }
        let hash = hex::encode(hasher.finalize());
        let submission_id = format!("local-batch-{}", &hash[..40]);

        tracing::info!(
            batch_size = batch.len(),
            submission_id = %submission_id,
            "Batch submitted locally for pool accounting"
        );

        Ok(submission_id)
    }

    /// Submit batch to primary validator URL via HTTP.
    async fn submit_batch_to_url(
        &self,
        primary_url: &str,
        batch: &[ProofRecord],
    ) -> TribeResult<String> {
        let client = reqwest::Client::new();

        // Build submission payload
        let payload = serde_json::json!({
            "node_id": self.node_id,
            "pool_strategy": self.pool_strategy,
            "batch": batch,
        });

        // Submit to primary validator
        let url = format!(
            "{}/api/pool/submit-batch",
            primary_url.trim_end_matches('/')
        );

        let response = client.post(&url).json(&payload).send().await.map_err(|e| {
            TribeError::NetworkError(format!("failed to submit batch to primary: {}", e))
        })?;

        if !response.status().is_success() {
            return Err(TribeError::NetworkError(format!(
                "batch submission failed: {}",
                response.status()
            )));
        }

        let resp_body: serde_json::Value = response
            .json()
            .await
            .map_err(|e| TribeError::NetworkError(format!("failed to parse response: {}", e)))?;

        resp_body
            .get("submission_id")
            .and_then(|id| id.as_str())
            .map(|id| id.to_string())
            .ok_or_else(|| TribeError::NetworkError("no submission_id in response".to_string()))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn create_proof(miner_pubkey: &str, timestamp: chrono::DateTime<Utc>) -> ProofRecord {
        ProofRecord {
            miner_pubkey: miner_pubkey.to_string(),
            challenge_id: "challenge1".to_string(),
            reward: 1000,
            timestamp,
        }
    }

    fn create_coordinator(
        pool_strategy: &str,
        primary_url: Option<String>,
    ) -> TribeResult<PoolCoordinator> {
        PoolCoordinator::new(
            "validator-1".to_string(),
            pool_strategy.to_string(),
            primary_url,
            2,    // min_batch_size = 2
            3600, // max_batch_age_secs = 1 hour
        )
    }

    // =========================================================================
    // Basic Creation Tests
    // =========================================================================

    #[test]
    fn test_coordinator_creation_solo() {
        let coordinator = create_coordinator("solo", None);
        assert!(coordinator.is_ok());
        let coord = coordinator.unwrap();
        assert_eq!(coord.pool_strategy, "solo");
        assert!(coord.is_primary_validator());
    }

    #[test]
    fn test_coordinator_creation_proportional() {
        let coordinator =
            create_coordinator("proportional", Some("http://primary:8900".to_string()));
        assert!(coordinator.is_ok());
        let coord = coordinator.unwrap();
        assert_eq!(coord.pool_strategy, "proportional");
        assert!(!coord.is_primary_validator());
    }

    #[test]
    fn test_coordinator_creation_pplns() {
        let coordinator = create_coordinator("pplns", None);
        assert!(coordinator.is_ok());
        let coord = coordinator.unwrap();
        assert_eq!(coord.pool_strategy, "pplns");
    }

    #[test]
    fn test_coordinator_invalid_strategy() {
        let result = PoolCoordinator::new(
            "validator-1".to_string(),
            "invalid-strategy".to_string(),
            None,
            2,
            3600,
        );
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("invalid pool strategy"));
    }

    #[test]
    fn test_coordinator_invalid_batch_size() {
        let result = PoolCoordinator::new(
            "validator-1".to_string(),
            "solo".to_string(),
            None,
            0, // invalid
            3600,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_coordinator_invalid_batch_age() {
        let result = PoolCoordinator::new(
            "validator-1".to_string(),
            "solo".to_string(),
            None,
            2,
            0, // invalid
        );
        assert!(result.is_err());
    }

    // =========================================================================
    // Proof Addition Tests
    // =========================================================================

    #[test]
    fn test_add_single_proof() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let proof = create_proof("miner1", Utc::now());

        let result = coordinator.add_proof(proof.clone());
        assert!(result.is_ok());

        let pending = coordinator.get_pending_proofs();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].miner_pubkey, "miner1");
    }

    #[test]
    fn test_add_multiple_proofs() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        for i in 0..5 {
            let proof = create_proof(&format!("miner{}", i), now);
            let result = coordinator.add_proof(proof);
            assert!(result.is_ok());
        }

        let pending = coordinator.get_pending_proofs();
        assert_eq!(pending.len(), 5);
    }

    #[test]
    fn test_get_pending_proofs_empty() {
        let coordinator = create_coordinator("solo", None).unwrap();
        let pending = coordinator.get_pending_proofs();
        assert_eq!(pending.len(), 0);
    }

    #[test]
    fn test_get_pending_proofs_returns_clone() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let proof = create_proof("miner1", Utc::now());
        coordinator.add_proof(proof).unwrap();

        let pending1 = coordinator.get_pending_proofs();
        let pending2 = coordinator.get_pending_proofs();

        // Should return independent clones
        assert_eq!(pending1.len(), pending2.len());
        assert_eq!(pending1[0].miner_pubkey, pending2[0].miner_pubkey);
    }

    // =========================================================================
    // Batch Readiness Tests
    // =========================================================================

    #[test]
    fn test_batch_ready_by_size() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        // Add 1 proof - not ready
        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        assert!(!coordinator.batch_ready());

        // Add 2nd proof - should be ready (min_batch_size=2)
        coordinator.add_proof(create_proof("miner2", now)).unwrap();
        assert!(coordinator.batch_ready());
    }

    #[test]
    fn test_batch_not_ready_insufficient_size() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        // Add 1 proof with fresh start time
        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        assert!(!coordinator.batch_ready());
    }

    #[test]
    fn test_batch_ready_empty() {
        let coordinator = create_coordinator("solo", None).unwrap();
        assert!(!coordinator.batch_ready());
    }

    #[test]
    fn test_batch_start_time_initialized_on_first_proof() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        assert!(coordinator.batch_start_time.is_none());

        coordinator
            .add_proof(create_proof("miner1", Utc::now()))
            .unwrap();
        assert!(coordinator.batch_start_time.is_some());
    }

    #[test]
    fn test_batch_start_time_not_reset_on_second_proof() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let first_time = Utc::now();

        coordinator
            .add_proof(create_proof("miner1", first_time))
            .unwrap();
        let first_batch_time = coordinator.batch_start_time;

        coordinator
            .add_proof(create_proof("miner2", Utc::now()))
            .unwrap();
        let second_batch_time = coordinator.batch_start_time;

        // Should be same start time
        assert_eq!(first_batch_time, second_batch_time);
    }

    // =========================================================================
    // Primary Validator Detection Tests
    // =========================================================================

    #[test]
    fn test_is_primary_validator_none_url() {
        let coordinator = create_coordinator("solo", None).unwrap();
        assert!(coordinator.is_primary_validator());
    }

    #[test]
    fn test_is_primary_validator_localhost() {
        let coordinator =
            create_coordinator("solo", Some("http://localhost:8900".to_string())).unwrap();
        assert!(coordinator.is_primary_validator());
    }

    #[test]
    fn test_is_primary_validator_127_0_0_1() {
        let coordinator =
            create_coordinator("solo", Some("http://127.0.0.1:8900".to_string())).unwrap();
        assert!(coordinator.is_primary_validator());
    }

    #[test]
    fn test_is_primary_validator_remote_url() {
        let coordinator =
            create_coordinator("solo", Some("http://primary.cluster:8900".to_string())).unwrap();
        assert!(!coordinator.is_primary_validator());
    }

    #[test]
    fn test_is_primary_validator_remote_ip() {
        let coordinator =
            create_coordinator("solo", Some("http://192.168.1.100:8900".to_string())).unwrap();
        assert!(!coordinator.is_primary_validator());
    }

    // =========================================================================
    // Batch Clearing Tests
    // =========================================================================

    #[test]
    fn test_clear_batch() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        coordinator.add_proof(create_proof("miner2", now)).unwrap();

        assert_eq!(coordinator.get_pending_proofs().len(), 2);
        assert!(coordinator.batch_start_time.is_some());

        coordinator.clear_batch();

        assert_eq!(coordinator.get_pending_proofs().len(), 0);
        assert!(coordinator.batch_start_time.is_none());
    }

    #[test]
    fn test_clear_batch_twice() {
        let mut coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        coordinator.clear_batch();
        coordinator.clear_batch(); // Should not panic

        assert_eq!(coordinator.get_pending_proofs().len(), 0);
    }

    // =========================================================================
    // Deterministic Sorting Tests
    // =========================================================================

    #[test]
    fn test_deterministic_sorting_by_pubkey() {
        let coordinator = create_coordinator("solo", None).unwrap();
        let now = Utc::now();

        let mut batch = vec![
            create_proof("zebra_miner", now),
            create_proof("alpha_miner", now),
            create_proof("beta_miner", now),
        ];

        coordinator.sort_proofs_deterministically(&mut batch);

        assert_eq!(batch[0].miner_pubkey, "alpha_miner");
        assert_eq!(batch[1].miner_pubkey, "beta_miner");
        assert_eq!(batch[2].miner_pubkey, "zebra_miner");
    }

    #[test]
    fn test_deterministic_sorting_by_timestamp_same_pubkey() {
        let coordinator = create_coordinator("solo", None).unwrap();
        let base_time = Utc::now();

        let mut batch = vec![
            create_proof("miner1", base_time + chrono::Duration::seconds(3)),
            create_proof("miner1", base_time + chrono::Duration::seconds(1)),
            create_proof("miner1", base_time + chrono::Duration::seconds(2)),
        ];

        coordinator.sort_proofs_deterministically(&mut batch);

        assert!(batch[0].timestamp <= batch[1].timestamp);
        assert!(batch[1].timestamp <= batch[2].timestamp);
    }

    #[test]
    fn test_deterministic_sorting_combined() {
        let coordinator = create_coordinator("solo", None).unwrap();
        let base_time = Utc::now();

        let mut batch = vec![
            create_proof("miner2", base_time + chrono::Duration::seconds(1)),
            create_proof("miner1", base_time + chrono::Duration::seconds(3)),
            create_proof("miner1", base_time + chrono::Duration::seconds(1)),
            create_proof("miner2", base_time + chrono::Duration::seconds(2)),
        ];

        coordinator.sort_proofs_deterministically(&mut batch);

        // All miner1 should come before miner2
        assert_eq!(batch[0].miner_pubkey, "miner1");
        assert_eq!(batch[1].miner_pubkey, "miner1");
        assert_eq!(batch[2].miner_pubkey, "miner2");
        assert_eq!(batch[3].miner_pubkey, "miner2");

        // Within each miner, should be sorted by timestamp
        assert!(batch[0].timestamp <= batch[1].timestamp);
        assert!(batch[2].timestamp <= batch[3].timestamp);
    }

    // =========================================================================
    // Integration Tests
    // =========================================================================

    #[test]
    fn test_full_workflow_primary() {
        let mut coordinator = create_coordinator("proportional", None).unwrap();
        let now = Utc::now();

        // Add proofs
        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        assert!(!coordinator.batch_ready());

        coordinator.add_proof(create_proof("miner2", now)).unwrap();
        assert!(coordinator.batch_ready());

        // Get batch and verify it's cloned
        let batch = coordinator.get_pending_proofs();
        assert_eq!(batch.len(), 2);

        // Clear batch
        coordinator.clear_batch();
        assert_eq!(coordinator.get_pending_proofs().len(), 0);
        assert!(!coordinator.batch_ready());
    }

    #[test]
    fn test_full_workflow_secondary() {
        let mut coordinator =
            create_coordinator("pplns", Some("http://primary.cluster:8900".to_string())).unwrap();
        let now = Utc::now();

        assert!(!coordinator.is_primary_validator());

        coordinator.add_proof(create_proof("miner1", now)).unwrap();
        coordinator.add_proof(create_proof("miner2", now)).unwrap();

        assert!(coordinator.batch_ready());

        let batch = coordinator.get_pending_proofs();
        assert_eq!(batch.len(), 2);

        coordinator.clear_batch();
        assert_eq!(coordinator.get_pending_proofs().len(), 0);
    }
}