@@ -62,47 +62,26 @@ class CDeterministicMN
6262 // only non-initial values
6363 assert (_internalId != std::numeric_limits<uint64_t >::max ());
6464 }
65-
6665 template <typename Stream>
67- CDeterministicMN (deserialize_type, Stream& s, const uint8_t format_version)
68- {
69- SerializationOp (s, CSerActionUnserialize (), format_version);
70- }
66+ CDeterministicMN (deserialize_type, Stream& s) { s >> *this ; }
7167
72- template <typename Stream, typename Operation>
73- inline void SerializationOp (Stream& s, Operation ser_action, const uint8_t format_version)
68+ SERIALIZE_METHODS (CDeterministicMN, obj)
7469 {
75- // We no longer support EvoDB formats below MN_VERSION_FORMAT
76- if (format_version < MN_VERSION_FORMAT) {
77- throw std::ios_base::failure (" EvoDb too old, run Dash Core with -reindex to rebuild" );
78- }
79- READWRITE (proTxHash);
80- READWRITE (VARINT (internalId));
81- READWRITE (collateralOutpoint);
82- READWRITE (nOperatorReward);
83- READWRITE (pdmnState);
70+ READWRITE (obj.proTxHash );
71+ READWRITE (VARINT (obj.internalId ));
72+ READWRITE (obj.collateralOutpoint );
73+ READWRITE (obj.nOperatorReward );
74+ READWRITE (obj.pdmnState );
8475 // We can't know if we are serialising for the Disk or for the Network here (s.GetType() is not accessible)
8576 // Therefore if s.GetVersion() == CLIENT_VERSION -> Then we know we are serialising for the Disk
8677 // Otherwise, we can safely check with protocol versioning logic so we won't break old clients
8778 if (s.GetVersion () == CLIENT_VERSION || s.GetVersion () >= DMN_TYPE_PROTO_VERSION) {
88- READWRITE (nType);
79+ READWRITE (obj. nType );
8980 } else {
90- nType = MnType::Regular;
81+ SER_READ (obj, obj. nType = MnType::Regular) ;
9182 }
9283 }
9384
94- template <typename Stream>
95- void Serialize (Stream& s) const
96- {
97- const_cast <CDeterministicMN*>(this )->SerializationOp (s, CSerActionSerialize (), MN_CURRENT_FORMAT);
98- }
99-
100- template <typename Stream>
101- void Unserialize (Stream& s, const uint8_t format_version = MN_CURRENT_FORMAT)
102- {
103- SerializationOp (s, CSerActionUnserialize (), format_version);
104- }
105-
10685 [[nodiscard]] uint64_t GetInternalId () const ;
10786
10887 [[nodiscard]] std::string ToString () const ;
@@ -193,27 +172,36 @@ class CDeterministicMNList
193172 void Serialize (Stream& s) const
194173 {
195174 const_cast <CDeterministicMNList*>(this )->SerializationOpBase (s, CSerActionSerialize ());
175+
196176 // Serialize the map as a vector
197177 WriteCompactSize (s, mnMap.size ());
198- for (const auto & p : mnMap) {
199- s << *p. second ;
178+ for (const auto & [_, dmn] : mnMap) {
179+ s << *dmn ;
200180 }
201181 }
202182
203183 template <typename Stream>
204- void Unserialize (Stream& s, const uint8_t format_version = CDeterministicMN::MN_CURRENT_FORMAT) {
205- mnMap = MnMap ();
206- mnUniquePropertyMap = MnUniquePropertyMap ();
207- mnInternalIdMap = MnInternalIdMap ();
184+ void Unserialize (Stream& s)
185+ {
186+ Clear ();
208187
209188 SerializationOpBase (s, CSerActionUnserialize ());
210189
211- size_t cnt = ReadCompactSize (s);
212- for (size_t i = 0 ; i < cnt; i++) {
213- AddMN (std::make_shared<CDeterministicMN>(deserialize, s, format_version), false );
190+ for (size_t to_read = ReadCompactSize (s); to_read > 0 ; --to_read) {
191+ AddMN (std::make_shared<CDeterministicMN>(deserialize, s), /* fBumpTotalCount=*/ false );
214192 }
215193 }
216194
195+ void Clear ()
196+ {
197+ blockHash = uint256{};
198+ nHeight = -1 ;
199+ nTotalRegisteredCount = 0 ;
200+ mnMap = MnMap ();
201+ mnUniquePropertyMap = MnUniquePropertyMap ();
202+ mnInternalIdMap = MnInternalIdMap ();
203+ }
204+
217205 [[nodiscard]] size_t GetAllMNsCount () const
218206 {
219207 return mnMap.size ();
@@ -483,46 +471,39 @@ class CDeterministicMNListDiff
483471 void Serialize (Stream& s) const
484472 {
485473 s << addedMNs;
474+
486475 WriteCompactSize (s, updatedMNs.size ());
487- for (const auto & p : updatedMNs) {
488- WriteVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s, p. first );
489- s << p. second ;
476+ for (const auto & [internalId, pdmnState] : updatedMNs) {
477+ WriteVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s, internalId );
478+ s << pdmnState ;
490479 }
480+
491481 WriteCompactSize (s, removedMns.size ());
492- for (const auto & p : removedMns) {
493- WriteVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s, p );
482+ for (const auto & internalId : removedMns) {
483+ WriteVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s, internalId );
494484 }
495485 }
496486
497487 template <typename Stream>
498- void Unserialize (Stream& s, const uint8_t format_version = CDeterministicMN::MN_CURRENT_FORMAT )
488+ void Unserialize (Stream& s)
499489 {
500490 updatedMNs.clear ();
501491 removedMns.clear ();
502492
503- size_t tmp;
504- uint64_t tmp2;
505- tmp = ReadCompactSize (s);
506- for (size_t i = 0 ; i < tmp; i++) {
507- CDeterministicMN mn (0 );
508- mn.Unserialize (s, format_version);
509- auto dmn = std::make_shared<CDeterministicMN>(mn);
510- addedMNs.push_back (dmn);
493+ for (size_t to_read = ReadCompactSize (s); to_read > 0 ; --to_read) {
494+ addedMNs.push_back (std::make_shared<CDeterministicMN>(deserialize, s));
511495 }
512- tmp = ReadCompactSize (s);
513- for (size_t i = 0 ; i < tmp; i++) {
514- CDeterministicMNStateDiff diff;
515- // CDeterministicMNState hold new fields {nConsecutivePayments, platformNodeID, platformP2PPort, platformHTTPPort} but no migration is needed here since:
516- // CDeterministicMNStateDiff is always serialised using a bitmask.
517- // Because the new field have a new bit guide value then we are good to continue
518- tmp2 = ReadVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s);
519- s >> diff;
520- updatedMNs.emplace (tmp2, std::move (diff));
496+
497+ for (size_t to_read = ReadCompactSize (s); to_read > 0 ; --to_read) {
498+ uint64_t internalId = ReadVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s);
499+ // CDeterministicMNState can have newer fields but doesn't need migration logic here as CDeterministicMNStateDiff
500+ // is always serialised using a bitmask and new fields have a new bit guide value, so we are good to continue.
501+ updatedMNs.emplace (internalId, CDeterministicMNStateDiff (deserialize, s));
521502 }
522- tmp = ReadCompactSize (s);
523- for (size_t i = 0 ; i < tmp; i++ ) {
524- tmp2 = ReadVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s);
525- removedMns.emplace (tmp2 );
503+
504+ for (size_t to_read = ReadCompactSize (s); to_read > 0 ; --to_read ) {
505+ uint64_t internalId = ReadVarInt<Stream, VarIntMode::DEFAULT, uint64_t >(s);
506+ removedMns.emplace (internalId );
526507 }
527508 }
528509
0 commit comments