From 37ebd7e71129ad42cb1b7458aefb679d59629ab8 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 15 Aug 2026 15:33:07 -0500 Subject: [PATCH 1/3] feat(evo): expose whether an operator key is in use at the chain tip Add an interfaces::EVO query predicate answering whether an operator BLS public key is assigned to any masternode in the deterministic list at the current chain tip, probing both BLS scheme encodings. This is a per-key chainstate query for callers that select fresh operator keys; it is a UX guard rather than a safety mechanism, so an unready node answers false. Co-Authored-By: Claude Fable 5 Co-Authored-By: GPT-6.1-Sol --- src/interfaces/node.h | 10 +++++++ src/node/interfaces.cpp | 19 +++++++++++++ src/qt/test/masternodewidgettests.cpp | 1 + src/test/evo_deterministicmns_tests.cpp | 36 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/src/interfaces/node.h b/src/interfaces/node.h index 5713fceaa88e..31fc62e3769e 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -32,6 +32,7 @@ class BanMan; class CBlockIndex; +class CBLSPublicKey; class CDeterministicMNList; class CFeeRate; class CGovernanceObject; @@ -182,6 +183,15 @@ class EVO virtual ProviderTxResult updateShare(Wallet& wallet, const SharedUpdateShareRequest& request) = 0; virtual ProviderTxResult prepareSharedRegistrarUpdate( Wallet& wallet, const SharedRegistrarUpdatePrepareRequest& request) = 0; + /** + * Whether an operator public key is assigned to any masternode in the + * deterministic list at the current chain tip, under either BLS scheme + * encoding. This is a UX guard for skipping keys that would be rejected + * by DIP3 duplicate-key checks, not a safety mechanism: when the node is + * not ready to answer (no tip or no masternode manager yet), it returns + * false. Keys used only historically also return false. + */ + virtual bool isMasternodeOperatorKeyInUse(const CBLSPublicKey& public_key) = 0; virtual void setContext(node::NodeContext* context) {} }; diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 2819541d62c2..217bdc61ba26 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -335,6 +335,25 @@ class EVOImpl : public EVO { return evo::provider::PrepareSharedRegistrarUpdate(context(), wallet, request); } + bool isMasternodeOperatorKeyInUse(const CBLSPublicKey& public_key) override + { + if (!m_context || !m_context->chainman || !m_context->dmnman) return false; + const CBlockIndex* tip{WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip())}; + if (!tip) return false; + CDeterministicMNList mn_list; + try { + mn_list = m_context->dmnman->GetListForBlock(tip); + } catch (const BlockDataUnavailableError& e) { + // Expected while a snapshot's background chainstate is still + // catching up; this predicate fails open by design. Any other + // exception means local EvoDB/list corruption and must not be + // hidden, so it deliberately stays unhandled. + LogPrintf("%s -- masternode list unavailable: %s\n", __func__, e.what()); + return false; + } + if (mn_list.GetBlockHash().IsNull()) return false; + return mn_list.HasOperatorKeyUnderAnyScheme(public_key, /*self=*/uint256()); + } void setContext(NodeContext* context) override { m_context = context; diff --git a/src/qt/test/masternodewidgettests.cpp b/src/qt/test/masternodewidgettests.cpp index 9dac30676fc7..903358e352f1 100644 --- a/src/qt/test/masternodewidgettests.cpp +++ b/src/qt/test/masternodewidgettests.cpp @@ -195,6 +195,7 @@ class BlockingEVO final : public interfaces::EVO { return TestProviderError(); } + bool isMasternodeOperatorKeyInUse(const CBLSPublicKey&) override { return false; } QSemaphore entered; QSemaphore proceed; diff --git a/src/test/evo_deterministicmns_tests.cpp b/src/test/evo_deterministicmns_tests.cpp index 2cc66706d5ef..50c1e2de16f7 100644 --- a/src/test/evo_deterministicmns_tests.cpp +++ b/src/test/evo_deterministicmns_tests.cpp @@ -1883,6 +1883,42 @@ BOOST_AUTO_TEST_CASE(v19_activation_legacy) FuncV19Activation(setup); } +BOOST_AUTO_TEST_CASE(operator_key_in_use_follows_current_list) +{ + TestMNChainSetup setup(DIP3_ACTIVATION_HEIGHT - 2, {"-dip3params=109:500"}); + setup.ProcessBlock(); // The next block may contain DIP3 transactions. + + auto node{interfaces::MakeNode(setup.m_node)}; + auto in_use = [&](const CBLSSecretKey& key) { return node->evo().isMasternodeOperatorKeyInUse(key.GetPublicKey()); }; + + BOOST_CHECK(!node->evo().isMasternodeOperatorKeyInUse(CBLSPublicKey{})); + + CKey owner_key; + CBLSSecretKey registered_key; + auto tx_reg{CreateProRegTx(setup.chainman, setup.utxos, 19999, GenerateRandomAddress(), setup.coinbaseKey, + owner_key, registered_key)}; + BOOST_CHECK(!in_use(registered_key)); + setup.ProcessBlock({tx_reg}); + BOOST_CHECK(in_use(registered_key)); + + // Rotating the operator key makes the old key immediately reusable: the + // predicate answers for the current list, not for historical assignments. + CBLSSecretKey rotated_key; + rotated_key.MakeNewKey(); + auto tx_upreg{CreateProUpRegTx(setup.chainman, setup.utxos, tx_reg.GetHash(), owner_key, rotated_key.GetPublicKey(), + owner_key.GetPubKey().GetID(), GenerateRandomAddress(), setup.coinbaseKey)}; + setup.ProcessBlock({tx_upreg}); + BOOST_CHECK(!in_use(registered_key)); + BOOST_CHECK(in_use(rotated_key)); + + // Revocation clears the operator key in the list while the masternode entry remains. + auto tx_revoke{CreateProUpRevTx(setup.chainman, setup.utxos, tx_reg.GetHash(), rotated_key, setup.coinbaseKey)}; + setup.ProcessBlock({tx_revoke}); + BOOST_REQUIRE(setup.dmnman.GetListAtChainTip().HasMN(tx_reg.GetHash())); + BOOST_CHECK(!in_use(registered_key)); + BOOST_CHECK(!in_use(rotated_key)); +} + // The invariant this whole change rests on: a stored operator key never advertises a scheme its own // state version contradicts, so the live list and the same list reloaded from disk agree — including // mnUniquePropertyMap, which IsEqual() compares directly. From 4d143f41614578e8ecf2907367e3b4526f3acf2f Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 3 Oct 2026 19:36:25 -0500 Subject: [PATCH 2/3] test: move FailDatabase wallet test helper into wallet/test/util.h FailCursor, FailBatch and FailDatabase were private to wallet_tests.cpp. Moving them unchanged into the shared wallet test utilities lets other wallet test suites exercise database write failures without copying the mock. Upstream later replaces this mock with MockableDatabase (bitcoin#26715); this move keeps a single definition until that lands. Co-Authored-By: Claude Opus 5.5 (1M context) Co-Authored-By: GPT-6.1-Sol --- src/wallet/test/util.h | 61 ++++++++++++++++++++++++++++++++ src/wallet/test/wallet_tests.cpp | 55 ---------------------------- 2 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/wallet/test/util.h b/src/wallet/test/util.h index 9621d043485e..fccf847bb31e 100644 --- a/src/wallet/test/util.h +++ b/src/wallet/test/util.h @@ -10,6 +10,8 @@ #endif #include