1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

Support (older) ETA-based fee estimation

This commit is contained in:
Roman Zeyde 2018-05-22 22:04:21 +03:00
parent fdff31a069
commit a681334074
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 18 additions and 2 deletions

View File

@ -310,5 +310,18 @@ impl<'a> Query<'a> {
self.tracker.read().unwrap().fee_histogram().clone()
}
// Fee rate [BTC/kB] to be confirmed in `blocks` from now.
pub fn estimate_fee(&self, blocks: usize) -> f32 {
let mut total_vsize = 0u32;
let mut last_fee_rate = 0.0;
let blocks_in_vbytes = (blocks * 1_000_000) as u32; // assume ~1MB blocks
for (fee_rate, vsize) in self.tracker.read().unwrap().fee_histogram() {
last_fee_rate = *fee_rate;
total_vsize += vsize;
if total_vsize >= blocks_in_vbytes {
break; // under-estimate the fee rate a bit
}
}
last_fee_rate * 1e-5 // [BTC/kB] = 10^5 [sat/B]
}
}

View File

@ -103,8 +103,11 @@ impl<'a> Connection<'a> {
Ok(json!(jsonify_header(&headers[0], height)))
}
fn blockchain_estimatefee(&self, _params: &[Value]) -> Result<Value> {
Ok(json!(-1)) // see mempool_get_fee_histogram() instead.
fn blockchain_estimatefee(&self, params: &[Value]) -> Result<Value> {
let blocks = params.get(0).chain_err(|| "missing blocks")?;
let blocks = blocks.as_u64().chain_err(|| "non-number blocks")? as usize;
let fee_rate = self.query.estimate_fee(blocks); // in BTC/kB
Ok(json!(fee_rate))
}
fn blockchain_relayfee(&self) -> Result<Value> {