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/impl/supermajority_checker_impl.hpp"
19 : #include <boost/range/adaptors.hpp>
20 : #include "interfaces/common_objects/peer.hpp"
21 : #include "interfaces/common_objects/signature.hpp"
22 : #include "validation/utils.hpp"
23 :
24 : namespace iroha {
25 : namespace consensus {
26 : namespace yac {
27 :
28 : bool SupermajorityCheckerImpl::hasSupermajority(
29 : const shared_model::interface::types::SignatureRangeType &signatures,
30 : const std::vector<std::shared_ptr<shared_model::interface::Peer>>
31 : &peers) const {
32 6 : return checkSize(boost::size(signatures), peers.size())
33 6 : and peersSubset(signatures, peers);
34 : }
35 :
36 : bool SupermajorityCheckerImpl::checkSize(PeersNumberType current,
37 : PeersNumberType all) const {
38 863 : if (current > all) {
39 2 : return false;
40 : }
41 861 : auto f = (all - 1) / 3.0;
42 861 : return current >= 2 * f + 1;
43 863 : }
44 :
45 : bool SupermajorityCheckerImpl::peersSubset(
46 : const shared_model::interface::types::SignatureRangeType &signatures,
47 : const std::vector<std::shared_ptr<shared_model::interface::Peer>>
48 : &peers) const {
49 4 : return validation::signaturesSubset(
50 4 : signatures,
51 4 : peers
52 4 : | boost::adaptors::transformed(
53 : [](const auto &p) -> decltype(auto) {
54 37 : return p->pubkey();
55 : }));
56 0 : }
57 :
58 : bool SupermajorityCheckerImpl::hasReject(PeersNumberType frequent,
59 : PeersNumberType voted,
60 : PeersNumberType all) const {
61 54 : auto not_voted = all - voted;
62 54 : return not checkSize(frequent + not_voted, all);
63 : }
64 :
65 : } // namespace yac
66 : } // namespace consensus
67 : } // namespace iroha
|