- 
                Notifications
    
You must be signed in to change notification settings  - Fork 44
 
feat(sdk): asset lock quorum and core locked height verification #2030
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
      
      
            lklimek
  wants to merge
  35
  commits into
  v2.0-dev
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
fix/sdk-invalid-quorum
  
      
      
   
  
    
  
  
  
 
  
      
    base: v2.0-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 18 commits
      Commits
    
    
            Show all changes
          
          
            35 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b3f5f17
              
                feat(sdk): asset  lock quorum verify against platform
              
              
                lklimek 9de013f
              
                feat(sdk)!: params of dash networks - testnet, mainnet, etc
              
              
                lklimek 6362883
              
                feat(sdk): network type, continued
              
              
                lklimek f984ced
              
                feat: asset lock verify
              
              
                lklimek bff31d5
              
                refactor(dapi-client): replace CanRetry.is_node_failure with !CanRetr…
              
              
                lklimek e3a5538
              
                feat(sdk): Error implements CanRetry
              
              
                lklimek 4df0c40
              
                fix(dapi-grpc): GetEpochsInfoRequest not marked as versioned
              
              
                lklimek f41f2bc
              
                test(sdk): asset locks verify tests, WIP
              
              
                lklimek 3517779
              
                fix(sdk): local devnet uses LlmqTest
              
              
                lklimek d9cada9
              
                test(sdk): asset_lock test vectors
              
              
                lklimek 0c04b42
              
                Merge remote-tracking branch 'origin/v1.1-dev' into fix/sdk-invalid-q…
              
              
                lklimek c9c4a21
              
                fix(dapi-grpc): fix const
              
              
                lklimek f57e455
              
                fix(sdk): build error due to imports
              
              
                lklimek b5dfc6a
              
                chore(sdk): cargo fmt
              
              
                lklimek 1f1c957
              
                Merge remote-tracking branch 'origin/v1.1-dev' into fix/sdk-invalid-q…
              
              
                lklimek c4cc766
              
                Merge branch 'master' into fix/sdk-invalid-quorum
              
              
                lklimek 5d33c31
              
                Merge remote-tracking branch 'origin/v1.7-dev' into fix/sdk-invalid-q…
              
              
                lklimek 727cc20
              
                chore: fix build
              
              
                lklimek 50a5267
              
                Merge branch 'v1.8-dev' into fix/sdk-invalid-quorum
              
              
                lklimek ee83d83
              
                Merge remote-tracking branch 'origin/v2.0-dev' into fix/sdk-invalid-q…
              
              
                lklimek bd6a4ff
              
                feat: verify instant asset lock signature
              
              
                lklimek 9a67e22
              
                Merge remote-tracking branch 'origin/v2.0-dev' into fix/sdk-invalid-q…
              
              
                lklimek b2c6b13
              
                refactor: use Network from dashcore
              
              
                lklimek 1264c18
              
                refactor: further simplify Network type processing
              
              
                lklimek 521fdac
              
                chore: remove verification of AssetLockProof::Instant
              
              
                lklimek 969fb0e
              
                chore: minor cleanup
              
              
                lklimek dd50797
              
                chore: self-review
              
              
                lklimek 821bfd7
              
                chore: fix warnings and failing build
              
              
                lklimek a6910f2
              
                test: remove unsupported test case
              
              
                lklimek 9ca5a7b
              
                doc: fix some docs
              
              
                lklimek de4de67
              
                chore: use async_trait for StateTransitionBuilder
              
              
                lklimek c22fa07
              
                chore: remove duplicate async_trait
              
              
                lklimek 20a836b
              
                chore(sdk): s/core_network()/network()/
              
              
                lklimek ed981b6
              
                Merge remote-tracking branch 'origin/v2.0-dev' into fix/sdk-invalid-q…
              
              
                lklimek bb8dcff
              
                build(dpp): remove unused ordered-float dependency
              
              
                lklimek 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //! Configuration of dash networks (devnet, testnet, mainnet, etc.). | ||
| //! | ||
| //! See also: | ||
| //! * https://github.com/dashpay/dash/blob/develop/src/chainparams.cpp | ||
| /* | ||
| Mainnet: | ||
| consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_400_60; | ||
| consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_60_75; | ||
| consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_100_67; | ||
| consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_400_85; | ||
| Testnet: | ||
| consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_50_60; | ||
| consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_60_75; | ||
| consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_25_67; | ||
| consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_50_60; | ||
| Devnet: | ||
| consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_DEVNET; | ||
| consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_DEVNET_DIP0024; | ||
| consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_DEVNET_PLATFORM; | ||
| consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_DEVNET; | ||
| */ | ||
| 
     | 
||
| use dashcore_rpc::json::QuorumType; | ||
| 
     | 
||
| /// Dash network types. | ||
| #[derive(Eq, PartialEq, Clone, Debug)] | ||
| pub enum NetworkType { | ||
| /// Mock implementation; in practice, feaults to Devnet config for Mock mode. Errors when used in non-mock mode. | ||
| Mock, | ||
| /// Mainnet network, used for production. | ||
| Mainnet, | ||
| /// Testnet network, used for testing and development. | ||
| Testnet, | ||
| /// Devnet network, used local for development. | ||
| Devnet, | ||
                
      
                  shumkov marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| /// Custom network configuration. | ||
| Custom(QuorumParams), | ||
                
      
                  shumkov marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| impl NetworkType { | ||
| pub fn instant_lock_quorum_type(&self) -> QuorumType { | ||
| self.to_quorum_params().instant_lock_quorum_type | ||
| } | ||
| 
     | 
||
| pub(crate) fn to_quorum_params(&self) -> QuorumParams { | ||
| match self { | ||
| NetworkType::Mainnet => QuorumParams::new_mainnet(), | ||
| NetworkType::Testnet => QuorumParams::new_testnet(), | ||
| NetworkType::Devnet => QuorumParams::new_devnet(), | ||
| NetworkType::Custom(config) => config.clone(), | ||
| NetworkType::Mock => QuorumParams::new_mock(), | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| /// Configuration of Dash Core Quorums. | ||
| /// | ||
| /// In most cases, you should use the [`new_mainnet`] or [`new_testnet`] functions to create a new instance. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct QuorumParams { | ||
| pub instant_lock_quorum_type: QuorumType, | ||
| } | ||
| 
     | 
||
| impl QuorumParams { | ||
                
      
                  shumkov marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| pub fn new_mainnet() -> Self { | ||
| QuorumParams { | ||
| instant_lock_quorum_type: QuorumType::Llmq400_60, | ||
| } | ||
| } | ||
| 
     | 
||
| pub fn new_testnet() -> Self { | ||
| QuorumParams { | ||
| instant_lock_quorum_type: QuorumType::Llmq50_60, | ||
| } | ||
| } | ||
| 
     | 
||
| pub fn new_devnet() -> Self { | ||
| QuorumParams { | ||
| // FIXME: local devnet uses regtest | ||
                
      
                  lklimek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| instant_lock_quorum_type: QuorumType::LlmqTest, | ||
| } | ||
| } | ||
| 
     | 
||
| pub fn new_mock() -> Self { | ||
| Self::new_devnet() | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //! [AssetLockProof] utilities | ||
| use crate::{Error, Sdk}; | ||
| use dapi_grpc::platform::v0::get_epochs_info_request::{self, GetEpochsInfoRequestV0}; | ||
| use dapi_grpc::platform::v0::GetEpochsInfoRequest; | ||
| use dapi_grpc::platform::VersionedGrpcResponse; | ||
| use dpp::dashcore::hashes::Hash; | ||
| use dpp::prelude::AssetLockProof; | ||
| use drive_proof_verifier::error::ContextProviderError; | ||
| use drive_proof_verifier::ContextProvider; | ||
| use rs_dapi_client::{DapiRequestExecutor, RequestSettings}; | ||
| #[async_trait::async_trait] | ||
| pub trait AssetLockProofVerifier { | ||
| /// Verifies the asset lock proof against the platform. | ||
| /// | ||
| /// This function will return an error if Dash Platform cannot use the provided asset lock proof. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// - [Error::CoreLockedHeightNotYetAvailable] if the core locked height in the proof is higher than the | ||
| /// current core locked height on the platform. Try again later. | ||
| /// - [Error::QuorumNotFound] if the quorum public key is not yet available on the platform, what implies that | ||
| /// the quorum is not (yet) available. Try again later. | ||
| /// - other errors when something goes wrong. | ||
| async fn verify(&self, sdk: &Sdk) -> Result<(), Error>; | ||
| } | ||
| 
     | 
||
| #[async_trait::async_trait] | ||
| impl AssetLockProofVerifier for AssetLockProof { | ||
| async fn verify(&self, sdk: &Sdk) -> Result<(), Error> { | ||
| let context_provider = sdk | ||
| .context_provider() | ||
| .ok_or(Error::Config("Context Provider not configured".to_string()))?; | ||
| 
     | 
||
| // Retrieve current core chain lock info from the platform | ||
| // TODO: implement some caching mechanism to avoid fetching the same data multiple times | ||
| let request = GetEpochsInfoRequest { | ||
                
      
                  shumkov marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| version: Some(get_epochs_info_request::Version::V0( | ||
| GetEpochsInfoRequestV0 { | ||
| ascending: false, | ||
| count: 1, | ||
| prove: true, | ||
| start_epoch: None, | ||
| }, | ||
| )), | ||
| }; | ||
| let response = sdk.execute(request, RequestSettings::default()).await?; | ||
| let platform_core_chain_locked_height = response.metadata()?.core_chain_locked_height; | ||
| 
     | 
||
| match self { | ||
| AssetLockProof::Chain(asset_lock) => { | ||
| if asset_lock.core_chain_locked_height > platform_core_chain_locked_height { | ||
                
      
                  shumkov marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| Err(Error::CoreLockedHeightNotYetAvailable( | ||
| asset_lock.core_chain_locked_height, | ||
| platform_core_chain_locked_height, | ||
| )) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
| AssetLockProof::Instant(v) => { | ||
                
      
                  lklimek marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| let quorum_hash = v.instant_lock().cyclehash.to_raw_hash().to_byte_array(); | ||
| let quorum_type = sdk.quorum_params().instant_lock_quorum_type; | ||
| // Try to fetch the quorum public key; if it fails, we assume platform does not have this quorum yet | ||
| match context_provider.get_quorum_public_key( | ||
| quorum_type as u32, | ||
| quorum_hash, | ||
| platform_core_chain_locked_height, | ||
| ) { | ||
| Err(ContextProviderError::InvalidQuorum(s)) => Err(Error::QuorumNotFound { | ||
| e: ContextProviderError::InvalidQuorum(s), | ||
| quorum_hash_hex: hex::encode(quorum_hash), | ||
| quorum_type: quorum_type as u32, | ||
| core_chain_locked_height: platform_core_chain_locked_height, | ||
| }), | ||
| Err(e) => Err(e.into()), | ||
| Ok(_) => Ok(()), | ||
                
      
                  shumkov marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide 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
    
  
  
    
              
  
    
      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.