From 3259557a4631495057d19c5682383004ef267507 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 8 Nov 2022 20:25:00 +0800 Subject: [PATCH] routing: fix format and add more docs --- routing/payment_lifecycle.go | 26 ++++++++++------ routing/router.go | 59 ++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/routing/payment_lifecycle.go b/routing/payment_lifecycle.go index 9a70925a7..11deb6574 100644 --- a/routing/payment_lifecycle.go +++ b/routing/payment_lifecycle.go @@ -34,9 +34,16 @@ type paymentLifecycle struct { // paymentState holds a number of key insights learned from a given MPPayment // that we use to determine what to do on each payment loop iteration. type paymentState struct { + // numShardsInFlight specifies the number of HTLCs the payment is + // waiting results for. numShardsInFlight int - remainingAmt lnwire.MilliSatoshi - remainingFees lnwire.MilliSatoshi + + // remainingAmt specifies how much more money to be sent. + remainingAmt lnwire.MilliSatoshi + + // remainingFees specifies the remaining budget that can be used as + // fees. + remainingFees lnwire.MilliSatoshi // terminate indicates the payment is in its final stage and no more // shards should be launched. This value is true if we have an HTLC @@ -65,9 +72,9 @@ func (ps paymentState) needWaitForShards() bool { return ps.terminate || ps.remainingAmt == 0 } -// fetchPaymentState will query the db for the latest payment state -// information we need to act on every iteration of the payment loop and update -// the paymentState. +// fetchPaymentState will query the db for the latest payment state information +// we need to act on every iteration of the payment loop and update the +// paymentState. func (p *paymentLifecycle) fetchPaymentState() (*channeldb.MPPayment, *paymentState, error) { @@ -84,8 +91,8 @@ func (p *paymentLifecycle) fetchPaymentState() (*channeldb.MPPayment, // Sanity check we haven't sent a value larger than the payment amount. totalAmt := payment.Info.Value if sentAmt > totalAmt { - return nil, nil, fmt.Errorf("amount sent %v exceeds "+ - "total amount %v", sentAmt, totalAmt) + return nil, nil, fmt.Errorf("amount sent %v exceeds total "+ + "amount %v", sentAmt, totalAmt) } // We'll subtract the used fee from our fee budget, but allow the fees @@ -635,8 +642,7 @@ func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) ( attempt.AttemptID, &attempt.Route, ) if err != nil { - log.Errorf("Error reporting payment success to mc: %v", - err) + log.Errorf("Error reporting payment success to mc: %v", err) } // In case of success we atomically store settle result to the DB move @@ -649,7 +655,7 @@ func (p *shardHandler) collectResult(attempt *channeldb.HTLCAttemptInfo) ( }, ) if err != nil { - log.Errorf("Unable to succeed payment attempt: %v", err) + log.Errorf("Unable to settle payment attempt: %v", err) return nil, err } diff --git a/routing/router.go b/routing/router.go index c6a5cec21..21a3206d9 100644 --- a/routing/router.go +++ b/routing/router.go @@ -101,20 +101,24 @@ type ChannelGraphSource interface { // AddNode is used to add information about a node to the router // database. If the node with this pubkey is not present in an existing // channel, it will be ignored. - AddNode(node *channeldb.LightningNode, op ...batch.SchedulerOption) error + AddNode(node *channeldb.LightningNode, + op ...batch.SchedulerOption) error // AddEdge is used to add edge/channel to the topology of the router, // after all information about channel will be gathered this // edge/channel might be used in construction of payment path. - AddEdge(edge *channeldb.ChannelEdgeInfo, op ...batch.SchedulerOption) error + AddEdge(edge *channeldb.ChannelEdgeInfo, + op ...batch.SchedulerOption) error // AddProof updates the channel edge info with proof which is needed to // properly announce the edge to the rest of the network. - AddProof(chanID lnwire.ShortChannelID, proof *channeldb.ChannelAuthProof) error + AddProof(chanID lnwire.ShortChannelID, + proof *channeldb.ChannelAuthProof) error // UpdateEdge is used to update edge information, without this message // edge considered as not fully constructed. - UpdateEdge(policy *channeldb.ChannelEdgePolicy, op ...batch.SchedulerOption) error + UpdateEdge(policy *channeldb.ChannelEdgePolicy, + op ...batch.SchedulerOption) error // IsStaleNode returns true if the graph source has a node announcement // for the target node with a more recent timestamp. This method will @@ -152,8 +156,9 @@ type ChannelGraphSource interface { CurrentBlockHeight() (uint32, error) // GetChannelByID return the channel by the channel id. - GetChannelByID(chanID lnwire.ShortChannelID) (*channeldb.ChannelEdgeInfo, - *channeldb.ChannelEdgePolicy, *channeldb.ChannelEdgePolicy, error) + GetChannelByID(chanID lnwire.ShortChannelID) ( + *channeldb.ChannelEdgeInfo, *channeldb.ChannelEdgePolicy, + *channeldb.ChannelEdgePolicy, error) // FetchLightningNode attempts to look up a target node by its identity // public key. channeldb.ErrGraphNodeNotFound is returned if the node @@ -226,8 +231,8 @@ type MissionController interface { failureSourceIdx *int, failure lnwire.FailureMessage) ( *channeldb.FailureReason, error) - // ReportPaymentSuccess reports a successful payment to mission control as input - // for future probability estimates. + // ReportPaymentSuccess reports a successful payment to mission control + // as input for future probability estimates. ReportPaymentSuccess(attemptID uint64, rt *route.Route) error // GetProbability is expected to return the success probability of a @@ -410,8 +415,8 @@ type ChannelRouter struct { // when doing any path finding. selfNode *channeldb.LightningNode - // cachedGraph is an instance of routingGraph that caches the source node as - // well as the channel graph itself in memory. + // cachedGraph is an instance of routingGraph that caches the source + // node as well as the channel graph itself in memory. cachedGraph routingGraph // newBlocks is a channel in which new blocks connected to the end of @@ -741,8 +746,8 @@ func (r *ChannelRouter) syncGraphWithChain() error { } } - log.Infof("Prune tip for Channel Graph: height=%v, hash=%v", pruneHeight, - pruneHash) + log.Infof("Prune tip for Channel Graph: height=%v, hash=%v", + pruneHeight, pruneHash) switch { @@ -931,9 +936,9 @@ func (r *ChannelRouter) pruneZombieChans() error { return nil } - // If AssumeChannelValid is present we'll look at the disabled bit for both - // edges. If they're both disabled, then we can interpret this as the - // channel being closed and can prune it from our graph. + // If AssumeChannelValid is present we'll look at the disabled bit for + // both edges. If they're both disabled, then we can interpret this as + // the channel being closed and can prune it from our graph. if r.cfg.AssumeChannelValid { disabledChanIDs, err := r.cfg.Graph.DisabledChannelIDs() if err != nil { @@ -1905,7 +1910,9 @@ func generateSphinxPacket(rt *route.Route, paymentHash []byte, log.Tracef("Constructed per-hop payloads for payment_hash=%x: %v", paymentHash[:], newLogClosure(func() string { - path := make([]sphinx.OnionHop, sphinxPath.TrueRouteLength()) + path := make( + []sphinx.OnionHop, sphinxPath.TrueRouteLength(), + ) for i := range path { hopCopy := sphinxPath[i] path[i] = hopCopy @@ -2390,10 +2397,6 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route, // to reach the destination. Additionally, the payment preimage will also be // returned. // -// The existing attempt argument should be set to nil if this is a payment that -// haven't had any payment attempt sent to the switch yet. If it has had an -// attempt already, it should be passed such that the result can be retrieved. -// // This method relies on the ControlTower's internal payment state machine to // carry out its execution. After restarts it is safe, and assumed, that the // router will call this method for every payment still in-flight according to @@ -2429,7 +2432,6 @@ func (r *ChannelRouter) sendPayment(feeLimit lnwire.MilliSatoshi, } return p.resumePayment() - } // extractChannelUpdate examines the error and extracts the channel update. @@ -2627,10 +2629,13 @@ func (r *ChannelRouter) FetchLightningNode( // ForEachNode is used to iterate over every node in router topology. // // NOTE: This method is part of the ChannelGraphSource interface. -func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) error { - return r.cfg.Graph.ForEachNode(func(_ kvdb.RTx, n *channeldb.LightningNode) error { - return cb(n) - }) +func (r *ChannelRouter) ForEachNode( + cb func(*channeldb.LightningNode) error) error { + + return r.cfg.Graph.ForEachNode( + func(_ kvdb.RTx, n *channeldb.LightningNode) error { + return cb(n) + }) } // ForAllOutgoingChannels is used to iterate over all outgoing channels owned by @@ -2699,7 +2704,9 @@ func (r *ChannelRouter) IsPublicNode(node route.Vertex) (bool, error) { // // NOTE: This method is part of the ChannelGraphSource interface. func (r *ChannelRouter) IsKnownEdge(chanID lnwire.ShortChannelID) bool { - _, _, exists, isZombie, _ := r.cfg.Graph.HasChannelEdge(chanID.ToUint64()) + _, _, exists, isZombie, _ := r.cfg.Graph.HasChannelEdge( + chanID.ToUint64(), + ) return exists || isZombie }