Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit fec8159

Browse files
authored
Merge pull request #85 from ava-labs/sync-0.0.393
sync: Bump to 0.0.393
2 parents 546f268 + cebfeae commit fec8159

File tree

4 files changed

+144
-7
lines changed

4 files changed

+144
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "avalanche-types"
3-
version = "0.0.390" # https://crates.io/crates/avalanche-types
3+
version = "0.0.393" # https://crates.io/crates/avalanche-types
44
edition = "2021"
55
rust-version = "1.69" # use "rustup override set stable" to overwrite current toolchain
66
publish = true

src/jsonrpc/admin.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
#[allow(dead_code)]
1+
use std::io::{self, Error as ioError, ErrorKind};
2+
3+
use serde::{Deserialize, Serialize};
24

35
/// The chain alias method name
46
const ALIAS_METHOD: &str = "admin.aliasChain";
57

68
/// The request to alias a chain via the admin API.
79
/// Ref: https://docs.avax.network/apis/avalanchego/apis/admin#adminaliaschain
8-
struct ChainAliasRequest {
10+
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
11+
pub struct ChainAliasRequest {
912
/// Jsonrpc version
1013
pub jsonrpc: String,
1114
/// Id of request
@@ -27,20 +30,63 @@ impl Default for ChainAliasRequest {
2730
}
2831
}
2932

33+
impl ChainAliasRequest {
34+
pub fn encode_json(&self) -> io::Result<String> {
35+
serde_json::to_string(&self)
36+
.map_err(|e| ioError::new(ErrorKind::Other, format!("failed to serialize JSON {}", e)))
37+
}
38+
}
39+
3040
/// Parameters for the alias request.
31-
struct ChainAliasParams {
41+
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
42+
pub struct ChainAliasParams {
3243
/// The long-form chain ID
3344
pub chain: String,
3445
/// The newly issues alias
3546
pub alias: String,
3647
}
3748

3849
/// Response for the alias request.
39-
struct ChainAliasResponse {
50+
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
51+
pub struct ChainAliasResponse {
4052
/// Jsonrpc version
4153
pub jsonrpc: String,
4254
/// Id of request
4355
pub id: u32,
44-
/// Result (empty)
45-
pub result: (),
56+
}
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use crate::jsonrpc::admin::{ChainAliasParams, ChainAliasRequest, ChainAliasResponse};
61+
use crate::jsonrpc::{DEFAULT_ID, DEFAULT_VERSION};
62+
63+
#[test]
64+
fn test_serialization() {
65+
let chain = String::from("sV6o671RtkGBcno1FiaDbVcFv2sG5aVXMZYzKdP4VQAWmJQnM");
66+
let alias = String::from("devnet");
67+
68+
let req = ChainAliasRequest {
69+
params: Some(ChainAliasParams { chain, alias }),
70+
..Default::default()
71+
};
72+
73+
let serialized = req.encode_json().expect("failed serialization");
74+
75+
let expected: &str = r#"{"jsonrpc":"2.0","id":1,"method":"admin.aliasChain","params":{"chain":"sV6o671RtkGBcno1FiaDbVcFv2sG5aVXMZYzKdP4VQAWmJQnM","alias":"devnet"}}"#;
76+
assert_eq!(serialized, expected);
77+
}
78+
79+
#[test]
80+
fn test_deserialization() {
81+
let resp = ChainAliasResponse {
82+
jsonrpc: String::from(DEFAULT_VERSION),
83+
id: DEFAULT_ID,
84+
};
85+
86+
let expected = r#"{"jsonrpc": "2.0","id": 1,"result": {}}"#.as_bytes();
87+
let deserialized: ChainAliasResponse =
88+
serde_json::from_slice(expected).expect("failed deserialization");
89+
90+
assert_eq!(resp, deserialized);
91+
}
4692
}

src/jsonrpc/client/admin.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use std::time::Duration;
2+
3+
use reqwest::{header::CONTENT_TYPE, ClientBuilder};
4+
5+
use crate::{
6+
errors::{Error, Result},
7+
jsonrpc::admin::{ChainAliasParams, ChainAliasRequest, ChainAliasResponse},
8+
utils,
9+
};
10+
11+
/// Set an alias for a chain.
12+
pub async fn alias_chain(
13+
http_rpc: &str,
14+
chain: String,
15+
alias: String,
16+
) -> Result<ChainAliasResponse> {
17+
let (scheme, host, port, _, _) =
18+
utils::urls::extract_scheme_host_port_path_chain_alias(http_rpc).map_err(|e| {
19+
Error::Other {
20+
message: format!("failed extract_scheme_host_port_path_chain_alias '{}'", e),
21+
retryable: false,
22+
}
23+
})?;
24+
25+
let u = if let Some(scheme) = scheme {
26+
if let Some(port) = port {
27+
format!("{scheme}://{host}:{port}/ext/info")
28+
} else {
29+
format!("{scheme}://{host}/ext/info")
30+
}
31+
} else {
32+
format!("http://{host}/ext/info")
33+
};
34+
log::info!("getting network name for {u}");
35+
36+
let data = ChainAliasRequest {
37+
params: Some(ChainAliasParams { chain, alias }),
38+
..Default::default()
39+
};
40+
41+
let d = data.encode_json().map_err(|e| Error::Other {
42+
message: format!("failed encode_json '{}'", e),
43+
retryable: false,
44+
})?;
45+
46+
let req_cli_builder = ClientBuilder::new()
47+
.user_agent(env!("CARGO_PKG_NAME"))
48+
.danger_accept_invalid_certs(true)
49+
.timeout(Duration::from_secs(15))
50+
.connection_verbose(true)
51+
.build()
52+
.map_err(|e| {
53+
// TODO: check retryable
54+
Error::Other {
55+
message: format!("failed reqwest::ClientBuilder.build '{}'", e),
56+
retryable: false,
57+
}
58+
})?;
59+
60+
let resp = req_cli_builder
61+
.post(&u)
62+
.header(CONTENT_TYPE, "application/json")
63+
.body(d)
64+
.send()
65+
.await
66+
.map_err(|e|
67+
// TODO: check retryable
68+
Error::API {
69+
message: format!("failed reqwest::Client.send '{}'", e),
70+
retryable: false,
71+
})?;
72+
73+
let out = resp.bytes().await.map_err(|e| {
74+
// TODO: check retryable
75+
Error::Other {
76+
message: format!("failed reqwest response bytes '{}'", e),
77+
retryable: false,
78+
}
79+
})?;
80+
let out: Vec<u8> = out.into();
81+
82+
let response: ChainAliasResponse = serde_json::from_slice(&out)
83+
.map_err(|e| Error::Other {
84+
message: format!("failed serde_json::from_slice '{}'", e),
85+
retryable: false,
86+
})
87+
.unwrap();
88+
89+
Ok(response)
90+
}

src/jsonrpc/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod admin;
12
pub mod evm;
23
pub mod health;
34
pub mod info;

0 commit comments

Comments
 (0)