Rewrite query and RPC functionality using new index API

This commit is contained in:
Roman Zeyde 2018-05-30 21:34:52 +03:00
parent 14ae2a6b71
commit ca21ba837d
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 19 additions and 24 deletions

View File

@ -1,4 +1,4 @@
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::block::Block;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::network::serialize::deserialize;
use bitcoin::util::hash::Sha256dHash;
@ -253,23 +253,16 @@ impl Query {
self.app.daemon().gettransaction(tx_hash)
}
pub fn get_headers(&self, heights: &[usize]) -> Vec<BlockHeader> {
let headers_list = self.app.index().headers_list();
let headers = headers_list.headers();
let mut result = vec![];
for height in heights {
let header: &BlockHeader = match headers.get(*height) {
Some(header) => header.header(),
None => break,
};
result.push(*header);
}
result
pub fn get_headers(&self, heights: &[usize]) -> Vec<HeaderEntry> {
let index = self.app.index();
heights
.iter()
.filter_map(|height| index.get_header(*height))
.collect()
}
pub fn get_best_header(&self) -> Result<HeaderEntry> {
let header_list = self.app.index().headers_list();
let last_header = header_list.headers().last();
let last_header = self.app.index().best_header();
Ok(last_header.chain_err(|| "no headers indexed")?.clone())
}
@ -278,13 +271,11 @@ impl Query {
tx_hash: &Sha256dHash,
height: usize,
) -> Result<(Vec<Sha256dHash>, usize)> {
let header_list = self.app.index().headers_list();
let blockhash = header_list
.headers()
.get(height)
.chain_err(|| format!("missing block #{}", height))?
.hash();
let block: Block = self.app.daemon().getblock(&blockhash).unwrap();
let header_entry = self.app
.index()
.get_header(height)
.chain_err(|| format!("missing block #{}", height))?;
let block: Block = self.app.daemon().getblock(&header_entry.hash())?;
let mut txids: Vec<Sha256dHash> = block.txdata.iter().map(|tx| tx.txid()).collect();
let pos = txids
.iter()

View File

@ -99,8 +99,12 @@ impl Connection {
fn blockchain_block_get_header(&self, params: &[Value]) -> Result<Value> {
let height = params.get(0).chain_err(|| "missing height")?;
let height = height.as_u64().chain_err(|| "non-number height")? as usize;
let headers = self.query.get_headers(&[height]);
Ok(json!(jsonify_header(&headers[0], height)))
let mut entries = self.query.get_headers(&[height]);
let entry = entries
.pop()
.chain_err(|| format!("missing header #{}", height))?;
assert_eq!(entries.len(), 0);
Ok(json!(jsonify_header(&entry)))
}
fn blockchain_estimatefee(&self, params: &[Value]) -> Result<Value> {