Skip to content

Commit

Permalink
Cleanup AcceptedLedger and AcceptedLedgerTx:
Browse files Browse the repository at this point in the history
This commit modernizes the `AcceptedLedger` and `AcceptedLedgerTx`
classes, reduces their memory footprint and reduces unnecessary
dynamic memory allocations.
  • Loading branch information
nbougalis authored and manojsdoshi committed Mar 30, 2022
1 parent c7e6803 commit 8f58687
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 272 deletions.
40 changes: 22 additions & 18 deletions src/ripple/app/ledger/AcceptedLedger.cpp
Expand Up @@ -19,8 +19,7 @@

#include <ripple/app/ledger/AcceptedLedger.h>
#include <ripple/app/main/Application.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/chrono.h>
#include <algorithm>

namespace ripple {

Expand All @@ -29,29 +28,34 @@ AcceptedLedger::AcceptedLedger(
Application& app)
: mLedger(ledger)
{
transactions_.reserve(256);

auto insertAll = [&](auto const& txns) {
auto const& idcache = app.accountIDCache();

for (auto const& item : txns)
{
insert(std::make_shared<AcceptedLedgerTx>(
ledger,
item.first,
item.second,
app.accountIDCache(),
app.logs()));
}
transactions_.emplace_back(std::make_unique<AcceptedLedgerTx>(
ledger, item.first, item.second, idcache));
};

if (app.config().reporting())
insertAll(flatFetchTransactions(*ledger, app));
{
auto const txs = flatFetchTransactions(*ledger, app);
transactions_.reserve(txs.size());
insertAll(txs);
}
else
{
transactions_.reserve(256);
insertAll(ledger->txs);
}

void
AcceptedLedger::insert(AcceptedLedgerTx::ref at)
{
assert(mMap.find(at->getIndex()) == mMap.end());
mMap.insert(std::make_pair(at->getIndex(), at));
}

std::sort(
transactions_.begin(),
transactions_.end(),
[](auto const& a, auto const& b) {
return a->getTxnSeq() < b->getTxnSeq();
});
}

} // namespace ripple
37 changes: 17 additions & 20 deletions src/ripple/app/ledger/AcceptedLedger.h
Expand Up @@ -41,43 +41,40 @@ namespace ripple {
the result of the a consensus process (though haven't validated
it yet).
*/
class AcceptedLedger
class AcceptedLedger : public CountedObject<AcceptedLedger>
{
public:
using pointer = std::shared_ptr<AcceptedLedger>;
using ret = const pointer&;
using map_t = std::map<int, AcceptedLedgerTx::pointer>;
// mapt_t must be an ordered map!
using value_type = map_t::value_type;
using const_iterator = map_t::const_iterator;
AcceptedLedger(
std::shared_ptr<ReadView const> const& ledger,
Application& app);

public:
std::shared_ptr<ReadView const> const&
getLedger() const
{
return mLedger;
}
const map_t&
getMap() const

std::size_t
size() const
{
return mMap;
return transactions_.size();
}

int
getTxnCount() const
auto
begin() const
{
return mMap.size();
return transactions_.begin();
}

AcceptedLedger(
std::shared_ptr<ReadView const> const& ledger,
Application& app);
auto
end() const
{
return transactions_.end();
}

private:
void insert(AcceptedLedgerTx::ref);

std::shared_ptr<ReadView const> mLedger;
map_t mMap;
std::vector<std::unique_ptr<AcceptedLedgerTx>> transactions_;
};

} // namespace ripple
Expand Down
70 changes: 17 additions & 53 deletions src/ripple/app/ledger/AcceptedLedgerTx.cpp
Expand Up @@ -18,7 +18,6 @@
//==============================================================================

#include <ripple/app/ledger/AcceptedLedgerTx.h>
#include <ripple/app/main/Application.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/protocol/UintTypes.h>
Expand All @@ -30,72 +29,30 @@ AcceptedLedgerTx::AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const& txn,
std::shared_ptr<STObject const> const& met,
AccountIDCache const& accountCache,
Logs& logs)
: mLedger(ledger)
, mTxn(txn)
, mMeta(std::make_shared<TxMeta>(
txn->getTransactionID(),
ledger->seq(),
*met))
, mAffected(mMeta->getAffectedAccounts(logs.journal("View")))
, accountCache_(accountCache)
, logs_(logs)
AccountIDCache const& accountCache)
: mTxn(txn)
, mMeta(txn->getTransactionID(), ledger->seq(), *met)
, mAffected(mMeta.getAffectedAccounts())
{
assert(!ledger->open());

mResult = mMeta->getResultTER();

Serializer s;
met->add(s);
mRawMeta = std::move(s.modData());

buildJson();
}

AcceptedLedgerTx::AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const& txn,
TER result,
AccountIDCache const& accountCache,
Logs& logs)
: mLedger(ledger)
, mTxn(txn)
, mResult(result)
, mAffected(txn->getMentionedAccounts())
, accountCache_(accountCache)
, logs_(logs)
{
assert(ledger->open());
buildJson();
}

std::string
AcceptedLedgerTx::getEscMeta() const
{
assert(!mRawMeta.empty());
return sqlBlobLiteral(mRawMeta);
}

void
AcceptedLedgerTx::buildJson()
{
mJson = Json::objectValue;
mJson[jss::transaction] = mTxn->getJson(JsonOptions::none);

if (mMeta)
{
mJson[jss::meta] = mMeta->getJson(JsonOptions::none);
mJson[jss::raw_meta] = strHex(mRawMeta);
}
mJson[jss::meta] = mMeta.getJson(JsonOptions::none);
mJson[jss::raw_meta] = strHex(mRawMeta);

mJson[jss::result] = transHuman(mResult);
mJson[jss::result] = transHuman(mMeta.getResultTER());

if (!mAffected.empty())
{
Json::Value& affected = (mJson[jss::affected] = Json::arrayValue);
for (auto const& account : mAffected)
affected.append(accountCache_.toBase58(account));
affected.append(accountCache.toBase58(account));
}

if (mTxn->getTxnType() == ttOFFER_CREATE)
Expand All @@ -107,14 +64,21 @@ AcceptedLedgerTx::buildJson()
if (account != amount.issue().account)
{
auto const ownerFunds = accountFunds(
*mLedger,
*ledger,
account,
amount,
fhIGNORE_FREEZE,
logs_.journal("View"));
beast::Journal{beast::Journal::getNullSink()});
mJson[jss::transaction][jss::owner_funds] = ownerFunds.getText();
}
}
}

std::string
AcceptedLedgerTx::getEscMeta() const
{
assert(!mRawMeta.empty());
return sqlBlobLiteral(mRawMeta);
}

} // namespace ripple
51 changes: 8 additions & 43 deletions src/ripple/app/ledger/AcceptedLedgerTx.h
Expand Up @@ -39,40 +39,22 @@ class Logs;
- Which accounts are affected
* This is used by InfoSub to report to clients
- Cached stuff
@code
@endcode
@see {uri}
@ingroup ripple_ledger
*/
class AcceptedLedgerTx
class AcceptedLedgerTx : public CountedObject<AcceptedLedgerTx>
{
public:
using pointer = std::shared_ptr<AcceptedLedgerTx>;
using ref = const pointer&;

public:
AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const&,
std::shared_ptr<STObject const> const&,
AccountIDCache const&,
Logs&);
AcceptedLedgerTx(
std::shared_ptr<ReadView const> const&,
std::shared_ptr<STTx const> const&,
TER,
AccountIDCache const&,
Logs&);
AccountIDCache const&);

std::shared_ptr<STTx const> const&
getTxn() const
{
return mTxn;
}
std::shared_ptr<TxMeta> const&
TxMeta const&
getMeta() const
{
return mMeta;
Expand All @@ -97,45 +79,28 @@ class AcceptedLedgerTx
TER
getResult() const
{
return mResult;
return mMeta.getResultTER();
}
std::uint32_t
getTxnSeq() const
{
return mMeta->getIndex();
}

bool
isApplied() const
{
return bool(mMeta);
}
int
getIndex() const
{
return mMeta ? mMeta->getIndex() : 0;
return mMeta.getIndex();
}
std::string
getEscMeta() const;
Json::Value

Json::Value const&
getJson() const
{
return mJson;
}

private:
std::shared_ptr<ReadView const> mLedger;
std::shared_ptr<STTx const> mTxn;
std::shared_ptr<TxMeta> mMeta;
TER mResult;
TxMeta mMeta;
boost::container::flat_set<AccountID> mAffected;
Blob mRawMeta;
Json::Value mJson;
AccountIDCache const& accountCache_;
Logs& logs_;

void
buildJson();
};

} // namespace ripple
Expand Down
5 changes: 1 addition & 4 deletions src/ripple/app/ledger/OrderBookDB.cpp
Expand Up @@ -245,10 +245,7 @@ OrderBookDB::processTxn(
// single client has subscribed to those books.
hash_set<std::uint64_t> havePublished;

// Check if this is an offer or an offer cancel or a payment that
// consumes an offer.
// Check to see what the meta looks like.
for (auto& node : alTx.getMeta()->getNodes())
for (auto const& node : alTx.getMeta().getNodes())
{
try
{
Expand Down

0 comments on commit 8f58687

Please sign in to comment.