multi: add SpewLogClosure to avoid code repetition

This commit is contained in:
yyforyongyu 2024-07-25 22:18:00 +08:00
parent b6049ff94b
commit d992cf94d6
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
19 changed files with 57 additions and 144 deletions

View File

@ -4,7 +4,6 @@ import (
"net"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnutils"
@ -64,9 +63,8 @@ func Recover(backups []Single, restorer ChannelRestorer,
log.Infof("Attempting to connect to node=%x (addrs=%v) to "+
"restore ChannelPoint(%v)",
backup.RemoteNodePub.SerializeCompressed(),
lnutils.NewLogClosure(func() string {
return spew.Sdump(backups[i].Addresses)
}), backup.FundingOutpoint)
lnutils.SpewLogClosure(backups[i].Addresses),
backup.FundingOutpoint)
err = peerConnector.ConnectPeer(
backup.RemoteNodePub, backup.Addresses,

View File

@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
@ -733,10 +732,8 @@ justiceTxBroadcast:
}
finalTx := justiceTxs.spendAll
brarLog.Debugf("Broadcasting justice tx: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(finalTx)
}))
brarLog.Debugf("Broadcasting justice tx: %v", lnutils.SpewLogClosure(
finalTx))
// We'll now attempt to broadcast the transaction which finalized the
// channel's retribution against the cheating counter party.
@ -859,9 +856,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending commitment outs: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
lnutils.SpewLogClosure(tx))
err = b.cfg.PublishTransaction(tx, label)
if err != nil {
@ -876,9 +871,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending HTLC outs: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
lnutils.SpewLogClosure(tx))
err = b.cfg.PublishTransaction(tx, label)
if err != nil {
@ -893,9 +886,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending second-level HTLC output: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
lnutils.SpewLogClosure(tx))
err = b.cfg.PublishTransaction(tx, label)
if err != nil {

View File

@ -14,7 +14,6 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/fn"
@ -466,10 +465,8 @@ func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
}
log.Debugf("Starting ChannelArbitrator(%v), htlc_set=%v, state=%v",
c.cfg.ChanPoint, lnutils.NewLogClosure(func() string {
return spew.Sdump(c.activeHTLCs)
}), state.currentState,
)
c.cfg.ChanPoint, lnutils.SpewLogClosure(c.activeHTLCs),
state.currentState)
// Set our state from our starting state.
c.state = state.currentState
@ -959,10 +956,7 @@ func (c *ChannelArbitrator) stateStep(
// Otherwise, we'll log that we checked the HTLC actions as the
// commitment transaction has already been broadcast.
log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v",
c.cfg.ChanPoint,
lnutils.NewLogClosure(func() string {
return spew.Sdump(chainActions)
}))
c.cfg.ChanPoint, lnutils.SpewLogClosure(chainActions))
// Depending on the type of trigger, we'll either "tunnel"
// through to a farther state, or just proceed linearly to the
@ -1097,10 +1091,7 @@ func (c *ChannelArbitrator) stateStep(
// channel resolution state.
log.Infof("Broadcasting force close transaction %v, "+
"ChannelPoint(%v): %v", closeTx.TxHash(),
c.cfg.ChanPoint,
lnutils.NewLogClosure(func() string {
return spew.Sdump(closeTx)
}))
c.cfg.ChanPoint, lnutils.SpewLogClosure(closeTx))
// At this point, we'll now broadcast the commitment
// transaction itself.
@ -1225,9 +1216,7 @@ func (c *ChannelArbitrator) stateStep(
if len(pktsToSend) != 0 {
log.Debugf("ChannelArbitrator(%v): sending "+
"resolution message=%v", c.cfg.ChanPoint,
lnutils.NewLogClosure(func() string {
return spew.Sdump(pktsToSend)
}))
lnutils.SpewLogClosure(pktsToSend))
err := c.cfg.DeliverResolutionMsg(pktsToSend...)
if err != nil {
@ -2742,11 +2731,7 @@ func (c *ChannelArbitrator) notifyContractUpdate(upd *ContractUpdate) {
c.unmergedSet[upd.HtlcKey] = newHtlcSet(upd.Htlcs)
log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v",
c.cfg.ChanPoint,
lnutils.NewLogClosure(func() string {
return spew.Sdump(upd)
}),
)
c.cfg.ChanPoint, lnutils.SpewLogClosure(upd))
}
// updateActiveHTLCs merges the unmerged set of HTLCs from the link with

View File

@ -948,10 +948,7 @@ func (u *UtxoNursery) waitForSweepConf(classHeight uint32,
func (u *UtxoNursery) sweepCribOutput(classHeight uint32, baby *babyOutput) error {
utxnLog.Infof("Publishing CLTV-delayed HTLC output using timeout tx "+
"(txid=%v): %v", baby.timeoutTx.TxHash(),
lnutils.NewLogClosure(func() string {
return spew.Sdump(baby.timeoutTx)
}),
)
lnutils.SpewLogClosure(baby.timeoutTx))
// We'll now broadcast the HTLC transaction, then wait for it to be
// confirmed before transitioning it to kindergarten.

View File

@ -14,7 +14,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/bech32"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire"
@ -432,10 +431,7 @@ search:
}
log.Tracef("Retrieved SRV records from dns seed: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(addrs)
}),
)
lnutils.SpewLogClosure(addrs))
// Next, we'll need to issue an A record request for each of
// the nodes, skipping it if nothing comes back.

View File

@ -12,7 +12,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/batch"
"github.com/lightningnetwork/lnd/chainntnfs"
@ -1432,9 +1431,7 @@ func (b *Builder) processUpdate(msg interface{},
}
log.Tracef("New channel update applied: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(msg)
}))
lnutils.SpewLogClosure(msg))
b.stats.incNumChannelUpdates()
default:

View File

@ -5,7 +5,6 @@ import (
"fmt"
"sync"
"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
@ -801,10 +800,8 @@ func (cm *circuitMap) CommitCircuits(circuits ...*PaymentCircuit) (
inKeys = append(inKeys, circuit.Incoming)
}
log.Tracef("Committing fresh circuits: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(inKeys)
}))
log.Tracef("Committing fresh circuits: %v", lnutils.SpewLogClosure(
inKeys))
actions := &CircuitFwdActions{}
@ -950,10 +947,8 @@ func (cm *circuitMap) OpenCircuits(keystones ...Keystone) error {
return nil
}
log.Tracef("Opening finalized circuits: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(keystones)
}))
log.Tracef("Opening finalized circuits: %v", lnutils.SpewLogClosure(
keystones))
// Check that all keystones correspond to committed-but-unopened
// circuits.
@ -1081,10 +1076,8 @@ func (cm *circuitMap) CloseCircuit(outKey CircuitKey) (*PaymentCircuit, error) {
// circuit was already cleaned up at a different point in time.
func (cm *circuitMap) DeleteCircuits(inKeys ...CircuitKey) error {
log.Tracef("Deleting resolved circuits: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(inKeys)
}))
log.Tracef("Deleting resolved circuits: %v", lnutils.SpewLogClosure(
inKeys))
var (
closingCircuits = make(map[CircuitKey]struct{})

View File

@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog"
"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/channeldb"
@ -2537,13 +2536,9 @@ func (l *channelLink) updateCommitTx() error {
l.log.Tracef("revocation window exhausted, unable to send: "+
"%v, pend_updates=%v, dangling_closes%v",
l.channel.PendingLocalUpdateCount(),
lnutils.NewLogClosure(func() string {
return spew.Sdump(l.openedCircuits)
}),
lnutils.NewLogClosure(func() string {
return spew.Sdump(l.closedCircuits)
}),
)
lnutils.SpewLogClosure(l.openedCircuits),
lnutils.SpewLogClosure(l.closedCircuits))
return nil
} else if err != nil {
return err

View File

@ -614,10 +614,8 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64,
func (s *Switch) UpdateForwardingPolicies(
chanPolicies map[wire.OutPoint]models.ForwardingPolicy) {
log.Tracef("Updating link policies: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(chanPolicies)
}))
log.Tracef("Updating link policies: %v", lnutils.SpewLogClosure(
chanPolicies))
s.indexMtx.RLock()
@ -1216,9 +1214,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
log.Warnf("unable to find err source for "+
"outgoing_link=%v, errors=%v",
packet.outgoingChanID,
lnutils.NewLogClosure(func() string {
return spew.Sdump(linkErrs)
}))
lnutils.SpewLogClosure(linkErrs))
}
log.Tracef("incoming HTLC(%x) violated "+
@ -1999,9 +1995,7 @@ out:
log.Tracef("Acked %d settle fails: %v",
len(s.pendingSettleFails),
lnutils.NewLogClosure(func() string {
return spew.Sdump(s.pendingSettleFails)
}))
lnutils.SpewLogClosure(s.pendingSettleFails))
// Reset the pendingSettleFails buffer while keeping acquired
// memory.

View File

@ -15,7 +15,6 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/invoices"
@ -474,10 +473,7 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
}
log.Tracef("[addinvoice] adding new invoice %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(newInvoice)
}),
)
lnutils.SpewLogClosure(newInvoice))
// With all sanity checks passed, write the invoice to the database.
_, err = cfg.AddInvoice(ctx, newInvoice, paymentHash)

View File

@ -1,5 +1,7 @@
package lnutils
import "github.com/davecgh/go-spew/spew"
// LogClosure is used to provide a closure over expensive logging operations so
// don't have to be performed when the logging level doesn't warrant it.
type LogClosure func() string
@ -15,3 +17,11 @@ func (c LogClosure) String() string {
func NewLogClosure(c func() string) LogClosure {
return LogClosure(c)
}
// SpewLogClosure takes an interface and returns the string of it created from
// `spew.Sdump` in a LogClosure.
func SpewLogClosure(a any) LogClosure {
return func() string {
return spew.Sdump(a)
}
}

View File

@ -870,10 +870,7 @@ func (c *ChanCloser) ReceiveClosingSigned( //nolint:funlen
// With the closing transaction crafted, we'll now broadcast it
// to the network.
chancloserLog.Infof("Broadcasting cooperative close tx: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(closeTx)
}),
)
lnutils.SpewLogClosure(closeTx))
// Create a close channel label.
chanID := c.cfg.Channel.ShortChanID()

View File

@ -1870,10 +1870,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.localCommitChain.addCommitment(localCommit)
lc.log.Tracef("starting local commitment: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.localCommitChain.tail())
}),
)
lnutils.SpewLogClosure(lc.localCommitChain.tail()))
// We'll also do the same for the remote commitment chain.
remoteCommit, err := lc.diskCommitToMemCommit(
@ -1886,10 +1883,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.remoteCommitChain.addCommitment(remoteCommit)
lc.log.Tracef("starting remote commitment: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.remoteCommitChain.tail())
}),
)
lnutils.SpewLogClosure(lc.remoteCommitChain.tail()))
var (
pendingRemoteCommit *commitment
@ -1921,10 +1915,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.remoteCommitChain.addCommitment(pendingRemoteCommit)
lc.log.Debugf("pending remote commitment: %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.remoteCommitChain.tip())
}),
)
lnutils.SpewLogClosure(lc.remoteCommitChain.tip()))
// We'll also re-create the set of commitment keys needed to
// fully re-derive the state.
@ -3246,12 +3237,8 @@ func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor,
return nil, fmt.Errorf("unable to find parent entry "+
"%d in %v update log: %v\nUpdatelog: %v",
entry.ParentIndex, logName,
lnutils.NewLogClosure(func() string {
return spew.Sdump(entry)
}), lnutils.NewLogClosure(func() string {
return spew.Sdump(updateLog)
}),
)
lnutils.SpewLogClosure(entry),
lnutils.SpewLogClosure(updateLog))
// The parent add height should never be zero at this point. If
// that's the case we probably forgot to send a new commitment.
@ -4250,10 +4237,7 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
"their_balance=%v, commit_tx: %v",
newCommitView.ourBalance,
newCommitView.theirBalance,
lnutils.NewLogClosure(func() string {
return spew.Sdump(newCommitView.txn)
}),
)
lnutils.SpewLogClosure(newCommitView.txn))
// With the commitment view constructed, if there are any HTLC's, we'll
// need to generate signatures of each of them for the remote party's
@ -5210,10 +5194,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
lc.log.Tracef("local chain: our_balance=%v, "+
"their_balance=%v, commit_tx: %v",
localCommitmentView.ourBalance, localCommitmentView.theirBalance,
lnutils.NewLogClosure(func() string {
return spew.Sdump(localCommitmentView.txn)
}),
)
lnutils.SpewLogClosure(localCommitmentView.txn))
// As an optimization, we'll generate a series of jobs for the worker
// pool to verify each of the HTLC signatures presented. Once

View File

@ -2184,9 +2184,7 @@ func (p *Brontide) logWireMessage(msg lnwire.Message, read bool) {
prefix = "writeMessage to peer"
}
p.log.Tracef(prefix+": %v", lnutils.NewLogClosure(func() string {
return spew.Sdump(msg)
}))
p.log.Tracef(prefix+": %v", lnutils.SpewLogClosure(msg))
}
// writeMessage writes and flushes the target lnwire.Message to the remote peer.

View File

@ -5,7 +5,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btclog"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
@ -433,7 +432,7 @@ func (p *paymentSession) UpdateAdditionalEdge(msg *lnwire.ChannelUpdate,
policy.FeeProportionalMillionths = lnwire.MilliSatoshi(msg.FeeRate)
log.Debugf("New private channel update applied: %v",
lnutils.NewLogClosure(func() string { return spew.Sdump(msg) }))
lnutils.SpewLogClosure(msg))
return true
}

View File

@ -670,10 +670,7 @@ func (r *ChannelRouter) FindRoute(req *RouteRequest) (*route.Route, float64,
}
go log.Tracef("Obtained path to send %v to %x: %v",
req.Amount, req.Target, lnutils.NewLogClosure(func() string {
return spew.Sdump(route)
}),
)
req.Amount, req.Target, lnutils.SpewLogClosure(route))
return route, probability, nil
}
@ -1091,11 +1088,8 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
return nil, err
}
log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v",
htlcHash, lnutils.NewLogClosure(func() string {
return spew.Sdump(rt)
}),
)
log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", htlcHash,
lnutils.SpewLogClosure(rt))
// Since the HTLC hashes and preimages are specified manually over the
// RPC for SendToRoute requests, we don't have to worry about creating

View File

@ -5846,9 +5846,7 @@ func (r *rpcServer) LookupInvoice(ctx context.Context,
}
rpcsLog.Tracef("[lookupinvoice] located invoice %v",
lnutils.NewLogClosure(func() string {
return spew.Sdump(invoice)
}))
lnutils.SpewLogClosure(invoice))
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
&invoice, r.cfg.ActiveNetParams.Params,

View File

@ -12,7 +12,6 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/chain"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
@ -313,10 +312,8 @@ func (t *TxPublisher) isNeutrinoBackend() bool {
//
// NOTE: part of the Bumper interface.
func (t *TxPublisher) Broadcast(req *BumpRequest) (<-chan *BumpResult, error) {
log.Tracef("Received broadcast request: %s", lnutils.NewLogClosure(
func() string {
return spew.Sdump(req)
})())
log.Tracef("Received broadcast request: %s", lnutils.SpewLogClosure(
req))
// Attempt an initial broadcast which is guaranteed to comply with the
// RBF rules.

View File

@ -1377,10 +1377,7 @@ func (s *UtxoSweeper) handleInputSpent(spend *chainntnfs.SpendDetail) {
log.Debugf("Detected third party spend related to in flight "+
"inputs (is_ours=%v): %v", isOurTx,
lnutils.NewLogClosure(func() string {
return spew.Sdump(spend.SpendingTx)
}),
)
lnutils.SpewLogClosure(spend.SpendingTx))
}
// We now use the spending tx to update the state of the inputs.