Skip to content

Commit 8e5cc93

Browse files
knstPastaPastaPasta
andcommitted
fix: add std::atomic to m_cached_sml to avoid concurency updates of shared_ptr value
Co-Authored-By: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>
1 parent 2b9e048 commit 8e5cc93

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/evo/deterministicmns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ std::vector<CDeterministicMNCPtr> CDeterministicMNList::GetProjectedMNPayees(gsl
266266

267267
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> CDeterministicMNList::to_sml() const
268268
{
269-
if (!m_cached_sml) {
269+
if (!m_cached_sml.load()) {
270270
std::vector<std::unique_ptr<CSimplifiedMNListEntry>> sml_entries;
271271
sml_entries.reserve(mnMap.size());
272272

@@ -535,7 +535,7 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
535535

536536
dmn->pdmnState = pdmnState;
537537
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
538-
if (m_cached_sml && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
538+
if (m_cached_sml.load() && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
539539
m_cached_sml = nullptr;
540540
}
541541
}

src/evo/deterministicmns.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class CDeterministicMNList
179179
// for multiple CDeterministicMNList until mnMap is actually changed.
180180
// Calls of AddMN, RemoveMN and (in some cases) UpdateMN reset this cache;
181181
// it happens also for indirect calls such as ApplyDiff
182-
mutable std::shared_ptr<const CSimplifiedMNList> m_cached_sml;
182+
mutable std::atomic<std::shared_ptr<const CSimplifiedMNList>> m_cached_sml;
183183

184184
public:
185185
CDeterministicMNList() = default;
@@ -191,6 +191,31 @@ class CDeterministicMNList
191191
assert(nHeight >= 0);
192192
}
193193

194+
CDeterministicMNList(const CDeterministicMNList& other) :
195+
blockHash(other.blockHash),
196+
nHeight(other.nHeight),
197+
nTotalRegisteredCount(other.nTotalRegisteredCount),
198+
mnMap(other.mnMap),
199+
mnInternalIdMap(other.mnInternalIdMap),
200+
mnUniquePropertyMap(other.mnUniquePropertyMap),
201+
m_cached_sml{other.m_cached_sml.load()}
202+
{
203+
}
204+
205+
CDeterministicMNList& operator=(const CDeterministicMNList& other)
206+
{
207+
if (this != &other) {
208+
blockHash = other.blockHash;
209+
nHeight = other.nHeight;
210+
nTotalRegisteredCount = other.nTotalRegisteredCount;
211+
mnMap = other.mnMap;
212+
mnInternalIdMap = other.mnInternalIdMap;
213+
mnUniquePropertyMap = other.mnUniquePropertyMap;
214+
m_cached_sml = other.m_cached_sml.load();
215+
}
216+
return *this;
217+
}
218+
194219
template <typename Stream, typename Operation>
195220
inline void SerializationOpBase(Stream& s, Operation ser_action)
196221
{

0 commit comments

Comments
 (0)