mirror of
https://github.com/ZmnSCPxj/clboss.git
synced 2026-08-15 12:50:42 +02:00
315 lines
8.9 KiB
C++
315 lines
8.9 KiB
C++
#undef NDEBUG
|
|
|
|
#include"Boss/Mod/FeeMonitor.hpp"
|
|
#include"Boss/Msg/CommandRequest.hpp"
|
|
#include"Boss/Msg/CommandResponse.hpp"
|
|
#include"Boss/Msg/DbResource.hpp"
|
|
#include"Boss/Msg/MonitorFeeByBalance.hpp"
|
|
#include"Boss/Msg/MonitorFeeBySize.hpp"
|
|
#include"Boss/Msg/MonitorFeeByTheory.hpp"
|
|
#include"Boss/Msg/MonitorFeeSetChannel.hpp"
|
|
#include"Boss/Msg/PeerMedianChannelFee.hpp"
|
|
#include"Ev/coroutine.hpp"
|
|
#include"Ev/start.hpp"
|
|
#include"Ev/yield.hpp"
|
|
#include"Jsmn/Object.hpp"
|
|
#include"Ln/NodeId.hpp"
|
|
#include"S/Bus.hpp"
|
|
#include"Sqlite3.hpp"
|
|
|
|
#include<assert.h>
|
|
#include<cstdint>
|
|
#include<iomanip>
|
|
#include<sstream>
|
|
|
|
namespace {
|
|
auto const A = Ln::NodeId(
|
|
"020000000000000000000000000000000000000000000000000000000000000001"
|
|
);
|
|
auto const B = Ln::NodeId(
|
|
"020000000000000000000000000000000000000000000000000000000000000002"
|
|
);
|
|
auto const C = Ln::NodeId(
|
|
"020000000000000000000000000000000000000000000000000000000000000003"
|
|
);
|
|
auto const far_past = std::int64_t(0);
|
|
auto const far_future = std::int64_t(4102444800);
|
|
|
|
std::string make_since_query(char const* nodeid, std::int64_t since) {
|
|
auto os = std::ostringstream();
|
|
os << std::setprecision(17)
|
|
<< "{\"nodeid\":\"" << nodeid << "\",\"since\":" << since << "}";
|
|
return os.str();
|
|
}
|
|
|
|
std::string make_before_query(char const* nodeid, std::int64_t before) {
|
|
auto os = std::ostringstream();
|
|
os << std::setprecision(17)
|
|
<< "{\"nodeid\":\"" << nodeid << "\",\"before\":" << before << "}";
|
|
return os.str();
|
|
}
|
|
|
|
std::string make_since_window_query(std::int64_t since) {
|
|
auto os = std::ostringstream();
|
|
os << std::setprecision(17)
|
|
<< "{\"since\":" << since << "}";
|
|
return os.str();
|
|
}
|
|
|
|
std::string make_before_window_query(std::int64_t before) {
|
|
auto os = std::ostringstream();
|
|
os << std::setprecision(17)
|
|
<< "{\"before\":" << before << "}";
|
|
return os.str();
|
|
}
|
|
}
|
|
|
|
Ev::Io<int> run() {
|
|
auto bus = S::Bus();
|
|
auto mut = Boss::Mod::FeeMonitor(bus);
|
|
auto db = Sqlite3::Db(":memory:");
|
|
|
|
auto req_id = std::uint64_t();
|
|
auto last_rsp = Boss::Msg::CommandResponse{};
|
|
auto rsp = false;
|
|
bus.subscribe<Boss::Msg::CommandResponse
|
|
>([&](Boss::Msg::CommandResponse const& m) {
|
|
last_rsp = m;
|
|
rsp = true;
|
|
return Ev::yield();
|
|
});
|
|
|
|
auto msg_db = Boss::Msg::DbResource{db};
|
|
co_await bus.raise(std::move(msg_db));
|
|
auto msg_peer_median = Boss::Msg::PeerMedianChannelFee{A, 10, 100};
|
|
co_await bus.raise(std::move(msg_peer_median));
|
|
auto msg_fee_size = Boss::Msg::MonitorFeeBySize{A, 10, 3, 1.1};
|
|
co_await bus.raise(std::move(msg_fee_size));
|
|
auto msg_fee_balance = Boss::Msg::MonitorFeeByBalance{A, 1.2, 1000, 2000};
|
|
co_await bus.raise(std::move(msg_fee_balance));
|
|
auto msg_fee_theory = Boss::Msg::MonitorFeeByTheory{A, 5, 1.3, 7};
|
|
co_await bus.raise(std::move(msg_fee_theory));
|
|
auto msg_set_a = Boss::Msg::MonitorFeeSetChannel{A, 1000, 10};
|
|
co_await bus.raise(std::move(msg_set_a));
|
|
auto msg_set_b = Boss::Msg::MonitorFeeSetChannel{B, 3000, 30};
|
|
co_await bus.raise(std::move(msg_set_b));
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_peers_initial = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-peers",
|
|
Jsmn::Object::parse_json("{}"),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_peers_initial));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
auto result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["peers"].size() == 2);
|
|
assert(std::string(result["peers"][0]) == std::string(A));
|
|
assert(std::string(result["peers"][1]) == std::string(B));
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_peers_before_past = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-peers",
|
|
Jsmn::Object::parse_json(
|
|
make_before_window_query(far_past).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_peers_before_past));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["peers"].size() == 0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_peers_since_future = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-peers",
|
|
Jsmn::Object::parse_json(
|
|
make_since_window_query(far_future).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_peers_since_future));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["peers"].size() == 0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_peers_before_future = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-peers",
|
|
Jsmn::Object::parse_json(
|
|
make_before_window_query(far_future).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_peers_before_future));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["peers"].size() == 2);
|
|
assert(std::string(result["peers"][0]) == std::string(A));
|
|
assert(std::string(result["peers"][1]) == std::string(B));
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_initial = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
"{\"nodeid\":\"020000000000000000000000000000000000000000000000000000000000000001\"}"
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_initial));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
auto history = result["history"];
|
|
assert(history.size() == 1);
|
|
assert(double(history[0]["set_base"]) == 1000.0);
|
|
assert(history[0].has("price_center"));
|
|
assert(double(history[0]["price_center"]) == 7.0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_since_past = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
make_since_query(
|
|
"020000000000000000000000000000000000000000000000000000000000000001",
|
|
far_past
|
|
).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_since_past));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 1);
|
|
assert(double(result["history"][0]["set_base"]) == 1000.0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_before_past = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
make_before_query(
|
|
"020000000000000000000000000000000000000000000000000000000000000001",
|
|
far_past
|
|
).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_before_past));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_since_future = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
make_since_query(
|
|
"020000000000000000000000000000000000000000000000000000000000000001",
|
|
far_future
|
|
).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_since_future));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_before_future = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
make_before_query(
|
|
"020000000000000000000000000000000000000000000000000000000000000001",
|
|
far_future
|
|
).c_str()
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_before_future));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 1);
|
|
assert(double(result["history"][0]["set_base"]) == 1000.0);
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_node_b = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
"{\"nodeid\":\"020000000000000000000000000000000000000000000000000000000000000002\"}"
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_node_b));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 1);
|
|
assert(double(result["history"][0]["set_base"]) == 3000.0);
|
|
assert(result["history"][0].has("price_center"));
|
|
assert(result["history"][0]["price_center"].is_null());
|
|
|
|
++req_id;
|
|
rsp = false;
|
|
auto req_node_c = Boss::Msg::CommandRequest{
|
|
"clboss-feemon-history",
|
|
Jsmn::Object::parse_json(
|
|
"{\"nodeid\":\"020000000000000000000000000000000000000000000000000000000000000003\"}"
|
|
),
|
|
Ln::CommandId::left(req_id)
|
|
};
|
|
co_await bus.raise(std::move(req_node_c));
|
|
assert(rsp);
|
|
assert(last_rsp.id == Ln::CommandId::left(req_id));
|
|
result = Jsmn::Object::parse_json(
|
|
last_rsp.response.output().c_str()
|
|
);
|
|
assert(result["history"].size() == 0);
|
|
|
|
co_return 0;
|
|
}
|
|
|
|
int main() {
|
|
auto io = run();
|
|
auto ec = Ev::start(io);
|
|
assert(ec == 0);
|
|
return 0;
|
|
}
|