Skip to content

Commit 202f988

Browse files
committed
multi: Remove bitcoind_wallet
1 parent 92f803f commit 202f988

File tree

4 files changed

+48
-75
lines changed

4 files changed

+48
-75
lines changed

lexe-ln/src/bitcoind/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::HashMap;
2-
use std::str::FromStr;
32
use std::sync::atomic::{AtomicU32, Ordering};
43
use std::sync::Arc;
54
use std::time::Duration;
@@ -8,7 +7,6 @@ use anyhow::{ensure, Context};
87
use bitcoin::blockdata::transaction::Transaction;
98
use bitcoin::consensus::encode;
109
use bitcoin::hash_types::{BlockHash, Txid};
11-
use bitcoin::util::address::Address;
1210
use common::cli::{BitcoindRpcInfo, Network};
1311
use common::shutdown::ShutdownChannel;
1412
use common::task::LxTask;
@@ -252,17 +250,6 @@ impl LexeBitcoind {
252250
.context("signrawtransactionwithwallet RPC call failed")
253251
}
254252

255-
pub async fn get_new_address(&self) -> anyhow::Result<Address> {
256-
let addr_args = vec![serde_json::json!("LDK output address")];
257-
let addr = self
258-
.rpc_client
259-
.call_method::<NewAddress>("getnewaddress", &addr_args)
260-
.await
261-
.context("getnewaddress RPC call failed")?;
262-
Address::from_str(addr.0.as_str())
263-
.context("Could not parse address from string")
264-
}
265-
266253
pub async fn get_blockchain_info(&self) -> anyhow::Result<BlockchainInfo> {
267254
self.rpc_client
268255
.call_method::<BlockchainInfo>("getblockchaininfo", &[])

lexe-ln/src/wallet/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::sync::Arc;
33
use anyhow::Context;
44
use bdk::blockchain::EsploraBlockchain;
55
use bdk::template::Bip84;
6-
use bdk::wallet::Wallet;
6+
use bdk::wallet::{AddressIndex, Wallet};
77
use bdk::{KeychainKind, SyncOptions};
8+
use bitcoin::util::address::Address;
89
use common::cli::Network;
910
use common::constants::{
1011
IMPORTANT_PERSIST_RETRIES, SINGLETON_DIRECTORY, WALLET_DB_FILENAME,
@@ -91,6 +92,16 @@ impl LexeWallet {
9192
.await
9293
.context("bdk::Wallet::sync failed")
9394
}
95+
96+
/// Returns a new address derived using the external descriptor.
97+
pub fn get_new_address(&self) -> anyhow::Result<Address> {
98+
self.0
99+
.try_lock()
100+
.context("Wallet is busy; perhaps it is still syncing?")?
101+
.get_address(AddressIndex::New)
102+
.map(|info| info.address)
103+
.context("Could not get new address")
104+
}
94105
}
95106

96107
/// Spawns a task that persists the current [`WalletDb`] state whenever it

node/src/event_handler.rs

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use lexe_ln::bitcoind::LexeBitcoind;
1919
use lexe_ln::invoice::{HTLCStatus, MillisatAmount, PaymentInfo};
2020
use lexe_ln::keys_manager::LexeKeysManager;
2121
use lexe_ln::test_event::{TestEvent, TestEventSender};
22+
use lexe_ln::wallet::LexeWallet;
2223
use lightning::chain::chaininterface::{
2324
BroadcasterInterface, ConfirmationTarget, FeeEstimator,
2425
};
@@ -28,49 +29,22 @@ use tracing::{debug, error, info};
2829

2930
use crate::channel_manager::NodeChannelManager;
3031

32+
// We pub(crate) all the fields to prevent having to specify each field two more
33+
// times in Self::new paramaters and in struct init syntax.
3134
pub struct NodeEventHandler {
32-
network: Network,
33-
lsp: ChannelPeer,
34-
channel_manager: NodeChannelManager,
35-
keys_manager: LexeKeysManager,
36-
bitcoind: Arc<LexeBitcoind>,
37-
network_graph: Arc<NetworkGraphType>,
38-
inbound_payments: PaymentInfoStorageType,
39-
outbound_payments: PaymentInfoStorageType,
40-
test_event_tx: TestEventSender,
35+
pub(crate) network: Network,
36+
pub(crate) lsp: ChannelPeer,
37+
pub(crate) wallet: LexeWallet,
38+
pub(crate) channel_manager: NodeChannelManager,
39+
pub(crate) keys_manager: LexeKeysManager,
40+
pub(crate) bitcoind: Arc<LexeBitcoind>,
41+
pub(crate) network_graph: Arc<NetworkGraphType>,
42+
pub(crate) inbound_payments: PaymentInfoStorageType,
43+
pub(crate) outbound_payments: PaymentInfoStorageType,
44+
pub(crate) test_event_tx: TestEventSender,
4145
// XXX: remove when `EventHandler` is async
42-
blocking_task_rt: BlockingTaskRt,
43-
shutdown: ShutdownChannel,
44-
}
45-
46-
impl NodeEventHandler {
47-
#[allow(clippy::too_many_arguments)]
48-
pub(crate) fn new(
49-
network: Network,
50-
lsp: ChannelPeer,
51-
channel_manager: NodeChannelManager,
52-
keys_manager: LexeKeysManager,
53-
bitcoind: Arc<LexeBitcoind>,
54-
network_graph: Arc<NetworkGraphType>,
55-
inbound_payments: PaymentInfoStorageType,
56-
outbound_payments: PaymentInfoStorageType,
57-
test_event_tx: TestEventSender,
58-
shutdown: ShutdownChannel,
59-
) -> Self {
60-
Self {
61-
network,
62-
lsp,
63-
channel_manager,
64-
keys_manager,
65-
bitcoind,
66-
network_graph,
67-
inbound_payments,
68-
outbound_payments,
69-
test_event_tx,
70-
blocking_task_rt: BlockingTaskRt::new(),
71-
shutdown,
72-
}
73-
}
46+
pub(crate) blocking_task_rt: BlockingTaskRt,
47+
pub(crate) shutdown: ShutdownChannel,
7448
}
7549

7650
impl EventHandler for NodeEventHandler {
@@ -110,6 +84,7 @@ impl EventHandler for NodeEventHandler {
11084
// handlilng is supported
11185
let network = self.network;
11286
let lsp = self.lsp.clone();
87+
let wallet = self.wallet.clone();
11388
let channel_manager = self.channel_manager.clone();
11489
let bitcoind = self.bitcoind.clone();
11590
let network_graph = self.network_graph.clone();
@@ -126,6 +101,7 @@ impl EventHandler for NodeEventHandler {
126101
handle_event(
127102
network,
128103
&lsp,
104+
&wallet,
129105
&channel_manager,
130106
&bitcoind,
131107
&network_graph,
@@ -146,6 +122,7 @@ impl EventHandler for NodeEventHandler {
146122
pub(crate) async fn handle_event(
147123
network: Network,
148124
lsp: &ChannelPeer,
125+
wallet: &LexeWallet,
149126
channel_manager: &NodeChannelManager,
150127
bitcoind: &LexeBitcoind,
151128
network_graph: &NetworkGraphType,
@@ -159,6 +136,7 @@ pub(crate) async fn handle_event(
159136
let handle_event_res = handle_event_fallible(
160137
network,
161138
lsp,
139+
wallet,
162140
channel_manager,
163141
bitcoind,
164142
network_graph,
@@ -180,6 +158,7 @@ pub(crate) async fn handle_event(
180158
async fn handle_event_fallible(
181159
network: Network,
182160
lsp: &ChannelPeer,
161+
wallet: &LexeWallet,
183162
channel_manager: &NodeChannelManager,
184163
bitcoind: &LexeBitcoind,
185164
network_graph: &NetworkGraphType,
@@ -490,10 +469,7 @@ async fn handle_event_fallible(
490469
});
491470
}
492471
Event::SpendableOutputs { outputs } => {
493-
let destination_address = bitcoind
494-
.get_new_address()
495-
.await
496-
.context("Could not get new address")?;
472+
let destination_address = wallet.get_new_address()?;
497473
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
498474
let tx_feerate = bitcoind
499475
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);

node/src/run.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use common::enclave::{
1818
use common::rng::Crng;
1919
use common::root_seed::RootSeed;
2020
use common::shutdown::ShutdownChannel;
21-
use common::task::{joined_task_state_label, LxTask};
21+
use common::task::{joined_task_state_label, BlockingTaskRt, LxTask};
2222
use futures::future::TryFutureExt;
2323
use futures::stream::{FuturesUnordered, StreamExt};
2424
use lexe_ln::alias::{
@@ -78,7 +78,6 @@ pub struct UserNode {
7878
pub logger: LexeTracingLogger,
7979
pub persister: NodePersister,
8080
pub wallet: LexeWallet,
81-
pub bitcoind_wallet: Arc<LexeBitcoind>, // TODO(max): Refactor this out
8281
block_source: Arc<BlockSourceType>,
8382
fee_estimator: Arc<FeeEstimatorType>,
8483
broadcaster: Arc<BroadcasterType>,
@@ -171,7 +170,6 @@ impl UserNode {
171170
// LexeBitcoind impls BlockSource, FeeEstimator and
172171
// BroadcasterInterface, and thus serves these functions.
173172
// A type alias is used for each as bitcoind is slowly refactored out
174-
let bitcoind_wallet = bitcoind.clone();
175173
let block_source = bitcoind.clone();
176174
let fee_estimator = bitcoind.clone();
177175
let broadcaster = bitcoind.clone();
@@ -352,18 +350,20 @@ impl UserNode {
352350
Arc::new(Mutex::new(HashMap::new()));
353351
let outbound_payments: PaymentInfoStorageType =
354352
Arc::new(Mutex::new(HashMap::new()));
355-
let event_handler = NodeEventHandler::new(
356-
args.network,
357-
args.lsp.clone(),
358-
channel_manager.clone(),
359-
keys_manager.clone(),
360-
bitcoind.clone(),
361-
network_graph.clone(),
362-
inbound_payments.clone(),
363-
outbound_payments.clone(),
364-
test_event_tx.clone(),
365-
shutdown.clone(),
366-
);
353+
let event_handler = NodeEventHandler {
354+
network: args.network,
355+
lsp: args.lsp.clone(),
356+
wallet: wallet.clone(),
357+
channel_manager: channel_manager.clone(),
358+
keys_manager: keys_manager.clone(),
359+
bitcoind: bitcoind.clone(),
360+
network_graph: network_graph.clone(),
361+
inbound_payments: inbound_payments.clone(),
362+
outbound_payments: outbound_payments.clone(),
363+
test_event_tx: test_event_tx.clone(),
364+
blocking_task_rt: BlockingTaskRt::new(),
365+
shutdown: shutdown.clone(),
366+
};
367367

368368
// Initialize InvoicePayer
369369
let router = DefaultRouter::new(
@@ -478,7 +478,6 @@ impl UserNode {
478478
logger,
479479
persister,
480480
wallet,
481-
bitcoind_wallet,
482481
block_source,
483482
fee_estimator,
484483
broadcaster,

0 commit comments

Comments
 (0)