Skip to main content

snap_coin/
blockchain_data_provider.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4use crate::{api::requests::RequestResponseError, core::{block::Block, blockchain::BlockchainError, transaction::{TransactionId, TransactionOutput}}, crypto::{Hash, keys::Public}};
5
6/// Provides a standardized way to access blockchain data from many sources
7#[derive(Error, Debug, Serialize, Deserialize)]
8pub enum BlockchainDataProviderError {
9    #[error("Failed to access data")]
10    AccessError,
11
12    #[error("Blockchain error: {0}")]
13    BlockchainError(#[from] BlockchainError),
14
15    #[error("Request / response error")]
16    RequestResponseError(#[from] RequestResponseError)
17}
18
19#[async_trait::async_trait]
20pub trait BlockchainDataProvider {
21    /// Get current height
22    async fn get_height(&self) -> Result<usize, BlockchainDataProviderError>;
23
24    /// Get current block reward
25    async fn get_reward(&self) -> Result<u64, BlockchainDataProviderError>;
26
27    /// Get block by height
28    /// Returns option
29    async fn get_block_by_height(&self, height: usize) -> Result<Option<Block>, BlockchainDataProviderError>;
30    
31    /// Get block by hash
32    /// Returns option
33    async fn get_block_by_hash(&self, hash: Hash) -> Result<Option<Block>, BlockchainDataProviderError>;
34
35    /// Get block height by its hash
36    /// Returns option
37    async fn get_height_by_hash(&self, hash: Hash) -> Result<Option<usize>, BlockchainDataProviderError>;
38    
39    /// Get blocks hash by its block height at which it was added
40    /// Returns option
41    async fn get_block_hash_by_height(&self, height: usize) -> Result<Option<Hash>, BlockchainDataProviderError>;
42    
43    /// Gets current transaction pow difficulty
44    async fn get_transaction_difficulty(&self) -> Result<[u8; 32], BlockchainDataProviderError>;
45
46    /// Gets current block pow difficulty
47    async fn get_block_difficulty(&self) -> Result<[u8; 32], BlockchainDataProviderError>;
48
49    /// Gets all available unspent transactions for an address
50    /// Returns a list of unspent outputs (sometimes referred to as inputs as well) and their associated transaction and index in the transactions outputs
51    async fn get_available_transaction_outputs(&self, address: Public) -> Result<Vec<(TransactionId, TransactionOutput, usize)>, BlockchainDataProviderError>;
52}