Simplify composite index construction somewhat
authorMatt Corallo <git@bluematt.me>
Mon, 12 Sep 2022 04:21:24 +0000 (04:21 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 13 Sep 2022 16:17:40 +0000 (16:17 +0000)
src/hex_utils.rs
src/persistence.rs

index cab5a1551299ccdb251f372364cc12663945e551..9d59aa91cece52a6a50c9d514fead7e6f1858685 100644 (file)
@@ -21,13 +21,14 @@ pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
     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<PublicKey> {
index ceeec9ded12d0b86433e2297a09c15849ead06e1..fcc3f300a34f0fe472df9dcc61f3d54228fcb585 100644 (file)
@@ -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,
                                                        &timestamp,
-                                                       &channel_flags,
-                                                       &(direction == 1),
+                                                       &(update.contents.flags as i32),
+                                                       &direction,
                                                        &disable,
                                                        &cltv_expiry_delta,
                                                        &htlc_minimum_msat,