-
Notifications
You must be signed in to change notification settings - Fork 53
refactor(swift-sdk): spv client config wrapper #3469
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
Open
ZocoLini
wants to merge
1
commit into
v3.1-dev
Choose a base branch
from
refactor/spv-config-wrapper
base: v3.1-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
96 changes: 96 additions & 0 deletions
96
packages/swift-sdk/Sources/SwiftDashSDK/Core/SPV/SPVConfig.swift
This file contains hidden or 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,96 @@ | ||
| import Foundation | ||
| import DashSDKFFI | ||
|
|
||
| public class SPVConfig: @unchecked Sendable { | ||
| internal var config: UnsafeMutablePointer<FFIClientConfig> | ||
|
|
||
| public init(_ network: Network) { | ||
| config = { | ||
| switch network.rawValue { | ||
| case 0: | ||
| return dash_spv_ffi_config_mainnet() | ||
| case 1: | ||
| return dash_spv_ffi_config_testnet() | ||
| case 2: | ||
| // Regtest (local Docker) | ||
| return dash_spv_ffi_config_new(FFINetwork(rawValue: 2)) | ||
| case 3: | ||
| // Map devnet to custom FFINetwork value 3 | ||
| return dash_spv_ffi_config_new(FFINetwork(rawValue: 3)) | ||
| default: | ||
| return dash_spv_ffi_config_testnet() | ||
| } | ||
| }() | ||
|
|
||
| // If requested, prefer local core peers (defaults to 127.0.0.1 with network default port) | ||
| let useLocalCore = UserDefaults.standard.bool(forKey: "useLocalhostCore") | ||
| || UserDefaults.standard.bool(forKey: "useDockerSetup") | ||
| // Only restrict to configured peers when using local core, if not, allow DNS discovery | ||
|
|
||
| self.restrictToConfiguredPeers(useLocalCore) | ||
|
|
||
| if useLocalCore { | ||
| let raw = UserDefaults.standard.string(forKey: "corePeerAddresses")?.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let peers = (raw?.isEmpty == false ? raw! : "127.0.0.1:20001") | ||
| .split(separator: ",") | ||
| .map { $0.trimmingCharacters(in: .whitespaces) } | ||
| .filter { !$0.isEmpty } | ||
|
|
||
| self.clearPeers(); | ||
|
|
||
| for peer in peers { | ||
| self.addPeer(peer) | ||
| } | ||
| } | ||
|
|
||
| // Set user agent to include SwiftDashSDK version from the framework bundle | ||
| do { | ||
| let bundle = Bundle(for: SPVClient.self) | ||
| let version = (bundle.infoDictionary?["CFBundleShortVersionString"] as? String) | ||
| ?? (bundle.infoDictionary?["CFBundleVersion"] as? String) | ||
| ?? "dev" | ||
| let ua = "SwiftDashSDK/\(version)" | ||
|
|
||
| self.setUserAgent(ua) | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| dash_spv_ffi_config_destroy(config) | ||
| } | ||
|
|
||
| public func setMasternodeSyncEnabled(_ enabled: Bool) { | ||
| dash_spv_ffi_config_set_masternode_sync_enabled(config, enabled) | ||
| } | ||
|
|
||
| public func setMempoolStrategy(_ strategy: FFIMempoolStrategy) { | ||
| dash_spv_ffi_config_set_mempool_tracking(config, true) | ||
| dash_spv_ffi_config_set_mempool_strategy(config, strategy) | ||
| dash_spv_ffi_config_set_fetch_mempool_transactions(config, true) | ||
| } | ||
|
|
||
| public func setUserAgent(_ userAgent: String) { | ||
| dash_spv_ffi_config_set_user_agent(config, userAgent) | ||
| } | ||
|
|
||
| public func setStartHeight(_ height: UInt32) { | ||
| dash_spv_ffi_config_set_start_from_height(config, height) | ||
| } | ||
|
|
||
| public func setDataDir(_ dataDir: String) { | ||
| dash_spv_ffi_config_set_data_dir(config, dataDir) | ||
| } | ||
|
|
||
| public func clearPeers() { | ||
| dash_spv_ffi_config_clear_peers(config) | ||
| } | ||
|
|
||
| // Supports "ip:port" or bare IP for network-default port | ||
| public func addPeer(_ peer: String) { | ||
| dash_spv_ffi_config_add_peer(config, peer) | ||
| } | ||
|
|
||
| public func restrictToConfiguredPeers(_ enabled: Bool) { | ||
| dash_spv_ffi_config_set_restrict_to_configured_peers(config, enabled) | ||
|
ZocoLini marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.