Skip to content
Snippets Groups Projects
Select Git revision
  • eb8a4737fedbece312e66f59ae30778aa15a96b0
  • master default protected
  • chrome-manifest-v3
  • feature/migrate-cordova-13
  • feat/improve-network-scan
  • feat/force-migration-check
  • develop
  • feature/encrypted_comment
  • feature/android_api_19
  • gitlab_migration_1
  • rml8
  • v1.7.15-rc1
  • v1.7.14
  • v1.7.13
  • v1.7.12
  • v1.7.11
  • v1.7.10
  • v1.7.9
  • v1.7.8
  • v1.7.7
  • v1.7.6
  • v1.7.5
  • v1.7.4
  • v1.7.3
  • v1.7.2
  • v1.7.1
  • v1.7.0
  • v1.7.0-rc2
  • v1.7.0-rc1
  • v1.6.12
  • v1.6.11
31 results

development_guide.md

Blame
  • traits.rs 4.43 KiB
    //  Copyright (C) 2017-2019  The AXIOM TEAM Association.
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as
    // published by the Free Software Foundation, either version 3 of the
    // License, or (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    //! BlockChain Datas Access Layer in Read-Only mode.
    // ! Define read only trait
    
    use crate::blocks::BlockDb;
    use crate::indexes::identities::{IdentityDb, IdentityStateDb};
    use crate::{BcDbWithReaderStruct, DbReadable, DbReader};
    use dubp_common_doc::{BlockNumber, Blockstamp};
    use dup_crypto::keys::PubKey;
    use durs_dbs_tools::DbError;
    #[cfg(feature = "mock")]
    use mockall::*;
    
    pub trait BcDbRead<DB>
    where
        DB: DbReadable,
    {
        /// Read datas in Db
        fn r<D, F>(&self, f: F) -> Result<D, DbError>
        where
            DB: DbReadable,
            F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>;
    }
    
    impl<DB> BcDbRead<DB> for DB
    where
        DB: DbReadable,
    {
        fn r<D, F>(&self, f: F) -> Result<D, DbError>
        where
            DB: DbReadable,
            F: FnOnce(&BcDbWithReaderStruct<DB>) -> Result<D, DbError>,
        {
            self.read(|r| f(&BcDbWithReaderStruct { db: self, r }))
        }
    }
    
    pub trait BcDbWithReader {
        type DB: DbReadable;
        type R: DbReader;
    
        fn db(&self) -> &Self::DB;
        fn r(&self) -> &Self::R;
    }
    
    #[cfg(feature = "mock")]
    impl<'a> BcDbWithReader for MockBcDbInReadTx {
        type DB = crate::BcDbRo;
        type R = durs_dbs_tools::kv_db::MockKvFileDbReader;
    
        fn db(&self) -> &Self::DB {
            unreachable!()
        }
        fn r(&self) -> &Self::R {
            unreachable!()
        }
    }
    
    #[cfg_attr(feature = "mock", automock)]
    pub trait BcDbInReadTx: BcDbWithReader {
        fn get_current_blockstamp(&self) -> Result<Option<Blockstamp>, DbError>;
        fn get_current_block(&self) -> Result<Option<BlockDb>, DbError>;
        fn get_db_block_in_local_blockchain(
            &self,
            block_number: BlockNumber,
        ) -> Result<Option<BlockDb>, DbError>;
        #[cfg(feature = "client-indexer")]
        fn get_db_blocks_in_local_blockchain(
            &self,
            numbers: Vec<BlockNumber>,
        ) -> Result<Vec<BlockDb>, DbError>;
        fn get_uid_from_pubkey(&self, pubkey: &PubKey) -> Result<Option<String>, DbError>;
        fn get_idty_state_by_pubkey(&self, pubkey: &PubKey)
            -> Result<Option<IdentityStateDb>, DbError>;
        fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError>;
    }
    
    impl<T> BcDbInReadTx for T
    where
        T: BcDbWithReader + durs_common_tools::traits::NotMock,
    {
        #[inline]
        fn get_current_blockstamp(&self) -> Result<Option<Blockstamp>, DbError> {
            crate::current_meta_datas::get_current_blockstamp(self)
        }
        #[inline]
        fn get_current_block(&self) -> Result<Option<BlockDb>, DbError> {
            if let Some(current_blockstamp) = crate::current_meta_datas::get_current_blockstamp(self)? {
                crate::blocks::get_db_block_in_local_blockchain(self, current_blockstamp.id)
            } else {
                Ok(None)
            }
        }
        #[inline]
        fn get_db_block_in_local_blockchain(
            &self,
            block_number: BlockNumber,
        ) -> Result<Option<BlockDb>, DbError> {
            crate::blocks::get_db_block_in_local_blockchain(self, block_number)
        }
        #[cfg(feature = "client-indexer")]
        #[inline]
        fn get_db_blocks_in_local_blockchain(
            &self,
            numbers: Vec<BlockNumber>,
        ) -> Result<Vec<BlockDb>, DbError> {
            crate::blocks::get_blocks_in_local_blockchain_by_numbers(self, numbers)
        }
        #[inline]
        fn get_uid_from_pubkey(&self, pubkey: &PubKey) -> Result<Option<String>, DbError> {
            crate::indexes::identities::get_uid(self, pubkey)
        }
        #[inline]
        fn get_idty_state_by_pubkey(
            &self,
            pubkey: &PubKey,
        ) -> Result<Option<IdentityStateDb>, DbError> {
            crate::indexes::identities::get_idty_state_by_pubkey(self, pubkey)
        }
        #[inline]
        fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError> {
            crate::indexes::identities::get_identity_by_pubkey(self, pubkey)
        }
    }