Skip to content

Commit

Permalink
Introduced chunk API
Browse files Browse the repository at this point in the history
  • Loading branch information
frol committed Oct 22, 2019
1 parent 75ff270 commit 9e66b10
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions chain/client/src/types.rs
Expand Up @@ -255,6 +255,7 @@ impl Message for GetBlock {

/// Actor message requesting a chunk by chunk hash and block hash + shard id.
pub enum GetChunk {
BlockHeight(BlockIndex, ShardId),
BlockHash(CryptoHash, ShardId),
ChunkHash(ChunkHash),
}
Expand Down
7 changes: 7 additions & 0 deletions chain/client/src/view_client.rs
Expand Up @@ -174,6 +174,13 @@ impl Handler<GetChunk> for ViewClientActor {
.get_chunk(&block.chunks[shard_id as usize].chunk_hash())
.map(Clone::clone)
})
},
GetChunk::BlockHeight(block_height, shard_id) => {
self.chain.get_block_by_height(block_height).map(Clone::clone).and_then(|block| {
self.chain
.get_chunk(&block.chunks[shard_id as usize].chunk_hash())
.map(Clone::clone)
})
}
}
.map(|chunk| chunk.into())
Expand Down
12 changes: 10 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Expand Up @@ -6,9 +6,9 @@ use serde::Deserialize;
use serde::Serialize;

use near_primitives::hash::CryptoHash;
use near_primitives::types::BlockIndex;
use near_primitives::types::{BlockIndex, ShardId};
use near_primitives::views::{
BlockView, ExecutionOutcomeView, FinalExecutionOutcomeView, QueryResponse, StatusResponse,
BlockView, ChunkView, ExecutionOutcomeView, FinalExecutionOutcomeView, QueryResponse, StatusResponse,
};

use crate::message::{from_slice, Message};
Expand All @@ -22,6 +22,13 @@ pub enum BlockId {
Hash(CryptoHash),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ChunkId {
BlockShardId(BlockId, ShardId),
Hash(CryptoHash),
}

/// Timeout for establishing connection.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

Expand Down Expand Up @@ -180,6 +187,7 @@ jsonrpc_client!(pub struct JsonRpcClient {
pub fn tx(&mut self, hash: String) -> RpcRequest<FinalExecutionOutcomeView>;
pub fn tx_details(&mut self, hash: String) -> RpcRequest<ExecutionOutcomeView>;
pub fn block(&mut self, id: BlockId) -> RpcRequest<BlockView>;
pub fn chunk(&mut self, id: ChunkId) -> RpcRequest<ChunkView>;
});

/// Create new JSON RPC client that connects to the given address.
Expand Down
21 changes: 19 additions & 2 deletions chain/jsonrpc/src/lib.rs
Expand Up @@ -18,10 +18,10 @@ use async_utils::{delay, timeout};
use message::Message;
use message::{Request, RpcError};
use near_client::{
ClientActor, GetBlock, GetNetworkInfo, Query, Status, TxDetails, TxStatus, ViewClientActor,
ClientActor, GetBlock, GetChunk, GetNetworkInfo, Query, Status, TxDetails, TxStatus, ViewClientActor,
};
pub use near_jsonrpc_client as client;
use near_jsonrpc_client::{message, BlockId};
use near_jsonrpc_client::{message, BlockId, ChunkId};
use near_metrics::{Encoder, TextEncoder};
use near_network::{NetworkClientMessages, NetworkClientResponses};
use near_primitives::hash::CryptoHash;
Expand Down Expand Up @@ -142,6 +142,7 @@ impl JsonRpcHandler {
"tx" => self.tx_status(request.params).await,
"tx_details" => self.tx_details(request.params).await,
"block" => self.block(request.params).await,
"chunk" => self.chunk(request.params).await,
"network_info" => self.network_info().await,
_ => Err(RpcError::method_not_found(request.method)),
}
Expand Down Expand Up @@ -238,6 +239,22 @@ impl JsonRpcHandler {
)
}

async fn chunk(&self, params: Option<Value>) -> Result<Value, RpcError> {
let (chunk_id,) = parse_params::<(ChunkId,)>(params)?;
jsonify(
self.view_client_addr
.send(match chunk_id {
ChunkId::BlockShardId(block_id, shard_id) => match block_id {
BlockId::Height(block_height) => GetChunk::BlockHeight(block_height, shard_id),
BlockId::Hash(block_hash) => GetChunk::BlockHash(block_hash.into(), shard_id),
},
ChunkId::Hash(chunk_hash) => GetChunk::ChunkHash(chunk_hash.into()),
})
.compat()
.await,
)
}

async fn network_info(&self) -> Result<Value, RpcError> {
jsonify(self.client_addr.send(GetNetworkInfo {}).compat().await)
}
Expand Down
45 changes: 44 additions & 1 deletion chain/jsonrpc/tests/rpc_query.rs
Expand Up @@ -4,11 +4,13 @@ use actix::System;
use futures::future;
use futures::future::Future;

use near_crypto::Signature;
use near_jsonrpc::client::new_client;
use near_jsonrpc::test_utils::start_all;
use near_jsonrpc_client::BlockId;
use near_jsonrpc_client::{BlockId, ChunkId};
use near_primitives::hash::CryptoHash;
use near_primitives::test_utils::init_test_logger;
use near_primitives::types::ShardId;

/// Retrieve blocks via json rpc
#[test]
Expand Down Expand Up @@ -65,6 +67,47 @@ fn test_block_by_hash() {
.unwrap();
}

/// Retrieve blocks via json rpc
#[test]
fn test_chunk_by_hash() {
init_test_logger();

System::run(|| {
let (_view_client_addr, addr) = start_all(false);

let mut client = new_client(&format!("http://{}", addr.clone()));
actix::spawn(client.chunk(ChunkId::BlockShardId(BlockId::Height(0), ShardId::from(0u64))).then(move |chunk| {
let chunk = chunk.unwrap();
assert_eq!(chunk.header.balance_burnt, 0);
assert_eq!(chunk.header.chunk_hash.as_ref().len(), 32);
assert_eq!(chunk.header.encoded_length, 8);
assert_eq!(chunk.header.encoded_merkle_root.as_ref().len(), 32);
assert_eq!(chunk.header.gas_limit, 10000000);
assert_eq!(chunk.header.gas_used, 0);
assert_eq!(chunk.header.height_created, 0);
assert_eq!(chunk.header.height_included, 0);
assert_eq!(chunk.header.outgoing_receipts_root.as_ref().len(), 32);
assert_eq!(chunk.header.prev_block_hash.as_ref().len(), 32);
assert_eq!(chunk.header.prev_state_num_parts, 9);
assert_eq!(chunk.header.prev_state_root_hash.as_ref().len(), 32);
assert_eq!(chunk.header.rent_paid, 0);
assert_eq!(chunk.header.shard_id, 0);
assert!(if let Signature::ED25519(_) = chunk.header.signature { true } else { false });
assert_eq!(chunk.header.tx_root.as_ref(), &[0; 32]);
assert_eq!(chunk.header.validator_proposals, vec![]);
assert_eq!(chunk.header.validator_reward, 0);
let mut client = new_client(&format!("http://{}", addr));
client.chunk(ChunkId::Hash(chunk.header.chunk_hash)).then(move |same_chunk| {
let same_chunk = same_chunk.unwrap();
assert_eq!(chunk.header.chunk_hash, same_chunk.header.chunk_hash);
System::current().stop();
future::ok(())
})
}));
})
.unwrap();
}

/// Connect to json rpc and query the client.
#[test]
fn test_query() {
Expand Down
6 changes: 6 additions & 0 deletions core/primitives/src/sharding.rs
Expand Up @@ -18,6 +18,12 @@ impl AsRef<[u8]> for ChunkHash {
}
}

impl From<CryptoHash> for ChunkHash {
fn from(crypto_hash: CryptoHash) -> Self {
Self(crypto_hash)
}
}

#[derive(BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, Debug)]
pub struct ShardChunkHeaderInner {
/// Previous block hash.
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/views.rs
Expand Up @@ -326,6 +326,7 @@ impl From<BlockHeaderView> for BlockHeader {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChunkHeaderView {
pub chunk_hash: CryptoHash,
pub prev_block_hash: CryptoHash,
pub prev_state_root_hash: CryptoHash,
pub prev_state_num_parts: u64,
Expand All @@ -351,6 +352,7 @@ pub struct ChunkHeaderView {
impl From<ShardChunkHeader> for ChunkHeaderView {
fn from(chunk: ShardChunkHeader) -> Self {
ChunkHeaderView {
chunk_hash: chunk.hash.0,
prev_block_hash: chunk.inner.prev_block_hash,
prev_state_root_hash: chunk.inner.prev_state_root.hash,
prev_state_num_parts: chunk.inner.prev_state_root.num_parts,
Expand Down Expand Up @@ -425,7 +427,6 @@ impl From<Block> for BlockView {

#[derive(Serialize, Deserialize, Debug)]
pub struct ChunkView {
pub chunk_hash: CryptoHash,
pub header: ChunkHeaderView,
pub transactions: Vec<SignedTransactionView>,
pub receipts: Vec<ReceiptView>,
Expand All @@ -434,7 +435,6 @@ pub struct ChunkView {
impl From<ShardChunk> for ChunkView {
fn from(chunk: ShardChunk) -> Self {
Self {
chunk_hash: chunk.chunk_hash.0,
header: chunk.header.into(),
transactions: chunk.transactions.into_iter().map(Into::into).collect(),
receipts: chunk.receipts.into_iter().map(Into::into).collect(),
Expand Down

0 comments on commit 9e66b10

Please sign in to comment.