-
Notifications
You must be signed in to change notification settings - Fork 58
feat(minibf): add block txs CBOR endpoints #1123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slowbackspace
wants to merge
2
commits into
main
Choose a base branch
from
feat/minibf-block-txs-cbor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ use axum::{ | |
| http::StatusCode, | ||
| Json, | ||
| }; | ||
| use blockfrost_openapi::models::block_content::BlockContent; | ||
| use blockfrost_openapi::models::{ | ||
| block_content::BlockContent, block_content_txs_cbor_inner::BlockContentTxsCborInner, | ||
| }; | ||
| use dolos_cardano::ChainSummary; | ||
| use dolos_core::{archive::Skippable as _, ArchiveStore as _, BlockBody, Domain}; | ||
| use futures::future::try_join_all; | ||
|
|
@@ -401,6 +403,34 @@ where | |
| )) | ||
| } | ||
|
|
||
| pub async fn by_hash_or_number_txs_cbor<D>( | ||
| Path(hash_or_number): Path<String>, | ||
| Query(params): Query<PaginationParameters>, | ||
| State(domain): State<Facade<D>>, | ||
| ) -> Result<Json<Vec<BlockContentTxsCborInner>>, Error> | ||
| where | ||
| D: Domain + Clone + Send + Sync + 'static, | ||
| { | ||
| let pagination = Pagination::try_from(params)?; | ||
| let hash_or_number = parse_hash_or_number(&hash_or_number)?; | ||
| let block = load_block_by_hash_or_number(&domain, &hash_or_number).await?; | ||
|
|
||
| let model = BlockModelBuilder::new(&block)?; | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = model.into_model()?; | ||
| let txs = match pagination.order { | ||
| Order::Asc => txs, | ||
| Order::Desc => txs.into_iter().rev().collect(), | ||
| }; | ||
|
|
||
| Ok(Json( | ||
| txs.into_iter() | ||
| .skip(pagination.skip()) | ||
| .take(pagination.count) | ||
| .collect(), | ||
| )) | ||
| } | ||
|
|
||
| pub async fn by_hash_or_number_addresses<D>( | ||
| Path(hash_or_number): Path<String>, | ||
| Query(params): Query<PaginationParameters>, | ||
|
|
@@ -459,6 +489,36 @@ where | |
| )) | ||
| } | ||
|
|
||
| pub async fn latest_txs_cbor<D>( | ||
| Query(params): Query<PaginationParameters>, | ||
| State(domain): State<Facade<D>>, | ||
| ) -> Result<Json<Vec<BlockContentTxsCborInner>>, Error> | ||
| where | ||
| D: Domain + Clone + Send + Sync + 'static, | ||
| { | ||
| let pagination = Pagination::try_from(params)?; | ||
| let (_, tip) = domain | ||
| .archive() | ||
| .get_tip() | ||
| .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? | ||
| .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; | ||
|
|
||
| let model = BlockModelBuilder::new(&tip)?; | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = model.into_model()?; | ||
| let txs = match pagination.order { | ||
| Order::Asc => txs, | ||
| Order::Desc => txs.into_iter().rev().collect(), | ||
| }; | ||
|
|
||
| Ok(Json( | ||
| txs.into_iter() | ||
| .skip(pagination.skip()) | ||
| .take(pagination.count) | ||
| .collect(), | ||
| )) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
@@ -570,6 +630,92 @@ mod tests { | |
| assert_eq!(txs, reversed); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_happy_path() { | ||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.first().expect("missing block vectors"); | ||
| let path = format!("/blocks/{}/txs/cbor", block.block_hash); | ||
| let (status, bytes) = app.get_bytes(&path).await; | ||
|
|
||
| assert_eq!( | ||
| status, | ||
| StatusCode::OK, | ||
| "unexpected status {status} with body: {}", | ||
| String::from_utf8_lossy(&bytes) | ||
| ); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse txs cbor"); | ||
|
|
||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| assert_eq!(hashes, block.tx_hashes); | ||
|
|
||
| for tx in txs { | ||
| let cbor = hex::decode(&tx.cbor).expect("cbor is not valid hex"); | ||
| let decoded = pallas::ledger::traverse::MultiEraTx::decode(&cbor) | ||
| .expect("cbor is not a decodable tx"); | ||
| assert_eq!(decoded.hash().to_string(), tx.tx_hash); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_order_desc() { | ||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.first().expect("missing block vectors"); | ||
| let path = format!("/blocks/{}/txs/cbor?order=desc", block.block_hash); | ||
| let (status, bytes) = app.get_bytes(&path).await; | ||
| assert_eq!(status, StatusCode::OK); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse desc txs cbor"); | ||
|
|
||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| let mut reversed = block.tx_hashes.clone(); | ||
| reversed.reverse(); | ||
| assert_eq!(hashes, reversed); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_paginated() { | ||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.first().expect("missing block vectors"); | ||
| let path = format!("/blocks/{}/txs/cbor?count=1&page=2", block.block_hash); | ||
| let (status, bytes) = app.get_bytes(&path).await; | ||
| assert_eq!(status, StatusCode::OK); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse paginated txs cbor"); | ||
|
|
||
| let expected: Vec<String> = block.tx_hashes.iter().skip(1).take(1).cloned().collect(); | ||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| assert_eq!(hashes, expected); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_bad_request() { | ||
| let app = TestApp::new(); | ||
| let path = format!("/blocks/{}/txs/cbor", invalid_block()); | ||
| assert_status(&app, &path, StatusCode::BAD_REQUEST).await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_not_found() { | ||
| let app = TestApp::new(); | ||
| let path = format!("/blocks/{}/txs/cbor", missing_block()); | ||
| assert_status(&app, &path, StatusCode::NOT_FOUND).await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_by_hash_or_number_txs_cbor_internal_error() { | ||
| let app = TestApp::new_with_fault(Some(TestFault::ArchiveStoreError)); | ||
| assert_status( | ||
| &app, | ||
| "/blocks/1/txs/cbor", | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| ) | ||
| .await; | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_latest_txs_order_asc() { | ||
| let app = TestApp::new(); | ||
|
|
@@ -595,4 +741,75 @@ mod tests { | |
| reversed.reverse(); | ||
| assert_eq!(txs, reversed); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_latest_txs_cbor_happy_path() { | ||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.last().expect("missing block vectors"); | ||
| let (status, bytes) = app.get_bytes("/blocks/latest/txs/cbor").await; | ||
|
|
||
| assert_eq!( | ||
| status, | ||
| StatusCode::OK, | ||
| "unexpected status {status} with body: {}", | ||
| String::from_utf8_lossy(&bytes) | ||
| ); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse latest txs cbor"); | ||
|
|
||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| assert_eq!(hashes, block.tx_hashes); | ||
|
|
||
| for tx in txs { | ||
| let cbor = hex::decode(&tx.cbor).expect("cbor is not valid hex"); | ||
| let decoded = pallas::ledger::traverse::MultiEraTx::decode(&cbor) | ||
| .expect("cbor is not a decodable tx"); | ||
| assert_eq!(decoded.hash().to_string(), tx.tx_hash); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_latest_txs_cbor_order_desc() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little asymmetry --
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — added |
||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.last().expect("missing block vectors"); | ||
| let (status, bytes) = app.get_bytes("/blocks/latest/txs/cbor?order=desc").await; | ||
| assert_eq!(status, StatusCode::OK); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse desc latest txs cbor"); | ||
|
|
||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| let mut reversed = block.tx_hashes.clone(); | ||
| reversed.reverse(); | ||
| assert_eq!(hashes, reversed); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_latest_txs_cbor_paginated() { | ||
| let app = TestApp::new(); | ||
| let block = app.vectors().blocks.last().expect("missing block vectors"); | ||
| let (status, bytes) = app | ||
| .get_bytes("/blocks/latest/txs/cbor?count=1&page=2") | ||
| .await; | ||
| assert_eq!(status, StatusCode::OK); | ||
|
|
||
| let txs: Vec<BlockContentTxsCborInner> = | ||
| serde_json::from_slice(&bytes).expect("failed to parse paginated latest txs cbor"); | ||
|
|
||
| let expected: Vec<String> = block.tx_hashes.iter().skip(1).take(1).cloned().collect(); | ||
| let hashes: Vec<String> = txs.iter().map(|tx| tx.tx_hash.clone()).collect(); | ||
| assert_eq!(hashes, expected); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn blocks_latest_txs_cbor_internal_error() { | ||
| let app = TestApp::new_with_fault(Some(TestFault::ArchiveStoreError)); | ||
| assert_status( | ||
| &app, | ||
| "/blocks/latest/txs/cbor", | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| ) | ||
| .await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description says "no re-encoding takes place", but
txis a&MultiEraTx<'_>, and in the definition of itsencode()I see severalminicbor::*calls. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we're
hex::encode’ing all elements even before pagination's.skip(…).take(…)above, but that's probably cheap enough?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right — "no re-encoding" was imprecise, reworded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah — deliberately kept for symmetry with the sibling
/txshandlers, which also materialize the full list beforeskip/take. Worst case is a few hundred txs at a few KB each, so the wasted hex is on the order of a megabyte of throwaway allocation per request — micro/milliseconds next to the archive read. If it ever shows up in a profile the fix is to slice the tx list before mapping, but that felt like premature restructuring here.