Skip to content

Commit f820169

Browse files
author
Ludo Galabru
authored
fix: better handling of database locks (#200)
1 parent 977a30e commit f820169

File tree

17 files changed

+726
-990
lines changed

17 files changed

+726
-990
lines changed

.github/workflows/ordhook-sdk-js.yml

-518
This file was deleted.

components/ordhook-cli/src/cli/mod.rs

+177-73
Large diffs are not rendered by default.

components/ordhook-cli/src/config/file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl ConfigFile {
4141
let config_file: ConfigFile = match toml::from_slice(&file_buffer) {
4242
Ok(s) => s,
4343
Err(e) => {
44-
return Err(format!("Config file malformatted {}", e.to_string()));
44+
return Err(format!("Config file malformatted {}", e));
4545
}
4646
};
4747
ConfigFile::from_config_file(config_file)
@@ -153,7 +153,7 @@ impl ConfigFile {
153153
(true, false, false, _) => Config::devnet_default(),
154154
(false, true, false, _) => Config::testnet_default(),
155155
(false, false, true, _) => Config::mainnet_default(),
156-
(false, false, false, Some(config_path)) => ConfigFile::from_file_path(&config_path)?,
156+
(false, false, false, Some(config_path)) => ConfigFile::from_file_path(config_path)?,
157157
_ => Err("Invalid combination of arguments".to_string())?,
158158
};
159159
Ok(config)

components/ordhook-cli/src/config/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ chainhook_internals = true
4545
"#,
4646
network = network.to_lowercase(),
4747
);
48-
return conf;
48+
conf
4949
}

components/ordhook-core/src/core/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use chainhook_sdk::{
1414

1515
use crate::{
1616
config::{Config, LogConfig},
17-
db::{find_lazy_block_at_block_height, open_readwrite_ordhook_db_conn_rocks_db},
17+
db::{find_lazy_block_at_block_height, open_ordhook_db_conn_rocks_db_loop},
1818
};
1919

2020
use crate::db::{
@@ -95,7 +95,7 @@ pub fn compute_next_satpoint_data(
9595
}
9696

9797
pub fn should_sync_rocks_db(config: &Config, ctx: &Context) -> Result<Option<(u64, u64)>, String> {
98-
let blocks_db = open_readwrite_ordhook_db_conn_rocks_db(&config.expected_cache_path(), &ctx)?;
98+
let blocks_db = open_ordhook_db_conn_rocks_db_loop(true, &config.expected_cache_path(), &ctx);
9999
let inscriptions_db_conn = open_readonly_ordhook_db_conn(&config.expected_cache_path(), &ctx)?;
100100
let last_compressed_block = find_last_block_inserted(&blocks_db) as u64;
101101
let last_indexed_block = match find_latest_inscription_block_height(&inscriptions_db_conn, ctx)?
@@ -128,7 +128,7 @@ pub fn should_sync_ordhook_db(
128128
}
129129
};
130130

131-
let blocks_db = open_readwrite_ordhook_db_conn_rocks_db(&config.expected_cache_path(), &ctx)?;
131+
let blocks_db = open_ordhook_db_conn_rocks_db_loop(true, &config.expected_cache_path(), &ctx);
132132
let mut start_block = find_last_block_inserted(&blocks_db) as u64;
133133

134134
if start_block == 0 {

components/ordhook-core/src/core/pipeline/processors/block_archiving.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
use crate::{
1010
config::Config,
1111
core::pipeline::{PostProcessorCommand, PostProcessorController, PostProcessorEvent},
12-
db::{insert_entry_in_blocks, open_readwrite_ordhook_db_conn_rocks_db, LazyBlock},
12+
db::{insert_entry_in_blocks, LazyBlock, open_ordhook_db_conn_rocks_db_loop},
1313
};
1414

1515
pub fn start_block_archiving_processor(
@@ -26,18 +26,15 @@ pub fn start_block_archiving_processor(
2626
let handle: JoinHandle<()> = hiro_system_kit::thread_named("Processor Runloop")
2727
.spawn(move || {
2828
let blocks_db_rw =
29-
open_readwrite_ordhook_db_conn_rocks_db(&config.expected_cache_path(), &ctx)
30-
.unwrap();
29+
open_ordhook_db_conn_rocks_db_loop(true, &config.expected_cache_path(), &ctx);
3130
let mut processed_blocks = 0;
3231

3332
loop {
34-
debug!(ctx.expect_logger(), "Tick");
3533
let (compacted_blocks, _) = match commands_rx.try_recv() {
3634
Ok(PostProcessorCommand::ProcessBlocks(compacted_blocks, blocks)) => {
3735
(compacted_blocks, blocks)
3836
}
3937
Ok(PostProcessorCommand::Terminate) => {
40-
debug!(ctx.expect_logger(), "Terminating block processor");
4138
let _ = events_tx.send(PostProcessorEvent::Terminated);
4239
break;
4340
}

components/ordhook-core/src/core/pipeline/processors/inscription_indexing.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ pub fn start_inscription_indexing_processor(
8383
(compacted_blocks, blocks)
8484
}
8585
Ok(PostProcessorCommand::Terminate) => {
86-
debug!(ctx.expect_logger(), "Terminating block processor");
8786
let _ = events_tx.send(PostProcessorEvent::Terminated);
8887
break;
8988
}
9089
Err(e) => match e {
9190
TryRecvError::Empty => {
9291
empty_cycles += 1;
9392
if empty_cycles == 180 {
94-
warn!(ctx.expect_logger(), "Block processor reached expiration");
93+
ctx.try_log(|logger| {
94+
info!(logger, "Block processor reached expiration")
95+
});
9596
let _ = events_tx.send(PostProcessorEvent::Expired);
9697
break;
9798
}
@@ -117,7 +118,7 @@ pub fn start_inscription_indexing_processor(
117118
);
118119
}
119120

120-
info!(ctx.expect_logger(), "Processing {} blocks", blocks.len());
121+
ctx.try_log(|logger| info!(logger, "Processing {} blocks", blocks.len()));
121122

122123
blocks = process_blocks(
123124
&mut blocks,
@@ -132,19 +133,19 @@ pub fn start_inscription_indexing_processor(
132133
garbage_collect_nth_block += blocks.len();
133134

134135
if garbage_collect_nth_block > garbage_collect_every_n_blocks {
136+
ctx.try_log(|logger| info!(logger, "Performing garbage collecting"));
137+
135138
// Clear L2 cache on a regular basis
136-
info!(
137-
ctx.expect_logger(),
138-
"Clearing cache L2 ({} entries)",
139-
cache_l2.len()
140-
);
139+
ctx.try_log(|logger| {
140+
info!(logger, "Clearing cache L2 ({} entries)", cache_l2.len())
141+
});
141142
cache_l2.clear();
142143

143144
// Recreate sqlite db connection on a regular basis
144145
inscriptions_db_conn_rw =
145146
open_readwrite_ordhook_db_conn(&config.expected_cache_path(), &ctx)
146147
.unwrap();
147-
148+
inscriptions_db_conn_rw.flush_prepared_statement_cache();
148149
garbage_collect_nth_block = 0;
149150
}
150151
}
@@ -215,7 +216,7 @@ pub fn process_blocks(
215216

216217
if any_existing_activity {
217218
ctx.try_log(|logger| {
218-
warn!(
219+
error!(
219220
logger,
220221
"Dropping updates for block #{}, activities present in database",
221222
block.block_identifier.index,

components/ordhook-core/src/core/pipeline/processors/transfers_recomputing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ pub fn start_transfers_recomputing_processor(
4444
blocks
4545
}
4646
Ok(PostProcessorCommand::Terminate) => {
47-
debug!(ctx.expect_logger(), "Terminating block processor");
4847
let _ = events_tx.send(PostProcessorEvent::Terminated);
4948
break;
5049
}
5150
Err(e) => match e {
5251
TryRecvError::Empty => {
5352
empty_cycles += 1;
5453
if empty_cycles == 10 {
55-
warn!(ctx.expect_logger(), "Block processor reached expiration");
54+
ctx.try_log(|logger| {
55+
warn!(logger, "Block processor reached expiration")
56+
});
5657
let _ = events_tx.send(PostProcessorEvent::Expired);
5758
break;
5859
}
@@ -65,7 +66,7 @@ pub fn start_transfers_recomputing_processor(
6566
},
6667
};
6768

68-
info!(ctx.expect_logger(), "Processing {} blocks", blocks.len());
69+
ctx.try_log(|logger| info!(logger, "Processing {} blocks", blocks.len()));
6970
let inscriptions_db_tx = inscriptions_db_conn_rw.transaction().unwrap();
7071

7172
for block in blocks.iter_mut() {

components/ordhook-core/src/core/protocol/inscription_sequencing.rs

-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use std::{
22
collections::{BTreeMap, HashMap, VecDeque},
33
hash::BuildHasherDefault,
44
sync::Arc,
5-
thread::sleep,
6-
time::Duration,
75
};
86

97
use chainhook_sdk::{
@@ -784,8 +782,6 @@ pub fn consolidate_block_with_pre_computed_ordinals_data(
784782
if results.len() == expected_inscriptions_count {
785783
break results;
786784
}
787-
// Handle race conditions: if the db is being updated, the number of expected entries could be un-met.
788-
sleep(Duration::from_secs(3));
789785
ctx.try_log(|logger| {
790786
warn!(
791787
logger,

components/ordhook-core/src/core/protocol/inscription_tracking.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use chainhook_sdk::{
22
bitcoincore_rpc_json::bitcoin::{hashes::hex::FromHex, Address, Network, Script},
33
types::{
44
BitcoinBlockData, BitcoinNetwork, BitcoinTransactionData, BlockIdentifier,
5-
OrdinalInscriptionTransferData, OrdinalOperation, TransactionIdentifier, OrdinalInscriptionTransferDestination,
5+
OrdinalInscriptionTransferData, OrdinalInscriptionTransferDestination, OrdinalOperation,
6+
TransactionIdentifier,
67
},
78
utils::Context,
89
};
@@ -84,14 +85,7 @@ pub fn augment_transaction_with_ordinals_transfers_data(
8485
);
8586

8687
let entries =
87-
match find_inscriptions_at_wached_outpoint(&outpoint_pre_transfer, &inscriptions_db_tx)
88-
{
89-
Ok(entries) => entries,
90-
Err(e) => {
91-
ctx.try_log(|logger| warn!(logger, "unable query inscriptions: {e}"));
92-
continue;
93-
}
94-
};
88+
find_inscriptions_at_wached_outpoint(&outpoint_pre_transfer, &inscriptions_db_tx, ctx);
9589
// For each satpoint inscribed retrieved, we need to compute the next
9690
// outpoint to watch
9791
for watched_satpoint in entries.into_iter() {
@@ -124,10 +118,12 @@ pub fn augment_transaction_with_ordinals_transfers_data(
124118
tx.metadata.outputs[output_index].get_script_pubkey_hex();
125119
let updated_address = match Script::from_hex(&script_pub_key_hex) {
126120
Ok(script) => match Address::from_script(&script, network.clone()) {
127-
Ok(address) => OrdinalInscriptionTransferDestination::Transferred(address.to_string()),
121+
Ok(address) => OrdinalInscriptionTransferDestination::Transferred(
122+
address.to_string(),
123+
),
128124
Err(e) => {
129125
ctx.try_log(|logger| {
130-
warn!(
126+
info!(
131127
logger,
132128
"unable to retrieve address from {script_pub_key_hex}: {}",
133129
e.to_string()
@@ -138,13 +134,15 @@ pub fn augment_transaction_with_ordinals_transfers_data(
138134
},
139135
Err(e) => {
140136
ctx.try_log(|logger| {
141-
warn!(
137+
info!(
142138
logger,
143139
"unable to retrieve address from {script_pub_key_hex}: {}",
144140
e.to_string()
145141
)
146142
});
147-
OrdinalInscriptionTransferDestination::Burnt(script_pub_key_hex.to_string())
143+
OrdinalInscriptionTransferDestination::Burnt(
144+
script_pub_key_hex.to_string(),
145+
)
148146
}
149147
};
150148

@@ -181,7 +179,12 @@ pub fn augment_transaction_with_ordinals_transfers_data(
181179
offset
182180
)
183181
});
184-
(outpoint, total_offset, OrdinalInscriptionTransferDestination::SpentInFees, None)
182+
(
183+
outpoint,
184+
total_offset,
185+
OrdinalInscriptionTransferDestination::SpentInFees,
186+
None,
187+
)
185188
}
186189
};
187190

0 commit comments

Comments
 (0)