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 22 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
23 changes: 16 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
3 changes: 2 additions & 1 deletion packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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" }
zeroize = { version = "1.8", features = ["derive"] }

[dev-dependencies]
tokio = { version = "1.40", features = ["macros", "rt-multi-thread"] }
Expand Down Expand Up @@ -71,6 +71,7 @@ mocks = [
"dep:dotenvy",
"dep:envy",
"dep:lru",
"zeroize/serde",
]

# Run integration tests using test vectors from `tests/vectors/` instead of connecting to live Dash Platform.
Expand Down
5 changes: 3 additions & 2 deletions packages/rs-sdk/examples/read_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap::Parser;
use dash_sdk::{mock::provider::GrpcContextProvider, platform::Fetch, Sdk, SdkBuilder};
use dpp::prelude::{DataContract, Identifier};
use rs_dapi_client::AddressList;
use zeroize::Zeroizing;

#[derive(clap::Parser, Debug)]
#[command(version)]
Expand All @@ -22,7 +23,7 @@ pub struct Config {

// Dash Core RPC password
#[arg(short = 'p', long)]
pub core_password: String,
pub core_password: Zeroizing<String>,

/// Dash Platform DAPI port
#[arg(short = 'd', long)]
Expand Down Expand Up @@ -86,7 +87,7 @@ fn setup_sdk(config: &Config) -> Sdk {
.expect("parse uri");

// Now, we create the Sdk with the wallet and context provider.
let mut sdk = SdkBuilder::new(AddressList::from_iter([uri]))
let sdk = SdkBuilder::new(AddressList::from_iter([uri]))
.build()
.expect("cannot build sdk");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@ use dpp::dashcore::ProTxHash;
use dpp::prelude::CoreBlockHeight;
use drive_proof_verifier::error::ContextProviderError;
use std::{fmt::Debug, sync::Mutex};
use zeroize::Zeroizing;

/// 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: Zeroizing<String>,
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 +50,7 @@ impl Debug for CoreClient {
}
}

impl CoreClient {
impl LowLevelDashCoreClient {
/// Create new Dash Core client.
///
/// # Arguments
Expand All @@ -63,13 +76,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().into(),
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: 1 addition & 2 deletions packages/rs-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
#![allow(rustdoc::private_intra_doc_links)]

pub mod core;
#[cfg(feature = "mocks")]
mod core_client;
pub mod error;
mod internal_cache;
pub mod mock;
Expand All @@ -78,6 +76,7 @@ pub use dpp;
pub use drive;
pub use drive_proof_verifier::types as query_types;
pub use rs_dapi_client as dapi_client;
pub mod sync;

/// Version of the SDK
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
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::platform::Fetch;
use crate::sync::block_on;
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
Loading