Skip to content

Commit 0939e11

Browse files
committed
[ft.] lightmode block fetch and decode.
[Enh.] Deser. Block Hex Data [Fix.] Iter support [Fmt.] Cargo format. Linter Fix [Clairty] removed unneeded comment txids -> txid_vec
1 parent 4d1fdaf commit 0939e11

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/new_index/schema.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,20 +518,20 @@ impl ChainQuery {
518518
pub fn get_block_txs(&self, hash: &BlockHash) -> Option<Vec<Transaction>> {
519519
let _timer = self.start_timer("get_block_txs");
520520

521-
let txids: Option<Vec<Txid>> = if self.light_mode {
522-
// TODO fetch block as binary from REST API instead of as hex
523-
let mut blockinfo = self.daemon.getblock_raw(hash, 1).ok()?;
524-
Some(serde_json::from_value(blockinfo["tx"].take()).unwrap())
521+
if self.light_mode {
522+
hex::decode(self.daemon.getblock_raw(hash, 0).ok()?.as_str()?)
523+
.ok()
524+
.and_then(|block_bytes| deserialize::<crate::chain::Block>(&block_bytes).ok())
525+
.map(|block| block.txdata)
525526
} else {
526-
self.store
527+
let txid_vec: Vec<Txid> = self
528+
.store
527529
.txstore_db
528530
.get(&BlockRow::txids_key(full_hash(&hash[..])))
529531
.map(|val| {
530532
bincode_util::deserialize_little(&val).expect("failed to parse block txids")
531-
})
532-
};
533+
})?;
533534

534-
txids.and_then(|txid_vec| {
535535
let mut transactions = Vec::with_capacity(txid_vec.len());
536536

537537
for txid in txid_vec {
@@ -542,7 +542,7 @@ impl ChainQuery {
542542
}
543543

544544
Some(transactions)
545-
})
545+
}
546546
}
547547

548548
pub fn get_block_meta(&self, hash: &BlockHash) -> Option<BlockMeta> {

src/rest.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,20 @@ fn handle_request(
783783
(&Method::GET, Some(&INTERNAL_PREFIX), Some(&"block"), Some(hash), Some(&"txs"), None) => {
784784
let hash = BlockHash::from_hex(hash)?;
785785
let block_id = query.chain().blockid_by_hash(&hash);
786+
787+
let block_id =
788+
Some(block_id.ok_or_else(|| {
789+
HttpError::not_found("Block with hash not found".to_string())
790+
})?);
791+
786792
let txs = query
787793
.chain()
788794
.get_block_txs(&hash)
789-
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?
795+
.ok_or_else(|| {
796+
HttpError::not_found(
797+
"Transactions could not be queried from block.".to_string(),
798+
)
799+
})?
790800
.into_iter()
791801
.map(|tx| (tx, block_id.clone()))
792802
.collect();

0 commit comments

Comments
 (0)