std::list<LedgerEntry>::const_iterator
LedgerStateRoot::Impl::loadOffers(StatementContext& prep,
                                  std::list<LedgerEntry>& offers) const
{
    std::string actIDStrKey;
    unsigned int sellingAssetType, buyingAssetType;
    std::string sellingAssetCode, buyingAssetCode, sellingIssuerStrKey,
        buyingIssuerStrKey;
    soci::indicator sellingAssetCodeIndicator, buyingAssetCodeIndicator,
        sellingIssuerIndicator, buyingIssuerIndicator;

    LedgerEntry le;
    le.data.type(OFFER);
    OfferEntry& oe = le.data.offer();

    auto& st = prep.statement();
    st.exchange(soci::into(actIDStrKey));
    st.exchange(soci::into(oe.offerID));
    st.exchange(soci::into(sellingAssetType));
    st.exchange(soci::into(sellingAssetCode, sellingAssetCodeIndicator));
    st.exchange(soci::into(sellingIssuerStrKey, sellingIssuerIndicator));
    st.exchange(soci::into(buyingAssetType));
    st.exchange(soci::into(buyingAssetCode, buyingAssetCodeIndicator));
    st.exchange(soci::into(buyingIssuerStrKey, buyingIssuerIndicator));
    st.exchange(soci::into(oe.amount));
    st.exchange(soci::into(oe.price.n));
    st.exchange(soci::into(oe.price.d));
    st.exchange(soci::into(oe.flags));
    st.exchange(soci::into(le.lastModifiedLedgerSeq));
    st.define_and_bind();
    st.execute(true);

    auto iterNext = offers.cend();
    while (st.got_data())
    {
        oe.sellerID = KeyUtils::fromStrKey<PublicKey>(actIDStrKey);
        processAsset(oe.selling, (AssetType)sellingAssetType,
                     sellingIssuerStrKey, sellingIssuerIndicator,
                     sellingAssetCode, sellingAssetCodeIndicator);
        processAsset(oe.buying, (AssetType)buyingAssetType, buyingIssuerStrKey,
                     buyingIssuerIndicator, buyingAssetCode,
                     buyingAssetCodeIndicator);

        if (iterNext == offers.cend())
        {
            iterNext = offers.emplace(iterNext, le);
        }
        else
        {
            offers.emplace_back(le);
        }
        st.fetch();
    }

    return iterNext;
}
Esempio n. 2
0
void
TrustFrame::loadLines(StatementContext& prep,
                      std::function<void(LedgerEntry const&)> trustProcessor)
{
    string actIDStrKey;
    std::string issuerStrKey, assetCode;
    unsigned int assetType;

    LedgerEntry le;
    le.data.type(TRUSTLINE);

    TrustLineEntry& tl = le.data.trustLine();

    auto& st = prep.statement();
    st.exchange(into(actIDStrKey));
    st.exchange(into(assetType));
    st.exchange(into(issuerStrKey));
    st.exchange(into(assetCode));
    st.exchange(into(tl.limit));
    st.exchange(into(tl.balance));
    st.exchange(into(tl.flags));
    st.exchange(into(le.lastModifiedLedgerSeq));
    st.define_and_bind();

    st.execute(true);
    while (st.got_data())
    {
        tl.accountID = PubKeyUtils::fromStrKey(actIDStrKey);
        tl.asset.type((AssetType)assetType);
        if (assetType == ASSET_TYPE_CREDIT_ALPHANUM4)
        {
            tl.asset.alphaNum4().issuer = PubKeyUtils::fromStrKey(issuerStrKey);
            strToAssetCode(tl.asset.alphaNum4().assetCode, assetCode);
        }
        else if (assetType == ASSET_TYPE_CREDIT_ALPHANUM12)
        {
            tl.asset.alphaNum12().issuer =
                PubKeyUtils::fromStrKey(issuerStrKey);
            strToAssetCode(tl.asset.alphaNum12().assetCode, assetCode);
        }

        if (!isValid(tl))
        {
            throw std::runtime_error("Invalid TrustEntry");
        }

        trustProcessor(le);

        st.fetch();
    }
}
Esempio n. 3
0
// peerRecordProcessor returns false if we should stop processing entries
void
PeerRecord::loadPeerRecords(
    Database& db, StatementContext& prep,
    std::function<bool(PeerRecord const&)> peerRecordProcessor)
{
    std::string ip;
    tm nextAttempt;
    int lport;
    int numFailures;
    auto& st = prep.statement();
    st.exchange(into(ip));
    st.exchange(into(lport));
    st.exchange(into(nextAttempt));
    st.exchange(into(numFailures));
    int flags;
    st.exchange(into(flags));

    st.define_and_bind();
    {
        auto timer = db.getSelectTimer("peer");
        st.execute(true);
    }
    while (st.got_data())
    {
        if (!ip.empty() && lport > 0)
        {
            auto address =
                PeerBareAddress{ip, static_cast<unsigned short>(lport)};
            auto pr = PeerRecord{address, VirtualClock::tmToPoint(nextAttempt),
                                 numFailures};
            pr.setPreferred((flags & PEER_RECORD_FLAGS_PREFERRED) != 0);

            if (!peerRecordProcessor(pr))
            {
                return;
            }
        }
        st.fetch();
    }
}
Esempio n. 4
0
void
OfferFrame::loadOffers(StatementContext& prep,
                       std::function<void(LedgerEntry const&)> offerProcessor)
{
    string actIDStrKey;
    unsigned int sellingAssetType, buyingAssetType;
    std::string sellingAssetCode, buyingAssetCode, sellingIssuerStrKey,
        buyingIssuerStrKey;

    soci::indicator sellingAssetCodeIndicator, buyingAssetCodeIndicator,
        sellingIssuerIndicator, buyingIssuerIndicator;

    LedgerEntry le;
    le.data.type(OFFER);
    OfferEntry& oe = le.data.offer();

    statement& st = prep.statement();
    st.exchange(into(actIDStrKey));
    st.exchange(into(oe.offerID));
    st.exchange(into(sellingAssetType));
    st.exchange(into(sellingAssetCode, sellingAssetCodeIndicator));
    st.exchange(into(sellingIssuerStrKey, sellingIssuerIndicator));
    st.exchange(into(buyingAssetType));
    st.exchange(into(buyingAssetCode, buyingAssetCodeIndicator));
    st.exchange(into(buyingIssuerStrKey, buyingIssuerIndicator));
    st.exchange(into(oe.amount));
    st.exchange(into(oe.price.n));
    st.exchange(into(oe.price.d));
    st.exchange(into(oe.flags));
    st.exchange(into(le.lastModifiedLedgerSeq));
    st.define_and_bind();
    st.execute(true);
    while (st.got_data())
    {
        oe.sellerID = PubKeyUtils::fromStrKey(actIDStrKey);
        if ((buyingAssetType > ASSET_TYPE_CREDIT_ALPHANUM12) ||
            (sellingAssetType > ASSET_TYPE_CREDIT_ALPHANUM12))
            throw std::runtime_error("bad database state");

        oe.buying.type((AssetType)buyingAssetType);
        oe.selling.type((AssetType)sellingAssetType);
        if (sellingAssetType != ASSET_TYPE_NATIVE)
        {
            if ((sellingAssetCodeIndicator != soci::i_ok) ||
                (sellingIssuerIndicator != soci::i_ok))
            {
                throw std::runtime_error("bad database state");
            }

            if (sellingAssetType == ASSET_TYPE_CREDIT_ALPHANUM12)
            {
                oe.selling.alphaNum12().issuer =
                    PubKeyUtils::fromStrKey(sellingIssuerStrKey);
                strToAssetCode(oe.selling.alphaNum12().assetCode,
                               sellingAssetCode);
            }
            else if (sellingAssetType == ASSET_TYPE_CREDIT_ALPHANUM4)
            {
                oe.selling.alphaNum4().issuer =
                    PubKeyUtils::fromStrKey(sellingIssuerStrKey);
                strToAssetCode(oe.selling.alphaNum4().assetCode,
                               sellingAssetCode);
            }
        }

        if (buyingAssetType != ASSET_TYPE_NATIVE)
        {
            if ((buyingAssetCodeIndicator != soci::i_ok) ||
                (buyingIssuerIndicator != soci::i_ok))
            {
                throw std::runtime_error("bad database state");
            }

            if (buyingAssetType == ASSET_TYPE_CREDIT_ALPHANUM12)
            {
                oe.buying.alphaNum12().issuer =
                    PubKeyUtils::fromStrKey(buyingIssuerStrKey);
                strToAssetCode(oe.buying.alphaNum12().assetCode,
                               buyingAssetCode);
            }
            else if (buyingAssetType == ASSET_TYPE_CREDIT_ALPHANUM4)
            {
                oe.buying.alphaNum4().issuer =
                    PubKeyUtils::fromStrKey(buyingIssuerStrKey);
                strToAssetCode(oe.buying.alphaNum4().assetCode,
                               buyingAssetCode);
            }
        }

        if (!isValid(oe))
        {
            throw std::runtime_error("Invalid asset");
        }

        offerProcessor(le);
        st.fetch();
    }
}