Skip to content

Commit 3c66022

Browse files
authored
Merge pull request Blockstream#24 from mempool/mononaut/bulk-block-txs
Bulk block txs
2 parents 682b10b + d688a8b commit 3c66022

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/new_index/schema.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,34 @@ impl ChainQuery {
371371
}
372372
}
373373

374+
pub fn get_block_txs(&self, hash: &BlockHash) -> Option<Vec<Transaction>> {
375+
let _timer = self.start_timer("get_block_txs");
376+
377+
let txids: Option<Vec<Txid>> = if self.light_mode {
378+
// TODO fetch block as binary from REST API instead of as hex
379+
let mut blockinfo = self.daemon.getblock_raw(hash, 1).ok()?;
380+
Some(serde_json::from_value(blockinfo["tx"].take()).unwrap())
381+
} else {
382+
self.store
383+
.txstore_db
384+
.get(&BlockRow::txids_key(full_hash(&hash[..])))
385+
.map(|val| bincode::deserialize(&val).expect("failed to parse block txids"))
386+
};
387+
388+
txids.and_then(|txid_vec| {
389+
let mut transactions = Vec::with_capacity(txid_vec.len());
390+
391+
for txid in txid_vec {
392+
match self.lookup_txn(&txid, Some(hash)) {
393+
Some(transaction) => transactions.push(transaction),
394+
None => return None,
395+
}
396+
}
397+
398+
Some(transactions)
399+
})
400+
}
401+
374402
pub fn get_block_meta(&self, hash: &BlockHash) -> Option<BlockMeta> {
375403
let _timer = self.start_timer("get_block_meta");
376404

src/rest.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,18 @@ fn handle_request(
699699
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?;
700700
json_response(txids, TTL_LONG)
701701
}
702+
(&Method::GET, Some(&"block"), Some(hash), Some(&"txs"), None, None) => {
703+
let hash = BlockHash::from_hex(hash)?;
704+
let txs = query
705+
.chain()
706+
.get_block_txs(&hash)
707+
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?
708+
.into_iter()
709+
.map(|tx| (tx, None))
710+
.collect();
711+
712+
json_maybe_error_response(prepare_txs(txs, query, config), TTL_SHORT)
713+
}
702714
(&Method::GET, Some(&"block"), Some(hash), Some(&"header"), None, None) => {
703715
let hash = BlockHash::from_hex(hash)?;
704716
let header = query

0 commit comments

Comments
 (0)