-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] add chain head polling background job
- Loading branch information
1 parent
d866334
commit 29fb9bf
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters