Skip to content

Commit a172a89

Browse files
committed
feat(phase5): improve error handling with graceful lock recovery
1 parent 474d3f1 commit a172a89

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

crates/bitcell-node/src/blockchain.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,26 @@ impl Blockchain {
8282

8383
/// Get current chain height
8484
pub fn height(&self) -> u64 {
85-
*self.height.read().unwrap()
85+
*self.height.read().unwrap_or_else(|e| {
86+
eprintln!("Lock poisoned in height(): {}", e);
87+
e.into_inner()
88+
})
8689
}
8790

8891
/// Get latest block hash
8992
pub fn latest_hash(&self) -> Hash256 {
90-
*self.latest_hash.read().unwrap()
93+
*self.latest_hash.read().unwrap_or_else(|e| {
94+
eprintln!("Lock poisoned in latest_hash(): {}", e);
95+
e.into_inner()
96+
})
9197
}
9298

9399
/// Get block by height
94100
pub fn get_block(&self, height: u64) -> Option<Block> {
95-
self.blocks.read().unwrap().get(&height).cloned()
101+
self.blocks.read().unwrap_or_else(|e| {
102+
eprintln!("Lock poisoned in get_block(): {}", e);
103+
e.into_inner()
104+
}).get(&height).cloned()
96105
}
97106

98107
/// Get state manager (read-only access)

crates/bitcell-node/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub enum Error {
3636

3737
#[error("Network error: {0}")]
3838
Network(String),
39+
40+
#[error("Lock error: {0}")]
41+
Lock(String),
3942
}
4043

4144
impl From<String> for Error {

crates/bitcell-node/src/rpc.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,11 @@ async fn eth_get_balance(state: &RpcState, params: Option<Value>) -> Result<Valu
357357
// Fetch balance from blockchain state
358358
let balance = {
359359
let state_lock = state.blockchain.state();
360-
let state = state_lock.read().unwrap();
360+
let state = state_lock.read().map_err(|_| JsonRpcError {
361+
code: -32603,
362+
message: "Failed to acquire state lock".to_string(),
363+
data: None,
364+
})?;
361365
state.get_account(&address)
362366
.map(|account| account.balance)
363367
.unwrap_or(0)
@@ -422,7 +426,11 @@ async fn eth_send_raw_transaction(state: &RpcState, params: Option<Value>) -> Re
422426
// Validate nonce and balance
423427
{
424428
let state_lock = state.blockchain.state();
425-
let state_guard = state_lock.read().unwrap();
429+
let state_guard = state_lock.read().map_err(|_| JsonRpcError {
430+
code: -32603,
431+
message: "Failed to acquire state lock".to_string(),
432+
data: None,
433+
})?;
426434

427435
if let Some(account) = state_guard.get_account(tx.from.as_bytes()) {
428436
if tx.nonce != account.nonce {

0 commit comments

Comments
 (0)