From 656a6c5eb5c665756c19c66bd4630ef6b060068d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 9 Jul 2015 16:19:11 -0700 Subject: [PATCH] Add modify function to OpenLedger::accept --- src/ripple/app/ledger/OpenLedger.h | 29 ++++++++++++++++++----- src/ripple/app/ledger/impl/OpenLedger.cpp | 9 ++++--- src/ripple/app/tests/Offer.test.cpp | 2 +- src/ripple/test/jtx/Env.h | 12 ++++++---- src/ripple/test/jtx/impl/Env.cpp | 5 ++-- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/ripple/app/ledger/OpenLedger.h b/src/ripple/app/ledger/OpenLedger.h index 1275b7083..0f8d415df 100644 --- a/src/ripple/app/ledger/OpenLedger.h +++ b/src/ripple/app/ledger/OpenLedger.h @@ -57,6 +57,20 @@ private: std::shared_ptr current_; public: + /** Signature for modification functions. + + The modification function is called during + apply and modify with an OpenView to accumulate + changes and the Journal to use for logging. + + A return value of `true` informs OpenLedger + that changes were made. Always returning + `true` won't cause harm, but it may be + sub-optimal. + */ + using modify_type = std::function< + bool(OpenView&, beast::Journal)>; + OpenLedger() = delete; OpenLedger (OpenLedger const&) = delete; OpenLedger& operator= (OpenLedger const&) = delete; @@ -105,17 +119,13 @@ public: Thread safety: Can be called concurrently from any thread. - `f` will be called as - bool(ReadView&) - If `f` returns `true`, the changes made in the OpenView will be published to the open ledger. @return `true` if the open view was changed */ bool - modify (std::function< - bool(OpenView&, beast::Journal)> const& f); + modify (modify_type const& f); /** Accept a new ledger. @@ -135,6 +145,12 @@ public: The list of local transactions are applied to the new open view. + The optional modify function f is called + to perform further modifications to the + open view, atomically. Changes made in + the modify function are not visible to + callers until accept() returns. + Any failed, retriable transactions are left in `retries` for the caller. @@ -150,7 +166,8 @@ public: OrderedTxs const& locals, bool retriesFirst, OrderedTxs& retries, ApplyFlags flags, HashRouter& router, - std::string const& suffix = ""); + std::string const& suffix = "", + modify_type const& f = {}); /** Algorithm for applying transactions. diff --git a/src/ripple/app/ledger/impl/OpenLedger.cpp b/src/ripple/app/ledger/impl/OpenLedger.cpp index 658a47324..5fc3afb5e 100644 --- a/src/ripple/app/ledger/impl/OpenLedger.cpp +++ b/src/ripple/app/ledger/impl/OpenLedger.cpp @@ -54,8 +54,7 @@ OpenLedger::current() const } bool -OpenLedger::modify (std::function< - bool(OpenView&, beast::Journal)> const& f) +OpenLedger::modify (modify_type const& f) { std::lock_guard< std::mutex> lock1(modify_mutex_); @@ -77,7 +76,8 @@ OpenLedger::accept(Application& app, Rules const& rules, std::shared_ptr const& ledger, OrderedTxs const& locals, bool retriesFirst, OrderedTxs& retries, ApplyFlags flags, - HashRouter& router, std::string const& suffix) + HashRouter& router, std::string const& suffix, + modify_type const& f) { JLOG(j_.trace) << "accept ledger " << ledger->seq() << " " << suffix; @@ -112,6 +112,9 @@ OpenLedger::accept(Application& app, Rules const& rules, for (auto const& item : locals) ripple::apply(app, *next, *item.second, flags, j_); + // Call the modifier + if (f) + f(*next, j_); // Switch to the new open view std::lock_guard< std::mutex> lock2(current_mutex_); diff --git a/src/ripple/app/tests/Offer.test.cpp b/src/ripple/app/tests/Offer.test.cpp index eab5cb00a..1c7e51d3b 100644 --- a/src/ripple/app/tests/Offer.test.cpp +++ b/src/ripple/app/tests/Offer.test.cpp @@ -56,7 +56,7 @@ public: // ledger close times have a dynamic resolution depending on network // conditions it appears the resolution in test is 10 seconds - env.close (tp); + env.close (tp, {}); NetClock::time_point const pct ( std::chrono::seconds (env.open ()->info ().parentCloseTime)); diff --git a/src/ripple/test/jtx/Env.h b/src/ripple/test/jtx/Env.h index 975d74c25..9744b4c48 100644 --- a/src/ripple/test/jtx/Env.h +++ b/src/ripple/test/jtx/Env.h @@ -195,7 +195,8 @@ public: The Env clock is set to the new time. */ void - close (NetClock::time_point const& closeTime); + close (NetClock::time_point const& closeTime, + OpenLedger::modify_type const& f = {}); /** Close and advance the ledger. @@ -205,10 +206,11 @@ public: template void close (std::chrono::duration< - Rep, Period> const& elapsed) + Rep, Period> const& elapsed, + OpenLedger::modify_type const& f = {}) { stopwatch_.advance(elapsed); - close (clock.now() + elapsed); + close (clock.now() + elapsed, f); } /** Close and advance the ledger. @@ -217,9 +219,9 @@ public: the previous ledger closing time. */ void - close() + close(OpenLedger::modify_type const& f = {}) { - close (std::chrono::seconds(5)); + close (std::chrono::seconds(5), f); } /** Turn on JSON tracing. diff --git a/src/ripple/test/jtx/impl/Env.cpp b/src/ripple/test/jtx/impl/Env.cpp index 284c60fbd..8e5d10108 100644 --- a/src/ripple/test/jtx/impl/Env.cpp +++ b/src/ripple/test/jtx/impl/Env.cpp @@ -104,7 +104,8 @@ Env::closed() const } void -Env::close(NetClock::time_point const& closeTime) +Env::close(NetClock::time_point const& closeTime, + OpenLedger::modify_type const& f) { clock.set(closeTime); // VFALCO TODO Fix the Ledger constructor @@ -134,7 +135,7 @@ Env::close(NetClock::time_point const& closeTime) ledgerPossibleTimeResolutions[0], false, app().config()); OrderedTxs locals({}); openLedger.accept(app(), next->rules(), next, - locals, false, retries, applyFlags(), *router); + locals, false, retries, applyFlags(), *router, "", f); closed_ = next; cachedSLEs_.expire(); }