message: feat: Return submit_block error
diff: +use crate::error::RPCError;
  use ckb_chain::chain::ChainController;
  use ckb_jsonrpc_types::{Block, BlockTemplate, Uint64, Version};
  use ckb_logger::{debug, error};
  @@ -10,6 +11,7 @@ use faketime::unix_time_as_millis;
  use jsonrpc_core::{Error, Result};
  use jsonrpc_derive::rpc;
  use std::collections::HashSet;
  +use std::fmt::Debug;
  use std::sync::Arc;
  #[rpc]
  @@ -79,22 +81,25 @@ impl MinerRpc for MinerRpcImpl {
  let header = block.header();
  let snapshot: &Snapshot = &self.shared.snapshot();
  let resolver = HeaderResolverWrapper::new(&header, snapshot, self.shared.consensus());
  - let header_verify_ret = {
  - let header_verifier =
  - HeaderVerifier::new(snapshot, Arc::clone(&self.shared.consensus().pow_engine()));
  - header_verifier.verify(&resolver)
  - };
  - if header_verify_ret.is_ok() {
  - let ret = self.chain.process_block(Arc::clone(&block), true);
  - if ret.is_ok() {
  +
  + // Verify header
  + HeaderVerifier::new(snapshot, Arc::clone(&self.shared.consensus().pow_engine()))
  + .verify(&resolver)
  + .map_err(|err| handle_submit_error(&work_id, &err))?;
  +
  + // Verify and insert block
  + let _is_new = self
  + .chain
  + .process_block(Arc::clone(&block), true)
  + .map_err(|err| handle_submit_error(&work_id, &err))?;
  +
  + // Announce new block
  debug!(
  "[block_relay] announce new block {} {} {}",
  block.header().number(),
  block.header().hash(),
  unix_time_as_millis()
  );
  - // announce new block
  -
  let content = packed::CompactBlock::build_from_block(&block, &HashSet::new());
  let message = packed::RelayMessage::new_builder().set(content).build();
  let data = message.as_slice().into();
  @@ -105,26 +110,19 @@ impl MinerRpc for MinerRpcImpl {
  error!("Broadcast block failed: {:?}", err);
  }
  Ok(Some(block.header().hash().unpack()))
  - } else {
  - error!("[{}] submit_block process_block {:?}", work_id, ret);
  + }
  +}
  +
  +fn handle_submit_error<E: Debug + ToString>(work_id: &str, err: &E) -> Error {
  + error!("[{}] submit_block error: {:?}", work_id, err);
  + capture_submit_error(err);
  + RPCError::custom(RPCError::Invalid, err.to_string())
  +}
  +
  +fn capture_submit_error<D: Debug>(err: &D) {
  use sentry::{capture_message, with_scope, Level};
  with_scope(
  |scope| scope.set_fingerprint(Some(&["ckb-rpc", "miner", "submit_block"])),
  - || {
  - capture_message(
  - &format!("submit_block process_block {:?}", ret),
  - Level::Error,
  - )
  - },
  + || capture_message(&format!("submit_block {:?}", err), Level::Error),
  );
  - Ok(None)
  - }
  - } else {
  - error!(
  - "[{}] submit_block header verifier {:?}",
  - work_id, header_verify_ret
  - );
  - Ok(None)
  - }
  - }
  }
  