Skip to content

Commit 7e91637

Browse files
committed
poller: prepare update_feerate_estimates for testing
1 parent 8039cd6 commit 7e91637

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

src/bitcoind/interface.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,27 @@ 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+
} else {
437+
let feerate = result
438+
.get("feerate")
439+
.and_then(|f| f.as_f64())
440+
.expect("'estimatesmartfee' didn't return a 'feerate' entry");
441+
let blocks = result
442+
.get("blocks")
443+
.and_then(|n| n.as_i64())
444+
.expect("'estimatesmartfee' didn't return a 'blocks' entry");
445+
446+
Some(FeeRate { feerate, blocks })
447+
}
448+
}
428449
}
429450

430451
/// Info about bitcoind's sync state
@@ -448,3 +469,9 @@ pub struct UtxoInfo {
448469
pub bestblock: BlockHash,
449470
pub value: Amount,
450471
}
472+
473+
/// FeeRate information from estimatesmartfee
474+
pub struct FeeRate {
475+
pub feerate: f64,
476+
pub blocks: i64,
477+
}

src/poller.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ fn maybe_revault(
380380
Ok(())
381381
}
382382

383+
fn update_feerate_estimates(
384+
db_path: &path::Path,
385+
bitcoind: &BitcoinD,
386+
current_tip: &ChainTip,
387+
) -> Result<(), PollerError> {
388+
let conf_target = 1;
389+
if let Some(feerate) = bitcoind.feerate_estimate(conf_target) {
390+
log::debug!(
391+
"At block {} feerate estimate is {:?}",
392+
current_tip.height,
393+
feerate.feerate
394+
)
395+
}
396+
Ok(())
397+
}
398+
383399
// We only do actual processing on new blocks. This puts a natural limit on the amount of work
384400
// we are doing, reduces the number of edge cases we need to handle, and there is no benefit to try
385401
// to cancel Unvaults right after their broadcast.
@@ -393,6 +409,7 @@ fn new_block(
393409
) -> Result<(), PollerError> {
394410
// Update the fee-bumping reserves estimates
395411
// TODO
412+
update_feerate_estimates(db_path, bitcoind, current_tip)?;
396413

397414
// Any vault to forget and feebump coins to unregister?
398415
// TODO

tests/test_chain.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def test_simple_unvault_broadcast(miradord, bitcoind):
3232
[
3333
f"Got a confirmed Unvault UTXO at '{unvault_txid}:0'",
3434
f"Broadcasted Cancel transaction '{txs['cancel']['tx']}'",
35-
f"Unvault transaction '{unvault_txid}' for vault at '{deposit_outpoint}' is still unspent",
35+
f"Unvault transaction '{unvault_txid}' for vault at '{deposit_outpoint}' is"
36+
" still unspent",
3637
]
3738
)
3839

@@ -70,7 +71,8 @@ def test_spent_cancel_detection(miradord, bitcoind):
7071
[
7172
f"Got a confirmed Unvault UTXO at '{unvault_txid}:0'",
7273
f"Broadcasted Cancel transaction '{txs['cancel']['tx']}'",
73-
f"Unvault transaction '{unvault_txid}' for vault at '{deposit_outpoint}' is still unspent",
74+
f"Unvault transaction '{unvault_txid}' for vault at '{deposit_outpoint}' is"
75+
" still unspent",
7476
]
7577
)
7678

@@ -82,7 +84,8 @@ def test_spent_cancel_detection(miradord, bitcoind):
8284

8385
bitcoind.generate_block(1, wait_for_mempool=[cancel_tx["txid"], unvault_txid])
8486
miradord.wait_for_log(
85-
f"Noticed at height .* that Cancel transaction was confirmed for vault at '{deposit_outpoint}'"
87+
"Noticed at height .* that Cancel transaction was confirmed for vault at"
88+
f" '{deposit_outpoint}'"
8689
)
8790

8891

@@ -119,9 +122,24 @@ def test_simple_spend_detection(miradord, bitcoind):
119122
bitcoind.rpc.sendrawtransaction(txs["spend"]["tx"])
120123
bitcoind.generate_block(1, 1)
121124
miradord.wait_for_log(
122-
f"Noticed .* that Spend transaction was confirmed for vault at '{deposit_outpoint}'"
125+
"Noticed .* that Spend transaction was confirmed for vault at"
126+
f" '{deposit_outpoint}'"
123127
)
124128

125129
# Generate two days worth of blocks, the WT should forget about this vault
126130
bitcoind.generate_block(288)
127131
miradord.wait_for_log(f"Forgetting about consumed vault at '{deposit_outpoint}'")
132+
133+
134+
def test_feerate_estimation(miradord, bitcoind):
135+
# Generate some transaction history for estimatesmartfee.
136+
# 10 transactions in 50 blocks (send to deposit address)
137+
amount = 1
138+
for block in range(0, 50):
139+
wait_for_mempool = []
140+
for tx in range(0, 10):
141+
txid, outpoint = bitcoind.create_utxo(DEPOSIT_ADDRESS, amount)
142+
wait_for_mempool.append(txid)
143+
bitcoind.generate_block(1, wait_for_mempool)
144+
feerate = bitcoind.estimatesmartfee(1)["feerate"].normalize()
145+
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
@@ -132,6 +132,9 @@ def generate_blocks_censor(self, n, txids):
132132
for txid in txids:
133133
self.rpc.prioritisetransaction(txid, None, fee_delta)
134134

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

0 commit comments

Comments
 (0)