Skip to content

Commit b228345

Browse files
committed
Add missing paging parameters to queries (still need implementing)
1 parent 651fc55 commit b228345

File tree

6 files changed

+148
-22
lines changed

6 files changed

+148
-22
lines changed

common/src/queries/blocks.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{serialization::Bech32WithHrp, BlockHash, KeyHash, TxHash};
1+
use crate::{queries::misc::Order, serialization::Bech32WithHrp, BlockHash, KeyHash, TxHash};
22
use cryptoxide::hashing::blake2b::Blake2b;
33
use serde::ser::{Serialize, SerializeStruct, Serializer};
44
use serde_with::{hex::Hex, serde_as};
@@ -9,10 +9,16 @@ pub const DEFAULT_BLOCKS_QUERY_TOPIC: (&str, &str) =
99
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
1010
pub enum BlocksStateQuery {
1111
GetLatestBlock,
12-
// TODO: add paging
13-
GetLatestBlockTransactions,
14-
// TODO: add paging
15-
GetLatestBlockTransactionsCBOR,
12+
GetLatestBlockTransactions {
13+
limit: u64,
14+
skip: u64,
15+
order: Order,
16+
},
17+
GetLatestBlockTransactionsCBOR {
18+
limit: u64,
19+
skip: u64,
20+
order: Order,
21+
},
1622
GetBlockInfo {
1723
block_key: BlockKey,
1824
},
@@ -35,12 +41,16 @@ pub enum BlocksStateQuery {
3541
},
3642
GetBlockTransactions {
3743
block_key: BlockKey,
44+
limit: u64,
45+
skip: u64,
46+
order: Order,
3847
},
39-
// TODO: add paging
4048
GetBlockTransactionsCBOR {
4149
block_key: BlockKey,
50+
limit: u64,
51+
skip: u64,
52+
order: Order,
4253
},
43-
// TODO: add paging
4454
GetBlockInvolvedAddresses {
4555
block_key: BlockKey,
4656
},

common/src/queries/misc.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::str::FromStr;
2+
3+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4+
pub enum Order {
5+
Asc,
6+
Desc,
7+
}
8+
9+
impl FromStr for Order {
10+
type Err = ();
11+
12+
fn from_str(s: &str) -> Result<Self, Self::Err> {
13+
match s.to_lowercase().as_str() {
14+
"asc" => Ok(Order::Asc),
15+
"desc" => Ok(Order::Desc),
16+
_ => Err(()),
17+
}
18+
}
19+
}

common/src/queries/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod governance;
1111
pub mod ledger;
1212
pub mod mempool;
1313
pub mod metadata;
14+
pub mod misc;
1415
pub mod network;
1516
pub mod parameters;
1617
pub mod pools;

modules/chain_store/src/chain_store.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,24 @@ impl ChainStore {
9393
let info = Self::to_block_info(block, store, true)?;
9494
Ok(BlocksStateQueryResponse::LatestBlock(info))
9595
}
96-
BlocksStateQuery::GetLatestBlockTransactions => {
96+
BlocksStateQuery::GetLatestBlockTransactions {
97+
// TODO: apply these parameters
98+
limit: _,
99+
skip: _,
100+
order: _,
101+
} => {
97102
let Some(block) = store.get_latest_block()? else {
98103
return Ok(BlocksStateQueryResponse::NotFound);
99104
};
100105
let txs = Self::to_block_transactions(block)?;
101106
Ok(BlocksStateQueryResponse::LatestBlockTransactions(txs))
102107
}
103-
BlocksStateQuery::GetLatestBlockTransactionsCBOR => {
108+
BlocksStateQuery::GetLatestBlockTransactionsCBOR {
109+
// TODO: apply these parameters
110+
limit: _,
111+
skip: _,
112+
order: _,
113+
} => {
104114
let Some(block) = store.get_latest_block()? else {
105115
return Ok(BlocksStateQueryResponse::NotFound);
106116
};
@@ -176,14 +186,26 @@ impl ChainStore {
176186
blocks: info,
177187
}))
178188
}
179-
BlocksStateQuery::GetBlockTransactions { block_key } => {
189+
BlocksStateQuery::GetBlockTransactions {
190+
block_key,
191+
// TODO: apply these parameters
192+
limit: _,
193+
skip: _,
194+
order: _,
195+
} => {
180196
let Some(block) = Self::get_block_by_key(store, block_key)? else {
181197
return Ok(BlocksStateQueryResponse::NotFound);
182198
};
183199
let txs = Self::to_block_transactions(block)?;
184200
Ok(BlocksStateQueryResponse::BlockTransactions(txs))
185201
}
186-
BlocksStateQuery::GetBlockTransactionsCBOR { block_key } => {
202+
BlocksStateQuery::GetBlockTransactionsCBOR {
203+
block_key,
204+
// TODO: apply these parameters
205+
limit: _,
206+
skip: _,
207+
order: _,
208+
} => {
187209
let Some(block) = Self::get_block_by_key(store, block_key)? else {
188210
return Ok(BlocksStateQueryResponse::NotFound);
189211
};

modules/rest_blockfrost/src/handlers/blocks.rs

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use acropolis_common::{
44
messages::{Message, RESTResponse, StateQuery, StateQueryResponse},
55
queries::{
66
blocks::{BlockKey, BlocksStateQuery, BlocksStateQueryResponse},
7+
misc::Order,
78
utils::query_state,
89
},
910
BlockHash,
@@ -147,28 +148,58 @@ async fn handle_blocks_hash_number_blockfrost(
147148
pub async fn handle_blocks_latest_hash_number_transactions_blockfrost(
148149
context: Arc<Context<Message>>,
149150
params: Vec<String>,
151+
query_params: HashMap<String, String>,
150152
handlers_config: Arc<HandlersConfig>,
151153
) -> Result<RESTResponse> {
152154
let param = match params.as_slice() {
153155
[param] => param,
154156
_ => return Ok(RESTResponse::with_text(400, "Invalid parameters")),
155157
};
156158

159+
extract_strict_query_params!(query_params, {
160+
"count" => limit: Option<u64>,
161+
"page" => page: Option<u64>,
162+
"order" => order: Option<Order>,
163+
});
164+
let limit = limit.unwrap_or(100);
165+
let skip = (page.unwrap_or(1) - 1) * limit;
166+
let order = order.unwrap_or(Order::Asc);
167+
157168
match param.as_str() {
158-
"latest" => handle_blocks_latest_transactions_blockfrost(context, handlers_config).await,
169+
"latest" => {
170+
handle_blocks_latest_transactions_blockfrost(
171+
context,
172+
limit,
173+
skip,
174+
order,
175+
handlers_config,
176+
)
177+
.await
178+
}
159179
_ => {
160-
handle_blocks_hash_number_transactions_blockfrost(context, param, handlers_config).await
180+
handle_blocks_hash_number_transactions_blockfrost(
181+
context,
182+
param,
183+
limit,
184+
skip,
185+
order,
186+
handlers_config,
187+
)
188+
.await
161189
}
162190
}
163191
}
164192

165193
/// Handle `/blocks/latest/txs`
166194
async fn handle_blocks_latest_transactions_blockfrost(
167195
context: Arc<Context<Message>>,
196+
limit: u64,
197+
skip: u64,
198+
order: Order,
168199
handlers_config: Arc<HandlersConfig>,
169200
) -> Result<RESTResponse> {
170201
let blocks_latest_txs_msg = Arc::new(Message::StateQuery(StateQuery::Blocks(
171-
BlocksStateQuery::GetLatestBlockTransactions,
202+
BlocksStateQuery::GetLatestBlockTransactions { limit, skip, order },
172203
)));
173204
let block_txs = query_state(
174205
&context,
@@ -207,6 +238,9 @@ async fn handle_blocks_latest_transactions_blockfrost(
207238
async fn handle_blocks_hash_number_transactions_blockfrost(
208239
context: Arc<Context<Message>>,
209240
hash_or_number: &str,
241+
limit: u64,
242+
skip: u64,
243+
order: Order,
210244
handlers_config: Arc<HandlersConfig>,
211245
) -> Result<RESTResponse> {
212246
let block_key = match parse_block_key(hash_or_number) {
@@ -215,7 +249,12 @@ async fn handle_blocks_hash_number_transactions_blockfrost(
215249
};
216250

217251
let block_txs_msg = Arc::new(Message::StateQuery(StateQuery::Blocks(
218-
BlocksStateQuery::GetBlockTransactions { block_key },
252+
BlocksStateQuery::GetBlockTransactions {
253+
block_key,
254+
limit,
255+
skip,
256+
order,
257+
},
219258
)));
220259
let block_txs = query_state(
221260
&context,
@@ -260,31 +299,58 @@ async fn handle_blocks_hash_number_transactions_blockfrost(
260299
pub async fn handle_blocks_latest_hash_number_transactions_cbor_blockfrost(
261300
context: Arc<Context<Message>>,
262301
params: Vec<String>,
302+
query_params: HashMap<String, String>,
263303
handlers_config: Arc<HandlersConfig>,
264304
) -> Result<RESTResponse> {
265305
let param = match params.as_slice() {
266306
[param] => param,
267307
_ => return Ok(RESTResponse::with_text(400, "Invalid parameters")),
268308
};
269309

310+
extract_strict_query_params!(query_params, {
311+
"count" => limit: Option<u64>,
312+
"page" => page: Option<u64>,
313+
"order" => order: Option<Order>,
314+
});
315+
let limit = limit.unwrap_or(100);
316+
let skip = (page.unwrap_or(1) - 1) * limit;
317+
let order = order.unwrap_or(Order::Asc);
318+
270319
match param.as_str() {
271320
"latest" => {
272-
handle_blocks_latest_transactions_cbor_blockfrost(context, handlers_config).await
321+
handle_blocks_latest_transactions_cbor_blockfrost(
322+
context,
323+
limit,
324+
skip,
325+
order,
326+
handlers_config,
327+
)
328+
.await
273329
}
274330
_ => {
275-
handle_blocks_hash_number_transactions_cbor_blockfrost(context, param, handlers_config)
276-
.await
331+
handle_blocks_hash_number_transactions_cbor_blockfrost(
332+
context,
333+
param,
334+
limit,
335+
skip,
336+
order,
337+
handlers_config,
338+
)
339+
.await
277340
}
278341
}
279342
}
280343

281344
/// Handle `/blocks/latest/txs/cbor`
282345
async fn handle_blocks_latest_transactions_cbor_blockfrost(
283346
context: Arc<Context<Message>>,
347+
limit: u64,
348+
skip: u64,
349+
order: Order,
284350
handlers_config: Arc<HandlersConfig>,
285351
) -> Result<RESTResponse> {
286352
let blocks_latest_txs_msg = Arc::new(Message::StateQuery(StateQuery::Blocks(
287-
BlocksStateQuery::GetLatestBlockTransactionsCBOR,
353+
BlocksStateQuery::GetLatestBlockTransactionsCBOR { limit, skip, order },
288354
)));
289355
let block_txs_cbor = query_state(
290356
&context,
@@ -323,6 +389,9 @@ async fn handle_blocks_latest_transactions_cbor_blockfrost(
323389
async fn handle_blocks_hash_number_transactions_cbor_blockfrost(
324390
context: Arc<Context<Message>>,
325391
hash_or_number: &str,
392+
limit: u64,
393+
skip: u64,
394+
order: Order,
326395
handlers_config: Arc<HandlersConfig>,
327396
) -> Result<RESTResponse> {
328397
let block_key = match parse_block_key(hash_or_number) {
@@ -331,7 +400,12 @@ async fn handle_blocks_hash_number_transactions_cbor_blockfrost(
331400
};
332401

333402
let block_txs_cbor_msg = Arc::new(Message::StateQuery(StateQuery::Blocks(
334-
BlocksStateQuery::GetBlockTransactionsCBOR { block_key },
403+
BlocksStateQuery::GetBlockTransactionsCBOR {
404+
block_key,
405+
limit,
406+
skip,
407+
order,
408+
},
335409
)));
336410
let block_txs_cbor = query_state(
337411
&context,

modules/rest_blockfrost/src/rest_blockfrost.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ impl BlockfrostREST {
236236
);
237237

238238
// Handler for /blocks/latest/txs, /blocks/{hash_or_number}/txs
239-
register_handler(
239+
register_handler_with_query(
240240
context.clone(),
241241
DEFAULT_HANDLE_BLOCKS_LATEST_HASH_NUMBER_TRANSACTIONS_TOPIC,
242242
handlers_config.clone(),
243243
handle_blocks_latest_hash_number_transactions_blockfrost,
244244
);
245245

246246
// Handler for /blocks/latest/txs/cbor, /blocks/{hash_or_number}/txs/cbor
247-
register_handler(
247+
register_handler_with_query(
248248
context.clone(),
249249
DEFAULT_HANDLE_BLOCKS_LATEST_HASH_NUMBER_TRANSACTIONS_CBOR_TOPIC,
250250
handlers_config.clone(),

0 commit comments

Comments
 (0)