Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk)!: improve mock context provider async processing #2232

Merged
merged 26 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3ee0e8d
changed contested resource type to a struct
QuantumExplorer Oct 7, 2024
fa72ece
expose drive_proof_verifier types
QuantumExplorer Oct 7, 2024
4b4a62d
updated to newer backon crate
QuantumExplorer Oct 8, 2024
ab91e07
fix: fixed pools to add tls
QuantumExplorer Oct 8, 2024
7a732a7
Merge branch 'feat/fixClientTLSConnections' into feat/sdkContestedRes…
QuantumExplorer Oct 8, 2024
2f60720
added root certificates
QuantumExplorer Oct 8, 2024
df989ee
Merge branch 'feat/fixClientTLSConnections' into feat/sdkContestedRes…
QuantumExplorer Oct 8, 2024
01282fb
assume http2
QuantumExplorer Oct 8, 2024
d406d64
Merge branch 'feat/fixClientTLSConnections' into feat/sdkContestedRes…
QuantumExplorer Oct 8, 2024
1d3b335
Merge branch 'v1.4-dev' into feat/sdkContestedResourcesFixes2
QuantumExplorer Oct 8, 2024
c22bafc
changed T to E
QuantumExplorer Oct 8, 2024
9921618
another fix
QuantumExplorer Oct 8, 2024
f5aa584
refactor(sdk): improve context provider async processing
lklimek Oct 9, 2024
f58e08c
refactor(sdk): store passwords in Zeroizing<String>
lklimek Oct 9, 2024
4c004a7
chore: rabbit's feedback
lklimek Oct 9, 2024
a8bb9ae
Merge branch 'v1.4-dev' into feat/set-context-provider-no-mut
QuantumExplorer Oct 9, 2024
5f9f907
Merge branch 'v1.4-dev' into feat/set-context-provider-no-mut
QuantumExplorer Oct 10, 2024
301c8e3
Merge branch 'v1.4-dev' into feat/set-context-provider-no-mut
QuantumExplorer Oct 10, 2024
612970f
fix(sdk): deadlock when executing async futures within sync context
lklimek Oct 10, 2024
d9035bd
refactor(sdk): move sync to main and pub
lklimek Oct 10, 2024
6994a5c
chore(sdk): Async error handling
lklimek Oct 10, 2024
909921c
chore: apply feedback
lklimek Oct 10, 2024
cc11cd8
refactor: minor context provider refactoring
lklimek Oct 10, 2024
00620f8
chore: apply linter issues
lklimek Oct 10, 2024
247df93
Merge branch 'v1.4-dev' into feat/set-context-provider-no-mut
lklimek Oct 10, 2024
a915288
refactor(sdk): simplify context provider load
lklimek Oct 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/rs-drive-proof-verifier/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub enum ContextProviderError {
/// Core Fork Error
#[error("activation fork error: {0}")]
ActivationForkError(String),

/// Async error, eg. when tokio runtime fails
#[error("async error: {0}")]
AsyncError(String),
}

impl From<drive::error::Error> for Error {
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive-proof-verifier/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ impl FromProof<platform::GetContestedResourcesRequest> for ContestedResources {
verify_tenderdash_proof(proof, mtd, &root_hash, provider)?;

let resources: ContestedResources =
items.into_iter().map(ContestedResource::Value).collect();
items.into_iter().map(|v| ContestedResource(v)).collect();

Ok((resources.into_option(), mtd.clone(), proof.clone()))
}
Expand Down
23 changes: 7 additions & 16 deletions packages/rs-drive-proof-verifier/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,8 @@ pub struct ElementFetchRequestItem(pub Element);
pub type IdentityBalanceAndRevision = (u64, Revision);

/// Contested resource values.
#[derive(Debug, derive_more::From, Clone, PartialEq)]
pub enum ContestedResource {
/// Generic [Value]
Value(Value),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ContestedResource(pub Value);

impl ContestedResource {
/// Get the value.
Expand All @@ -244,13 +241,9 @@ impl ContestedResource {
}
}

impl TryInto<Value> for ContestedResource {
type Error = crate::Error;

fn try_into(self) -> Result<Value, Self::Error> {
match self {
ContestedResource::Value(value) => Ok(value),
}
impl Into<Value> for ContestedResource {
fn into(self) -> Value {
self.0
}
}

Expand All @@ -261,9 +254,7 @@ impl PlatformVersionEncode for ContestedResource {
encoder: &mut E,
_platform_version: &platform_version::PlatformVersion,
) -> Result<(), bincode::error::EncodeError> {
match self {
ContestedResource::Value(value) => value.encode(encoder),
}
self.0.encode(encoder)
}
}

Expand All @@ -273,7 +264,7 @@ impl PlatformVersionedDecode for ContestedResource {
decoder: &mut D,
_platform_version: &platform_version::PlatformVersion,
) -> Result<Self, bincode::error::DecodeError> {
Ok(ContestedResource::Value(Value::decode(decoder)?))
Ok(ContestedResource(Value::decode(decoder)?))
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ derive_more = { version = "1.0", features = ["from"] }
dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore-rpc", tag = "v0.15.4" }
lru = { version = "0.12.3", optional = true }
bip37-bloom-filter = { git = "https://github.com/dashpay/rs-bip37-bloom-filter", branch = "develop" }
pollster = { version = "0.3.0" }

[dev-dependencies]
tokio = { version = "1.40", features = ["macros", "rt-multi-thread"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ use std::{fmt::Debug, sync::Mutex};

/// Core RPC client that can be used to retrieve quorum keys from core.
///
/// Implements [`ContextProvider`] trait.
///
/// TODO: This is a temporary implementation, effective until we integrate SPV.
pub struct CoreClient {
pub struct LowLevelDashCoreClient {
core: Mutex<Client>,
server_address: String,
core_user: String,
core_password: String,
lklimek marked this conversation as resolved.
Show resolved Hide resolved
core_port: u16,
}

impl Debug for CoreClient {
impl Clone for LowLevelDashCoreClient {
// As Client does not implement Clone, we just create a new instance of CoreClient here.
fn clone(&self) -> Self {
LowLevelDashCoreClient::new(
&self.server_address,
self.core_port,
&self.core_user,
&self.core_password,
)
.expect("Failed to clone CoreClient when cloning, this should not happen")
}
}

impl Debug for LowLevelDashCoreClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CoreClient")
.field("server_address", &self.server_address)
Expand All @@ -37,7 +49,7 @@ impl Debug for CoreClient {
}
}

impl CoreClient {
impl LowLevelDashCoreClient {
/// Create new Dash Core client.
///
/// # Arguments
Expand All @@ -63,13 +75,14 @@ impl CoreClient {
core: Mutex::new(core),
server_address: server_address.to_string(),
core_user: core_user.to_string(),
core_password: core_password.to_string(),
core_port,
})
}
}

// Wallet functions
impl CoreClient {
impl LowLevelDashCoreClient {
/// List unspent transactions
///
/// ## Arguments
Expand Down
4 changes: 4 additions & 0 deletions packages/rs-sdk/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//! Dash Core SDK implementation.
//!
//! TODO: This is work in progress.
#[cfg(feature = "mocks")]
mod dash_core_client;
mod transaction;
#[cfg(feature = "mocks")]
pub use dash_core_client::LowLevelDashCoreClient;
3 changes: 3 additions & 0 deletions packages/rs-sdk/src/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Various internal utilities used by the SDK.

pub(crate) mod sync;
32 changes: 32 additions & 0 deletions packages/rs-sdk/src/internal/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! futures-related utilities to handle async code from sync code.

use std::future::Future;

use drive_proof_verifier::error::ContextProviderError;

#[derive(Debug, thiserror::Error)]
pub(crate) enum AsyncError {
#[error("asynchronous call from synchronous context failed: {0}")]
#[allow(unused)]
Generic(String),
}

impl From<AsyncError> for ContextProviderError {
fn from(error: AsyncError) -> Self {
ContextProviderError::AsyncError(error.to_string())
}
}

impl From<AsyncError> for crate::Error {
fn from(error: AsyncError) -> Self {
Self::ContextProviderError(error.into())
}
}

/// Block on the provided future and return the result.
pub(crate) fn block_on<F: Future + Send + 'static>(fut: F) -> Result<F::Output, AsyncError>
where
F::Output: Send,
{
Ok(futures::executor::block_on(fut))
}
4 changes: 2 additions & 2 deletions packages/rs-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@
#![allow(rustdoc::private_intra_doc_links)]

pub mod core;
#[cfg(feature = "mocks")]
mod core_client;
pub mod error;
mod internal;
mod internal_cache;
pub mod mock;
pub mod platform;
Expand All @@ -76,6 +75,7 @@ pub use sdk::{RequestSettings, Sdk, SdkBuilder};
pub use dashcore_rpc;
pub use dpp;
pub use drive;
pub use drive_proof_verifier::types as query_types;
pub use rs_dapi_client as dapi_client;

/// Version of the SDK
Expand Down
15 changes: 8 additions & 7 deletions packages/rs-sdk/src/mock/provider.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Example ContextProvider that uses the Core gRPC API to fetch data from Platform.

use crate::core_client::CoreClient;
use crate::core::LowLevelDashCoreClient;
use crate::internal::sync::block_on;
use crate::platform::Fetch;
use crate::{Error, Sdk};
use arc_swap::ArcSwapAny;
use dpp::prelude::{CoreBlockHeight, DataContract, Identifier};
use drive_proof_verifier::error::ContextProviderError;
use drive_proof_verifier::ContextProvider;
use pollster::FutureExt;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::sync::Arc;
Expand All @@ -17,7 +17,7 @@ use std::sync::Arc;
/// Example [ContextProvider] used by the Sdk for testing purposes.
pub struct GrpcContextProvider {
/// Core client
core: CoreClient,
core: LowLevelDashCoreClient,
/// [Sdk] to use when fetching data from Platform
///
/// Note that if the `sdk` is `None`, the context provider will not be able to fetch data itself and will rely on
Expand Down Expand Up @@ -62,7 +62,8 @@ impl GrpcContextProvider {
data_contracts_cache_size: NonZeroUsize,
quorum_public_keys_cache_size: NonZeroUsize,
) -> Result<Self, Error> {
let core_client = CoreClient::new(core_ip, core_port, core_user, core_password)?;
let core_client =
LowLevelDashCoreClient::new(core_ip, core_port, core_user, core_password)?;
Ok(Self {
core: core_client,
sdk: ArcSwapAny::new(Arc::new(sdk)),
Expand Down Expand Up @@ -197,9 +198,9 @@ impl ContextProvider for GrpcContextProvider {

let sdk_cloned = sdk.clone();

let data_contract: Option<DataContract> = DataContract::fetch(&sdk_cloned, contract_id)
.block_on()
.map_err(|e| ContextProviderError::DataContractFailure(e.to_string()))?;
let data_contract: Option<DataContract> =
block_on(async move { DataContract::fetch(&sdk_cloned, contract_id).await })?
.map_err(|e| ContextProviderError::DataContractFailure(e.to_string()))?;
lklimek marked this conversation as resolved.
Show resolved Hide resolved

if let Some(ref dc) = data_contract {
self.data_contracts_cache.put(*data_contract_id, dc.clone());
Expand Down
Loading