forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathp2p_sync.cpp
102 lines (86 loc) · 2.97 KB
/
p2p_sync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2019-2021 Xenios SEZC
// https://www.veriblock.org
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "vbk/p2p_sync.hpp"
#include "validation.h"
#include <net_processing.h>
#include <protocol.h>
#include <vbk/util.hpp>
#include <veriblock/pop.hpp>
namespace VeriBlock {
namespace p2p {
template <typename T>
static void DoSubmitPopPayload(
CNode* pfrom,
CConnman* connman,
const CNetMsgMaker& msgMaker,
altintegration::MemPool& mp,
const CInv& inv,
std::vector<CInv>& vNotFound)
{
const auto id = VeriBlock::Uint256ToId<T>(inv.hash);
auto* atv = mp.get<T>(id);
if (atv == nullptr) {
vNotFound.push_back(inv);
} else {
LogPrint(BCLog::NET, "sending %s to peer %d\n", atv->toShortPrettyString(), pfrom->GetId());
connman->PushMessage(pfrom, msgMaker.Make(T::name(), *atv));
}
}
void ProcessGetPopPayloads(
std::deque<CInv>::iterator& it,
CNode* pfrom,
CConnman* connman,
const std::atomic<bool>& interruptMsgProc,
std::vector<CInv>& vNotFound) EXCLUSIVE_LOCKS_REQUIRED(cs_main) // for pop mempool
{
AssertLockHeld(cs_main);
const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
auto& mp = VeriBlock::GetPop().getMemPool();
while (it != pfrom->vRecvGetData.end() && (it->type == MSG_POP_ATV || it->type == MSG_POP_VTB || it->type == MSG_POP_VBK)) {
if (interruptMsgProc) {
return;
}
// Don't bother if send buffer is too full to respond anyway
if (pfrom->fPauseSend) {
break;
}
const CInv& inv = *it;
++it;
if (inv.type == MSG_POP_ATV) {
DoSubmitPopPayload<altintegration::ATV>(pfrom, connman, msgMaker, mp, inv, vNotFound);
} else if (inv.type == MSG_POP_VTB) {
DoSubmitPopPayload<altintegration::VTB>(pfrom, connman, msgMaker, mp, inv, vNotFound);
} else if (inv.type == MSG_POP_VBK) {
DoSubmitPopPayload<altintegration::VbkBlock>(pfrom, connman, msgMaker, mp, inv, vNotFound);
}
}
}
void SendPopPayload(
CNode* pto,
CConnman* connman,
int typeIn,
CRollingBloomFilter& filterInventoryKnown,
std::set<uint256>& toSend,
std::vector<CInv>& vInv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
const CNetMsgMaker msgMaker(pto->GetSendVersion());
for (const auto& hash : toSend) {
CInv inv(typeIn, hash);
if (filterInventoryKnown.contains(hash)) {
LogPrint(BCLog::NET, "inv %s is known by peer %d (bloom filtered)\n", inv.ToString(), pto->GetId());
continue;
}
vInv.push_back(inv);
filterInventoryKnown.insert(hash);
if (vInv.size() == MAX_INV_SZ) {
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
vInv.clear();
}
}
toSend.clear();
}
} // namespace p2p
} // namespace VeriBlock