Skip to content

Commit

Permalink
start gui even if app fails to start
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Nov 25, 2024
1 parent cd4bb7e commit 6e7a433
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 133 deletions.
15 changes: 9 additions & 6 deletions app/gui/block_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ impl BlockExplorer {
Self { height }
}

pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) {
let max_height = app.node.get_height().unwrap_or(0);
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
let max_height =
app.and_then(|app| app.node.get_height().ok()).unwrap_or(0);
let block: Option<(Header, Body)> = {
if let Ok(Some(block_hash)) =
app.node.try_get_block_hash(self.height)
if let Some(app) = app
&& let Ok(Some(block_hash)) =
app.node.try_get_block_hash(self.height)
&& let Ok(header) = app.node.get_header(block_hash)
&& let Ok(body) = app.node.get_body(block_hash)
{
Expand Down Expand Up @@ -97,8 +99,9 @@ impl BlockExplorer {
let body_sigops_limit = State::body_sigops_limit(self.height);
ui.monospace(format!("Body sigops limit: {body_sigops_limit}"));

if let Ok(Some(acc)) =
app.node.try_get_accumulator(header.hash())
if let Some(app) = app
&& let Ok(Some(acc)) =
app.node.try_get_accumulator(header.hash())
{
ui.monospace_selectable_multiline(format!(
"Utreexo accumulator: \n{}",
Expand Down
4 changes: 2 additions & 2 deletions app/gui/coins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ pub struct Coins {
}

impl Coins {
pub fn new(app: &App) -> Self {
pub fn new(app: Option<&App>) -> Self {
Self {
transfer_receive: TransferReceive::new(app),
tab: Tab::default(),
tx_builder: TxBuilder::default(),
}
}

pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) {
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
egui::TopBottomPanel::top("coins_tabs").show(ui.ctx(), |ui| {
ui.horizontal(|ui| {
Tab::iter().for_each(|tab_variant| {
Expand Down
38 changes: 25 additions & 13 deletions app/gui/coins/transfer_receive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eframe::egui;
use eframe::egui::{self, Button};
use thunder::types::Address;

use crate::{app::App, gui::util::UiExt};
Expand All @@ -25,7 +25,7 @@ fn create_transfer(
}

impl Transfer {
fn show(&mut self, app: &App, ui: &mut egui::Ui) {
fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
ui.add_sized((250., 10.), |ui: &mut egui::Ui| {
ui.horizontal(|ui| {
let dest_edit = egui::TextEdit::singleline(&mut self.dest)
Expand Down Expand Up @@ -66,13 +66,16 @@ impl Transfer {
);
if ui
.add_enabled(
dest.is_some() && amount.is_ok() && fee.is_ok(),
app.is_some()
&& dest.is_some()
&& amount.is_ok()
&& fee.is_ok(),
egui::Button::new("transfer"),
)
.clicked()
{
if let Err(err) = create_transfer(
app,
app.unwrap(),
dest.expect("should not happen"),
amount.expect("should not happen"),
fee.expect("should not happen"),
Expand All @@ -87,29 +90,38 @@ impl Transfer {

#[derive(Debug)]
struct Receive {
address: anyhow::Result<Address>,
address: Option<anyhow::Result<Address>>,
}

impl Receive {
fn new(app: &App) -> Self {
fn new(app: Option<&App>) -> Self {
let Some(app) = app else {
return Self { address: None };
};
let address = app
.wallet
.get_new_address()
.map_err(anyhow::Error::from)
.inspect_err(|err| tracing::error!("{err:#}"));
Self { address }
Self {
address: Some(address),
}
}

fn show(&mut self, app: &App, ui: &mut egui::Ui) {
fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
match &self.address {
Ok(address) => {
Some(Ok(address)) => {
ui.monospace_selectable_singleline(false, address.to_string());
}
Err(err) => {
Some(Err(err)) => {
ui.monospace_selectable_multiline(format!("{err:#}"));
}
None => (),
}
if ui.button("generate").clicked() {
if ui
.add_enabled(app.is_some(), Button::new("generate"))
.clicked()
{
*self = Self::new(app)
}
}
Expand All @@ -122,14 +134,14 @@ pub(super) struct TransferReceive {
}

impl TransferReceive {
pub fn new(app: &App) -> Self {
pub fn new(app: Option<&App>) -> Self {
Self {
transfer: Transfer::default(),
receive: Receive::new(app),
}
}

pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) {
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
egui::SidePanel::left("transfer")
.exact_width(ui.available_width() / 2.)
.resizable(false)
Expand Down
7 changes: 5 additions & 2 deletions app/gui/coins/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ pub struct TxBuilder {
}

impl TxBuilder {
pub fn show_value_in(&mut self, app: &mut App, ui: &mut egui::Ui) {
pub fn show_value_in(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
ui.heading("Value In");
let Some(app) = app else {
return;
};
let selected: HashSet<_> = self
.base_tx
.inputs
Expand Down Expand Up @@ -102,7 +105,7 @@ impl TxBuilder {

pub fn show(
&mut self,
app: &mut App,
app: Option<&App>,
ui: &mut egui::Ui,
) -> anyhow::Result<()> {
egui::SidePanel::left("spend_utxo")
Expand Down
11 changes: 7 additions & 4 deletions app/gui/coins/tx_creator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eframe::egui;
use eframe::egui::{self, Button};

use thunder::types::{Transaction, Txid};

Expand All @@ -22,7 +22,7 @@ fn send_tx(app: &App, tx: &mut Transaction) -> anyhow::Result<()> {
impl TxCreator {
pub fn show(
&mut self,
app: &mut App,
app: Option<&App>,
ui: &mut egui::Ui,
base_tx: &mut Transaction,
) -> anyhow::Result<()> {
Expand All @@ -48,8 +48,11 @@ impl TxCreator {
if self.value_in >= self.value_out {
let fee = self.value_in - self.value_out;
ui.monospace(format!("fee: {fee}"));
if ui.button("sign and send").clicked() {
if let Err(err) = send_tx(app, final_tx) {
if ui
.add_enabled(app.is_some(), Button::new("sign and send"))
.clicked()
{
if let Err(err) = send_tx(app.unwrap(), final_tx) {
tracing::error!("{err:#}");
} else {
*base_tx = Transaction::default();
Expand Down
23 changes: 16 additions & 7 deletions app/gui/coins/utxo_creator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eframe::egui;
use eframe::egui::{self, Button};
use thunder::types::{self, Output, OutputContent, Transaction};

use crate::app::App;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl Default for UtxoCreator {
impl UtxoCreator {
pub fn show(
&mut self,
app: &mut App,
app: Option<&App>,
ui: &mut egui::Ui,
tx: &mut Transaction,
) {
Expand Down Expand Up @@ -73,8 +73,12 @@ impl UtxoCreator {
ui.horizontal(|ui| {
ui.monospace("Address: ");
ui.add(egui::TextEdit::singleline(&mut self.address));
if ui.button("generate").clicked() {
if ui
.add_enabled(app.is_some(), Button::new("generate"))
.clicked()
{
self.address = app
.unwrap()
.wallet
.get_new_address()
.map(|address| format!("{address}"))
Expand All @@ -85,8 +89,11 @@ impl UtxoCreator {
ui.horizontal(|ui| {
ui.monospace("Main Address:");
ui.add(egui::TextEdit::singleline(&mut self.main_address));
if ui.button("generate").clicked() {
match app.get_new_main_address() {
if ui
.add_enabled(app.is_some(), Button::new("generate"))
.clicked()
{
match app.unwrap().get_new_main_address() {
Ok(main_address) => {
self.main_address = format!("{main_address}");
}
Expand Down Expand Up @@ -171,8 +178,10 @@ impl UtxoCreator {
}
}
}
let num_addresses = app.wallet.get_num_addresses().unwrap();
ui.label(format!("{num_addresses} addresses generated"));
if let Some(app) = app {
let num_addresses = app.wallet.get_num_addresses().unwrap();
ui.label(format!("{num_addresses} addresses generated"));
}
});
}
}
26 changes: 16 additions & 10 deletions app/gui/coins/utxo_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@ pub struct UtxoSelector;
impl UtxoSelector {
pub fn show(
&mut self,
app: &mut App,
app: Option<&App>,
ui: &mut egui::Ui,
tx: &mut Transaction,
) {
ui.heading("Spend UTXO");
let selected: HashSet<_> =
tx.inputs.iter().map(|(outpoint, _)| *outpoint).collect();
let utxos_read = app.utxos.read();
let total: bitcoin::Amount = utxos_read
.iter()
.filter(|(outpoint, _)| !selected.contains(outpoint))
.map(|(_, output)| output.get_value())
.sum();
let mut utxos: Vec<_> = (*utxos_read).clone().into_iter().collect();
drop(utxos_read);
utxos.sort_by_key(|(outpoint, _)| format!("{outpoint}"));
let (total, utxos): (bitcoin::Amount, Vec<_>) = app
.map(|app| {
let utxos_read = app.utxos.read();
let total: bitcoin::Amount = utxos_read
.iter()
.filter(|(outpoint, _)| !selected.contains(outpoint))
.map(|(_, output)| output.get_value())
.sum();
let mut utxos: Vec<_> =
(*utxos_read).clone().into_iter().collect();
drop(utxos_read);
utxos.sort_by_key(|(outpoint, _)| format!("{outpoint}"));
(total, utxos)
})
.unwrap_or_default();
ui.separator();
ui.monospace(format!("Total: {}", total));
ui.separator();
Expand Down
7 changes: 4 additions & 3 deletions app/gui/console_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl ConsoleLogs {
});
}

pub fn show(&mut self, app: &App, ui: &mut egui::Ui) {
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| {
let line_buffer_read = self.line_buffer.as_str();
let mut logs: &str = &line_buffer_read;
Expand All @@ -106,15 +106,16 @@ impl ConsoleLogs {
.hint_text("help")
.return_key(SHIFT_ENTER);
let command_input_resp = ui.add_enabled(
!self.running_command.load(atomic::Ordering::SeqCst),
app.is_some()
&& !self.running_command.load(atomic::Ordering::SeqCst),
command_input,
);
if command_input_resp.ctx.input_mut(|input| {
!input.consume_shortcut(&SHIFT_ENTER)
&& input.consume_key(Modifiers::NONE, Key::Enter)
&& !self.running_command.load(atomic::Ordering::SeqCst)
}) {
self.console_command(app);
self.console_command(app.unwrap());
}
});
}
Expand Down
10 changes: 7 additions & 3 deletions app/gui/mempool_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ pub struct MemPoolExplorer {
}

impl MemPoolExplorer {
pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) {
let transactions = app.node.get_all_transactions().unwrap_or_default();
let utxos = app.wallet.get_utxos().unwrap_or_default();
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
let transactions = app
.and_then(|app| app.node.get_all_transactions().ok())
.unwrap_or_default();
let utxos = app
.and_then(|app| app.wallet.get_utxos().ok())
.unwrap_or_default();
egui::SidePanel::left("transaction_picker")
.resizable(false)
.show_inside(ui, |ui| {
Expand Down
16 changes: 10 additions & 6 deletions app/gui/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ impl Default for Miner {
}

impl Miner {
pub fn show(&mut self, app: &App, ui: &mut egui::Ui) {
let block_height = app.node.get_height().unwrap_or(0);
let best_hash = app.node.get_best_hash().unwrap_or([0; 32].into());
pub fn show(&mut self, app: Option<&App>, ui: &mut egui::Ui) {
let block_height =
app.and_then(|app| app.node.get_height().ok()).unwrap_or(0);
let best_hash = app
.and_then(|app| app.node.get_best_hash().ok())
.unwrap_or([0; 32].into());
ui.label("Block height: ");
ui.monospace(format!("{block_height}"));
ui.label("Best hash: ");
let best_hash = &format!("{best_hash}")[0..8];
ui.monospace(format!("{best_hash}..."));
let running = self.running.load(atomic::Ordering::SeqCst);
if ui
.add_enabled(!running, Button::new("Mine / Refresh Block"))
.clicked()
if let Some(app) = app
&& ui
.add_enabled(!running, Button::new("Mine / Refresh Block"))
.clicked()
{
self.running.store(true, atomic::Ordering::SeqCst);
app.local_pool.spawn_pinned({
Expand Down
Loading

0 comments on commit 6e7a433

Please sign in to comment.