Skip to content

Commit 38b8a31

Browse files
ekulkisnekAsh-L2L
authored andcommitted
Address PR review feedback
1 parent b4496cc commit 38b8a31

7 files changed

Lines changed: 37 additions & 43 deletions

File tree

app/app.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl App {
287287
Ok(())
288288
}
289289

290-
pub async fn get_new_main_address_async(
290+
pub async fn get_new_main_address(
291291
&self,
292292
) -> Result<bitcoin::Address<bitcoin::address::NetworkChecked>, Error> {
293293
let Some(miner) = self.miner.as_ref() else {
@@ -306,10 +306,10 @@ impl App {
306306
Ok(res)
307307
}
308308

309-
pub fn get_new_main_address(
309+
pub fn get_new_main_address_blocking(
310310
&self,
311311
) -> Result<bitcoin::Address<bitcoin::address::NetworkChecked>, Error> {
312-
self.runtime.block_on(self.get_new_main_address_async())
312+
self.runtime.block_on(self.get_new_main_address())
313313
}
314314

315315
const EMPTY_BLOCK_BMM_BRIBE: bitcoin::Amount =
@@ -530,7 +530,7 @@ impl App {
530530
Ok(())
531531
}
532532

533-
pub async fn deposit_async(
533+
pub async fn deposit(
534534
&self,
535535
address: Address,
536536
amount: bitcoin::Amount,
@@ -548,14 +548,13 @@ impl App {
548548
Ok(txid)
549549
}
550550

551-
pub fn deposit(
551+
pub fn deposit_blocking(
552552
&self,
553553
address: Address,
554554
amount: bitcoin::Amount,
555555
fee: bitcoin::Amount,
556556
) -> Result<bitcoin::Txid, Error> {
557-
self.runtime
558-
.block_on(self.deposit_async(address, amount, fee))
557+
self.runtime.block_on(self.deposit(address, amount, fee))
559558
}
560559
}
561560

app/gui/coins/transfer_receive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Receive {
100100
};
101101
let address = app
102102
.wallet
103-
.get_address_or_new()
103+
.get_or_generate_last_address()
104104
.map_err(anyhow::Error::from)
105105
.inspect_err(|err| tracing::error!("{err:#}"));
106106
Self {

app/gui/coins/utxo_creator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl UtxoCreator {
9393
.add_enabled(app.is_some(), Button::new("generate"))
9494
.clicked()
9595
{
96-
match app.unwrap().get_new_main_address() {
96+
match app.unwrap().get_new_main_address_blocking() {
9797
Ok(main_address) => {
9898
self.main_address = format!("{main_address}");
9999
}

app/gui/parent_chain/transfer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Deposit {
9595
Ok(address) => {
9696
self.promise =
9797
Some(poll_promise::Promise::spawn_async(async move {
98-
app.deposit_async(address, amount, fee)
98+
app.deposit(address, amount, fee)
9999
.await
100100
.map_err(|e| format!("{e:#}"))
101101
}));
@@ -191,7 +191,7 @@ impl Withdrawal {
191191
let app = app.unwrap().clone();
192192
self.generate_promise =
193193
Some(poll_promise::Promise::spawn_async(async move {
194-
app.get_new_main_address_async()
194+
app.get_new_main_address()
195195
.await
196196
.map_err(|e| format!("{e:#}"))
197197
}));

app/main.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::Path;
1+
use std::{path::Path, sync::Arc};
22

33
use clap::Parser as _;
44
use mimalloc::MiMalloc;
@@ -229,17 +229,16 @@ fn main() -> anyhow::Result<()> {
229229
});
230230

231231
if !config.headless {
232-
let fallback_rt;
233-
let _guard = match &app {
234-
Ok(app) => Some(app.runtime.enter()),
232+
let rt = match &app {
233+
Ok(app) => Arc::clone(&app.runtime),
235234
Err(_) => {
236-
fallback_rt = tokio::runtime::Builder::new_multi_thread()
235+
let rt = tokio::runtime::Builder::new_multi_thread()
237236
.enable_all()
238-
.build()
239-
.ok();
240-
fallback_rt.as_ref().map(|rt| rt.enter())
237+
.build()?;
238+
Arc::new(rt)
241239
}
242240
};
241+
let _rt_guard = rt.enter();
243242
// For GUI mode we want the GUI to start, even if the app fails to start.
244243
return run_egui_app(&config, line_buffer, app)
245244
.map_err(|e| anyhow::anyhow!("failed to run egui app: {e:#}"));

app/rpc_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl RpcServer for RpcServerImpl {
5151
) -> RpcResult<bitcoin::Txid> {
5252
let app = self.app.clone();
5353
tokio::task::spawn_blocking(move || {
54-
app.deposit(
54+
app.deposit_blocking(
5555
address,
5656
bitcoin::Amount::from_sat(value_sats),
5757
bitcoin::Amount::from_sat(fee_sats),

lib/wallet.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,17 @@ impl Wallet {
510510
Ok(address)
511511
}
512512

513-
pub fn get_last_address(&self) -> Result<Option<Address>, Error> {
513+
/// Gets the latest generated address.
514+
pub fn try_get_last_address(&self) -> Result<Option<Address>, Error> {
514515
let txn = self.env.read_txn().map_err(EnvError::from)?;
515516
let last = self.index_to_address.last(&txn).map_err(DbError::from)?;
516517
Ok(last.map(|(_, address)| address))
517518
}
518519

519-
pub fn get_address_or_new(&self) -> Result<Address, Error> {
520-
if let Some(address) = self.get_last_address()? {
520+
/// Gets the latest generated address, or generates a new one if no
521+
/// addresses have already been generated.
522+
pub fn get_or_generate_last_address(&self) -> Result<Address, Error> {
523+
if let Some(address) = self.try_get_last_address()? {
521524
Ok(address)
522525
} else {
523526
self.get_new_address()
@@ -595,14 +598,12 @@ mod tests {
595598
use super::*;
596599

597600
#[test]
598-
fn test_get_address_or_new() -> Result<(), Error> {
599-
let test_dir = std::env::temp_dir().join(format!(
600-
"thunder_test_wallet_{}",
601-
std::time::SystemTime::now()
602-
.duration_since(std::time::UNIX_EPOCH)
603-
.unwrap()
604-
.as_nanos()
605-
));
601+
fn test_get_or_generate_last_address() -> anyhow::Result<()> {
602+
let nanos = std::time::SystemTime::now()
603+
.duration_since(std::time::UNIX_EPOCH)?
604+
.as_nanos();
605+
let test_dir =
606+
std::env::temp_dir().join(format!("thunder_test_wallet_{nanos}"));
606607

607608
// Ensure clean state
608609
if test_dir.exists() {
@@ -618,30 +619,25 @@ mod tests {
618619
assert!(wallet.has_seed()?);
619620

620621
// Get last address when none have been generated
621-
let last = wallet.get_last_address()?;
622+
let last = wallet.try_get_last_address()?;
622623
assert!(last.is_none());
623624

624-
// Get address or new should generate the first address
625-
let addr1 = wallet.get_address_or_new()?;
625+
// The first call should generate the first address.
626+
let addr1 = wallet.get_or_generate_last_address()?;
626627

627-
// Now last address should be addr1
628-
let last = wallet.get_last_address()?;
628+
let last = wallet.try_get_last_address()?;
629629
assert_eq!(last, Some(addr1));
630630

631-
// Subsequent get_address_or_new calls should return the same addr1
632-
let addr2 = wallet.get_address_or_new()?;
631+
let addr2 = wallet.get_or_generate_last_address()?;
633632
assert_eq!(addr1, addr2);
634633

635-
// Generating a new address explicitly should give a new one
636634
let addr3 = wallet.get_new_address()?;
637635
assert_ne!(addr1, addr3);
638636

639-
// Now last address should be addr3
640-
let last = wallet.get_last_address()?;
637+
let last = wallet.try_get_last_address()?;
641638
assert_eq!(last, Some(addr3));
642639

643-
// And get_address_or_new should return addr3
644-
let addr4 = wallet.get_address_or_new()?;
640+
let addr4 = wallet.get_or_generate_last_address()?;
645641
assert_eq!(addr3, addr4);
646642

647643
// Clean up

0 commit comments

Comments
 (0)