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: Change all u64 fields in RPC to String #434

Merged
merged 5 commits into from
Apr 10, 2019
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 31 additions & 13 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ckb_core::script::Script;
use ckb_core::service::{Request, DEFAULT_CHANNEL_SIZE, SIGNAL_CHANNEL_SIZE};
use ckb_core::transaction::{CellInput, CellOutput, Transaction, TransactionBuilder};
use ckb_core::uncle::UncleBlock;
use ckb_core::BlockNumber;
use ckb_core::{Cycle, Version};
use ckb_notify::NotifyController;
use ckb_shared::{index::ChainIndex, shared::Shared, tx_pool::PoolEntry};
Expand Down Expand Up @@ -45,7 +44,7 @@ impl TemplateCache {
last_uncles_updated_at: u64,
last_txs_updated_at: u64,
current_time: u64,
number: BlockNumber,
number: String,
) -> bool {
last_uncles_updated_at != self.uncles_updated_at
|| last_txs_updated_at != self.txs_updated_at
Expand Down Expand Up @@ -245,7 +244,7 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
last_uncles_updated_at,
last_txs_updated_at,
current_time,
number,
number.to_string(),
) {
return Ok(template_cache.template.clone());
}
Expand Down Expand Up @@ -274,11 +273,11 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
let template = BlockTemplate {
version,
difficulty,
current_time,
number,
current_time: current_time.to_string(),
number: number.to_string(),
parent_hash: header.hash(),
cycles_limit,
bytes_limit,
cycles_limit: cycles_limit.to_string(),
bytes_limit: bytes_limit.to_string(),
uncles_count_limit,
uncles: uncles.into_iter().map(Self::transform_uncle).collect(),
commit_transactions: commit_transactions
Expand Down Expand Up @@ -428,6 +427,7 @@ mod tests {
use ckb_shared::shared::SharedBuilder;
use ckb_shared::store::ChainKVStore;
use ckb_traits::ChainProvider;
use ckb_util::TryInto;
use ckb_verification::{BlockVerifier, HeaderResolverWrapper, HeaderVerifier, Verifier};
use jsonrpc_types::{BlockTemplate, CellbaseTemplate};
use numext_fixed_hash::H256;
Expand Down Expand Up @@ -499,16 +499,34 @@ mod tests {

let header_builder = HeaderBuilder::default()
.version(version)
.number(number)
.number(number.parse::<BlockNumber>().unwrap())
.difficulty(difficulty)
.timestamp(current_time)
.timestamp(current_time.parse::<u64>().unwrap())
.parent_hash(parent_hash);

let block = BlockBuilder::default()
.uncles(uncles.into_iter().map(Into::into).collect())
.commit_transaction(cellbase.into())
.commit_transactions(commit_transactions.into_iter().map(Into::into).collect())
.proposal_transactions(proposal_transactions.into_iter().map(Into::into).collect())
.uncles(
uncles
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.unwrap(),
)
.commit_transaction(cellbase.try_into().unwrap())
.commit_transactions(
commit_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.unwrap(),
)
.proposal_transactions(
proposal_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.unwrap(),
)
.with_header_builder(header_builder);

let resolver = HeaderResolverWrapper::new(block.header(), shared.clone());
Expand Down
52 changes: 38 additions & 14 deletions miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use crate::client::Client;
use crate::Work;
use ckb_core::block::{Block, BlockBuilder};
use ckb_core::header::{HeaderBuilder, RawHeader, Seal};
use ckb_core::BlockNumber;
use ckb_pow::PowEngine;
use ckb_util::TryInto;
use crossbeam_channel::Receiver;
use failure::Error;
use jsonrpc_types::{BlockTemplate, CellbaseTemplate};
use log::{debug, info};
use log::{debug, error, info};
use rand::{thread_rng, Rng};
use std::sync::Arc;

Expand Down Expand Up @@ -33,13 +36,18 @@ impl Miner {
pub fn run(&self) {
loop {
self.client.try_update_block_template();
if let Some((work_id, block)) = self.mine() {
self.client.submit_block(&work_id, &block);
}
match self.mine() {
Ok(result) => {
if let Some((work_id, block)) = result {
self.client.submit_block(&work_id, &block);
}
}
Err(e) => error!(target: "miner", "mining error encountered: {:?}", e),
};
}
}

fn mine(&self) -> Option<(String, Block)> {
fn mine(&self) -> Result<Option<(String, Block)>, Error> {
if let Some(template) = { self.current_work.lock().clone() } {
let BlockTemplate {
version,
Expand All @@ -65,30 +73,46 @@ impl Miner {

let header_builder = HeaderBuilder::default()
.version(version)
.number(number)
.number(number.parse::<BlockNumber>()?)
.difficulty(difficulty)
.timestamp(current_time)
.timestamp(current_time.parse::<u64>()?)
.parent_hash(parent_hash);

let block = BlockBuilder::default()
.uncles(uncles.into_iter().map(Into::into).collect())
.commit_transaction(cellbase.into())
.commit_transactions(commit_transactions.into_iter().map(Into::into).collect())
.proposal_transactions(proposal_transactions.into_iter().map(Into::into).collect())
.uncles(
uncles
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
)
.commit_transaction(cellbase.try_into()?)
.commit_transactions(
commit_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
)
.proposal_transactions(
proposal_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
)
.with_header_builder(header_builder);

let raw_header = block.header().raw().clone();

self.mine_loop(&raw_header)
Ok(self
.mine_loop(&raw_header)
.map(|seal| {
BlockBuilder::default()
.block(block)
.header(raw_header.with_seal(seal))
.build()
})
.map(|block| (work_id, block))
.map(|block| (work_id, block)))
} else {
None
Ok(None)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ build-info = { path = "../util/build-info" }
futures = "0.1"
ckb-verification = { path = "../verification" }
ckb-traits = { path = "../traits" }

ckb-util = { path = "../util" }

[dev-dependencies]
ckb-db = { path = "../db" }
33 changes: 22 additions & 11 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ckb_core::cell::CellProvider;
use ckb_core::BlockNumber;
use ckb_shared::{index::ChainIndex, shared::Shared};
use ckb_traits::ChainProvider;
use ckb_util::TryInto;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use jsonrpc_types::{Block, CellOutputWithOutPoint, CellWithStatus, Header, OutPoint, Transaction};
Expand All @@ -16,7 +17,7 @@ pub trait ChainRpc {
fn get_transaction(&self, _hash: H256) -> Result<Option<Transaction>>;

#[rpc(name = "get_block_hash")]
fn get_block_hash(&self, _number: u64) -> Result<Option<H256>>;
fn get_block_hash(&self, _number: String) -> Result<Option<H256>>;

#[rpc(name = "get_tip_header")]
fn get_tip_header(&self) -> Result<Header>;
Expand All @@ -25,15 +26,15 @@ pub trait ChainRpc {
fn get_cells_by_lock_hash(
&self,
_lock_hash: H256,
_from: BlockNumber,
_to: BlockNumber,
_from: String,
_to: String,
) -> Result<Vec<CellOutputWithOutPoint>>;

#[rpc(name = "get_live_cell")]
fn get_live_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;

#[rpc(name = "get_tip_block_number")]
fn get_tip_block_number(&self) -> Result<BlockNumber>;
fn get_tip_block_number(&self) -> Result<String>;
}

pub(crate) struct ChainRpcImpl<CI> {
Expand All @@ -49,8 +50,12 @@ impl<CI: ChainIndex + 'static> ChainRpc for ChainRpcImpl<CI> {
Ok(self.shared.get_transaction(&hash).as_ref().map(Into::into))
}

fn get_block_hash(&self, number: BlockNumber) -> Result<Option<H256>> {
Ok(self.shared.block_hash(number))
fn get_block_hash(&self, number: String) -> Result<Option<H256>> {
Ok(self.shared.block_hash(
number
.parse::<BlockNumber>()
.map_err(|_| Error::parse_error())?,
))
}

fn get_tip_header(&self) -> Result<Header> {
Expand All @@ -61,11 +66,17 @@ impl<CI: ChainIndex + 'static> ChainRpc for ChainRpcImpl<CI> {
fn get_cells_by_lock_hash(
&self,
lock_hash: H256,
from: BlockNumber,
to: BlockNumber,
from: String,
to: String,
) -> Result<Vec<CellOutputWithOutPoint>> {
let mut result = Vec::new();
let chain_state = self.shared.chain_state().lock();
let from = from
.parse::<BlockNumber>()
.map_err(|_| Error::parse_error())?;
let to = to
.parse::<BlockNumber>()
.map_err(|_| Error::parse_error())?;
for block_number in from..=to {
if let Some(block_hash) = self.shared.block_hash(block_number) {
let block = self
Expand Down Expand Up @@ -100,11 +111,11 @@ impl<CI: ChainIndex + 'static> ChainRpc for ChainRpcImpl<CI> {
.shared
.chain_state()
.lock()
.cell(&(out_point.into()))
.cell(&(out_point.try_into().map_err(|_| Error::parse_error())?))
.into())
}

fn get_tip_block_number(&self) -> Result<BlockNumber> {
Ok(self.shared.chain_state().lock().tip_number())
fn get_tip_block_number(&self) -> Result<String> {
Ok(self.shared.chain_state().lock().tip_number().to_string())
}
}
20 changes: 15 additions & 5 deletions rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use ckb_chain::chain::ChainController;
use ckb_core::block::Block as CoreBlock;
use ckb_core::Cycle;
use ckb_miner::BlockAssemblerController;
use ckb_network::{NetworkController, ProtocolId};
use ckb_protocol::RelayMessage;
use ckb_shared::{index::ChainIndex, shared::Shared};
use ckb_sync::NetworkProtocol;
use ckb_traits::ChainProvider;
use ckb_util::TryInto;
use ckb_verification::{HeaderResolverWrapper, HeaderVerifier, Verifier};
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::{Error, Result};
Expand All @@ -22,8 +24,8 @@ pub trait MinerRpc {
#[rpc(name = "get_block_template")]
fn get_block_template(
&self,
cycles_limit: Option<u64>,
bytes_limit: Option<u64>,
cycles_limit: Option<String>,
bytes_limit: Option<String>,
max_version: Option<u32>,
) -> Result<BlockTemplate>;

Expand All @@ -42,17 +44,25 @@ pub(crate) struct MinerRpcImpl<CI> {
impl<CI: ChainIndex + 'static> MinerRpc for MinerRpcImpl<CI> {
fn get_block_template(
&self,
cycles_limit: Option<u64>,
bytes_limit: Option<u64>,
cycles_limit: Option<String>,
bytes_limit: Option<String>,
max_version: Option<u32>,
) -> Result<BlockTemplate> {
let cycles_limit = match cycles_limit {
Some(c) => Some(c.parse::<Cycle>().map_err(|_| Error::parse_error())?),
None => None,
};
let bytes_limit = match bytes_limit {
Some(b) => Some(b.parse::<u64>().map_err(|_| Error::parse_error())?),
None => None,
};
self.block_assembler
.get_block_template(cycles_limit, bytes_limit, max_version)
.map_err(|_| Error::internal_error())
}

fn submit_block(&self, _work_id: String, data: Block) -> Result<Option<H256>> {
let block: Arc<CoreBlock> = Arc::new(data.into());
let block: Arc<CoreBlock> = Arc::new(data.try_into().map_err(|_| Error::parse_error())?);
let resolver = HeaderResolverWrapper::new(block.header(), self.shared.clone());
let header_verifier = HeaderVerifier::new(
self.shared.clone(),
Expand Down
5 changes: 3 additions & 2 deletions rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use ckb_shared::shared::Shared;
use ckb_shared::tx_pool::types::PoolEntry;
use ckb_sync::NetworkProtocol;
use ckb_traits::chain_provider::ChainProvider;
use ckb_util::TryInto;
use ckb_verification::TransactionError;
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::Result;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use jsonrpc_types::Transaction;
use log::debug;
Expand All @@ -33,7 +34,7 @@ pub(crate) struct PoolRpcImpl<CI> {

impl<CI: ChainIndex + 'static> PoolRpc for PoolRpcImpl<CI> {
fn send_transaction(&self, tx: Transaction) -> Result<H256> {
let tx: CoreTransaction = tx.into();
let tx: CoreTransaction = tx.try_into().map_err(|_| Error::parse_error())?;
let tx_hash = tx.hash().clone();
let cycles = {
let mut chain_state = self.shared.chain_state().lock();
Expand Down
5 changes: 3 additions & 2 deletions rpc/src/module/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use ckb_shared::tx_pool::types::PoolEntry;
use ckb_shared::tx_pool::TxTrace;
use ckb_sync::NetworkProtocol;
use ckb_traits::chain_provider::ChainProvider;
use ckb_util::TryInto;
use ckb_verification::TransactionError;
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::Result;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use jsonrpc_types::Transaction;
use log::debug;
Expand All @@ -32,7 +33,7 @@ pub(crate) struct TraceRpcImpl<CI> {

impl<CI: ChainIndex + 'static> TraceRpc for TraceRpcImpl<CI> {
fn trace_transaction(&self, tx: Transaction) -> Result<H256> {
let tx: CoreTransaction = tx.into();
let tx: CoreTransaction = tx.try_into().map_err(|_| Error::parse_error())?;
let tx_hash = tx.hash().clone();
let cycles = {
let mut chain_state = self.shared.chain_state().lock();
Expand Down
Loading