diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -250,12 +250,6 @@ class NotificationsHandler; std::unique_ptr chainNotificationsHandler; - /** - * Flag indicating that the proof must be registered at first new block - * after IBD - */ - bool mustRegisterProof = false; - Processor(interfaces::Chain &chain, CConnman *connmanIn, NodePeerManager *nodePeerManagerIn, std::unique_ptr peerDataIn, CKey sessionKeyIn); @@ -292,6 +286,7 @@ std::shared_ptr getLocalProof() const; Peer::Timestamp getProofTime(const ProofId &proofid) const; std::shared_ptr getOrphan(const ProofId &proofid) const; + bool registerLocalProof(); /* * Return whether the avalanche service flag should be set. diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -209,12 +209,6 @@ void updatedBlockTip() override { LOCK(m_processor->cs_peerManager); - if (m_processor->mustRegisterProof && - !::ChainstateActive().IsInitialBlockDownload()) { - m_processor->peerManager->getPeerId(m_processor->peerData->proof); - m_processor->mustRegisterProof = false; - } - m_processor->peerManager->updatedBlockTip(); } }; @@ -225,10 +219,7 @@ : connman(connmanIn), nodePeerManager(nodePeerManagerIn), queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), round(0), peerManager(std::make_unique()), - peerData(std::move(peerDataIn)), sessionKey(std::move(sessionKeyIn)), - // Schedule proof registration at the first new block after IBD. - // FIXME: get rid of this flag - mustRegisterProof(!!peerData) { + peerData(std::move(peerDataIn)), sessionKey(std::move(sessionKeyIn)) { // Make sure we get notified of chain state changes. chainNotificationsHandler = chain.handleNotifications(std::make_shared(this)); @@ -594,6 +585,18 @@ return peerManager->getOrphan(proofid); } +bool Processor::registerLocalProof() { + auto localProof = getLocalProof(); + if (!localProof) { + return false; + } + if (!addProof(localProof) && !getOrphan(localProof->getId())) { + return false; + } + // Proof successfully added as a peer or orphan. + return true; +} + bool Processor::startEventLoop(CScheduler &scheduler) { return eventLoop.startEventLoop( scheduler, [this]() { this->runEventLoop(); }, AVALANCHE_TIME_STEP); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -3017,6 +3017,12 @@ }, DUMP_BANS_INTERVAL); + // Register local proof if available + if (g_avalanche->getLocalProof() && !g_avalanche->registerLocalProof()) { + // TODO: try to get a better error message. + return InitError(_("Invalid proof")); + } + // Start Avalanche's event loop. g_avalanche->startEventLoop(*node.scheduler); diff --git a/test/functional/abc_rpc_avalancheproof.py b/test/functional/abc_rpc_avalancheproof.py --- a/test/functional/abc_rpc_avalancheproof.py +++ b/test/functional/abc_rpc_avalancheproof.py @@ -101,23 +101,13 @@ assert_raises_rpc_error(-22, "Proof has invalid format", node.decodeavalancheproof, proof[:-2]) - # Restart the node, making sure it is initially in IBD mode - minchainwork = int(node.getblockchaininfo()["chainwork"], 16) + 1 + # Restart the node with this proof self.restart_node(0, self.extra_args[0] + [ "-avaproof={}".format(proof), "-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", - "-minimumchainwork=0x{:x}".format(minchainwork), ]) - self.log.info( - "The proof verification should be delayed until IBD is complete") - assert node.getblockchaininfo()["initialblockdownload"] is True - # Our proof cannot be verified during IBD, so we should have no peer - assert not node.getavalanchepeerinfo() - # Mining one more block should cause us to leave IBD - node.generate(1) - # Our proof is now verified and our node is added as a peer - assert node.getblockchaininfo()["initialblockdownload"] is False + self.log.info("The proof is registered") wait_until(lambda: len(node.getavalanchepeerinfo()) == 1, timeout=5) if self.is_wallet_compiled():