Skip to content

Commit

Permalink
chore: bump jsonrpsee and ckb related crates version (#1438)
Browse files Browse the repository at this point in the history
* chore: bump ckb related crates and jsonrpsee version

* format some code

* bump ckb-vm version

* fix e2e test
  • Loading branch information
Eason Gao authored Sep 21, 2023
1 parent 8e5d567 commit f7457d2
Show file tree
Hide file tree
Showing 26 changed files with 543 additions and 389 deletions.
270 changes: 151 additions & 119 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion common/apm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ syn = { version = "1.0", features = ["full"] }
[dev-dependencies]
async-trait = "0.1"
common-apm = { path = "../apm" }
jsonrpsee = { version = "0.16", features = ["macros"] }
jsonrpsee = { version = "0.20", features = ["macros"] }
log = "0.4"

protocol = { path = "../../protocol", package = "axon-protocol" }
6 changes: 1 addition & 5 deletions common/config-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
pub mod types;
use serde::de;

use std::error;
use std::fmt;
use std::fs;
use std::io;
use std::path::Path;
use std::{error, fmt, fs, io, path::Path};

/// Parse a config from reader.
pub fn parse_reader<R: io::Read, T: de::DeserializeOwned>(r: &mut R) -> Result<T, ParseError> {
Expand Down
6 changes: 2 additions & 4 deletions common/config-parser/src/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use std::{

use clap::{
builder::{StringValueParser, TypedValueParser, ValueParserFactory},
Args,
Args, ValueEnum,
};
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;

use common_crypto::Secp256k1RecoverablePrivateKey;
use protocol::{
Expand Down Expand Up @@ -275,9 +276,6 @@ impl Genesis {
}
}

use clap::ValueEnum;
use strum_macros::EnumIter;

#[derive(Clone, Debug, Deserialize, Args)]
pub struct HardforkInput {
#[arg(
Expand Down
8 changes: 4 additions & 4 deletions core/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ edition = "2021"
[dependencies]
# async-graphql = { version = "3.0", features = ["tracing"] }
beef = "0.5"
ckb-jsonrpc-types = "0.108"
ckb-traits = "0.108"
ckb-types = "0.108"
jsonrpsee = { version = "0.16", features = ["macros", "server"] }
ckb-jsonrpc-types = "0.111"
ckb-traits = "0.111"
ckb-types = "0.111"
jsonrpsee = { version = "0.20", features = ["macros", "server"] }
log = "0.4"
parking_lot = "0.12"
pprof = { version = "0.11", features = ["prost-codec"], optional = true }
Expand Down
153 changes: 132 additions & 21 deletions core/api/src/jsonrpc/error.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,152 @@
use jsonrpsee::core::Error;
use jsonrpsee::types::error::{CallError, ErrorObject};
use jsonrpsee::types::{error::ErrorObject, ErrorObjectOwned};

use protocol::codec::hex_encode;
use protocol::types::{ExitReason, TxResp};
use protocol::{codec::hex_encode, Display};

use core_executor::decode_revert_msg;

const EXEC_ERROR: i32 = -32015;
use crate::jsonrpc::web3_types::BlockId;

#[derive(Clone, Debug)]
#[derive(Clone, Display, Debug)]
pub enum RpcError {
#[display(fmt = "Decode interoperation signature r error")]
DecodeInteroperationSigR(String),
#[display(fmt = "Decode interoperation signature s error")]
DecodeInteroperationSigS(String),
#[display(fmt = "Invalid address source")]
InvalidAddressSource,
#[display(fmt = "Missing dummy input cell")]
MissingDummyInputCell,
#[display(fmt = "Cannot find image cell")]
CannotFindImageCell,
#[display(fmt = "Gas price is zero")]
GasPriceIsZero,
#[display(fmt = "Gas price is too large")]
GasPriceIsTooLarge,
#[display(fmt = "Gas limit is less than 21000")]
GasLimitIsTooLow,
#[display(fmt = "Gas limit is too large")]
GasLimitIsTooLarge,
#[display(fmt = "Gas limit is zero")]
GasLimitIsZero,
#[display(fmt = "Transaction is not signed")]
TransactionIsNotSigned,
#[display(fmt = "Cannot get receipt by hash")]
CannotGetReceiptByHash,
#[display(fmt = "Cannot get latest block")]
CannotGetLatestBlock,
#[display(fmt = "Invalid block hash")]
InvalidBlockHash,
#[display(fmt = "Invalid from block number {}", _0)]
InvalidFromBlockNumber(u64),
#[display(fmt = "Invalid block range from {} to {} limit to {}", _0, _1, _2)]
InvalidBlockRange(u64, u64, u64),
#[display(fmt = "Invalid newest block {:?}", _0)]
InvalidNewestBlock(BlockId),
#[display(fmt = "Invalid position {}", _0)]
InvalidPosition(u64),
#[display(fmt = "Cannot find the block")]
CannotFindBlock,
#[display(fmt = "Invalid reward percentiles {} {}", _0, _1)]
InvalidRewardPercentiles(f64, f64),
#[display(fmt = "Invalid from block number and to block number union")]
InvalidFromBlockAndToBlockUnion,
#[display(fmt = "Invalid filter id {}", _0)]
CannotFindFilterId(u64),

#[display(fmt = "CKB-VM error {}", "decode_revert_msg(&_0.ret)")]
VM(TxResp),
#[display(fmt = "Internal error")]
Internal(String),
}

impl From<RpcError> for Error {
impl From<RpcError> for String {
fn from(err: RpcError) -> Self {
match err {
RpcError::VM(resp) => vm_err(resp),
err.to_string()
}
}

impl RpcError {
fn code(&self) -> i32 {
match self {
RpcError::DecodeInteroperationSigR(_) => -40001,
RpcError::DecodeInteroperationSigS(_) => -40002,
RpcError::InvalidAddressSource => -40003,
RpcError::MissingDummyInputCell => -40004,
RpcError::CannotFindImageCell => -40005,
RpcError::GasPriceIsZero => -40006,
RpcError::GasPriceIsTooLarge => -40007,
RpcError::GasLimitIsTooLow => -40008,
RpcError::GasLimitIsTooLarge => -40009,
RpcError::GasLimitIsZero => -40010,
RpcError::TransactionIsNotSigned => -40011,
RpcError::CannotGetReceiptByHash => -40012,
RpcError::CannotGetLatestBlock => -40013,
RpcError::InvalidBlockHash => -40014,
RpcError::InvalidFromBlockNumber(_) => -40015,
RpcError::InvalidBlockRange(_, _, _) => -40016,
RpcError::InvalidNewestBlock(_) => -40017,
RpcError::InvalidPosition(_) => -40018,
RpcError::CannotFindBlock => -40019,
RpcError::InvalidRewardPercentiles(_, _) => -40020,
RpcError::InvalidFromBlockAndToBlockUnion => -40021,
RpcError::CannotFindFilterId(_) => -40022,

RpcError::VM(_) => -49998,
RpcError::Internal(_) => -49999,
}
}
}

pub fn vm_err(resp: TxResp) -> Error {
let data = match resp.exit_reason {
impl From<RpcError> for ErrorObjectOwned {
fn from(err: RpcError) -> Self {
let none_data: Option<String> = None;
let err_code = err.code();
match &err {
RpcError::DecodeInteroperationSigR(msg) => {
ErrorObject::owned(err_code, err.clone(), Some(msg))
}
RpcError::DecodeInteroperationSigS(msg) => {
ErrorObject::owned(err_code, err.clone(), Some(msg))
}
RpcError::InvalidAddressSource => ErrorObject::owned(err_code, err, none_data),
RpcError::MissingDummyInputCell => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotFindImageCell => ErrorObject::owned(err_code, err, none_data),
RpcError::GasPriceIsZero => ErrorObject::owned(err_code, err, none_data),
RpcError::GasPriceIsTooLarge => ErrorObject::owned(err_code, err, none_data),
RpcError::GasLimitIsTooLow => ErrorObject::owned(err_code, err, none_data),
RpcError::GasLimitIsTooLarge => ErrorObject::owned(err_code, err, none_data),
RpcError::GasLimitIsZero => ErrorObject::owned(err_code, err, none_data),
RpcError::TransactionIsNotSigned => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotGetReceiptByHash => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotGetLatestBlock => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidBlockHash => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidFromBlockNumber(_) => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidBlockRange(_, _, _) => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidNewestBlock(_) => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidPosition(_) => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotFindBlock => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidRewardPercentiles(_, _) => {
ErrorObject::owned(err_code, err, none_data)
}
RpcError::InvalidFromBlockAndToBlockUnion => {
ErrorObject::owned(err_code, err, none_data)
}
RpcError::CannotFindFilterId(_) => ErrorObject::owned(err_code, err, none_data),

RpcError::VM(resp) => {
ErrorObject::owned(err_code, err.clone(), Some(vm_err(resp.clone())))
}
RpcError::Internal(msg) => ErrorObject::owned(err_code, err.clone(), Some(msg)),
}
}
}

pub fn vm_err(resp: TxResp) -> String {
match resp.exit_reason {
ExitReason::Revert(_) => format!("0x{}", hex_encode(&resp.ret),),
ExitReason::Error(err) => format!("{:?}", err),
ExitReason::Fatal(fatal) => format!("{:?}", fatal),
_ => unreachable!(),
};

into_rpc_err(ErrorObject::owned(
EXEC_ERROR,
decode_revert_msg(&resp.ret),
Some(data),
))
}

fn into_rpc_err(obj: ErrorObject<'static>) -> Error {
CallError::Custom(obj).into()
}
}
28 changes: 14 additions & 14 deletions core/api/src/jsonrpc/impl/axon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc};

use jsonrpsee::core::Error;
use jsonrpsee::{core::RpcResult, types::error::ErrorCode};
use strum::IntoEnumIterator;

use common_config_parser::types::spec::HardforkName;
Expand All @@ -11,7 +11,7 @@ use protocol::types::{
};

use crate::jsonrpc::web3_types::{BlockId, HardforkStatus};
use crate::jsonrpc::{AxonRpcServer, RpcResult};
use crate::jsonrpc::{error::RpcError, AxonRpcServer};

pub struct AxonRpcImpl<Adapter> {
adapter: Arc<Adapter>,
Expand All @@ -34,9 +34,9 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.await
}
BlockId::Latest => self.adapter.get_block_by_number(Context::new(), None).await,
_ => return Err(Error::InvalidRequestId),
_ => return Err(ErrorCode::InvalidRequest.into()),
}
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

Ok(ret)
}
Expand All @@ -54,7 +54,7 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.adapter
.get_metadata_by_number(Context::new(), Some(block_number.as_u64()))
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

Ok(ret)
}
Expand All @@ -65,14 +65,14 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.adapter
.get_block_by_number(Context::new(), Some(block_number - 1))
.await
.map_err(|e| Error::Custom(e.to_string()))?
.ok_or_else(|| Error::Custom("prev block not found".to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?
.ok_or_else(|| RpcError::Internal("prev block not found".to_string()))?;
let block = self
.adapter
.get_block_by_number(Context::new(), Some(block_number))
.await
.map_err(|e| Error::Custom(e.to_string()))?
.ok_or_else(|| Error::Custom("block not found".to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?
.ok_or_else(|| RpcError::Internal("block not found".to_string()))?;

Ok(Proposal::new_with_state_root(
&block.header,
Expand All @@ -86,7 +86,7 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.adapter
.get_metadata_by_number(Context::new(), None)
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

Ok(ret)
}
Expand All @@ -96,7 +96,7 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.adapter
.get_ckb_related_info(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

Ok(ret)
}
Expand All @@ -106,19 +106,19 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {
.adapter
.hardfork_info(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

let proposal = self
.adapter
.hardfork_proposal(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

let current_number = self
.adapter
.get_block_header_by_number(Default::default(), None)
.await
.map_err(|e| Error::Custom(e.to_string()))?
.map_err(|e| RpcError::Internal(e.to_string()))?
.unwrap()
.number;

Expand Down
8 changes: 4 additions & 4 deletions core/api/src/jsonrpc/impl/ckb_light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use ckb_jsonrpc_types::{CellData, CellInfo, HeaderView as CkbHeaderView, JsonByt
use ckb_traits::HeaderProvider;
use ckb_types::core::cell::{CellProvider, CellStatus};
use ckb_types::{packed, prelude::Pack};
use jsonrpsee::core::RpcResult;

use core_executor::DataProvider;
use jsonrpsee::core::Error;
use protocol::traits::{APIAdapter, Context};
use protocol::{async_trait, ckb_blake2b_256, types::H256};

use crate::jsonrpc::{CkbLightClientRpcServer, RpcResult};
use crate::jsonrpc::{error::RpcError, CkbLightClientRpcServer};

#[derive(Clone, Debug)]
pub struct CkbLightClientRpcImpl<Adapter: APIAdapter> {
Expand All @@ -24,7 +24,7 @@ impl<Adapter: APIAdapter + 'static> CkbLightClientRpcServer for CkbLightClientRp
.adapter
.get_image_cell_root(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;
Ok(DataProvider::new(root)
.get_header(&(hash.0.pack()))
.map(Into::into))
Expand All @@ -40,7 +40,7 @@ impl<Adapter: APIAdapter + 'static> CkbLightClientRpcServer for CkbLightClientRp
.adapter
.get_image_cell_root(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
.map_err(|e| RpcError::Internal(e.to_string()))?;

match DataProvider::new(root).cell(&out_point, false) {
CellStatus::Live(c) => {
Expand Down
Loading

0 comments on commit f7457d2

Please sign in to comment.