From 0daaa30056c161988b102ef2f51b9c945f7ccdb2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 12 Sep 2022 04:21:24 +0000 Subject: [PATCH] Simplify composite index construction somewhat --- src/hex_utils.rs | 15 ++++++++------- src/persistence.rs | 12 +++++------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/hex_utils.rs b/src/hex_utils.rs index cab5a15..9d59aa9 100644 --- a/src/hex_utils.rs +++ b/src/hex_utils.rs @@ -21,13 +21,14 @@ pub fn to_vec(hex: &str) -> Option> { Some(out) } -#[inline] -pub fn hex_str(value: &[u8]) -> String { - let mut res = String::with_capacity(64); - for v in value { - res += &format!("{:02x}", v); - } - res +pub fn to_composite_index(scid: i64, timestamp: i64, direction: bool) -> String { + let scid_be = scid.to_be_bytes(); + let res = format!("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}:{}:{}", + scid_be[0], scid_be[1], scid_be[2], scid_be[3], + scid_be[4], scid_be[5], scid_be[6], scid_be[7], + timestamp, direction as u8); + assert_eq!(res.len(), 29); // Our SQL Type requires len of 29 + res } pub fn to_compressed_pubkey(hex: &str) -> Option { diff --git a/src/persistence.rs b/src/persistence.rs index ceeec9d..fcc3f30 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -135,15 +135,13 @@ impl GossipPersister { } GossipMessage::ChannelUpdate(update) => { let scid = update.contents.short_channel_id as i64; - let scid_hex = hex_utils::hex_str(&scid.to_be_bytes()); let timestamp = update.contents.timestamp as i64; - let channel_flags = update.contents.flags as i32; - let direction = channel_flags & 1; - let disable = (channel_flags & 2) > 0; + let direction = (update.contents.flags & 1) == 1; + let disable = (update.contents.flags & 2) > 0; - let composite_index = format!("{}:{}:{}", scid_hex, timestamp, direction); + let composite_index = hex_utils::to_composite_index(scid, timestamp, direction); let cltv_expiry_delta = update.contents.cltv_expiry_delta as i32; let htlc_minimum_msat = update.contents.htlc_minimum_msat as i64; @@ -174,8 +172,8 @@ impl GossipPersister { &composite_index, &scid, ×tamp, - &channel_flags, - &(direction == 1), + &(update.contents.flags as i32), + &direction, &disable, &cltv_expiry_delta, &htlc_minimum_msat, -- 2.39.5