Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
Show First 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | static bool IsWorthPolling(const CBlockIndex *pindex) { | ||||
return true; | return true; | ||||
} | } | ||||
AvalancheProcessor::AvalancheProcessor(CConnman *connmanIn) | AvalancheProcessor::AvalancheProcessor(CConnman *connmanIn) | ||||
: connman(connmanIn), | : connman(connmanIn), | ||||
queryTimeoutDuration( | queryTimeoutDuration( | ||||
AVALANCHE_DEFAULT_QUERY_TIMEOUT_DURATION_MILLISECONDS), | AVALANCHE_DEFAULT_QUERY_TIMEOUT_DURATION_MILLISECONDS), | ||||
round(0), stopRequest(false), running(false) {} | round(0), stopRequest(false), running(false) { | ||||
// Pick a random key for the session. | |||||
sessionKey.MakeNewKey(true); | |||||
} | |||||
AvalancheProcessor::~AvalancheProcessor() { | AvalancheProcessor::~AvalancheProcessor() { | ||||
stopEventLoop(); | stopEventLoop(); | ||||
} | } | ||||
bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | ||||
bool isAccepted; | bool isAccepted; | ||||
Show All 27 Lines | int AvalancheProcessor::getConfidence(const CBlockIndex *pindex) const { | ||||
auto it = r->find(pindex); | auto it = r->find(pindex); | ||||
if (it == r.end()) { | if (it == r.end()) { | ||||
return -1; | return -1; | ||||
} | } | ||||
return it->second.getConfidence(); | return it->second.getConfidence(); | ||||
} | } | ||||
namespace { | |||||
/** | |||||
* When using TCP, we need to sign all messages as the transport layer is not | |||||
* secure. | |||||
*/ | |||||
class TCPAvalancheResponse { | |||||
AvalancheResponse response; | |||||
std::array<uint8_t, 64> sig; | |||||
public: | |||||
TCPAvalancheResponse(AvalancheResponse responseIn, const CKey &key) | |||||
: response(std::move(responseIn)) { | |||||
CHashWriter hasher(SER_GETHASH, 0); | |||||
hasher << response; | |||||
const uint256 hash = hasher.GetHash(); | |||||
// Now let's sign! | |||||
std::vector<uint8_t> vchSig; | |||||
if (key.SignSchnorr(hash, vchSig)) { | |||||
// Schnorr sigs are 64 bytes in size. | |||||
assert(vchSig.size() == 64); | |||||
std::copy(vchSig.begin(), vchSig.end(), sig.begin()); | |||||
} else { | |||||
sig.fill(0); | |||||
} | |||||
} | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(response); | |||||
READWRITE(sig); | |||||
} | |||||
}; | |||||
} // namespace | |||||
void AvalancheProcessor::sendResponse(CNode *pfrom, | void AvalancheProcessor::sendResponse(CNode *pfrom, | ||||
AvalancheResponse response) const { | AvalancheResponse response) const { | ||||
connman->PushMessage( | connman->PushMessage( | ||||
pfrom, CNetMsgMaker(pfrom->GetSendVersion()) | pfrom, | ||||
.Make(NetMsgType::AVARESPONSE, std::move(response))); | CNetMsgMaker(pfrom->GetSendVersion()) | ||||
.Make(NetMsgType::AVARESPONSE, | |||||
TCPAvalancheResponse(std::move(response), sessionKey))); | |||||
} | } | ||||
bool AvalancheProcessor::registerVotes( | bool AvalancheProcessor::registerVotes( | ||||
NodeId nodeid, const AvalancheResponse &response, | NodeId nodeid, const AvalancheResponse &response, | ||||
std::vector<AvalancheBlockUpdate> &updates) { | std::vector<AvalancheBlockUpdate> &updates) { | ||||
{ | { | ||||
// Save the time at which we can query again. | // Save the time at which we can query again. | ||||
auto w = peerSet.getWriteView(); | auto w = peerSet.getWriteView(); | ||||
▲ Show 20 Lines • Show All 319 Lines • Show Last 20 Lines |