-
Notifications
You must be signed in to change notification settings - Fork 1k
lsps-plugin: add cln-lsps-policy plugin for LSPS2 fee policy management #9349
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
Open
Andezion
wants to merge
3
commits into
ElementsProject:master
Choose a base branch
from
Andezion:lsps-policy-rpc-master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| use anyhow::{anyhow, bail}; | ||
| use chrono::{Duration, Utc}; | ||
| use cln_lsps::proto::lsps0::{Msat, Ppm}; | ||
| use cln_lsps::proto::lsps2::{ | ||
| Lsps2PolicyGetChannelCapacityRequest, Lsps2PolicyGetChannelCapacityResponse, | ||
| Lsps2PolicyGetInfoRequest, Lsps2PolicyGetInfoResponse, PolicyOpeningFeeParams, | ||
| }; | ||
| use cln_plugin::options::{self, DefaultIntegerConfigOption}; | ||
|
|
||
| const OPTION_MIN_FEE_MSAT: DefaultIntegerConfigOption = options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-min-fee-msat", | ||
| 1_000, | ||
| "Minimum opening fee in millisatoshis charged by the LSP", | ||
| ); | ||
|
|
||
| const OPTION_PROPORTIONAL_PPM: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-proportional-ppm", | ||
| 0, | ||
| "Proportional opening fee in parts-per-million of the payment size", | ||
| ); | ||
|
|
||
| const OPTION_VALID_UNTIL_HOURS: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-valid-until-hours", | ||
| 1, | ||
| "Number of hours for which the offered fee parameters are valid", | ||
| ); | ||
|
|
||
| const OPTION_MIN_LIFETIME: DefaultIntegerConfigOption = options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-min-lifetime", | ||
| 144, | ||
| "Minimum channel lifetime in blocks that the LSP guarantees", | ||
| ); | ||
|
|
||
| const OPTION_MAX_CLIENT_TO_SELF_DELAY: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-max-client-to-self-delay", | ||
| 2016, | ||
| "Maximum to_self_delay (in blocks) accepted from the client", | ||
| ); | ||
|
|
||
| const OPTION_MIN_PAYMENT_SIZE_MSAT: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-min-payment-size-msat", | ||
| 1_000, | ||
| "Minimum payment size in millisatoshis that will trigger a JIT channel opening", | ||
| ); | ||
|
|
||
| const OPTION_MAX_PAYMENT_SIZE_MSAT: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-max-payment-size-msat", | ||
| 10_000_000_000, | ||
| "Maximum payment size in millisatoshis that will trigger a JIT channel opening", | ||
| ); | ||
|
|
||
| const OPTION_CHANNEL_CAPACITY_MSAT: DefaultIntegerConfigOption = | ||
| options::ConfigOption::new_i64_with_default( | ||
| "lsps2-policy-channel-capacity-msat", | ||
| 10_000_000_000, | ||
| "Channel capacity in millisatoshis to open for JIT channel requests", | ||
| ); | ||
|
|
||
| #[derive(Clone)] | ||
| struct State { | ||
| min_fee_msat: Msat, | ||
| proportional: Ppm, | ||
| valid_until_hours: i64, | ||
| min_lifetime: u32, | ||
| max_client_to_self_delay: u32, | ||
| min_payment_size_msat: Msat, | ||
| max_payment_size_msat: Msat, | ||
| channel_capacity_msat: u64, | ||
| } | ||
|
|
||
| #[allow(clippy::too_many_arguments)] | ||
| fn build_state( | ||
| min_fee_msat: i64, | ||
| proportional: i64, | ||
| valid_until_hours: i64, | ||
| min_lifetime: i64, | ||
| max_client_to_self_delay: i64, | ||
| min_payment_size_msat: i64, | ||
| max_payment_size_msat: i64, | ||
| channel_capacity_msat: i64, | ||
| ) -> Result<State, anyhow::Error> { | ||
| if min_fee_msat < 0 { | ||
| bail!( | ||
| "`{}` must be non-negative, got {}", | ||
| OPTION_MIN_FEE_MSAT.name, | ||
| min_fee_msat | ||
| ); | ||
| } | ||
| if proportional < 0 || proportional > 1_000_000 { | ||
| bail!( | ||
| "`{}` must be between 0 and 1000000, got {}", | ||
| OPTION_PROPORTIONAL_PPM.name, | ||
| proportional | ||
| ); | ||
| } | ||
| if valid_until_hours <= 0 { | ||
| bail!( | ||
| "`{}` must be positive, got {}", | ||
| OPTION_VALID_UNTIL_HOURS.name, | ||
| valid_until_hours | ||
| ); | ||
| } | ||
| if Duration::try_hours(valid_until_hours).is_none() { | ||
| bail!( | ||
| "`{}` is out of range, got {}", | ||
| OPTION_VALID_UNTIL_HOURS.name, | ||
| valid_until_hours | ||
| ); | ||
| } | ||
| if min_lifetime < 0 || min_lifetime > i64::from(u32::MAX) { | ||
| bail!( | ||
| "`{}` must be between 0 and {}, got {}", | ||
| OPTION_MIN_LIFETIME.name, | ||
| u32::MAX, | ||
| min_lifetime | ||
| ); | ||
| } | ||
| if max_client_to_self_delay < 0 || max_client_to_self_delay > i64::from(u32::MAX) { | ||
| bail!( | ||
| "`{}` must be between 0 and {}, got {}", | ||
| OPTION_MAX_CLIENT_TO_SELF_DELAY.name, | ||
| u32::MAX, | ||
| max_client_to_self_delay | ||
| ); | ||
| } | ||
| if min_payment_size_msat < 0 { | ||
| bail!( | ||
| "`{}` must be non-negative, got {}", | ||
| OPTION_MIN_PAYMENT_SIZE_MSAT.name, | ||
| min_payment_size_msat | ||
| ); | ||
| } | ||
| if max_payment_size_msat <= min_payment_size_msat { | ||
| bail!( | ||
| "`{}` must be greater than `{}`, got {} <= {}", | ||
| OPTION_MAX_PAYMENT_SIZE_MSAT.name, | ||
| OPTION_MIN_PAYMENT_SIZE_MSAT.name, | ||
| max_payment_size_msat, | ||
| min_payment_size_msat | ||
| ); | ||
| } | ||
| if channel_capacity_msat <= 0 { | ||
| bail!( | ||
| "`{}` must be positive, got {}", | ||
| OPTION_CHANNEL_CAPACITY_MSAT.name, | ||
| channel_capacity_msat | ||
| ); | ||
| } | ||
| if channel_capacity_msat % 1_000 != 0 { | ||
| bail!( | ||
| "`{}` must be divisible by 1000 (whole satoshis), got {}", | ||
| OPTION_CHANNEL_CAPACITY_MSAT.name, | ||
| channel_capacity_msat | ||
| ); | ||
| } | ||
|
|
||
| Ok(State { | ||
| min_fee_msat: Msat::from_msat(min_fee_msat as u64), | ||
| proportional: Ppm::from_ppm(proportional as u32), | ||
| valid_until_hours, | ||
| min_lifetime: min_lifetime as u32, | ||
| max_client_to_self_delay: max_client_to_self_delay as u32, | ||
| min_payment_size_msat: Msat::from_msat(min_payment_size_msat as u64), | ||
| max_payment_size_msat: Msat::from_msat(max_payment_size_msat as u64), | ||
| channel_capacity_msat: channel_capacity_msat as u64, | ||
| }) | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), anyhow::Error> { | ||
| if let Some(plugin) = cln_plugin::Builder::new(tokio::io::stdin(), tokio::io::stdout()) | ||
| .option(OPTION_MIN_FEE_MSAT) | ||
| .option(OPTION_PROPORTIONAL_PPM) | ||
| .option(OPTION_VALID_UNTIL_HOURS) | ||
| .option(OPTION_MIN_LIFETIME) | ||
| .option(OPTION_MAX_CLIENT_TO_SELF_DELAY) | ||
| .option(OPTION_MIN_PAYMENT_SIZE_MSAT) | ||
| .option(OPTION_MAX_PAYMENT_SIZE_MSAT) | ||
| .option(OPTION_CHANNEL_CAPACITY_MSAT) | ||
| .rpcmethod( | ||
| "lsps2-policy-getpolicy", | ||
| "Returns the LSP fee policy used for LSPS2 JIT channel requests", | ||
| on_getpolicy, | ||
| ) | ||
| .rpcmethod( | ||
| "lsps2-policy-getchannelcapacity", | ||
| "Returns the channel capacity the LSP will open for a given JIT channel request", | ||
| on_getchannelcapacity, | ||
| ) | ||
| .configure() | ||
| .await? | ||
| { | ||
| let state = build_state( | ||
| plugin.option(&OPTION_MIN_FEE_MSAT)?, | ||
| plugin.option(&OPTION_PROPORTIONAL_PPM)?, | ||
| plugin.option(&OPTION_VALID_UNTIL_HOURS)?, | ||
| plugin.option(&OPTION_MIN_LIFETIME)?, | ||
| plugin.option(&OPTION_MAX_CLIENT_TO_SELF_DELAY)?, | ||
| plugin.option(&OPTION_MIN_PAYMENT_SIZE_MSAT)?, | ||
| plugin.option(&OPTION_MAX_PAYMENT_SIZE_MSAT)?, | ||
| plugin.option(&OPTION_CHANNEL_CAPACITY_MSAT)?, | ||
| )?; | ||
|
|
||
| let plugin = plugin.start(state).await?; | ||
| plugin.join().await | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| async fn on_getpolicy( | ||
| p: cln_plugin::Plugin<State>, | ||
| v: serde_json::Value, | ||
| ) -> Result<serde_json::Value, anyhow::Error> { | ||
| let _req: Lsps2PolicyGetInfoRequest = serde_json::from_value(v)?; | ||
|
|
||
| let s = p.state(); | ||
| let offset = Duration::try_hours(s.valid_until_hours) | ||
| .ok_or_else(|| anyhow!("`{}` is out of range", OPTION_VALID_UNTIL_HOURS.name))?; | ||
| let valid_until = Utc::now() | ||
| .checked_add_signed(offset) | ||
| .ok_or_else(|| anyhow!("`{}` is out of range", OPTION_VALID_UNTIL_HOURS.name))?; | ||
|
|
||
| let params = PolicyOpeningFeeParams { | ||
| min_fee_msat: s.min_fee_msat, | ||
| proportional: s.proportional, | ||
| valid_until, | ||
| min_lifetime: s.min_lifetime, | ||
| max_client_to_self_delay: s.max_client_to_self_delay, | ||
| min_payment_size_msat: s.min_payment_size_msat, | ||
| max_payment_size_msat: s.max_payment_size_msat, | ||
| }; | ||
|
|
||
| let res = Lsps2PolicyGetInfoResponse { | ||
| policy_opening_fee_params_menu: vec![params], | ||
| client_rejected: false, | ||
| }; | ||
| Ok(serde_json::to_value(res)?) | ||
| } | ||
|
|
||
| async fn on_getchannelcapacity( | ||
| p: cln_plugin::Plugin<State>, | ||
| v: serde_json::Value, | ||
| ) -> Result<serde_json::Value, anyhow::Error> { | ||
| let req: Lsps2PolicyGetChannelCapacityRequest = serde_json::from_value(v)?; | ||
|
|
||
| let capacity = p | ||
| .state() | ||
| .channel_capacity_msat | ||
| .max(req.init_payment_size.msat()); | ||
|
|
||
| let res = Lsps2PolicyGetChannelCapacityResponse { | ||
| channel_capacity_msat: Some(capacity), | ||
| }; | ||
| Ok(serde_json::to_value(res)?) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this checks that the value is positive, but not that it can actually be converted below. a very large positive
i64would be accepted at startup, thenDuration::try_hourswould fail on everygetpolicycall.could we validate the upper bound here as well so an invalid config fails during startup?