Bitcoin ABC  0.24.7
P2P Digital Currency
proofcomparator.h
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_AVALANCHE_PROOFCOMPARATOR_H
6 #define BITCOIN_AVALANCHE_PROOFCOMPARATOR_H
7 
8 #include <avalanche/proof.h>
9 
10 #include <cstdint>
11 #include <memory>
12 
13 namespace avalanche {
14 
19  bool operator()(const Proof &lhs, const Proof &rhs) const {
20  uint32_t scoreLhs = lhs.getScore();
21  uint32_t scoreRhs = rhs.getScore();
22 
23  return (scoreLhs != scoreRhs) ? scoreLhs > scoreRhs
24  : lhs.getId() < rhs.getId();
25  }
26 
27  bool operator()(const ProofRef &lhs, const ProofRef &rhs) const {
28  return (*this)(*lhs, *rhs);
29  }
30 };
31 
36  bool operator()(const Proof &lhs, const Proof &rhs) const {
37  // If the proof master is the same, assume the sequence number is the
38  // righteous discriminant; otherwise, use costly parameters.
39  // This is so to prevent a user participating in an aggregated proof
40  // with other users from being able to invalidate the proof for free and
41  // make the aggregation mechanism inefficient.
42  // TODO this only makes sense if the staked coins are locked.
43  if (lhs.getMaster() == rhs.getMaster()) {
44  if (lhs.getSequence() != rhs.getSequence()) {
45  return lhs.getSequence() > rhs.getSequence();
46  }
47  }
48 
49  // Favor the proof which is the most likely to be selected, i.e. the one
50  // with the highest staked amount.
51  if (lhs.getScore() != rhs.getScore()) {
52  return lhs.getScore() > rhs.getScore();
53  }
54 
55  // Select the proof with the least stakes, as this means the individual
56  // stakes have higher amount in average.
57  if (lhs.getStakes().size() != rhs.getStakes().size()) {
58  return lhs.getStakes().size() < rhs.getStakes().size();
59  }
60 
61  // When there is no better discriminant, use the proof id which is
62  // guaranteed to be unique so equality is not possible.
63  return lhs.getId() < rhs.getId();
64  }
65 
66  bool operator()(const ProofRef &lhs, const ProofRef &rhs) const {
67  return (*this)(*lhs, *rhs);
68  }
69 };
70 
71 } // namespace avalanche
72 
73 #endif // BITCOIN_AVALANCHE_PROOFCOMPARATOR_H
avalanche::ProofComparator::operator()
bool operator()(const Proof &lhs, const Proof &rhs) const
Definition: proofcomparator.h:19
avalanche::ConflictingProofComparator::operator()
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const
Definition: proofcomparator.h:66
avalanche
Definition: avalanche.h:11
avalanche::Proof::getId
const ProofId & getId() const
Definition: proof.h:152
avalanche::ProofComparator::operator()
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const
Definition: proofcomparator.h:27
avalanche::Proof::getSequence
uint64_t getSequence() const
Definition: proof.h:143
avalanche::Proof::getScore
uint32_t getScore() const
Definition: proof.cpp:98
avalanche::ProofComparator
Compare proofs by score, then by id in case of equality.
Definition: proofcomparator.h:18
avalanche::Proof::getStakes
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:146
avalanche::Proof
Definition: proof.h:102
avalanche::ConflictingProofComparator
Compare conflicting proofs.
Definition: proofcomparator.h:35
avalanche::ConflictingProofComparator::operator()
bool operator()(const Proof &lhs, const Proof &rhs) const
Definition: proofcomparator.h:36
proof.h
avalanche::Proof::getMaster
const CPubKey & getMaster() const
Definition: proof.h:145
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163