Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/chaincash_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ impl ChainCashApp {

let node = node_from_config(&self.config.node)?;

let state = Arc::new(ServerState::new(node, store, predicates));
let state = Arc::new(ServerState::new(
node,
self.config.node.api_key(),
store,
predicates,
)?);
start_scanner(state.clone()).await.unwrap();
Ok(Server::serve(listener, state).await?)
}
Expand Down
6 changes: 6 additions & 0 deletions crates/chaincash_offchain/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct Config {
api_key: String,
}

impl Config {
pub fn api_key(&self) -> &str {
&self.api_key
}
}

pub fn node_from_config(cfg: &Config) -> Result<NodeClient, NodeError> {
Ok(NodeClient::from_url_str(
&cfg.url,
Expand Down
1 change: 0 additions & 1 deletion crates/chaincash_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ ergo-lib = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
ergo_client = { workspace = true }
reqwest = "0.11"
14 changes: 9 additions & 5 deletions crates/chaincash_server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ mod tests {
)
.unwrap();

Arc::new(ServerState::new(
node,
ChainCashStore::open_in_memory().unwrap(),
vec![],
))
Arc::new(
ServerState::new(
node,
"hello",
ChainCashStore::open_in_memory().unwrap(),
vec![],
)
.unwrap(),
)
}
#[tokio::test]
async fn test_healthcheck() {
Expand Down
66 changes: 34 additions & 32 deletions crates/chaincash_server/src/healthcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,45 @@ use chaincash_services::ServerState;
use serde_json::{json, Value};
use std::sync::Arc;

fn internal_error(e: impl std::fmt::Display) -> (StatusCode, String) {
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
}

pub async fn healthcheck(
State(state): State<Arc<ServerState>>,
) -> Result<Json<Value>, (StatusCode, String)> {
let url = state.node.endpoints().url();
let url2 = url.join("blockchain/indexedHeight").map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to join URL: {}", e),
)
})?;
let response = reqwest::get(url2)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to make request: {}", e),
)
})?
.text()
let info = state
.node
.endpoints()
.root()
.map_err(internal_error)?
.info()
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to get response text: {}", e),
)
})?;
.map_err(|e| internal_error(format!("Failed to reach node: {e}")))?;

let response_json: Value = serde_json::from_str(&response).map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Invalid JSON response: {}", e),
)
})?;
let combined = json!({
"status": "Node running",
"response": response_json
let mut response = json!({
"network": info.network,
"fullHeight": info.full_height,
"headersHeight": info.headers_height,
});

Ok(Json(combined))
// Extra indexing is optional. Report its progress if the node has it enabled
let indexed = state.chain.has_indexer().await.map_err(internal_error)?;
if indexed {
let indexed_height = state
.node
.endpoints()
.blockchain()
.map_err(internal_error)?
.indexed_height()
.await
.map_err(internal_error)?;
response["indexedHeight"] = indexed_height.indexed_height.into();
}
response["indexed"] = indexed.into();

Ok(Json(json!({
"status": "Node running",
"response": response
})))
}
2 changes: 2 additions & 0 deletions crates/chaincash_services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ license.workspace = true
[dependencies]
bs58 = "0.5.0"
serde = { workspace = true }
serde_json = { workspace = true }
ergo-lib = { workspace = true }
ergo_client = { workspace = true }
reqwest = { version = "0.11", features = ["json"] }
chaincash_store = { path = "../chaincash_store" }
chaincash_offchain = { path = "../chaincash_offchain" }
chaincash_predicate = { path = "../chaincash_predicate" }
Expand Down
Loading
Loading