From 31e483548d1f19913d6dc69ef01b15f98452217e Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 27 Oct 2021 12:49:37 +0300 Subject: [PATCH] Introduce intermediate structure as helper one Signed-off-by: Anthony Fieroni --- src/interfaces/chain.cpp | 4 +-- src/interfaces/chain.h | 2 +- src/masternodes/masternodes.cpp | 37 ++++++++++++++++--------- src/masternodes/masternodes.h | 43 +++++++++++++++++++---------- src/masternodes/mn_checks.cpp | 5 ---- src/masternodes/rpc_masternodes.cpp | 3 +- 6 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 70a76e5091a..29e75314423 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -280,10 +280,10 @@ class ChainImpl : public Chain return pcustomcsview->CanSpend(nodeId, height); } - boost::optional mnExists(const uint256 & nodeId) const override + bool mnExists(const uint256 & nodeId) const override { LOCK(cs_main); - return pcustomcsview->GetMasternode(nodeId); + return pcustomcsview->GetMasternode(nodeId).has_value(); } std::unique_ptr existTokenGuessId(const std::string & str, DCT_ID & id) const override { diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 8c904912bd8..bb8b77de6a5 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -150,8 +150,8 @@ class Chain //! populates the values. virtual void findCoins(std::map& coins) = 0; + virtual bool mnExists(const uint256 & nodeId) const = 0; virtual bool mnCanSpend(const uint256 & nodeId, int height) const = 0; - virtual boost::optional mnExists(const uint256 & nodeId) const = 0; virtual std::unique_ptr existTokenGuessId(const std::string & str, DCT_ID & id) const = 0; //! Estimate fraction of total transactions verified if blocks up to diff --git a/src/masternodes/masternodes.cpp b/src/masternodes/masternodes.cpp index b7f27d30512..aca9b62b4c8 100644 --- a/src/masternodes/masternodes.cpp +++ b/src/masternodes/masternodes.cpp @@ -78,8 +78,6 @@ CMasternode::CMasternode() , operatorType(0) , creationHeight(0) , resignHeight(-1) - , version(-1) - , resignTx() { } @@ -158,9 +156,7 @@ bool operator==(CMasternode const & a, CMasternode const & b) a.operatorType == b.operatorType && a.operatorAuthAddress == b.operatorAuthAddress && a.creationHeight == b.creationHeight && - a.resignHeight == b.resignHeight && - a.version == b.version && - a.resignTx == b.resignTx); + a.resignHeight == b.resignHeight); } bool operator!=(CMasternode const & a, CMasternode const & b) @@ -168,12 +164,25 @@ bool operator!=(CMasternode const & a, CMasternode const & b) return !(a == b); } +bool operator==(CMasternodeV2 const & a, CMasternodeV2 const & b) +{ + return static_cast(a) == static_cast(b) && + a.resignTx == b.resignTx && + a.rewardAddress == b.rewardAddress && + a.rewardAddressType == b.rewardAddressType; +} + +bool operator!=(CMasternodeV2 const & a, CMasternodeV2 const & b) +{ + return !(a == b); +} + /* * CMasternodesView */ -boost::optional CMasternodesView::GetMasternode(const uint256 & id) const +boost::optional CMasternodesView::GetMasternode(const uint256 & id) const { - return ReadBy(id); + return ReadBy(id); } boost::optional CMasternodesView::GetMasternodeV2(const uint256 & id, int height) const @@ -183,6 +192,7 @@ boost::optional CMasternodesView::GetMasternodeV2(const uint256 & } if (auto node = GetMasternode(id)) { CMasternodeV2 nodeV2{}; + nodeV2.resignTx = std::move(node->resignTx); static_cast(nodeV2) = std::move(*node); return nodeV2; } @@ -199,9 +209,9 @@ boost::optional CMasternodesView::GetMasternodeIdByOwner(const CKeyID & return ReadBy(id); } -void CMasternodesView::ForEachMasternode(std::function)> callback, uint256 const & start) +void CMasternodesView::ForEachMasternode(std::function)> callback, uint256 const & start) { - ForEach(callback, start); + ForEach(callback, start); } void CMasternodesView::ForEachMasternodeV2(std::function)> callback, uint256 const & start) @@ -211,14 +221,15 @@ void CMasternodesView::ForEachMasternodeV2(std::function nodes; - ForEachMasternode([&](const uint256& nodeId, CMasternode node) { + std::map nodes; + ForEachMasternode([&](const uint256& nodeId, CMasternodeV1 node) { nodes.emplace(nodeId, std::move(node)); return true; }); CMasternodeV2 nodeV2{}; - for (const auto& node : nodes) { - static_cast(nodeV2) = node.second; + for (auto& node : nodes) { + nodeV2.resignTx = std::move(node.second.resignTx); + static_cast(nodeV2) = std::move(node.second); WriteBy(node.first, nodeV2); } } diff --git a/src/masternodes/masternodes.h b/src/masternodes/masternodes.h index 109e6631bbf..5a4dc6ca3c8 100644 --- a/src/masternodes/masternodes.h +++ b/src/masternodes/masternodes.h @@ -61,11 +61,6 @@ class CMasternode TENYEAR = 520 }; - enum Version : int32_t { - PRE_FORT_CANNING = -1, - VERSION0 = 0, - }; - //! Minted blocks counter uint32_t mintedBlocks; @@ -81,11 +76,6 @@ class CMasternode int32_t creationHeight; //! Resign height int32_t resignHeight; - //! Was used to set a ban height but is now unused - int32_t version; - - //! This fields are for transaction rollback (by disconnecting block) - uint256 resignTx; //! empty constructor CMasternode(); @@ -111,9 +101,6 @@ class CMasternode READWRITE(creationHeight); READWRITE(resignHeight); - READWRITE(version); - - READWRITE(resignTx); } //! equality test @@ -121,8 +108,29 @@ class CMasternode friend bool operator!=(CMasternode const & a, CMasternode const & b); }; +// intermidate data structure for compatibility +class CMasternodeV1 : public CMasternode { +public: + //! Was used to set a ban height but is now unused + int32_t version; + + //! This fields are for transaction rollback (by disconnecting block) + uint256 resignTx; + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action) + { + READWRITEAS(CMasternode, *this); + READWRITE(version); + READWRITE(resignTx); + } +}; + class CMasternodeV2 : public CMasternode { public: + uint256 resignTx; //! Consensus-enforced address for operator rewards. CKeyID rewardAddress; char rewardAddressType; @@ -133,9 +141,14 @@ class CMasternodeV2 : public CMasternode { inline void SerializationOp(Stream& s, Operation ser_action) { READWRITEAS(CMasternode, *this); + READWRITE(resignTx); READWRITE(rewardAddressType); READWRITE(rewardAddress); } + + //! equality test + friend bool operator==(CMasternodeV2 const & a, CMasternodeV2 const & b); + friend bool operator!=(CMasternodeV2 const & a, CMasternodeV2 const & b); }; struct MNBlockTimeKey @@ -190,11 +203,11 @@ class CMasternodesView : public virtual CStorageView // CMasternodesView() = default; void MigrateMasternodesToV2(); - boost::optional GetMasternode(uint256 const & id) const; + boost::optional GetMasternode(uint256 const & id) const; boost::optional GetMasternodeIdByOperator(CKeyID const & id) const; boost::optional GetMasternodeIdByOwner(CKeyID const & id) const; boost::optional GetMasternodeV2(uint256 const & id, int height) const; - void ForEachMasternode(std::function)> callback, uint256 const & start = uint256()); + void ForEachMasternode(std::function)> callback, uint256 const & start = uint256()); void ForEachMasternodeV2(std::function)> callback, uint256 const & start = uint256()); void IncrementMintedBy(const uint256& nodeId, int height); diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 5b0221dc5f9..2401d9c6474 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -875,11 +875,6 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor node.operatorType = obj.operatorType; node.operatorAuthAddress = obj.operatorAuthAddress; - // Set masternode version2 after FC for new serialisation - if (height >= static_cast(Params().GetConsensus().FortCanningHeight)) { - node.version = CMasternode::VERSION0; - } - res = mnview.CreateMasternode(tx.GetHash(), node, obj.timelock); // Build coinage from the point of masternode creation if (res) { diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index a57b06f5a94..72e52cbac99 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -670,7 +670,8 @@ UniValue listmasternodes(const JSONRPCRequest& request) }, start); } else { CMasternodeV2 nodeV2{}; - pcustomcsview->ForEachMasternode([&](uint256 const& nodeId, CMasternode node) { + pcustomcsview->ForEachMasternode([&](uint256 const& nodeId, CMasternodeV1 node) { + nodeV2.resignTx = std::move(node.resignTx); static_cast(nodeV2) = std::move(node); ret.pushKVs(mnToJSON(nodeId, nodeV2, verbose, mnIds, pwallet)); return --limit != 0;