apex-sdk 0.1.6

Compile-time safe, unified Rust SDK for cross-chain blockchain development across Substrate and EVM ecosystems
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Advanced features and utilities.

use crate::sdk::ApexSDK;
use std::collections::VecDeque;
use std::sync::Arc;
use tokio::sync::broadcast;
use tokio_util::sync::CancellationToken;

/// Block information
#[derive(Debug, Clone)]
pub struct BlockInfo {
    pub number: u64,
    pub hash: String,
    pub timestamp: u64,
}

/// Block subscription for real-time updates
pub struct BlockSubscription {
    receiver: broadcast::Receiver<BlockInfo>,
    cancellation_token: CancellationToken,
}

impl BlockSubscription {
    /// Create a new block subscription with cancellation support
    pub fn new() -> (broadcast::Sender<BlockInfo>, CancellationToken, Self) {
        let (sender, receiver) = broadcast::channel(100);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();
        (
            sender,
            cancellation_token,
            Self {
                receiver,
                cancellation_token: token_clone,
            },
        )
    }

    /// Get the next block from the subscription
    pub async fn next(&mut self) -> Option<BlockInfo> {
        tokio::select! {
            result = self.receiver.recv() => result.ok(),
            _ = self.cancellation_token.cancelled() => None,
        }
    }

    /// Stop the subscription by triggering cancellation
    pub fn stop(&self) {
        self.cancellation_token.cancel();
        tracing::debug!("Block subscription stopped");
    }

    /// Check if the subscription has been stopped
    pub fn is_stopped(&self) -> bool {
        self.cancellation_token.is_cancelled()
    }
}

/// Event subscription for blockchain events
pub struct EventSubscription {
    receiver: broadcast::Receiver<String>,
    cancellation_token: CancellationToken,
}

impl EventSubscription {
    /// Create a new event subscription with cancellation support
    pub fn new() -> (broadcast::Sender<String>, CancellationToken, Self) {
        let (sender, receiver) = broadcast::channel(100);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();
        (
            sender,
            cancellation_token,
            Self {
                receiver,
                cancellation_token: token_clone,
            },
        )
    }

    /// Get the next event from the subscription
    pub async fn next(&mut self) -> Option<String> {
        tokio::select! {
            result = self.receiver.recv() => result.ok(),
            _ = self.cancellation_token.cancelled() => None,
        }
    }

    /// Stop the subscription by triggering cancellation
    pub fn stop(&self) {
        self.cancellation_token.cancel();
        tracing::debug!("Event subscription stopped");
    }

    /// Check if the subscription has been stopped
    pub fn is_stopped(&self) -> bool {
        self.cancellation_token.is_cancelled()
    }
}

/// Transaction batch for executing multiple transactions
#[derive(Debug, Clone)]
pub struct TransactionBatch {
    transactions: VecDeque<crate::transaction::Transaction>,
}

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

impl TransactionBatch {
    /// Create a new empty transaction batch
    pub fn new() -> Self {
        Self {
            transactions: VecDeque::new(),
        }
    }

    /// Add a transaction to the batch
    pub fn add_transaction(&mut self, tx: crate::transaction::Transaction) {
        self.transactions.push_back(tx);
    }

    /// Get the number of transactions in the batch
    pub fn len(&self) -> usize {
        self.transactions.len()
    }

    /// Check if the batch is empty
    pub fn is_empty(&self) -> bool {
        self.transactions.is_empty()
    }

    /// Consume the batch and return the transactions as a vector
    pub fn into_transactions(self) -> Vec<crate::transaction::Transaction> {
        self.transactions.into_iter().collect()
    }
}

/// Result of a parallel batch execution
#[derive(Debug)]
pub struct BatchExecutionResult {
    /// Successfully executed transactions
    pub successes: Vec<crate::transaction::TransactionResult>,
    /// Failed transactions with their errors
    pub failures: Vec<(crate::transaction::Transaction, crate::error::Error)>,
    /// Total execution time in milliseconds
    pub execution_time_ms: u128,
}

impl BatchExecutionResult {
    /// Get the total number of transactions processed
    pub fn total(&self) -> usize {
        self.successes.len() + self.failures.len()
    }

    /// Get the number of successful transactions
    pub fn success_count(&self) -> usize {
        self.successes.len()
    }

    /// Get the number of failed transactions
    pub fn failure_count(&self) -> usize {
        self.failures.len()
    }

    /// Get the success rate as a percentage
    pub fn success_rate(&self) -> f64 {
        if self.total() == 0 {
            return 0.0;
        }
        (self.success_count() as f64 / self.total() as f64) * 100.0
    }
}

/// Parallel executor for high-throughput transaction execution
///
/// Executes multiple transactions concurrently using tokio tasks,
/// with configurable concurrency limits to prevent overwhelming the network.
///
/// # Example
///
/// ```rust,no_run
/// use apex_sdk::advanced::{ParallelExecutor, TransactionBatch};
/// use apex_sdk::ApexSDK;
/// use std::sync::Arc;
///
/// # async fn example(sdk: ApexSDK) -> Result<(), Box<dyn std::error::Error>> {
/// let mut batch = TransactionBatch::new();
/// // Add transactions to batch...
///
/// let sdk = Arc::new(sdk);
/// let executor = ParallelExecutor::new(sdk, 10); // Max 10 concurrent transactions
/// let result = executor.execute_batch(batch).await;
///
/// println!("Executed {} transactions", result.total());
/// println!("Success rate: {:.2}%", result.success_rate());
/// # Ok(())
/// # }
/// ```
pub struct ParallelExecutor {
    sdk: Arc<crate::sdk::ApexSDK>,
    concurrency: usize,
}

impl ParallelExecutor {
    /// Create a new parallel executor with the specified concurrency limit
    ///
    /// # Arguments
    ///
    /// * `sdk` - The Apex SDK instance to use for executing transactions
    /// * `concurrency` - Maximum number of concurrent transactions (recommended: 5-20)
    pub fn new(sdk: Arc<crate::sdk::ApexSDK>, concurrency: usize) -> Self {
        let concurrency = if concurrency == 0 { 1 } else { concurrency };
        Self { sdk, concurrency }
    }

    /// Execute a batch of transactions in parallel
    ///
    /// Transactions are executed concurrently up to the configured concurrency limit.
    /// Results include both successful and failed transactions, along with timing metrics.
    ///
    /// # Arguments
    ///
    /// * `batch` - The batch of transactions to execute
    ///
    /// # Returns
    ///
    /// A `BatchExecutionResult` containing successes, failures, and timing information
    pub async fn execute_batch(&self, batch: TransactionBatch) -> BatchExecutionResult {
        let start = std::time::Instant::now();
        let transactions = batch.into_transactions();

        if transactions.is_empty() {
            return BatchExecutionResult {
                successes: vec![],
                failures: vec![],
                execution_time_ms: 0,
            };
        }

        tracing::info!(
            "Executing batch of {} transactions with concurrency limit {}",
            transactions.len(),
            self.concurrency
        );

        let semaphore = Arc::new(tokio::sync::Semaphore::new(self.concurrency));
        let mut tasks = Vec::new();

        for tx in transactions {
            let sdk: Arc<ApexSDK> = Arc::clone(&self.sdk);
            let semaphore = Arc::clone(&semaphore);

            let task = tokio::spawn(async move {
                let _permit = semaphore
                    .acquire()
                    .await
                    .expect("Semaphore should not be closed during batch execution");

                let result = sdk.execute(tx.clone()).await;

                match result {
                    Ok(tx_result) => Ok(tx_result),
                    Err(e) => Err((tx, e)),
                }
            });

            tasks.push(task);
        }

        let mut successes = Vec::new();
        let mut failures = Vec::new();

        for task in tasks {
            match task.await {
                Ok(Ok(tx_result)) => successes.push(tx_result),
                Ok(Err((tx, error))) => failures.push((tx, error)),
                Err(join_error) => {
                    tracing::error!("Task join error: {}", join_error);
                }
            }
        }

        let execution_time_ms = start.elapsed().as_millis();

        tracing::info!(
            "Batch execution completed: {} successes, {} failures, {} ms",
            successes.len(),
            failures.len(),
            execution_time_ms
        );

        BatchExecutionResult {
            successes,
            failures,
            execution_time_ms,
        }
    }
}

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

    #[test]
    fn test_transaction_batch_add_and_len() {
        let mut batch = TransactionBatch::new();
        assert!(batch.is_empty());
        assert_eq!(batch.len(), 0);

        let tx = Transaction::builder()
            .from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbD")
            .to_evm_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
            .amount(1000)
            .build()
            .unwrap();

        batch.add_transaction(tx.clone());
        assert_eq!(batch.len(), 1);
        assert!(!batch.is_empty());

        batch.add_transaction(tx);
        assert_eq!(batch.len(), 2);
    }

    #[test]
    fn test_transaction_batch_into_transactions() {
        let mut batch = TransactionBatch::new();

        let tx = Transaction::builder()
            .from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbD")
            .to_evm_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
            .amount(1000)
            .build()
            .unwrap();

        batch.add_transaction(tx.clone());
        batch.add_transaction(tx);

        let transactions = batch.into_transactions();
        assert_eq!(transactions.len(), 2);
    }

    #[tokio::test]
    async fn test_block_subscription_stop() {
        let (_sender, cancellation_token, mut subscription) = BlockSubscription::new();

        assert!(!subscription.is_stopped());

        subscription.stop();

        assert!(subscription.is_stopped());

        assert!(cancellation_token.is_cancelled());

        let result = subscription.next().await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_block_subscription_receives_blocks() {
        let (sender, _cancellation_token, mut subscription) = BlockSubscription::new();

        let send_task = tokio::spawn(async move {
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            let _ = sender.send(BlockInfo {
                number: 100,
                hash: "0xabc123".to_string(),
                timestamp: 1234567890,
            });
        });

        let block = subscription.next().await;
        assert!(block.is_some());
        let block = block.unwrap();
        assert_eq!(block.number, 100);
        assert_eq!(block.hash, "0xabc123");

        send_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_event_subscription_stop() {
        let (_sender, cancellation_token, mut subscription) = EventSubscription::new();

        assert!(!subscription.is_stopped());

        subscription.stop();

        assert!(subscription.is_stopped());
        assert!(cancellation_token.is_cancelled());

        let result = subscription.next().await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_event_subscription_receives_events() {
        let (sender, _cancellation_token, mut subscription) = EventSubscription::new();

        let send_task = tokio::spawn(async move {
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            let _ = sender.send("TestEvent".to_string());
        });

        let event = subscription.next().await;
        assert!(event.is_some());
        assert_eq!(event.unwrap(), "TestEvent");

        send_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_subscription_multiple_events() {
        let (sender, _cancellation_token, mut subscription) = BlockSubscription::new();

        let send_task = tokio::spawn(async move {
            for i in 0..3 {
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                let _ = sender.send(BlockInfo {
                    number: i,
                    hash: format!("0x{:x}", i),
                    timestamp: 1000000 + i,
                });
            }
        });

        let mut received_blocks = Vec::new();
        for _ in 0..3 {
            if let Some(block) = subscription.next().await {
                received_blocks.push(block);
            }
        }

        assert_eq!(received_blocks.len(), 3);
        assert_eq!(received_blocks[0].number, 0);
        assert_eq!(received_blocks[1].number, 1);
        assert_eq!(received_blocks[2].number, 2);

        send_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_subscription_cancellation_via_token() {
        let (sender, cancellation_token, mut subscription) = BlockSubscription::new();

        let send_task = tokio::spawn(async move {
            for i in 0..10 {
                tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                if sender
                    .send(BlockInfo {
                        number: i,
                        hash: format!("0x{:x}", i),
                        timestamp: 1000000 + i,
                    })
                    .is_err()
                {
                    break; // All receivers dropped
                }
            }
        });

        let block1 = subscription.next().await;
        assert!(block1.is_some());
        assert_eq!(block1.unwrap().number, 0);

        cancellation_token.cancel();

        let block2 = subscription.next().await;
        assert!(block2.is_none());

        assert!(subscription.is_stopped());

        send_task.await.unwrap();
    }

    #[tokio::test]
    async fn test_multiple_subscribers() {
        let (sender, _token1, mut sub1) = BlockSubscription::new();
        let (sender2, _token2, mut sub2) = BlockSubscription::new();

        let task1 = tokio::spawn(async move {
            let _ = sender.send(BlockInfo {
                number: 100,
                hash: "0xabc".to_string(),
                timestamp: 2000000,
            });
        });

        let task2 = tokio::spawn(async move {
            let _ = sender2.send(BlockInfo {
                number: 200,
                hash: "0xdef".to_string(),
                timestamp: 3000000,
            });
        });

        let block1 = sub1.next().await;
        let block2 = sub2.next().await;

        assert!(block1.is_some());
        assert!(block2.is_some());
        assert_eq!(block1.unwrap().number, 100);
        assert_eq!(block2.unwrap().number, 200);

        task1.await.unwrap();
        task2.await.unwrap();
    }

    #[tokio::test]
    async fn test_subscription_timeout_behavior() {
        let (_sender, _token, mut subscription) = EventSubscription::new();

        let result =
            tokio::time::timeout(tokio::time::Duration::from_millis(100), subscription.next())
                .await;

        assert!(result.is_err()); // Should timeout
    }

    #[tokio::test]
    async fn test_subscription_drop_handling() {
        let (sender, _token, subscription) = EventSubscription::new();

        drop(subscription);

        let send_result = sender.send("test".to_string());
        assert_eq!(send_result.err().unwrap().0, "test");
    }

    #[test]
    fn test_batch_execution_result_empty() {
        let result = BatchExecutionResult {
            successes: vec![],
            failures: vec![],
            execution_time_ms: 0,
        };

        assert_eq!(result.total(), 0);
        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 0);
        assert_eq!(result.success_rate(), 0.0);
    }

    #[test]
    fn test_batch_execution_result_success_rate() {
        use crate::transaction::{TransactionResult, TransactionStatus};

        let result = BatchExecutionResult {
            successes: vec![
                TransactionResult::new("0x1".to_string()).with_status(TransactionStatus::Success),
                TransactionResult::new("0x2".to_string()).with_status(TransactionStatus::Success),
                TransactionResult::new("0x3".to_string()).with_status(TransactionStatus::Success),
            ],
            failures: vec![(
                Transaction::builder()
                    .from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbD")
                    .to_evm_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
                    .amount(1000)
                    .build()
                    .unwrap(),
                crate::error::Error::Transaction("Test error".to_string()),
            )],
            execution_time_ms: 1000,
        };

        assert_eq!(result.total(), 4);
        assert_eq!(result.success_count(), 3);
        assert_eq!(result.failure_count(), 1);
        assert_eq!(result.success_rate(), 75.0);
    }

    #[test]
    #[cfg(not(all(feature = "substrate", feature = "evm")))]
    fn test_parallel_executor_concurrency_limit() {
        let sdk = std::sync::Arc::new(
            crate::sdk::ApexSDK::new(
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "evm")]
                None,
                #[cfg(feature = "evm")]
                None,
                std::time::Duration::from_secs(30),
            )
            .unwrap(),
        );

        let executor_zero = ParallelExecutor::new(sdk.clone(), 0);
        assert_eq!(executor_zero.concurrency, 1);

        let executor_normal = ParallelExecutor::new(sdk.clone(), 10);
        assert_eq!(executor_normal.concurrency, 10);
    }

    #[tokio::test]
    #[cfg(not(all(feature = "substrate", feature = "evm")))]
    async fn test_parallel_executor_empty_batch() {
        let sdk = std::sync::Arc::new(
            crate::sdk::ApexSDK::new(
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "evm")]
                None,
                #[cfg(feature = "evm")]
                None,
                std::time::Duration::from_secs(30),
            )
            .unwrap(),
        );

        let executor = ParallelExecutor::new(sdk, 5);
        let batch = TransactionBatch::new();

        let result = executor.execute_batch(batch).await;

        assert_eq!(result.total(), 0);
        assert_eq!(result.success_count(), 0);
        assert_eq!(result.failure_count(), 0);
        assert_eq!(result.success_rate(), 0.0);
        assert_eq!(result.execution_time_ms, 0);
    }

    #[tokio::test]
    #[cfg(not(all(feature = "substrate", feature = "evm")))]
    async fn test_parallel_executor_metrics() {
        let sdk = std::sync::Arc::new(
            crate::sdk::ApexSDK::new(
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "substrate")]
                None,
                #[cfg(feature = "evm")]
                None,
                #[cfg(feature = "evm")]
                None,
                std::time::Duration::from_secs(30),
            )
            .unwrap(),
        );

        let executor = ParallelExecutor::new(sdk, 5);
        let batch = TransactionBatch::new(); // Empty batch

        let result = executor.execute_batch(batch).await;

        assert_eq!(
            result.total(),
            result.success_count() + result.failure_count()
        );
        assert!(result.execution_time_ms < 1000); // Should complete quickly for empty batch
    }

    #[test]
    fn test_batch_execution_result_metrics() {
        use crate::transaction::{TransactionResult, TransactionStatus};

        let all_success = BatchExecutionResult {
            successes: vec![
                TransactionResult::new("0x1".to_string()).with_status(TransactionStatus::Success),
                TransactionResult::new("0x2".to_string()).with_status(TransactionStatus::Success),
            ],
            failures: vec![],
            execution_time_ms: 500,
        };

        assert_eq!(all_success.success_rate(), 100.0);
        assert_eq!(all_success.total(), 2);
        assert_eq!(all_success.failure_count(), 0);

        let all_failures = BatchExecutionResult {
            successes: vec![],
            failures: vec![
                (
                    Transaction::builder()
                        .from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbD")
                        .to_evm_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
                        .amount(1000)
                        .build()
                        .unwrap(),
                    crate::error::Error::Transaction("Error 1".to_string()),
                ),
                (
                    Transaction::builder()
                        .from_evm_address("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbD")
                        .to_evm_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
                        .amount(2000)
                        .build()
                        .unwrap(),
                    crate::error::Error::Transaction("Error 2".to_string()),
                ),
            ],
            execution_time_ms: 750,
        };

        assert_eq!(all_failures.success_rate(), 0.0);
        assert_eq!(all_failures.total(), 2);
        assert_eq!(all_failures.success_count(), 0);
    }
}