-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed a few issues and implemented verify methods
- Loading branch information
1 parent
6a1f1dd
commit 1548233
Showing
10 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
mod verify_epoch_infos; | ||
mod verify_upgrade_state; | ||
mod verify_upgrade_vote_status; |
51 changes: 51 additions & 0 deletions
51
packages/rs-drive/src/drive/verify/system/verify_upgrade_state/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::drive::verify::RootHash; | ||
use crate::drive::Drive; | ||
use crate::error::drive::DriveError; | ||
use crate::error::Error; | ||
use dpp::block::epoch::EpochIndex; | ||
use dpp::block::extended_epoch_info::ExtendedEpochInfo; | ||
use dpp::util::deserializer::ProtocolVersion; | ||
use dpp::version::PlatformVersion; | ||
use nohash_hasher::IntMap; | ||
|
||
mod v0; | ||
|
||
impl Drive { | ||
/// Verifies a proof containing the current upgrade state. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `proof`: A byte slice representing the proof to be verified. | ||
/// - `platform_version`: the platform version, | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` with a tuple of `RootHash` and `Vec<ExtendedEpochInfo>`. The `Vec<ExtendedEpochInfo>` | ||
/// represents verified epoch information if it exists. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an `Error` if: | ||
/// | ||
/// - The proof is corrupted. | ||
/// - The GroveDb query fails. | ||
pub fn verify_upgrade_state( | ||
proof: &[u8], | ||
platform_version: &PlatformVersion, | ||
) -> Result<(RootHash, IntMap<ProtocolVersion, u64>), Error> { | ||
match platform_version | ||
.drive | ||
.methods | ||
.verify | ||
.system | ||
.verify_upgrade_state | ||
{ | ||
0 => Drive::verify_upgrade_state_v0(proof), | ||
version => Err(Error::Drive(DriveError::UnknownVersionMismatch { | ||
method: "verify_upgrade_state".to_string(), | ||
known_versions: vec![0], | ||
received: version, | ||
})), | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/rs-drive/src/drive/verify/system/verify_upgrade_state/v0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::drive::protocol_upgrade::versions_counter_path_vec; | ||
use crate::drive::verify::RootHash; | ||
use crate::drive::Drive; | ||
use crate::error::proof::ProofError; | ||
use crate::error::Error; | ||
use crate::query::{Query, QueryItem}; | ||
use dpp::util::deserializer::ProtocolVersion; | ||
use grovedb::{GroveDb, PathQuery}; | ||
use integer_encoding::VarInt; | ||
use nohash_hasher::IntMap; | ||
use std::ops::RangeFull; | ||
impl Drive { | ||
/// Verifies a proof containing the current upgrade state. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `proof`: A byte slice representing the proof to be verified. | ||
/// - `platform_version`: the platform version, | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` with a tuple of `RootHash` and `IntMap<ProtocolVersion, u64>`. The `IntMap<ProtocolVersion, u64>` | ||
/// represents vote count of each version in the current epoch. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an `Error` if: | ||
/// | ||
/// - The proof is corrupted. | ||
/// - The GroveDb query fails. | ||
pub(super) fn verify_upgrade_state_v0( | ||
proof: &[u8], | ||
) -> Result<(RootHash, IntMap<ProtocolVersion, u64>), Error> { | ||
let path_query = PathQuery::new_unsized( | ||
versions_counter_path_vec(), | ||
Query::new_single_query_item(QueryItem::RangeFull(RangeFull)), | ||
); | ||
|
||
let (root_hash, elements) = GroveDb::verify_query(proof, &path_query)?; | ||
|
||
let protocol_version_map = elements | ||
.into_iter() | ||
.map(|(_, key, element)| { | ||
let version = ProtocolVersion::decode_var(key.as_slice()) | ||
.ok_or(ProofError::CorruptedProof("protocol version not decodable"))? | ||
.0; | ||
let element = element.ok_or(ProofError::CorruptedProof( | ||
"expected a count for each version, got none", | ||
))?; | ||
let count_bytes = element.as_item_bytes().map_err(|_| { | ||
ProofError::CorruptedProof("expected an item for the element of a version") | ||
})?; | ||
let count = u64::decode_var(count_bytes) | ||
.ok_or(ProofError::CorruptedProof("version count not decodable"))? | ||
.0; | ||
Ok((version, count)) | ||
}) | ||
.collect::<Result<IntMap<ProtocolVersion, u64>, Error>>()?; | ||
|
||
Ok((root_hash, protocol_version_map)) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/rs-drive/src/drive/verify/system/verify_upgrade_vote_status/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
mod v0; | ||
|
||
use crate::drive::verify::RootHash; | ||
use crate::drive::Drive; | ||
use crate::error::drive::DriveError; | ||
use crate::error::Error; | ||
use dpp::util::deserializer::ProtocolVersion; | ||
use dpp::version::PlatformVersion; | ||
use std::collections::BTreeMap; | ||
|
||
impl Drive { | ||
/// Verifies a proof containing potentially multiple epoch infos. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `proof`: A byte slice representing the proof to be verified. | ||
/// - `first_pro_tx_hash`: the first pro tx hash that we are querying for. | ||
/// - `count`: the amount of Evonodes that we want to retrieve. | ||
/// - `platform_version`: the platform version, | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` with a tuple of `RootHash` and `BTreeMap<[u8;32], ProtocolVersion>`. The `BTreeMap<[u8;32], ProtocolVersion>` | ||
/// represents a map of the version that each Evonode has voted for. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an `Error` if: | ||
/// | ||
/// - The proof is corrupted. | ||
/// - The GroveDb query fails. | ||
pub fn verify_upgrade_vote_status( | ||
proof: &[u8], | ||
start_protx_hash: Option<[u8; 32]>, | ||
count: u16, | ||
platform_version: &PlatformVersion, | ||
) -> Result<(RootHash, BTreeMap<[u8; 32], ProtocolVersion>), Error> { | ||
match platform_version | ||
.drive | ||
.methods | ||
.verify | ||
.system | ||
.verify_upgrade_vote_status | ||
{ | ||
0 => Drive::verify_upgrade_vote_status_v0(proof, start_protx_hash, count), | ||
version => Err(Error::Drive(DriveError::UnknownVersionMismatch { | ||
method: "verify_upgrade_vote_status".to_string(), | ||
known_versions: vec![0], | ||
received: version, | ||
})), | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/rs-drive/src/drive/verify/system/verify_upgrade_vote_status/v0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::drive::protocol_upgrade::{ | ||
desired_version_for_validators_path_vec, versions_counter_path_vec, | ||
}; | ||
use crate::drive::verify::RootHash; | ||
use crate::drive::Drive; | ||
use crate::error::proof::ProofError; | ||
use crate::error::Error; | ||
use crate::query::{Query, QueryItem}; | ||
use dpp::util::deserializer::ProtocolVersion; | ||
use grovedb::{GroveDb, PathQuery, SizedQuery}; | ||
use integer_encoding::VarInt; | ||
use std::collections::BTreeMap; | ||
use std::ops::RangeFull; | ||
impl Drive { | ||
/// Verifies a proof containing the current upgrade state. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `proof`: A byte slice representing the proof to be verified. | ||
/// - `first_pro_tx_hash`: the first pro tx hash that we are querying for. | ||
/// - `count`: the amount of Evonodes that we want to retrieve. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` with a tuple of `RootHash` and `BTreeMap<[u8;32], ProtocolVersion>`. The `BTreeMap<[u8;32], ProtocolVersion>` | ||
/// represents a map of the version that each Evonode has voted for. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an `Error` if: | ||
/// | ||
/// - The proof is corrupted. | ||
/// - The GroveDb query fails. | ||
pub(super) fn verify_upgrade_vote_status_v0( | ||
proof: &[u8], | ||
start_protx_hash: Option<[u8; 32]>, | ||
count: u16, | ||
) -> Result<(RootHash, BTreeMap<[u8; 32], ProtocolVersion>), Error> { | ||
let path = desired_version_for_validators_path_vec(); | ||
|
||
let query_item = if let Some(start_protx_hash) = start_protx_hash { | ||
QueryItem::RangeFrom(start_protx_hash.to_vec()..) | ||
} else { | ||
QueryItem::RangeFull(RangeFull) | ||
}; | ||
|
||
let path_query = PathQuery::new( | ||
path, | ||
SizedQuery::new(Query::new_single_query_item(query_item), Some(count), None), | ||
); | ||
|
||
let (root_hash, elements) = GroveDb::verify_query(proof, &path_query)?; | ||
|
||
let protocol_version_map = elements | ||
.into_iter() | ||
.map(|(_, key, element)| { | ||
let pro_tx_hash: [u8; 32] = key | ||
.try_into() | ||
.map_err(|_| ProofError::CorruptedProof("protocol version not decodable"))?; | ||
let element = element.ok_or(ProofError::CorruptedProof( | ||
"expected a count for each version, got none", | ||
))?; | ||
let version_bytes = element.as_item_bytes().map_err(|_| { | ||
ProofError::CorruptedProof("expected an item for the element of a version") | ||
})?; | ||
let version = u32::decode_var(version_bytes) | ||
.ok_or(ProofError::CorruptedProof("version count not decodable"))? | ||
.0; | ||
Ok((pro_tx_hash, version)) | ||
}) | ||
.collect::<Result<BTreeMap<[u8; 32], ProtocolVersion>, Error>>()?; | ||
|
||
Ok((root_hash, protocol_version_map)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters