Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
Show First 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, | Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, | ||||
NodePeerManager *nodePeerManagerIn) | NodePeerManager *nodePeerManagerIn) | ||||
: connman(connmanIn), nodePeerManager(nodePeerManagerIn), | : connman(connmanIn), nodePeerManager(nodePeerManagerIn), | ||||
queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), round(0), | queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), round(0), | ||||
peerManager(std::make_unique<PeerManager>()) { | peerManager(std::make_unique<PeerManager>()) { | ||||
if (gArgs.IsArgSet("-avasessionkey")) { | if (gArgs.IsArgSet("-avasessionkey")) { | ||||
sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); | peerData.sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); | ||||
} else { | } else { | ||||
// Pick a random key for the session. | // Pick a random key for the session. | ||||
sessionKey.MakeNewKey(true); | peerData.sessionKey.MakeNewKey(true); | ||||
} | } | ||||
if (gArgs.IsArgSet("-avaproof")) { | if (gArgs.IsArgSet("-avaproof")) { | ||||
{ | { | ||||
// The proof. | // The proof. | ||||
CDataStream stream(ParseHex(gArgs.GetArg("-avaproof", "")), | CDataStream stream(ParseHex(gArgs.GetArg("-avaproof", "")), | ||||
SER_NETWORK, 0); | SER_NETWORK, 0); | ||||
Proof proof; | Proof proof; | ||||
stream >> proof; | stream >> proof; | ||||
peerData.proof = proof; | peerData.proof = proof; | ||||
// Schedule proof registration at the first new block after IBD. | // Schedule proof registration at the first new block after IBD. | ||||
mustRegisterProof = true; | mustRegisterProof = true; | ||||
} | } | ||||
// Generate the delegation to the session key. | // Generate the delegation to the session key. | ||||
DelegationBuilder dgb(peerData.proof.value()); | DelegationBuilder dgb(peerData.proof.value()); | ||||
if (sessionKey.GetPubKey() != peerData.proof->getMaster()) { | if (peerData.sessionKey.GetPubKey() != peerData.proof->getMaster()) { | ||||
dgb.addLevel(DecodeSecret(gArgs.GetArg("-avamasterkey", "")), | dgb.addLevel(DecodeSecret(gArgs.GetArg("-avamasterkey", "")), | ||||
sessionKey.GetPubKey()); | peerData.sessionKey.GetPubKey()); | ||||
} | } | ||||
peerData.delegation = dgb.build(); | peerData.delegation = dgb.build(); | ||||
} | } | ||||
// Make sure we get notified of chain state changes. | // Make sure we get notified of chain state changes. | ||||
chainNotificationsHandler = | chainNotificationsHandler = | ||||
chain.handleNotifications(std::make_shared<NotificationsHandler>(this)); | chain.handleNotifications(std::make_shared<NotificationsHandler>(this)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | public: | ||||
// serialization support | // serialization support | ||||
SERIALIZE_METHODS(TCPResponse, obj) { | SERIALIZE_METHODS(TCPResponse, obj) { | ||||
READWRITE(obj.response, obj.sig); | READWRITE(obj.response, obj.sig); | ||||
} | } | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
void Processor::sendResponse(CNode *pfrom, Response response) const { | void Processor::sendResponse(CNode *pfrom, Response response) const { | ||||
connman->PushMessage( | connman->PushMessage(pfrom, CNetMsgMaker(pfrom->GetCommonVersion()) | ||||
pfrom, CNetMsgMaker(pfrom->GetCommonVersion()) | |||||
.Make(NetMsgType::AVARESPONSE, | .Make(NetMsgType::AVARESPONSE, | ||||
TCPResponse(std::move(response), sessionKey))); | TCPResponse(std::move(response), | ||||
peerData.sessionKey))); | |||||
} | } | ||||
bool Processor::registerVotes(NodeId nodeid, const Response &response, | bool Processor::registerVotes(NodeId nodeid, const Response &response, | ||||
std::vector<BlockUpdate> &updates) { | std::vector<BlockUpdate> &updates) { | ||||
{ | { | ||||
// Save the time at which we can query again. | // Save the time at which we can query again. | ||||
LOCK(cs_peerManager); | LOCK(cs_peerManager); | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | |||||
bool Processor::forNode(NodeId nodeid, | bool Processor::forNode(NodeId nodeid, | ||||
std::function<bool(const Node &n)> func) const { | std::function<bool(const Node &n)> func) const { | ||||
LOCK(cs_peerManager); | LOCK(cs_peerManager); | ||||
return peerManager->forNode(nodeid, std::move(func)); | return peerManager->forNode(nodeid, std::move(func)); | ||||
} | } | ||||
CPubKey Processor::getSessionPubKey() const { | CPubKey Processor::getSessionPubKey() const { | ||||
return sessionKey.GetPubKey(); | return peerData.sessionKey.GetPubKey(); | ||||
} | } | ||||
uint256 Processor::buildLocalSighash(CNode *pfrom) const { | uint256 Processor::buildLocalSighash(CNode *pfrom) const { | ||||
CHashWriter hasher(SER_GETHASH, 0); | CHashWriter hasher(SER_GETHASH, 0); | ||||
hasher << peerData.delegation->getId(); | hasher << peerData.delegation->getId(); | ||||
hasher << pfrom->GetLocalNonce(); | hasher << pfrom->GetLocalNonce(); | ||||
hasher << pfrom->nRemoteHostNonce; | hasher << pfrom->nRemoteHostNonce; | ||||
hasher << pfrom->GetLocalExtraEntropy(); | hasher << pfrom->GetLocalExtraEntropy(); | ||||
Show All 18 Lines | bool Processor::sendHello(CNode *pfrom) const { | ||||
} | } | ||||
// Now let's sign! | // Now let's sign! | ||||
SchnorrSig sig; | SchnorrSig sig; | ||||
{ | { | ||||
const uint256 hash = buildLocalSighash(pfrom); | const uint256 hash = buildLocalSighash(pfrom); | ||||
if (!sessionKey.SignSchnorr(hash, sig)) { | if (!peerData.sessionKey.SignSchnorr(hash, sig)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
connman->PushMessage(pfrom, | connman->PushMessage(pfrom, | ||||
CNetMsgMaker(pfrom->GetCommonVersion()) | CNetMsgMaker(pfrom->GetCommonVersion()) | ||||
.Make(NetMsgType::AVAHELLO, | .Make(NetMsgType::AVAHELLO, | ||||
Hello(peerData.delegation.value(), sig))); | Hello(peerData.delegation.value(), sig))); | ||||
▲ Show 20 Lines • Show All 184 Lines • Show Last 20 Lines |