Skip to content

Commit

Permalink
optimize code using shared_ptr return
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyShi22 committed Jan 17, 2024
1 parent 77fd245 commit bb1df47
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions bcos-ledger/src/libledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1463,9 +1463,9 @@ void Ledger::asyncGetSystemTableEntry(const std::string_view& table, const std::
}

template <typename MerkleType, typename HashRangeType>
static std::vector<h256> getMerkleTreeFromCache(int64_t blockNumber, Ledger::CacheType& cache,
RecursiveMutex& mutex, const std::string& cacheName, const MerkleType& merkle,
const HashRangeType& hashesRange)
static std::shared_ptr<std::vector<h256>> getMerkleTreeFromCache(int64_t blockNumber,
Ledger::CacheType& cache, RecursiveMutex& mutex, const std::string& cacheName,
const MerkleType& merkle, const HashRangeType& hashesRange)
{
std::shared_ptr<std::vector<h256>> merkleTree = std::make_shared<std::vector<h256>>();
{
Expand All @@ -1475,7 +1475,7 @@ static std::vector<h256> getMerkleTreeFromCache(int64_t blockNumber, Ledger::Cac
{
cache.insert(blockNumber, merkleTree);
}
else if (!merkleTreePtr.get()->empty())
else
{
merkleTree = merkleTreePtr.get();
LEDGER_LOG(DEBUG) << LOG_BADGE(cacheName) << LOG_DESC("Hit cache")
Expand All @@ -1491,7 +1491,7 @@ static std::vector<h256> getMerkleTreeFromCache(int64_t blockNumber, Ledger::Cac
merkle.template generateMerkle(hashesRange, *newMerkleTree);
{
RecursiveGuard l(mutex);
merkleTree.reset(newMerkleTree.get());
*merkleTree = std::move(*newMerkleTree);
}

LEDGER_LOG(DEBUG) << LOG_BADGE(cacheName)
Expand All @@ -1500,7 +1500,7 @@ static std::vector<h256> getMerkleTreeFromCache(int64_t blockNumber, Ledger::Cac
<< LOG_KV("blockNumber", blockNumber);
}

return *merkleTree;
return merkleTree;
}

void Ledger::getTxProof(
Expand Down Expand Up @@ -1556,7 +1556,7 @@ void Ledger::getTxProof(
getMerkleTreeFromCache(blockNumber, m_txProofMerkleCache,
m_txMerkleMtx, "getTxProof", merkle, hashesRange);
merkle.template generateMerkleProof(
hashesRange, merkleTree, _txHash, *merkleProofPtr);
hashesRange, *merkleTree, _txHash, *merkleProofPtr);

LEDGER_LOG(TRACE)
<< LOG_BADGE("getTxProof") << LOG_DESC("get merkle proof success")
Expand Down Expand Up @@ -1604,7 +1604,7 @@ void Ledger::getReceiptProof(protocol::TransactionReceipt::Ptr _receipt,
m_receiptMerkleMtx, "getReceiptProof", merkle, hashesRange);

merkle.template generateMerkleProof(
hashesRange, merkleTree, receiptHash, *merkleProofPtr);
hashesRange, *merkleTree, receiptHash, *merkleProofPtr);

LEDGER_LOG(TRACE)
<< LOG_BADGE("getReceiptProof") << LOG_DESC("get merkle proof success")
Expand Down

0 comments on commit bb1df47

Please sign in to comment.