Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revise epoch rpc #1088

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,10 @@ http://localhost:8114
"id": 2,
"jsonrpc": "2.0",
"result": {
"block_reward": "100000000000",
"difficulty": "0x3e8",
"last_block_hash_in_previous_epoch": "0x0000000000000000000000000000000000000000000000000000000000000000",
"epoch_reward": "125000000000000",
"length": "1250",
"number": "0",
"remainder_reward": "0",
"start_number": "0"
}
}
Expand Down Expand Up @@ -386,12 +384,10 @@ http://localhost:8114
"id": 2,
"jsonrpc": "2.0",
"result": {
"block_reward": "100000000000",
"difficulty": "0x3e8",
"last_block_hash_in_previous_epoch": "0x0000000000000000000000000000000000000000000000000000000000000000",
"epoch_reward": "125000000000000",
"length": "1250",
"number": "0",
"remainder_reward": "0",
"start_number": "0"
}
}
Expand Down
8 changes: 2 additions & 6 deletions rpc/json/rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
"module": "chain",
"params": [],
"result": {
"block_reward": "100000000000",
"difficulty": "0x3e8",
"last_block_hash_in_previous_epoch": "0x0000000000000000000000000000000000000000000000000000000000000000",
"epoch_reward": "125000000000000",
"length": "1250",
"number": "0",
"remainder_reward": "0",
"start_number": "0"
}
},
Expand All @@ -53,12 +51,10 @@
"0"
],
"result": {
"block_reward": "100000000000",
"difficulty": "0x3e8",
"last_block_hash_in_previous_epoch": "0x0000000000000000000000000000000000000000000000000000000000000000",
"epoch_reward": "125000000000000",
"length": "1250",
"number": "0",
"remainder_reward": "0",
"start_number": "0"
},
"types": [
Expand Down
19 changes: 12 additions & 7 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use jsonrpc_types::{
BlockNumber, BlockView, Capacity, CellOutPoint, CellOutputWithOutPoint, CellWithStatus,
EpochExt, EpochNumber, HeaderView, OutPoint, TransactionWithStatus, Unsigned,
EpochNumber, EpochView, HeaderView, OutPoint, TransactionWithStatus, Unsigned,
};
use numext_fixed_hash::H256;

Expand Down Expand Up @@ -46,10 +46,10 @@ pub trait ChainRpc {
fn get_tip_block_number(&self) -> Result<BlockNumber>;

#[rpc(name = "get_current_epoch")]
fn get_current_epoch(&self) -> Result<EpochExt>;
fn get_current_epoch(&self) -> Result<EpochView>;

#[rpc(name = "get_epoch_by_number")]
fn get_epoch_by_number(&self, number: EpochNumber) -> Result<Option<EpochExt>>;
fn get_epoch_by_number(&self, number: EpochNumber) -> Result<Option<EpochView>>;
}

pub(crate) struct ChainRpcImpl<CS> {
Expand Down Expand Up @@ -119,21 +119,26 @@ impl<CS: ChainStore + 'static> ChainRpc for ChainRpcImpl<CS> {
.expect("tip header exists"))
}

fn get_current_epoch(&self) -> Result<EpochExt> {
fn get_current_epoch(&self) -> Result<EpochView> {
Ok(self
.shared
.store()
.get_current_epoch_ext()
.map(Into::into)
.map(|ext| EpochView::from_ext(self.shared.consensus().epoch_reward(), &ext))
.expect("current_epoch exists"))
}

fn get_epoch_by_number(&self, number: EpochNumber) -> Result<Option<EpochExt>> {
fn get_epoch_by_number(&self, number: EpochNumber) -> Result<Option<EpochView>> {
Ok(self
.shared
.store()
.get_epoch_index(number.0)
.and_then(|hash| self.shared.store().get_epoch_ext(&hash).map(Into::into)))
.and_then(|hash| {
self.shared
.store()
.get_epoch_ext(&hash)
.map(|ext| EpochView::from_ext(self.shared.consensus().epoch_reward(), &ext))
}))
}

// TODO: we need to build a proper index instead of scanning every time
Expand Down
10 changes: 5 additions & 5 deletions test/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jsonrpc_client_core::{expand_params, jsonrpc_client, Result as JsonRpcResult
use jsonrpc_client_http::{HttpHandle, HttpTransport};
use jsonrpc_types::{
Alert, Block, BlockNumber, BlockTemplate, BlockView, CellOutputWithOutPoint, CellTransaction,
CellWithStatus, ChainInfo, DryRunResult, EpochExt, EpochNumber, HeaderView, LiveCell,
CellWithStatus, ChainInfo, DryRunResult, EpochNumber, EpochView, HeaderView, LiveCell,
LockHashIndexState, Node, OutPoint, PeerState, Transaction, TransactionWithStatus, TxPoolInfo,
Unsigned, Version,
};
Expand Down Expand Up @@ -101,15 +101,15 @@ impl RpcClient {
.0
}

pub fn get_current_epoch(&self) -> EpochExt {
pub fn get_current_epoch(&self) -> EpochView {
self.inner
.lock()
.get_current_epoch()
.call()
.expect("rpc call get_current_epoch")
}

pub fn get_epoch_by_number(&self, number: CoreEpochNumber) -> Option<EpochExt> {
pub fn get_epoch_by_number(&self, number: CoreEpochNumber) -> Option<EpochView> {
self.inner
.lock()
.get_epoch_by_number(EpochNumber(number))
Expand Down Expand Up @@ -298,8 +298,8 @@ jsonrpc_client!(pub struct Inner {
) -> RpcRequest<Vec<CellOutputWithOutPoint>>;
pub fn get_live_cell(&mut self, _out_point: OutPoint) -> RpcRequest<CellWithStatus>;
pub fn get_tip_block_number(&mut self) -> RpcRequest<BlockNumber>;
pub fn get_current_epoch(&mut self) -> RpcRequest<EpochExt>;
pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest<Option<EpochExt>>;
pub fn get_current_epoch(&mut self) -> RpcRequest<EpochView>;
pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest<Option<EpochView>>;
pub fn local_node_info(&mut self) -> RpcRequest<Node>;
pub fn get_peers(&mut self) -> RpcRequest<Vec<Node>>;
pub fn get_block_template(
Expand Down
58 changes: 11 additions & 47 deletions util/jsonrpc-types/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ckb_core::transaction::{
Witness as CoreWitness,
};
use ckb_core::uncle::UncleBlock as CoreUncleBlock;
use ckb_core::Capacity as CoreCapacity;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -552,63 +553,26 @@ impl From<BlockView> for CoreBlock {
}

#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct EpochExt {
pub struct EpochView {
pub number: EpochNumber,
pub block_reward: Capacity,
pub last_block_hash_in_previous_epoch: H256,
pub epoch_reward: Capacity,
pub start_number: BlockNumber,
pub length: BlockNumber,
pub difficulty: U256,
pub remainder_reward: Capacity,
}

impl From<CoreEpochExt> for EpochExt {
fn from(core: CoreEpochExt) -> EpochExt {
let (
number,
block_reward,
remainder_reward,
last_block_hash_in_previous_epoch,
start_number,
length,
difficulty,
) = core.destruct();

EpochExt {
number: EpochNumber(number),
block_reward: Capacity(block_reward),
remainder_reward: Capacity(remainder_reward),
last_block_hash_in_previous_epoch,
start_number: BlockNumber(start_number),
length: BlockNumber(length),
difficulty,
impl EpochView {
pub fn from_ext(epoch_reward: CoreCapacity, ext: &CoreEpochExt) -> EpochView {
EpochView {
number: EpochNumber(ext.number()),
start_number: BlockNumber(ext.start_number()),
length: BlockNumber(ext.length()),
difficulty: ext.difficulty().clone(),
epoch_reward: Capacity(epoch_reward),
}
}
}

impl From<EpochExt> for CoreEpochExt {
fn from(json: EpochExt) -> Self {
let EpochExt {
number,
block_reward,
last_block_hash_in_previous_epoch,
start_number,
length,
difficulty,
remainder_reward,
} = json;
CoreEpochExt::new(
number.0,
block_reward.0,
remainder_reward.0,
last_block_hash_in_previous_epoch,
start_number.0,
length.0,
difficulty,
)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion util/jsonrpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use self::block_template::{
BlockTemplate, CellbaseTemplate, TransactionTemplate, UncleTemplate,
};
pub use self::blockchain::{
Block, BlockView, CellInput, CellOutPoint, CellOutput, EpochExt, Header, HeaderView, OutPoint,
Block, BlockView, CellInput, CellOutPoint, CellOutput, EpochView, Header, HeaderView, OutPoint,
Script, Seal, Transaction, TransactionView, TransactionWithStatus, TxStatus, UncleBlock,
UncleBlockView, Witness,
};
Expand Down