Skip to content

Commit 817fd28

Browse files
torkelrogstadAsh-L2L
authored andcommitted
multi: avoid calculating next block target ourselves
Fetch directly from Core instead. Calculation has been broken on signets with shorter block times: https://github.com/LayerTwo-Labs/bip300301_enforcer/blob/a30226f2c1c937bef99744ae8d9ab3761f43c844/lib/wallet/mine.rs#L612-L633
1 parent d6cbe63 commit 817fd28

5 files changed

Lines changed: 10 additions & 44 deletions

File tree

app/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ async fn main() -> anyhow::Result<()> {
100100
let shutdown_signal = shutdown_rx.map(|_: Result<_, _>| ());
101101
mempool::init_sync_mempool(
102102
&mut enforcer,
103-
network,
104103
rpc_client.clone(),
105104
&cli.node_zmq_addr_sequence,
106105
shutdown_signal.fuse(),

integration_tests/setup.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::{collections::HashSet, path::PathBuf, sync::Arc, time::Duration};
55

66
use anyhow::Context;
7-
use bitcoin::{Address, BlockHash, Network, Txid};
7+
use bitcoin::{Address, BlockHash, Txid};
88
use cusf_enforcer_mempool::{
99
cusf_enforcer::CusfEnforcer,
1010
mempool::{self, MempoolSync, SyncTaskError},
@@ -194,7 +194,6 @@ where
194194
{
195195
let mempool_synced = mempool::init_sync_mempool(
196196
&mut enforcer,
197-
Network::Regtest,
198197
node.rpc_client.clone(),
199198
&node.bitcoind.zmq_addr(),
200199
std::future::pending::<()>().fuse(),

lib/mempool/mod.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use bitcoin::{BlockHash, Network, Target, Transaction, Txid, Weight};
3+
use bitcoin::{BlockHash, Transaction, Txid, Weight};
44
use bitcoin_jsonrpsee::client::{BlockTemplateTransaction, RawMempoolTxFees};
55
use hashlink::{LinkedHashMap, LinkedHashSet};
66
use imbl::{OrdMap, OrdSet, ordmap};
@@ -482,23 +482,21 @@ fn saturating_sub_weight(lhs: Weight, rhs: Weight) -> Weight {
482482
pub struct Mempool {
483483
by_ancestor_fee_rate: ByAncestorFeeRate,
484484
chain: Chain,
485-
network: Network,
486485
/// Map of txs (which may not be in the mempool) to their direct child txs,
487486
/// which MUST be in the mempool
488487
tx_childs: TxChilds,
489488
txs: MempoolTxs,
490489
}
491490

492491
impl Mempool {
493-
fn new(network: Network, prev_blockhash: BlockHash) -> Self {
492+
fn new(prev_blockhash: BlockHash) -> Self {
494493
let chain = Chain {
495494
tip: prev_blockhash,
496495
blocks: imbl::HashMap::new(),
497496
};
498497
Self {
499498
by_ancestor_fee_rate: ByAncestorFeeRate::default(),
500499
chain,
501-
network,
502500
tx_childs: TxChilds::default(),
503501
txs: MempoolTxs::default(),
504502
}
@@ -508,34 +506,6 @@ impl Mempool {
508506
&self.chain.blocks[&self.chain.tip]
509507
}
510508

511-
/// Next target, if known
512-
pub fn next_target(&self) -> Option<Target> {
513-
let tip = self.tip();
514-
let next_height = tip.height + 1;
515-
let network_params = self.network.params();
516-
if !network_params.no_pow_retargeting
517-
&& next_height % network_params.miner_confirmation_window == 0
518-
{
519-
if let Some(first_block_in_period) = self
520-
.chain
521-
.iter()
522-
.nth(network_params.miner_confirmation_window as usize - 1)
523-
{
524-
let spacing = tip.time - first_block_in_period.time;
525-
let res = bitcoin::CompactTarget::from_next_work_required(
526-
tip.compact_target,
527-
spacing as u64,
528-
network_params,
529-
);
530-
Some(res.into())
531-
} else {
532-
None
533-
}
534-
} else {
535-
Some(tip.compact_target.into())
536-
}
537-
}
538-
539509
/// Insert a tx into the mempool,
540510
/// stating conflicts with other txs due to reasons other than shared
541511
/// inputs.
@@ -979,7 +949,7 @@ impl Mempool {
979949
mod tests {
980950
use super::*;
981951
use bitcoin::{
982-
Amount, Network, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Witness,
952+
Amount, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Witness,
983953
absolute::LockTime, hashes::Hash as _, transaction::Version,
984954
};
985955

@@ -1006,7 +976,7 @@ mod tests {
1006976
}
1007977

1008978
fn test_mempool() -> Mempool {
1009-
Mempool::new(Network::Regtest, BlockHash::all_zeros())
979+
Mempool::new(BlockHash::all_zeros())
1010980
}
1011981

1012982
/// Reproduces the prod crash: inserting a tx whose `conflicts_with`

lib/mempool/sync/task.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,6 @@ pub async fn init_sync_mempool<
11051105
ShutdownSignal,
11061106
>(
11071107
mut enforcer: Enforcer,
1108-
network: bitcoin::Network,
11091108
rpc_client: RpcClient,
11101109
zmq_addr_sequence: &str,
11111110
// Would it be better to return a Some/None, indicating sync stoppage?
@@ -1137,7 +1136,7 @@ where
11371136
let inner = MempoolSyncInner {
11381137
abandoned_pool: AbandonedPool::default(),
11391138
enforcer,
1140-
mempool: Mempool::new(network, best_block_hash),
1139+
mempool: Mempool::new(best_block_hash),
11411140
unfiltered_mempool: UnfilteredMempool {
11421141
tip: best_block_hash,
11431142
txs: HashSet::new(),

lib/server.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ async fn block_txs<const COINBASE_TXN: bool, BP>(
565565
}
566566

567567
struct MempoolQueryOutput {
568-
target: Option<bitcoin::Target>,
569568
prev_blockhash: BlockHash,
570569
tip_block_mediantime: u32,
571570
tip_block_height: u32,
@@ -607,7 +606,6 @@ where
607606
(None, block_txs, None)
608607
};
609608
Ok(MempoolQueryOutput {
610-
target: mempool.next_target(),
611609
prev_blockhash: tip_block.hash,
612610
tip_block_mediantime: tip_block.mediantime,
613611
tip_block_height: tip_block.height,
@@ -704,7 +702,6 @@ where
704702
internal_error(err)
705703
})?;
706704
let MempoolQueryOutput {
707-
target,
708705
prev_blockhash,
709706
tip_block_mediantime,
710707
tip_block_height,
@@ -722,9 +719,11 @@ where
722719
self.known_targets.read().get(&prev_blockhash).copied();
723720
if let Some(target) = known_target {
724721
target
725-
} else if let Some(target) = target {
726-
target
727722
} else {
723+
// We used to calculate the next block's target here. This didn't
724+
// work with signets with custom block times. Instead we always
725+
// read directly from Core. This only happens 1 time per chain tip,
726+
// so the performance impact is negligible.
728727
let mining_info = self
729728
.rpc_client
730729
.get_mining_info()

0 commit comments

Comments
 (0)