From 4fbc76a85bfe04305a810f16a111f41b4bcc2262 Mon Sep 17 00:00:00 2001 From: Aiwe Date: Mon, 3 Jan 2022 09:36:42 +0200 Subject: [PATCH] Add blobs caching for faster mining and validation, sync speed is restored even got small boost comparing to CN --- src/CryptoNoteCore/Blockchain.cpp | 42 ++++++++++++++++++++++--------- src/CryptoNoteCore/Blockchain.h | 10 ++++++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/CryptoNoteCore/Blockchain.cpp b/src/CryptoNoteCore/Blockchain.cpp index bd2d25309b..468cfadbf3 100644 --- a/src/CryptoNoteCore/Blockchain.cpp +++ b/src/CryptoNoteCore/Blockchain.cpp @@ -1,6 +1,6 @@ // Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers, The Monero developers // Copyright (c) 2018, Ryo Currency Project -// Copyright (c) 2016-2021, The Karbo developers +// Copyright (c) 2016-2022, The Karbo developers // // This file is part of Karbo. // @@ -64,7 +64,7 @@ bool operator<(const Crypto::KeyImage& keyImage1, const Crypto::KeyImage& keyIma } } -#define CURRENT_BLOCKCACHE_STORAGE_ARCHIVE_VER 3 +#define CURRENT_BLOCKCACHE_STORAGE_ARCHIVE_VER 4 #define CURRENT_BLOCKCHAININDICES_STORAGE_ARCHIVE_VER 1 namespace CryptoNote { @@ -227,6 +227,9 @@ class BlockCacheSerializer { logger(INFO) << operation << "multi-signature outputs..."; s(m_bs.m_multisignatureOutputs, "multisig_outputs"); + logger(INFO) << operation << "hashing blobs..."; + s(m_bs.m_blobs, "hashing_blobs"); + auto dur = std::chrono::steady_clock::now() - start; logger(INFO) << "Serialization time: " << std::chrono::duration_cast(dur).count() << "ms"; @@ -572,6 +575,7 @@ void Blockchain::rebuildCache() { m_spent_key_images.clear(); m_outputs.clear(); m_multisignatureOutputs.clear(); + m_blobs.clear(); for (uint32_t b = 0; b < m_blocks.size(); ++b) { if (b % 1000 == 0) { logger(INFO, BRIGHT_WHITE) << "Height " << b << " of " << m_blocks.size(); @@ -606,6 +610,15 @@ void Blockchain::rebuildCache() { } } } + + // process blobs for fast mining + BinaryArray ba; + const Block& blk = m_blocks[b].bl; + if (!get_block_hashing_blob(blk, ba)) { + logger(ERROR, BRIGHT_RED) << "Failed to get_block_hashing_blob of block at height " << b; + } + m_blobs.push_back(ba); + } std::chrono::duration duration = std::chrono::steady_clock::now() - timePoint; @@ -1202,6 +1215,12 @@ bool Blockchain::checkProofOfWork(Crypto::cn_context& context, const Block& bloc return true; } +bool Blockchain::getHashingBlob(const uint32_t height, BinaryArray& blob) { + blob = m_blobs[height]; + + return true; +} + bool Blockchain::get_block_long_hash(Crypto::cn_context &context, const Block& b, Crypto::Hash& res) { if (b.majorVersion < CryptoNote::BLOCK_MAJOR_VERSION_5) { return get_block_longhash(context, b, res); @@ -1235,16 +1254,7 @@ bool Blockchain::get_block_long_hash(Crypto::cn_context &context, const Block& b (chunk[3]); uint32_t height_j = n % maxHeight; - std::lock_guard lk(m_blockchain_lock); - const Block& bj = m_blocks[height_j].bl; - - BinaryArray ba; - if (!get_block_hashing_blob(bj, ba)) { - logger(ERROR, BRIGHT_RED) << "Failed to get_block_hashing_blob of additional block " - << j << " at height " << height_j; - return false; - } - + BinaryArray &ba = m_blobs[height_j]; pot.insert(std::end(pot), std::begin(ba), std::end(ba)); } } @@ -2363,6 +2373,13 @@ bool Blockchain::pushBlock(BlockEntry& block, const Crypto::Hash& blockHash) { m_timestampIndex.add(block.bl.timestamp, blockHash); m_generatedTransactionsIndex.add(block.bl); + BinaryArray ba; + const Block& blk = block.bl; + if (!get_block_hashing_blob(blk, ba)) { + logger(ERROR, BRIGHT_RED) << "Failed to get_block_hashing_blob of block " << blockHash; + } + m_blobs.push_back(ba); + assert(m_blockIndex.size() == m_blocks.size()); return true; @@ -2653,6 +2670,7 @@ void Blockchain::removeLastBlock() { m_blocks.pop_back(); m_blockIndex.pop(); + m_blobs.pop_back(); assert(m_blockIndex.size() == m_blocks.size()); } diff --git a/src/CryptoNoteCore/Blockchain.h b/src/CryptoNoteCore/Blockchain.h index dfae3d9c17..fd579a216c 100755 --- a/src/CryptoNoteCore/Blockchain.h +++ b/src/CryptoNoteCore/Blockchain.h @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers -// Copyright (c) 2016-2021, The Karbo developers +// Copyright (c) 2016-2022, The Karbo developers // // This file is part of Karbo. // @@ -130,6 +130,8 @@ namespace CryptoNote { bool isBlockInMainChain(const Crypto::Hash& blockId); bool isInCheckpointZone(const uint32_t height); + bool getHashingBlob(const uint32_t height, BinaryArray& blob); + template bool scanOutputKeysForIndexes(const KeyInput& tx_in_to_key, visitor_t& vis, uint32_t* pmax_related_block_height = NULL); bool addMessageQueue(MessageQueue& messageQueue); @@ -262,6 +264,8 @@ namespace CryptoNote { typedef parallel_flat_hash_map>> outputs_container; //Crypto::Hash - tx hash, size_t - index of out in transaction typedef parallel_flat_hash_map> MultisignatureOutputsContainer; + typedef std::vector hashing_blobs_container; + const Currency& m_currency; tx_memory_pool& m_tx_pool; std::recursive_mutex m_blockchain_lock; // TODO: add here reader/writer lock @@ -280,7 +284,6 @@ namespace CryptoNote { typedef parallel_flat_hash_map BlockMap; typedef parallel_flat_hash_map TransactionMap; typedef BasicUpgradeDetector UpgradeDetector; - friend class BlockCacheSerializer; friend class BlockchainIndicesSerializer; @@ -288,6 +291,9 @@ namespace CryptoNote { CryptoNote::BlockIndex m_blockIndex; TransactionMap m_transactionMap; MultisignatureOutputsContainer m_multisignatureOutputs; + + hashing_blobs_container m_blobs; + UpgradeDetector m_upgradeDetectorV2; UpgradeDetector m_upgradeDetectorV3; UpgradeDetector m_upgradeDetectorV4;