Skip to content

Commit

Permalink
multi: add SpewLogClosure to avoid code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Jul 25, 2024
1 parent b6049ff commit d992cf9
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 144 deletions.
6 changes: 2 additions & 4 deletions chanbackup/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 5 additions & 14 deletions contractcourt/breach_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
27 changes: 6 additions & 21 deletions contractcourt/channel_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions contractcourt/utxonursery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions discovery/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions graph/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 6 additions & 13 deletions htlcswitch/circuit_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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{})
Expand Down
11 changes: 3 additions & 8 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions htlcswitch/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 "+
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions lnrpc/invoicesrpc/addinvoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lnutils/log.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
5 changes: 1 addition & 4 deletions lnwallet/chancloser/chancloser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading

0 comments on commit d992cf9

Please sign in to comment.