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
@@ -3422,20 +3422,12 @@ func (d *AuthenticatedGossiper) handleChanUpdate(ctx context.Context,
|
|||||||
// different alias. This might mean that SigBytes is incorrect as it
|
// different alias. This might mean that SigBytes is incorrect as it
|
||||||
// signs a different SCID than the database SCID, but since there will
|
// signs a different SCID than the database SCID, but since there will
|
||||||
// only be a difference if AuthProof == nil, this is fine.
|
// only be a difference if AuthProof == nil, this is fine.
|
||||||
update := &models.ChannelEdgePolicy{
|
update, err := models.ChanEdgePolicyFromWire(
|
||||||
Version: upd.GossipVersion(),
|
chanInfo.ChannelID, upd,
|
||||||
SigBytes: upd.Signature.ToSignatureBytes(),
|
)
|
||||||
ChannelID: chanInfo.ChannelID,
|
if err != nil {
|
||||||
LastUpdate: timestamp,
|
nMsg.err <- err
|
||||||
MessageFlags: upd.MessageFlags,
|
return nil, false
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := d.cfg.Graph.UpdateEdge(ctx, update, ops...); err != nil {
|
if err := d.cfg.Graph.UpdateEdge(ctx, update, ops...); err != nil {
|
||||||
|
|||||||
+6
-14
@@ -952,20 +952,12 @@ func (b *Builder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
update := &models.ChannelEdgePolicy{
|
update, err := models.ChanEdgePolicyFromWire(
|
||||||
Version: msg.GossipVersion(),
|
msg.ShortChannelID.ToUint64(), msg,
|
||||||
SigBytes: msg.Signature.ToSignatureBytes(),
|
)
|
||||||
ChannelID: msg.ShortChannelID.ToUint64(),
|
if err != nil {
|
||||||
LastUpdate: time.Unix(int64(msg.Timestamp), 0),
|
log.Errorf("Unable to parse channel update: %v", err)
|
||||||
MessageFlags: msg.MessageFlags,
|
return false
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.UpdateEdge(ctx, update)
|
err = b.UpdateEdge(ctx, update)
|
||||||
|
|||||||
@@ -99,6 +99,56 @@ type ChannelEdgePolicy struct {
|
|||||||
ExtraSignedFields map[uint64][]byte
|
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.
|
// IsNode1 returns true if this policy was announced by the channel's node_1.
|
||||||
func (c *ChannelEdgePolicy) IsNode1() bool {
|
func (c *ChannelEdgePolicy) IsNode1() bool {
|
||||||
if c.Version == lnwire.GossipVersion1 {
|
if c.Version == lnwire.GossipVersion1 {
|
||||||
|
|||||||
+7
-14
@@ -2968,20 +2968,13 @@ func (m *mockGraphBuilder) ApplyChannelUpdate(msg *lnwire.ChannelUpdate1) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
err := m.updateEdge(&models.ChannelEdgePolicy{
|
update, err := models.ChanEdgePolicyFromWire(
|
||||||
Version: msg.GossipVersion(),
|
msg.ShortChannelID.ToUint64(), msg,
|
||||||
SigBytes: msg.Signature.ToSignatureBytes(),
|
)
|
||||||
ChannelID: msg.ShortChannelID.ToUint64(),
|
if err != nil {
|
||||||
LastUpdate: time.Unix(int64(msg.Timestamp), 0),
|
return false
|
||||||
MessageFlags: msg.MessageFlags,
|
}
|
||||||
ChannelFlags: msg.ChannelFlags,
|
err = m.updateEdge(update)
|
||||||
TimeLockDelta: msg.TimeLockDelta,
|
|
||||||
MinHTLC: msg.HtlcMinimumMsat,
|
|
||||||
MaxHTLC: msg.HtlcMaximumMsat,
|
|
||||||
FeeBaseMSat: lnwire.MilliSatoshi(msg.BaseFee),
|
|
||||||
FeeProportionalMillionths: lnwire.MilliSatoshi(msg.FeeRate),
|
|
||||||
ExtraOpaqueData: msg.ExtraOpaqueData,
|
|
||||||
})
|
|
||||||
|
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user