Skip to content

Commit 1264c18

Browse files
committed
refactor: further simplify Network type processing
1 parent b2c6b13 commit 1264c18

File tree

3 files changed

+29
-63
lines changed

3 files changed

+29
-63
lines changed

packages/rs-sdk/src/networks.rs

+9-38
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,19 @@ pub const NETWORK_TESTNET: Network = Network::Testnet;
3535

3636
/// Local development network, run in containers on a local machine for development purposess
3737
pub const NETWORK_LOCAL: Network = Network::Regtest;
38+
pub trait NetworkSettings {
39+
fn core_network(&self) -> Network;
3840

39-
/// Configuration of the Dash Platform network.
40-
///
41-
/// In most cases, you should use [NETWORK_MAINNET], [NETWORK_TESTNET], or [NETWORK_LOCAL] constants.
42-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
43-
pub enum NetworkSettings {
44-
Default(Network),
45-
Custom {
46-
core_network: Network,
47-
instant_lock_quorum_type: QuorumType,
48-
},
49-
/// Mock network for testing purposes
50-
#[cfg(feature = "mocks")]
51-
Mock,
41+
fn chain_locks_quorum_type(&self) -> QuorumType;
5242
}
5343

54-
impl NetworkSettings {
55-
pub fn core_network(&self) -> Network {
56-
match self {
57-
Self::Default(network) => *network,
58-
Self::Custom { core_network, .. } => *core_network,
59-
#[cfg(feature = "mocks")]
60-
Self::Mock => NETWORK_LOCAL,
61-
}
44+
impl NetworkSettings for Network {
45+
fn core_network(&self) -> Network {
46+
*self
6247
}
6348

64-
pub fn chain_locks_type(&self) -> QuorumType {
65-
let llmq_type = match self {
66-
Self::Default(network) => network.chain_locks_type() as u32,
67-
Self::Custom {
68-
instant_lock_quorum_type,
69-
..
70-
} => *instant_lock_quorum_type as u32,
71-
#[cfg(feature = "mocks")]
72-
Self::Mock => self.core_network().chain_locks_type() as u32,
73-
};
74-
QuorumType::from(llmq_type)
75-
}
76-
}
77-
78-
impl From<Network> for NetworkSettings {
79-
fn from(network: Network) -> Self {
80-
NetworkSettings::Default(network)
49+
fn chain_locks_quorum_type(&self) -> QuorumType {
50+
let llmq_type: u8 = self.chain_locks_type().into();
51+
QuorumType::from(llmq_type as u32)
8152
}
8253
}

packages/rs-sdk/src/platform/transition/asset_lock.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! [AssetLockProof] utilities
22
3+
use crate::networks::NetworkSettings;
34
use crate::{Error, Sdk};
45
use dapi_grpc::platform::v0::get_epochs_info_request::{self, GetEpochsInfoRequestV0};
56
use dapi_grpc::platform::v0::GetEpochsInfoRequest;
@@ -71,7 +72,7 @@ impl AssetLockProofVerifier for AssetLockProof {
7172
.cyclehash
7273
.to_raw_hash()
7374
.to_byte_array();
74-
let quorum_type = sdk.quorum_params().instant_lock_quorum_type;
75+
let quorum_type: QuorumType = sdk.network_settings().chain_locks_quorum_type();
7576
// Try to fetch the quorum public key; if it fails, we assume platform does not have this quorum yet
7677
let quorum_pubkey = match context_provider.get_quorum_public_key(
7778
quorum_type as u32,
@@ -183,7 +184,7 @@ fn verify_instant_lock_signature(
183184
mod tests {
184185

185186
use dashcore_rpc::json::QuorumType;
186-
use dpp::dashcore::{consensus::deserialize, InstantLock};
187+
use dpp::dashcore::{consensus::deserialize, hashes::Hash, InstantLock};
187188

188189
use crate::platform::transition::asset_lock::verify_instant_lock_signature;
189190

packages/rs-sdk/src/sdk.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub type LastQueryTimestamp = u64;
9797
pub struct Sdk {
9898
inner: SdkInstance,
9999
/// Type of network we use. Determines some parameters, like quorum types.
100-
network_settings: NetworkSettings,
100+
network: Network,
101101
/// Use proofs when retrieving data from the platform.
102102
///
103103
/// This is set to `true` by default. `false` is not implemented yet.
@@ -149,7 +149,7 @@ impl Clone for Sdk {
149149
metadata_height_tolerance: self.metadata_height_tolerance,
150150
metadata_time_tolerance_ms: self.metadata_time_tolerance_ms,
151151
dapi_client_settings: self.dapi_client_settings,
152-
network_settings: self.network_settings,
152+
network: self.network,
153153
#[cfg(feature = "mocks")]
154154
dump_dir: self.dump_dir.clone(),
155155
}
@@ -278,7 +278,7 @@ impl Sdk {
278278

279279
/// Get configured Dash Core network type.
280280
pub fn core_network(&self) -> Network {
281-
self.network_settings.core_network()
281+
self.network.core_network()
282282
}
283283

284284
/// Retrieve object `O` from proof contained in `request` (of type `R`) and `response`.
@@ -305,7 +305,7 @@ impl Sdk {
305305
SdkInstance::Dapi { .. } => O::maybe_from_proof_with_metadata(
306306
request,
307307
response,
308-
self.network_settings.core_network(),
308+
self.network.core_network(),
309309
self.version(),
310310
&provider,
311311
),
@@ -533,8 +533,8 @@ impl Sdk {
533533
}
534534

535535
/// Return configuration of quorum, like type of quorum used for instant lock.
536-
pub(crate) fn network_settings(&self) -> NetworkSettings {
537-
self.network_settings
536+
pub(crate) fn network_settings(&self) -> Network {
537+
self.network
538538
}
539539

540540
/// Return [Dash Platform version](PlatformVersion) information used by this SDK.
@@ -731,8 +731,10 @@ pub struct SdkBuilder {
731731
core_user: String,
732732
core_password: Zeroizing<String>,
733733

734-
/// Customized network settings of a Dash network used by the SDK.
735-
network_settings: NetworkSettings,
734+
/// Dash Core network type used by the SDK.
735+
///
736+
/// Defaults to [NETWORK_MAINNET](crate::networks::NETWORK_MAINNET).
737+
network: Network,
736738

737739
/// If true, request and verify proofs of the responses.
738740
proofs: bool,
@@ -784,7 +786,7 @@ impl Default for SdkBuilder {
784786
core_port: 0,
785787
core_password: "".to_string().into(),
786788
core_user: "".to_string(),
787-
network_settings: NETWORK_MAINNET.into(),
789+
network: NETWORK_MAINNET,
788790
proofs: true,
789791
metadata_height_tolerance: Some(1),
790792
metadata_time_tolerance_ms: None,
@@ -825,7 +827,7 @@ impl SdkBuilder {
825827

826828
/// Create a new SdkBuilder that will generate mock client.
827829
pub fn new_mock() -> Self {
828-
Self::default().with_network_settings(NetworkSettings::Mock)
830+
Self::default()
829831
}
830832

831833
/// Create a new SdkBuilder instance preconfigured for testnet. NOT IMPLEMENTED YET.
@@ -867,8 +869,9 @@ impl SdkBuilder {
867869
/// Defaults to [NETWORK_MAINNET](crate::networks::NETWORK_MAINNET).
868870
///
869871
/// For more control over the configuration, use [SdkBuilder::with_network_settings()].
870-
pub fn with_network(self, network: Network) -> Self {
871-
self.with_network_settings(network)
872+
pub fn with_network(mut self, network: Network) -> Self {
873+
self.network = network;
874+
self
872875
}
873876

874877
/// Configure CA certificate to use when verifying TLS connections.
@@ -953,15 +956,6 @@ impl SdkBuilder {
953956
self
954957
}
955958

956-
/// Customize custom Dash Platform network settings.
957-
///
958-
/// This method is aimed for advanced use cases, where [SdkBuilder::with_network()] is not good enough.
959-
/// It allows to configure network settings like quorum type, network type, etc. by creating [NetworkSettings] directly.
960-
pub fn with_network_settings<T: Into<NetworkSettings>>(mut self, network_type: T) -> Self {
961-
self.network_settings = network_type.into();
962-
self
963-
}
964-
965959
/// Use Dash Core as a wallet and context provider.
966960
///
967961
/// This is a convenience method that configures the SDK to use Dash Core as a wallet and context provider.
@@ -1063,7 +1057,7 @@ impl SdkBuilder {
10631057
let mut sdk= Sdk{
10641058
dapi_client_settings,
10651059
inner:SdkInstance::Dapi { dapi, version:self.version },
1066-
network_settings: self.network_settings,
1060+
network: self.network,
10671061
proofs:self.proofs,
10681062
context_provider: ArcSwapOption::new( self.context_provider.map(Arc::new)),
10691063
cancel_token: self.cancel_token,
@@ -1131,7 +1125,7 @@ impl SdkBuilder {
11311125
address_list: AddressList::new(),
11321126
version: self.version,
11331127
},
1134-
network_settings: self.network_settings,
1128+
network: self.network,
11351129
dump_dir: self.dump_dir.clone(),
11361130
proofs:self.proofs,
11371131
internal_cache: Default::default(),

0 commit comments

Comments
 (0)