Skip to content

Commit

Permalink
fix: limit scope of stacks_rw_db usage to free lock (#543)
Browse files Browse the repository at this point in the history
Every 32 stacks blocks, Chainhook attempts to check for a new archive
file to fix any potential holes in the database. This process requires
readwrite access to the stacks.rocksdb. However, the process that called
this subprocess already had a readwrite instance of the database open,
which was preventing the lock from being cleared.

This PR sets the scope of the initial usage of the db in order to free
up the lock.
  • Loading branch information
MicaiahReid committed Apr 2, 2024
1 parent 123ec9d commit 0c287ca
Showing 1 changed file with 49 additions and 51 deletions.
100 changes: 49 additions & 51 deletions components/chainhook-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,58 @@ impl Service {
}
}
ObserverEvent::StacksChainEvent((chain_event, report)) => {
let stacks_db_conn_rw = match open_readwrite_stacks_db_conn(
match open_readwrite_stacks_db_conn(
&self.config.expected_cache_path(),
&self.ctx,
) {
Ok(db_conn) => db_conn,
Ok(stacks_db_conn_rw) => match &chain_event {
StacksChainEvent::ChainUpdatedWithBlocks(data) => {
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable to add confirmed entries to stacks db: {}", e
);
};
if let Err(e) = draft_entries_in_stacks_blocks(
&data.new_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable to add unconfirmed entries to stacks db: {}", e
);
};
}
StacksChainEvent::ChainUpdatedWithReorg(data) => {
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable to add confirmed entries to stacks db: {}", e
);
};
if let Err(e) = draft_entries_in_stacks_blocks(
&data.blocks_to_apply,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable to add unconfirmed entries to stacks db: {}", e
);
};
}
StacksChainEvent::ChainUpdatedWithMicroblocks(_)
| StacksChainEvent::ChainUpdatedWithMicroblocksReorg(_) => {}
},
Err(e) => {
error!(
self.ctx.expect_logger(),
Expand All @@ -478,55 +525,6 @@ impl Service {
}
};

match &chain_event {
StacksChainEvent::ChainUpdatedWithBlocks(data) => {
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable add confirmed entries to stacks db: {}", e
);
};
if let Err(e) = draft_entries_in_stacks_blocks(
&data.new_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable add unconfirmed entries to stacks db: {}", e
);
};
}
StacksChainEvent::ChainUpdatedWithReorg(data) => {
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable add confirmed entries to stacks db: {}", e
);
};
if let Err(e) = draft_entries_in_stacks_blocks(
&data.blocks_to_apply,
&stacks_db_conn_rw,
&self.ctx,
) {
error!(
self.ctx.expect_logger(),
"unable add unconfirmed entries to stacks db: {}", e
);
};
}
StacksChainEvent::ChainUpdatedWithMicroblocks(_)
| StacksChainEvent::ChainUpdatedWithMicroblocksReorg(_) => {}
};

if let PredicatesApi::On(ref config) = self.config.http_api {
let Ok(mut predicates_db_conn) =
open_readwrite_predicates_db_conn_verbose(&config, &ctx)
Expand Down

0 comments on commit 0c287ca

Please sign in to comment.