Skip to content

Commit

Permalink
Fix tests according to changes in jsonrpc query method refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
khorolets committed Feb 26, 2021
1 parent edaa89f commit e94101f
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 128 deletions.
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.

6 changes: 2 additions & 4 deletions chain/client/tests/catching_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ mod tests {
))
.then(move |res| {
let res_inner = res.unwrap();
if let Ok(Some(query_response)) =
res_inner
{
if let Ok(query_response) = res_inner {
if let ViewAccount(
view_account_result,
) = query_response.kind
Expand Down Expand Up @@ -538,7 +536,7 @@ mod tests {
))
.then(move |res| {
let res_inner = res.unwrap();
if let Ok(Some(query_response)) =
if let Ok(query_response) =
res_inner
{
if let ViewAccount(
Expand Down
4 changes: 2 additions & 2 deletions chain/client/tests/cross_shard_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ mod tests {
}

fn test_cross_shard_tx_callback(
res: Result<Result<Option<QueryResponse>, String>, MailboxError>,
res: Result<Result<QueryResponse, near_client::QueryError>, MailboxError>,
account_id: AccountId,
connectors: Arc<RwLock<Vec<(Addr<ClientActor>, Addr<ViewClientActor>)>>>,
iteration: Arc<AtomicUsize>,
Expand All @@ -188,7 +188,7 @@ mod tests {
min_ratio: Option<f64>,
max_ratio: Option<f64>,
) {
let res = res.unwrap().and_then(|r| r.ok_or_else(|| "Request routed".to_string()));
let res = res.unwrap();

let query_response = match res {
Ok(query_response) => query_response,
Expand Down
2 changes: 1 addition & 1 deletion chain/jsonrpc-primitives/src/types/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct RpcQueryResponse {
pub block_hash: near_primitives::hash::CryptoHash,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum QueryResponseKind {
ViewAccount(near_primitives::views::AccountView),
Expand Down
8 changes: 6 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use near_primitives::hash::CryptoHash;
use near_primitives::types::{BlockId, BlockReference, MaybeBlockId, ShardId};
use near_primitives::views::{
BlockView, ChunkView, EpochValidatorInfo, FinalExecutionOutcomeView, GasPriceView,
QueryResponse, StatusResponse, ValidatorStakeView,
StatusResponse, ValidatorStakeView,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -194,7 +194,11 @@ jsonrpc_client!(pub struct JsonRpcClient {
impl JsonRpcClient {
/// This is a soft-deprecated method to do query RPC request with a path and data positional
/// parameters.
pub fn query_by_path(&self, path: String, data: String) -> RpcRequest<QueryResponse> {
pub fn query_by_path(
&self,
path: String,
data: String,
) -> RpcRequest<near_jsonrpc_primitives::types::query::RpcQueryResponse> {
call_method(&self.client, &self.server_addr, "query", [path, data])
}

Expand Down
77 changes: 29 additions & 48 deletions chain/jsonrpc/tests/rpc_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use near_crypto::{KeyType, PublicKey, Signature};
use near_jsonrpc::client::new_client;
use near_jsonrpc_client::ChunkId;
use near_jsonrpc_primitives::rpc::RpcValidatorsOrderedRequest;
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_logger_utils::init_test_logger;
use near_network::test_utils::WaitOrTimeout;
use near_primitives::account::{AccessKey, AccessKeyPermission};
use near_primitives::hash::CryptoHash;
use near_primitives::types::{BlockId, BlockReference, ShardId, SyncCheckpoint};
use near_primitives::views::{QueryRequest, QueryResponseKind};
use near_primitives::views::QueryRequest;

#[macro_use]
pub mod test_utils;
Expand Down Expand Up @@ -180,17 +181,14 @@ fn test_query_account() {
.await
.unwrap();
for query_response in [query_response_1, query_response_2, query_response_3].iter() {
assert_eq!(query_response.query_response.block_height, 0);
assert_eq!(query_response.query_response.block_hash, block_hash);
assert_eq!(query_response.block_height, 0);
assert_eq!(query_response.block_hash, block_hash);
let account_info = if let QueryResponseKind::ViewAccount(ref account) =
query_response.query_response.kind
query_response.kind
{
account
} else {
panic!(
"queried account, but received something else: {:?}",
query_response.query_response.kind
);
panic!("queried account, but received something else: {:?}", query_response.kind);
};
assert_eq!(account_info.amount, 0);
assert_eq!(account_info.code_hash.as_ref(), &[0; 32]);
Expand Down Expand Up @@ -231,16 +229,12 @@ fn test_query_access_keys() {
})
.await
.unwrap();
assert_eq!(query_response.query_response.block_height, 0);
let access_keys = if let QueryResponseKind::AccessKeyList(access_keys) =
query_response.query_response.kind
assert_eq!(query_response.block_height, 0);
let access_keys = if let QueryResponseKind::AccessKeyList(access_keys) = query_response.kind
{
access_keys
} else {
panic!(
"queried access keys, but received something else: {:?}",
query_response.query_response.kind
);
panic!("queried access keys, but received something else: {:?}", query_response.kind);
};
assert_eq!(access_keys.keys.len(), 1);
assert_eq!(access_keys.keys[0].access_key, AccessKey::full_access().into());
Expand Down Expand Up @@ -286,16 +280,12 @@ fn test_query_access_key() {
})
.await
.unwrap();
assert_eq!(query_response.query_response.block_height, 0);
let access_key =
if let QueryResponseKind::AccessKey(access_keys) = query_response.query_response.kind {
access_keys
} else {
panic!(
"queried access keys, but received something else: {:?}",
query_response.query_response.kind
);
};
assert_eq!(query_response.block_height, 0);
let access_key = if let QueryResponseKind::AccessKey(access_keys) = query_response.kind {
access_keys
} else {
panic!("queried access keys, but received something else: {:?}", query_response.kind);
};
assert_eq!(access_key.nonce, 0);
assert_eq!(access_key.permission, AccessKeyPermission::FullAccess.into());
});
Expand All @@ -315,15 +305,11 @@ fn test_query_state() {
})
.await
.unwrap();
assert_eq!(query_response.query_response.block_height, 0);
let state = if let QueryResponseKind::ViewState(state) = query_response.query_response.kind
{
assert_eq!(query_response.block_height, 0);
let state = if let QueryResponseKind::ViewState(state) = query_response.kind {
state
} else {
panic!(
"queried state, but received something else: {:?}",
query_response.query_response.kind
);
panic!("queried state, but received something else: {:?}", query_response.kind);
};
assert_eq!(state.values.len(), 0);
});
Expand All @@ -344,15 +330,13 @@ fn test_query_call_function() {
})
.await
.unwrap();
assert_eq!(query_response.query_response.block_height, 0);
let call_result = if let QueryResponseKind::CallResult(call_result) =
query_response.query_response.kind
{
assert_eq!(query_response.block_height, 0);
let call_result = if let QueryResponseKind::CallResult(call_result) = query_response.kind {
call_result
} else {
panic!(
"expected a call function result, but received something else: {:?}",
query_response.query_response.kind
query_response.kind
);
};
assert_eq!(call_result.result.len(), 0);
Expand All @@ -371,14 +355,11 @@ fn test_query_contract_code() {
})
.await
.unwrap();
assert_eq!(query_response.query_response.block_height, 0);
let code = if let QueryResponseKind::ViewCode(code) = query_response.query_response.kind {
assert_eq!(query_response.block_height, 0);
let code = if let QueryResponseKind::ViewCode(code) = query_response.kind {
code
} else {
panic!(
"queried code, but received something else: {:?}",
query_response.query_response.kind
);
panic!("queried code, but received something else: {:?}", query_response.kind);
};
assert_eq!(code.code, Vec::<u8>::new());
assert_eq!(code.hash.to_string(), "11111111111111111111111111111111");
Expand Down Expand Up @@ -594,7 +575,7 @@ fn test_query_view_account_non_existing_account_must_return_error() {
.unwrap();

assert!(
!matches!(query_response.query_response.kind, QueryResponseKind::ViewAccount(_)),
!matches!(query_response.kind, QueryResponseKind::ViewAccount(_)),
"queried view account for not exsiting account, but received success instead of error"
);
});
Expand All @@ -616,7 +597,7 @@ fn test_view_access_key_non_existing_account_id_and_public_key_must_return_error
.unwrap();

assert!(
!matches!(query_response.query_response.kind, QueryResponseKind::AccessKey(_)),
!matches!(query_response.kind, QueryResponseKind::AccessKey(_)),
"queried access key with not existing account and public key, received success instead of error"
);
});
Expand All @@ -641,7 +622,7 @@ fn test_call_function_non_existing_account_method_name() {
.unwrap();

assert!(
!matches!(query_response.query_response.kind, QueryResponseKind::CallResult(_)),
!matches!(query_response.kind, QueryResponseKind::CallResult(_)),
"queried call function with not existing account and method name, received success instead of error"
);
});
Expand All @@ -662,7 +643,7 @@ fn test_view_access_key_list_non_existing_account() {
.unwrap();

assert!(
!matches!(query_response.query_response.kind, QueryResponseKind::AccessKeyList(_)),
!matches!(query_response.kind, QueryResponseKind::AccessKeyList(_)),
"queried access key list with not existing account, received success instead of error"
);
});
Expand All @@ -684,7 +665,7 @@ fn test_view_state_non_existing_account_invalid_prefix() {
.unwrap();

assert!(
!matches!(query_response.query_response.kind, QueryResponseKind::ViewState(_)),
!matches!(query_response.kind, QueryResponseKind::ViewState(_)),
"queried view account for not exsiting account, but received success instead of error"
);
});
Expand Down
61 changes: 2 additions & 59 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ impl std::iter::FromIterator<AccessKeyInfoView> for AccessKeyList {
}
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(untagged)]
#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Clone)]
pub enum QueryResponseKind {
ViewAccount(AccountView),
ViewCode(ContractCodeView),
Expand Down Expand Up @@ -287,9 +286,8 @@ pub enum QueryRequest {
},
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Clone)]
pub struct QueryResponse {
#[serde(flatten)]
pub kind: QueryResponseKind,
pub block_height: BlockHeight,
pub block_hash: CryptoHash,
Expand Down Expand Up @@ -332,61 +330,6 @@ pub struct StatusResponse {
pub validator_account_id: Option<AccountId>,
}

impl TryFrom<QueryResponse> for AccountView {
type Error = String;

fn try_from(query_response: QueryResponse) -> Result<Self, Self::Error> {
match query_response.kind {
QueryResponseKind::ViewAccount(acc) => Ok(acc),
_ => Err("Invalid type of response".into()),
}
}
}

impl TryFrom<QueryResponse> for CallResult {
type Error = String;

fn try_from(query_response: QueryResponse) -> Result<Self, Self::Error> {
match query_response.kind {
QueryResponseKind::CallResult(res) => Ok(res),
_ => Err("Invalid type of response".into()),
}
}
}

impl TryFrom<QueryResponse> for ViewStateResult {
type Error = String;

fn try_from(query_response: QueryResponse) -> Result<Self, Self::Error> {
match query_response.kind {
QueryResponseKind::ViewState(vs) => Ok(vs),
_ => Err("Invalid type of response".into()),
}
}
}

impl TryFrom<QueryResponse> for AccessKeyView {
type Error = String;

fn try_from(query_response: QueryResponse) -> Result<Self, Self::Error> {
match query_response.kind {
QueryResponseKind::AccessKey(access_key) => Ok(access_key),
_ => Err("Invalid type of response".into()),
}
}
}

impl TryFrom<QueryResponse> for ContractCodeView {
type Error = String;

fn try_from(query_response: QueryResponse) -> Result<Self, Self::Error> {
match query_response.kind {
QueryResponseKind::ViewCode(contract_code) => Ok(contract_code),
_ => Err("Invalid type of response".into()),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChallengeView {
// TODO: decide how to represent challenges in json.
Expand Down
3 changes: 1 addition & 2 deletions neard/tests/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use near_primitives::transaction::{PartialExecutionStatus, SignedTransaction};
use near_primitives::types::{BlockId, BlockReference, Finality, TransactionOrReceiptId};
use near_primitives::views::{
ExecutionOutcomeView, ExecutionStatusView, FinalExecutionOutcomeViewEnum, FinalExecutionStatus,
QueryResponseKind,
};
use neard::config::TESTING_INIT_BALANCE;
use std::sync::atomic::AtomicBool;
Expand Down Expand Up @@ -397,7 +396,7 @@ fn test_rpc_routing() {
.query_by_path("account/near.2".to_string(), "".to_string())
.map_err(|err| panic_on_rpc_error!(err))
.map_ok(move |result| match result.kind {
QueryResponseKind::ViewAccount(account_view) => {
near_jsonrpc_primitives::types::query::QueryResponseKind::ViewAccount(account_view) => {
assert_eq!(
account_view.amount,
TESTING_INIT_BALANCE
Expand Down
1 change: 1 addition & 0 deletions test-utils/testlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ near-vm-errors = { path = "../../runtime/near-vm-errors" }
near-chain = { path = "../../chain/chain" }
near-client = { path = "../../chain/client" }
near-jsonrpc = { path = "../../chain/jsonrpc" }
near-jsonrpc-primitives = { path = "../../chain/jsonrpc-primitives" }
near-network = { path = "../../chain/network" }
near-jsonrpc-client = { path = "../../chain/jsonrpc/client" }
neard = { path = "../../neard" }
Expand Down
Loading

0 comments on commit e94101f

Please sign in to comment.