Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
Show First 20 Lines • Show All 155 Lines • ▼ Show 20 Lines | void updatedBlockTip() override { | ||||
m_processor->peerManager->getPeerId(m_processor->peerData->proof); | m_processor->peerManager->getPeerId(m_processor->peerData->proof); | ||||
m_processor->mustRegisterProof = false; | m_processor->mustRegisterProof = false; | ||||
} | } | ||||
m_processor->peerManager->updatedBlockTip(); | m_processor->peerManager->updatedBlockTip(); | ||||
} | } | ||||
}; | }; | ||||
const std::string Processor::Init(interfaces::Chain &chain, CConnman *connmanIn, | |||||
deadalnix: The rest of the codebase returns a `bool` and pass the error as an out parameter. But in this… | |||||
NodePeerManager *nodePeerManagerIn) { | |||||
g_avalanche = | |||||
std::make_unique<Processor>(chain, connmanIn, nodePeerManagerIn); | |||||
deadalnixUnsubmitted Not Done Inline ActionsIf you extract part of the logic of the constructor in there, You won't need to twiddle with the internal state of the processor after the fact. Inf act, do you want to build a processor in an invalid state? that doesn't seems like the most solid design. deadalnix: If you extract part of the logic of the constructor in there, You won't need to twiddle with… | |||||
// If avalanche is enabled and a proof is supplied, make sure it does | |||||
// not contain garbage. At this point the validity of the utxos cannot | |||||
// be checked, so only basic verification is performed. | |||||
if (gArgs.IsArgSet("-avaproof")) { | |||||
ProofValidationState proof_state; | |||||
if (!g_avalanche->peerData->proof.verify(proof_state)) { | |||||
switch (proof_state.GetResult()) { | |||||
case avalanche::ProofValidationResult::NO_STAKE: | |||||
return "the avalanche proof has no stake"; | |||||
case avalanche::ProofValidationResult::DUST_THRESOLD: | |||||
return "the avalanche proof stake is too low"; | |||||
case avalanche::ProofValidationResult::DUPLICATE_STAKE: | |||||
return "the avalanche proof has duplicated stake"; | |||||
case avalanche::ProofValidationResult::INVALID_SIGNATURE: | |||||
return "the avalanche proof has invalid stake signatures"; | |||||
case avalanche::ProofValidationResult::TOO_MANY_UTXOS: | |||||
return "the avalanche proof has too many utxos"; | |||||
default: | |||||
return "the avalanche proof is invalid"; | |||||
} | |||||
} | |||||
} | |||||
return ""; | |||||
} | |||||
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", "")); | sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); | ||||
} else { | } else { | ||||
Show All 16 Lines | if (gArgs.IsArgSet("-avaproof")) { | ||||
// 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(DecodeSecret(gArgs.GetArg("-avamasterkey", "")), | dgb.addLevel(DecodeSecret(gArgs.GetArg("-avamasterkey", "")), | ||||
sessionKey.GetPubKey()); | sessionKey.GetPubKey()); | ||||
} | } | ||||
peerData->delegation = dgb.build(); | peerData->delegation = dgb.build(); | ||||
} else if (gArgs.GetBoolArg("-enableavalanche", | |||||
AVALANCHE_DEFAULT_ENABLED)) { | |||||
LogPrintf("Avalanche is enabled but no proof supplied, the node " | |||||
"will not be able to vote\n"); | |||||
deadalnixUnsubmitted Not Done Inline ActionsThis assumes intent rather than be informative. deadalnix: This assumes intent rather than be informative. | |||||
} | } | ||||
// 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() { | ||||
▲ Show 20 Lines • Show All 421 Lines • Show Last 20 Lines |
The rest of the codebase returns a bool and pass the error as an out parameter. But in this case, is it even necessary? Why can't this call InitError directly? This is clear meant to initialize things, because if it isn't, then the function needs to be renamed. To me, it seems more like this function is there to ensure that the processor is in some valid state. It seems that initializing g_avalancheis therefore out of scope (after all, the validation logic is not specific to g_avalanche, that same logic would be just as valid for any other instance) and not really about initialization either (which, to begin with belong in init.cpp, but second, this logic remains valid whether it is run during the node startup or at another point in time).
What you want is a factory method.