Skip to content

Commit

Permalink
eth2near crates: add timeout on NEAR RPC requests (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
karim-en authored Mar 20, 2023
1 parent d8e3def commit 4b24cbd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions eth2near/contract_wrapper/src/dao_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ mod tests {
SIGNER_ACCOUNT_ID,
SIGNER_PRIVATE_KEY,
DAO_CONTRACT_ACCOUNT_ID,
None,
);

let mut dao_contract = DAOContract::new(Box::new(near_contract_wrapper));
Expand Down
2 changes: 2 additions & 0 deletions eth2near/contract_wrapper/src/dao_eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod tests {
&signer_account_id,
&signer_private_key,
CONTRACT_ACCOUNT_ID,
None,
));

let eth_client = eth_client_contract::EthClientContract::new(near_contract_wrapper);
Expand Down Expand Up @@ -246,6 +247,7 @@ mod tests {
&signer_account_id,
&signer_private_key,
DAO_CONTRACT_ACCOUNT_ID,
None,
);
let dao_contract = dao_contract::DAOContract::new(Box::new(dao_contract_wrapper));
let mut dao_client =
Expand Down
5 changes: 4 additions & 1 deletion eth2near/contract_wrapper/src/near_contract_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ impl NearContractWrapper {
account_id: &str,
signer_secret_key: &str,
contract_account_id: &str,
timeout: Option<std::time::Duration>,
) -> NearContractWrapper {
let signer_account_id = account_id.parse().expect("Error on parsing account id during creation near contract wrapper");
let client = JsonRpcClient::connect(near_endpoint);
let client = JsonRpcClient::with(utils::new_near_rpc_client(timeout)).connect(near_endpoint);
let contract_account = contract_account_id.parse().expect("Error on parsing contract account id during creation near contract wrapper");

let signer =
Expand All @@ -70,6 +71,7 @@ impl NearContractWrapper {
account_id: &str,
path_to_signer_secret_key: &str,
contract_account_id: &str,
timeout: Option<std::time::Duration>,
) -> NearContractWrapper {
let v: Value = serde_json::from_str(
&std::fs::read_to_string(path_to_signer_secret_key).expect("Unable to read file"),
Expand All @@ -82,6 +84,7 @@ impl NearContractWrapper {
account_id,
&signer_secret_key,
contract_account_id,
timeout,
)
}
}
Expand Down
14 changes: 14 additions & 0 deletions eth2near/contract_wrapper/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ pub fn trim_quotes(s: String) -> String {

res_str
}

pub fn new_near_rpc_client(timeout: Option<std::time::Duration>) -> reqwest::Client {
let mut headers = reqwest::header::HeaderMap::with_capacity(2);
headers.insert(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static("application/json"),
);

let mut builder = reqwest::Client::builder().default_headers(headers);
if let Some(timeout) = timeout {
builder = builder.timeout(timeout);
}
builder.build().unwrap()
}
3 changes: 3 additions & 0 deletions eth2near/eth2near-block-relay-rs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub struct Config {
// Timeout for ETH RPC get status requests in seconds
pub state_requests_timeout_seconds: u64,

// Timeout for NEAR RPC requests in seconds
pub near_requests_timeout_seconds: u64,

// Sleep time in seconds when ETH client is synchronized with ETH network
pub sleep_time_on_sync_secs: u64,

Expand Down
6 changes: 6 additions & 0 deletions eth2near/eth2near-block-relay-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ fn get_eth_contract_wrapper(config: &Config) -> Box<dyn ContractWrapper> {
&config.signer_account_id,
&config.path_to_signer_secret_key,
&config.contract_account_id,
Some(std::time::Duration::from_secs(
config.near_requests_timeout_seconds,
)),
))
}

Expand All @@ -50,6 +53,9 @@ fn get_dao_contract_wrapper(config: &Config) -> Box<dyn ContractWrapper> {
&config.path_to_signer_secret_key,
&dao_contract_account_id
.expect("No DAO contract account ID provided for relay running in DAO mode"),
Some(std::time::Duration::from_secs(
config.near_requests_timeout_seconds,
)),
))
}

Expand Down
1 change: 1 addition & 0 deletions eth2near/eth2near-block-relay-rs/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fn get_config(config_for_test: &ConfigForTests) -> Config {
path_to_finality_state: None,
eth_requests_timeout_seconds: 30,
state_requests_timeout_seconds: 1000,
near_requests_timeout_seconds: 30,
sleep_time_on_sync_secs: 0,
sleep_time_after_submission_secs: 5,
hashes_gc_threshold: None,
Expand Down

0 comments on commit 4b24cbd

Please sign in to comment.