LCOV - code coverage report
Current view: top level - irohad/consensus/yac/storage/impl - yac_block_storage.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 30 32 93.8 %
Date: 2019-03-07 14:46:43 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "consensus/yac/storage/yac_block_storage.hpp"
       7             : 
       8             : #include "logger/logger.hpp"
       9             : 
      10             : namespace iroha {
      11             :   namespace consensus {
      12             :     namespace yac {
      13             : 
      14             :       // --------| Public API |--------
      15             : 
      16             :       YacBlockStorage::YacBlockStorage(
      17             :           YacHash hash,
      18             :           PeersNumberType peers_in_round,
      19             :           std::shared_ptr<SupermajorityChecker> supermajority_checker,
      20             :           logger::LoggerPtr log)
      21        3156 :           : storage_key_(std::move(hash)),
      22        3156 :             peers_in_round_(peers_in_round),
      23        3156 :             supermajority_checker_(std::move(supermajority_checker)),
      24        3156 :             log_(std::move(log)) {}
      25             : 
      26             :       boost::optional<Answer> YacBlockStorage::insert(VoteMessage msg) {
      27        3639 :         if (validScheme(msg) and uniqueVote(msg)) {
      28        3639 :           votes_.push_back(msg);
      29             : 
      30        3639 :           log_->info(
      31        3639 :               "Vote with round {} and hashes ({}, {}) inserted, votes in "
      32             :               "storage [{}/{}]",
      33        3639 :               msg.hash.vote_round,
      34        3639 :               msg.hash.vote_hashes.proposal_hash,
      35        3639 :               msg.hash.vote_hashes.block_hash,
      36        3639 :               votes_.size(),
      37        3639 :               peers_in_round_);
      38        3639 :         }
      39        3639 :         return getState();
      40           0 :       }
      41             : 
      42             :       boost::optional<Answer> YacBlockStorage::insert(
      43             :           std::vector<VoteMessage> votes) {
      44             :         std::for_each(votes.begin(), votes.end(), [this](auto vote) {
      45           9 :           this->insert(vote);
      46           9 :         });
      47           3 :         return getState();
      48             :       }
      49             : 
      50             :       std::vector<VoteMessage> YacBlockStorage::getVotes() const {
      51          13 :         return votes_;
      52             :       }
      53             : 
      54             :       size_t YacBlockStorage::getNumberOfVotes() const {
      55         950 :         return votes_.size();
      56             :       }
      57             : 
      58             :       boost::optional<Answer> YacBlockStorage::getState() {
      59        3642 :         auto supermajority = supermajority_checker_->hasSupermajority(
      60        3642 :             votes_.size(), peers_in_round_);
      61        3642 :         if (supermajority) {
      62        3158 :           return Answer(CommitMessage(votes_));
      63             :         }
      64         484 :         return boost::none;
      65        3642 :       }
      66             : 
      67             :       bool YacBlockStorage::isContains(const VoteMessage &msg) const {
      68        4062 :         return std::count(votes_.begin(), votes_.end(), msg) != 0;
      69             :       }
      70             : 
      71             :       YacHash YacBlockStorage::getStorageKey() const {
      72        8191 :         return storage_key_;
      73             :       }
      74             : 
      75             :       // --------| private api |--------
      76             : 
      77             :       bool YacBlockStorage::uniqueVote(VoteMessage &msg) {
      78             :         // lookup take O(n) times
      79             :         return std::all_of(votes_.begin(), votes_.end(), [&msg](auto vote) {
      80         569 :           return vote != msg;
      81             :         });
      82             :       }
      83             : 
      84             :       bool YacBlockStorage::validScheme(VoteMessage &vote) {
      85        3639 :         return getStorageKey() == vote.hash;
      86           0 :       }
      87             : 
      88             :     }  // namespace yac
      89             :   }    // namespace consensus
      90             : }  // namespace iroha

Generated by: LCOV version 1.13