Ejemplo n.º 1
0
bool
OfferStream::step (Logs& l)
{
    // Modifying the order or logic of these
    // operations causes a protocol breaking change.

    auto viewJ = l.journal ("View");
    for(;;)
    {
        // BookTip::step deletes the current offer from the view before
        // advancing to the next (unless the ledger entry is missing).
        if (! tip_.step(l))
            return false;

        std::shared_ptr<SLE> entry = tip_.entry();

        // If we exceed the maximum number of allowed steps, we're done.
        if (!counter_.step ())
            return false;

        // Remove if missing
        if (! entry)
        {
            erase (view_);
            erase (cancelView_);
            continue;
        }

        // Remove if expired
        using d = NetClock::duration;
        using tp = NetClock::time_point;
        if (entry->isFieldPresent (sfExpiration) &&
            tp{d{(*entry)[sfExpiration]}} <= expire_)
        {
            JLOG(j_.trace) <<
                "Removing expired offer " << entry->getIndex();
            offerDelete (cancelView_,
                cancelView_.peek(keylet::offer(entry->key())), viewJ);
            continue;
        }

        offer_ = Offer (entry, tip_.quality());

        Amounts const amount (offer_.amount());

        // Remove if either amount is zero
        if (amount.empty())
        {
            JLOG(j_.warning) <<
                "Removing bad offer " << entry->getIndex();
            offerDelete (cancelView_,
                cancelView_.peek(keylet::offer(entry->key())), viewJ);
            offer_ = Offer{};
            continue;
        }

        // Calculate owner funds
        auto const owner_funds = accountFunds(view_,
            offer_.owner(), amount.out, fhZERO_IF_FROZEN, viewJ);

        // Check for unfunded offer
        if (owner_funds <= zero)
        {
            // If the owner's balance in the pristine view is the same,
            // we haven't modified the balance and therefore the
            // offer is "found unfunded" versus "became unfunded"
            auto const original_funds = accountFunds(cancelView_,
                offer_.owner(), amount.out, fhZERO_IF_FROZEN, viewJ);

            if (original_funds == owner_funds)
            {
                offerDelete (cancelView_, cancelView_.peek(
                    keylet::offer(entry->key())), viewJ);
                JLOG(j_.trace) <<
                    "Removing unfunded offer " << entry->getIndex();
            }
            else
            {
                JLOG(j_.trace) <<
                    "Removing became unfunded offer " << entry->getIndex();
            }
            offer_ = Offer{};
            continue;
        }

        break;
    }

    return true;
}
Ejemplo n.º 2
0
bool
OfferStream::step ()
{
    // Modifying the order or logic of these
    // operations causes a protocol breaking change.

    for(;;)
    {
        // BookTip::step deletes the current offer from the view before
        // advancing to the next (unless the ledger entry is missing).
        if (! m_tip.step())
            return false;

        SLE::pointer const& entry (m_tip.entry());

        // Remove if missing
        if (! entry)
        {
            erase (view());
            erase (view_cancel());
            continue;
        }

        // Remove if expired
        if (entry->isFieldPresent (sfExpiration) &&
                entry->getFieldU32 (sfExpiration) <= m_when)
        {
            view_cancel().offerDelete (entry->getIndex());
            if (m_journal.trace) m_journal.trace <<
                                                     "Removing expired offer " << entry->getIndex();
            continue;
        }

        m_offer = Offer (entry, m_tip.quality());

        Amounts const amount (m_offer.amount());

        // Remove if either amount is zero
        if (amount.empty())
        {
            view_cancel().offerDelete (entry->getIndex());
            if (m_journal.warning) m_journal.warning <<
                        "Removing bad offer " << entry->getIndex();
            m_offer = Offer{};
            continue;
        }

        // Calculate owner funds
        // NIKB NOTE The calling code also checks the funds, how expensive is
        //           looking up the funds twice?
        Amount const owner_funds (view().accountFunds (
                                      m_offer.account(), m_offer.amount().out, fhZERO_IF_FROZEN));

        // Check for unfunded offer
        if (owner_funds <= zero)
        {
            // If the owner's balance in the pristine view is the same,
            // we haven't modified the balance and therefore the
            // offer is "found unfunded" versus "became unfunded"
            if (view_cancel().accountFunds (m_offer.account(),
                                            m_offer.amount().out, fhZERO_IF_FROZEN) == owner_funds)
            {
                view_cancel().offerDelete (entry->getIndex());
                if (m_journal.trace) m_journal.trace <<
                                                         "Removing unfunded offer " << entry->getIndex();
            }
            else
            {
                if (m_journal.trace) m_journal.trace <<
                                                         "Removing became unfunded offer " << entry->getIndex();
            }
            m_offer = Offer{};
            continue;
        }

        break;
    }

    return true;
}