Drop redundant block_height column on announcements, use i16 for flags
authorMatt Corallo <git@bluematt.me>
Tue, 13 Sep 2022 15:33:41 +0000 (15:33 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 13 Sep 2022 16:17:40 +0000 (16:17 +0000)
src/config.rs
src/persistence.rs

index a1289263ce155978246e469c743e924d36006aad..cd7a09d265d2b5b331c8ee8146950d914ba0a233 100644 (file)
@@ -11,7 +11,7 @@ use crate::hex_utils;
 
 use futures::stream::{FuturesUnordered, StreamExt};
 
-pub(crate) const SCHEMA_VERSION: i32 = 5;
+pub(crate) const SCHEMA_VERSION: i32 = 6;
 pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
 pub(crate) const DOWNLOAD_NEW_GOSSIP: bool = true;
 
@@ -54,7 +54,6 @@ pub(crate) fn db_announcement_table_creation_query() -> &'static str {
        "CREATE TABLE IF NOT EXISTS channel_announcements (
                id SERIAL PRIMARY KEY,
                short_channel_id bigint NOT NULL UNIQUE,
-               block_height integer,
                announcement_signed BYTEA,
                seen timestamp NOT NULL DEFAULT NOW()
        )"
@@ -67,7 +66,7 @@ pub(crate) fn db_channel_update_table_creation_query() -> &'static str {
                composite_index character(29) UNIQUE,
                short_channel_id bigint NOT NULL,
                timestamp bigint,
-               channel_flags integer,
+               channel_flags smallint,
                direction boolean NOT NULL,
                disable boolean,
                cltv_expiry_delta integer,
@@ -167,6 +166,13 @@ pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client)
                tx.execute("UPDATE config SET db_schema = 5 WHERE id = 1", &[]).await.unwrap();
                tx.commit().await.unwrap();
        }
+       if schema >= 1 && schema <= 5 {
+               let tx = client.transaction().await.unwrap();
+               tx.execute("ALTER TABLE channel_updates ALTER channel_flags SET DATA TYPE smallint", &[]).await.unwrap();
+               tx.execute("ALTER TABLE channel_announcements DROP COLUMN block_height", &[]).await.unwrap();
+               tx.execute("UPDATE config SET db_schema = 6 WHERE id = 1", &[]).await.unwrap();
+               tx.commit().await.unwrap();
+       }
        if schema <= 1 || schema > SCHEMA_VERSION {
                panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION);
        }
index fcc3f300a34f0fe472df9dcc61f3d54228fcb585..404792d201504ef3140a9449d4d45c575ada2c96 100644 (file)
@@ -110,10 +110,6 @@ impl GossipPersister {
                        match &gossip_message {
                                GossipMessage::ChannelAnnouncement(announcement) => {
                                        let scid = announcement.contents.short_channel_id as i64;
-                                       // scid is 8 bytes
-                                       // block height is the first three bytes
-                                       // to obtain block height, shift scid right by 5 bytes (40 bits)
-                                       let block_height = (scid >> 5 * 8) as i32;
 
                                        // start with the type prefix, which is already known a priori
                                        let mut announcement_signed = Vec::new();
@@ -122,11 +118,9 @@ impl GossipPersister {
                                        let result = client
                                                .execute("INSERT INTO channel_announcements (\
                                                        short_channel_id, \
-                                                       block_height, \
                                                        announcement_signed \
-                                               ) VALUES ($1, $2, $3) ON CONFLICT (short_channel_id) DO NOTHING", &[
+                                               ) VALUES ($1, $2) ON CONFLICT (short_channel_id) DO NOTHING", &[
                                                        &scid,
-                                                       &block_height,
                                                        &announcement_signed
                                                ]).await;
                                        if result.is_err() {
@@ -172,7 +166,7 @@ impl GossipPersister {
                                                        &composite_index,
                                                        &scid,
                                                        &timestamp,
-                                                       &(update.contents.flags as i32),
+                                                       &(update.contents.flags as i16),
                                                        &direction,
                                                        &disable,
                                                        &cltv_expiry_delta,