Skip to main content

ContextProvider

Trait ContextProvider 

pub trait ContextProvider: Send + Sync {
    // Required methods
    fn get_data_contract(
        &self,
        id: &Identifier,
        platform_version: &PlatformVersion,
    ) -> Result<Option<Arc<DataContract>>, ContextProviderError>;
    fn get_token_configuration(
        &self,
        token_id: &Identifier,
    ) -> Result<Option<TokenConfiguration>, ContextProviderError>;
    fn get_quorum_public_key(
        &self,
        quorum_type: u32,
        quorum_hash: [u8; 32],
        core_chain_locked_height: u32,
    ) -> Result<[u8; 48], ContextProviderError>;
    fn get_platform_activation_height(
        &self,
    ) -> Result<u32, ContextProviderError>;

    // Provided methods
    fn register_data_contract(&self, _contract: Arc<DataContract>) { ... }
    fn fetch_quorum_public_key(
        &self,
        _quorum_type: u32,
        _quorum_hash: [u8; 32],
        _core_chain_locked_height: u32,
    ) -> Option<Pin<Box<dyn Future<Output = Result<Option<[u8; 48]>, ContextProviderError>> + Send>>> { ... }
}
Expand description

Interface between the Sdk and state of the application.

ContextProvider is called by the FromProof trait (and similar) to get information about the application and/or network state, including data contracts that might be cached by the application or quorum public keys.

Developers using the Dash Platform SDK should implement this trait to provide required information to the Sdk, especially implementation of FromProof trait.

A ContextProvider should be thread-safe and manage timeouts and other concurrency-related issues internally, as the FromProof implementations can block on ContextProvider calls.

Required Methods§

fn get_data_contract( &self, id: &Identifier, platform_version: &PlatformVersion, ) -> Result<Option<Arc<DataContract>>, ContextProviderError>

Fetches the data contract for a specified data contract ID. This method is used by FromProof implementations to fetch data contracts referenced in proofs.

§Arguments
  • data_contract_id: The ID of the data contract to fetch.
  • platform_version: The platform version to use.
§Returns
  • Ok(Option<Arc<DataContract>>): On success, returns the data contract if it exists, or None if it does not. We use Arc to avoid copying the data contract.
  • Err(Error): On failure, returns an error indicating why the operation failed.

fn get_token_configuration( &self, token_id: &Identifier, ) -> Result<Option<TokenConfiguration>, ContextProviderError>

Fetches the token configuration for a specified token ID. This method is used by FromProof implementations to fetch token configurations referenced in proofs.

§Arguments
  • token_id: The ID of the token to fetch.
  • platform_version: The platform version to use.
§Returns
  • Ok(Option<TokenConfiguration>): On success, returns the token configuration if it exists, or None if it does not. We use Arc to avoid copying the token configuration.
  • Err(Error): On failure, returns an error indicating why the operation failed.

fn get_quorum_public_key( &self, quorum_type: u32, quorum_hash: [u8; 32], core_chain_locked_height: u32, ) -> Result<[u8; 48], ContextProviderError>

Fetches the public key for a specified quorum.

§Arguments
  • quorum_type: The type of the quorum.
  • quorum_hash: The hash of the quorum. This is used to determine which quorum’s public key to fetch.
  • core_chain_locked_height: Core chain locked height for which the quorum must be valid
§Returns
  • Ok(Vec<u8>): On success, returns a byte vector representing the public key of the quorum.
  • Err(Error): On failure, returns an error indicating why the operation failed.

fn get_platform_activation_height(&self) -> Result<u32, ContextProviderError>

Gets the platform activation height from core. Once this has happened this can be hardcoded.

§Returns
  • Ok(CoreBlockHeight): On success, returns the platform activation height as defined by mn_rr
  • Err(Error): On failure, returns an error indicating why the operation failed.

Provided Methods§

fn register_data_contract(&self, _contract: Arc<DataContract>)

Register a data contract into the provider’s cache so that a subsequent proof verification (e.g. the returned-proof check after a document or token state-transition broadcast) can resolve it without a network fetch.

Providers that maintain a writable known-contracts cache (e.g. the mobile TrustedHttpContextProvider) override this; the default is a no-op for providers that fetch on demand or don’t cache.

fn fetch_quorum_public_key( &self, _quorum_type: u32, _quorum_hash: [u8; 32], _core_chain_locked_height: u32, ) -> Option<Pin<Box<dyn Future<Output = Result<Option<[u8; 48]>, ContextProviderError>> + Send>>>

Fetches the public key of a quorum this provider does not hold yet.

The SDK calls this from async code when proof verification failed because get_quorum_public_key had no key for the quorum, and verifies the same response again when a key comes back. This lets a provider that caches quorum keys, and cannot block inside the synchronous lookup, fetch a key that is newer than its cache.

The arguments come from a response that has not been verified yet. The key must come from the provider’s own trusted source, never from the arguments, and the work done must stay bounded whatever they are.

§Returns
  • None: an asynchronous fetch would not help, for example because the provider cannot fetch keys or its synchronous lookup already fetched. This is the default.
  • Some(future), resolving to:
    • Ok(Some(key)): the key, which get_quorum_public_key now returns as well.
    • Ok(None): the trusted source, asked after this call was made, answered and does not know this quorum.
    • Err(_): the provider could not find out, for example because its trusted source was unreachable.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

§

impl<T> ContextProvider for Mutex<T>
where T: ContextProvider, Mutex<T>: Sync + Send,

§

fn get_data_contract( &self, id: &Identifier, platform_version: &PlatformVersion, ) -> Result<Option<Arc<DataContract>>, ContextProviderError>

§

fn register_data_contract(&self, contract: Arc<DataContract>)

§

fn get_token_configuration( &self, token_id: &Identifier, ) -> Result<Option<TokenConfiguration>, ContextProviderError>

§

fn get_quorum_public_key( &self, quorum_type: u32, quorum_hash: [u8; 32], core_chain_locked_height: u32, ) -> Result<[u8; 48], ContextProviderError>

§

fn fetch_quorum_public_key( &self, quorum_type: u32, quorum_hash: [u8; 32], core_chain_locked_height: u32, ) -> Option<Pin<Box<dyn Future<Output = Result<Option<[u8; 48]>, ContextProviderError>> + Send>>>

§

fn get_platform_activation_height(&self) -> Result<u32, ContextProviderError>

Implementors§

§

impl ContextProvider for MockContextProvider

Available on crate feature mocks only.
§

impl<C> ContextProvider for C
where C: AsRef<dyn ContextProvider> + Send + Sync,