Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "consensus/yac/storage/yac_block_storage.hpp"
19 :
20 : using namespace logger;
21 :
22 : namespace iroha {
23 : namespace consensus {
24 : namespace yac {
25 :
26 : // --------| Public API |--------
27 :
28 : YacBlockStorage::YacBlockStorage(
29 : YacHash hash,
30 : PeersNumberType peers_in_round,
31 : std::shared_ptr<SupermajorityChecker> supermajority_checker)
32 737 : : storage_key_(std::move(hash)),
33 737 : peers_in_round_(peers_in_round),
34 737 : supermajority_checker_(std::move(supermajority_checker)) {
35 737 : log_ = log("YacBlockStorage");
36 737 : }
37 :
38 : boost::optional<Answer> YacBlockStorage::insert(VoteMessage msg) {
39 790 : if (validScheme(msg) and uniqueVote(msg)) {
40 790 : votes_.push_back(msg);
41 :
42 790 : log_->info("Vote with rounds ({}, {}) and hashes ({}, {}) inserted",
43 790 : msg.hash.vote_round.block_round,
44 790 : msg.hash.vote_round.reject_round,
45 790 : msg.hash.vote_hashes.proposal_hash,
46 790 : msg.hash.vote_hashes.block_hash);
47 790 : log_->info(
48 790 : "Votes in storage [{}/{}]", votes_.size(), peers_in_round_);
49 790 : }
50 790 : return getState();
51 : }
52 :
53 : boost::optional<Answer> YacBlockStorage::insert(
54 : std::vector<VoteMessage> votes) {
55 : std::for_each(votes.begin(), votes.end(), [this](auto vote) {
56 9 : this->insert(vote);
57 9 : });
58 3 : return getState();
59 : }
60 :
61 : std::vector<VoteMessage> YacBlockStorage::getVotes() const {
62 9 : return votes_;
63 : }
64 :
65 : size_t YacBlockStorage::getNumberOfVotes() const {
66 120 : return votes_.size();
67 : }
68 :
69 : boost::optional<Answer> YacBlockStorage::getState() {
70 793 : auto supermajority =
71 793 : supermajority_checker_->checkSize(votes_.size(), peers_in_round_);
72 793 : if (supermajority) {
73 736 : return Answer(CommitMessage(votes_));
74 : }
75 57 : return boost::none;
76 793 : }
77 :
78 : bool YacBlockStorage::isContains(const VoteMessage &msg) const {
79 788 : return std::count(votes_.begin(), votes_.end(), msg) != 0;
80 : }
81 :
82 : YacHash YacBlockStorage::getStorageKey() const {
83 1640 : return storage_key_;
84 : }
85 :
86 : // --------| private api |--------
87 :
88 : bool YacBlockStorage::uniqueVote(VoteMessage &msg) {
89 : // lookup take O(n) times
90 : return std::all_of(votes_.begin(), votes_.end(), [&msg](auto vote) {
91 133 : return vote != msg;
92 : });
93 : }
94 :
95 : bool YacBlockStorage::validScheme(VoteMessage &vote) {
96 790 : return getStorageKey() == vote.hash;
97 0 : }
98 :
99 : } // namespace yac
100 : } // namespace consensus
101 : } // namespace iroha
|