Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better relay handling #34

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions nomen/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ impl Config {
Ok((keys, client))
}

pub async fn nostr_keys_client(
&self,
keys: &nostr_sdk::Keys,
) -> anyhow::Result<nostr_sdk::Client> {
let client = nostr_sdk::Client::with_opts(keys, Options::new().wait_for_send(true));
let relays = self.relays();
for relay in relays {
client.add_relay(relay, None).await?;
}
client.connect().await;
Ok(client)
}

pub async fn nostr_random_client(
&self,
) -> anyhow::Result<(nostr_sdk::Keys, nostr_sdk::Client)> {
Expand Down
3 changes: 3 additions & 0 deletions nomen/src/db/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,8 @@ pub async fn reindex(
sqlx::query("DELETE FROM name_events;")
.execute(conn)
.await?;
sqlx::query("DELETE FROM relay_index_queue;")
.execute(conn)
.await?;
Ok(())
}
3 changes: 2 additions & 1 deletion nomen/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod raw;
pub mod relay_index;
pub mod stats;

static MIGRATIONS: [&str; 17] = [
static MIGRATIONS: [&str; 18] = [
"CREATE TABLE event_log (id INTEGER PRIMARY KEY, created_at, type, data);",
"CREATE TABLE index_height (blockheight INTEGER PRIMARY KEY, blockhash);",
"CREATE TABLE raw_blockchain (id INTEGER PRIMARY KEY, blockhash, txid, blocktime, blockheight, txheight, vout, data, indexed_at);",
Expand Down Expand Up @@ -46,6 +46,7 @@ static MIGRATIONS: [&str; 17] = [
"CREATE TABLE relay_index_queue (name);",
"ALTER TABLE blockchain_index ADD COLUMN v1_upgrade_blockheight;",
"ALTER TABLE blockchain_index ADD COLUMN v1_upgrade_txid",
"CREATE UNIQUE INDEX riq_name_idx ON relay_index_queue (name)",
];

pub async fn initialize(config: &Config) -> anyhow::Result<SqlitePool> {
Expand Down
10 changes: 7 additions & 3 deletions nomen/src/db/relay_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub async fn queue(
conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy,
name: &str,
) -> anyhow::Result<()> {
sqlx::query("INSERT INTO relay_index_queue (name) VALUES (?)")
sqlx::query("INSERT OR IGNORE INTO relay_index_queue (name) VALUES (?)")
.bind(name)
.execute(conn)
.await?;
Expand All @@ -31,8 +31,12 @@ pub async fn fetch_all(
Ok(results)
}

pub async fn clear(conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy) -> anyhow::Result<()> {
sqlx::query("DELETE FROM relay_index_queue;")
pub async fn delete(
conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy,
name: &str,
) -> anyhow::Result<()> {
sqlx::query("DELETE FROM relay_index_queue WHERE name = ?;")
.bind(name)
.execute(conn)
.await?;
Ok(())
Expand Down
24 changes: 17 additions & 7 deletions nomen/src/subcommands/index/events/relay_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ pub async fn publish(config: &Config, pool: &SqlitePool) -> anyhow::Result<()> {
.expect("Missing config validation for secret")
.into();
let keys = Keys::new(sk);
let client = config.nostr_keys_client(&keys).await?;
let (_, client) = config.nostr_random_client().await?;

tracing::info!("Publishing relay index.");
let names = db::relay_index::fetch_all(pool).await?;
send_events(pool, names, keys, &client).await?;
tracing::info!("Publishing relay index complete.");

send_event(names, keys, &client).await?;

db::relay_index::clear(pool).await?;
client.disconnect().await.ok();
Ok(())
}

async fn send_event(
async fn send_events(
conn: &SqlitePool,
names: Vec<Name>,
keys: Keys,
client: &nostr_sdk::Client,
Expand All @@ -50,8 +51,17 @@ async fn send_event(
)
.to_event(&keys)?;

if client.send_event(event).await.is_err() {
tracing::error!("Unable to broadcast event during relay index publish");
match client.send_event(event.clone()).await {
Ok(s) => {
tracing::info!("Broadcast event id {s}");
db::relay_index::delete(conn, &name.name).await?;
}
Err(e) => {
tracing::error!(
"Unable to broadcast event {} during relay index publish: {e}",
event.id
);
}
}
}
Ok(())
Expand Down