sp1_sdk/network/validation.rs
1//! # Network Validation
2//!
3//! This module provides validation functions for the network sdk.
4
5use super::{FulfillmentStrategy, NetworkMode};
6
7/// Errors that can occur during network validation.
8#[derive(Debug, thiserror::Error)]
9pub enum ValidationError {
10 /// The fulfillment strategy is not compatible with the specified network mode.
11 #[error("FulfillmentStrategy::{strategy:?} is not compatible with NetworkMode::{mode:?}")]
12 IncompatibleStrategy {
13 /// The fulfillment strategy that was attempted.
14 strategy: FulfillmentStrategy,
15 /// The network mode that was specified.
16 mode: NetworkMode,
17 },
18}
19
20/// Validates that the given fulfillment strategy is compatible with the specified network mode.
21///
22/// # Arguments
23///
24/// * `mode` - The network mode (Mainnet or Reserved)
25/// * `strategy` - The fulfillment strategy to validate
26///
27/// # Returns
28///
29/// Returns `Ok(())` if the strategy is compatible with the mode, otherwise returns
30/// a `ValidationError::IncompatibleStrategy`.
31///
32/// # Examples
33///
34/// ```
35/// use sp1_sdk::network::{
36/// validation::validate_strategy_compatibility, FulfillmentStrategy, NetworkMode,
37/// };
38///
39/// // Valid combination
40/// assert!(
41/// validate_strategy_compatibility(NetworkMode::Mainnet, FulfillmentStrategy::Auction).is_ok()
42/// );
43///
44/// // Invalid combination
45/// assert!(
46/// validate_strategy_compatibility(NetworkMode::Mainnet, FulfillmentStrategy::Hosted).is_err()
47/// );
48/// ```
49pub fn validate_strategy_compatibility(
50 mode: NetworkMode,
51 strategy: FulfillmentStrategy,
52) -> Result<(), ValidationError> {
53 match (mode, strategy) {
54 // Valid combinations.
55 (NetworkMode::Mainnet, FulfillmentStrategy::Auction) |
56 (NetworkMode::Reserved, FulfillmentStrategy::Hosted | FulfillmentStrategy::Reserved) => {
57 Ok(())
58 }
59
60 // Invalid combinations.
61 (mode, strategy) => Err(ValidationError::IncompatibleStrategy { strategy, mode }),
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_valid_combinations() {
71 assert!(validate_strategy_compatibility(
72 NetworkMode::Mainnet,
73 FulfillmentStrategy::Auction
74 )
75 .is_ok());
76 assert!(validate_strategy_compatibility(
77 NetworkMode::Reserved,
78 FulfillmentStrategy::Hosted
79 )
80 .is_ok());
81 assert!(validate_strategy_compatibility(
82 NetworkMode::Reserved,
83 FulfillmentStrategy::Reserved
84 )
85 .is_ok());
86 }
87
88 #[test]
89 fn test_invalid_combinations() {
90 assert!(validate_strategy_compatibility(
91 NetworkMode::Reserved,
92 FulfillmentStrategy::Auction
93 )
94 .is_err());
95 assert!(validate_strategy_compatibility(NetworkMode::Mainnet, FulfillmentStrategy::Hosted)
96 .is_err());
97 assert!(validate_strategy_compatibility(
98 NetworkMode::Mainnet,
99 FulfillmentStrategy::Reserved
100 )
101 .is_err());
102 }
103}