Bitcoin ABC  0.24.7
P2P Digital Currency
processor.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2019 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_PROCESSOR_H
6 #define BITCOIN_AVALANCHE_PROCESSOR_H
7 
8 #include <avalanche/node.h>
10 #include <avalanche/protocol.h>
12 #include <eventloop.h>
13 #include <interfaces/chain.h>
14 #include <interfaces/handler.h>
15 #include <key.h>
16 #include <rwcollection.h>
17 
18 #include <boost/multi_index/composite_key.hpp>
19 #include <boost/multi_index/hashed_index.hpp>
20 #include <boost/multi_index/member.hpp>
21 #include <boost/multi_index/ordered_index.hpp>
22 #include <boost/multi_index_container.hpp>
23 
24 #include <atomic>
25 #include <chrono>
26 #include <cstdint>
27 #include <memory>
28 #include <vector>
29 
30 class ArgsManager;
31 class CBlockIndex;
32 class CConnman;
33 class CNode;
34 class CScheduler;
35 class Config;
36 class PeerManager;
37 struct bilingual_str;
38 
42 static constexpr size_t AVALANCHE_MAX_ELEMENT_POLL = 16;
43 
47 static constexpr std::chrono::milliseconds AVALANCHE_DEFAULT_QUERY_TIMEOUT{
48  10000};
49 
50 namespace avalanche {
51 
52 class Delegation;
53 class PeerManager;
54 class Proof;
55 struct VoteRecord;
56 
57 enum struct VoteStatus : uint8_t {
58  Invalid,
59  Rejected,
60  Accepted,
61  Finalized,
62 };
63 
64 template <typename VoteItem> class VoteItemUpdate {
65  VoteItem item;
67 
68 public:
69  VoteItemUpdate(const VoteItem itemIn, VoteStatus statusIn)
70  : item(std::move(itemIn)), status(statusIn) {}
71 
72  const VoteStatus &getStatus() const { return status; }
73 
74  VoteItem getVoteItem() { return item; }
75  const VoteItem getVoteItem() const { return item; }
76 };
77 
80 
81 using BlockVoteMap =
82  std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator>;
83 using ProofVoteMap = std::map<const ProofRef, VoteRecord, ProofComparator>;
84 
85 struct query_timeout {};
86 
87 namespace {
88  struct AvalancheTest;
89 }
90 
91 class Processor {
93  std::chrono::milliseconds queryTimeoutDuration;
94 
99 
104 
108  std::atomic<uint64_t> round;
109 
114  std::unique_ptr<PeerManager> peerManager GUARDED_BY(cs_peerManager);
115 
116  struct Query {
118  uint64_t round;
120 
127  mutable std::vector<CInv> invs;
128  };
129 
130  using QuerySet = boost::multi_index_container<
131  Query,
132  boost::multi_index::indexed_by<
133  // index by nodeid/round
134  boost::multi_index::hashed_unique<boost::multi_index::composite_key<
135  Query,
136  boost::multi_index::member<Query, NodeId, &Query::nodeid>,
137  boost::multi_index::member<Query, uint64_t, &Query::round>>>,
138  // sorted by timeout
139  boost::multi_index::ordered_non_unique<
140  boost::multi_index::tag<query_timeout>,
141  boost::multi_index::member<Query, TimePoint,
143 
145 
147  struct PeerData;
148  std::unique_ptr<PeerData> peerData;
150 
153 
156  std::unique_ptr<interfaces::Handler> chainNotificationsHandler;
157 
158  Processor(interfaces::Chain &chain, CConnman *connmanIn,
159  std::unique_ptr<PeerData> peerDataIn, CKey sessionKeyIn);
160 
161 public:
162  ~Processor();
163 
164  static std::unique_ptr<Processor> MakeProcessor(const ArgsManager &argsman,
165  interfaces::Chain &chain,
166  CConnman *connman,
168 
169  void setQueryTimeoutDuration(std::chrono::milliseconds d) {
171  }
172 
173  bool addBlockToReconcile(const CBlockIndex *pindex);
174  void addProofToReconcile(const ProofRef &proof);
175  bool isAccepted(const CBlockIndex *pindex) const;
176  bool isAccepted(const ProofRef &proof) const;
177  int getConfidence(const CBlockIndex *pindex) const;
178  int getConfidence(const ProofRef &proof) const;
179 
180  // TODO: Refactor the API to remove the dependency on avalanche/protocol.h
181  void sendResponse(CNode *pfrom, Response response) const;
182  bool registerVotes(NodeId nodeid, const Response &response,
183  std::vector<BlockUpdate> &blockUpdates,
184  std::vector<ProofUpdate> &proofUpdates, int &banscore,
185  std::string &error);
186 
187  template <typename Callable> auto withPeerManager(Callable &&func) const {
189  return func(*peerManager);
190  }
191 
192  CPubKey getSessionPubKey() const;
193  bool sendHello(CNode *pfrom) const;
194 
195  ProofRef getLocalProof() const;
196 
197  /*
198  * Return whether the avalanche service flag should be set.
199  */
201 
202  bool startEventLoop(CScheduler &scheduler);
203  bool stopEventLoop();
204 
205 private:
206  void runEventLoop();
207  void clearTimedoutRequests();
208  std::vector<CInv> getInvsForNextPoll(bool forPoll = true);
210 
215  uint256 buildLocalSighash(CNode *pfrom) const;
216 
217  friend struct ::avalanche::AvalancheTest;
218 };
219 
220 } // namespace avalanche
221 
222 #endif // BITCOIN_AVALANCHE_PROCESSOR_H
avalanche::Processor::QuerySet
boost::multi_index_container< Query, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::composite_key< Query, boost::multi_index::member< Query, NodeId, &Query::nodeid >, boost::multi_index::member< Query, uint64_t, &Query::round > >>, boost::multi_index::ordered_non_unique< boost::multi_index::tag< query_timeout >, boost::multi_index::member< Query, TimePoint, &Query::timeout > >> > QuerySet
Definition: processor.h:142
avalanche::VoteStatus::Rejected
@ Rejected
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:35
avalanche::Processor::Query::round
uint64_t round
Definition: processor.h:118
avalanche::Processor::sendHello
bool sendHello(CNode *pfrom) const
Definition: processor.cpp:498
avalanche::Processor::NotificationsHandler
Definition: processor.cpp:113
avalanche::VoteItemUpdate::getStatus
const VoteStatus & getStatus() const
Definition: processor.h:72
avalanche::VoteItemUpdate::status
VoteStatus status
Definition: processor.h:66
protocol.h
avalanche::Processor::chainNotificationsHandler
std::unique_ptr< interfaces::Handler > chainNotificationsHandler
Definition: processor.h:155
avalanche::Processor::MakeProcessor
static std::unique_ptr< Processor > MakeProcessor(const ArgsManager &argsman, interfaces::Chain &chain, CConnman *connman, bilingual_str &error)
Definition: processor.cpp:147
avalanche::Processor::withPeerManager
auto withPeerManager(Callable &&func) const
Definition: processor.h:187
avalanche
Definition: avalanche.h:11
avalanche::Processor::getInvsForNextPoll
std::vector< CInv > getInvsForNextPoll(bool forPoll=true)
Definition: processor.cpp:537
avalanche::VoteStatus
VoteStatus
Definition: processor.h:57
avalanche::VoteItemUpdate
Definition: processor.h:64
avalanche::Processor::addBlockToReconcile
bool addBlockToReconcile(const CBlockIndex *pindex)
Definition: processor.cpp:246
bilingual_str
Bilingual messages:
Definition: translation.h:17
avalanche::Processor::getConfidence
int getConfidence(const CBlockIndex *pindex) const
Definition: processor.cpp:295
blockindexworkcomparator.h
avalanche::Processor::getSuitableNodeToQuery
NodeId getSuitableNodeToQuery()
Definition: processor.cpp:592
CNode
Information about a peer.
Definition: net.h:926
avalanche::Processor::peerData
std::unique_ptr< PeerData > peerData
Definition: processor.h:147
avalanche::Processor::registerVotes
bool registerVotes(NodeId nodeid, const Response &response, std::vector< BlockUpdate > &blockUpdates, std::vector< ProofUpdate > &proofUpdates, int &banscore, std::string &error)
Definition: processor.cpp:351
avalanche::Processor::Query::invs
std::vector< CInv > invs
We declare this as mutable so it can be modified in the multi_index.
Definition: processor.h:127
handler.h
avalanche::Processor::sessionKey
CKey sessionKey
Definition: processor.h:149
avalanche::VoteItemUpdate::getVoteItem
VoteItem getVoteItem()
Definition: processor.h:74
AVALANCHE_DEFAULT_QUERY_TIMEOUT
static constexpr std::chrono::milliseconds AVALANCHE_DEFAULT_QUERY_TIMEOUT
How long before we consider that a query timed out.
Definition: processor.h:47
avalanche::Processor
Definition: processor.h:91
AVALANCHE_MAX_ELEMENT_POLL
static constexpr size_t AVALANCHE_MAX_ELEMENT_POLL
Maximum item that can be polled at once.
Definition: processor.h:42
avalanche::Processor::sendResponse
void sendResponse(CNode *pfrom, Response response) const
Definition: processor.cpp:344
avalanche::Processor::setQueryTimeoutDuration
void setQueryTimeoutDuration(std::chrono::milliseconds d)
Definition: processor.h:169
AnnotatedMixin< std::mutex >
avalanche::Processor::isAvalancheServiceAvailable
bool isAvalancheServiceAvailable()
Definition: processor.h:200
chain.h
avalanche::ProofVoteMap
std::map< const ProofRef, VoteRecord, ProofComparator > ProofVoteMap
Definition: processor.h:83
avalanche::Processor::Query::timeout
TimePoint timeout
Definition: processor.h:119
avalanche::Processor::blockVoteRecords
RWCollection< BlockVoteMap > blockVoteRecords
Blocks to run avalanche on.
Definition: processor.h:98
TimePoint
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: node.h:17
avalanche::Processor::getLocalProof
ProofRef getLocalProof() const
Definition: processor.cpp:524
Config
Definition: config.h:17
eventloop.h
interfaces::Chain
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:108
response
Response response
Definition: processor.cpp:321
avalanche::query_timeout
Definition: processor.h:85
avalanche::Response
Definition: protocol.h:33
avalanche::VoteStatus::Finalized
@ Finalized
avalanche::Processor::eventLoop
EventLoop eventLoop
Event loop machinery.
Definition: processor.h:152
avalanche::Processor::isAccepted
bool isAccepted(const CBlockIndex *pindex) const
Definition: processor.cpp:275
avalanche::VoteItemUpdate::item
VoteItem item
Definition: processor.h:65
avalanche::Processor::buildLocalSighash
uint256 buildLocalSighash(CNode *pfrom) const
Build and return the challenge whose signature is included in the AVAHELLO message that we send to a ...
Definition: processor.cpp:488
avalanche::Processor::GUARDED_BY
std::unique_ptr< PeerManager > peerManager GUARDED_BY(cs_peerManager)
avalanche::Processor::proofVoteRecords
RWCollection< ProofVoteMap > proofVoteRecords
Proofs to run avalanche on.
Definition: processor.h:103
PeerManager
Definition: net_processing.h:45
uint256
256-bit opaque blob.
Definition: uint256.h:127
avalanche::Processor::connman
CConnman * connman
Definition: processor.h:92
avalanche::Processor::Query
Definition: processor.h:116
avalanche::Processor::runEventLoop
void runEventLoop()
Definition: processor.cpp:659
avalanche::Processor::addProofToReconcile
void addProofToReconcile(const ProofRef &proof)
Definition: processor.cpp:264
CConnman
Definition: net.h:221
key.h
avalanche::Processor::queries
RWCollection< QuerySet > queries
Definition: processor.h:144
avalanche::Processor::queryTimeoutDuration
std::chrono::milliseconds queryTimeoutDuration
Definition: processor.h:93
avalanche::Processor::Processor
Processor(interfaces::Chain &chain, CConnman *connmanIn, std::unique_ptr< PeerData > peerDataIn, CKey sessionKeyIn)
Definition: processor.cpp:132
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
avalanche::BlockVoteMap
std::map< const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator > BlockVoteMap
Definition: processor.h:82
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
ArgsManager
Definition: system.h:152
avalanche::VoteItemUpdate::getVoteItem
const VoteItem getVoteItem() const
Definition: processor.h:75
avalanche::Processor::cs_peerManager
Mutex cs_peerManager
Keep track of the peers and associated infos.
Definition: processor.h:113
LOCK
#define LOCK(cs)
Definition: sync.h:241
avalanche::Processor::startEventLoop
bool startEventLoop(CScheduler &scheduler)
Definition: processor.cpp:528
avalanche::Processor::getSessionPubKey
CPubKey getSessionPubKey() const
Definition: processor.cpp:484
proofcomparator.h
NodeId
int64_t NodeId
Definition: nodeid.h:10
avalanche::Processor::PeerData
Definition: processor.cpp:108
avalanche::Processor::round
std::atomic< uint64_t > round
Keep track of peers and queries sent.
Definition: processor.h:108
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163
avalanche::VoteItemUpdate::VoteItemUpdate
VoteItemUpdate(const VoteItem itemIn, VoteStatus statusIn)
Definition: processor.h:69
avalanche::VoteStatus::Accepted
@ Accepted
avalanche::Processor::clearTimedoutRequests
void clearTimedoutRequests()
Definition: processor.cpp:597
node.h
avalanche::VoteStatus::Invalid
@ Invalid
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
avalanche::Processor::stopEventLoop
bool stopEventLoop()
Definition: processor.cpp:533
rwcollection.h
avalanche::Processor::~Processor
~Processor()
Definition: processor.cpp:142
EventLoop
Definition: eventloop.h:17
avalanche::Processor::Query::nodeid
NodeId nodeid
Definition: processor.h:117
RWCollection< BlockVoteMap >