-
Notifications
You must be signed in to change notification settings - Fork 53
feat(swift-sdk): use SPV-synced quorums for Platform proof verification #3417
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
QuantumExplorer
wants to merge
7
commits into
v3.1-dev
Choose a base branch
from
feat/ios-spv-quorums
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 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4703d6
feat(swift-sdk): use SPV-synced quorums for Platform proof verification
QuantumExplorer ad6904a
refactor(rs-sdk-ffi): move SPV context provider from Swift to Rust
QuantumExplorer d9e478e
feat(platform-wallet): add pure-Rust SpvContextProvider
QuantumExplorer 8d2bb27
fix: address CodeRabbit review feedback on SPV quorums PR
QuantumExplorer fa7e531
feat: use pure-Rust SpvContextProvider, bump rust-dashcore
QuantumExplorer 54b6ed0
fix: eliminate race between didSet and handleNetworkSwitch
QuantumExplorer cfde224
fix: use try_read() instead of blocking_read(), add dep: prefix
QuantumExplorer 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
142 changes: 142 additions & 0 deletions
142
packages/rs-platform-wallet/src/spv_context_provider.rs
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,142 @@ | ||
| //! SPV-based Context Provider | ||
| //! | ||
| //! Pure Rust implementation that reads quorum data directly from a | ||
| //! [`MasternodeListEngine`], with no FFI calls. | ||
| //! | ||
| //! # Architecture | ||
| //! | ||
| //! The [`SpvContextProvider`] holds an `Arc<RwLock<MasternodeListEngine>>` | ||
| //! (shared with the SPV client) and reads quorum public keys by looking up | ||
| //! the masternode list closest to the requested core chain-locked height. | ||
| //! | ||
| //! This design eliminates the need for FFI round-trips: the same in-memory | ||
| //! masternode list engine that the SPV client populates during sync is read | ||
| //! directly by the Platform SDK's proof verifier. | ||
| //! | ||
| //! # Usage | ||
| //! | ||
| //! ```ignore | ||
| //! use std::sync::Arc; | ||
| //! use tokio::sync::RwLock; | ||
| //! use dash_spv::MasternodeListEngine; | ||
| //! use dashcore::Network; | ||
| //! use platform_wallet::spv_context_provider::SpvContextProvider; | ||
| //! | ||
| //! let engine: Arc<RwLock<MasternodeListEngine>> = /* from DashSpvClient */; | ||
| //! let provider = SpvContextProvider::new(engine, Network::Testnet); | ||
| //! ``` | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use dash_context_provider::ContextProvider; | ||
| use dash_context_provider::ContextProviderError; | ||
| use dash_spv::LLMQType; | ||
| use dash_spv::MasternodeListEngine; | ||
| use dashcore::hashes::Hash; | ||
| use dashcore::Network; | ||
| use dashcore::QuorumHash; | ||
| use dpp::data_contract::TokenConfiguration; | ||
| use dpp::prelude::{CoreBlockHeight, DataContract, Identifier}; | ||
| use dpp::version::PlatformVersion; | ||
| use tokio::sync::RwLock; | ||
|
|
||
| /// Context provider backed by an SPV client's synced masternode data. | ||
| /// | ||
| /// Reads quorum public keys directly from the [`MasternodeListEngine`] | ||
| /// without any FFI calls. The engine is shared with the SPV client via | ||
| /// `Arc<RwLock<...>>`, so all data stays in-process. | ||
| pub struct SpvContextProvider { | ||
| masternode_engine: Arc<RwLock<MasternodeListEngine>>, | ||
| network: Network, | ||
| } | ||
|
|
||
| impl SpvContextProvider { | ||
| /// Create a new SPV context provider. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `masternode_engine` - Shared reference to the masternode list engine, | ||
| /// typically obtained from [`DashSpvClient::masternode_list_engine()`]. | ||
| /// * `network` - The Dash network (mainnet, testnet, devnet, etc.). | ||
| pub fn new(masternode_engine: Arc<RwLock<MasternodeListEngine>>, network: Network) -> Self { | ||
| Self { | ||
| masternode_engine, | ||
| network, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ContextProvider for SpvContextProvider { | ||
| fn get_quorum_public_key( | ||
| &self, | ||
| quorum_type: u32, | ||
| quorum_hash: [u8; 32], | ||
| core_chain_locked_height: u32, | ||
| ) -> Result<[u8; 48], ContextProviderError> { | ||
| let llmq_type: LLMQType = (quorum_type as u8).into(); | ||
| let quorum_hash = QuorumHash::from_byte_array(quorum_hash); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| let engine = self.masternode_engine.blocking_read(); | ||
| let (before, _after) = engine.masternode_lists_around_height(core_chain_locked_height); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| let ml = before.ok_or_else(|| { | ||
| ContextProviderError::InvalidQuorum(format!( | ||
| "No masternode list found at or before height {}", | ||
| core_chain_locked_height | ||
| )) | ||
| })?; | ||
|
|
||
| let list_height = ml.known_height; | ||
|
|
||
| let quorums = ml.quorums.get(&llmq_type).ok_or_else(|| { | ||
| ContextProviderError::InvalidQuorum(format!( | ||
| "No quorums of type {} found at list height {} (requested {})", | ||
| quorum_type, list_height, core_chain_locked_height | ||
| )) | ||
| })?; | ||
|
|
||
| let quorum = quorums.get(&quorum_hash).ok_or_else(|| { | ||
| ContextProviderError::InvalidQuorum(format!( | ||
| "Quorum not found: type {} at list height {} (requested {}) \ | ||
| with hash {:x} (masternode list has {} quorums of this type)", | ||
| quorum_type, | ||
| list_height, | ||
| core_chain_locked_height, | ||
| quorum_hash, | ||
| quorums.len() | ||
| )) | ||
| })?; | ||
|
|
||
| let pubkey_bytes: &[u8; 48] = quorum.quorum_entry.quorum_public_key.as_ref(); | ||
| Ok(*pubkey_bytes) | ||
| } | ||
|
|
||
| fn get_platform_activation_height(&self) -> Result<CoreBlockHeight, ContextProviderError> { | ||
| let height = match self.network { | ||
| Network::Mainnet => 1_888_888, | ||
| Network::Testnet => 1_289_520, | ||
| Network::Devnet => 1, | ||
| _ => 0, | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| Ok(height) | ||
| } | ||
|
|
||
| fn get_data_contract( | ||
| &self, | ||
| _data_contract_id: &Identifier, | ||
| _platform_version: &PlatformVersion, | ||
| ) -> Result<Option<Arc<DataContract>>, ContextProviderError> { | ||
| // Data contract lookup is handled by the SDK's contract cache, | ||
| // not the SPV layer. | ||
| Ok(None) | ||
| } | ||
|
|
||
| fn get_token_configuration( | ||
| &self, | ||
| _token_id: &Identifier, | ||
| ) -> Result<Option<TokenConfiguration>, ContextProviderError> { | ||
| // Token configuration lookup is handled by the SDK's contract cache, | ||
| // not the SPV layer. | ||
| Ok(None) | ||
| } | ||
| } | ||
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
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
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.