mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
ensure peekMutex works correctly in the new setup
This commit is contained in:
@@ -58,12 +58,15 @@ LedgerHistory::insert(std::shared_ptr<Ledger const> ledger, bool validated)
|
|||||||
|
|
||||||
assert(ledger->stateMap().getHash().isNonZero());
|
assert(ledger->stateMap().getHash().isNonZero());
|
||||||
|
|
||||||
std::unique_lock sl(m_ledgers_by_hash.peekMutex());
|
std::unique_lock sl(m_ledgers_by_hash.peekMutex(ledger->info().hash));
|
||||||
|
|
||||||
const bool alreadyHad = m_ledgers_by_hash.canonicalize_replace_cache(
|
const bool alreadyHad = m_ledgers_by_hash.canonicalize_replace_cache(
|
||||||
ledger->info().hash, ledger);
|
ledger->info().hash, ledger);
|
||||||
if (validated)
|
if (validated)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::shared_mutex> lock(mLedgersByIndexMutex);
|
||||||
mLedgersByIndex[ledger->info().seq] = ledger->info().hash;
|
mLedgersByIndex[ledger->info().seq] = ledger->info().hash;
|
||||||
|
}
|
||||||
|
|
||||||
return alreadyHad;
|
return alreadyHad;
|
||||||
}
|
}
|
||||||
@@ -71,12 +74,12 @@ LedgerHistory::insert(std::shared_ptr<Ledger const> ledger, bool validated)
|
|||||||
LedgerHash
|
LedgerHash
|
||||||
LedgerHistory::getLedgerHash(LedgerIndex index)
|
LedgerHistory::getLedgerHash(LedgerIndex index)
|
||||||
{
|
{
|
||||||
std::unique_lock sl(m_ledgers_by_hash.peekMutex());
|
std::unique_lock<std::shared_mutex> lock(mLedgersByIndexMutex);
|
||||||
auto it = mLedgersByIndex.find(index);
|
auto it = mLedgersByIndex.find(index);
|
||||||
|
|
||||||
if (it != mLedgersByIndex.end())
|
if (it != mLedgersByIndex.end())
|
||||||
return it->second;
|
return it->second;
|
||||||
|
|
||||||
return uint256();
|
return uint256();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,13 +87,13 @@ std::shared_ptr<Ledger const>
|
|||||||
LedgerHistory::getLedgerBySeq(LedgerIndex index)
|
LedgerHistory::getLedgerBySeq(LedgerIndex index)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::unique_lock sl(m_ledgers_by_hash.peekMutex());
|
std::unique_lock<std::shared_mutex> lock(mLedgersByIndexMutex);
|
||||||
|
|
||||||
auto it = mLedgersByIndex.find(index);
|
auto it = mLedgersByIndex.find(index);
|
||||||
|
|
||||||
if (it != mLedgersByIndex.end())
|
if (it != mLedgersByIndex.end())
|
||||||
{
|
{
|
||||||
uint256 hash = it->second;
|
uint256 hash = it->second;
|
||||||
sl.unlock();
|
|
||||||
return getLedgerByHash(hash);
|
return getLedgerByHash(hash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,11 +107,16 @@ LedgerHistory::getLedgerBySeq(LedgerIndex index)
|
|||||||
|
|
||||||
{
|
{
|
||||||
// Add this ledger to the local tracking by index
|
// Add this ledger to the local tracking by index
|
||||||
std::unique_lock sl(m_ledgers_by_hash.peekMutex());
|
{
|
||||||
|
std::unique_lock sl(m_ledgers_by_hash.peekMutex(ret->info().hash));
|
||||||
assert(ret->isImmutable());
|
assert(ret->isImmutable());
|
||||||
m_ledgers_by_hash.canonicalize_replace_client(ret->info().hash, ret);
|
m_ledgers_by_hash.canonicalize_replace_client(ret->info().hash, ret);
|
||||||
mLedgersByIndex[ret->info().seq] = ret->info().hash;
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::unique_lock<std::shared_mutex> lock(mLedgersByIndexMutex);
|
||||||
|
mLedgersByIndex[ret->info().seq] = ret->info().hash;
|
||||||
|
}
|
||||||
return (ret->info().seq == index) ? ret : nullptr;
|
return (ret->info().seq == index) ? ret : nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -440,7 +448,7 @@ LedgerHistory::builtLedger(
|
|||||||
LedgerHash hash = ledger->info().hash;
|
LedgerHash hash = ledger->info().hash;
|
||||||
assert(!hash.isZero());
|
assert(!hash.isZero());
|
||||||
|
|
||||||
std::unique_lock sl(m_consensus_validated.peekMutex());
|
std::unique_lock sl(m_consensus_validated.peekMutex(index));
|
||||||
|
|
||||||
auto entry = std::make_shared<cv_entry>();
|
auto entry = std::make_shared<cv_entry>();
|
||||||
m_consensus_validated.canonicalize_replace_client(index, entry);
|
m_consensus_validated.canonicalize_replace_client(index, entry);
|
||||||
@@ -480,7 +488,7 @@ LedgerHistory::validatedLedger(
|
|||||||
LedgerHash hash = ledger->info().hash;
|
LedgerHash hash = ledger->info().hash;
|
||||||
assert(!hash.isZero());
|
assert(!hash.isZero());
|
||||||
|
|
||||||
std::unique_lock sl(m_consensus_validated.peekMutex());
|
std::unique_lock sl(m_consensus_validated.peekMutex(index));
|
||||||
|
|
||||||
auto entry = std::make_shared<cv_entry>();
|
auto entry = std::make_shared<cv_entry>();
|
||||||
m_consensus_validated.canonicalize_replace_client(index, entry);
|
m_consensus_validated.canonicalize_replace_client(index, entry);
|
||||||
@@ -515,7 +523,7 @@ LedgerHistory::validatedLedger(
|
|||||||
bool
|
bool
|
||||||
LedgerHistory::fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash)
|
LedgerHistory::fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash)
|
||||||
{
|
{
|
||||||
std::unique_lock sl(m_ledgers_by_hash.peekMutex());
|
std::unique_lock<std::shared_mutex> lock(mLedgersByIndexMutex);
|
||||||
auto it = mLedgersByIndex.find(ledgerIndex);
|
auto it = mLedgersByIndex.find(ledgerIndex);
|
||||||
|
|
||||||
if ((it != mLedgersByIndex.end()) && (it->second != ledgerHash))
|
if ((it != mLedgersByIndex.end()) && (it->second != ledgerHash))
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include <ripple/protocol/RippleLedgerHash.h>
|
#include <ripple/protocol/RippleLedgerHash.h>
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <shared_mutex>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
@@ -150,6 +151,7 @@ private:
|
|||||||
|
|
||||||
// Maps ledger indexes to the corresponding hash.
|
// Maps ledger indexes to the corresponding hash.
|
||||||
std::map<LedgerIndex, LedgerHash> mLedgersByIndex; // validated ledgers
|
std::map<LedgerIndex, LedgerHash> mLedgersByIndex; // validated ledgers
|
||||||
|
std::shared_mutex mLedgersByIndexMutex;
|
||||||
|
|
||||||
beast::Journal j_;
|
beast::Journal j_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -981,12 +981,9 @@ public:
|
|||||||
return caches[getCacheIndex(key)]->retrieve(key, data);
|
return caches[getCacheIndex(key)]->retrieve(key, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_type& peekMutex()
|
mutex_type& peekMutex(key_type const& key)
|
||||||
{
|
{
|
||||||
// This is tricky as we have multiple mutexes now.
|
return caches[getCacheIndex(key)]->peekMutex();
|
||||||
// For simplicity, we'll return the mutex of the first cache,
|
|
||||||
// but this might not be the best approach in all scenarios.
|
|
||||||
return caches[0]->peekMutex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<key_type> getKeys() const
|
std::vector<key_type> getKeys() const
|
||||||
|
|||||||
Reference in New Issue
Block a user