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

Early work on Chaos Monkey #1438

Merged
merged 2 commits into from Oct 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion chain/chain/src/chain.rs
Expand Up @@ -1820,7 +1820,7 @@ impl<'a> ChainUpdate<'a> {
// Verify that proposals from chunks match block header proposals.
let mut all_chunk_proposals = vec![];
for chunk in block.chunks.iter() {
if chunk.inner.height_created == chunk.height_included {
if block.header.inner.height == chunk.height_included {
all_chunk_proposals.extend(chunk.inner.validator_proposals.clone());
}
}
Expand Down
13 changes: 11 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Expand Up @@ -2,16 +2,25 @@ use std::time::Duration;

use actix_web::client::Client;
use futures::Future;
use serde::Deserialize;
use serde::Serialize;

use near_primitives::types::BlockIndex;
use near_primitives::views::{
BlockView, ExecutionOutcomeView, FinalExecutionOutcomeView, QueryResponse, StatusResponse,
BlockView, CryptoHashView, ExecutionOutcomeView, FinalExecutionOutcomeView, QueryResponse,
StatusResponse,
};

pub mod message;
use crate::message::{from_slice, Message};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BlockId {
Height(BlockIndex),
Hash(CryptoHashView),
}

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

Expand Down Expand Up @@ -169,7 +178,7 @@ jsonrpc_client!(pub struct JsonRpcClient {
pub fn health(&mut self) -> RpcRequest<()>;
pub fn tx(&mut self, hash: String) -> RpcRequest<FinalExecutionOutcomeView>;
pub fn tx_details(&mut self, hash: String) -> RpcRequest<ExecutionOutcomeView>;
pub fn block(&mut self, height: BlockIndex) -> RpcRequest<BlockView>;
pub fn block(&mut self, id: BlockId) -> RpcRequest<BlockView>;
});

/// Create new JSON RPC client that connects to the given address.
Expand Down
10 changes: 6 additions & 4 deletions chain/jsonrpc/src/lib.rs
Expand Up @@ -18,12 +18,11 @@ use message::{Request, RpcError};
use message::Message;
use near_client::{ClientActor, GetBlock, Query, Status, TxDetails, TxStatus, ViewClientActor, GetNetworkInfo};
pub use near_jsonrpc_client as client;
use near_jsonrpc_client::message as message;
use near_jsonrpc_client::{message as message, BlockId};
use near_network::{NetworkClientMessages, NetworkClientResponses};
use near_primitives::hash::CryptoHash;
use near_primitives::serialize::{BaseEncode, from_base, from_base64};
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::BlockIndex;
use near_primitives::views::{FinalExecutionStatus, ExecutionErrorView};

pub mod test_utils;
Expand Down Expand Up @@ -216,8 +215,11 @@ impl JsonRpcHandler {
}

async fn block(&self, params: Option<Value>) -> Result<Value, RpcError> {
let (height,) = parse_params::<(BlockIndex,)>(params)?;
jsonify(self.view_client_addr.send(GetBlock::Height(height)).compat().await)
let (block_id,) = parse_params::<(BlockId,)>(params)?;
jsonify(self.view_client_addr.send(match block_id {
BlockId::Height(height) => GetBlock::Height(height),
BlockId::Hash(hash) => GetBlock::Hash(hash.into()),
}).compat().await)
}

async fn network_info(&self) -> Result<Value, RpcError> {
Expand Down
29 changes: 27 additions & 2 deletions chain/jsonrpc/tests/rpc_query.rs
Expand Up @@ -5,6 +5,7 @@ use futures::future::Future;
use near_crypto::BlsSignature;
use near_jsonrpc::client::new_client;
use near_jsonrpc::test_utils::start_all;
use near_jsonrpc_client::BlockId;
use near_primitives::test_utils::init_test_logger;

/// Retrieve blocks via json rpc
Expand All @@ -16,7 +17,8 @@ fn test_block() {
let (_view_client_addr, addr) = start_all(false);

let mut client = new_client(&format!("http://{}", addr));
actix::spawn(client.block(0).then(|res| {

actix::spawn(client.block(BlockId::Height(0)).then(|res| {
let res = res.unwrap();
assert_eq!(res.header.height, 0);
assert_eq!(res.header.epoch_id.0, &[0; 32]);
Expand All @@ -35,7 +37,30 @@ fn test_block() {
assert_eq!(res.header.total_weight, 0);
assert_eq!(res.header.validator_proposals.len(), 0);
System::current().stop();
future::result(Ok(()))
future::ok(())
}));
})
.unwrap();
}

/// Retrieve blocks via json rpc
#[test]
fn test_block_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.block(BlockId::Height(0)).then(move |res| {
let res = res.unwrap();
let mut client = new_client(&format!("http://{}", addr));
client.block(BlockId::Hash(res.header.hash)).then(move |res| {
let res = res.unwrap();
assert_eq!(res.header.height, 0);
System::current().stop();
future::ok(())
})
}));
})
.unwrap();
Expand Down