Skip to content

Commit

Permalink
remove unwrap/expect from database updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid committed Mar 15, 2024
1 parent b61ce48 commit fb0692b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
3 changes: 1 addition & 2 deletions components/chainhook-cli/src/scan/stacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ pub async fn consolidate_local_stacks_chainstate_using_csv(
}
};

// TODO: return a result
insert_entry_in_stacks_blocks(&block_data, &stacks_db_rw, ctx);
insert_entry_in_stacks_blocks(&block_data, &stacks_db_rw, ctx)?;

if blocks_inserted % 2500 == 0 {
info!(
Expand Down
36 changes: 28 additions & 8 deletions components/chainhook-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,28 +451,48 @@ impl Service {

match &chain_event {
StacksChainEvent::ChainUpdatedWithBlocks(data) => {
confirm_entries_in_stacks_blocks(
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
);
draft_entries_in_stacks_blocks(
) {
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) => {
confirm_entries_in_stacks_blocks(
if let Err(e) = confirm_entries_in_stacks_blocks(
&data.confirmed_blocks,
&stacks_db_conn_rw,
&self.ctx,
);
draft_entries_in_stacks_blocks(
) {
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(_) => {}
Expand Down
36 changes: 23 additions & 13 deletions components/chainhook-cli/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,48 +109,56 @@ fn get_last_unconfirmed_insert_key() -> [u8; 3] {
*b"m:~"
}

pub fn insert_entry_in_stacks_blocks(block: &StacksBlockData, stacks_db_rw: &DB, _ctx: &Context) {
pub fn insert_entry_in_stacks_blocks(
block: &StacksBlockData,
stacks_db_rw: &DB,
_ctx: &Context,
) -> Result<(), String> {
let key = get_block_key(&block.block_identifier);
let block_bytes = json!(block);
stacks_db_rw
.put(&key, &block_bytes.to_string().as_bytes())
.expect("unable to insert blocks");
.map_err(|e| format!("unable to insert blocks: {}", e))?;
let previous_last_inserted = get_last_block_height_inserted(stacks_db_rw, _ctx).unwrap_or(0);
if block.block_identifier.index > previous_last_inserted {
stacks_db_rw
.put(
get_last_confirmed_insert_key(),
block.block_identifier.index.to_be_bytes(),
)
.expect("unable to insert metadata");
.map_err(|e| format!("unable to insert metadata: {}", e))?;
}
Ok(())
}

pub fn insert_unconfirmed_entry_in_stacks_blocks(
block: &StacksBlockData,
stacks_db_rw: &DB,
_ctx: &Context,
) {
) -> Result<(), String> {
let key = get_unconfirmed_block_key(&block.block_identifier);
let block_bytes = json!(block);
stacks_db_rw
.put(&key, &block_bytes.to_string().as_bytes())
.expect("unable to insert blocks");
.map_err(|e| format!("unable to insert blocks: {}", e))?;
stacks_db_rw
.put(
get_last_unconfirmed_insert_key(),
block.block_identifier.index.to_be_bytes(),
)
.expect("unable to insert metadata");
.map_err(|e| format!("unable to insert metadata: {}", e))?;
Ok(())
}

pub fn delete_unconfirmed_entry_from_stacks_blocks(
block_identifier: &BlockIdentifier,
stacks_db_rw: &DB,
_ctx: &Context,
) {
) -> Result<(), String> {
let key = get_unconfirmed_block_key(&block_identifier);
stacks_db_rw.delete(&key).expect("unable to delete blocks");
stacks_db_rw
.delete(&key)
.map_err(|e| format!("unable to delete blocks: {}", e))
}

pub fn get_last_unconfirmed_block_height_inserted(stacks_db: &DB, _ctx: &Context) -> Option<u64> {
Expand Down Expand Up @@ -179,22 +187,24 @@ pub fn confirm_entries_in_stacks_blocks(
blocks: &Vec<StacksBlockData>,
stacks_db_rw: &DB,
ctx: &Context,
) {
) -> Result<(), String> {
for block in blocks.iter() {
insert_entry_in_stacks_blocks(block, stacks_db_rw, ctx);
delete_unconfirmed_entry_from_stacks_blocks(&block.block_identifier, stacks_db_rw, ctx);
insert_entry_in_stacks_blocks(block, stacks_db_rw, ctx)?;
delete_unconfirmed_entry_from_stacks_blocks(&block.block_identifier, stacks_db_rw, ctx)?;
}
Ok(())
}

pub fn draft_entries_in_stacks_blocks(
block_updates: &Vec<StacksBlockUpdate>,
stacks_db_rw: &DB,
ctx: &Context,
) {
) -> Result<(), String> {
for update in block_updates.iter() {
// TODO: Could be imperfect, from a microblock point of view
insert_unconfirmed_entry_in_stacks_blocks(&update.block, stacks_db_rw, ctx);
insert_unconfirmed_entry_in_stacks_blocks(&update.block, stacks_db_rw, ctx)?;
}
Ok(())
}

pub fn get_stacks_block_at_block_height(
Expand Down

0 comments on commit fb0692b

Please sign in to comment.