115 lines
3.9 KiB
Rust
115 lines
3.9 KiB
Rust
//! The pool routing surface of the prefix cache over HTTP: `GET /v1/status` carries the engine's summary and
|
|
//! `POST /v1/prefix_match` answers from it (`docs/prefix-cache.md`, "Pool protocol").
|
|
|
|
use std::sync::Arc;
|
|
use strata_control::app::ModelView;
|
|
|
|
use serde_json::{Value, json};
|
|
use strata_control::{
|
|
app::{AppState, router},
|
|
prefix_view::PrefixView,
|
|
};
|
|
|
|
fn summary() -> String {
|
|
json!({
|
|
"version": 1, "chain_version": 1, "namespace": "00000000000000ab", "block_tokens": 2048,
|
|
"budget_bytes": 1u64 << 30, "used_bytes": 1u64 << 20, "nodes": 2, "blocks": 6, "truncated": false,
|
|
"heads": [{"h": "00000000000000a1", "tokens": 4096}, {"h": "00000000000000a2", "tokens": 12288}],
|
|
"stats": {"requests": 3, "hits": 1, "shared_hits": 1, "misses": 2, "reused_tokens": 4096,
|
|
"prompt_tokens": 9000, "admitted": 2, "rejected": 0, "evicted": 0},
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
async fn get_json(client: &reqwest::Client, url: &str) -> Value {
|
|
serde_json::from_str(&client.get(url).send().await.unwrap().text().await.unwrap()).unwrap()
|
|
}
|
|
|
|
async fn post_json(client: &reqwest::Client, url: &str, body: Value) -> Value {
|
|
let text = client
|
|
.post(url)
|
|
.body(body.to_string())
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.text()
|
|
.await
|
|
.unwrap();
|
|
serde_json::from_str(&text).unwrap()
|
|
}
|
|
|
|
async fn serve(view: Arc<PrefixView>) -> String {
|
|
let state = AppState {
|
|
model: ModelView {
|
|
prefix_cache: view,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
};
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let url = format!("http://{}", listener.local_addr().unwrap());
|
|
tokio::spawn(async move { axum::serve(listener, router(state)).await });
|
|
url
|
|
}
|
|
|
|
/// no cache reported: null, and an honest "not available"
|
|
async fn assert_unavailable_without_a_summary(client: &reqwest::Client, url: &str) {
|
|
let status: Value = get_json(client, &format!("{url}/v1/status")).await;
|
|
assert_eq!(status["prefix_cache"], Value::Null);
|
|
let none = post_json(
|
|
client,
|
|
&format!("{url}/v1/prefix_match"),
|
|
json!({"block_hashes": ["00000000000000a1"]}),
|
|
)
|
|
.await;
|
|
assert_eq!(
|
|
(none["available"].as_bool(), none["cached_tokens"].as_u64()),
|
|
(Some(false), Some(0))
|
|
);
|
|
}
|
|
|
|
async fn assert_match_from_the_summary(client: &reqwest::Client, url: &str) {
|
|
let status: Value = get_json(client, &format!("{url}/v1/status")).await;
|
|
assert_eq!(status["prefix_cache"]["namespace"], "00000000000000ab");
|
|
assert_eq!(status["prefix_cache"]["heads"].as_array().unwrap().len(), 2);
|
|
|
|
let hashes = ["00000000000000a1", "00000000000000ff", "00000000000000a2"];
|
|
let answer = post_json(
|
|
client,
|
|
&format!("{url}/v1/prefix_match"),
|
|
json!({"namespace": "00000000000000ab", "block_hashes": hashes}),
|
|
)
|
|
.await;
|
|
assert_eq!(answer["cached_tokens"], 12288);
|
|
assert_eq!(answer["block_tokens"], 2048);
|
|
assert_eq!(answer["compatible"], true);
|
|
}
|
|
|
|
/// refusals: not JSON, not hashes, a hash of the wrong shape
|
|
async fn assert_bad_bodies_refused(client: &reqwest::Client, url: &str) {
|
|
for body in [
|
|
"not json",
|
|
r#"{"block_hashes": 5}"#,
|
|
r#"{"block_hashes": ["zz"]}"#,
|
|
] {
|
|
let response = client
|
|
.post(format!("{url}/v1/prefix_match"))
|
|
.body(body.to_owned())
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status().as_u16(), 400, "{body}");
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn status_shows_the_summary_and_prefix_match_answers_from_it() {
|
|
let view = Arc::new(PrefixView::default());
|
|
let url = serve(view.clone()).await;
|
|
let client = reqwest::Client::new();
|
|
assert_unavailable_without_a_summary(&client, &url).await;
|
|
view.set(Some(summary()));
|
|
assert_match_from_the_summary(&client, &url).await;
|
|
assert_bad_bodies_refused(&client, &url).await;
|
|
}
|