drive/verify/address_funds/verify_addresses_infos/mod.rs
1mod v0;
2
3use crate::drive::Drive;
4use crate::error::drive::DriveError;
5use crate::error::Error;
6use crate::verify::RootHash;
7use dpp::address_funds::PlatformAddress;
8use dpp::fee::Credits;
9use dpp::prelude::AddressNonce;
10use dpp::version::PlatformVersion;
11
12impl Drive {
13 /// Verifies the proof of multiple addresses' balance and nonce information.
14 ///
15 /// This method validates and extracts balance and nonce information for multiple addresses based on the provided proof.
16 /// It uses the proof to confirm the integrity and authenticity of the address data. The method supports
17 /// different versions for backward compatibility and forwards the verification logic to the appropriate versioned implementation.
18 ///
19 /// # Type Parameters
20 /// - `T`: The output container type that implements `FromIterator`. This is used to collect the verified address information
21 /// as pairs of [`PlatformAddress`] and `Option<(AddressNonce, Credits)>`.
22 ///
23 /// # Arguments
24 /// - `proof`: A byte slice containing the cryptographic proof for the address information.
25 /// - `addresses`: An iterator over the platform addresses to verify.
26 /// - `verify_subset_of_proof`: A boolean flag indicating whether to verify only a subset of the proof (useful for optimizations).
27 /// - `platform_version`: A reference to the platform version, used to determine the appropriate versioned implementation.
28 ///
29 /// # Returns
30 /// - `Ok((RootHash, T))`: On success, returns a tuple containing:
31 /// - `RootHash`: The root hash of the Merkle tree, confirming the proof's validity.
32 /// - `T`: A collection of verified address information as pairs of [`PlatformAddress`] and `Option<(AddressNonce, Credits)>`.
33 /// - `Err(Error)`: If verification fails, returns an [`Error`] indicating the cause of failure.
34 ///
35 /// # Errors
36 /// - [`Error::Proof`]: If the proof is invalid, corrupted, or contains unexpected data structures.
37 /// - [`Error::Drive(DriveError::UnknownVersionMismatch)`]: If the method is called with an unsupported platform version.
38 /// - Any other errors propagated from the versioned implementation.
39 pub fn verify_addresses_infos<
40 'a,
41 I: IntoIterator<Item = &'a PlatformAddress>,
42 T: FromIterator<(PlatformAddress, Option<(AddressNonce, Credits)>)>,
43 >(
44 proof: &[u8],
45 addresses: I,
46 verify_subset_of_proof: bool,
47 platform_version: &PlatformVersion,
48 ) -> Result<(RootHash, T), Error> {
49 match platform_version
50 .drive
51 .methods
52 .verify
53 .address_funds
54 .verify_addresses_infos
55 {
56 0 => Self::verify_addresses_infos_v0(
57 proof,
58 addresses,
59 verify_subset_of_proof,
60 platform_version,
61 ),
62 version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
63 method: "verify_addresses_infos".to_string(),
64 known_versions: vec![0],
65 received: version,
66 })),
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use dpp::address_funds::PlatformAddress;
75 use dpp::version::PlatformVersion;
76
77 #[test]
78 fn test_verify_addresses_infos_unknown_version_mismatch() {
79 let mut platform_version = PlatformVersion::latest().clone();
80 platform_version
81 .drive
82 .methods
83 .verify
84 .address_funds
85 .verify_addresses_infos = 255;
86
87 let addresses: Vec<PlatformAddress> = vec![];
88
89 let result = Drive::verify_addresses_infos::<
90 std::slice::Iter<PlatformAddress>,
91 Vec<(PlatformAddress, Option<(AddressNonce, Credits)>)>,
92 >(&[], addresses.iter(), false, &platform_version);
93
94 assert!(
95 matches!(
96 result,
97 Err(Error::Drive(DriveError::UnknownVersionMismatch { .. }))
98 ),
99 "expected UnknownVersionMismatch, got {:?}",
100 result,
101 );
102 }
103}