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