From 09d89f86d07077246755dc722ba9fb8cfe403585 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Thu, 3 Oct 2024 13:40:16 +0700 Subject: [PATCH] fix(sdk): small sdk improvements and fixes for v1.4 (#2200) --- .../rs-dpp/src/core_types/validator/v0/mod.rs | 3 +- .../src/core_types/validator_set/mod.rs | 9 +++++ .../src/core_types/validator_set/v0/mod.rs | 33 ++++++++++++++++++- .../rs-drive-proof-verifier/src/unproved.rs | 2 +- packages/rs-sdk/src/mock/requests.rs | 25 ++++++++++++++ packages/rs-sdk/src/platform/fetch_many.rs | 26 +++++++++++---- packages/rs-sdk/src/platform/query.rs | 28 +++++++++++++--- packages/withdrawals-contract/src/lib.rs | 14 ++++++++ 8 files changed, 127 insertions(+), 13 deletions(-) diff --git a/packages/rs-dpp/src/core_types/validator/v0/mod.rs b/packages/rs-dpp/src/core_types/validator/v0/mod.rs index daadc197fe..d472b4a145 100644 --- a/packages/rs-dpp/src/core_types/validator/v0/mod.rs +++ b/packages/rs-dpp/src/core_types/validator/v0/mod.rs @@ -1,5 +1,6 @@ use dashcore::{ProTxHash, PubkeyHash}; -use std::fmt::{Debug, Formatter}; +use std::fmt; +use std::fmt::{Debug, Display, Formatter}; use crate::bls_signatures::PublicKey as BlsPublicKey; #[cfg(feature = "core-types-serde-conversion")] diff --git a/packages/rs-dpp/src/core_types/validator_set/mod.rs b/packages/rs-dpp/src/core_types/validator_set/mod.rs index 7238bc0b1b..74b78fca05 100644 --- a/packages/rs-dpp/src/core_types/validator_set/mod.rs +++ b/packages/rs-dpp/src/core_types/validator_set/mod.rs @@ -13,6 +13,7 @@ use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize}; #[cfg(feature = "core-types-serde-conversion")] use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; +use std::fmt::{Display, Formatter}; /// Version 0 pub mod v0; @@ -34,6 +35,14 @@ pub enum ValidatorSet { V0(ValidatorSetV0), } +impl Display for ValidatorSet { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ValidatorSet::V0(v0) => write!(f, "{}", v0), + } + } +} + impl ValidatorSetV0Getters for ValidatorSet { fn quorum_hash(&self) -> &QuorumHash { match self { diff --git a/packages/rs-dpp/src/core_types/validator_set/v0/mod.rs b/packages/rs-dpp/src/core_types/validator_set/v0/mod.rs index 886489ee3b..2cf167277b 100644 --- a/packages/rs-dpp/src/core_types/validator_set/v0/mod.rs +++ b/packages/rs-dpp/src/core_types/validator_set/v0/mod.rs @@ -10,10 +10,12 @@ use bincode::error::EncodeError; use bincode::{BorrowDecode, Decode, Encode}; use dashcore::hashes::Hash; use dashcore::{ProTxHash, QuorumHash}; +use itertools::Itertools; #[cfg(feature = "core-types-serde-conversion")] use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; -use std::fmt::{Debug, Formatter}; +use std::fmt; +use std::fmt::{Debug, Display, Formatter}; /// The validator set is only slightly different from a quorum as it does not contain non valid /// members @@ -35,6 +37,35 @@ pub struct ValidatorSetV0 { pub threshold_public_key: BlsPublicKey, } +impl Display for ValidatorSetV0 { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!( + f, + "ValidatorSet {{ + quorum_hash: {}, + quorum_index: {}, + core_height: {}, + members: [{}], + threshold_public_key: {} +}}", + hex::encode(self.quorum_hash), // Assuming QuorumHash is a byte array and should be in hex format + match self.quorum_index { + Some(index) => index.to_string(), + None => "None".to_string(), + }, + self.core_height, + self.members + .iter() + .map(|(pro_tx_hash, validator)| format!( + "{{{}: {}}}", + pro_tx_hash, validator.node_ip + )) + .join(", "), + hex::encode(self.threshold_public_key.to_bytes().as_slice()) // Assuming BlsPublicKey is a byte array + ) + } +} + #[cfg(feature = "core-types-serialization")] impl Encode for ValidatorSetV0 { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { diff --git a/packages/rs-drive-proof-verifier/src/unproved.rs b/packages/rs-drive-proof-verifier/src/unproved.rs index d9fd37009c..bc2f2e9e51 100644 --- a/packages/rs-drive-proof-verifier/src/unproved.rs +++ b/packages/rs-drive-proof-verifier/src/unproved.rs @@ -231,7 +231,7 @@ impl FromUnproved for CurrentQuorumsInfo pro_tx_hash, public_key: None, // Assuming it's not provided here node_ip: member.node_ip, - node_id: PubkeyHash::from_slice(&[0; 32]).unwrap(), // Placeholder, since not provided + node_id: PubkeyHash::from_slice(&[0; 20]).expect("expected to make pub key hash from 20 byte empty array"), // Placeholder, since not provided core_port: 0, // Placeholder, since not provided platform_http_port: 0, // Placeholder, since not provided platform_p2p_port: 0, // Placeholder, since not provided diff --git a/packages/rs-sdk/src/mock/requests.rs b/packages/rs-sdk/src/mock/requests.rs index d29e48d410..0b2e43692f 100644 --- a/packages/rs-sdk/src/mock/requests.rs +++ b/packages/rs-sdk/src/mock/requests.rs @@ -1,4 +1,5 @@ use super::MockDashPlatformSdk; +use dpp::bincode::config::standard; use dpp::{ bincode, block::extended_epoch_info::ExtendedEpochInfo, @@ -14,6 +15,7 @@ use dpp::{ }, voting::votes::{resource_vote::ResourceVote, Vote}, }; +use drive::grovedb::Element; use drive_proof_verifier::types::{ Contenders, ContestedResources, CurrentQuorumsInfo, ElementFetchRequestItem, EvoNodeStatus, IdentityBalanceAndRevision, MasternodeProtocolVote, PrefundedSpecializedBalance, @@ -165,6 +167,29 @@ impl MockResponse for Document { } } +impl MockResponse for Element { + fn mock_serialize(&self, _sdk: &MockDashPlatformSdk) -> Vec { + // Create a bincode configuration + let config = standard(); + + // Serialize using the specified configuration + bincode::encode_to_vec(self, config).expect("Failed to serialize Element") + } + + fn mock_deserialize(_sdk: &MockDashPlatformSdk, buf: &[u8]) -> Self + where + Self: Sized, + { + // Create a bincode configuration + let config = standard(); + + // Deserialize using the specified configuration + bincode::decode_from_slice(buf, config) + .expect("Failed to deserialize Element") + .0 + } +} + impl MockResponse for drive_proof_verifier::types::IdentityNonceFetcher { fn mock_serialize(&self, _sdk: &MockDashPlatformSdk) -> Vec { (self.0).to_be_bytes().to_vec() diff --git a/packages/rs-sdk/src/platform/fetch_many.rs b/packages/rs-sdk/src/platform/fetch_many.rs index feba82fd65..897be764ae 100644 --- a/packages/rs-sdk/src/platform/fetch_many.rs +++ b/packages/rs-sdk/src/platform/fetch_many.rs @@ -16,8 +16,9 @@ use dapi_grpc::platform::v0::{ GetContestedResourceVotersForIdentityRequest, GetContestedResourcesRequest, GetDataContractsRequest, GetDocumentsResponse, GetEpochsInfoRequest, GetEvonodesProposedEpochBlocksByIdsRequest, GetEvonodesProposedEpochBlocksByRangeRequest, - GetIdentitiesBalancesRequest, GetIdentityKeysRequest, GetProtocolVersionUpgradeStateRequest, - GetProtocolVersionUpgradeVoteStatusRequest, GetVotePollsByEndDateRequest, + GetIdentitiesBalancesRequest, GetIdentityKeysRequest, GetPathElementsRequest, + GetProtocolVersionUpgradeStateRequest, GetProtocolVersionUpgradeVoteStatusRequest, + GetVotePollsByEndDateRequest, }; use dashcore_rpc::dashcore::ProTxHash; use dpp::data_contract::DataContract; @@ -30,11 +31,14 @@ use dpp::{ block::extended_epoch_info::ExtendedEpochInfo, voting::votes::resource_vote::ResourceVote, }; use dpp::{document::Document, voting::contender_structs::ContenderWithSerializedDocument}; +use drive::grovedb::query_result_type::{Key, Path}; +use drive::grovedb::Element; use drive_proof_verifier::types::{ - Contenders, ContestedResource, ContestedResources, DataContracts, ExtendedEpochInfos, - IdentityBalances, IdentityPublicKeys, MasternodeProtocolVote, MasternodeProtocolVotes, - ProposerBlockCountById, ProposerBlockCountByRange, ProposerBlockCounts, - ProtocolVersionUpgrades, ResourceVotesByIdentity, VotePollsGroupedByTimestamp, Voter, Voters, + Contenders, ContestedResource, ContestedResources, DataContracts, Elements, ExtendedEpochInfos, + IdentityBalances, IdentityPublicKeys, KeysInPath, MasternodeProtocolVote, + MasternodeProtocolVotes, ProposerBlockCountById, ProposerBlockCountByRange, + ProposerBlockCounts, ProtocolVersionUpgrades, ResourceVotesByIdentity, + VotePollsGroupedByTimestamp, Voter, Voters, }; use drive_proof_verifier::{types::Documents, FromProof}; use rs_dapi_client::{transport::TransportRequest, DapiRequest, RequestSettings}; @@ -415,3 +419,13 @@ impl FetchMany for VotePoll { impl FetchMany for drive_proof_verifier::types::IdentityBalance { type Request = GetIdentitiesBalancesRequest; } + +// +/// Fetch multiple elements. +/// +/// ## Supported query types +/// +/// * [KeysInPath] +impl FetchMany for Element { + type Request = GetPathElementsRequest; +} diff --git a/packages/rs-sdk/src/platform/query.rs b/packages/rs-sdk/src/platform/query.rs index 287f8b6951..a145d89058 100644 --- a/packages/rs-sdk/src/platform/query.rs +++ b/packages/rs-sdk/src/platform/query.rs @@ -8,15 +8,17 @@ use dapi_grpc::mock::Mockable; use dapi_grpc::platform::v0::get_contested_resource_identity_votes_request::GetContestedResourceIdentityVotesRequestV0; use dapi_grpc::platform::v0::get_contested_resource_voters_for_identity_request::GetContestedResourceVotersForIdentityRequestV0; use dapi_grpc::platform::v0::get_contested_resources_request::GetContestedResourcesRequestV0; +use dapi_grpc::platform::v0::get_current_quorums_info_request::GetCurrentQuorumsInfoRequestV0; use dapi_grpc::platform::v0::get_evonodes_proposed_epoch_blocks_by_range_request::GetEvonodesProposedEpochBlocksByRangeRequestV0; use dapi_grpc::platform::v0::get_path_elements_request::GetPathElementsRequestV0; use dapi_grpc::platform::v0::get_status_request::GetStatusRequestV0; use dapi_grpc::platform::v0::get_total_credits_in_platform_request::GetTotalCreditsInPlatformRequestV0; use dapi_grpc::platform::v0::{ - self as proto, get_identity_keys_request, get_identity_keys_request::GetIdentityKeysRequestV0, - get_path_elements_request, get_total_credits_in_platform_request, AllKeys, - GetContestedResourceVoteStateRequest, GetContestedResourceVotersForIdentityRequest, - GetContestedResourcesRequest, GetEpochsInfoRequest, + self as proto, get_current_quorums_info_request, get_identity_keys_request, + get_identity_keys_request::GetIdentityKeysRequestV0, get_path_elements_request, + get_total_credits_in_platform_request, AllKeys, GetContestedResourceVoteStateRequest, + GetContestedResourceVotersForIdentityRequest, GetContestedResourcesRequest, + GetCurrentQuorumsInfoRequest, GetEpochsInfoRequest, GetEvonodesProposedEpochBlocksByRangeRequest, GetIdentityKeysRequest, GetPathElementsRequest, GetProtocolVersionUpgradeStateRequest, GetProtocolVersionUpgradeVoteStatusRequest, GetTotalCreditsInPlatformRequest, KeyRequestType, @@ -621,6 +623,24 @@ impl Query for NoParamQuery { } } +impl Query for NoParamQuery { + fn query(self, prove: bool) -> Result { + if prove { + unimplemented!( + "query with proof are not supported yet for GetCurrentQuorumsInfoRequest" + ); + } + + let request: GetCurrentQuorumsInfoRequest = GetCurrentQuorumsInfoRequest { + version: Some(get_current_quorums_info_request::Version::V0( + GetCurrentQuorumsInfoRequestV0 {}, + )), + }; + + Ok(request) + } +} + impl Query for LimitQuery> { fn query(self, prove: bool) -> Result { if !prove { diff --git a/packages/withdrawals-contract/src/lib.rs b/packages/withdrawals-contract/src/lib.rs index baa5887f8e..47e735b938 100644 --- a/packages/withdrawals-contract/src/lib.rs +++ b/packages/withdrawals-contract/src/lib.rs @@ -4,6 +4,7 @@ use platform_value::{Identifier, IdentifierBytes32}; use platform_version::version::PlatformVersion; use serde_json::Value; use serde_repr::{Deserialize_repr, Serialize_repr}; +use std::fmt; mod error; pub mod v1; @@ -44,6 +45,19 @@ pub enum WithdrawalStatus { EXPIRED = 4, } +impl fmt::Display for WithdrawalStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let status_str = match self { + WithdrawalStatus::QUEUED => "Queued", + WithdrawalStatus::POOLED => "Pooled", + WithdrawalStatus::BROADCASTED => "Broadcasted", + WithdrawalStatus::COMPLETE => "Complete", + WithdrawalStatus::EXPIRED => "Expired", + }; + write!(f, "{}", status_str) + } +} + pub fn load_definitions(platform_version: &PlatformVersion) -> Result, Error> { match platform_version.system_data_contracts.withdrawals { 1 => Ok(None),