lsp_plugin: add reversed feature-bit check

Core-Lightning returns the feature-bits in reversed order but we don't
want to rely on the caller to reverse the u8 slice themselfs. This
commit adds a convenience function that reverses the bitmap to avoid
hard to debug mistakes.

Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
This commit is contained in:
Peter Neuroth
2025-10-07 17:31:45 +02:00
committed by Rusty Russell
parent 22269eb646
commit 722c19ae93
2 changed files with 27 additions and 3 deletions

View File

@@ -591,9 +591,7 @@ async fn ensure_lsp_connected(cln_client: &mut ClnRpc, lsp_id: &str) -> Result<(
// Check that feature bit is set
peer.features.as_deref().map_or(false, |f_str| {
if let Some(feature_bits) = hex::decode(f_str).ok() {
let mut fb = feature_bits.clone();
fb.reverse();
util::is_feature_bit_set(&fb, LSP_FEATURE_BIT)
util::is_feature_bit_set_reversed(&feature_bits, LSP_FEATURE_BIT)
} else {
false
}

View File

@@ -5,6 +5,32 @@ use core::fmt;
use serde_json::Value;
use std::str::FromStr;
/// Checks whether a feature bit is set in a bitmap interpreted as
/// **big-endian across bytes**, while keeping **LSB-first within each byte**.
///
/// This function creates a reversed copy of `bitmap` (so the least-significant
/// byte becomes last), then calls the simple LSB-first `is_feature_bit_set` on it.
/// No mutation of the callers slice occurs.
///
/// In other words:
/// - byte order: **reversed** (big-endian across the slice)
/// - bit order within a byte: **LSB-first** (unchanged)
///
/// If you need *full* MSB-first (also within a byte), dont use this helper—
/// rewrite the mask as `1u8 << (7 - bit_index)` instead.
///
/// # Arguments
/// * `bitmap` byte slice containing the bitfield (original order, not modified)
/// * `feature_bit` zero-based bit index across the entire bitmap
///
/// # Returns
/// `true` if the bit is set; `false` if the bit is unset or out of bounds
pub fn is_feature_bit_set_reversed(bitmap: &[u8], feature_bit: usize) -> bool {
let mut reversed = bitmap.to_vec();
reversed.reverse();
is_feature_bit_set(&reversed, feature_bit)
}
/// Checks if the feature bit is set in the provided bitmap.
/// Returns true if the `feature_bit` is set in the `bitmap`. Returns false if
/// the `feature_bit` is unset or our ouf bounds.