|
1 | 1 | #include "sharechain_storage.hpp" |
2 | 2 | #include <functional> |
3 | | -#include <impl/ltc/share.hpp> |
4 | 3 |
|
5 | 4 | namespace c2pool { |
6 | 5 | namespace storage { |
@@ -35,60 +34,7 @@ bool SharechainStorage::is_available() const { |
35 | 34 | return m_leveldb_store != nullptr; |
36 | 35 | } |
37 | 36 |
|
38 | | -template<typename ShareChainType> |
39 | | -void SharechainStorage::save_sharechain(const ShareChainType& chain) |
40 | | -{ |
41 | | - if (!m_leveldb_store) { |
42 | | - LOG_ERROR << "LevelDB store not available"; |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - try { |
47 | | - // For now, log that we would save (full integration needs sharechain API) |
48 | | - LOG_INFO << "LevelDB sharechain storage is ready for persistent share storage"; |
49 | | - LOG_INFO << " Network: " << m_network_name; |
50 | | - LOG_INFO << " Current stored shares: " << m_leveldb_store->get_share_count(); |
51 | | - LOG_INFO << " Storage path: " << m_leveldb_store->get_base_path() << "/" << m_network_name << "/sharechain_leveldb"; |
52 | | - |
53 | | - } catch (const std::exception& e) { |
54 | | - LOG_ERROR << "Error with LevelDB sharechain storage: " << e.what(); |
55 | | - } |
56 | | -} |
57 | | - |
58 | | -template<typename ShareChainType> |
59 | | -bool SharechainStorage::load_sharechain(ShareChainType& chain) |
60 | | -{ |
61 | | - if (!m_leveldb_store) { |
62 | | - LOG_WARNING << "LevelDB store not available, starting with empty sharechain"; |
63 | | - return false; |
64 | | - } |
65 | | - |
66 | | - try { |
67 | | - uint64_t stored_shares = m_leveldb_store->get_share_count(); |
68 | | - if (stored_shares == 0) { |
69 | | - LOG_INFO << "No shares found in LevelDB storage, starting fresh"; |
70 | | - return false; |
71 | | - } |
72 | | - |
73 | | - uint256 best_hash = m_leveldb_store->get_best_hash(); |
74 | | - uint64_t best_height = m_leveldb_store->get_best_height(); |
75 | | - |
76 | | - LOG_INFO << "LevelDB sharechain storage contains " << stored_shares << " shares"; |
77 | | - LOG_INFO << " Best height: " << best_height; |
78 | | - LOG_INFO << " Best hash: " << best_hash.ToString().substr(0, 16) << "..."; |
79 | | - |
80 | | - // For now, just report availability - full integration needs sharechain API |
81 | | - LOG_INFO << "LevelDB storage is ready for share loading and recovery"; |
82 | | - |
83 | | - return stored_shares > 0; |
84 | | - |
85 | | - } catch (const std::exception& e) { |
86 | | - LOG_ERROR << "Error loading from LevelDB sharechain storage: " << e.what(); |
87 | | - return false; |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -bool SharechainStorage::store_share(const uint256& hash, const std::vector<uint8_t>& serialized_data, |
| 37 | +bool SharechainStorage::store_share(const uint256& hash, const std::vector<uint8_t>& serialized_data, |
92 | 38 | const uint256& prev_hash, uint64_t height, uint64_t timestamp, |
93 | 39 | const uint256& work, const uint256& target, bool is_orphan) |
94 | 40 | { |
@@ -268,57 +214,8 @@ std::vector<uint256> SharechainStorage::get_shares_by_height_range(uint64_t star |
268 | 214 | return m_leveldb_store->get_shares_by_height_range(start_height, end_height); |
269 | 215 | } |
270 | 216 |
|
271 | | -template<typename ShareChainType> |
272 | | -void SharechainStorage::schedule_periodic_save(ShareChainType& chain, boost::asio::io_context& ioc, int interval_seconds) |
273 | | -{ |
274 | | - auto timer = std::make_shared<boost::asio::steady_timer>(ioc); |
275 | | - |
276 | | - // Create a safe capture by copying what we need |
277 | | - auto leveldb_store_ptr = m_leveldb_store.get(); // Raw pointer for safety check |
278 | | - |
279 | | - // Use a shared_ptr to hold the recursive callback |
280 | | - auto save_task = std::make_shared<std::function<void()>>(); |
281 | | - *save_task = [leveldb_store_ptr, timer, interval_seconds, save_task]() { |
282 | | - if (leveldb_store_ptr) { |
283 | | - LOG_INFO << "Periodic LevelDB storage maintenance"; |
284 | | - |
285 | | - try { |
286 | | - uint64_t share_count = leveldb_store_ptr->get_share_count(); |
287 | | - uint64_t best_height = leveldb_store_ptr->get_best_height(); |
288 | | - |
289 | | - LOG_INFO << "LevelDB Storage Stats:"; |
290 | | - LOG_INFO << " Total shares: " << share_count; |
291 | | - LOG_INFO << " Best height: " << best_height; |
292 | | - |
293 | | - // Periodic compaction (every hour) |
294 | | - static int compact_counter = 0; |
295 | | - if (++compact_counter >= (3600 / interval_seconds)) { |
296 | | - LOG_INFO << "Compacting LevelDB sharechain storage..."; |
297 | | - leveldb_store_ptr->compact(); |
298 | | - compact_counter = 0; |
299 | | - } |
300 | | - } catch (const std::exception& e) { |
301 | | - LOG_ERROR << "Error in periodic LevelDB maintenance: " << e.what(); |
302 | | - } |
303 | | - } |
304 | | - |
305 | | - timer->expires_after(std::chrono::seconds(interval_seconds)); |
306 | | - timer->async_wait([save_task](const boost::system::error_code&) { |
307 | | - if (save_task) (*save_task)(); |
308 | | - }); |
309 | | - }; |
310 | | - |
311 | | - // Start after initial delay |
312 | | - timer->expires_after(std::chrono::seconds(30)); |
313 | | - timer->async_wait([save_task](const boost::system::error_code&) { |
314 | | - if (save_task) (*save_task)(); |
315 | | - }); |
316 | | -} |
317 | | - |
318 | | -// Explicit template instantiations for common types |
319 | | -template void SharechainStorage::save_sharechain<ltc::ShareChain>(const ltc::ShareChain& chain); |
320 | | -template bool SharechainStorage::load_sharechain<ltc::ShareChain>(ltc::ShareChain& chain); |
321 | | -template void SharechainStorage::schedule_periodic_save<ltc::ShareChain>(ltc::ShareChain& chain, boost::asio::io_context& ioc, int interval_seconds); |
| 217 | +// Template method bodies (save_sharechain, load_sharechain, schedule_periodic_save) |
| 218 | +// live in the header so any coin's ShareChain type gets implicit instantiation. |
322 | 219 |
|
323 | 220 | } // namespace storage |
324 | 221 | } // namespace c2pool |
0 commit comments