Skip to content

Commit b31d4ae

Browse files
committed
rusk: Expose /stats/tx_count endpoint for transaction counts
1 parent 0dde758 commit b31d4ae

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

rusk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Add `status` to GQL block fields
1515
- Add activaction height for host queries
1616
- Add blob config section
17+
- add `/stats/tx_count` endpoint [#3625]
1718

1819
### Changed
1920

@@ -389,6 +390,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
389390
- Add build system that generates keys for circuits and caches them.
390391

391392
<!-- Issues -->
393+
[#3625]: https://github.com/dusk-network/rusk/issues/3625
392394
[#3613]: https://github.com/dusk-network/rusk/issues/3613
393395
[#3512]: https://github.com/dusk-network/rusk/issues/3512
394396
[#3470]: https://github.com/dusk-network/rusk/issues/3470

rusk/src/lib/http/chain.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl HandleRequest for RuskNode {
6868
("blocks", _, "gas-price") => true,
6969
("blobs", Some(_), "commitment") => true,
7070
("blobs", Some(_), "hash") => true,
71+
("stats", _, "tx_count") => true,
7172

7273
_ => false,
7374
}
@@ -120,10 +121,14 @@ impl HandleRequest for RuskNode {
120121
.map_err(|_| anyhow::anyhow!("Invalid hash length"))?;
121122
self.blob_by_hash(&hash, request.is_json()).await
122123
}
124+
125+
("stats", _, "tx_count") => self.get_tx_count().await,
126+
123127
_ => anyhow::bail!("Unsupported"),
124128
}
125129
}
126130
}
131+
127132
impl RuskNode {
128133
async fn handle_gql(
129134
&self,
@@ -384,6 +389,34 @@ impl RuskNode {
384389
"next_nonce": next_nonce,
385390
})))
386391
}
392+
393+
/// Returns the total number of finalized transactions observed in the
394+
/// archive, split into `public`, `shielded` and `total. The response is
395+
/// a JSON object:
396+
/// ```json
397+
/// { "public": 123, "shielded": 456, "total": 579 }
398+
/// ```
399+
///
400+
/// # Errors
401+
/// Returns an error if the archive feature is not enabled.
402+
async fn get_tx_count(&self) -> anyhow::Result<ResponseData> {
403+
#[cfg(feature = "archive")]
404+
{
405+
let (moonlight, phoenix) = self.archive().fetch_tx_count().await?;
406+
let total = moonlight + phoenix;
407+
let body = serde_json::json!({
408+
"public": moonlight,
409+
"shielded": phoenix,
410+
"total": total
411+
});
412+
Ok(ResponseData::new(body))
413+
}
414+
415+
#[cfg(not(feature = "archive"))]
416+
{
417+
anyhow::bail!("The archive feature is required for this endpoint.");
418+
}
419+
}
387420
}
388421

389422
async fn load_tip<DB: database::DB>(

0 commit comments

Comments
 (0)