fix!: Fix on QuorumDataRequests and refactoring (#4937)

* qdata typo fixes and refactoring

* code style fix

* Add LOCK2 back

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This commit is contained in:
Odysseas Gabrielides 2022-07-26 23:31:03 +03:00 committed by pasta
parent 1fa81c6a04
commit b5ae437ca7
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
3 changed files with 57 additions and 32 deletions

View File

@ -32,10 +32,7 @@ static const std::string DB_QUORUM_QUORUM_VVEC = "q_Qqvvec";
CQuorumManager* quorumManager;
CCriticalSection cs_data_requests;
//key = <ProTx, bool, quorumHash, llmqType>
//TODO: Document purpose of bool
using key_t = std::tuple<uint256, bool, uint256, uint8_t>;
static std::unordered_map<key_t, CQuorumDataRequest, StaticSaltedHasher> mapQuorumDataRequests GUARDED_BY(cs_data_requests);
static std::unordered_map<CQuorumDataRequestKey, CQuorumDataRequest, StaticSaltedHasher> mapQuorumDataRequests GUARDED_BY(cs_data_requests);
static uint256 MakeQuorumKey(const CQuorum& q)
{
@ -436,8 +433,11 @@ bool CQuorumManager::RequestQuorumData(CNode* pFrom, Consensus::LLMQType llmqTyp
}
LOCK(cs_data_requests);
auto quorumHash = pQuorumBaseBlockIndex->GetBlockHash();
auto key = std::make_tuple(pFrom->GetVerifiedProRegTxHash(), true, quorumHash, (uint8_t)llmqType);
CQuorumDataRequestKey key;
key.proRegTx = pFrom->GetVerifiedProRegTxHash();
key.flag = true;
key.quorumHash = pQuorumBaseBlockIndex->GetBlockHash();
key.llmqType = llmqType;
auto it = mapQuorumDataRequests.emplace(key, CQuorumDataRequest(llmqType, pQuorumBaseBlockIndex->GetBlockHash(), nDataMask, proTxHash));
if (!it.second && !it.first->second.IsExpired()) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Already requested\n", __func__);
@ -588,9 +588,11 @@ void CQuorumManager::ProcessMessage(CNode* pFrom, const std::string& msg_type, C
{
LOCK2(cs_main, cs_data_requests);
auto quorumHash = request.GetQuorumHash();
auto llmqType = (uint8_t) request.GetLLMQType();
auto key = std::make_tuple(pFrom->GetVerifiedProRegTxHash(), false, quorumHash, llmqType);
CQuorumDataRequestKey key;
key.proRegTx = pFrom->GetVerifiedProRegTxHash();
key.flag = false;
key.quorumHash = request.GetQuorumHash();
key.llmqType = request.GetLLMQType();
auto it = mapQuorumDataRequests.find(key);
if (it == mapQuorumDataRequests.end()) {
it = mapQuorumDataRequests.emplace(key, request).first;
@ -663,9 +665,11 @@ void CQuorumManager::ProcessMessage(CNode* pFrom, const std::string& msg_type, C
{
LOCK2(cs_main, cs_data_requests);
auto quorumHash = request.GetQuorumHash();
auto llmqType = (uint8_t) request.GetLLMQType();
auto key = std::make_tuple(pFrom->GetVerifiedProRegTxHash(), true, quorumHash, llmqType);
CQuorumDataRequestKey key;
key.proRegTx = pFrom->GetVerifiedProRegTxHash();
key.flag = true;
key.quorumHash = request.GetQuorumHash();
key.llmqType = request.GetLLMQType();
auto it = mapQuorumDataRequests.find(key);
if (it == mapQuorumDataRequests.end()) {
errorHandler("Not requested");
@ -838,9 +842,11 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co
pCurrentMemberHash = &vecMemberHashes[(nMyStartOffset + nTries++) % vecMemberHashes.size()];
{
LOCK(cs_data_requests);
auto quorumHash = pQuorum->qc->quorumHash;
auto llmqType = (uint8_t)pQuorum->qc->quorumIndex;
auto key = std::make_tuple(*pCurrentMemberHash, true, quorumHash, (uint8_t)llmqType);
CQuorumDataRequestKey key;
key.proRegTx = *pCurrentMemberHash;
key.flag = true;
key.quorumHash = pQuorum->qc->quorumHash;
key.llmqType = pQuorum->qc->llmqType;
auto it = mapQuorumDataRequests.find(key);
if (it != mapQuorumDataRequests.end() && !it->second.IsExpired()) {
printLog("Already asked");
@ -866,9 +872,11 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co
printLog("Requested");
} else {
LOCK(cs_data_requests);
auto quorumHash = pQuorum->qc->quorumHash;
auto llmqType = (uint8_t)pQuorum->qc->quorumIndex;
auto key = std::make_tuple(*pCurrentMemberHash, true, quorumHash, (uint8_t)llmqType);
CQuorumDataRequestKey key;
key.proRegTx = *pCurrentMemberHash;
key.flag = true;
key.quorumHash = pQuorum->qc->quorumHash;
key.llmqType = pQuorum->qc->llmqType;
auto it = mapQuorumDataRequests.find(key);
if (it == mapQuorumDataRequests.end()) {
printLog("Failed");

View File

@ -32,6 +32,22 @@ class CDKGSessionManager;
// If true, we will connect to all new quorums and watch their communication
static constexpr bool DEFAULT_WATCH_QUORUMS{false};
/**
* Object used as a key to store CQuorumDataRequest
*/
struct CQuorumDataRequestKey
{
uint256 proRegTx;
//TODO: Investigate purpose of this flag and rename accordingly
bool flag;
uint256 quorumHash;
Consensus::LLMQType llmqType;
bool operator ==(const CQuorumDataRequestKey& obj) const
{
return (proRegTx == obj.proRegTx && flag == obj.flag && quorumHash == obj.quorumHash && llmqType == obj.llmqType);
}
};
/**
* An object of this class represents a QGETDATA request or a QDATA response header
@ -243,6 +259,21 @@ extern CQuorumManager* quorumManager;
} // namespace llmq
template<typename T> struct SaltedHasherImpl;
template<>
struct SaltedHasherImpl<llmq::CQuorumDataRequestKey>
{
static std::size_t CalcHash(const llmq::CQuorumDataRequestKey& v, uint64_t k0, uint64_t k1)
{
CSipHasher c(k0, k1);
c.Write((unsigned char*)&(v.proRegTx), sizeof(v.proRegTx));
c.Write((unsigned char*)&(v.flag), sizeof(v.flag));
c.Write((unsigned char*)&(v.quorumHash), sizeof(v.quorumHash));
c.Write((unsigned char*)&(v.llmqType), sizeof(v.llmqType));
return c.Finalize();
}
};
template<> struct is_serializable_enum<llmq::CQuorumDataRequest::Errors> : std::true_type {};
#endif // BITCOIN_LLMQ_QUORUMS_H

View File

@ -13,20 +13,6 @@
template<typename T> struct SaltedHasherImpl;
template<typename N, typename M, typename K, typename Q>
struct SaltedHasherImpl<std::tuple<N, M, K, Q>>
{
static std::size_t CalcHash(const std::tuple<N, M, K, Q>& v, uint64_t k0, uint64_t k1)
{
CSipHasher c(k0, k1);
c.Write((unsigned char*)&std::get<0>(v), sizeof(M));
c.Write((unsigned char*)&std::get<1>(v), sizeof(N));
c.Write((unsigned char*)&std::get<2>(v), sizeof(K));
c.Write((unsigned char*)&std::get<3>(v), sizeof(Q));
return c.Finalize();
}
};
template<typename N>
struct SaltedHasherImpl<std::pair<uint256, N>>
{