Bitcoin ABC  0.24.10
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>
12 #include <primitives/transaction.h>
13 
14 #include <boost/multi_index/hashed_index.hpp>
15 #include <boost/multi_index/mem_fun.hpp>
16 #include <boost/multi_index_container.hpp>
17 
18 #include <cstdint>
19 
20 namespace avalanche {
21 
22 class PeerManager;
23 
25  size_t utxoIndex;
27 
28  const COutPoint &getUTXO() const {
29  return proof->getStakes().at(utxoIndex).getStake().getUTXO();
30  }
31 
32  ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
33  : utxoIndex(_utxoIndex), proof(std::move(_proof)) {}
34 };
35 
36 struct by_utxo;
37 struct by_proofid;
38 
41  result_type operator()(const ProofPoolEntry &entry) const {
42  return entry.proof->getId();
43  }
44 };
45 
46 namespace bmi = boost::multi_index;
47 
51 class ProofPool {
52  boost::multi_index_container<
54  bmi::indexed_by<
55  // index by utxo
56  bmi::hashed_unique<
57  bmi::tag<by_utxo>,
58  bmi::const_mem_fun<ProofPoolEntry, const COutPoint &,
61  // index by proofid
62  bmi::hashed_non_unique<bmi::tag<by_proofid>,
66 
67 public:
69  REJECTED = 0,
70  SUCCEED = 1,
71  DUPLICATED = 2,
72  };
73 
74  using ConflictingProofSet = std::set<ProofRef, ConflictingProofComparator>;
75 
81  ConflictingProofSet &conflictingProofs);
83  ConflictingProofSet dummy;
84  return addProofIfNoConflict(proof, dummy);
85  }
86 
93  ConflictingProofSet &conflictingProofs);
95  ConflictingProofSet dummy;
96  return addProofIfPreferred(proof, dummy);
97  }
98 
99  bool removeProof(ProofId proofid);
100 
101  void rescan(PeerManager &peerManager);
102 
103  ProofRef getProof(const ProofId &proofid) const;
104  ProofRef getProof(const COutPoint &outpoint) const;
105 
106  size_t size() const { return pool.size(); }
107 };
108 
109 } // namespace avalanche
110 
111 #endif // BITCOIN_AVALANCHE_PROOFPOOL_H
avalanche::ProofPoolEntry::getUTXO
const COutPoint & getUTXO() const
Definition: proofpool.h:28
avalanche::ProofPoolEntry
Definition: proofpool.h:24
avalanche::ProofPoolEntry::proof
ProofRef proof
Definition: proofpool.h:26
avalanche::ProofPool::addProofIfNoConflict
AddProofStatus addProofIfNoConflict(const ProofRef &proof)
Definition: proofpool.h:82
proofid.h
avalanche::SaltedProofIdHasher
Definition: proofid.h:41
avalanche
Definition: avalanche.h:11
transaction.h
avalanche::ProofPool::AddProofStatus
AddProofStatus
Definition: proofpool.h:68
avalanche::ProofPoolEntry::ProofPoolEntry
ProofPoolEntry(size_t _utxoIndex, ProofRef _proof)
Definition: proofpool.h:32
avalanche::ProofId
Definition: proofid.h:17
avalanche::ProofPool::addProofIfPreferred
AddProofStatus addProofIfPreferred(const ProofRef &proof)
Definition: proofpool.h:94
avalanche::ProofPool::ConflictingProofSet
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
Definition: proofpool.h:74
avalanche::ProofPool::size
size_t size() const
Definition: proofpool.h:106
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::DUPLICATED
@ DUPLICATED
Already in pool.
Definition: proofpool.h:71
avalanche::ProofPoolEntry::utxoIndex
size_t utxoIndex
Definition: proofpool.h:25
PeerManager
Definition: net_processing.h:48
coins.h
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::ProofPool::rescan
void rescan(PeerManager &peerManager)
Definition: proofpool.cpp:82
SaltedOutpointHasher
Definition: coins.h:72
avalanche::ProofPoolEntryProofIdKeyExtractor
Definition: proofpool.h:39
avalanche::ProofPool
Map a proof to each utxo.
Definition: proofpool.h:51
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
proof.h
proofcomparator.h
avalanche::ProofPool::SUCCEED
@ SUCCEED
Added successfully.
Definition: proofpool.h:70
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
avalanche::ProofPool::REJECTED
@ REJECTED
Rejected due to conflicts.
Definition: proofpool.h:69
avalanche::ProofPoolEntryProofIdKeyExtractor::operator()
result_type operator()(const ProofPoolEntry &entry) const
Definition: proofpool.h:41