From 9f9e4aa1712edec2a5a523475311eb32c459c0cb Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 12 Oct 2023 12:28:44 +0200 Subject: [PATCH] nd: ignore RpcInWarmup bitcoind RPC error the daemon now pretends the report is sent over to ngui on such errors. this is a stop-gap to avoid writing errors on tty. see linked issue. part of https://git.qcode.ch/nakamochi/ndg/issues/30 --- src/nd/Daemon.zig | 110 ++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/src/nd/Daemon.zig b/src/nd/Daemon.zig index 6e65322..8a666e9 100644 --- a/src/nd/Daemon.zig +++ b/src/nd/Daemon.zig @@ -567,54 +567,50 @@ fn readWPACtrlMsg(self: *Daemon) !void { } fn sendOnchainReport(self: *Daemon) !void { - var client = bitcoindrpc.Client{ - .allocator = self.allocator, - .cookiepath = "/ssd/bitcoind/mainnet/.cookie", - }; - const bcinfo = try client.call(.getblockchaininfo, {}); - defer bcinfo.deinit(); - const netinfo = try client.call(.getnetworkinfo, {}); - defer netinfo.deinit(); - const mempool = try client.call(.getmempoolinfo, {}); - defer mempool.deinit(); - - const balance: ?lndhttp.Client.Result(.walletbalance) = blk: { // lndhttp.WalletBalance - var lndc = lndhttp.Client.init(.{ - .allocator = self.allocator, - .tlscert_path = "/home/lnd/.lnd/tls.cert", - .macaroon_ro_path = "/ssd/lnd/data/chain/bitcoin/mainnet/readonly.macaroon", - }) catch break :blk null; - defer lndc.deinit(); - const res = lndc.call(.walletbalance, {}) catch break :blk null; - break :blk res; + const stats = self.fetchOnchainStats() catch |err| { + switch (err) { + error.FileNotFound, // cookie file might not exist yet + error.RpcInWarmup, + // bitcoind is still starting up: pretend the repost is sent. + // TODO: report actual startup ptogress to the UI + // https://git.qcode.ch/nakamochi/ndg/issues/30 + => return, + // otherwise, propagate the error to the caller. + else => return err, + } }; - defer if (balance) |bal| bal.deinit(); + defer { + stats.bcinfo.deinit(); + stats.netinfo.deinit(); + stats.mempool.deinit(); + if (stats.balance) |bal| bal.deinit(); + } const btcrep: comm.Message.OnchainReport = .{ - .blocks = bcinfo.value.blocks, - .headers = bcinfo.value.headers, - .timestamp = bcinfo.value.time, - .hash = bcinfo.value.bestblockhash, - .ibd = bcinfo.value.initialblockdownload, - .diskusage = bcinfo.value.size_on_disk, - .version = netinfo.value.subversion, - .conn_in = netinfo.value.connections_in, - .conn_out = netinfo.value.connections_out, - .warnings = bcinfo.value.warnings, // TODO: netinfo.result.warnings + .blocks = stats.bcinfo.value.blocks, + .headers = stats.bcinfo.value.headers, + .timestamp = stats.bcinfo.value.time, + .hash = stats.bcinfo.value.bestblockhash, + .ibd = stats.bcinfo.value.initialblockdownload, + .diskusage = stats.bcinfo.value.size_on_disk, + .version = stats.netinfo.value.subversion, + .conn_in = stats.netinfo.value.connections_in, + .conn_out = stats.netinfo.value.connections_out, + .warnings = stats.bcinfo.value.warnings, // TODO: netinfo.result.warnings .localaddr = &.{}, // TODO: populate // something similar to this: // @round(bcinfo.verificationprogress * 100) .verifyprogress = 0, .mempool = .{ - .loaded = mempool.value.loaded, - .txcount = mempool.value.size, - .usage = mempool.value.usage, - .max = mempool.value.maxmempool, - .totalfee = mempool.value.total_fee, - .minfee = mempool.value.mempoolminfee, - .fullrbf = mempool.value.fullrbf, + .loaded = stats.mempool.value.loaded, + .txcount = stats.mempool.value.size, + .usage = stats.mempool.value.usage, + .max = stats.mempool.value.maxmempool, + .totalfee = stats.mempool.value.total_fee, + .minfee = stats.mempool.value.mempoolminfee, + .fullrbf = stats.mempool.value.fullrbf, }, - .balance = if (balance) |bal| .{ + .balance = if (stats.balance) |bal| .{ .source = .lnd, .total = bal.value.total_balance, .confirmed = bal.value.confirmed_balance, @@ -627,6 +623,42 @@ fn sendOnchainReport(self: *Daemon) !void { try comm.write(self.allocator, self.uiwriter, .{ .onchain_report = btcrep }); } +const OnchainStats = struct { + bcinfo: bitcoindrpc.Client.Result(.getblockchaininfo), + netinfo: bitcoindrpc.Client.Result(.getnetworkinfo), + mempool: bitcoindrpc.Client.Result(.getmempoolinfo), + // lnd wallet may be uninitialized + balance: ?lndhttp.Client.Result(.walletbalance), +}; + +/// callers own returned value. +fn fetchOnchainStats(self: *Daemon) !OnchainStats { + var client = bitcoindrpc.Client{ + .allocator = self.allocator, + .cookiepath = "/ssd/bitcoind/mainnet/.cookie", + }; + const bcinfo = try client.call(.getblockchaininfo, {}); + const netinfo = try client.call(.getnetworkinfo, {}); + const mempool = try client.call(.getmempoolinfo, {}); + + const balance: ?lndhttp.Client.Result(.walletbalance) = blk: { // lndhttp.WalletBalance + var lndc = lndhttp.Client.init(.{ + .allocator = self.allocator, + .tlscert_path = "/home/lnd/.lnd/tls.cert", + .macaroon_ro_path = "/ssd/lnd/data/chain/bitcoin/mainnet/readonly.macaroon", + }) catch break :blk null; + defer lndc.deinit(); + const res = lndc.call(.walletbalance, {}) catch break :blk null; + break :blk res; + }; + return .{ + .bcinfo = bcinfo, + .netinfo = netinfo, + .mempool = mempool, + .balance = balance, + }; +} + fn sendLightningReport(self: *Daemon) !void { var client = try lndhttp.Client.init(.{ .allocator = self.allocator,