示例#1
0
    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 {};
    }
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;
}