Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
// Copyright (c) 2018-2019 The Bitcoin developers | // Copyright (c) 2018-2019 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/processor.h> | #include <avalanche/processor.h> | ||||
#include <avalanche/delegationbuilder.h> | |||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <key_io.h> // For DecodeSecret | #include <key_io.h> // For DecodeSecret | ||||
#include <net_processing.h> // For Misbehaving | #include <net_processing.h> // For Misbehaving | ||||
#include <netmessagemaker.h> | #include <netmessagemaker.h> | ||||
#include <reverse_iterator.h> | #include <reverse_iterator.h> | ||||
#include <scheduler.h> | #include <scheduler.h> | ||||
#include <util/bitmanip.h> | #include <util/bitmanip.h> | ||||
▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | static bool IsWorthPolling(const CBlockIndex *pindex) { | ||||
if (::ChainstateActive().IsBlockFinalized(pindex)) { | if (::ChainstateActive().IsBlockFinalized(pindex)) { | ||||
// There is no point polling finalized block. | // There is no point polling finalized block. | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
struct Processor::PeerData { | |||||
Proof proof; | |||||
Delegation delegation; | |||||
}; | |||||
class Processor::NotificationsHandler | class Processor::NotificationsHandler | ||||
: public interfaces::Chain::Notifications { | : public interfaces::Chain::Notifications { | ||||
Processor *m_processor; | Processor *m_processor; | ||||
public: | public: | ||||
NotificationsHandler(Processor *p) : m_processor(p) {} | NotificationsHandler(Processor *p) : m_processor(p) {} | ||||
void updatedBlockTip() override { | void updatedBlockTip() override { | ||||
LOCK(m_processor->cs_peerManager); | LOCK(m_processor->cs_peerManager); | ||||
m_processor->peerManager->updatedBlockTip(); | m_processor->peerManager->updatedBlockTip(); | ||||
} | } | ||||
}; | }; | ||||
Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn) | Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn) | ||||
: connman(connmanIn), queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), | : connman(connmanIn), queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), | ||||
round(0), peerManager(std::make_unique<PeerManager>()) { | round(0), peerManager(std::make_unique<PeerManager>()) { | ||||
if (gArgs.IsArgSet("-avasessionkey")) { | if (gArgs.IsArgSet("-avasessionkey")) { | ||||
sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); | sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); | ||||
} else { | } else { | ||||
// Pick a random key for the session. | // Pick a random key for the session. | ||||
sessionKey.MakeNewKey(true); | sessionKey.MakeNewKey(true); | ||||
} | } | ||||
if (gArgs.IsArgSet("-avaproof")) { | |||||
peerData = std::make_unique<PeerData>(); | |||||
{ | |||||
// The proof. | |||||
CDataStream stream(ParseHex(gArgs.GetArg("-avaproof", "")), | |||||
SER_NETWORK, 0); | |||||
stream >> peerData->proof; | |||||
LOCK(cs_peerManager); | |||||
peerManager->getPeer(peerData->proof); | |||||
} | |||||
// Generate the delegation to the session key. | |||||
DelegationBuilder dgb(peerData->proof); | |||||
if (sessionKey.GetPubKey() != peerData->proof.getMaster()) { | |||||
dgb.addLevel(DecodeSecret(gArgs.GetArg("-avamasterkey", "")), | |||||
sessionKey.GetPubKey()); | |||||
} | |||||
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)); | ||||
} | } | ||||
Processor::~Processor() { | Processor::~Processor() { | ||||
chainNotificationsHandler.reset(); | chainNotificationsHandler.reset(); | ||||
stopEventLoop(); | stopEventLoop(); | ||||
▲ Show 20 Lines • Show All 191 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 { | |||||
return sessionKey.GetPubKey(); | |||||
} | |||||
bool Processor::startEventLoop(CScheduler &scheduler) { | bool Processor::startEventLoop(CScheduler &scheduler) { | ||||
return eventLoop.startEventLoop( | return eventLoop.startEventLoop( | ||||
scheduler, [this]() { this->runEventLoop(); }, AVALANCHE_TIME_STEP); | scheduler, [this]() { this->runEventLoop(); }, AVALANCHE_TIME_STEP); | ||||
} | } | ||||
bool Processor::stopEventLoop() { | bool Processor::stopEventLoop() { | ||||
return eventLoop.stopEventLoop(); | return eventLoop.stopEventLoop(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 153 Lines • Show Last 20 Lines |