Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boss/Mod/Tally.cpp: Implement clboss-tally. #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 312 additions & 0 deletions Boss/Mod/Tally.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
#include"Boss/Mod/Tally.hpp"
#include"Boss/Msg/CommandRequest.hpp"
#include"Boss/Msg/CommandResponse.hpp"
#include"Boss/Msg/DbResource.hpp"
#include"Boss/Msg/ForwardFee.hpp"
#include"Boss/Msg/Manifestation.hpp"
#include"Boss/Msg/ManifestCommand.hpp"
#include"Boss/Msg/ResponseMoveFunds.hpp"
#include"Boss/Msg/SwapCompleted.hpp"
#include"Boss/Msg/TimerTwiceDaily.hpp"
#include"Boss/concurrent.hpp"
#include"Ev/Io.hpp"
#include"Ev/now.hpp"
#include"Ev/yield.hpp"
#include"Json/Out.hpp"
#include"Ln/Amount.hpp"
#include"S/Bus.hpp"
#include"Sqlite3.hpp"
#include"Util/date.hpp"
#include"Util/make_unique.hpp"
#include<assert.h>

namespace {

/* How long, in seconds, to keep tally data history. */
auto constexpr tally_history_age = double(60.0 * 60.0 * 24.0 * 365.0);

}

namespace Boss { namespace Mod {

class Tally::Impl {
private:
S::Bus& bus;
Sqlite3::Db db;

void start() {
bus.subscribe<Msg::DbResource
>([this](Msg::DbResource const& m) {
db = m.db;
return init();
});

bus.subscribe<Msg::ForwardFee
>([this](Msg::ForwardFee const& m) {
return add_tally( "+forwarding_earnings", m.fee
, "Total earned from forwarding "
"fees."
);
});
bus.subscribe<Msg::ResponseMoveFunds
>([this](Msg::ResponseMoveFunds const& m) {
return add_tally( "-rebalancing_costs", m.fee_spent
, "Total lost paying for "
"rebalances."
);
});
bus.subscribe<Msg::SwapCompleted
>([this](Msg::SwapCompleted const& m) {
/* SwapCompleted is broadcast while another
* module has a transaction open.
* So do it in the background.
*/
auto cost = m.amount_sent - m.amount_received;
return Boss::concurrent(Ev::lift().then([ this
, cost
]() {
return add_tally( "-inbound_liquidity_swap_costs"
, cost
, "Total lost buying inbound "
"liquidity using the "
"swap-to-onchain technique."
);
}));
});
/* TODO: opening costs, closing costs, liquidity ads
* (earnings from them buying, costs of us buying). */

bus.subscribe<Msg::Manifestation
>([this](Msg::Manifestation const&) {
return bus.raise(Msg::ManifestCommand{
"clboss-cleartally", "",
"Clear the tally.",
false
}) + bus.raise(Msg::ManifestCommand{
"clboss-tally", "",
"Get the tally of earnings and "
"expenditures.",
false
});
});
bus.subscribe<Msg::CommandRequest
>([this](Msg::CommandRequest const& m) {
if (m.command != "clboss-cleartally")
return Ev::lift();
auto id = m.id;
return db.transact().then([ this
, id
](Sqlite3::Tx tx) {
tx.query_execute(R"QRY(
DELETE FROM "Tally";
)QRY");
tx.query_execute(R"QRY(
DELETE FROM "Tally_history";
)QRY");
tx.commit();
return bus.raise(Msg::CommandResponse{
id, Json::Out::empty_object()
});
});
});
bus.subscribe<Msg::CommandRequest
>([this](Msg::CommandRequest const& m) {
if (m.command != "clboss-tally")
return Ev::lift();
auto id = m.id;
return get_tally().then([ this
, id
](Json::Out res) {
return bus.raise(Msg::CommandResponse{
id, std::move(res)
});
});
});

/* Sample the current total and add an entry
* to the "Tally_history" table.
*/
bus.subscribe<Msg::TimerTwiceDaily
>([this](Msg::TimerTwiceDaily const&) {
return sample_total();
});
}

Ev::Io<void> init() {
return db.transact().then([](Sqlite3::Tx tx) {
tx.query_execute(R"QRY(
CREATE TABLE IF NOT EXISTS "Tally"
( name TEXT PRIMARY KEY
, amount INTEGER NOT NULL
, comment TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "Tally_history"
( time REAL PRIMARY KEY
, total INTEGER NOT NULL
);
)QRY");
tx.commit();
return Ev::lift();
});
}

Ev::Io<void> add_tally( char const* name
, Ln::Amount amount
, char const* comment
) {
assert(name[0] == '+' || name[0] == '-');
if (!db)
return Ev::yield().then([ this
, name, amount, comment
]() {
return add_tally(name, amount, comment);
});
return db.transact().then([ name, amount, comment
](Sqlite3::Tx tx) {
auto curr_amount = Ln::Amount::msat(0);
auto fetch = tx.query(R"SQL(
SELECT amount FROM "Tally"
WHERE name = :name;
)SQL")
.bind(":name", name)
.execute()
;
for (auto& r : fetch) {
curr_amount = Ln::Amount::msat(
r.get<std::uint64_t>(0)
);
}
tx.query(R"SQL(
INSERT OR REPLACE INTO "Tally"
VALUES(:name, :amount, :comment);
)SQL")
.bind(":name", name)
.bind(":amount", (curr_amount + amount).to_msat())
.bind(":comment", comment)
.execute()
;
tx.commit();

return Ev::lift();
});
}

Ev::Io<Json::Out> get_tally() {
if (!db)
return Ev::yield().then([this]() {
return get_tally();
});
return db.transact().then([](Sqlite3::Tx tx) {
auto total = std::int64_t(0);

auto fetch = tx.query(R"QRY(
SELECT name, amount, comment FROM "Tally";
)QRY").execute();
auto result = Json::Out();
auto obj = result.start_object();
for (auto& r : fetch) {
auto name = r.get<std::string>(0);
auto amount = Ln::Amount::msat(
r.get<std::uint64_t>(1)
);
auto comment = r.get<std::string>(2);
obj.start_object(name)
.field("amount", std::string(amount))
.field("comment", comment)
.end_object();
if (name.size() >= 1 && name[0] == '+')
total += std::int64_t(
amount.to_msat()
);
else
total -= std::int64_t(
amount.to_msat()
);
}
obj.field("total", total);
obj.field( "comment"
, "Reset all tallies to 0 "
"via `clboss-cleartally`. "
"This data is purely for node "
"operator and CLBOSS will never "
"use this in its heuristics."
);
auto arr = obj.start_array("history");
auto fetch2 = tx.query(R"QRY(
SELECT time, total FROM "Tally_history"
ORDER BY time ASC;
)QRY").execute();
for (auto& r : fetch2) {
auto time = r.get<double>(0);
auto total = r.get<std::int64_t>(1);
arr.start_object()
.field("time", time)
.field("time_human", Util::date(time))
.field("total", total)
.end_object();
}
arr.end_array();
obj.end_object();

tx.commit();
return Ev::lift(std::move(result));
});
}

Ev::Io<void> sample_total() {
if (!db)
return Ev::yield().then([this]() {
return sample_total();
});
return db.transact().then([](Sqlite3::Tx tx) {
auto total = std::int64_t(0);
auto fetch = tx.query(R"QRY(
SELECT name, amount FROM "Tally";
)QRY").execute();
for (auto& r : fetch) {
auto name = r.get<std::string>(0);
auto amount = r.get<std::uint64_t>(1);
if (name.size() >= 1 && name[0] == '+')
total += std::int64_t(amount);
else
total -= std::int64_t(amount);
}
tx.query(R"QRY(
INSERT OR IGNORE INTO "Tally_history"
VALUES(:time, :total);
)QRY")
.bind(":time", Ev::now())
.bind(":total", total)
.execute()
;
tx.query(R"QRY(
DELETE FROM "Tally_history"
WHERE time < :mintime;
)QRY")
.bind( ":mintime"
, Ev::now() - tally_history_age
)
.execute()
;
tx.commit();

return Ev::lift();
});
}

public:
Impl() =delete;
Impl(Impl const&) =delete;
Impl(Impl&&) =delete;

explicit
Impl(S::Bus& bus_) : bus(bus_) { start(); }
};

Tally::Tally(Tally&&) =default;
Tally::~Tally() =default;

Tally::Tally(S::Bus& bus)
: pimpl(Util::make_unique<Impl>(bus)) { }

}}
32 changes: 32 additions & 0 deletions Boss/Mod/Tally.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef BOSS_MOD_TALLY_HPP
#define BOSS_MOD_TALLY_HPP

#include<memory>

namespace S { class Bus; }

namespace Boss { namespace Mod {

/** class Boss::Mod::Tally
*
* @brief Module to keep track of some msat values.
*/
class Tally {
private:
class Impl;
std::unique_ptr<Impl> pimpl;

public:
Tally() =delete;
Tally(Tally const&) =delete;

Tally(Tally&&);
~Tally();

explicit
Tally(S::Bus&);
};

}}

#endif /* BOSS_MOD_TALLY_HPP */
4 changes: 4 additions & 0 deletions Boss/Mod/all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include"Boss/Mod/StatusCommand.hpp"
#include"Boss/Mod/SwapManager.hpp"
#include"Boss/Mod/SwapReporter.hpp"
#include"Boss/Mod/Tally.hpp"
#include"Boss/Mod/TimerTwiceDailyAnnouncer.hpp"
#include"Boss/Mod/Timers.hpp"
#include"Boss/Mod/UnmanagedManager.hpp"
Expand Down Expand Up @@ -215,6 +216,9 @@ std::shared_ptr<void> all( std::ostream& cout
/* Unmanaged nodes. */
all->install<UnmanagedManager>(bus);

/* Tally. */
all->install<Tally>(bus);

return all;
}

Expand Down
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Implement `clboss-tally`, which provides summary on earnings vs. expenditures for the use of the node operator.
- Support string "id" fields in the plugin interface.
- Disable compiling debug information by default; if you need this, explicitly include `-g` in your `configure` command, like so: `./configure CXXFLAGS="-O2 -g"`. This reduces binary size by 20x.
- Enable SQLITE3 extended error codes.
Expand Down
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ libclboss_la_SOURCES = \
Boss/Mod/SwapManager.hpp \
Boss/Mod/SwapReporter.cpp \
Boss/Mod/SwapReporter.hpp \
Boss/Mod/Tally.cpp \
Boss/Mod/Tally.hpp \
Boss/Mod/TimerTwiceDailyAnnouncer.cpp \
Boss/Mod/TimerTwiceDailyAnnouncer.hpp \
Boss/Mod/Timers.cpp \
Expand Down Expand Up @@ -594,6 +596,7 @@ TESTS = \
tests/boss/test_rpc \
tests/boss/test_stringid \
tests/boss/test_swapmanager \
tests/boss/test_tally \
tests/boss/test_unmanagedmanager \
tests/boss/test_version \
tests/boss/test_waiter_timed \
Expand Down
Loading