multi: add ChanEdgePolicyFromWire constructor for ChannelEdgePolicy
Add ChanEdgePolicyFromWire to construct ChannelEdgePolicy from channel update messages, centralizing v1/v2 field mapping. Update call sites to use the helper: - discovery/gossiper: handleChanUpdate - graph/builder: ApplyChannelUpdate - routing/router_test: ApplyChannelUpdate test helper This consolidates update-to-policy conversion logic across versions.
This commit is contained in:
4 files changed
+69
-42
No files matched your search
+6
-14
@@ -952,20 +952,12 @@ func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
update := &models.ChannelEdgePolicy{
|
||||
Version: msg.GossipVersion(),
|
||||
SigBytes: msg.Signature.ToSignatureBytes(),
|
||||
ChannelID: msg.ShortChannelID.ToUint64(),
|
||||
LastUpdate: time.Unix(int64(msg.Timestamp), 0),
|
||||
MessageFlags: msg.MessageFlags,
|
||||
ChannelFlags: msg.ChannelFlags,
|
||||
TimeLockDelta: msg.TimeLockDelta,
|
||||
MinHTLC: msg.HtlcMinimumMsat,
|
||||
MaxHTLC: msg.HtlcMaximumMsat,
|
||||
FeeBaseMSat: lnwire.MilliSatoshi(msg.BaseFee),
|
||||
FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
|
||||
InboundFee: msg.InboundFee.ValOpt(),
|
||||
ExtraOpaqueData: msg.ExtraOpaqueData,
|
||||
update, err := models.ChanEdgePolicyFromWire(
|
||||
msg.ShortChannelID.ToUint64(), msg,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to parse channel update: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
err = b.UpdateEdge(ctx, update)
|
||||
|
||||
@@ -99,6 +99,56 @@ type ChannelEdgePolicy struct {
|
||||
ExtraSignedFields map[uint64][]byte
|
||||
}
|
||||
|
||||
// ChanEdgePolicyFromWire constructs a ChannelEdgePolicy from a channel update
|
||||
// message.
|
||||
func ChanEdgePolicyFromWire(scid uint64,
|
||||
update lnwire.ChannelUpdate) (*ChannelEdgePolicy, error) {
|
||||
|
||||
switch upd := update.(type) {
|
||||
case *lnwire.ChannelUpdate1:
|
||||
//nolint:ll
|
||||
return &ChannelEdgePolicy{
|
||||
Version: lnwire.GossipVersion1,
|
||||
SigBytes: upd.Signature.ToSignatureBytes(),
|
||||
ChannelID: scid,
|
||||
LastUpdate: time.Unix(int64(upd.Timestamp), 0),
|
||||
MessageFlags: upd.MessageFlags,
|
||||
ChannelFlags: upd.ChannelFlags,
|
||||
TimeLockDelta: upd.TimeLockDelta,
|
||||
MinHTLC: upd.HtlcMinimumMsat,
|
||||
MaxHTLC: upd.HtlcMaximumMsat,
|
||||
FeeBaseMSat: lnwire.MilliSatoshi(upd.BaseFee),
|
||||
FeeProportionalMillionths: lnwire.MilliSatoshi(upd.FeeRate),
|
||||
InboundFee: upd.InboundFee.ValOpt(),
|
||||
ExtraOpaqueData: upd.ExtraOpaqueData,
|
||||
}, nil
|
||||
|
||||
case *lnwire.ChannelUpdate2:
|
||||
return &ChannelEdgePolicy{
|
||||
Version: lnwire.GossipVersion2,
|
||||
SigBytes: upd.Signature.Val.ToSignatureBytes(),
|
||||
ChannelID: scid,
|
||||
LastBlockHeight: upd.BlockHeight.Val,
|
||||
SecondPeer: upd.SecondPeer.IsSome(),
|
||||
DisableFlags: upd.DisabledFlags.Val,
|
||||
TimeLockDelta: upd.CLTVExpiryDelta.Val,
|
||||
MinHTLC: upd.HTLCMinimumMsat.Val,
|
||||
MaxHTLC: upd.HTLCMaximumMsat.Val,
|
||||
FeeBaseMSat: lnwire.MilliSatoshi(
|
||||
upd.FeeBaseMsat.Val,
|
||||
),
|
||||
FeeProportionalMillionths: lnwire.MilliSatoshi(
|
||||
upd.FeeProportionalMillionths.Val,
|
||||
),
|
||||
InboundFee: upd.InboundFee.ValOpt(),
|
||||
ExtraSignedFields: upd.ExtraSignedFields,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unknown channel update version: %v",
|
||||
update.MsgType())
|
||||
}
|
||||
|
||||
// IsNode1 returns true if this policy was announced by the channel's node_1.
|
||||
func (c *ChannelEdgePolicy) IsNode1() bool {
|
||||
if c.Version == lnwire.GossipVersion1 {
|
||||
|
||||
Reference in new issue
Block a user