Skip to content

Commit

Permalink
fix: get genesis block proposal may panic (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Gao authored Nov 15, 2023
1 parent d000f87 commit 5072b57
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions core/api/src/jsonrpc/impl/axon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
}

async fn get_proposal_by_number(&self, block_number: U256) -> RpcResult<Proposal> {
let block_number = block_number.as_u64();
let prev_block = self
.adapter
.get_block_by_number(Context::new(), Some(block_number - 1))
.await
.map_err(|e| RpcError::Internal(e.to_string()))?
.ok_or_else(|| RpcError::Internal("prev block not found".to_string()))?;
let block_number = block_number.low_u64();

let prev_state_root = if block_number == 0 {
H256::default()
} else {
self.adapter
.get_block_by_number(Context::new(), Some(block_number - 1))
.await
.map_err(|e| RpcError::Internal(e.to_string()))?
.ok_or_else(|| RpcError::Internal("prev block not found".to_string()))?
.header
.state_root
};

let block = self
.adapter
.get_block_by_number(Context::new(), Some(block_number))
Expand All @@ -76,7 +83,7 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {

Ok(Proposal::new_with_state_root(
&block.header,
prev_block.header.state_root,
prev_state_root,
block.tx_hashes,
))
}
Expand Down

0 comments on commit 5072b57

Please sign in to comment.