Skip to content
Snippets Groups Projects
Select Git revision
  • vainamoinen197-transactiondocument-replace-vec-fields-by-smallvec-2
  • dvermd/200-keypairs-dewif
  • elois/wot
  • jawaka/155-dbex-add-dump-fork-tree-command
  • dev default protected
  • elois/195-bcdbwriteop
  • elois/deps-crypto
  • elois/gva-monetary-mass
  • elois/191-sled
  • elois/195
  • ji_emme/gva-humantimefield
  • 184-gva-rename-commontime-field-to-blockchaintime
  • ji_emme/182-gva-implement-block-meta-data
  • ji_emme/rml14
  • hugo/151-ws2pv2-sync
  • ji_emme/181-gva-implement-identity-request
  • ji_emme/89-implement-client-api-gva-graphql-verification-api
  • logo
  • test-juniper-from-schema
  • elois/exemple-gva-global-context
  • v0.2.0-a4 protected
  • v0.2.0-a2 protected
  • v0.2.0-a protected
  • v0.1.1-a1 protected
  • documents/v0.10.0-b1 protected
  • crypto/v0.4.0-b1 protected
  • crypto/v0.3.0-b3 protected
  • crypto/v0.3.0-b2 protected
  • crypto/v0.3.0-b1 protected
  • wot/v0.8.0-a0.9 protected
  • wot/v0.8.0-a0.8 protected
  • 0.1.0-a0.1 protected
  • v0.0.1-a0.12 protected
  • v0.0.1-a0.11 protected
  • v0.0.1-a0.10 protected
  • v0.0.1-a0.9 protected
  • v0.0.1-a0.8 protected
  • v0.0.1-a0.7 protected
  • v0.0.1-a0.6 protected
  • v0.0.1-a0.5 protected
40 results

traits.rs

Blame
  • traits.rs 4.68 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::current_metadata::current_ud::CurrentUdDb;
    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: Fn(&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: Fn(&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_old::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>;
        fn get_current_ud(&self) -> Result<Option<CurrentUdDb>, 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_metadata::get_current_blockstamp(self)
        }
        #[inline]
        fn get_current_block(&self) -> Result<Option<BlockDb>, DbError> {
            if let Some(current_blockstamp) = crate::current_metadata::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)
        }
        #[inline]
        fn get_current_ud(&self) -> Result<Option<CurrentUdDb>, DbError> {
            crate::current_metadata::get_current_ud(self)
        }
    }