clboss/Boss/Mod/SetConfigHandler.cpp
Ken Sedgwick 7fa87c01e7
XMoveFunds: age the clboss-xrebalance layer, runtime-tunable via setconfig
Adds periodic askrene-age against the persistent clboss-xrebalance
layer so capacity constraints written by inform_channel_constrained
do not accumulate forever.  Mirrors FundsMover's age_clboss_layer:
fires on Msg::TimerRandomHourly, logs num_removed at Debug, and
catches RpcError with -32601 stayed at Debug (graceful degradation
on CLN without askrene-age) while other codes promote to Warn so
sustained aging failure is visible.

The aging cutoff is controlled by a new plugin option,
clboss-xrebalance-age-secs, default 3600 (1h) to match FundsMover's
production value.  Operators on networks with slower flows (signet)
are expected to widen this; the right value is empirical and will
be tuned after observation.

The option is registered dynamic=true so the window is mutable at
runtime via:

  lightning-cli setconfig clboss-xrebalance-age-secs <secs>

No clboss / lightningd restart required.

To support that, three small infrastructure pieces:

  * Boss::Msg::ManifestOption gains a bool dynamic field (default
    false; preserves existing behavior).

  * Boss::Mod::Manifester emits the per-option dynamic flag in the
    getmanifest response, so lightningd knows to forward setconfig
    for that option.

  * New Boss::Mod::SetConfigHandler module records (name -> dynamic
    flag) from Msg::ManifestOption events, then handles incoming
    Msg::CommandRequest where command == "setconfig" by validating
    the named option is registered + dynamic and re-raising a fresh
    Msg::Option on the bus.  Existing option handlers re-apply the
    new value transparently.

Contract for any future opt-in to dynamic: at startup lightningd
encodes Int / Bool / Flag option values as JSON primitives, but at
setconfig time it encodes them as JSON strings.  Handlers for
dynamic options must accept both Jsmn shapes.  The XMoveFunds
option handler does this; the contract is documented in
SetConfigHandler's header comment so future modules can opt in
safely.
2026-06-01 09:50:46 -07:00

100 lines
3 KiB
C++

#include"Boss/Mod/SetConfigHandler.hpp"
#include"Boss/Msg/CommandFail.hpp"
#include"Boss/Msg/CommandRequest.hpp"
#include"Boss/Msg/CommandResponse.hpp"
#include"Boss/Msg/ManifestOption.hpp"
#include"Boss/Msg/Option.hpp"
#include"Boss/log.hpp"
#include"Ev/Io.hpp"
#include"Jsmn/Object.hpp"
#include"Json/Out.hpp"
#include"S/Bus.hpp"
namespace {
/* JSON-RPC error code for malformed parameters, matching the
* JSONRPC2 invalid-params constant used elsewhere in clboss. */
constexpr int RPC_INVALID_PARAMS = -32602;
}
namespace Boss { namespace Mod {
void SetConfigHandler::start() {
bus.subscribe<Boss::Msg::ManifestOption
>([this](Boss::Msg::ManifestOption const& o) {
options[o.name] = o.dynamic;
return Ev::lift();
});
bus.subscribe<Boss::Msg::CommandRequest
>([this](Boss::Msg::CommandRequest const& m) {
if (m.command != "setconfig")
return Ev::lift();
auto id = m.id;
auto const& params = m.params;
/* Extract `config` (required string). */
if (!params.is_object() || !params.has("config")
|| !params["config"].is_string()) {
return bus.raise(Boss::Msg::CommandFail{
id, RPC_INVALID_PARAMS,
"setconfig: missing or non-string 'config' "
"parameter",
Json::Out::empty_object()
});
}
auto name = std::string(params["config"]);
/* Verify the option is one we registered, and is
* declared dynamic. Lightningd should never forward
* setconfig for a non-dynamic option (libplugin would
* refuse it on the receiving side too), but defending
* here keeps the error surface clear and prevents a
* surprise Msg::Option re-raise for an option whose
* handler may not expect runtime updates. */
auto it = options.find(name);
if (it == options.end()) {
return bus.raise(Boss::Msg::CommandFail{
id, RPC_INVALID_PARAMS,
"setconfig: unknown option '" + name + "'",
Json::Out::empty_object()
});
}
if (!it->second) {
return bus.raise(Boss::Msg::CommandFail{
id, RPC_INVALID_PARAMS,
"setconfig: option '" + name
+ "' is not dynamic",
Json::Out::empty_object()
});
}
/* Forward the value as-is. Lightningd encodes the new
* value as a JSON string (see plugin_set_dynamic_opt in
* cln/lightningd/plugin.c), so handlers will see a
* Jsmn::Object with is_string() == true here even for
* numeric option types. The contract documented on
* SetConfigHandler covers this.
*
* Note: bus.raise(Msg::Option) broadcasts to every
* Msg::Option subscriber, not just the one that owns
* this option. Subscribers must filter by name and
* no-op on non-matches -- see the doc comment on
* Boss::Msg::Option for the full contract. */
auto value = params.has("val")
? params["val"]
: Jsmn::Object();
return Boss::log( bus, Debug
, "SetConfigHandler: dispatching setconfig "
"'%s'"
, name.c_str()
)
+ bus.raise(Boss::Msg::Option{name, std::move(value)})
+ bus.raise(Boss::Msg::CommandResponse{
id, Json::Out::empty_object()
});
});
}
}}