Skip to content

Commit

Permalink
[WIP] add chain head polling background job
Browse files Browse the repository at this point in the history
  • Loading branch information
doscortados committed Feb 7, 2025
1 parent d866334 commit 29fb9bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
60 changes: 60 additions & 0 deletions core/src/background.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::{ops::DerefMut, time::Duration};

use bitcoincore_rpc::RpcApi;
use tokio::{task::JoinHandle, time::sleep};

use crate::{database::Database, errors::BridgeError, extended_rpc::ExtendedRpc};

pub fn start_polling_chain_head(
db: Database,
rpc: ExtendedRpc,
poll_delay: Duration,
) -> JoinHandle<Result<(), BridgeError>> {
tokio::spawn(async move {
loop {
if let Err(e) = poll_and_save_chain_head(&db, &rpc).await {
tracing::error!("Failed to poll chain head: {e}");
}
sleep(poll_delay).await;
}
})
}

async fn poll_and_save_chain_head(db: &Database, rpc: &ExtendedRpc) -> Result<(), BridgeError> {
let info = rpc.client.get_blockchain_info().await?;
let (block_hash, height) = (info.best_block_hash.to_string(), info.blocks);

let mut tx = db.begin_transaction().await?;
db.set_tx_sender_chain_head(&mut tx, block_hash, height)
.await?;
tx.commit().await?;
Ok(())
}

impl Database {
pub async fn set_tx_sender_chain_head(
&self,
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
block_hash: String,
height: u64,
) -> Result<(String, u64), BridgeError> {
sqlx::query("DELETE FROM tx_sender_block_info")
.execute(tx.deref_mut())
.await?;
sqlx::query("INSERT INTO tx_sender_block_info (block_hash, height) VALUES (?, ?)")
.bind(&block_hash)
.bind(height as i64)
.execute(tx.deref_mut())
.await?;
Ok((block_hash, height))
}

pub async fn get_tx_sender_chain_head(&self) -> Result<Option<(String, u64)>, BridgeError> {
let mut tx = self.begin_transaction().await?;
let ret: Option<(String, i64)> =
sqlx::query_as("SELECT (block_hash, height) FROM tx_sender_block_info LIMIT 1")
.fetch_optional(tx.deref_mut())
.await?;
Ok(ret.map(|(block_hash, height)| (block_hash, height as u64)))
}
}
3 changes: 2 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};

pub mod actor;
pub mod aggregator;
pub mod background;
pub mod builder;
pub mod cli;
pub mod config;
Expand All @@ -22,10 +23,10 @@ pub mod musig2;
pub mod operator;
pub mod rpc;
pub mod servers;
pub mod tx_sender;
pub mod utils;
pub mod verifier;
pub mod watchtower;
pub mod tx_sender;

#[cfg(test)]
mod test_utils;
Expand Down
5 changes: 5 additions & 0 deletions scripts/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,8 @@ create table if not exists operators_challenge_ack_hashes (
);

COMMIT;

create table if not exists tx_sender_block_info (
block_hash text not null primary key,
height int not null,
);

0 comments on commit 29fb9bf

Please sign in to comment.