Skip to content

Commit

Permalink
Merge pull request #890 from tonlabs/SDK-3790-move-ever-sdk-tests-to-…
Browse files Browse the repository at this point in the history
…authorization

SDK-3790: move ever sdk tests to authorization
  • Loading branch information
d3p authored Oct 6, 2022
2 parents 95b84bf + f61d3c3 commit 86fe6b1
Show file tree
Hide file tree
Showing 13 changed files with 502 additions and 281 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ TON_USE_SE: true/false - flag defining if tests run against Evernode SE or a rea
TON_NETWORK_ADDRESS - Dapp server or Evernode SE addresses separated by comma.
TON_GIVER_SECRET - Giver secret key. If not defined, default Evernode SE giver keys are used
TON_GIVER_ADDRESS - Address of the giver to use for prepaying accounts before deploying test contracts. If not defined, the address is calculated using `GiverV2.tvc` and configured public key
EVERCLOUD_AUTH_PROJECT – Evercloud project id used to authorise tests that requires main net interaction
```

## Download precompiled binaries
Expand Down
6 changes: 3 additions & 3 deletions ton_client/src/debot/dengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const EMPTY_CELL: &'static str = "te6ccgEBAQEAAgAAAA==";
fn create_client(url: &str) -> Result<TonClient, String> {
let cli_conf = ClientConfig {
network: NetworkConfig {
server_address: Some(url.to_owned()),
endpoints: Some(vec![url.to_string()]),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -159,7 +159,7 @@ impl DEngine {
state.clone(),
addr.clone(),
abi.clone(),
"getDebotInfo",
"getDebotInfo",
None
).await;
let mut info: DInfo = match result {
Expand All @@ -170,7 +170,7 @@ impl DEngine {
info.interfaces = interfaces;
info.dabi_version = dabi_version;

// TODO DEPRECATED
// TODO DEPRECATED
// For compatibility with previous debots that returns abi in
// getDebotOptions. Remove later.
if info.dabi.is_none() {
Expand Down
5 changes: 5 additions & 0 deletions ton_client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::client::core_version;
use serde_json::Value;
use std::fmt::Display;
use chrono::TimeZone;
use crate::net;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, ApiType)]
pub struct ClientError {
Expand Down Expand Up @@ -151,6 +152,10 @@ impl ClientError {
self.data["account_address"] = address.to_string().into();
self
}

pub fn is_unauthorized(&self) -> bool {
self.code == net::ErrorCode::Unauthorized as u32
}
}

pub(crate) fn format_time(time: u32) -> String {
Expand Down
14 changes: 9 additions & 5 deletions ton_client/src/net/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ impl Endpoint {
fn expand_address(base_url: &str) -> String {
let mut base_url = base_url.trim_end_matches("/").to_lowercase();
if !base_url.starts_with(HTTP_PROTOCOL) && !base_url.starts_with(HTTPS_PROTOCOL) {
let protocol = if base_url == "localhost" || base_url == "127.0.0.1" || base_url == "0.0.0.0" {
HTTP_PROTOCOL
} else {
HTTPS_PROTOCOL
};
let protocol =
if base_url == "localhost" || base_url == "127.0.0.1" || base_url == "0.0.0.0" {
HTTP_PROTOCOL
} else {
HTTPS_PROTOCOL
};
base_url = format!("{}{}", protocol, base_url);
};
if base_url.ends_with("/graphql") {
Expand Down Expand Up @@ -114,6 +115,9 @@ impl Endpoint {
timeout,
)
.await?;
if response.status == 401 {
return Err(Error::unauthorized());
}
let query_url = response.url.trim_end_matches(query).to_owned();
let info = response.body_as_json()?["data"]["info"].to_owned();
Ok((info, query_url, response.remote_address))
Expand Down
18 changes: 15 additions & 3 deletions ton_client/src/net/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{ClientError, format_time};
use crate::error::{format_time, ClientError};
use serde_json::Value;
use std::fmt::Display;

Expand All @@ -18,6 +18,7 @@ pub enum ErrorCode {
NoEndpointsProvided = 612,
GraphqlWebsocketInitError = 613,
NetworkModuleResumed = 614,
Unauthorized = 615,
}

pub struct Error;
Expand All @@ -27,6 +28,10 @@ fn error(code: ErrorCode, message: String) -> ClientError {
}

impl Error {
pub fn unauthorized() -> ClientError {
error(ErrorCode::Unauthorized, "Unauthorized".to_string())
}

pub fn queries_query_failed<E: Display>(err: E) -> ClientError {
error(ErrorCode::QueryFailed, format!("Query failed: {}", err))
}
Expand All @@ -38,7 +43,11 @@ impl Error {
)
}

pub fn queries_wait_for_failed<E: Display>(err: E, filter: Option<Value>, timestamp: u32) -> ClientError {
pub fn queries_wait_for_failed<E: Display>(
err: E,
filter: Option<Value>,
timestamp: u32,
) -> ClientError {
let mut err = error(ErrorCode::WaitForFailed, format!("WaitFor failed: {}", err));
err.data = json!({
"filter": filter,
Expand Down Expand Up @@ -84,7 +93,10 @@ impl Error {
fn try_get_message_and_code(server_errors: &[Value]) -> (Option<String>, Option<i64>) {
for error in server_errors.iter() {
if let Some(message) = error["message"].as_str() {
return (Some(message.to_string()), error["extensions"]["exception"]["code"].as_i64());
let code = error["extensions"]["exception"]["code"]
.as_i64()
.or_else(|| error["extensions"]["code"].as_i64());
return (Some(message.to_string()), code);
}
}
(None, None)
Expand Down
31 changes: 23 additions & 8 deletions ton_client/src/net/server_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use tokio::sync::{watch, Mutex, RwLock};

pub const MAX_TIMEOUT: u32 = std::i32::MAX as u32;
pub const MAX_TIMEOUT: u32 = i32::MAX as u32;
pub const MIN_RESUME_TIMEOUT: u32 = 500;
pub const MAX_RESUME_TIMEOUT: u32 = 3000;

Expand Down Expand Up @@ -298,6 +298,7 @@ impl NetworkState {
}));
}
let mut selected = Err(crate::client::Error::net_module_not_init());
let mut unauthorised = true;
while futures.len() != 0 {
let (result, _, remain_futures) = futures::future::select_all(futures).await;
if let Ok(endpoint) = &result {
Expand All @@ -306,13 +307,21 @@ impl NetworkState {
}
}
futures = remain_futures;
if let Err(err) = &result {
if !err.is_unauthorized() {
unauthorised = false;
}
}
if is_better(&result, &selected) {
selected = result;
}
}
if selected.is_ok() {
return selected;
}
if unauthorised {
return Err(Error::unauthorized());
}
retry_count += 1;
if retry_count > self.config.network_retries_count {
return selected;
Expand Down Expand Up @@ -574,13 +583,19 @@ impl ServerLink {

let result = match result {
Err(err) => Err(err),
Ok(response) => match response.body_as_json() {
Err(err) => Err(err),
Ok(value) => match Self::try_extract_error(&value) {
Some(err) => Err(err),
None => Ok(value),
},
},
Ok(response) => {
if response.status == 401 {
Err(Error::unauthorized())
} else {
match response.body_as_json() {
Err(err) => Err(err),
Ok(value) => match Self::try_extract_error(&value) {
Some(err) => Err(err),
None => Ok(value),
},
}
}
}
};

if let Err(err) = &result {
Expand Down
Loading

0 comments on commit 86fe6b1

Please sign in to comment.