//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ripple { Transaction::Transaction( std::shared_ptr const& stx, std::string& reason, Application& app) noexcept : mTransaction(stx), mApp(app), j_(app.journal("Ledger")) { try { mTransactionID = mTransaction->getTransactionID(); } catch (std::exception& e) { reason = e.what(); return; } mStatus = NEW; } // // Misc. // void Transaction::setStatus( TransStatus ts, std::uint32_t lseq, std::optional tseq, std::optional netID) { mStatus = ts; mLedgerIndex = lseq; if (tseq) mTxnSeq = tseq; if (netID) mNetworkID = netID; } TransStatus Transaction::sqlTransactionStatus(boost::optional const& status) { char const c = (status) ? (*status)[0] : safe_cast(txnSqlUnknown); switch (c) { case txnSqlNew: return NEW; case txnSqlConflict: return CONFLICTED; case txnSqlHeld: return HELD; case txnSqlValidated: return COMMITTED; case txnSqlIncluded: return INCLUDED; } assert(c == txnSqlUnknown); return INVALID; } Transaction::pointer Transaction::transactionFromSQL( boost::optional const& ledgerSeq, boost::optional const& status, Blob const& rawTxn, Application& app) { std::uint32_t const inLedger = rangeCheckedCast(ledgerSeq.value_or(0)); SerialIter it(makeSlice(rawTxn)); auto txn = std::make_shared(it); std::string reason; auto tr = std::make_shared(txn, reason, app); tr->setStatus(sqlTransactionStatus(status)); tr->setLedger(inLedger); return tr; } std::variant< std::pair, std::shared_ptr>, TxSearched> Transaction::load(uint256 const& id, Application& app, error_code_i& ec) { return load(id, app, std::nullopt, ec); } std::variant< std::pair, std::shared_ptr>, TxSearched> Transaction::load( uint256 const& id, Application& app, ClosedInterval const& range, error_code_i& ec) { using op = std::optional>; return load(id, app, op{range}, ec); } Transaction::Locator Transaction::locate(uint256 const& id, Application& app) { auto const db = dynamic_cast(&app.getRelationalDatabase()); if (!db) { Throw("Failed to get relational database"); } return db->locateTransaction(id); } std::variant< std::pair, std::shared_ptr>, TxSearched> Transaction::load( uint256 const& id, Application& app, std::optional> const& range, error_code_i& ec) { auto const db = dynamic_cast(&app.getRelationalDatabase()); if (!db) { Throw("Failed to get relational database"); } return db->getTransaction(id, range, ec); } // options 1 to include the date of the transaction Json::Value Transaction::getJson(JsonOptions options, bool binary) const { // Note, we explicitly suppress `include_date` option here Json::Value ret( mTransaction->getJson(options & ~JsonOptions::include_date, binary)); // NOTE Binary STTx::getJson output might not be a JSON object if (ret.isObject() && mLedgerIndex) { if (!(options & JsonOptions::disable_API_prior_V2)) { // Behaviour before API version 2 ret[jss::inLedger] = mLedgerIndex; } // TODO: disable_API_prior_V3 to disable output of both `date` and // `ledger_index` elements (taking precedence over include_date) ret[jss::ledger_index] = mLedgerIndex; if (options & JsonOptions::include_date) { auto ct = mApp.getLedgerMaster().getCloseTimeBySeq(mLedgerIndex); if (ct) ret[jss::date] = ct->time_since_epoch().count(); } // compute outgoing CTID // override local network id if it's explicitly in the txn std::optional netID = mNetworkID; if (mTransaction->isFieldPresent(sfNetworkID)) netID = mTransaction->getFieldU32(sfNetworkID); if (mTxnSeq && netID && *mTxnSeq <= 0xFFFFU && *netID < 0xFFFFU && mLedgerIndex < 0xFFFFFFFUL) { std::optional ctid = RPC::encodeCTID(mLedgerIndex, *mTxnSeq, *netID); if (ctid) ret[jss::ctid] = *ctid; } } return ret; } } // namespace ripple