// VFALCO TODO Should this be called findOrAdd ? // InboundLedger::pointer findCreate (uint256 const& hash, std::uint32_t seq, InboundLedger::fcReason reason) { assert (hash.isNonZero ()); InboundLedger::pointer ret; // Ensure that any previous IL is destroyed outside the lock InboundLedger::pointer oldLedger; { ScopedLockType sl (mLock); if (! isStopping ()) { if (reason == InboundLedger::fcCONSENSUS) { if (mConsensusLedger.isNonZero() && (mValidationLedger != mConsensusLedger) && (hash != mConsensusLedger)) { hash_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (mConsensusLedger); if (it != mLedgers.end ()) { oldLedger = it->second; mLedgers.erase (it); } } mConsensusLedger = hash; } else if (reason == InboundLedger::fcVALIDATION) { if (mValidationLedger.isNonZero() && (mValidationLedger != mConsensusLedger) && (hash != mValidationLedger)) { hash_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (mValidationLedger); if (it != mLedgers.end ()) { oldLedger = it->second; mLedgers.erase (it); } } mValidationLedger = hash; } hash_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (hash); if (it != mLedgers.end ()) { ret = it->second; // FIXME: Should set the sequence if it's not set } else { ret = std::make_shared <InboundLedger> (hash, seq, reason, std::ref (m_clock)); assert (ret); mLedgers.insert (std::make_pair (hash, ret)); ret->init (sl); ++mCounter; } } } return ret; }
Ledger::pointer acquire (uint256 const& hash, std::uint32_t seq, InboundLedger::fcReason reason) { assert (hash.isNonZero ()); InboundLedger::pointer inbound; { ScopedLockType sl (mLock); if (! isStopping ()) { auto it = mLedgers.find (hash); if (it != mLedgers.end ()) { inbound = it->second; // If the acquisition failed, don't mark the item as // recently accessed so that it can expire. if (! inbound->isFailed ()) inbound->update (seq); } else { inbound = std::make_shared <InboundLedger> ( hash, seq, reason, std::ref (m_clock)); mLedgers.emplace (hash, inbound); inbound->init (sl); ++mCounter; } } } if (inbound && inbound->isComplete ()) return inbound->getLedger(); return {}; }
bool InboundLedgers::hasLedger (uint256 const& hash) { assert (hash.isNonZero ()); boost::mutex::scoped_lock sl (mLock); return mLedgers.find (hash) != mLedgers.end (); }
std::shared_ptr<Ledger const> acquire(uint256 const& hash, std::uint32_t seq, InboundLedger::Reason reason) override { assert(hash.isNonZero()); assert(reason != InboundLedger::Reason::SHARD || (seq != 0 && app_.getShardStore())); if (isStopping()) return {}; bool isNew = true; std::shared_ptr<InboundLedger> inbound; { ScopedLockType sl(mLock); auto it = mLedgers.find(hash); if (it != mLedgers.end()) { isNew = false; inbound = it->second; } else { inbound = std::make_shared <InboundLedger>( app_, hash, seq, reason, std::ref(m_clock)); mLedgers.emplace(hash, inbound); inbound->init(sl); ++mCounter; } } if (inbound->isFailed()) return {}; if (! isNew) inbound->update(seq); if (! inbound->isComplete()) return {}; if (reason == InboundLedger::Reason::HISTORY) { if (inbound->getLedger()->stateMap().family().isShardBacked()) app_.getNodeStore().copyLedger(inbound->getLedger()); } else if (reason == InboundLedger::Reason::SHARD) { auto shardStore = app_.getShardStore(); if (!shardStore) { JLOG(j_.error()) << "Acquiring shard with no shard store available"; return {}; } if (inbound->getLedger()->stateMap().family().isShardBacked()) shardStore->setStored(inbound->getLedger()); else shardStore->copyLedger(inbound->getLedger()); } return inbound->getLedger(); }
InboundLedger::pointer InboundLedgers::findCreate (uint256 const& hash, uint32 seq) { assert (hash.isNonZero ()); boost::mutex::scoped_lock sl (mLock); InboundLedger::pointer& ptr = mLedgers[hash]; if (ptr) { ptr->touch (); return ptr; } ptr = boost::make_shared<InboundLedger> (hash, seq); if (!ptr->isDone ()) { ptr->addPeers (); ptr->setTimer (); // Cannot call in constructor } else { Ledger::pointer ledger = ptr->getLedger (); ledger->setClosed (); ledger->setImmutable (); theApp->getLedgerMaster ().storeLedger (ledger); WriteLog (lsDEBUG, InboundLedger) << "Acquiring ledger we already have: " << hash; } return ptr; }
void InboundLedgers::dropLedger (uint256 const& hash) { assert (hash.isNonZero ()); boost::mutex::scoped_lock sl (mLock); mLedgers.erase (hash); }
InboundLedger::pointer InboundLedgers::find (uint256 const& hash) { assert (hash.isNonZero ()); boost::mutex::scoped_lock sl (mLock); std::map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (hash); if (it != mLedgers.end ()) { it->second->touch (); return it->second; } return InboundLedger::pointer (); }
bool SHAMapTreeNode::setChildHash (int m, uint256 const& hash) { assert ((m >= 0) && (m < 16)); assert (mType == tnINNER); if (mHashes[m] == hash) return false; mHashes[m] = hash; if (hash.isNonZero ()) mIsBranch |= (1 << m); else mIsBranch &= ~ (1 << m); return updateHash (); }
InboundLedger::pointer find (uint256 const& hash) { assert (hash.isNonZero ()); InboundLedger::pointer ret; { ScopedLockType sl (mLock); hash_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (hash); if (it != mLedgers.end ()) { ret = it->second; } } return ret; }
std::shared_ptr<InboundLedger> find (uint256 const& hash) override { assert (hash.isNonZero ()); std::shared_ptr<InboundLedger> ret; { ScopedLockType sl (mLock); auto it = mLedgers.find (hash); if (it != mLedgers.end ()) { ret = it->second; } } return ret; }
InboundLedger::pointer InboundLedgers::find (uint256 const& hash) { assert (hash.isNonZero ()); InboundLedger::pointer ret; { boost::mutex::scoped_lock sl (mLock); boost::unordered_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (hash); if (it != mLedgers.end ()) { ret = it->second; } } return ret; }
InboundLedger::pointer InboundLedgers::findCreate (uint256 const& hash, uint32 seq) { assert (hash.isNonZero ()); InboundLedger::pointer ret; { boost::mutex::scoped_lock sl (mLock); boost::unordered_map<uint256, InboundLedger::pointer>::iterator it = mLedgers.find (hash); if (it != mLedgers.end ()) { ret = it->second; // FIXME: Should set the sequence if it's not set } else { ret = boost::make_shared<InboundLedger> (hash, seq); assert (ret); mLedgers.insert (std::make_pair (hash, ret)); if (!ret->tryLocal()) { ret->addPeers (); ret->setTimer (); // Cannot call in constructor } else if (!ret->isFailed ()) { WriteLog (lsDEBUG, InboundLedger) << "Acquiring ledger we already have locally: " << hash; Ledger::pointer ledger = ret->getLedger (); ledger->setClosed (); ledger->setImmutable (); getApp().getLedgerMaster ().storeLedger (ledger); } } } return ret; }