1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::{
collections::HashMap,
io::{self, Error, ErrorKind},
};
use avalanche_types::{ids, jsonrpc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PingResponse {
pub jsonrpc: String,
pub id: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<crate::api::PingResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<APIError>,
}
pub async fn ping(http_rpc: &str, url_path: &str) -> io::Result<PingResponse> {
log::info!("ping {http_rpc} with {url_path}");
let mut data = jsonrpc::RequestWithParamsArray::default();
data.method = String::from("timestampvm.ping");
let d = data.encode_json()?;
let rb = http_manager::post_non_tls(http_rpc, url_path, &d).await?;
serde_json::from_slice(&rb)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed ping '{}'", e)))
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LastAcceptedResponse {
pub jsonrpc: String,
pub id: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<crate::api::chain_handlers::LastAcceptedResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<APIError>,
}
pub async fn last_accepted(http_rpc: &str, url_path: &str) -> io::Result<LastAcceptedResponse> {
log::info!("last_accepted {http_rpc} with {url_path}");
let mut data = jsonrpc::RequestWithParamsArray::default();
data.method = String::from("timestampvm.lastAccepted");
let d = data.encode_json()?;
let rb = http_manager::post_non_tls(http_rpc, url_path, &d).await?;
serde_json::from_slice(&rb)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed last_accepted '{}'", e)))
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetBlockResponse {
pub jsonrpc: String,
pub id: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<crate::api::chain_handlers::GetBlockResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<APIError>,
}
pub async fn get_block(
http_rpc: &str,
url_path: &str,
id: &ids::Id,
) -> io::Result<GetBlockResponse> {
log::info!("get_block {http_rpc} with {url_path}");
let mut data = jsonrpc::RequestWithParamsHashMapArray::default();
data.method = String::from("timestampvm.getBlock");
let mut m = HashMap::new();
m.insert("id".to_string(), id.to_string());
let params = vec![m];
data.params = Some(params);
let d = data.encode_json()?;
let rb = http_manager::post_non_tls(http_rpc, url_path, &d).await?;
serde_json::from_slice(&rb)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed get_block '{}'", e)))
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProposeBlockResponse {
pub jsonrpc: String,
pub id: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<crate::api::chain_handlers::ProposeBlockResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<APIError>,
}
pub async fn propose_block(
http_rpc: &str,
url_path: &str,
d: Vec<u8>,
) -> io::Result<ProposeBlockResponse> {
log::info!("propose_block {http_rpc} with {url_path}");
let mut data = jsonrpc::RequestWithParamsHashMapArray::default();
data.method = String::from("timestampvm.proposeBlock");
let mut m = HashMap::new();
m.insert("data".to_string(), base64::encode(&d));
let params = vec![m];
data.params = Some(params);
let d = data.encode_json()?;
let rb = http_manager::post_non_tls(http_rpc, url_path, &d).await?;
serde_json::from_slice(&rb)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed propose_block '{}'", e)))
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct APIError {
pub code: i32,
pub message: String,
}