Skip to content

Commit 0a65eef

Browse files
committed
poller: prepare update_feerate_estimates for testing
1 parent f6f3f4a commit 0a65eef

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

src/bitcoind/interface.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,31 @@ impl BitcoinD {
425425
self.make_node_request_failible("sendrawtransaction", &params!(tx_hex))
426426
.map(|_| ())
427427
}
428+
429+
/// Get fee-rate estimate
430+
/// TODO: limit conf_target to range 1 to 1008
431+
pub fn feerate_estimate(&self, conf_target: i16) -> Option<FeeRate> {
432+
let result = self.make_node_request("estimatesmartfee", &params!(conf_target));
433+
434+
if let Some(_) = result.get("errors") {
435+
None
436+
}
437+
else {
438+
let feerate = result
439+
.get("feerate")
440+
.and_then(|f| f.as_f64())
441+
.expect("'estimatesmartfee' didn't return a 'feerate' entry");
442+
let blocks = result
443+
.get("blocks")
444+
.and_then(|n| n.as_i64())
445+
.expect("'estimatesmartfee' didn't return a 'blocks' entry");
446+
447+
Some(FeeRate {
448+
feerate,
449+
blocks
450+
})
451+
}
452+
}
428453
}
429454

430455
/// Info about bitcoind's sync state
@@ -448,3 +473,9 @@ pub struct UtxoInfo {
448473
pub bestblock: BlockHash,
449474
pub value: Amount,
450475
}
476+
477+
/// FeeRate information from estimatesmartfee
478+
pub struct FeeRate {
479+
pub feerate: f64,
480+
pub blocks: i64,
481+
}

src/poller.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ fn check_for_unvault(
340340
Ok(())
341341
}
342342

343+
fn update_feerate_estimates(
344+
db_path: &path::Path,
345+
bitcoind: &BitcoinD,
346+
current_tip: &ChainTip
347+
) -> Result<(), PollerError> {
348+
let conf_target = 1;
349+
if let Some(feerate) = bitcoind.feerate_estimate(conf_target) {
350+
log::debug!("At block {} feerate estimate is {:?}", current_tip.height, feerate.feerate)
351+
}
352+
Ok(())
353+
}
354+
343355
// We only do actual processing on new blocks. This puts a natural limit on the amount of work
344356
// we are doing, reduces the number of edge cases we need to handle, and there is no benefit to try
345357
// to cancel Unvaults right after their broadcast.
@@ -353,6 +365,7 @@ fn new_block(
353365
) -> Result<(), PollerError> {
354366
// 1. Update the fee-bumping reserves estimates
355367
// TODO
368+
update_feerate_estimates(db_path, bitcoind, current_tip)?;
356369

357370
// 2. Any vault to forget and feebump coins to unregister?
358371
// TODO

tests/test_chain.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def test_simple_unvault_broadcast(miradord, bitcoind):
2626
[
2727
f"Got a confirmed Unvault UTXO at '{unvault_txid}:0'",
2828
f"Broadcasted Cancel transaction '{txs['cancel']['tx']}'",
29-
f"Cancel transaction '{cancel_txid}' for vault at '{deposit_outpoint}' is still unconfirmed",
29+
f"Cancel transaction '{cancel_txid}' for vault at '{deposit_outpoint}' is"
30+
" still unconfirmed",
3031
]
3132
)
3233

@@ -60,7 +61,8 @@ def test_spent_cancel_detection(miradord, bitcoind):
6061
[
6162
f"Got a confirmed Unvault UTXO at '{unvault_txid}:0'",
6263
f"Broadcasted Cancel transaction '{txs['cancel']['tx']}'",
63-
f"Cancel transaction '{cancel_tx['txid']}' for vault at '{deposit_outpoint}' is still unconfirmed",
64+
f"Cancel transaction '{cancel_tx['txid']}' for vault at"
65+
f" '{deposit_outpoint}' is still unconfirmed",
6466
]
6567
)
6668

@@ -72,7 +74,8 @@ def test_spent_cancel_detection(miradord, bitcoind):
7274

7375
bitcoind.generate_block(1, wait_for_mempool=[cancel_tx["txid"], unvault_txid])
7476
miradord.wait_for_log(
75-
f"Noticed at height .* that Cancel transaction '{cancel_tx['txid']}' was confirmed for vault at '{deposit_outpoint}'"
77+
f"Noticed at height .* that Cancel transaction '{cancel_tx['txid']}' was"
78+
f" confirmed for vault at '{deposit_outpoint}'"
7679
)
7780

7881

@@ -95,7 +98,8 @@ def test_undetected_cancel(miradord, bitcoind):
9598
[
9699
f"Got a confirmed Unvault UTXO at '{unvault_txid}:0'",
97100
f"Broadcasted Cancel transaction '{txs['cancel']['tx']}'",
98-
f"Cancel transaction '{cancel_tx['txid']}' for vault at '{deposit_outpoint}' is still unconfirmed",
101+
f"Cancel transaction '{cancel_tx['txid']}' for vault at"
102+
f" '{deposit_outpoint}' is still unconfirmed",
99103
]
100104
)
101105
bitcoind.generate_blocks_censor(CSV, [cancel_tx["txid"]])
@@ -114,3 +118,17 @@ def test_undetected_cancel(miradord, bitcoind):
114118
f"spent for vault at '{deposit_outpoint}', but our Cancel transaction output"
115119
" is not part of the UTxO set."
116120
)
121+
122+
123+
def test_feerate_estimation(miradord, bitcoind):
124+
# Generate some transaction history for estimatesmartfee.
125+
# 10 transactions in 50 blocks (send to deposit address)
126+
amount = 1
127+
for block in range(0, 50):
128+
wait_for_mempool = []
129+
for tx in range(0, 10):
130+
txid, outpoint = bitcoind.create_utxo(DEPOSIT_ADDRESS, amount)
131+
wait_for_mempool.append(txid)
132+
bitcoind.generate_block(1, wait_for_mempool)
133+
feerate = bitcoind.estimatesmartfee(1)['feerate'].normalize()
134+
miradord.wait_for_logs([f"feerate estimate is {feerate}"])

tests/test_framework/bitcoind.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def generate_blocks_censor(self, n, txids):
133133
for txid in txids:
134134
self.rpc.prioritisetransaction(txid, None, fee_delta)
135135

136+
def estimatesmartfee(self, conf_target):
137+
return self.rpc.estimatesmartfee(conf_target)
138+
136139
def simple_reorg(self, height, shift=0):
137140
"""
138141
Reorganize chain by creating a fork at height={height} and:

0 commit comments

Comments
 (0)