@@ -2084,7 +2084,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
2084
2084
// A new authenticated channel edge update has arrived. This indicates
2085
2085
// that the directional information for an already known channel has
2086
2086
// been updated.
2087
- case * lnwire.ChannelUpdate1 :
2087
+ case lnwire.ChannelUpdate :
2088
2088
return d .handleChanUpdate (nMsg , msg , schedulerOp )
2089
2089
2090
2090
// A new signature announcement has been received. This indicates
@@ -2843,40 +2843,45 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
2843
2843
2844
2844
// handleChanUpdate processes a new channel update.
2845
2845
func (d * AuthenticatedGossiper ) handleChanUpdate (nMsg * networkMsg ,
2846
- upd * lnwire.ChannelUpdate1 ,
2847
- ops []batch. SchedulerOption ) ([] networkMsg , bool ) {
2846
+ upd lnwire.ChannelUpdate , ops []batch. SchedulerOption ) ([] networkMsg ,
2847
+ bool ) {
2848
2848
2849
- log .Debugf ("Processing ChannelUpdate1: peer=%v, short_chan_id=%v, " ,
2850
- nMsg .peer , upd .ShortChannelID .ToUint64 ())
2849
+ var (
2850
+ scid = upd .SCID ()
2851
+ chainHash = upd .GetChainHash ()
2852
+ )
2853
+
2854
+ log .Debugf ("Processing ChannelUpdate: peer=%v, short_chan_id=%v, " ,
2855
+ nMsg .peer , scid )
2851
2856
2852
2857
// We'll ignore any channel updates that target any chain other than
2853
2858
// the set of chains we know of.
2854
- if ! bytes .Equal (upd .ChainHash [:], d .cfg .ChainHash [:]) {
2855
- err := fmt .Errorf ("ignoring ChannelUpdate1 from chain=%v, " +
2856
- "gossiper on chain=%v" , upd .ChainHash , d .cfg .ChainHash )
2859
+ if ! bytes .Equal (chainHash [:], d .cfg .ChainHash [:]) {
2860
+ err := fmt .Errorf ("ignoring %s from chain=%v, " +
2861
+ "gossiper on chain=%v" , upd .MsgType (), chainHash ,
2862
+ d .cfg .ChainHash )
2857
2863
log .Errorf (err .Error ())
2858
2864
2859
2865
key := newRejectCacheKey (
2860
- upd .ShortChannelID .ToUint64 (),
2861
- sourceToPub (nMsg .source ),
2866
+ scid .ToUint64 (), sourceToPub (nMsg .source ),
2862
2867
)
2863
2868
_ , _ = d .recentRejects .Put (key , & cachedReject {})
2864
2869
2865
2870
nMsg .err <- err
2866
2871
return nil , false
2867
2872
}
2868
2873
2869
- blockHeight := upd .ShortChannelID .BlockHeight
2870
- shortChanID := upd .ShortChannelID .ToUint64 ()
2874
+ blockHeight := upd .SCID () .BlockHeight
2875
+ shortChanID := upd .SCID () .ToUint64 ()
2871
2876
2872
2877
// If the advertised inclusionary block is beyond our knowledge of the
2873
2878
// chain tip, then we'll put the announcement in limbo to be fully
2874
2879
// verified once we advance forward in the chain. If the update has an
2875
2880
// alias SCID, we'll skip the isPremature check. This is necessary
2876
2881
// since aliases start at block height 16_000_000.
2877
2882
d .Lock ()
2878
- if nMsg .isRemote && ! d .cfg .IsAlias (upd . ShortChannelID ) &&
2879
- d .isPremature (upd . ShortChannelID , 0 , nMsg ) {
2883
+ if nMsg .isRemote && ! d .cfg .IsAlias (scid ) &&
2884
+ d .isPremature (scid , 0 , nMsg ) {
2880
2885
2881
2886
log .Warnf ("Update announcement for short_chan_id(%v), is " +
2882
2887
"premature: advertises height %v, only height %v is " +
@@ -2887,31 +2892,28 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
2887
2892
}
2888
2893
d .Unlock ()
2889
2894
2890
- // Before we perform any of the expensive checks below, we'll check
2891
- // whether this update is stale or is for a zombie channel in order to
2892
- // quickly reject it.
2893
- timestamp := time .Unix (int64 (upd .Timestamp ), 0 )
2894
-
2895
2895
// Fetch the SCID we should be using to lock the channelMtx and make
2896
2896
// graph queries with.
2897
- graphScid , err := d .cfg .FindBaseByAlias (upd . ShortChannelID )
2897
+ graphScid , err := d .cfg .FindBaseByAlias (scid )
2898
2898
if err != nil {
2899
2899
// Fallback and set the graphScid to the peer-provided SCID.
2900
2900
// This will occur for non-option-scid-alias channels and for
2901
2901
// public option-scid-alias channels after 6 confirmations.
2902
2902
// Once public option-scid-alias channels have 6 confs, we'll
2903
2903
// ignore ChannelUpdates with one of their aliases.
2904
- graphScid = upd . ShortChannelID
2904
+ graphScid = scid
2905
2905
}
2906
2906
2907
+ // Before we perform any of the expensive checks below, we'll check
2908
+ // whether this update is stale or is for a zombie channel in order to
2909
+ // quickly reject it.
2907
2910
if d .cfg .Router .IsStaleEdgePolicy (graphScid , upd ) {
2908
2911
log .Debugf ("Ignored stale edge policy for short_chan_id(%v): " +
2909
2912
"peer=%v, msg=%s, is_remote=%v" , shortChanID ,
2910
2913
nMsg .peer , nMsg .msg .MsgType (), nMsg .isRemote ,
2911
2914
)
2912
2915
2913
2916
nMsg .err <- nil
2914
-
2915
2917
return nil , true
2916
2918
}
2917
2919
@@ -2953,7 +2955,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
2953
2955
// If the edge corresponding to this ChannelUpdate1 was not
2954
2956
// found in the graph, this might be a channel in the process
2955
2957
// of being opened, and we haven't processed our own
2956
- // ChannelAnnouncement1 yet, hence it is not found in the
2958
+ // ChannelAnnouncement yet, hence it is not found in the
2957
2959
// graph. This usually gets resolved after the channel proofs
2958
2960
// are exchanged and the channel is broadcasted to the rest of
2959
2961
// the network, but in case this is a private channel this
@@ -2990,7 +2992,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
2990
2992
})
2991
2993
}
2992
2994
2993
- log .Debugf ("Got ChannelUpdate1 for edge not found in graph" +
2995
+ log .Debugf ("Got ChannelUpdate for edge not found in graph" +
2994
2996
"(shortChanID=%v), saving for reprocessing later" ,
2995
2997
shortChanID )
2996
2998
@@ -3006,7 +3008,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3006
3008
nMsg .err <- err
3007
3009
3008
3010
key := newRejectCacheKey (
3009
- upd . ShortChannelID .ToUint64 (),
3011
+ scid .ToUint64 (),
3010
3012
sourceToPub (nMsg .source ),
3011
3013
)
3012
3014
_ , _ = d .recentRejects .Put (key , & cachedReject {})
@@ -3020,21 +3022,20 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3020
3022
var (
3021
3023
pubKey * btcec.PublicKey
3022
3024
edgeToUpdate models.ChannelEdgePolicy
3025
+ direction int
3023
3026
)
3024
- direction := upd .ChannelFlags & lnwire .ChanUpdateDirection
3025
- switch direction {
3026
- case 0 :
3027
+ if upd .IsNode1 () {
3027
3028
pubKey , _ = chanInfo .NodeKey1 ()
3028
3029
edgeToUpdate = e1
3029
- case 1 :
3030
+ direction = 0
3031
+ } else {
3030
3032
pubKey , _ = chanInfo .NodeKey2 ()
3031
3033
edgeToUpdate = e2
3034
+ direction = 1
3032
3035
}
3033
3036
3034
- var chanID = chanInfo .GetChanID ()
3035
-
3036
3037
log .Debugf ("Validating ChannelUpdate1: channel=%v, from node=%x, has " +
3037
- "edge=%v" , chanID , pubKey .SerializeCompressed (),
3038
+ "edge=%v" , chanInfo . GetChanID () , pubKey .SerializeCompressed (),
3038
3039
edgeToUpdate != nil )
3039
3040
3040
3041
// Validate the channel announcement with the expected public key and
@@ -3046,38 +3047,21 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3046
3047
if err != nil {
3047
3048
rErr := fmt .Errorf ("unable to validate channel update " +
3048
3049
"announcement for short_chan_id=%v: %v" ,
3049
- spew .Sdump (upd . ShortChannelID ), err )
3050
+ spew .Sdump (scid ), err )
3050
3051
3051
3052
log .Error (rErr )
3052
3053
nMsg .err <- rErr
3053
3054
return nil , false
3054
3055
}
3055
3056
3056
- var edge * models.ChannelEdgePolicy1
3057
- if edgeToUpdate != nil {
3058
- var ok bool
3059
- edge , ok = edgeToUpdate .(* models.ChannelEdgePolicy1 )
3060
- if ! ok {
3061
- rErr := fmt .Errorf ("expected " +
3062
- "*models.ChannelEdgePolicy1, got: %T" ,
3063
- edgeToUpdate )
3064
-
3065
- log .Error (rErr )
3066
- nMsg .err <- rErr
3067
-
3068
- return nil , false
3069
- }
3070
- }
3071
-
3072
3057
// If we have a previous version of the edge being updated, we'll want
3073
3058
// to rate limit its updates to prevent spam throughout the network.
3074
- if nMsg .isRemote && edge != nil {
3059
+ if nMsg .isRemote && edgeToUpdate != nil {
3075
3060
// If it's a keep-alive update, we'll only propagate one if
3076
3061
// it's been a day since the previous. This follows our own
3077
3062
// heuristic of sending keep-alive updates after the same
3078
3063
// duration (see retransmitStaleAnns).
3079
- timeSinceLastUpdate := timestamp .Sub (edge .LastUpdate )
3080
- isKeepAlive , err := IsKeepAliveUpdate (upd , edge )
3064
+ isKeepAlive , err := IsKeepAliveUpdate (upd , edgeToUpdate )
3081
3065
if err != nil {
3082
3066
log .Errorf ("Could not determine if update is " +
3083
3067
"keepalive: %v" , err )
@@ -3087,7 +3071,18 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3087
3071
}
3088
3072
3089
3073
if isKeepAlive {
3090
- if timeSinceLastUpdate < d .cfg .RebroadcastInterval {
3074
+ within , err := d .updateWithinRebroadcastInterval (
3075
+ upd , edgeToUpdate ,
3076
+ )
3077
+ if err != nil {
3078
+ log .Errorf ("Could not determine if update is " +
3079
+ "within rebroadcast interval: %v" , err )
3080
+ nMsg .err <- err
3081
+
3082
+ return nil , false
3083
+ }
3084
+
3085
+ if ! within {
3091
3086
log .Debugf ("Ignoring keep alive update not " +
3092
3087
"within %v period for channel %v" ,
3093
3088
d .cfg .RebroadcastInterval , shortChanID )
@@ -3106,7 +3101,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3106
3101
// multiple aliases for a channel and we may otherwise
3107
3102
// rate-limit only a single alias of the channel,
3108
3103
// instead of the whole channel.
3109
- baseScid := chanID
3104
+ baseScid := chanInfo . GetChanID ()
3110
3105
d .Lock ()
3111
3106
rls , ok := d .chanUpdateRateLimiter [baseScid ]
3112
3107
if ! ok {
@@ -3137,18 +3132,23 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3137
3132
// different alias. This might mean that SigBytes is incorrect as it
3138
3133
// signs a different SCID than the database SCID, but since there will
3139
3134
// only be a difference if AuthProof == nil, this is fine.
3140
- update := & models.ChannelEdgePolicy1 {
3141
- SigBytes : upd .Signature .ToSignatureBytes (),
3142
- ChannelID : chanID ,
3143
- LastUpdate : timestamp ,
3144
- MessageFlags : upd .MessageFlags ,
3145
- ChannelFlags : upd .ChannelFlags ,
3146
- TimeLockDelta : upd .TimeLockDelta ,
3147
- MinHTLC : upd .HtlcMinimumMsat ,
3148
- MaxHTLC : upd .HtlcMaximumMsat ,
3149
- FeeBaseMSat : lnwire .MilliSatoshi (upd .BaseFee ),
3150
- FeeProportionalMillionths : lnwire .MilliSatoshi (upd .FeeRate ),
3151
- ExtraOpaqueData : upd .ExtraOpaqueData ,
3135
+ update , err := models .EdgePolicyFromUpdate (upd )
3136
+ if err != nil {
3137
+ rErr := fmt .Errorf ("unable to convert update to policy for " +
3138
+ "short_chan_id=%v: %v" , spew .Sdump (scid ), err )
3139
+
3140
+ log .Error (rErr )
3141
+ nMsg .err <- rErr
3142
+
3143
+ return nil , false
3144
+ }
3145
+ switch upd := update .(type ) {
3146
+ case * models.ChannelEdgePolicy1 :
3147
+ upd .ChannelID = chanInfo .GetChanID ()
3148
+ case * models.ChannelEdgePolicy2 :
3149
+ upd .ShortChannelID .Val = lnwire .NewShortChanIDFromInt (
3150
+ chanInfo .GetChanID (),
3151
+ )
3152
3152
}
3153
3153
3154
3154
if err := d .cfg .Router .UpdateEdge (update , ops ... ); err != nil {
@@ -3164,7 +3164,8 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3164
3164
// Since we know the stored SCID in the graph, we'll
3165
3165
// cache that SCID.
3166
3166
key := newRejectCacheKey (
3167
- chanID , sourceToPub (nMsg .source ),
3167
+ chanInfo .GetChanID (),
3168
+ sourceToPub (nMsg .source ),
3168
3169
)
3169
3170
_ , _ = d .recentRejects .Put (key , & cachedReject {})
3170
3171
@@ -3173,32 +3174,33 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3173
3174
}
3174
3175
3175
3176
nMsg .err <- err
3177
+
3176
3178
return nil , false
3177
3179
}
3178
3180
3179
3181
// If this is a local ChannelUpdate1 without an AuthProof, it means it
3180
3182
// is an update to a channel that is not (yet) supposed to be announced
3181
- // to the greater network. However, our channel counter party will need
3183
+ // to the greater network. However, our channel counterparty will need
3182
3184
// to be given the update, so we'll try sending the update directly to
3183
3185
// the remote peer.
3184
- if ! nMsg .isRemote && chanInfo .GetAuthProof () == nil { //nolint:nestif
3185
- if nMsg .optionalMsgFields != nil {
3186
+ if ! nMsg .isRemote && chanInfo .GetAuthProof () == nil {
3187
+ if nMsg .optionalMsgFields != nil &&
3188
+ nMsg .optionalMsgFields .remoteAlias != nil {
3189
+
3190
+ // The remoteAlias field was specified, meaning
3191
+ // that we should replace the SCID in the
3192
+ // update with the remote's alias. We'll also
3193
+ // need to re-sign the channel update. This is
3194
+ // required for option-scid-alias feature-bit
3195
+ // negotiated channels.
3186
3196
remoteAlias := nMsg .optionalMsgFields .remoteAlias
3187
- if remoteAlias != nil {
3188
- // The remoteAlias field was specified, meaning
3189
- // that we should replace the SCID in the
3190
- // update with the remote's alias. We'll also
3191
- // need to re-sign the channel update. This is
3192
- // required for option-scid-alias feature-bit
3193
- // negotiated channels.
3194
- upd .ShortChannelID = * remoteAlias
3195
-
3196
- err := d .cfg .SignAliasUpdate (upd )
3197
- if err != nil {
3198
- log .Error (err )
3199
- nMsg .err <- err
3200
- return nil , false
3201
- }
3197
+ upd .SetSCID (* remoteAlias )
3198
+
3199
+ err := d .cfg .SignAliasUpdate (upd )
3200
+ if err != nil {
3201
+ log .Error (err )
3202
+ nMsg .err <- err
3203
+ return nil , false
3202
3204
}
3203
3205
}
3204
3206
@@ -3215,7 +3217,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3215
3217
if err != nil {
3216
3218
err := fmt .Errorf ("unable to reliably send %v for " +
3217
3219
"channel=%v to peer=%x: %v" , upd .MsgType (),
3218
- upd . ShortChannelID , remotePubKey , err )
3220
+ scid , remotePubKey , err )
3219
3221
nMsg .err <- err
3220
3222
return nil , false
3221
3223
}
@@ -3228,7 +3230,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3228
3230
// contains an alias because the network would reject this.
3229
3231
var announcements []networkMsg
3230
3232
if chanInfo .GetAuthProof () != nil &&
3231
- ! d .cfg .IsAlias (upd . ShortChannelID ) {
3233
+ ! d .cfg .IsAlias (scid ) {
3232
3234
3233
3235
announcements = append (announcements , networkMsg {
3234
3236
peer : nMsg .peer ,
@@ -3240,9 +3242,9 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
3240
3242
3241
3243
nMsg .err <- nil
3242
3244
3243
- log .Debugf ("Processed ChannelUpdate1 : peer=%v, short_chan_id=%v, " +
3244
- "timestamp=%v" , nMsg .peer , upd . ShortChannelID . ToUint64 (),
3245
- timestamp )
3245
+ log .Debugf ("Processed %s : peer=%v, short_chan_id=%v, " , upd . MsgType (),
3246
+ nMsg .peer , scid . ToUint64 ())
3247
+
3246
3248
return announcements , true
3247
3249
}
3248
3250
@@ -3669,6 +3671,39 @@ func (d *AuthenticatedGossiper) handleAnnSig(nMsg *networkMsg,
3669
3671
return announcements , true
3670
3672
}
3671
3673
3674
+ func (d * AuthenticatedGossiper ) updateWithinRebroadcastInterval (
3675
+ upd lnwire.ChannelUpdate , policy models.ChannelEdgePolicy ) (bool ,
3676
+ error ) {
3677
+
3678
+ switch update := upd .(type ) {
3679
+ case * lnwire.ChannelUpdate1 :
3680
+ pol , ok := policy .(* models.ChannelEdgePolicy1 )
3681
+ if ! ok {
3682
+ return false , fmt .Errorf ("expected chan edge policy 1" )
3683
+ }
3684
+
3685
+ timestamp := time .Unix (int64 (update .Timestamp ), 0 )
3686
+ timeSinceLastUpdate := timestamp .Sub (pol .LastUpdate )
3687
+
3688
+ return timeSinceLastUpdate >= d .cfg .RebroadcastInterval , nil
3689
+
3690
+ case * lnwire.ChannelUpdate2 :
3691
+ pol , ok := policy .(* models.ChannelEdgePolicy2 )
3692
+ if ! ok {
3693
+ return false , fmt .Errorf ("expected chan edge policy 2" )
3694
+ }
3695
+
3696
+ blocksSinceLastUpdate := update .BlockHeight .Val -
3697
+ pol .BlockHeight .Val
3698
+
3699
+ return blocksSinceLastUpdate >=
3700
+ uint32 (d .cfg .RebroadcastInterval .Hours ()* 6 ), nil
3701
+
3702
+ default :
3703
+ return false , fmt .Errorf ("unhandled impl of Chan Update" )
3704
+ }
3705
+ }
3706
+
3672
3707
func buildChanProof (ann lnwire.ChannelAnnouncement ) (
3673
3708
models.ChannelAuthProof , error ) {
3674
3709
0 commit comments