diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -267,6 +267,9 @@ NodePeerManager *nodePeerManagerIn); ~Processor(); + static const std::string Init(interfaces::Chain &chain, CConnman *connmanIn, + NodePeerManager *nodePeerManagerIn); + void setQueryTimeoutDuration(std::chrono::milliseconds d) { queryTimeoutDuration = d; } diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -161,6 +161,36 @@ } }; +const std::string Processor::Init(interfaces::Chain &chain, CConnman *connmanIn, + NodePeerManager *nodePeerManagerIn) { + g_avalanche = + std::make_unique(chain, connmanIn, nodePeerManagerIn); + + // 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, NodePeerManager *nodePeerManagerIn) : connman(connmanIn), nodePeerManager(nodePeerManagerIn), @@ -193,6 +223,10 @@ sessionKey.GetPubKey()); } 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"); } // Make sure we get notified of chain state changes. diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -2432,48 +2432,15 @@ } // Step 6.5 (I guess ?): Initialize Avalanche. - g_avalanche = std::make_unique( + std::string avalancheInitError = avalanche::Processor::Init( *node.chain, node.connman.get(), node.peerman.get()); + if (!avalancheInitError.empty()) { + InitError(_(avalancheInitError.c_str())); + return false; + } + if (args.GetBoolArg("-enableavalanche", AVALANCHE_DEFAULT_ENABLED)) { nLocalServices = ServiceFlags(nLocalServices | NODE_AVALANCHE); - - // 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. - const avalanche::Proof *proof = g_avalanche->getLocalProof(); - - if (proof) { - avalanche::ProofValidationState proof_state; - if (!proof->verify(proof_state)) { - switch (proof_state.GetResult()) { - case avalanche::ProofValidationResult::NO_STAKE: - InitError(_("the avalanche proof has no stake")); - return false; - case avalanche::ProofValidationResult::DUST_THRESOLD: - InitError(_("the avalanche proof stake is too low")); - return false; - case avalanche::ProofValidationResult::DUPLICATE_STAKE: - InitError( - _("the avalanche proof has duplicated stake")); - return false; - case avalanche::ProofValidationResult::INVALID_SIGNATURE: - InitError(_("the avalanche proof has invalid stake " - "signatures")); - return false; - case avalanche::ProofValidationResult::TOO_MANY_UTXOS: - InitError(strprintf(_("the avalanche proof has too " - "many utxos (max: %u)"), - AVALANCHE_MAX_PROOF_STAKES)); - return false; - default: - InitError(_("the avalanche proof is invalid")); - return false; - } - } - } else { - LogPrintf("Avalanche is enabled but no proof supplied, the node " - "will not be able to vote\n"); - } } // Step 7: load block chain