Skip to content

Commit

Permalink
Introduce intermediate structure as helper one
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Oct 28, 2021
1 parent 05980f7 commit 56f4266
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ class ChainImpl : public Chain
return pcustomcsview->CanSpend(nodeId, height);
}

boost::optional<CMasternode> 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<CToken> existTokenGuessId(const std::string & str, DCT_ID & id) const override
{
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class Chain
//! populates the values.
virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;

virtual bool mnExists(const uint256 & nodeId) const = 0;
virtual bool mnCanSpend(const uint256 & nodeId, int height) const = 0;
virtual boost::optional<CMasternode> mnExists(const uint256 & nodeId) const = 0;
virtual std::unique_ptr<CToken> existTokenGuessId(const std::string & str, DCT_ID & id) const = 0;

//! Estimate fraction of total transactions verified if blocks up to
Expand Down
62 changes: 43 additions & 19 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ CMasternode::CMasternode()
, operatorType(0)
, creationHeight(0)
, resignHeight(-1)
, version(-1)
, resignTx()
{
}

Expand Down Expand Up @@ -158,22 +156,33 @@ 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)
{
return !(a == b);
}

bool operator==(CMasternodeV2 const & a, CMasternodeV2 const & b)
{
return static_cast<CMasternode const &>(a) == static_cast<CMasternode const &>(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<CMasternode> CMasternodesView::GetMasternode(const uint256 & id) const
boost::optional<CMasternodeV1> CMasternodesView::GetMasternode(const uint256 & id) const
{
return ReadBy<ID, CMasternode>(id);
return ReadBy<ID, CMasternodeV1>(id);
}

boost::optional<CMasternodeV2> CMasternodesView::GetMasternodeV2(const uint256 & id, int height) const
Expand All @@ -183,6 +192,7 @@ boost::optional<CMasternodeV2> CMasternodesView::GetMasternodeV2(const uint256 &
}
if (auto node = GetMasternode(id)) {
CMasternodeV2 nodeV2{};
nodeV2.resignTx = std::move(node->resignTx);
static_cast<CMasternode&>(nodeV2) = std::move(*node);
return nodeV2;
}
Expand All @@ -199,9 +209,9 @@ boost::optional<uint256> CMasternodesView::GetMasternodeIdByOwner(const CKeyID &
return ReadBy<Owner, uint256>(id);
}

void CMasternodesView::ForEachMasternode(std::function<bool (const uint256 &, CLazySerialize<CMasternode>)> callback, uint256 const & start)
void CMasternodesView::ForEachMasternode(std::function<bool (const uint256 &, CLazySerialize<CMasternodeV1>)> callback, uint256 const & start)
{
ForEach<ID, uint256, CMasternode>(callback, start);
ForEach<ID, uint256, CMasternodeV1>(callback, start);
}

void CMasternodesView::ForEachMasternodeV2(std::function<bool (const uint256 &, CLazySerialize<CMasternodeV2>)> callback, uint256 const & start)
Expand All @@ -211,32 +221,45 @@ void CMasternodesView::ForEachMasternodeV2(std::function<bool (const uint256 &,

void CMasternodesView::MigrateMasternodesToV2()
{
std::map<uint256, CMasternode> nodes;
ForEachMasternode([&](const uint256& nodeId, CMasternode node) {
std::map<uint256, CMasternodeV1> nodes;
ForEachMasternode([&](const uint256& nodeId, CMasternodeV1 node) {
nodes.emplace(nodeId, std::move(node));
return true;
});
CMasternodeV2 nodeV2{};
for (const auto& node : nodes) {
static_cast<CMasternode&>(nodeV2) = node.second;
for (auto& node : nodes) {
nodeV2.resignTx = std::move(node.second.resignTx);
static_cast<CMasternode&>(nodeV2) = std::move(node.second);
WriteBy<ID>(node.first, nodeV2);
}
}

void CMasternodesView::StoreMasternode(const uint256& nodeId, const CMasternodeV2& node, int height)
{
if (height >= Params().GetConsensus().FortCanningHeight) {
WriteBy<ID>(nodeId, node);
} else {
CMasternodeV1 nodeV1{};
nodeV1.resignTx = node.resignTx;
static_cast<CMasternode&>(nodeV1) = node;
WriteBy<ID>(nodeId, nodeV1);
}
}

void CMasternodesView::IncrementMintedBy(const uint256& nodeId, int height)
{
auto node = GetMasternodeV2(nodeId, height);
assert(node);
++node->mintedBlocks;
WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);
}

void CMasternodesView::DecrementMintedBy(const uint256& nodeId, int height)
{
auto node = GetMasternodeV2(nodeId, height);
assert(node);
--node->mintedBlocks;
WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);
}

boost::optional<std::pair<CKeyID, uint256> > CMasternodesView::AmIOperator() const
Expand Down Expand Up @@ -297,6 +320,7 @@ Res CMasternodesView::CreateMasternode(const uint256 & nodeId, const CMasternode
return Res::Err("bad owner and|or operator address (should be P2PKH or P2WPKH only) or node with those addresses exists");
}

// we can safety write base node
WriteBy<ID>(nodeId, node);
WriteBy<Owner>(node.ownerAuthAddress, nodeId);
WriteBy<Operator>(node.operatorAuthAddress, nodeId);
Expand Down Expand Up @@ -333,7 +357,7 @@ Res CMasternodesView::ResignMasternode(const uint256 & nodeId, const uint256 & t
node->resignTx = txid;
node->resignHeight = height;

WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);

return Res::Ok();
}
Expand All @@ -352,7 +376,7 @@ Res CMasternodesView::SetForcedRewardAddress(uint256 const & nodeId, const char
// Set new reward address
node->rewardAddressType = rewardAddressType;
node->rewardAddress = rewardAddress;
WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);

return Res::Ok();
}
Expand All @@ -370,7 +394,7 @@ Res CMasternodesView::RemForcedRewardAddress(uint256 const & nodeId, int height)

node->rewardAddressType = 0;
node->rewardAddress.SetNull();
WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);

return Res::Ok();
}
Expand Down Expand Up @@ -398,7 +422,7 @@ Res CMasternodesView::UpdateMasternode(uint256 const & nodeId, char operatorType
node->operatorType = operatorType;
node->operatorAuthAddress = operatorAuthAddress;

WriteBy<ID>(nodeId, *node);
StoreMasternode(nodeId, *node, height);
WriteBy<Operator>(node->operatorAuthAddress, nodeId);

return Res::Ok();
Expand Down
44 changes: 29 additions & 15 deletions src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ class CMasternode
TENYEAR = 520
};

enum Version : int32_t {
PRE_FORT_CANNING = -1,
VERSION0 = 0,
};

//! Minted blocks counter
uint32_t mintedBlocks;

Expand All @@ -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();
Expand All @@ -111,18 +101,36 @@ class CMasternode

READWRITE(creationHeight);
READWRITE(resignHeight);
READWRITE(version);

READWRITE(resignTx);
}

//! equality test
friend bool operator==(CMasternode const & a, CMasternode const & b);
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 <typename Stream, typename Operation>
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;
Expand All @@ -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
Expand Down Expand Up @@ -185,16 +198,17 @@ struct SubNodeBlockTimeKey
class CMasternodesView : public virtual CStorageView
{
std::map<CKeyID, std::pair<uint32_t, int64_t>> minterTimeCache;
void StoreMasternode(const uint256& nodeId, const CMasternodeV2& node, int height);

public:
// CMasternodesView() = default;

void MigrateMasternodesToV2();
boost::optional<CMasternode> GetMasternode(uint256 const & id) const;
boost::optional<CMasternodeV1> GetMasternode(uint256 const & id) const;
boost::optional<uint256> GetMasternodeIdByOperator(CKeyID const & id) const;
boost::optional<uint256> GetMasternodeIdByOwner(CKeyID const & id) const;
boost::optional<CMasternodeV2> GetMasternodeV2(uint256 const & id, int height) const;
void ForEachMasternode(std::function<bool(uint256 const &, CLazySerialize<CMasternode>)> callback, uint256 const & start = uint256());
void ForEachMasternode(std::function<bool(uint256 const &, CLazySerialize<CMasternodeV1>)> callback, uint256 const & start = uint256());
void ForEachMasternodeV2(std::function<bool(uint256 const &, CLazySerialize<CMasternodeV2>)> callback, uint256 const & start = uint256());

void IncrementMintedBy(const uint256& nodeId, int height);
Expand Down
5 changes: 0 additions & 5 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,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<uint32_t>(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) {
Expand Down
3 changes: 2 additions & 1 deletion src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CMasternode&>(nodeV2) = std::move(node);
ret.pushKVs(mnToJSON(nodeId, nodeV2, verbose, mnIds, pwallet));
return --limit != 0;
Expand Down

0 comments on commit 56f4266

Please sign in to comment.