fixed tests

This commit is contained in:
Svyatoslav Nikolsky 2016-12-10 14:38:51 +03:00
parent 416ac097b5
commit 8cd9b2dbdc
3 changed files with 24 additions and 6 deletions

View File

@ -3,6 +3,7 @@
mod codes { mod codes {
// NOTE [ToDr] Codes from [-32099, -32000] // NOTE [ToDr] Codes from [-32099, -32000]
pub const EXECUTION_ERROR: i64 = -32015; pub const EXECUTION_ERROR: i64 = -32015;
pub const BLOCK_NOT_FOUND: i64 = -32099;
} }
@ -36,3 +37,11 @@ pub fn execution<T: fmt::Debug>(data: T) -> Error {
data: Some(Value::String(format!("{:?}", data))), data: Some(Value::String(format!("{:?}", data))),
} }
} }
pub fn block_not_found<T: fmt::Debug>(data: T) -> Error {
Error {
code: ErrorCode::ServerError(codes::BLOCK_NOT_FOUND),
message: "Block not found".into(),
data: Some(Value::String(format!("{:?}", data))),
}
}

View File

@ -6,6 +6,7 @@ use v1::types::GetTxOutSetInfoResponse;
use v1::types::RawBlock; use v1::types::RawBlock;
use v1::types::H256; use v1::types::H256;
use v1::types::U256; use v1::types::U256;
use v1::helpers::errors::block_not_found;
use jsonrpc_core::Error; use jsonrpc_core::Error;
use db; use db;
use verification; use verification;
@ -13,7 +14,7 @@ use ser::serialize;
use primitives::hash::H256 as GlobalH256; use primitives::hash::H256 as GlobalH256;
pub struct BlockChainClient<T: BlockChainClientCoreApi> { pub struct BlockChainClient<T: BlockChainClientCoreApi> {
_core: T, core: T,
} }
pub trait BlockChainClientCoreApi: Send + Sync + 'static { pub trait BlockChainClientCoreApi: Send + Sync + 'static {
@ -79,7 +80,7 @@ impl BlockChainClientCoreApi for BlockChainClientCore {
impl<T> BlockChainClient<T> where T: BlockChainClientCoreApi { impl<T> BlockChainClient<T> where T: BlockChainClientCoreApi {
pub fn new(core: T) -> Self { pub fn new(core: T) -> Self {
BlockChainClient { BlockChainClient {
_core: core, core: core,
} }
} }
} }
@ -97,8 +98,16 @@ impl<T> BlockChain for BlockChainClient<T> where T: BlockChainClientCoreApi {
rpc_unimplemented!() rpc_unimplemented!()
} }
fn block(&self, _hash: H256, _verbose: Option<bool>) -> Result<GetBlockResponse, Error> { fn block(&self, hash: H256, verbose: Option<bool>) -> Result<GetBlockResponse, Error> {
rpc_unimplemented!() let global_hash: GlobalH256 = hash.clone().into();
if verbose.unwrap_or_default() {
self.core.get_verbose_block(global_hash.reversed())
.map(|block| GetBlockResponse::Verbose(block))
} else {
self.core.get_raw_block(global_hash.reversed())
.map(|block| GetBlockResponse::Raw(block))
}
.ok_or(block_not_found(hash))
} }
fn transaction(&self, _hash: H256, _watch_only: Option<bool>) -> Result<GetTransactionResponse, Error> { fn transaction(&self, _hash: H256, _watch_only: Option<bool>) -> Result<GetTransactionResponse, Error> {

View File

@ -75,7 +75,7 @@ mod tests {
#[test] #[test]
fn verbose_block_serialize() { fn verbose_block_serialize() {
let block = VerboseBlock::default(); let block = VerboseBlock::default();
assert_eq!(serde_json::to_string(&block).unwrap(), r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":null,"version":0,"versionHex":"","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":null,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"0","previousblockhash":null,"nextblockhash":null}"#); assert_eq!(serde_json::to_string(&block).unwrap(), r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":null,"version":0,"versionHex":"","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":null,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"","previousblockhash":null,"nextblockhash":null}"#);
let block = VerboseBlock { let block = VerboseBlock {
hash: H256::from(1), hash: H256::from(1),
@ -142,6 +142,6 @@ mod tests {
fn get_block_response_verbose_serialize() { fn get_block_response_verbose_serialize() {
let block = VerboseBlock::default(); let block = VerboseBlock::default();
let verbose_response = GetBlockResponse::Verbose(block); let verbose_response = GetBlockResponse::Verbose(block);
assert_eq!(serde_json::to_string(&verbose_response).unwrap(), r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":null,"version":0,"versionHex":"","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":null,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"0","previousblockhash":null,"nextblockhash":null}"#); assert_eq!(serde_json::to_string(&verbose_response).unwrap(), r#"{"hash":"0000000000000000000000000000000000000000000000000000000000000000","confirmations":0,"size":0,"strippedsize":0,"weight":0,"height":null,"version":0,"versionHex":"","merkleroot":"0000000000000000000000000000000000000000000000000000000000000000","tx":[],"time":0,"mediantime":null,"nonce":0,"bits":0,"difficulty":0.0,"chainwork":"","previousblockhash":null,"nextblockhash":null}"#);
} }
} }