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 4 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.

27 changes: 23 additions & 4 deletions miner/src/block_assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,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 @@ -505,10 +506,28 @@ mod tests {
.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
47 changes: 35 additions & 12 deletions miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::Work;
use ckb_core::block::{Block, BlockBuilder};
use ckb_core::header::{HeaderBuilder, RawHeader, Seal};
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 +35,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 Down Expand Up @@ -71,24 +78,40 @@ impl Miner {
.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" }
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -100,7 +101,7 @@ 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())
}

Expand Down
3 changes: 2 additions & 1 deletion rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 Down Expand Up @@ -52,7 +53,7 @@ impl<CI: ChainIndex + 'static> MinerRpc for MinerRpcImpl<CI> {
}

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
1 change: 1 addition & 0 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ckb-network = { path = "../network" }
ckb-shared = { path = "../shared" }
ckb-sync = { path = "../sync" }
ckb-protocol = { path = "../protocol"}
ckb-util = { path = "../util" }
numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
fs_extra = "1.1"
tempfile = "3.0"
Expand Down
37 changes: 31 additions & 6 deletions test/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ckb_core::block::{Block, BlockBuilder};
use ckb_core::header::{HeaderBuilder, Seal};
use ckb_core::script::Script;
use ckb_core::transaction::{CellInput, CellOutput, OutPoint, Transaction, TransactionBuilder};
use ckb_util::TryInto;
use fs_extra::dir::{copy, CopyOptions};
use jsonrpc_client_http::{HttpHandle, HttpTransport};
use jsonrpc_types::{BlockTemplate, CellbaseTemplate};
Expand Down Expand Up @@ -126,7 +127,10 @@ impl Node {
.call()
.expect("rpc call get_block failed")
.expect("get_block result none");
let cellbase: Transaction = block.commit_transactions[0].clone().into();
let cellbase: Transaction = block.commit_transactions[0]
.clone()
.try_into()
.expect("parse cellbase transaction failed");
rpc.send_transaction((&self.new_transaction(cellbase.hash())).into())
.call()
.expect("rpc call send_transaction failed")
Expand All @@ -148,7 +152,10 @@ impl Node {
.call()
.expect("rpc call get_block failed")
.expect("get_block result none");
let cellbase: Transaction = block.commit_transactions[0].clone().into();
let cellbase: Transaction = block.commit_transactions[0]
.clone()
.try_into()
.expect("parse cellbase transaction failed");
rpc.trace_transaction((&self.new_transaction(cellbase.hash())).into())
.call()
.expect("rpc call send_transaction failed")
Expand Down Expand Up @@ -188,10 +195,28 @@ impl Node {
.seal(Seal::new(rand::random(), Vec::new()));

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<_, _>>()
.expect("parse uncles failed"),
)
.commit_transaction(cellbase.try_into().expect("parse cellbase failed"))
.commit_transactions(
commit_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.expect("parse commit transactions failed"),
)
.proposal_transactions(
proposal_transactions
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()
.expect("parse proposal transactions failed"),
)
.with_header_builder(header_builder)
}

Expand Down
7 changes: 5 additions & 2 deletions test/src/specs/mining.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Net, Spec};
use ckb_core::block::Block;
use ckb_core::transaction::ProposalShortId;
use ckb_util::TryInto;
use log::info;

pub struct MiningBasic;
Expand All @@ -25,14 +26,16 @@ impl Spec for MiningBasic {
.call()
.unwrap()
.unwrap()
.into();
.try_into()
.unwrap();
let block3: Block = node
.rpc_client()
.get_block(block3_hash)
.call()
.unwrap()
.unwrap()
.into();
.try_into()
.unwrap();

info!("Generated tx should be included in next block's proposal txs");
assert!(block1
Expand Down
2 changes: 2 additions & 0 deletions util/jsonrpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ serde_derive = "1.0"
serde_json = "1.0"
faster-hex = "0.3"
jsonrpc-core = "10.1"
failure = "0.1.5"
ckb-util = { path = ".." }

[dev-dependencies]
proptest = "0.9"
Loading