Bitcoin ABC  0.26.3
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 ProofRef &lhs, const ProofRef &rhs) const {
20  return lhs.get() < rhs.get();
21  }
22 };
23 
28  bool operator()(const Proof &lhs, const Proof &rhs) const {
29  uint32_t scoreLhs = lhs.getScore();
30  uint32_t scoreRhs = rhs.getScore();
31 
32  return (scoreLhs != scoreRhs) ? scoreLhs > scoreRhs
33  : lhs.getId() < rhs.getId();
34  }
35 
36  bool operator()(const ProofRef &lhs, const ProofRef &rhs) const {
37  return (*this)(*lhs, *rhs);
38  }
39 };
40 
45  bool operator()(const Proof &lhs, const Proof &rhs) const {
46  // If the proof master is the same, assume the sequence number is the
47  // righteous discriminant; otherwise, use costly parameters.
48  // This is so to prevent a user participating in an aggregated proof
49  // with other users from being able to invalidate the proof for free and
50  // make the aggregation mechanism inefficient.
51  // TODO this only makes sense if the staked coins are locked.
52  if (lhs.getMaster() == rhs.getMaster()) {
53  if (lhs.getSequence() != rhs.getSequence()) {
54  return lhs.getSequence() > rhs.getSequence();
55  }
56  }
57 
58  // Favor the proof which is the most likely to be selected, i.e. the one
59  // with the highest staked amount.
60  if (lhs.getScore() != rhs.getScore()) {
61  return lhs.getScore() > rhs.getScore();
62  }
63 
64  // Select the proof with the least stakes, as this means the individual
65  // stakes have higher amount in average.
66  if (lhs.getStakes().size() != rhs.getStakes().size()) {
67  return lhs.getStakes().size() < rhs.getStakes().size();
68  }
69 
70  // When there is no better discriminant, use the proof id which is
71  // guaranteed to be unique so equality is not possible.
72  return lhs.getId() < rhs.getId();
73  }
74 
75  bool operator()(const ProofRef &lhs, const ProofRef &rhs) const {
76  return (*this)(*lhs, *rhs);
77  }
78 };
79 
80 } // namespace avalanche
81 
82 #endif // BITCOIN_AVALANCHE_PROOFCOMPARATOR_H
T * get()
Get allows to access the undelying pointer.
Definition: rcu.h:170
const ProofId & getId() const
Definition: proof.h:169
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:165
uint64_t getSequence() const
Definition: proof.h:162
const CPubKey & getMaster() const
Definition: proof.h:164
uint32_t getScore() const
Definition: proof.h:174
Compare conflicting proofs.
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const
bool operator()(const Proof &lhs, const Proof &rhs) const
Compare proofs by score, then by id in case of equality.
bool operator()(const Proof &lhs, const Proof &rhs) const
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const
Compare proof references by pointer address.
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const