Bitcoin ABC 0.30.7
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>
11#include <coins.h>
13
14#include <boost/multi_index/hashed_index.hpp>
15#include <boost/multi_index/mem_fun.hpp>
16#include <boost/multi_index/member.hpp>
17#include <boost/multi_index/ordered_index.hpp>
18#include <boost/multi_index_container.hpp>
19
20#include <cstdint>
21#include <unordered_set>
22
23namespace avalanche {
24
25class PeerManager;
26
28 size_t utxoIndex;
30
31 const COutPoint &getUTXO() const {
32 return proof->getStakes().at(utxoIndex).getStake().getUTXO();
33 }
34
35 ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
36 : utxoIndex(_utxoIndex), proof(std::move(_proof)) {}
37};
38
39struct by_utxo;
40struct by_proofid;
41struct by_proof_score;
42
46 return entry.proof->getId();
47 }
48};
49
50namespace bmi = boost::multi_index;
51
52using ProofIdSet = std::unordered_set<ProofId, SaltedProofIdHasher>;
53
57class ProofPool {
58 boost::multi_index_container<
60 bmi::indexed_by<
61 // index by utxo
62 bmi::hashed_unique<
63 bmi::tag<by_utxo>,
64 bmi::const_mem_fun<ProofPoolEntry, const COutPoint &,
67 // index by proofid
68 bmi::hashed_non_unique<bmi::tag<by_proofid>,
71 // index by proof score
72 bmi::ordered_non_unique<
73 bmi::tag<by_proof_score>,
74 bmi::member<ProofPoolEntry, ProofRef, &ProofPoolEntry::proof>,
77
78 mutable bool cacheClean = true;
79 mutable size_t cacheProofCount = 0;
80
81public:
84 SUCCEED = 1,
86 };
87
88 using ConflictingProofSet = std::set<ProofRef, ConflictingProofComparator>;
89
95 ConflictingProofSet &conflictingProofs);
98 return addProofIfNoConflict(proof, dummy);
99 }
100
107 ConflictingProofSet &conflictingProofs);
110 return addProofIfPreferred(proof, dummy);
111 }
112
113 bool removeProof(ProofId proofid);
114
115 std::unordered_set<ProofRef, SaltedProofHasher>
116 rescan(PeerManager &peerManager);
117
118 template <typename Callable> void forEachProof(Callable &&func) const {
119 ProofId lastProofId;
120 auto &poolView = pool.get<by_proofid>();
121 for (auto it = poolView.begin(); it != poolView.end(); it++) {
122 const ProofId &proofId = it->proof->getId();
123 if (lastProofId != proofId) {
124 func(it->proof);
125 lastProofId = proofId;
126 }
127 }
128 }
129
130 ProofIdSet getProofIds() const;
131 ProofRef getProof(const ProofId &proofid) const;
132 ProofRef getProof(const COutPoint &outpoint) const;
134
135 size_t size() const { return pool.size(); }
136 size_t countProofs() const;
137};
138
139} // namespace avalanche
140
141#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:57
@ DUPLICATED
Already in pool.
Definition: proofpool.h:85
@ REJECTED
Rejected due to conflicts.
Definition: proofpool.h:83
@ SUCCEED
Added successfully.
Definition: proofpool.h:84
AddProofStatus addProofIfNoConflict(const ProofRef &proof)
Definition: proofpool.h:96
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:135
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:79
void forEachProof(Callable &&func) const
Definition: proofpool.h:118
ProofRef getProof(const ProofId &proofid) const
Definition: proofpool.cpp:112
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
Definition: proofpool.h:88
AddProofStatus addProofIfPreferred(const ProofRef &proof)
Definition: proofpool.h:108
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:76
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:52
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:27
const COutPoint & getUTXO() const
Definition: proofpool.h:31
ProofRef proof
Definition: proofpool.h:29
ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
Definition: proofpool.h:35
size_t utxoIndex
Definition: proofpool.h:28
Definition: proofpool.h:43
result_type operator()(const ProofPoolEntry &entry) const
Definition: proofpool.h:45