use essential_node::{self as node};
use essential_node_api as node_api;
use essential_node_types::block_notify::BlockTx;
use essential_types::{convert::bytes_from_word, Block, Value};
use futures::{StreamExt, TryStreamExt};
use tokio_util::{
bytes::{self, Buf},
codec::FramedRead,
io::StreamReader,
};
use util::{
client, get_url, init_tracing_subscriber, reqwest_get, state_db_only, test_conn_pool,
with_test_server,
};
mod util;
#[tokio::test]
async fn test_health_check() {
#[cfg(feature = "tracing")]
init_tracing_subscriber();
let db = test_conn_pool();
with_test_server(state_db_only(db), |port| async move {
let response = reqwest_get(port, node_api::endpoint::health_check::PATH).await;
assert!(response.status().is_success());
})
.await;
}
#[tokio::test]
async fn test_query_state() {
#[cfg(feature = "tracing")]
init_tracing_subscriber();
let db = test_conn_pool();
// Create some test blocks with state mutations.
let n_blocks = 100;
let (blocks, _) = node::test_utils::test_blocks(n_blocks);
let mut mutations = vec![];
// Insert them into the node's DB.
for block in &blocks {
let iter = block
.solutions
.iter()
.flat_map(|s| s.data.iter())
.flat_map(|d| {
d.state_mutations
.iter()
.cloned()
.map(|m| (d.predicate_to_solve.contract.clone(), m))
});
mutations.extend(iter);
let block_ca = db
.insert_block(std::sync::Arc::new(block.clone()))
.await
.unwrap();
// Make sure to finalize them.
db.finalize_block(block_ca).await.unwrap();
}
// Check state
with_test_server(state_db_only(db), |port| async move {
for (contract, m) in &mutations {
let key_bytes: Vec<_> = m.key.iter().copied().flat_map(bytes_from_word).collect();
let key = hex::encode(&key_bytes);
let response = client()
.get(get_url(
port,
&format!("/query-state/{contract}/{key}?block_inclusive={}", n_blocks),
))
.send()
.await
.unwrap();
assert!(
response.status().is_success(),
"{:?}",
response.text().await
);
let r = response.json::