Bitcoin ABC 0.33.5
P2P Digital Currency
proofpool.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_PROOFPOOL_H
6#define BITCOIN_AVALANCHE_PROOFPOOL_H
7
8#include <avalanche/proof.h>
10#include <avalanche/proofid.h>
12
13#include <boost/multi_index/hashed_index.hpp>
14#include <boost/multi_index/mem_fun.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/multi_index/ordered_index.hpp>
17#include <boost/multi_index_container.hpp>
18
19#include <cstdint>
20#include <unordered_set>
21
22namespace avalanche {
23
24class PeerManager;
25
27 size_t utxoIndex;
29
30 const COutPoint &getUTXO() const {
31 return proof->getStakes().at(utxoIndex).getStake().getUTXO();
32 }
33
34 ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
35 : utxoIndex(_utxoIndex), proof(std::move(_proof)) {}
36};
37
38struct by_utxo;
39struct by_proofid;
40struct by_proof_score;
41
45 return entry.proof->getId();
46 }
47};
48
49namespace bmi = boost::multi_index;
50
51using ProofIdSet = std::unordered_set<ProofId, SaltedProofIdHasher>;
52
56class ProofPool {
57 boost::multi_index_container<
59 bmi::indexed_by<
60 // index by utxo
61 bmi::hashed_unique<
62 bmi::tag<by_utxo>,
63 bmi::const_mem_fun<ProofPoolEntry, const COutPoint &,
66 // index by proofid
67 bmi::hashed_non_unique<bmi::tag<by_proofid>,
70 // index by proof score
71 bmi::ordered_non_unique<
72 bmi::tag<by_proof_score>,
73 bmi::member<ProofPoolEntry, ProofRef, &ProofPoolEntry::proof>,
76
77 mutable bool cacheClean = true;
78 mutable size_t cacheProofCount = 0;
79
80public:
83 SUCCEED = 1,
85 };
86
87 using ConflictingProofSet = std::set<ProofRef, ConflictingProofComparator>;
88
94 ConflictingProofSet &conflictingProofs);
97 return addProofIfNoConflict(proof, dummy);
98 }
99
106 ConflictingProofSet &conflictingProofs);
109 return addProofIfPreferred(proof, dummy);
110 }
111
112 bool removeProof(ProofId proofid);
113
114 std::unordered_set<ProofRef, SaltedProofHasher>
115 rescan(PeerManager &peerManager);
116
117 template <typename Callable> void forEachProof(Callable &&func) const {
118 ProofId lastProofId;
119 auto &poolView = pool.get<by_proofid>();
120 for (auto it = poolView.begin(); it != poolView.end(); it++) {
121 const ProofId &proofId = it->proof->getId();
122 if (lastProofId != proofId) {
123 func(it->proof);
124 lastProofId = proofId;
125 }
126 }
127 }
128
129 ProofIdSet getProofIds() const;
130 ProofRef getProof(const ProofId &proofid) const;
131 ProofRef getProof(const COutPoint &outpoint) const;
133
134 size_t size() const { return pool.size(); }
135 size_t countProofs() const;
136};
137
138} // namespace avalanche
139
140#endif // BITCOIN_AVALANCHE_PROOFPOOL_H
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
Map a proof to each utxo.
Definition: proofpool.h:56
@ DUPLICATED
Already in pool.
Definition: proofpool.h:84
@ REJECTED
Rejected due to conflicts.
Definition: proofpool.h:82
@ SUCCEED
Added successfully.
Definition: proofpool.h:83
AddProofStatus addProofIfNoConflict(const ProofRef &proof)
Definition: proofpool.h:95
AddProofStatus addProofIfPreferred(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool.
Definition: proofpool.cpp:54
size_t size() const
Definition: proofpool.h:134
AddProofStatus addProofIfNoConflict(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool, and fail if there is a conflict on any UTXO.
Definition: proofpool.cpp:13
size_t countProofs() const
Definition: proofpool.cpp:129
bool removeProof(ProofId proofid)
Definition: proofpool.cpp:79
size_t cacheProofCount
Definition: proofpool.h:78
void forEachProof(Callable &&func) const
Definition: proofpool.h:117
ProofRef getProof(const ProofId &proofid) const
Definition: proofpool.cpp:112
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
Definition: proofpool.h:87
AddProofStatus addProofIfPreferred(const ProofRef &proof)
Definition: proofpool.h:107
ProofRef getLowestScoreProof() const
Definition: proofpool.cpp:123
boost::multi_index_container< ProofPoolEntry, bmi::indexed_by< bmi::hashed_unique< bmi::tag< by_utxo >, bmi::const_mem_fun< ProofPoolEntry, const COutPoint &, &ProofPoolEntry::getUTXO >, SaltedOutpointHasher >, bmi::hashed_non_unique< bmi::tag< by_proofid >, ProofPoolEntryProofIdKeyExtractor, SaltedProofIdHasher >, bmi::ordered_non_unique< bmi::tag< by_proof_score >, bmi::member< ProofPoolEntry, ProofRef, &ProofPoolEntry::proof >, ProofComparatorByScore > > > pool
Definition: proofpool.h:75
std::unordered_set< ProofRef, SaltedProofHasher > rescan(PeerManager &peerManager)
Definition: proofpool.cpp:86
ProofIdSet getProofIds() const
Definition: proofpool.cpp:101
std::unordered_set< ProofId, SaltedProofIdHasher > ProofIdSet
Definition: proofpool.h:51
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
Compare proofs by score, then by id in case of equality.
Definition: proofpool.h:26
const COutPoint & getUTXO() const
Definition: proofpool.h:30
ProofRef proof
Definition: proofpool.h:28
ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
Definition: proofpool.h:34
size_t utxoIndex
Definition: proofpool.h:27
Definition: proofpool.h:42
result_type operator()(const ProofPoolEntry &entry) const
Definition: proofpool.h:44