|
| 1 | +// Copyright (c) 2025 The Dogecoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "rpc/auxcache.h" |
| 6 | + |
| 7 | +#include "script/standard.h" // for CScriptID |
| 8 | +#include "primitives/block.h" // for CBlock |
| 9 | +#include "uint256.h" // for uint256 |
| 10 | +#include "utilmemory.h" // for MakeUnique |
| 11 | +#include "utiltime.h" // for GetTimeMicros |
| 12 | + |
| 13 | +#include <boost/multi_index_container.hpp> |
| 14 | +#include <boost/multi_index/ordered_index.hpp> |
| 15 | + |
| 16 | +#include <cstdint> // for uint64_t |
| 17 | +#include <memory> // for std::unique_ptr, std::shared_ptr |
| 18 | +#include <mutex> // for std::mutex |
| 19 | +#include <tuple> // for std::tuple |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +//! Type that is stored in the cache |
| 24 | +struct AuxCacheItem { |
| 25 | + int64_t cache_time; |
| 26 | + CScriptID scriptId; |
| 27 | + uint256 hash; |
| 28 | + std::shared_ptr<CBlock> pblock; |
| 29 | +}; |
| 30 | + |
| 31 | +// The ByScriptId index sorts by CScriptID and item age |
| 32 | +struct ByScriptId {}; |
| 33 | +using ByScriptIdView = std::tuple<const CScriptID&, const int64_t>; |
| 34 | +struct ByScriptIdExtractor |
| 35 | +{ |
| 36 | + using result_type = ByScriptIdView; |
| 37 | + result_type operator()(const AuxCacheItem& item) const |
| 38 | + { |
| 39 | + // calculate age |
| 40 | + const int64_t age = GetMockableTimeMicros() - item.cache_time; |
| 41 | + return ByScriptIdView{item.scriptId, age}; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// The ByBlockHash index sorts by blockhash (uint256) |
| 46 | +struct ByBlockHash {}; |
| 47 | +struct ByBlockHashExtractor |
| 48 | +{ |
| 49 | + using result_type = uint256; |
| 50 | + result_type operator()(const AuxCacheItem& item) const |
| 51 | + { |
| 52 | + return item.hash; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +/** Data type for the main data structure (AuxCacheItem objects with ByScriptID/ByBlockHash indexes). */ |
| 57 | +using AuxCacheIndex = boost::multi_index_container< |
| 58 | + AuxCacheItem, |
| 59 | + boost::multi_index::indexed_by< |
| 60 | + boost::multi_index::ordered_non_unique<boost::multi_index::tag<ByScriptId>, ByScriptIdExtractor>, |
| 61 | + boost::multi_index::ordered_unique<boost::multi_index::tag<ByBlockHash>, ByBlockHashExtractor> |
| 62 | + > |
| 63 | +>; |
| 64 | + |
| 65 | +} //anon namespace |
| 66 | + |
| 67 | +class CAuxBlockCache::Impl { |
| 68 | + AuxCacheIndex m_index; |
| 69 | + |
| 70 | +private: |
| 71 | + std::mutex mut; |
| 72 | + |
| 73 | +public: |
| 74 | + Impl() : m_index(boost::make_tuple( |
| 75 | + boost::make_tuple(ByScriptIdExtractor(), std::less<ByScriptIdView>()), |
| 76 | + boost::make_tuple(ByBlockHashExtractor(), std::less<uint256>()) |
| 77 | + )) {}; |
| 78 | + |
| 79 | + Impl(const Impl&) = delete; |
| 80 | + Impl& operator=(const Impl&) = delete; |
| 81 | + |
| 82 | + bool Add(const CScriptID scriptId, std::shared_ptr<CBlock> pblock) { |
| 83 | + |
| 84 | + uint256 hash = pblock->GetHash(); |
| 85 | + int64_t micros = GetMockableTimeMicros(); |
| 86 | + |
| 87 | + std::lock_guard<std::mutex> guard(mut); |
| 88 | + auto ret = m_index.get<ByBlockHash>().emplace(AuxCacheItem{micros, scriptId, hash, pblock}); |
| 89 | + |
| 90 | + return ret.second; |
| 91 | + } |
| 92 | + |
| 93 | + void Reset() { |
| 94 | + m_index.clear(); |
| 95 | + } |
| 96 | + |
| 97 | + bool Get(const CScriptID scriptId, std::shared_ptr<CBlock>& pblock) const { |
| 98 | + auto it = m_index.get<ByScriptId>().lower_bound(ByScriptIdView{scriptId, 0LL}); |
| 99 | + if (it != m_index.get<ByScriptId>().end() && it->scriptId == scriptId) { |
| 100 | + pblock = it->pblock; |
| 101 | + return true; |
| 102 | + } |
| 103 | + pblock.reset(); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + bool Get(const uint256 blockhash, std::shared_ptr<CBlock>& pblock) const { |
| 108 | + auto it = m_index.get<ByBlockHash>().lower_bound(blockhash); |
| 109 | + if (it != m_index.get<ByBlockHash>().end() && it->hash == blockhash) { |
| 110 | + pblock = it->pblock; |
| 111 | + return true; |
| 112 | + } |
| 113 | + pblock.reset(); |
| 114 | + return false; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +CAuxBlockCache::CAuxBlockCache() : m_impl(MakeUnique<CAuxBlockCache::Impl>()) {}; |
| 119 | +CAuxBlockCache::~CAuxBlockCache() = default; |
| 120 | + |
| 121 | +bool CAuxBlockCache::Add(const CScriptID scriptId, std::shared_ptr<CBlock> pblock) { |
| 122 | + return m_impl->Add(scriptId, pblock); |
| 123 | +} |
| 124 | + |
| 125 | +void CAuxBlockCache::Reset() { |
| 126 | + m_impl->Reset(); |
| 127 | +} |
| 128 | + |
| 129 | +bool CAuxBlockCache::Get(const CScriptID scriptId, std::shared_ptr<CBlock>& pblock) { |
| 130 | + return m_impl->Get(scriptId, pblock); |
| 131 | +} |
| 132 | + |
| 133 | +bool CAuxBlockCache::Get(const uint256 blockhash, std::shared_ptr<CBlock>& pblock) { |
| 134 | + return m_impl->Get(blockhash, pblock); |
| 135 | +} |
0 commit comments