Skip to content

Commit

Permalink
fix: safer error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Apr 4, 2023
1 parent 741ac84 commit 11509e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion components/chainhook-cli/src/scan/bitcoin.rs
Expand Up @@ -173,7 +173,7 @@ pub async fn scan_bitcoin_chain_with_predicate(
&ctx,
);

update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
let _ = update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
&mut block,
&mut storage,
&ctx,
Expand Down
9 changes: 5 additions & 4 deletions components/chainhook-event-observer/src/hord/db/mod.rs
Expand Up @@ -406,13 +406,14 @@ pub struct WatchedSatpoint {
pub fn find_inscriptions_at_wached_outpoint(
outpoint: &str,
hord_db_conn: &Connection,
) -> Vec<WatchedSatpoint> {
) -> Result<Vec<WatchedSatpoint>, String> {
let args: &[&dyn ToSql] = &[&outpoint.to_sql().unwrap()];
let mut stmt = hord_db_conn
.prepare("SELECT inscription_id, inscription_number, ordinal_number, offset FROM inscriptions WHERE outpoint_to_watch = ? ORDER BY offset ASC")
.unwrap();
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
let mut results = vec![];
let mut rows = stmt.query(args).unwrap();
let mut rows = stmt.query(args)
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
while let Ok(Some(row)) = rows.next() {
let inscription_id: String = row.get(0).unwrap();
let inscription_number: u64 = row.get(1).unwrap();
Expand All @@ -425,7 +426,7 @@ pub fn find_inscriptions_at_wached_outpoint(
offset,
});
}
return results;
return Ok(results);
}

pub fn insert_entry_in_blocks(
Expand Down
7 changes: 4 additions & 3 deletions components/chainhook-event-observer/src/hord/mod.rs
Expand Up @@ -153,7 +153,7 @@ pub fn update_hord_db_and_augment_bitcoin_block(
new_block,
&mut Storage::Sqlite(rw_hord_db_conn),
&ctx,
);
)?;
Ok(())
}

Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
block: &mut BitcoinBlockData,
storage: &mut Storage,
ctx: &Context,
) {
) -> Result<(), String> {
let mut cumulated_fees = 0;
let first_sat_post_subsidy = Height(block.block_identifier.index).starting_sat().0;
let coinbase_txid = &block.transactions[0].transaction_identifier.hash.clone();
Expand All @@ -291,7 +291,7 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(

let entries = match storage {
Storage::Sqlite(rw_hord_db_conn) => {
find_inscriptions_at_wached_outpoint(&outpoint_pre_transfer, &rw_hord_db_conn)
find_inscriptions_at_wached_outpoint(&outpoint_pre_transfer, &rw_hord_db_conn)?
}
Storage::Memory(ref mut map) => match map.remove(&outpoint_pre_transfer) {
Some(entries) => entries,
Expand Down Expand Up @@ -424,4 +424,5 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(
}
cumulated_fees += new_tx.metadata.fee;
}
Ok(())
}

0 comments on commit 11509e4

Please sign in to comment.