mirror of
https://github.com/romanz/electrs.git
synced 2025-02-24 23:08:39 +01:00
Simplify latest headers' retrieval
This commit is contained in:
parent
548de46410
commit
313420b0e1
3 changed files with 14 additions and 20 deletions
|
@ -288,30 +288,24 @@ impl Daemon {
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_missing_headers(&self, mut header_map: HeaderMap) -> Result<(HeaderMap, Sha256dHash)> {
|
pub fn get_latest_headers(&self, indexed_headers: &HeaderList) -> Result<HeaderList> {
|
||||||
|
let mut header_map = if indexed_headers.headers().is_empty() {
|
||||||
|
self.get_all_headers()
|
||||||
|
.chain_err(|| "failed to download all headers")?
|
||||||
|
} else {
|
||||||
|
indexed_headers.as_map()
|
||||||
|
};
|
||||||
// Get current best blockhash (using JSONRPC API)
|
// Get current best blockhash (using JSONRPC API)
|
||||||
let bestblockhash = self.getbestblockhash()?;
|
let bestblockhash = self.getbestblockhash()?;
|
||||||
// Iterate back over headers until known blockash is found:
|
// Iterate back over headers until known blockash is found:
|
||||||
let mut blockhash = bestblockhash;
|
let mut blockhash = bestblockhash;
|
||||||
while !header_map.contains_key(&blockhash) {
|
while !header_map.contains_key(&blockhash) {
|
||||||
let header = self.getblockheader(&blockhash)
|
let header = self.getblockheader(&blockhash)
|
||||||
.chain_err(|| format!("failed to get missing header for {}", blockhash))?;
|
.chain_err(|| format!("failed to get {} header", blockhash))?;
|
||||||
debug!("downloaded {} block header", blockhash);
|
debug!("downloaded {} block header", blockhash);
|
||||||
header_map.insert(blockhash, header);
|
header_map.insert(blockhash, header);
|
||||||
blockhash = header.prev_blockhash;
|
blockhash = header.prev_blockhash;
|
||||||
}
|
}
|
||||||
Ok((header_map, bestblockhash))
|
Ok(HeaderList::build(header_map, bestblockhash))
|
||||||
}
|
|
||||||
|
|
||||||
pub fn enumerate_headers(&self, indexed_headers: &HeaderList) -> Result<HeaderList> {
|
|
||||||
let header_map = if indexed_headers.headers().is_empty() {
|
|
||||||
self.get_all_headers()
|
|
||||||
.chain_err(|| "failed to download all headers")?
|
|
||||||
} else {
|
|
||||||
indexed_headers.as_map()
|
|
||||||
};
|
|
||||||
let (header_map, blockhash) = self.add_missing_headers(header_map)
|
|
||||||
.chain_err(|| "failed to add missing headers")?;
|
|
||||||
Ok(HeaderList::build(header_map, blockhash))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,16 +360,16 @@ impl Index {
|
||||||
read_last_indexed_blockhash(store),
|
read_last_indexed_blockhash(store),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let current_headers = daemon.enumerate_headers(&*indexed_headers)?;
|
let latest_headers = daemon.get_latest_headers(&*indexed_headers)?;
|
||||||
for rows in Batching::new(Indexer::new(
|
for rows in Batching::new(Indexer::new(
|
||||||
current_headers.get_missing_headers(&indexed_headers.as_map()),
|
latest_headers.get_missing_headers(&indexed_headers.as_map()),
|
||||||
&daemon,
|
&daemon,
|
||||||
/*use_progress_bar=*/ no_indexed_headers,
|
/*use_progress_bar=*/ no_indexed_headers,
|
||||||
)) {
|
)) {
|
||||||
store.write(rows);
|
store.write(rows);
|
||||||
}
|
}
|
||||||
let tip = current_headers.tip();
|
let tip = latest_headers.tip();
|
||||||
*(self.headers.write().unwrap()) = Arc::new(current_headers);
|
*(self.headers.write().unwrap()) = Arc::new(latest_headers);
|
||||||
Ok(tip)
|
Ok(tip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ impl HeaderList {
|
||||||
blockhash = header.prev_blockhash;
|
blockhash = header.prev_blockhash;
|
||||||
}
|
}
|
||||||
if !header_map.is_empty() {
|
if !header_map.is_empty() {
|
||||||
warn!("orphaned blocks: {:?}", header_map);
|
warn!("{} orphaned blocks: {:?}", header_map.len(), header_map);
|
||||||
}
|
}
|
||||||
HeaderList {
|
HeaderList {
|
||||||
headers: hashed_headers
|
headers: hashed_headers
|
||||||
|
|
Loading…
Add table
Reference in a new issue