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,10 @@ NodePeerManager *nodePeerManagerIn); ~Processor(); + static Processor *MakeProcessor(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 @@ -11,9 +11,12 @@ #include // For DecodeSecret #include // For ::PeerManager #include +#include #include #include #include +#include + #include #include @@ -161,6 +164,48 @@ } }; +Processor *Processor::MakeProcessor(interfaces::Chain &chain, + CConnman *connmanIn, + NodePeerManager *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")) { + Proof proof; + CDataStream stream(ParseHex(gArgs.GetArg("-avaproof", "")), SER_NETWORK, + 0); + stream >> proof; + + ProofValidationState proof_state; + if (!proof.verify(proof_state)) { + switch (proof_state.GetResult()) { + case avalanche::ProofValidationResult::NO_STAKE: + InitError(_("the avalanche proof has no stake")); + break; + case avalanche::ProofValidationResult::DUST_THRESOLD: + InitError(_("the avalanche proof stake is too low")); + break; + case avalanche::ProofValidationResult::DUPLICATE_STAKE: + InitError(_("the avalanche proof has duplicated stake")); + break; + case avalanche::ProofValidationResult::INVALID_SIGNATURE: + InitError( + _("the avalanche proof has invalid stake signatures")); + break; + case avalanche::ProofValidationResult::TOO_MANY_UTXOS: + InitError(_("the avalanche proof has too many utxos")); + break; + default: + InitError(_("the avalanche proof is invalid")); + } + return nullptr; + } + } + + Processor *proc = new Processor(chain, connmanIn, nodePeerManagerIn); + return proc; +} + Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, NodePeerManager *nodePeerManagerIn) : connman(connmanIn), nodePeerManager(nodePeerManagerIn), 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( + avalanche::Processor *proc = avalanche::Processor::MakeProcessor( *node.chain, node.connman.get(), node.peerman.get()); + if (!proc) { + return false; + } + g_avalanche.reset(proc); + 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