Skip to content

Commit d0a959e

Browse files
committed
[Refactoring] Introduce CFinalizedBudget/CBudgetProposal::GetBroadcast
Returning the serialized object for the network message. Also, introduce class constructors from CDataStream.
1 parent f092c28 commit d0a959e

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/masternode-budget.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,25 @@ CBudgetProposal::CBudgetProposal(const CBudgetProposal& other)
13341334
strInvalid = "";
13351335
}
13361336

1337+
// initialize from network broadcast message
1338+
bool CBudgetProposal::ParseBroadcast(CDataStream& broadcast)
1339+
{
1340+
*this = CBudgetProposal();
1341+
try {
1342+
broadcast >> LIMITED_STRING(strProposalName, 20);
1343+
broadcast >> LIMITED_STRING(strURL, 64);
1344+
broadcast >> nTime;
1345+
broadcast >> nBlockStart;
1346+
broadcast >> nBlockEnd;
1347+
broadcast >> nAmount;
1348+
broadcast >> *(CScriptBase*)(&address);
1349+
broadcast >> nFeeTXHash;
1350+
} catch (std::exception& e) {
1351+
return error("Unable to deserialize proposal broadcast: %s", e.what());
1352+
}
1353+
return true;
1354+
}
1355+
13371356
void CBudgetProposal::SyncVotes(CNode* pfrom, bool fPartial, int& nInvCount) const
13381357
{
13391358
for (const auto& it: mapVotes) {
@@ -1590,6 +1609,22 @@ int CBudgetProposal::GetRemainingPaymentCount(int nCurrentHeight) const
15901609
return std::min(nPayments, GetTotalPaymentCount());
15911610
}
15921611

1612+
// return broadcast serialization
1613+
CDataStream CBudgetProposal::GetBroadcast() const
1614+
{
1615+
CDataStream broadcast(SER_NETWORK, PROTOCOL_VERSION);
1616+
broadcast.reserve(1000);
1617+
broadcast << LIMITED_STRING(strProposalName, 20);
1618+
broadcast << LIMITED_STRING(strURL, 64);
1619+
broadcast << nTime;
1620+
broadcast << nBlockStart;
1621+
broadcast << nBlockEnd;
1622+
broadcast << nAmount;
1623+
broadcast << *(CScriptBase*)(&address);
1624+
broadcast << nFeeTXHash;
1625+
return broadcast;
1626+
}
1627+
15931628
inline bool CBudgetProposal::PtrHigherYes(CBudgetProposal* a, CBudgetProposal* b)
15941629
{
15951630
const int netYes_a = a->GetYeas() - a->GetNays();
@@ -1710,6 +1745,20 @@ CFinalizedBudget::CFinalizedBudget(const CFinalizedBudget& other) :
17101745
nTime(other.nTime)
17111746
{ }
17121747

1748+
bool CFinalizedBudget::ParseBroadcast(CDataStream& broadcast)
1749+
{
1750+
*this = CFinalizedBudget();
1751+
try {
1752+
broadcast >> LIMITED_STRING(strBudgetName, 20);
1753+
broadcast >> nBlockStart;
1754+
broadcast >> vecBudgetPayments;
1755+
broadcast >> nFeeTXHash;
1756+
} catch (std::exception& e) {
1757+
return error("Unable to deserialize finalized budget broadcast: %s", e.what());
1758+
}
1759+
return true;
1760+
}
1761+
17131762
bool CFinalizedBudget::AddOrUpdateVote(const CFinalizedBudgetVote& vote, std::string& strError)
17141763
{
17151764
const uint256& hash = vote.GetVin().prevout.GetHash();
@@ -2132,6 +2181,19 @@ void CFinalizedBudget::SubmitVote()
21322181
}
21332182
}
21342183

2184+
// return broadcast serialization
2185+
CDataStream CFinalizedBudget::GetBroadcast() const
2186+
{
2187+
CDataStream broadcast(SER_NETWORK, PROTOCOL_VERSION);
2188+
broadcast.reserve(1000);
2189+
broadcast << LIMITED_STRING(strBudgetName, 20);
2190+
broadcast << nBlockStart;
2191+
broadcast << vecBudgetPayments;
2192+
broadcast << nFeeTXHash;
2193+
return broadcast;
2194+
}
2195+
2196+
21352197
bool CFinalizedBudget::operator>(const CFinalizedBudget& other) const
21362198
{
21372199
const int count = GetVoteCount();

src/masternode-budget.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ class CFinalizedBudget
471471
return ss.GetHash();
472472
}
473473

474+
// Serialization for local DB
474475
ADD_SERIALIZE_METHODS;
475476
template <typename Stream, typename Operation>
476477
inline void SerializationOp(Stream& s, Operation ser_action)
@@ -485,6 +486,10 @@ class CFinalizedBudget
485486
READWRITE(strProposals);
486487
}
487488

489+
// Serialization for network messages.
490+
bool ParseBroadcast(CDataStream& broadcast);
491+
CDataStream GetBroadcast() const;
492+
488493
// compare finalized budget by votes (sort tie with feeHash)
489494
bool operator>(const CFinalizedBudget& other) const;
490495
// compare finalized budget pointers
@@ -623,24 +628,26 @@ class CBudgetProposal
623628
return ss.GetHash();
624629
}
625630

631+
// Serialization for local DB
626632
ADD_SERIALIZE_METHODS;
627633
template <typename Stream, typename Operation>
628634
inline void SerializationOp(Stream& s, Operation ser_action)
629635
{
630-
//for syncing with other clients
631636
READWRITE(LIMITED_STRING(strProposalName, 20));
632637
READWRITE(LIMITED_STRING(strURL, 64));
633638
READWRITE(nBlockStart);
634639
READWRITE(nBlockEnd);
635640
READWRITE(nAmount);
636641
READWRITE(*(CScriptBase*)(&address));
637-
READWRITE(nTime);
638642
READWRITE(nFeeTXHash);
639-
640-
//for saving to the serialized db
643+
READWRITE(nTime);
641644
READWRITE(mapVotes);
642645
}
643646

647+
// Serialization for network messages.
648+
bool ParseBroadcast(CDataStream& broadcast);
649+
CDataStream GetBroadcast() const;
650+
644651
// compare proposals by proposal hash
645652
inline bool operator>(const CBudgetProposal& other) const { return GetHash() > other.GetHash(); }
646653
// compare proposals pointers by hash

0 commit comments

Comments
 (0)