Bitcoin ABC  0.24.10
P2P Digital Currency
proofpool.cpp
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 #include <avalanche/proofpool.h>
6 
9 
10 namespace avalanche {
11 
14  ConflictingProofSet &conflictingProofs) {
15  const ProofId &proofid = proof->getId();
16 
17  // Make sure the set is empty before we add items
18  conflictingProofs.clear();
19 
20  auto &poolView = pool.get<by_proofid>();
21  if (poolView.find(proofid) != poolView.end()) {
22  return AddProofStatus::DUPLICATED;
23  }
24 
25  // Attach UTXOs to this proof.
26  for (size_t i = 0; i < proof->getStakes().size(); i++) {
27  auto p = pool.emplace(i, proof);
28  if (!p.second) {
29  // We have a collision with an existing proof.
30  conflictingProofs.insert(p.first->proof);
31  }
32  }
33 
34  // If there is a conflict, just cleanup the mess.
35  if (conflictingProofs.size() > 0) {
36  for (const auto &s : proof->getStakes()) {
37  auto it = pool.find(s.getStake().getUTXO());
38  assert(it != pool.end());
39 
40  // We need to delete that one.
41  if (it->proof->getId() == proofid) {
42  pool.erase(it);
43  }
44  }
45 
46  return AddProofStatus::REJECTED;
47  }
48 
49  return AddProofStatus::SUCCEED;
50 }
51 
54  ConflictingProofSet &conflictingProofs) {
55  auto status = addProofIfNoConflict(proof, conflictingProofs);
56 
57  // In case the proof was rejected due to conflict and it is the best
58  // candidate, override the conflicting ones and add it again
59  if (status != AddProofStatus::REJECTED ||
60  ConflictingProofComparator()(*conflictingProofs.begin(), proof)) {
61  return status;
62  }
63 
64  for (auto &conflictingProof : conflictingProofs) {
65  removeProof(conflictingProof->getId());
66  }
67 
68  status = addProofIfNoConflict(proof);
69  assert(status == AddProofStatus::SUCCEED);
70 
71  return AddProofStatus::SUCCEED;
72 }
73 
74 // Having the ProofId passed by reference is risky because it is usually a
75 // reference to a proof member. This proof will be deleted during the erasure
76 // loop so we pass it by value.
78  auto &poolView = pool.get<by_proofid>();
79  return poolView.erase(proofid);
80 }
81 
82 void ProofPool::rescan(PeerManager &peerManager) {
83  auto previousPool = std::move(pool);
84  pool.clear();
85 
86  for (auto &entry : previousPool) {
87  peerManager.registerProof(entry.proof);
88  }
89 }
90 
91 ProofRef ProofPool::getProof(const ProofId &proofid) const {
92  auto &poolView = pool.get<by_proofid>();
93  auto it = poolView.find(proofid);
94  return it == poolView.end() ? nullptr : it->proof;
95 }
96 
97 ProofRef ProofPool::getProof(const COutPoint &outpoint) const {
98  auto it = pool.find(outpoint);
99  return it == pool.end() ? nullptr : it->proof;
100 }
101 
102 } // namespace avalanche
avalanche
Definition: avalanche.h:11
avalanche::ProofPool::AddProofStatus
AddProofStatus
Definition: proofpool.h:68
avalanche::ProofId
Definition: proofid.h:17
avalanche::ProofPool::ConflictingProofSet
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
Definition: proofpool.h:74
proofpool.h
avalanche::ProofPool::removeProof
bool removeProof(ProofId proofid)
Definition: proofpool.cpp:77
avalanche::ProofPool::addProofIfPreferred
AddProofStatus addProofIfPreferred(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool.
Definition: proofpool.cpp:53
avalanche::PeerManager
Definition: peermanager.h:109
avalanche::ProofPool::pool
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 > > > pool
Definition: proofpool.h:65
avalanche::ConflictingProofComparator
Compare conflicting proofs.
Definition: proofcomparator.h:35
avalanche::ProofPool::rescan
void rescan(PeerManager &peerManager)
Definition: proofpool.cpp:82
peermanager.h
avalanche::ProofPool::addProofIfNoConflict
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
avalanche::PeerManager::registerProof
bool registerProof(const ProofRef &proof, RegistrationMode mode=RegistrationMode::DEFAULT)
Definition: peermanager.cpp:167
proofcomparator.h
avalanche::ProofPool::getProof
ProofRef getProof(const ProofId &proofid) const
Definition: proofpool.cpp:91
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163