Skip to content

Commit

Permalink
restore some backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 committed May 16, 2024
1 parent cc6131e commit d11493a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut networkdir = PathBuf::from_str(&plugin.configuration().lightning_dir).unwrap();
networkdir.pop();
let getinfo = rpc.call_typed(&GetinfoRequest {}).await?;
state = PluginState::new(getinfo.id, rpc_path, sling_dir, networkdir);
state = PluginState::new(getinfo.id, rpc_path, sling_dir, networkdir, getinfo.version);
{
*state.blockheight.lock() = getinfo.blockheight;
}
Expand Down
5 changes: 5 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ impl PluginState {
rpc_path: PathBuf,
sling_dir: PathBuf,
network_dir: PathBuf,
version: String,
) -> PluginState {
PluginState {
config: Arc::new(Mutex::new(Config::new(
pubkey,
rpc_path,
sling_dir,
network_dir,
version,
))),
peer_channels: Arc::new(Mutex::new(BTreeMap::new())),
graph: Arc::new(Mutex::new(LnGraph::new())),
Expand Down Expand Up @@ -143,6 +145,7 @@ pub struct Config {
pub rpc_path: PathBuf,
pub sling_dir: PathBuf,
pub network_dir: PathBuf,
pub version: String,
pub utf8: DynamicConfigOption<bool>,
pub refresh_peers_interval: DynamicConfigOption<u64>,
pub refresh_aliasmap_interval: DynamicConfigOption<u64>,
Expand All @@ -167,12 +170,14 @@ impl Config {
rpc_path: PathBuf,
sling_dir: PathBuf,
network_dir: PathBuf,
version: String,
) -> Config {
Config {
pubkey,
rpc_path,
sling_dir,
network_dir,
version,
utf8: DynamicConfigOption {
name: OPT_UTF8.name,
value: true,
Expand Down
57 changes: 38 additions & 19 deletions src/slings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::util::{
feeppm_effective, feeppm_effective_from_amts, get_normal_channel_from_listpeerchannels,
get_preimage_paymend_hash_pair, get_total_htlc_count, is_channel_normal, my_sleep,
};
use crate::{channel_jobstate_update, wait_for_gossip};
use crate::{channel_jobstate_update, get_remote_feeppm_effective, wait_for_gossip, LnGraph};

pub async fn sling(job: &Job, task: &Task, plugin: &Plugin<PluginState>) -> Result<(), Error> {
let config = plugin.state().config.lock().clone();
Expand Down Expand Up @@ -316,14 +316,36 @@ async fn next_route(
let candidatelist;
if let Some(c) = &job.candidatelist {
if !c.is_empty() {
candidatelist =
build_candidatelist(peer_channels, job, tempbans, config, Some(c), blockheight)
candidatelist = build_candidatelist(
peer_channels,
job,
&graph,
tempbans,
config,
Some(c),
blockheight,
)
} else {
candidatelist =
build_candidatelist(peer_channels, job, tempbans, config, None, blockheight)
candidatelist = build_candidatelist(
peer_channels,
job,
&graph,
tempbans,
config,
None,
blockheight,
)
}
} else {
candidatelist = build_candidatelist(peer_channels, job, tempbans, config, None, blockheight)
candidatelist = build_candidatelist(
peer_channels,
job,
&graph,
tempbans,
config,
None,
blockheight,
)
}

debug!(
Expand Down Expand Up @@ -630,6 +652,7 @@ async fn health_check(
fn build_candidatelist(
peer_channels: &BTreeMap<ShortChannelId, ListpeerchannelsChannels>,
job: &Job,
graph: &LnGraph,
tempbans: &HashMap<ShortChannelId, u64>,
config: &Config,
custom_candidates: Option<&Vec<ShortChannelId>>,
Expand Down Expand Up @@ -659,20 +682,16 @@ fn build_candidatelist(
}
&& scid.block() <= blockheight - config.candidates_min_age.value
{
let chan_updates = if let Some(updates) = &channel.updates {
if let Some(remote) = &updates.remote {
remote
} else {
continue;
}
} else {
continue;
};
let chan_in_ppm = feeppm_effective(
chan_updates.fee_proportional_millionths.unwrap(),
Amount::msat(&chan_updates.fee_base_msat.unwrap()) as u32,
let chan_in_ppm = match get_remote_feeppm_effective(
channel,
graph,
scid,
job.amount_msat,
);
&config.version,
) {
Ok(o) => o,
Err(_) => continue,
};

let to_us_msat = Amount::msat(&channel.to_us_msat.unwrap());
let total_msat = Amount::msat(&channel.total_msat.unwrap());
Expand Down
31 changes: 23 additions & 8 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ pub async fn refresh_graph(plugin: Plugin<PluginState>) -> Result<(), Error> {
}
}

info!(
"Added {} private channels to sling graph after {}ms!",
private_channel_added_count,
now.elapsed().as_millis().to_string()
);

let mut public_channel_added_count = 0;
for chan in &channels {
if (feeppm_effective(
Expand All @@ -246,7 +240,19 @@ pub async fn refresh_graph(plugin: Plugin<PluginState>) -> Result<(), Error> {
source: chan.source,
destination: chan.destination,
short_channel_id: chan.short_channel_id,
scid_alias: None,
scid_alias: if chan.public {
None
} else if let Some(loc_chan) =
local_channels.get(&chan.short_channel_id)
{
if chan.source == my_pubkey {
Some(loc_chan.alias.as_ref().unwrap().local.unwrap())
} else {
Some(loc_chan.alias.as_ref().unwrap().remote.unwrap())
}
} else {
continue;
},
fee_per_millionth: chan.fee_per_millionth,
base_fee_millisatoshi: chan.base_fee_millisatoshi,
htlc_maximum_msat: chan.htlc_maximum_msat,
Expand All @@ -255,9 +261,18 @@ pub async fn refresh_graph(plugin: Plugin<PluginState>) -> Result<(), Error> {
delay: chan.delay,
}),
);
public_channel_added_count += 1;
if chan.public {
public_channel_added_count += 1;
} else {
private_channel_added_count += 1;
}
}
}
info!(
"Added {} private channels to sling graph after {}ms!",
private_channel_added_count,
now.elapsed().as_millis().to_string()
);
info!(
"Added {} public channels to sling graph after {}ms!",
public_channel_added_count,
Expand Down
62 changes: 62 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bitcoin::secp256k1::hashes::Hash;
use bitcoin::secp256k1::hashes::HashEngine;
use cln_rpc::model::responses::ListpeerchannelsChannels;
use cln_rpc::model::responses::ListpeerchannelsChannelsState;
use cln_rpc::primitives::Amount;
use cln_rpc::primitives::PublicKey;
use cln_rpc::primitives::Sha256;
use parking_lot::Mutex;
Expand Down Expand Up @@ -409,3 +410,64 @@ pub async fn wait_for_gossip(plugin: &Plugin<PluginState>, task: &Task) -> Resul
}
Ok(())
}

pub fn get_remote_feeppm_effective(
channel: &ListpeerchannelsChannels,
graph: &LnGraph,
scid: ShortChannelId,
amount_msat: u64,
version: &str,
) -> Result<u64, Error> {
if at_or_above_version(version, "v24.02")? {
let chan_updates = if let Some(updates) = &channel.updates {
if let Some(remote) = &updates.remote {
remote
} else {
return Err(anyhow!("No remote gossip in listpeerchannels"));
}
} else {
return Err(anyhow!("No gossip in listpeerchannels"));
};
let chan_in_ppm = feeppm_effective(
chan_updates.fee_proportional_millionths.unwrap(),
Amount::msat(&chan_updates.fee_base_msat.unwrap()) as u32,
amount_msat,
);
Ok(chan_in_ppm)
} else {
let chan_from_peer = match graph.get_channel(&channel.peer_id.unwrap(), &scid) {
Ok(chan) => chan.channel,
Err(_) => return Err(anyhow!("No gossip for {} in graph", scid)),
};
let chan_in_ppm = feeppm_effective(
chan_from_peer.fee_per_millionth,
chan_from_peer.base_fee_millisatoshi,
amount_msat,
);
Ok(chan_in_ppm)
}
}

pub fn at_or_above_version(my_version: &str, min_version: &str) -> Result<bool, Error> {
let clean_my_version = my_version
.trim_end_matches("-modded")
.trim_start_matches('v');
let clean_min_version = min_version.trim_start_matches('v');

let my_version_parts: Vec<&str> = clean_my_version.split('.').collect();
let min_version_parts: Vec<&str> = clean_min_version.split('.').collect();

if my_version_parts.len() <= 1 || my_version_parts.len() > 3 {
return Err(anyhow!("Version string parse error: {}", my_version));
}
for (my, min) in my_version_parts.iter().zip(min_version_parts.iter()) {
let my_num: u32 = my.parse()?;
let min_num: u32 = min.parse()?;

if my_num != min_num {
return Ok(my_num > min_num);
}
}

Ok(my_version_parts.len() >= min_version_parts.len())
}
22 changes: 11 additions & 11 deletions tests/test_sling.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,15 @@ def test_option_errors(node_factory, get_plugin): # noqa: F811
def test_maxhops_2(node_factory, bitcoind, get_plugin): # noqa: F811
l1, l2 = node_factory.get_nodes(
2,
opts={
"plugin": get_plugin,
"sling-maxhops": 2,
"sling-refresh-graph-interval": 1,
},
opts=[
{
"plugin": get_plugin,
"sling-maxhops": 2,
"sling-refresh-graph-interval": 1,
"log-level": "io",
},
{},
],
)
l1.fundwallet(10_000_000)
l2.fundwallet(10_000_000)
Expand Down Expand Up @@ -445,13 +449,9 @@ def test_private_channel_receive(node_factory, bitcoind, get_plugin): # noqa: F
scid_pub = chan["short_channel_id"]

l1.wait_channel_active(scid_pub)
l1.wait_local_channel_active(scid_priv)

if l1.info["version"].startswith("v23"):
l1.daemon.wait_for_log(r"Added 4 public channels")
else:
l1.daemon.wait_for_log(r"Added 2 private channels")
l1.daemon.wait_for_log(r"Added 2 public channels")
l1.daemon.wait_for_log(r"Added 2 private channels")
l1.daemon.wait_for_log(r"Added 2 public channels")

l1.rpc.call(
"sling-job",
Expand Down

0 comments on commit d11493a

Please sign in to comment.