Skip to content

Commit

Permalink
Merge pull request #168 from aivve/pow/fast_blodha
Browse files Browse the repository at this point in the history
Add blobs caching for faster mining and validation
  • Loading branch information
aivve authored Jan 3, 2022
2 parents 7dd5c1e + 4fbc76a commit 6f9b52a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
42 changes: 30 additions & 12 deletions src/CryptoNoteCore/Blockchain.cpp
Original file line number Diff line number Diff line change
@@ -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.
//
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<std::chrono::milliseconds>(dur).count() << "ms";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<double> duration = std::chrono::steady_clock::now() - timePoint;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<decltype(m_blockchain_lock)> 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));
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
10 changes: 8 additions & 2 deletions src/CryptoNoteCore/Blockchain.h
Original file line number Diff line number Diff line change
@@ -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.
//
Expand Down Expand Up @@ -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<class visitor_t> bool scanOutputKeysForIndexes(const KeyInput& tx_in_to_key, visitor_t& vis, uint32_t* pmax_related_block_height = NULL);

bool addMessageQueue(MessageQueue<BlockchainMessage>& messageQueue);
Expand Down Expand Up @@ -262,6 +264,8 @@ namespace CryptoNote {
typedef parallel_flat_hash_map<uint64_t, std::vector<std::pair<TransactionIndex, uint16_t>>> outputs_container; //Crypto::Hash - tx hash, size_t - index of out in transaction
typedef parallel_flat_hash_map<uint64_t, std::vector<MultisignatureOutputUsage>> MultisignatureOutputsContainer;

typedef std::vector<BinaryArray> 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
Expand All @@ -280,14 +284,16 @@ namespace CryptoNote {
typedef parallel_flat_hash_map<Crypto::Hash, uint32_t> BlockMap;
typedef parallel_flat_hash_map<Crypto::Hash, TransactionIndex> TransactionMap;
typedef BasicUpgradeDetector<Blocks> UpgradeDetector;

friend class BlockCacheSerializer;
friend class BlockchainIndicesSerializer;

Blocks m_blocks;
CryptoNote::BlockIndex m_blockIndex;
TransactionMap m_transactionMap;
MultisignatureOutputsContainer m_multisignatureOutputs;

hashing_blobs_container m_blobs;

UpgradeDetector m_upgradeDetectorV2;
UpgradeDetector m_upgradeDetectorV3;
UpgradeDetector m_upgradeDetectorV4;
Expand Down

0 comments on commit 6f9b52a

Please sign in to comment.