forked from SO4-Markets/so4-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprices.rs
More file actions
313 lines (280 loc) · 10.5 KB
/
Copy pathprices.rs
File metadata and controls
313 lines (280 loc) · 10.5 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::sync::Arc;
use std::time::Duration;
use axum::extract::{FromRequestParts, Query, State};
use axum::http::request::Parts;
use axum::http::StatusCode;
use axum::Json;
use serde::{Deserialize, Serialize};
use super::{AdminAuth, ApiError};
use crate::state::{AppState, CachedPrice, FailedSubmission};
const READY_BALANCE_RETRY_ATTEMPTS: u32 = 3;
const READY_BALANCE_RETRY_BASE_DELAY_MS: u64 = 100;
#[derive(Debug, Deserialize)]
pub struct FailedSubmissionsQuery {
pub operation: Option<String>,
pub limit: Option<usize>,
}
// #602 — axum's built-in `Query` rejection renders as a bare text/plain body,
// which breaks the `{"error": "..."}` envelope every other endpoint returns.
// Extracting through this impl maps the rejection onto `ApiError` so a malformed
// `?limit=` value stays parseable for clients that unconditionally read JSON.
impl FromRequestParts<Arc<AppState>> for FailedSubmissionsQuery {
type Rejection = ApiError;
async fn from_request_parts(
parts: &mut Parts,
state: &Arc<AppState>,
) -> Result<Self, Self::Rejection> {
Query::<Self>::from_request_parts(parts, state)
.await
.map(|Query(query)| query)
.map_err(|rejection| ApiError::new(rejection.status(), rejection.body_text()))
}
}
// #497 — extended health response exposes last-cycle timestamps and failure
// counts so staleness or failure streaks are detectable without grepping logs.
#[derive(Debug, Serialize)]
pub struct HealthResponse {
pub status: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_price_cycle_secs_ago: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_keeper_cycle_secs_ago: Option<u64>,
pub price_cycle_count: u64,
pub keeper_cycle_count: u64,
pub token_fetch_failures: u64,
pub submit_failures: u64,
}
#[derive(Debug, Serialize)]
pub struct FailuresResponse {
pub failures: Vec<FailedSubmission>,
pub total_count: usize,
}
pub async fn health(State(state): State<Arc<AppState>>) -> Json<HealthResponse> {
let cycle = state.cycle_status.read().await;
let metrics = state.metrics.to_response();
let last_price_cycle_secs_ago = cycle
.last_price_cycle_at
.and_then(|t| t.elapsed().ok())
.map(|d| d.as_secs());
let last_keeper_cycle_secs_ago = cycle
.last_keeper_cycle_at
.and_then(|t| t.elapsed().ok())
.map(|d| d.as_secs());
Json(HealthResponse {
status: "ok",
last_price_cycle_secs_ago,
last_keeper_cycle_secs_ago,
price_cycle_count: metrics.price_cycle_count,
keeper_cycle_count: metrics.keeper_cycle_count,
token_fetch_failures: metrics.token_fetch_failures,
submit_failures: metrics.submit_failures,
})
}
pub async fn ready(State(state): State<Arc<AppState>>) -> Result<Json<HealthResponse>, ApiError> {
// Check price cache has at least one cycle completed
{
let cache = state.price_cache.read().await;
if cache.prices.is_empty() {
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"no_prices_cached",
));
}
}
// Check price loop is not stale (must have run within 3x the loop interval)
{
let cycle = state.cycle_status.read().await;
let stale_threshold = state.config.price_loop_interval * 3;
let is_stale = cycle
.last_price_cycle_at
.map(|last| last.elapsed().unwrap_or_default() > stale_threshold)
.unwrap_or(true);
if is_stale {
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"price_loop_stale",
));
}
}
// Check keeper loop is not stale (must have run within 3x the loop interval)
{
let cycle = state.cycle_status.read().await;
let stale_threshold = state.config.keeper_loop_interval * 3;
let is_stale = cycle
.last_keeper_cycle_at
.map(|last| last.elapsed().unwrap_or_default() > stale_threshold)
.unwrap_or(true);
if is_stale {
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"keeper_loop_stale",
));
}
}
// Check cached external RPC and keeper balance readiness result (3s TTL)
let metrics = state.metrics.to_response();
{
let cache = state.ready_cache.read().await;
if let Some(last) = cache.last_checked {
if last.elapsed() < std::time::Duration::from_secs(3) {
if let Some((status, msg)) = cache.last_error.clone() {
return Err(ApiError::new(status, msg));
}
return Ok(Json(HealthResponse {
status: "ok",
last_price_cycle_secs_ago: None,
last_keeper_cycle_secs_ago: None,
price_cycle_count: metrics.price_cycle_count,
keeper_cycle_count: metrics.keeper_cycle_count,
token_fetch_failures: metrics.token_fetch_failures,
submit_failures: metrics.submit_failures,
}));
}
}
}
let check_res = perform_external_ready_checks(&state).await;
{
let mut cache = state.ready_cache.write().await;
cache.last_checked = Some(std::time::Instant::now());
match check_res {
Ok(()) => {
cache.last_error = None;
}
Err(err) => {
cache.last_error = Some((err.status, err.message.clone()));
return Err(err);
}
}
}
let metrics = state.metrics.to_response();
Ok(Json(HealthResponse {
status: "ok",
last_price_cycle_secs_ago: None,
last_keeper_cycle_secs_ago: None,
price_cycle_count: metrics.price_cycle_count,
keeper_cycle_count: metrics.keeper_cycle_count,
token_fetch_failures: metrics.token_fetch_failures,
submit_failures: metrics.submit_failures,
}))
}
async fn perform_external_ready_checks(state: &AppState) -> Result<(), ApiError> {
// Check RPC reachability
let rpc_url = &state.config.stellar_rpc_url;
let response = state
.http
.get(rpc_url)
.send()
.await
.map_err(|_| ApiError::new(StatusCode::SERVICE_UNAVAILABLE, "rpc_unreachable"))?;
if !response.status().is_success() {
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"rpc_unhealthy",
));
}
// Check keeper balance
let keeper_cfg = crate::keeper::KeeperBalanceConfig {
horizon_url: state.config.horizon_url.clone(),
account_id: state.config.keeper_account_id.clone(),
min_balance_xlm: state.config.min_keeper_balance_xlm,
};
match check_keeper_balance_for_ready(&keeper_cfg).await {
Ok(_) => {}
Err(crate::stellar_rpc::RpcError::BalanceBelowMinimum { .. }) => {
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"keeper_balance_low",
));
}
Err(error) => {
tracing::warn!(error = %error, "keeper balance check failed");
return Err(ApiError::new(
StatusCode::SERVICE_UNAVAILABLE,
"keeper_balance_check_failed",
));
}
}
Ok(())
}
async fn check_keeper_balance_for_ready(
keeper_cfg: &crate::keeper::KeeperBalanceConfig,
) -> Result<i64, crate::stellar_rpc::RpcError> {
let mut last_error = None;
for attempt in 1..=READY_BALANCE_RETRY_ATTEMPTS {
match crate::keeper::check_keeper_balance(keeper_cfg).await {
Ok(stroops) => return Ok(stroops),
Err(error @ crate::stellar_rpc::RpcError::BalanceBelowMinimum { .. }) => {
return Err(error);
}
Err(error) => {
tracing::warn!(
attempt,
max_attempts = READY_BALANCE_RETRY_ATTEMPTS,
error = %error,
"ready keeper balance attempt failed"
);
last_error = Some(error);
if attempt < READY_BALANCE_RETRY_ATTEMPTS {
tokio::time::sleep(Duration::from_millis(
READY_BALANCE_RETRY_BASE_DELAY_MS * 2_u64.pow(attempt - 1),
))
.await;
}
}
}
}
Err(last_error.expect("READY_BALANCE_RETRY_ATTEMPTS is greater than zero"))
}
pub async fn prices(
State(state): State<Arc<AppState>>,
) -> Result<Json<Vec<CachedPrice>>, ApiError> {
let cache = state.price_cache.read().await;
if cache.prices.is_empty() {
return Err(ApiError::new(StatusCode::SERVICE_UNAVAILABLE, "no_prices"));
}
Ok(Json(cache.prices.values().cloned().collect()))
}
pub async fn failed_submissions(
_auth: AdminAuth,
query: FailedSubmissionsQuery,
State(state): State<Arc<AppState>>,
) -> Json<FailuresResponse> {
let all_failures = state.failures.lock().await;
let failures_iter = all_failures.iter().rev();
let filtered: Vec<FailedSubmission> = match &query.operation {
Some(op) => failures_iter
.filter(|f| f.operation.starts_with(op.as_str()))
.cloned()
.collect(),
None => failures_iter.cloned().collect(),
};
let total_count = filtered.len();
let limit = query.limit.unwrap_or(100).min(256);
let failures: Vec<_> = filtered.into_iter().take(limit).collect();
Json(FailuresResponse {
failures,
total_count,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{AppState, Config};
fn test_state() -> Arc<AppState> {
let config = Arc::new(Config::default_for_tests());
Arc::new(AppState::new(config))
}
// #339 — GET /health must return 200 with {"status":"ok"}, no auth required
// #497 — health now also surfaces cycle timestamps and failure counters
#[tokio::test]
async fn health_returns_status_ok() {
let state = test_state();
let Json(body) = health(State(state)).await;
assert_eq!(body.status, "ok");
// No cycles have run yet — counts are zero and timestamps are absent
assert_eq!(body.price_cycle_count, 0);
assert_eq!(body.keeper_cycle_count, 0);
assert!(body.last_price_cycle_secs_ago.is_none());
assert!(body.last_keeper_cycle_secs_ago.is_none());
}
}