Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
Show First 20 Lines • Show All 132 Lines • ▼ Show 20 Lines | 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 { | struct Processor::PeerData { | ||||
Proof proof; | std::shared_ptr<Proof> proof; | ||||
Delegation delegation; | 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); | ||||
if (m_processor->mustRegisterProof && | if (m_processor->mustRegisterProof && | ||||
!::ChainstateActive().IsInitialBlockDownload()) { | !::ChainstateActive().IsInitialBlockDownload()) { | ||||
m_processor->peerManager->getPeerId( | m_processor->peerManager->getPeerId(m_processor->peerData->proof); | ||||
std::make_shared<Proof>(m_processor->peerData->proof)); | |||||
m_processor->mustRegisterProof = false; | m_processor->mustRegisterProof = false; | ||||
} | } | ||||
m_processor->peerManager->updatedBlockTip(); | m_processor->peerManager->updatedBlockTip(); | ||||
} | } | ||||
}; | }; | ||||
Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, | Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | if (argsman.IsArgSet("-avaproof")) { | ||||
masterKey = DecodeSecret(argsman.GetArg("-avamasterkey", "")); | masterKey = DecodeSecret(argsman.GetArg("-avamasterkey", "")); | ||||
if (!masterKey.IsValid()) { | if (!masterKey.IsValid()) { | ||||
error = _("the avalanche master key is invalid"); | error = _("the avalanche master key is invalid"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
peerData = std::make_unique<PeerData>(); | peerData = std::make_unique<PeerData>(); | ||||
if (!Proof::FromHex(peerData->proof, argsman.GetArg("-avaproof", ""), | peerData->proof = std::make_shared<Proof>(); | ||||
if (!Proof::FromHex(*peerData->proof, argsman.GetArg("-avaproof", ""), | |||||
error)) { | error)) { | ||||
// error is set by FromHex | // error is set by FromHex | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
ProofValidationState proof_state; | ProofValidationState proof_state; | ||||
if (!peerData->proof.verify(proof_state)) { | if (!peerData->proof->verify(proof_state)) { | ||||
switch (proof_state.GetResult()) { | switch (proof_state.GetResult()) { | ||||
case ProofValidationResult::NO_STAKE: | case ProofValidationResult::NO_STAKE: | ||||
error = _("the avalanche proof has no stake"); | error = _("the avalanche proof has no stake"); | ||||
return nullptr; | return nullptr; | ||||
case ProofValidationResult::DUST_THRESOLD: | case ProofValidationResult::DUST_THRESOLD: | ||||
error = _("the avalanche proof stake is too low"); | error = _("the avalanche proof stake is too low"); | ||||
return nullptr; | return nullptr; | ||||
case ProofValidationResult::DUPLICATE_STAKE: | case ProofValidationResult::DUPLICATE_STAKE: | ||||
Show All 10 Lines | if (argsman.IsArgSet("-avaproof")) { | ||||
return nullptr; | return nullptr; | ||||
default: | default: | ||||
error = _("the avalanche proof is invalid"); | error = _("the avalanche proof is invalid"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | } | ||||
// Generate the delegation to the session key. | // Generate the delegation to the session key. | ||||
DelegationBuilder dgb(peerData->proof); | DelegationBuilder dgb(*peerData->proof); | ||||
if (sessionKey.GetPubKey() != peerData->proof.getMaster()) { | if (sessionKey.GetPubKey() != peerData->proof->getMaster()) { | ||||
dgb.addLevel(masterKey, sessionKey.GetPubKey()); | dgb.addLevel(masterKey, sessionKey.GetPubKey()); | ||||
} | } | ||||
peerData->delegation = dgb.build(); | peerData->delegation = dgb.build(); | ||||
} | } | ||||
// We can't use std::make_unique with a private constructor | // We can't use std::make_unique with a private constructor | ||||
return std::unique_ptr<Processor>( | return std::unique_ptr<Processor>( | ||||
new Processor(chain, connman, nodePeerManager, std::move(peerData), | new Processor(chain, connman, nodePeerManager, std::move(peerData), | ||||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | std::map<CBlockIndex *, Vote> responseIndex; | ||||
: BlockUpdate::Status::Invalid); | : BlockUpdate::Status::Invalid); | ||||
w->erase(it); | w->erase(it); | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool Processor::addNode(NodeId nodeid, const Proof &proof, | bool Processor::addNode(NodeId nodeid, const std::shared_ptr<Proof> &proof, | ||||
const Delegation &delegation) { | const Delegation &delegation) { | ||||
LOCK(cs_peerManager); | LOCK(cs_peerManager); | ||||
return peerManager->addNode(nodeid, std::make_shared<Proof>(proof), | return peerManager->addNode(nodeid, proof, delegation); | ||||
delegation); | |||||
} | } | ||||
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)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 224 Lines • Show Last 20 Lines |