diff --git a/rpc/README.md b/rpc/README.md index 02f41e7f7e..1aa3969c3e 100644 --- a/rpc/README.md +++ b/rpc/README.md @@ -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" } } @@ -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" } } diff --git a/rpc/json/rpc.json b/rpc/json/rpc.json index f32124ffa8..c658f4825f 100644 --- a/rpc/json/rpc.json +++ b/rpc/json/rpc.json @@ -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" } }, @@ -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": [ diff --git a/rpc/src/module/chain.rs b/rpc/src/module/chain.rs index 2d3ed9a4af..b205f5c508 100644 --- a/rpc/src/module/chain.rs +++ b/rpc/src/module/chain.rs @@ -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; @@ -46,10 +46,10 @@ pub trait ChainRpc { fn get_tip_block_number(&self) -> Result; #[rpc(name = "get_current_epoch")] - fn get_current_epoch(&self) -> Result; + fn get_current_epoch(&self) -> Result; #[rpc(name = "get_epoch_by_number")] - fn get_epoch_by_number(&self, number: EpochNumber) -> Result>; + fn get_epoch_by_number(&self, number: EpochNumber) -> Result>; } pub(crate) struct ChainRpcImpl { @@ -119,21 +119,26 @@ impl ChainRpc for ChainRpcImpl { .expect("tip header exists")) } - fn get_current_epoch(&self) -> Result { + fn get_current_epoch(&self) -> Result { 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> { + fn get_epoch_by_number(&self, number: EpochNumber) -> Result> { 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 diff --git a/test/src/rpc.rs b/test/src/rpc.rs index eb779a85f6..968e09bc74 100644 --- a/test/src/rpc.rs +++ b/test/src/rpc.rs @@ -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, }; @@ -101,7 +101,7 @@ impl RpcClient { .0 } - pub fn get_current_epoch(&self) -> EpochExt { + pub fn get_current_epoch(&self) -> EpochView { self.inner .lock() .get_current_epoch() @@ -109,7 +109,7 @@ impl RpcClient { .expect("rpc call get_current_epoch") } - pub fn get_epoch_by_number(&self, number: CoreEpochNumber) -> Option { + pub fn get_epoch_by_number(&self, number: CoreEpochNumber) -> Option { self.inner .lock() .get_epoch_by_number(EpochNumber(number)) @@ -298,8 +298,8 @@ jsonrpc_client!(pub struct Inner { ) -> RpcRequest>; pub fn get_live_cell(&mut self, _out_point: OutPoint) -> RpcRequest; pub fn get_tip_block_number(&mut self) -> RpcRequest; - pub fn get_current_epoch(&mut self) -> RpcRequest; - pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest>; + pub fn get_current_epoch(&mut self) -> RpcRequest; + pub fn get_epoch_by_number(&mut self, number: EpochNumber) -> RpcRequest>; pub fn local_node_info(&mut self) -> RpcRequest; pub fn get_peers(&mut self) -> RpcRequest>; pub fn get_block_template( diff --git a/util/jsonrpc-types/src/blockchain.rs b/util/jsonrpc-types/src/blockchain.rs index 994a6e9c13..730acb64c7 100644 --- a/util/jsonrpc-types/src/blockchain.rs +++ b/util/jsonrpc-types/src/blockchain.rs @@ -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}; @@ -552,63 +553,26 @@ impl From 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 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 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::*; diff --git a/util/jsonrpc-types/src/lib.rs b/util/jsonrpc-types/src/lib.rs index 04b9ca6bd3..2d09a4cd8c 100644 --- a/util/jsonrpc-types/src/lib.rs +++ b/util/jsonrpc-types/src/lib.rs @@ -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, };