Skip to content

Commit 98e8aa7

Browse files
app: more logs for mining, wallet updates, net (#16)
* app: more logs for mining + wallet updates * node: include source error in Error::SendNewTipReady * node: more networking logs * net+node: more logs * multi: add context to MailboxItem::AcceptConnection about remote address * net: add remote_address to Error::Connection * logging: make level for file log configurable * net: tune logs * multi: more BMM logs * cli: make RPC timeout configurable * cli: turn RPC request timeout into Option<u64> * net: remove invalid peers at startup * app: remove confusing error conversion functions * node: delete unused function * main: update bad param name * app: remove inspect_err
1 parent 1fd5f5f commit 98e8aa7

File tree

10 files changed

+228
-123
lines changed

10 files changed

+228
-123
lines changed

app/app.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub enum Error {
4646
}
4747

4848
fn update_wallet(node: &Node, wallet: &Wallet) -> Result<(), Error> {
49+
tracing::trace!("starting wallet update");
4950
let addresses = wallet.get_addresses()?;
5051
let utxos = node.get_utxos_by_addresses(&addresses)?;
5152
let outpoints: Vec<_> = wallet.get_utxos()?.into_keys().collect();
@@ -56,6 +57,8 @@ fn update_wallet(node: &Node, wallet: &Wallet) -> Result<(), Error> {
5657
.collect();
5758
wallet.put_utxos(&utxos)?;
5859
wallet.spend_utxos(&spent)?;
60+
61+
tracing::debug!("finished wallet update");
5962
Ok(())
6063
}
6164

@@ -355,34 +358,35 @@ impl App {
355358
.attempt_bmm(bribe.to_sat(), 0, header, body)
356359
.await?;
357360

358-
// miner_write.generate().await?;
359361
tracing::debug!(%bmm_txid, "mine: confirming BMM...");
360362
if let Some((main_hash, header, body)) =
361363
miner_write.confirm_bmm().await?
362364
{
363365
tracing::debug!(
364-
"mine: confirmed BMM, submitting block {}",
365-
header.hash()
366+
%main_hash, side_hash = %header.hash(), "mine: confirmed BMM, submitting block",
366367
);
367368
match self.node.submit_block(main_hash, &header, &body).await? {
368369
true => {
369370
tracing::debug!(
370-
"mine: BMM accepted as new tip: {}",
371-
main_hash
371+
%main_hash, "mine: BMM accepted as new tip",
372372
);
373373
}
374374
false => {
375375
tracing::warn!(
376-
"mine: BMM not accepted as new tip: {}",
377-
main_hash
376+
%main_hash, "mine: BMM not accepted as new tip",
378377
);
379378
}
380379
}
381380
}
382381

383382
drop(miner_write);
384383
let () = self.update()?;
385-
self.node.regenerate_proof(&mut self.transaction.write())?;
384+
385+
self.node
386+
.regenerate_proof(&mut self.transaction.write())
387+
.inspect_err(|err| {
388+
tracing::error!("mine: unable to regenerate proof: {err:#}");
389+
})?;
386390
Ok(())
387391
}
388392

app/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ pub(super) struct Cli {
109109
/// If set to the empty string, logging to file will be disabled.
110110
#[arg(long)]
111111
log_dir: Option<PathBuf>,
112+
113+
/// Log level for logs that get written to file
114+
#[arg(default_value_t = tracing::Level::WARN, long)]
115+
log_level_file: tracing::Level,
116+
112117
/// Log level
113118
#[arg(default_value_t = tracing::Level::DEBUG, long)]
114119
log_level: tracing::Level,
@@ -135,6 +140,7 @@ pub struct Config {
135140
/// If None, logging to file should be disabled.
136141
pub log_dir: Option<PathBuf>,
137142
pub log_level: tracing::Level,
143+
pub log_level_file: tracing::Level, // Level for logs that get written to file
138144
pub mainchain_grpc_address: url::Url,
139145
pub mnemonic_seed_phrase_path: Option<PathBuf>,
140146
pub net_addr: SocketAddr,
@@ -164,6 +170,7 @@ impl Cli {
164170
headless: self.headless,
165171
log_dir,
166172
log_level: self.log_level,
173+
log_level_file: self.log_level_file,
167174
mainchain_grpc_address: self.mainchain_grpc_address,
168175
mnemonic_seed_phrase_path: self.mnemonic_seed_phrase_path,
169176
net_addr: self.net_addr,

app/gui/console_logs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl ConsoleLogs {
7272
};
7373
let cli = thunder_app_cli_lib::Cli {
7474
rpc_addr: self.rpc_addr,
75+
timeout: None,
7576
command,
7677
};
7778
app.runtime.spawn({

app/main.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ type RollingLoggerGuard = tracing_appender::non_blocking::WorkerGuard;
5656
/// to keep the file logger alive.
5757
fn rolling_logger<S>(
5858
log_dir: &Path,
59+
log_level: tracing::Level,
5960
) -> anyhow::Result<(impl Layer<S>, RollingLoggerGuard)>
6061
where
6162
S: tracing::Subscriber
6263
+ for<'s> tracing_subscriber::registry::LookupSpan<'s>,
6364
{
64-
const DEFAULT_LEVEL: tracing::Level = tracing::Level::WARN;
6565
const LOG_FILE_SUFFIX: &str = "log";
6666
let rolling_log_appender = tracing_appender::rolling::Builder::new()
6767
.rotation(tracing_appender::rolling::Rotation::DAILY)
6868
.filename_suffix(LOG_FILE_SUFFIX)
6969
.build(log_dir)?;
7070
let (non_blocking_rolling_log_writer, rolling_log_guard) =
7171
tracing_appender::non_blocking(rolling_log_appender);
72-
let level_filter =
73-
tracing_filter::Targets::new().with_default(DEFAULT_LEVEL);
72+
let level_filter = tracing_filter::Targets::new().with_default(log_level);
73+
7474
let rolling_log_layer = tracing_subscriber::fmt::layer()
7575
.compact()
7676
.with_ansi(false)
@@ -85,6 +85,7 @@ where
8585
fn set_tracing_subscriber(
8686
log_dir: Option<&Path>,
8787
log_level: tracing::Level,
88+
log_level_file: tracing::Level,
8889
) -> anyhow::Result<(LineBuffer, Option<RollingLoggerGuard>)> {
8990
let targets_filter = {
9091
let default_directives_str = targets_directive_str([
@@ -114,7 +115,7 @@ fn set_tracing_subscriber(
114115
let (rolling_log_layer, rolling_log_guard) = match log_dir {
115116
None => (None, None),
116117
Some(log_dir) => {
117-
let (layer, guard) = rolling_logger(log_dir)?;
118+
let (layer, guard) = rolling_logger(log_dir, log_level_file)?;
118119
(Some(layer), Some(guard))
119120
}
120121
};
@@ -136,18 +137,25 @@ fn set_tracing_subscriber(
136137
fn main() -> anyhow::Result<()> {
137138
let cli = cli::Cli::parse();
138139
let config = cli.get_config()?;
139-
let (line_buffer, _rolling_log_guard) =
140-
set_tracing_subscriber(config.log_dir.as_deref(), config.log_level)?;
141-
let app: Result<app::App, app::Error> =
142-
app::App::new(&config).inspect(|app| {
140+
let (line_buffer, _rolling_log_guard) = set_tracing_subscriber(
141+
config.log_dir.as_deref(),
142+
config.log_level,
143+
config.log_level_file,
144+
)?;
145+
let app: Result<app::App, app::Error> = app::App::new(&config)
146+
.inspect(|app| {
143147
// spawn rpc server
144148
app.runtime.spawn({
145149
let app = app.clone();
146150
async move {
147151
rpc_server::run_server(app, config.rpc_addr).await.unwrap()
148152
}
149153
});
154+
})
155+
.inspect_err(|err| {
156+
tracing::error!("application error: {:?}", err);
150157
});
158+
151159
if config.headless {
152160
tracing::info!("Running in headless mode");
153161
drop(line_buffer);

app/rpc_server.rs

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use jsonrpsee::{
77
types::ErrorObject,
88
};
99
use thunder::{
10-
node,
1110
types::{Address, PointedOutput, Txid, WithdrawalBundle},
12-
wallet::{self, Balance},
11+
wallet::Balance,
1312
};
1413
use thunder_app_rpc_api::RpcServer;
1514

16-
use crate::app::{self, App};
15+
use crate::app::App;
1716

1817
pub struct RpcServerImpl {
1918
app: App,
@@ -30,25 +29,10 @@ where
3029
let error = anyhow::Error::from(error);
3130
custom_err_msg(format!("{error:#}"))
3231
}
33-
34-
fn convert_app_err(err: app::Error) -> ErrorObject<'static> {
35-
let err = anyhow::anyhow!(err);
36-
tracing::error!("{err:#}");
37-
custom_err(err)
38-
}
39-
40-
fn convert_node_err(err: node::Error) -> ErrorObject<'static> {
41-
custom_err(err)
42-
}
43-
44-
fn convert_wallet_err(err: wallet::Error) -> ErrorObject<'static> {
45-
custom_err(err)
46-
}
47-
4832
#[async_trait]
4933
impl RpcServer for RpcServerImpl {
5034
async fn balance(&self) -> RpcResult<Balance> {
51-
self.app.wallet.get_balance().map_err(convert_wallet_err)
35+
self.app.wallet.get_balance().map_err(custom_err)
5236
}
5337

5438
async fn create_deposit(
@@ -64,14 +48,14 @@ impl RpcServer for RpcServerImpl {
6448
bitcoin::Amount::from_sat(value_sats),
6549
bitcoin::Amount::from_sat(fee_sats),
6650
)
67-
.map_err(convert_app_err)
51+
.map_err(custom_err)
6852
})
6953
.await
7054
.unwrap()
7155
}
7256

7357
async fn connect_peer(&self, addr: SocketAddr) -> RpcResult<()> {
74-
self.app.node.connect_peer(addr).map_err(convert_node_err)
58+
self.app.node.connect_peer(addr).map_err(custom_err)
7559
}
7660

7761
async fn format_deposit_address(
@@ -118,25 +102,18 @@ impl RpcServer for RpcServerImpl {
118102
}
119103

120104
async fn get_new_address(&self) -> RpcResult<Address> {
121-
self.app
122-
.wallet
123-
.get_new_address()
124-
.map_err(convert_wallet_err)
105+
self.app.wallet.get_new_address().map_err(custom_err)
125106
}
126107

127108
async fn get_wallet_addresses(&self) -> RpcResult<Vec<Address>> {
128-
let addrs = self
129-
.app
130-
.wallet
131-
.get_addresses()
132-
.map_err(convert_wallet_err)?;
109+
let addrs = self.app.wallet.get_addresses().map_err(custom_err)?;
133110
let mut res: Vec<_> = addrs.into_iter().collect();
134111
res.sort_by_key(|addr| addr.as_base58());
135112
Ok(res)
136113
}
137114

138115
async fn get_wallet_utxos(&self) -> RpcResult<Vec<PointedOutput>> {
139-
let utxos = self.app.wallet.get_utxos().map_err(convert_wallet_err)?;
116+
let utxos = self.app.wallet.get_utxos().map_err(custom_err)?;
140117
let utxos = utxos
141118
.into_iter()
142119
.map(|(outpoint, output)| PointedOutput { outpoint, output })
@@ -145,8 +122,7 @@ impl RpcServer for RpcServerImpl {
145122
}
146123

147124
async fn getblockcount(&self) -> RpcResult<u32> {
148-
let height =
149-
self.app.node.try_get_height().map_err(convert_node_err)?;
125+
let height = self.app.node.try_get_height().map_err(custom_err)?;
150126
let block_count = height.map_or(0, |height| height + 1);
151127
Ok(block_count)
152128
}
@@ -158,7 +134,7 @@ impl RpcServer for RpcServerImpl {
158134
.app
159135
.node
160136
.get_latest_failed_withdrawal_bundle_height()
161-
.map_err(convert_node_err)?;
137+
.map_err(custom_err)?;
162138
Ok(height)
163139
}
164140

@@ -168,7 +144,7 @@ impl RpcServer for RpcServerImpl {
168144
}
169145

170146
async fn list_utxos(&self) -> RpcResult<Vec<PointedOutput>> {
171-
let utxos = self.app.node.get_all_utxos().map_err(convert_node_err)?;
147+
let utxos = self.app.node.get_all_utxos().map_err(custom_err)?;
172148
let res = utxos
173149
.into_iter()
174150
.map(|(outpoint, output)| PointedOutput { outpoint, output })
@@ -178,10 +154,14 @@ impl RpcServer for RpcServerImpl {
178154

179155
async fn mine(&self, fee: Option<u64>) -> RpcResult<()> {
180156
let fee = fee.map(bitcoin::Amount::from_sat);
181-
self.app.local_pool.spawn_pinned({
182-
let app = self.app.clone();
183-
move || async move { app.mine(fee).await.map_err(convert_app_err) }
184-
}).await.unwrap()
157+
self.app
158+
.local_pool
159+
.spawn_pinned({
160+
let app = self.app.clone();
161+
move || async move { app.mine(fee).await.map_err(custom_err) }
162+
})
163+
.await
164+
.unwrap()
185165
}
186166

187167
async fn pending_withdrawal_bundle(
@@ -190,7 +170,7 @@ impl RpcServer for RpcServerImpl {
190170
self.app
191171
.node
192172
.get_pending_withdrawal_bundle()
193-
.map_err(convert_node_err)
173+
.map_err(custom_err)
194174
}
195175

196176
async fn openapi_schema(&self) -> RpcResult<utoipa::openapi::OpenApi> {
@@ -199,10 +179,7 @@ impl RpcServer for RpcServerImpl {
199179
}
200180

201181
async fn remove_from_mempool(&self, txid: Txid) -> RpcResult<()> {
202-
self.app
203-
.node
204-
.remove_from_mempool(txid)
205-
.map_err(convert_node_err)
182+
self.app.node.remove_from_mempool(txid).map_err(custom_err)
206183
}
207184

208185
async fn set_seed_from_mnemonic(&self, mnemonic: String) -> RpcResult<()> {
@@ -213,18 +190,12 @@ impl RpcServer for RpcServerImpl {
213190
let seed_bytes: [u8; 64] = seed.as_bytes().try_into().map_err(
214191
|err: <[u8; 64] as TryFrom<&[u8]>>::Error| custom_err(err),
215192
)?;
216-
self.app
217-
.wallet
218-
.set_seed(&seed_bytes)
219-
.map_err(convert_wallet_err)
193+
self.app.wallet.set_seed(&seed_bytes).map_err(custom_err)
220194
}
221195

222196
async fn sidechain_wealth_sats(&self) -> RpcResult<u64> {
223-
let sidechain_wealth = self
224-
.app
225-
.node
226-
.get_sidechain_wealth()
227-
.map_err(convert_node_err)?;
197+
let sidechain_wealth =
198+
self.app.node.get_sidechain_wealth().map_err(custom_err)?;
228199
Ok(sidechain_wealth.to_sat())
229200
}
230201

@@ -238,11 +209,8 @@ impl RpcServer for RpcServerImpl {
238209
value_sats: u64,
239210
fee_sats: u64,
240211
) -> RpcResult<Txid> {
241-
let accumulator = self
242-
.app
243-
.node
244-
.get_tip_accumulator()
245-
.map_err(convert_node_err)?;
212+
let accumulator =
213+
self.app.node.get_tip_accumulator().map_err(custom_err)?;
246214
let tx = self
247215
.app
248216
.wallet
@@ -252,9 +220,9 @@ impl RpcServer for RpcServerImpl {
252220
Amount::from_sat(value_sats),
253221
Amount::from_sat(fee_sats),
254222
)
255-
.map_err(convert_wallet_err)?;
223+
.map_err(custom_err)?;
256224
let txid = tx.txid();
257-
self.app.sign_and_send(tx).map_err(convert_app_err)?;
225+
self.app.sign_and_send(tx).map_err(custom_err)?;
258226
Ok(txid)
259227
}
260228

@@ -265,11 +233,8 @@ impl RpcServer for RpcServerImpl {
265233
fee_sats: u64,
266234
mainchain_fee_sats: u64,
267235
) -> RpcResult<Txid> {
268-
let accumulator = self
269-
.app
270-
.node
271-
.get_tip_accumulator()
272-
.map_err(convert_node_err)?;
236+
let accumulator =
237+
self.app.node.get_tip_accumulator().map_err(custom_err)?;
273238
let tx = self
274239
.app
275240
.wallet
@@ -280,9 +245,9 @@ impl RpcServer for RpcServerImpl {
280245
Amount::from_sat(mainchain_fee_sats),
281246
Amount::from_sat(fee_sats),
282247
)
283-
.map_err(convert_wallet_err)?;
248+
.map_err(custom_err)?;
284249
let txid = tx.txid();
285-
self.app.sign_and_send(tx).map_err(convert_app_err)?;
250+
self.app.sign_and_send(tx).map_err(custom_err)?;
286251
Ok(txid)
287252
}
288253
}

0 commit comments

Comments
 (0)