Skip to main content

dpp/data_contract/methods/registration_cost/
mod.rs

1mod v1;
2mod v2;
3
4use crate::data_contract::serialized_version::DataContractInSerializationFormat;
5use crate::data_contract::DataContract;
6use crate::ProtocolError;
7use platform_version::version::PlatformVersion;
8
9impl DataContract {
10    /// Returns the registration cost of the data contract based on the current platform version
11    /// and the number of associated search keywords.
12    ///
13    /// This method dispatches to a version-specific implementation based on the
14    /// platform version configuration. If the version is unrecognized, it returns a version mismatch error.
15    ///
16    /// # Arguments
17    /// - `platform_version`: A reference to the platform version, used to determine which
18    ///   registration cost algorithm to apply.
19    ///
20    /// # Returns
21    /// - `Ok(u64)`: The total registration cost in credits for this contract.
22    /// - `Err(ProtocolError)`: If the platform version is unrecognized or if the fee computation overflows.
23    ///
24    /// # Version Behavior
25    /// - Version 0: Always returns `0` (used before protocol version 9, ie before 2.0, where registration cost was not charged).
26    /// - Version 1: Uses a detailed cost model based on document types, indexes, tokens, and keyword count.
27    /// - Version 2: Version 1 plus the surcharge for tokens with a once-per-identity distribution (protocol version 14).
28    pub fn registration_cost(
29        &self,
30        platform_version: &PlatformVersion,
31    ) -> Result<u64, ProtocolError> {
32        match platform_version
33            .dpp
34            .contract_versions
35            .methods
36            .registration_cost
37        {
38            0 => Ok(0), // Before 2.0 it's just 0 (There was some validation cost)
39            1 => Ok(self.registration_cost_v1(platform_version)),
40            2 => Ok(self.registration_cost_v2(platform_version)),
41            version => Err(ProtocolError::UnknownVersionMismatch {
42                method: "DataContract::registration_cost".to_string(),
43                known_versions: vec![0, 1, 2],
44                received: version,
45            }),
46        }
47    }
48}
49
50impl DataContractInSerializationFormat {
51    /// Returns the registration cost of the data contract based on the current platform version
52    /// and the number of associated search keywords.
53    ///
54    /// This method dispatches to a version-specific implementation based on the
55    /// platform version configuration. If the version is unrecognized, it returns a version mismatch error.
56    ///
57    /// # Arguments
58    /// - `platform_version`: A reference to the platform version, used to determine which
59    ///   registration cost algorithm to apply.
60    ///
61    /// # Returns
62    /// - `Ok(u64)`: The total registration cost in credits for this contract.
63    /// - `Err(ProtocolError)`: If the platform version is unrecognized or if the fee computation overflows.
64    ///
65    /// # Version Behavior
66    /// - Version 0: Always returns `0` (used before protocol version 9, ie before 2.0, where registration cost was not charged).
67    /// - Version 1: Uses a detailed cost model based on document types, indexes, tokens, and keyword count.
68    /// - Version 2: Version 1 plus the surcharge for tokens with a once-per-identity distribution (protocol version 14).
69    pub fn registration_cost(
70        &self,
71        platform_version: &PlatformVersion,
72    ) -> Result<u64, ProtocolError> {
73        match platform_version
74            .dpp
75            .contract_versions
76            .methods
77            .registration_cost
78        {
79            0 => Ok(0), // Before 2.0 it's just 0 (There was some validation cost)
80            1 => Ok(self.registration_cost_v1(platform_version)),
81            2 => Ok(self.registration_cost_v2(platform_version)),
82            version => Err(ProtocolError::UnknownVersionMismatch {
83                method: "DataContract::registration_cost".to_string(),
84                known_versions: vec![0, 1, 2],
85                received: version,
86            }),
87        }
88    }
89}