2020-10-05 16:28:44 +08:00
|
|
|
#include"Boss/Mod/ForwardFeeMonitor.hpp"
|
|
|
|
|
#include"Boss/Msg/ForwardFee.hpp"
|
|
|
|
|
#include"Boss/Msg/ManifestNotification.hpp"
|
|
|
|
|
#include"Boss/Msg/Manifestation.hpp"
|
|
|
|
|
#include"Boss/Msg/Notification.hpp"
|
|
|
|
|
#include"Boss/concurrent.hpp"
|
|
|
|
|
#include"Boss/log.hpp"
|
2022-05-28 11:52:30 +08:00
|
|
|
#include"Ev/map.hpp"
|
2020-10-05 16:28:44 +08:00
|
|
|
#include"Jsmn/Object.hpp"
|
|
|
|
|
#include"S/Bus.hpp"
|
|
|
|
|
#include"Util/stringify.hpp"
|
|
|
|
|
|
|
|
|
|
namespace Boss { namespace Mod {
|
|
|
|
|
|
|
|
|
|
void ForwardFeeMonitor::start() {
|
|
|
|
|
bus.subscribe<Msg::Manifestation
|
|
|
|
|
>([this](Msg::Manifestation const& _) {
|
|
|
|
|
return bus.raise(Msg::ManifestNotification{"forward_event"});
|
|
|
|
|
});
|
|
|
|
|
bus.subscribe<Msg::Notification
|
|
|
|
|
>([this](Msg::Notification const& n) {
|
|
|
|
|
if (n.notification != "forward_event")
|
|
|
|
|
return Ev::lift();
|
|
|
|
|
|
2022-05-28 11:52:30 +08:00
|
|
|
auto in_scid = Ln::Scid();
|
|
|
|
|
auto out_scid = Ln::Scid();
|
2020-10-05 16:28:44 +08:00
|
|
|
auto fee = Ln::Amount();
|
|
|
|
|
auto resolution_time = double();
|
|
|
|
|
try {
|
|
|
|
|
auto payload = n.params["forward_event"];
|
|
|
|
|
if ( !payload.has("out_channel")
|
2024-07-21 12:47:00 -07:00
|
|
|
|| !payload.has("fee_msat")
|
2020-10-05 16:28:44 +08:00
|
|
|
|| !payload.has("resolved_time")
|
|
|
|
|
|| !payload.has("received_time")
|
|
|
|
|
)
|
|
|
|
|
return Ev::lift();
|
|
|
|
|
if (std::string(payload["status"]) != "settled")
|
|
|
|
|
return Ev::lift();
|
|
|
|
|
|
2022-05-28 11:52:30 +08:00
|
|
|
in_scid = Ln::Scid(std::string(
|
2020-10-05 16:28:44 +08:00
|
|
|
payload["in_channel"]
|
|
|
|
|
));
|
2022-05-28 11:52:30 +08:00
|
|
|
out_scid = Ln::Scid(std::string(
|
2020-10-05 16:28:44 +08:00
|
|
|
payload["out_channel"]
|
|
|
|
|
));
|
2023-05-02 20:20:50 -07:00
|
|
|
fee = Ln::Amount::object(
|
2020-10-05 16:28:44 +08:00
|
|
|
payload["fee_msat"]
|
2023-05-02 20:20:50 -07:00
|
|
|
);
|
2020-10-05 16:28:44 +08:00
|
|
|
resolution_time = double(payload["resolved_time"])
|
|
|
|
|
- double(payload["received_time"])
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
} catch (std::runtime_error const& _) {
|
|
|
|
|
return Boss::log( bus, Error
|
|
|
|
|
, "ForwardFeeMonitor: Unexpected "
|
|
|
|
|
"forward_event payload: %s"
|
|
|
|
|
, Util::stringify(n.params).c_str()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 11:52:30 +08:00
|
|
|
auto f = [this](Ln::Scid scid) {
|
|
|
|
|
return peer_from_scid_rr.execute(Msg::RequestPeerFromScid{
|
|
|
|
|
nullptr, scid
|
|
|
|
|
}).then([](Msg::ResponsePeerFromScid r) {
|
|
|
|
|
return Ev::lift(std::move(r.peer));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
auto scids = std::vector<Ln::Scid>{in_scid, out_scid};
|
|
|
|
|
return Ev::map( std::move(f), std::move(scids)
|
|
|
|
|
).then([ this
|
|
|
|
|
, fee
|
|
|
|
|
, resolution_time
|
|
|
|
|
](std::vector<Ln::NodeId> nids) {
|
|
|
|
|
return cont( std::move(nids[0])
|
|
|
|
|
, std::move(nids[1])
|
|
|
|
|
, fee
|
|
|
|
|
, resolution_time
|
|
|
|
|
);
|
|
|
|
|
});
|
2020-10-05 16:28:44 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 11:52:30 +08:00
|
|
|
Ev::Io<void> ForwardFeeMonitor::cont( Ln::NodeId in_id
|
|
|
|
|
, Ln::NodeId out_id
|
|
|
|
|
, Ln::Amount fee
|
|
|
|
|
, double resolution_time
|
|
|
|
|
) {
|
|
|
|
|
if (!in_id || !out_id)
|
|
|
|
|
return Ev::lift();
|
|
|
|
|
|
|
|
|
|
auto act = Ev::lift();
|
|
|
|
|
act += Boss::log( bus, Debug
|
|
|
|
|
, "ForwardFeeMonitor: %s -> %s, fee: %s, "
|
|
|
|
|
"time: %.3f"
|
|
|
|
|
, std::string(in_id).c_str()
|
|
|
|
|
, std::string(out_id).c_str()
|
|
|
|
|
, std::string(fee).c_str()
|
|
|
|
|
, resolution_time
|
|
|
|
|
);
|
|
|
|
|
act += Boss::concurrent(bus.raise(Msg::ForwardFee{
|
|
|
|
|
std::move(in_id),
|
|
|
|
|
std::move(out_id),
|
|
|
|
|
fee,
|
|
|
|
|
resolution_time
|
|
|
|
|
}));
|
|
|
|
|
return act;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:28:44 +08:00
|
|
|
}}
|