graph/db: update policy builders to support v2 fields
Update buildChanPolicy and related functions in both KV and SQL stores to properly construct ChannelEdgePolicy with version-specific fields: KVStore changes: - Reject non-v1 policies in updateEdgePolicy and serializeChanEdgePolicy since KV store only supports v1 gossip protocol. - Set Version to GossipVersion1 when deserializing policies from KV. SQLStore changes: - Add isNode1 parameter to buildChanPolicy functions to properly set SecondPeer field (v2 uses SecondPeer instead of ChannelFlags direction). - Extract Version from database and populate version-specific fields: - For v1: MessageFlags, ChannelFlags, LastUpdate, ExtraOpaqueData - For v2: DisableFlags, LastBlockHeight, ExtraSignedFields - Thread isNode1 through buildChanPolicyWithBatchData and buildCachedChanPolicies call sites. This enables the SQL store to read and reconstruct both v1 and v2 channel policies from the database with proper field mapping.
This commit is contained in:
3 files changed
+204
-122
No files matched your search
+93
-62
@@ -1048,6 +1048,7 @@ func createChannelEdge(node1, node2 *models.Node) (*models.ChannelEdgeInfo,
|
|||||||
SigBytes: testSig.Serialize(),
|
SigBytes: testSig.Serialize(),
|
||||||
ChannelID: chanID,
|
ChannelID: chanID,
|
||||||
LastUpdate: nextUpdateTime(),
|
LastUpdate: nextUpdateTime(),
|
||||||
|
SecondPeer: false,
|
||||||
MessageFlags: 1,
|
MessageFlags: 1,
|
||||||
ChannelFlags: 0,
|
ChannelFlags: 0,
|
||||||
TimeLockDelta: 99,
|
TimeLockDelta: 99,
|
||||||
@@ -1062,6 +1063,7 @@ func createChannelEdge(node1, node2 *models.Node) (*models.ChannelEdgeInfo,
|
|||||||
Version: lnwire.GossipVersion1,
|
Version: lnwire.GossipVersion1,
|
||||||
SigBytes: testSig.Serialize(),
|
SigBytes: testSig.Serialize(),
|
||||||
ChannelID: chanID,
|
ChannelID: chanID,
|
||||||
|
SecondPeer: true,
|
||||||
LastUpdate: nextUpdateTime(),
|
LastUpdate: nextUpdateTime(),
|
||||||
MessageFlags: 1,
|
MessageFlags: 1,
|
||||||
ChannelFlags: 1,
|
ChannelFlags: 1,
|
||||||
@@ -1439,7 +1441,7 @@ func assertEdgeWithPolicyInCache(t *testing.T, g *ChannelGraph,
|
|||||||
func randEdgePolicy(chanID uint64) *models.ChannelEdgePolicy {
|
func randEdgePolicy(chanID uint64) *models.ChannelEdgePolicy {
|
||||||
update := prand.Int63()
|
update := prand.Int63()
|
||||||
|
|
||||||
return newEdgePolicy(chanID, update)
|
return newEdgePolicy(lnwire.GossipVersion1, chanID, update, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyEdgePolicy(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
|
func copyEdgePolicy(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
|
||||||
@@ -1448,8 +1450,11 @@ func copyEdgePolicy(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
|
|||||||
SigBytes: p.SigBytes,
|
SigBytes: p.SigBytes,
|
||||||
ChannelID: p.ChannelID,
|
ChannelID: p.ChannelID,
|
||||||
LastUpdate: p.LastUpdate,
|
LastUpdate: p.LastUpdate,
|
||||||
|
LastBlockHeight: p.LastBlockHeight,
|
||||||
|
SecondPeer: p.SecondPeer,
|
||||||
MessageFlags: p.MessageFlags,
|
MessageFlags: p.MessageFlags,
|
||||||
ChannelFlags: p.ChannelFlags,
|
ChannelFlags: p.ChannelFlags,
|
||||||
|
DisableFlags: p.DisableFlags,
|
||||||
TimeLockDelta: p.TimeLockDelta,
|
TimeLockDelta: p.TimeLockDelta,
|
||||||
MinHTLC: p.MinHTLC,
|
MinHTLC: p.MinHTLC,
|
||||||
MaxHTLC: p.MaxHTLC,
|
MaxHTLC: p.MaxHTLC,
|
||||||
@@ -1457,22 +1462,40 @@ func copyEdgePolicy(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
|
|||||||
FeeProportionalMillionths: p.FeeProportionalMillionths,
|
FeeProportionalMillionths: p.FeeProportionalMillionths,
|
||||||
ToNode: p.ToNode,
|
ToNode: p.ToNode,
|
||||||
ExtraOpaqueData: p.ExtraOpaqueData,
|
ExtraOpaqueData: p.ExtraOpaqueData,
|
||||||
|
ExtraSignedFields: p.ExtraSignedFields,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEdgePolicy(chanID uint64, updateTime int64) *models.ChannelEdgePolicy {
|
func newEdgePolicy(v lnwire.GossipVersion, chanID uint64,
|
||||||
return &models.ChannelEdgePolicy{
|
updateTime int64, isNode1 bool) *models.ChannelEdgePolicy {
|
||||||
Version: lnwire.GossipVersion1,
|
|
||||||
|
policy := &models.ChannelEdgePolicy{
|
||||||
|
Version: v,
|
||||||
|
SecondPeer: !isNode1,
|
||||||
ChannelID: chanID,
|
ChannelID: chanID,
|
||||||
LastUpdate: time.Unix(updateTime, 0),
|
|
||||||
MessageFlags: 1,
|
|
||||||
ChannelFlags: 0,
|
|
||||||
TimeLockDelta: uint16(prand.Int63()),
|
TimeLockDelta: uint16(prand.Int63()),
|
||||||
MinHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
MinHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
||||||
MaxHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
MaxHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
||||||
FeeBaseMSat: lnwire.MilliSatoshi(prand.Int63()),
|
FeeBaseMSat: lnwire.MilliSatoshi(prand.Int63()),
|
||||||
FeeProportionalMillionths: lnwire.MilliSatoshi(prand.Int63()),
|
FeeProportionalMillionths: lnwire.MilliSatoshi(prand.Int63()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v == lnwire.GossipVersion1 {
|
||||||
|
policy.LastUpdate = time.Unix(updateTime, 0)
|
||||||
|
policy.MessageFlags = 1
|
||||||
|
if !isNode1 {
|
||||||
|
policy.ChannelFlags = lnwire.ChanUpdateDirection
|
||||||
|
}
|
||||||
|
policy.ExtraOpaqueData = []byte{1, 0}
|
||||||
|
} else {
|
||||||
|
policy.LastBlockHeight = nextBlockHeight()
|
||||||
|
policy.DisableFlags = 0
|
||||||
|
policy.ExtraSignedFields = map[uint64][]byte{
|
||||||
|
100: {0x1, 0x2, 0x3},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return policy
|
||||||
}
|
}
|
||||||
|
|
||||||
// testAddEdgeProof tests the ability to add an edge proof to an existing edge.
|
// testAddEdgeProof tests the ability to add an edge proof to an existing edge.
|
||||||
@@ -2358,7 +2381,8 @@ func TestChanUpdatesInHorizon(t *testing.T) {
|
|||||||
endTime = endTime.Add(time.Second * 10)
|
endTime = endTime.Add(time.Second * 10)
|
||||||
|
|
||||||
edge1 := newEdgePolicy(
|
edge1 := newEdgePolicy(
|
||||||
chanID.ToUint64(), edge1UpdateTime.Unix(),
|
lnwire.GossipVersion1, chanID.ToUint64(),
|
||||||
|
edge1UpdateTime.Unix(), true,
|
||||||
)
|
)
|
||||||
edge1.ChannelFlags = 0
|
edge1.ChannelFlags = 0
|
||||||
edge1.ToNode = node2.PubKeyBytes
|
edge1.ToNode = node2.PubKeyBytes
|
||||||
@@ -2368,7 +2392,8 @@ func TestChanUpdatesInHorizon(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
edge2 := newEdgePolicy(
|
edge2 := newEdgePolicy(
|
||||||
chanID.ToUint64(), edge2UpdateTime.Unix(),
|
lnwire.GossipVersion1, chanID.ToUint64(),
|
||||||
|
edge2UpdateTime.Unix(), false,
|
||||||
)
|
)
|
||||||
edge2.ChannelFlags = 1
|
edge2.ChannelFlags = 1
|
||||||
edge2.ToNode = node1.PubKeyBytes
|
edge2.ToNode = node1.PubKeyBytes
|
||||||
@@ -2821,7 +2846,9 @@ func TestChanUpdatesInHorizonBoundaryConditions(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
edge1 := newEdgePolicy(
|
edge1 := newEdgePolicy(
|
||||||
|
lnwire.GossipVersion1,
|
||||||
chanID.ToUint64(), updateTime.Unix(),
|
chanID.ToUint64(), updateTime.Unix(),
|
||||||
|
true,
|
||||||
)
|
)
|
||||||
edge1.ChannelFlags = 0
|
edge1.ChannelFlags = 0
|
||||||
edge1.ToNode = node2.PubKeyBytes
|
edge1.ToNode = node2.PubKeyBytes
|
||||||
@@ -2831,7 +2858,9 @@ func TestChanUpdatesInHorizonBoundaryConditions(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
edge2 := newEdgePolicy(
|
edge2 := newEdgePolicy(
|
||||||
|
lnwire.GossipVersion1,
|
||||||
chanID.ToUint64(), updateTime.Unix(),
|
chanID.ToUint64(), updateTime.Unix(),
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
edge2.ChannelFlags = 1
|
edge2.ChannelFlags = 1
|
||||||
edge2.ToNode = node1.PubKeyBytes
|
edge2.ToNode = node1.PubKeyBytes
|
||||||
@@ -3668,16 +3697,20 @@ func TestFetchChanInfos(t *testing.T) {
|
|||||||
updateTime := endTime
|
updateTime := endTime
|
||||||
endTime = updateTime.Add(time.Second * 10)
|
endTime = updateTime.Add(time.Second * 10)
|
||||||
|
|
||||||
edge1 := newEdgePolicy(chanID.ToUint64(), updateTime.Unix())
|
edge1 := newEdgePolicy(
|
||||||
edge1.ChannelFlags = 0
|
lnwire.GossipVersion1, chanID.ToUint64(),
|
||||||
|
updateTime.Unix(), true,
|
||||||
|
)
|
||||||
edge1.ToNode = node2.PubKeyBytes
|
edge1.ToNode = node2.PubKeyBytes
|
||||||
edge1.SigBytes = testSig.Serialize()
|
edge1.SigBytes = testSig.Serialize()
|
||||||
if err := graph.UpdateEdgePolicy(ctx, edge1); err != nil {
|
if err := graph.UpdateEdgePolicy(ctx, edge1); err != nil {
|
||||||
t.Fatalf("unable to update edge: %v", err)
|
t.Fatalf("unable to update edge: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
edge2 := newEdgePolicy(chanID.ToUint64(), updateTime.Unix())
|
edge2 := newEdgePolicy(
|
||||||
edge2.ChannelFlags = 1
|
lnwire.GossipVersion1, chanID.ToUint64(),
|
||||||
|
updateTime.Unix(), false,
|
||||||
|
)
|
||||||
edge2.ToNode = node1.PubKeyBytes
|
edge2.ToNode = node1.PubKeyBytes
|
||||||
edge2.SigBytes = testSig.Serialize()
|
edge2.SigBytes = testSig.Serialize()
|
||||||
if err := graph.UpdateEdgePolicy(ctx, edge2); err != nil {
|
if err := graph.UpdateEdgePolicy(ctx, edge2); err != nil {
|
||||||
@@ -3800,8 +3833,10 @@ func TestIncompleteChannelPolicies(t *testing.T) {
|
|||||||
// unknown.
|
// unknown.
|
||||||
updateTime := time.Unix(1234, 0)
|
updateTime := time.Unix(1234, 0)
|
||||||
|
|
||||||
edgePolicy := newEdgePolicy(chanID.ToUint64(), updateTime.Unix())
|
edgePolicy := newEdgePolicy(
|
||||||
edgePolicy.ChannelFlags = 0
|
lnwire.GossipVersion1, chanID.ToUint64(), updateTime.Unix(),
|
||||||
|
true,
|
||||||
|
)
|
||||||
edgePolicy.ToNode = node2.PubKeyBytes
|
edgePolicy.ToNode = node2.PubKeyBytes
|
||||||
edgePolicy.SigBytes = testSig.Serialize()
|
edgePolicy.SigBytes = testSig.Serialize()
|
||||||
if err := graph.UpdateEdgePolicy(ctx, edgePolicy); err != nil {
|
if err := graph.UpdateEdgePolicy(ctx, edgePolicy); err != nil {
|
||||||
@@ -3813,8 +3848,10 @@ func TestIncompleteChannelPolicies(t *testing.T) {
|
|||||||
|
|
||||||
// Create second policy and assert that both policies are reported
|
// Create second policy and assert that both policies are reported
|
||||||
// as present.
|
// as present.
|
||||||
edgePolicy = newEdgePolicy(chanID.ToUint64(), updateTime.Unix())
|
edgePolicy = newEdgePolicy(
|
||||||
edgePolicy.ChannelFlags = 1
|
lnwire.GossipVersion1, chanID.ToUint64(), updateTime.Unix(),
|
||||||
|
false,
|
||||||
|
)
|
||||||
edgePolicy.ToNode = node1.PubKeyBytes
|
edgePolicy.ToNode = node1.PubKeyBytes
|
||||||
edgePolicy.SigBytes = testSig.Serialize()
|
edgePolicy.SigBytes = testSig.Serialize()
|
||||||
if err := graph.UpdateEdgePolicy(ctx, edgePolicy); err != nil {
|
if err := graph.UpdateEdgePolicy(ctx, edgePolicy); err != nil {
|
||||||
@@ -4661,53 +4698,47 @@ func compareNodes(t *testing.T, a, b *models.Node) {
|
|||||||
require.Equal(t, a, b)
|
require.Equal(t, a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// compareEdgePolicies is used to compare two ChannelEdgePolices using
|
// compareEdgePolicies compares two ChannelEdgePolicy values for semantic
|
||||||
// compareNodes, so as to exclude comparisons of the Nodes' Features struct.
|
// equality after normalizing version-specific/backend-specific differences.
|
||||||
func compareEdgePolicies(a, b *models.ChannelEdgePolicy) error {
|
func compareEdgePolicies(a, b *models.ChannelEdgePolicy) error {
|
||||||
if a.ChannelID != b.ChannelID {
|
//nolint:ll
|
||||||
return fmt.Errorf("ChannelID doesn't match: expected %v, "+
|
normalize := func(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
|
||||||
"got %v", a.ChannelID, b.ChannelID)
|
if p == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
policy := copyEdgePolicy(p)
|
||||||
|
if len(policy.ExtraOpaqueData) == 0 {
|
||||||
|
policy.ExtraOpaqueData = nil
|
||||||
|
}
|
||||||
|
if len(policy.ExtraSignedFields) == 0 {
|
||||||
|
policy.ExtraSignedFields = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch policy.Version {
|
||||||
|
case lnwire.GossipVersion1:
|
||||||
|
// SecondPeer is v2-specific; derive canonical direction
|
||||||
|
// for v1.
|
||||||
|
policy.SecondPeer = !policy.IsNode1()
|
||||||
|
policy.LastBlockHeight = 0
|
||||||
|
policy.DisableFlags = 0
|
||||||
|
policy.ExtraSignedFields = nil
|
||||||
|
|
||||||
|
case lnwire.GossipVersion2:
|
||||||
|
policy.LastUpdate = time.Time{}
|
||||||
|
policy.MessageFlags = 0
|
||||||
|
policy.ChannelFlags = 0
|
||||||
|
policy.ExtraOpaqueData = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return policy
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(a.LastUpdate, b.LastUpdate) {
|
|
||||||
return fmt.Errorf("edge LastUpdate doesn't match: "+
|
normalizedA := normalize(a)
|
||||||
"expected %#v, got %#v", a.LastUpdate, b.LastUpdate)
|
normalizedB := normalize(b)
|
||||||
}
|
if !reflect.DeepEqual(normalizedA, normalizedB) {
|
||||||
if a.MessageFlags != b.MessageFlags {
|
return fmt.Errorf("expected %v, got %v", normalizedA,
|
||||||
return fmt.Errorf("MessageFlags doesn't match: expected %v, "+
|
normalizedB)
|
||||||
"got %v", a.MessageFlags, b.MessageFlags)
|
|
||||||
}
|
|
||||||
if a.ChannelFlags != b.ChannelFlags {
|
|
||||||
return fmt.Errorf("ChannelFlags doesn't match: expected %v, "+
|
|
||||||
"got %v", a.ChannelFlags, b.ChannelFlags)
|
|
||||||
}
|
|
||||||
if a.TimeLockDelta != b.TimeLockDelta {
|
|
||||||
return fmt.Errorf("TimeLockDelta doesn't match: expected %v, "+
|
|
||||||
"got %v", a.TimeLockDelta, b.TimeLockDelta)
|
|
||||||
}
|
|
||||||
if a.MinHTLC != b.MinHTLC {
|
|
||||||
return fmt.Errorf("MinHTLC doesn't match: expected %v, "+
|
|
||||||
"got %v", a.MinHTLC, b.MinHTLC)
|
|
||||||
}
|
|
||||||
if a.MaxHTLC != b.MaxHTLC {
|
|
||||||
return fmt.Errorf("MaxHTLC doesn't match: expected %v, "+
|
|
||||||
"got %v", a.MaxHTLC, b.MaxHTLC)
|
|
||||||
}
|
|
||||||
if a.FeeBaseMSat != b.FeeBaseMSat {
|
|
||||||
return fmt.Errorf("FeeBaseMSat doesn't match: expected %v, "+
|
|
||||||
"got %v", a.FeeBaseMSat, b.FeeBaseMSat)
|
|
||||||
}
|
|
||||||
if a.FeeProportionalMillionths != b.FeeProportionalMillionths {
|
|
||||||
return fmt.Errorf("FeeProportionalMillionths doesn't match: "+
|
|
||||||
"expected %v, got %v", a.FeeProportionalMillionths,
|
|
||||||
b.FeeProportionalMillionths)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(a.ExtraOpaqueData, b.ExtraOpaqueData) {
|
|
||||||
return fmt.Errorf("extra data doesn't match: %v vs %v",
|
|
||||||
a.ExtraOpaqueData, b.ExtraOpaqueData)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(a.ToNode[:], b.ToNode[:]) {
|
|
||||||
return fmt.Errorf("ToNode doesn't match: expected %x, got %x",
|
|
||||||
a.ToNode, b.ToNode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+10
-1
@@ -3296,6 +3296,9 @@ func updateEdgePolicy(tx kvdb.RwTx, edge *models.ChannelEdgePolicy) (
|
|||||||
route.Vertex, route.Vertex, bool, error) {
|
route.Vertex, route.Vertex, bool, error) {
|
||||||
|
|
||||||
var noVertex route.Vertex
|
var noVertex route.Vertex
|
||||||
|
if edge.Version != lnwire.GossipVersion1 {
|
||||||
|
return noVertex, noVertex, false, ErrVersionNotSupportedForKVDB
|
||||||
|
}
|
||||||
|
|
||||||
edges := tx.ReadWriteBucket(edgeBucket)
|
edges := tx.ReadWriteBucket(edgeBucket)
|
||||||
if edges == nil {
|
if edges == nil {
|
||||||
@@ -5226,6 +5229,10 @@ func fetchChanEdgePolicies(edgeIndex kvdb.RBucket, edges kvdb.RBucket,
|
|||||||
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
|
func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
|
||||||
to []byte) error {
|
to []byte) error {
|
||||||
|
|
||||||
|
if edge.Version != lnwire.GossipVersion1 {
|
||||||
|
return ErrVersionNotSupportedForKVDB
|
||||||
|
}
|
||||||
|
|
||||||
err := wire.WriteVarBytes(w, 0, edge.SigBytes)
|
err := wire.WriteVarBytes(w, 0, edge.SigBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -5314,7 +5321,9 @@ func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) {
|
|||||||
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
|
func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy,
|
||||||
error) {
|
error) {
|
||||||
|
|
||||||
edge := &models.ChannelEdgePolicy{}
|
edge := &models.ChannelEdgePolicy{
|
||||||
|
Version: lnwire.GossipVersion1,
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
|
edge.SigBytes, err = wire.ReadVarBytes(r, 0, 80, "sig")
|
||||||
|
|||||||
+101
-59
@@ -3455,10 +3455,16 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
node1Pub, node2Pub route.Vertex
|
node1Pub, node2Pub route.Vertex
|
||||||
isNode1 bool
|
|
||||||
chanIDB = channelIDToBytes(edge.ChannelID)
|
chanIDB = channelIDToBytes(edge.ChannelID)
|
||||||
|
version = edge.Version
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if !isKnownGossipVersion(version) {
|
||||||
|
return node1Pub, node2Pub, false, fmt.Errorf(
|
||||||
|
"unsupported gossip version: %d", version,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Check that this edge policy refers to a channel that we already
|
// Check that this edge policy refers to a channel that we already
|
||||||
// know of. We do this explicitly so that we can return the appropriate
|
// know of. We do this explicitly so that we can return the appropriate
|
||||||
// ErrEdgeNotFound error if the channel doesn't exist, rather than
|
// ErrEdgeNotFound error if the channel doesn't exist, rather than
|
||||||
@@ -3466,7 +3472,7 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
|
|||||||
dbChan, err := tx.GetChannelAndNodesBySCID(
|
dbChan, err := tx.GetChannelAndNodesBySCID(
|
||||||
ctx, sqlc.GetChannelAndNodesBySCIDParams{
|
ctx, sqlc.GetChannelAndNodesBySCIDParams{
|
||||||
Scid: chanIDB,
|
Scid: chanIDB,
|
||||||
Version: int16(lnwire.GossipVersion1),
|
Version: int16(version),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
@@ -3480,7 +3486,7 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
|
|||||||
copy(node2Pub[:], dbChan.Node2PubKey)
|
copy(node2Pub[:], dbChan.Node2PubKey)
|
||||||
|
|
||||||
// Figure out which node this edge is from.
|
// Figure out which node this edge is from.
|
||||||
isNode1 = edge.ChannelFlags&lnwire.ChanUpdateDirection == 0
|
isNode1 := edge.IsNode1()
|
||||||
nodeID := dbChan.NodeID1
|
nodeID := dbChan.NodeID1
|
||||||
if !isNode1 {
|
if !isNode1 {
|
||||||
nodeID = dbChan.NodeID2
|
nodeID = dbChan.NodeID2
|
||||||
@@ -3495,31 +3501,40 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
|
|||||||
inboundBase = sqldb.SQLInt64(fee.BaseFee)
|
inboundBase = sqldb.SQLInt64(fee.BaseFee)
|
||||||
})
|
})
|
||||||
|
|
||||||
id, err := tx.UpsertEdgePolicy(ctx, sqlc.UpsertEdgePolicyParams{
|
params := sqlc.UpsertEdgePolicyParams{
|
||||||
Version: int16(lnwire.GossipVersion1),
|
Version: int16(version),
|
||||||
ChannelID: dbChan.ID,
|
ChannelID: dbChan.ID,
|
||||||
NodeID: nodeID,
|
NodeID: nodeID,
|
||||||
Timelock: int32(edge.TimeLockDelta),
|
Timelock: int32(edge.TimeLockDelta),
|
||||||
FeePpm: int64(edge.FeeProportionalMillionths),
|
FeePpm: int64(edge.FeeProportionalMillionths),
|
||||||
BaseFeeMsat: int64(edge.FeeBaseMSat),
|
BaseFeeMsat: int64(edge.FeeBaseMSat),
|
||||||
MinHtlcMsat: int64(edge.MinHTLC),
|
MinHtlcMsat: int64(edge.MinHTLC),
|
||||||
LastUpdate: sqldb.SQLInt64(edge.LastUpdate.Unix()),
|
|
||||||
Disabled: sql.NullBool{
|
|
||||||
Valid: true,
|
|
||||||
Bool: edge.IsDisabled(),
|
|
||||||
},
|
|
||||||
MaxHtlcMsat: sql.NullInt64{
|
|
||||||
Valid: edge.MessageFlags.HasMaxHtlc(),
|
|
||||||
Int64: int64(edge.MaxHTLC),
|
|
||||||
},
|
|
||||||
MessageFlags: sqldb.SQLInt16(edge.MessageFlags),
|
MessageFlags: sqldb.SQLInt16(edge.MessageFlags),
|
||||||
ChannelFlags: sqldb.SQLInt16(edge.ChannelFlags),
|
ChannelFlags: sqldb.SQLInt16(edge.ChannelFlags),
|
||||||
InboundBaseFeeMsat: inboundBase,
|
InboundBaseFeeMsat: inboundBase,
|
||||||
InboundFeeRateMilliMsat: inboundRate,
|
InboundFeeRateMilliMsat: inboundRate,
|
||||||
Signature: edge.SigBytes,
|
Signature: edge.SigBytes,
|
||||||
BlockHeight: sql.NullInt64{},
|
}
|
||||||
DisableFlags: sql.NullInt16{},
|
|
||||||
})
|
if version == lnwire.GossipVersion1 {
|
||||||
|
params.LastUpdate = sqldb.SQLInt64(edge.LastUpdate.Unix())
|
||||||
|
params.Disabled = sql.NullBool{
|
||||||
|
Valid: true,
|
||||||
|
Bool: edge.IsDisabled(),
|
||||||
|
}
|
||||||
|
params.MaxHtlcMsat = sql.NullInt64{
|
||||||
|
Valid: edge.MessageFlags.HasMaxHtlc(),
|
||||||
|
Int64: int64(edge.MaxHTLC),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
params.BlockHeight = sqldb.SQLInt64(
|
||||||
|
int64(edge.LastBlockHeight),
|
||||||
|
)
|
||||||
|
params.DisableFlags = sqldb.SQLInt16(edge.DisableFlags)
|
||||||
|
params.MaxHtlcMsat = sqldb.SQLInt64(int64(edge.MaxHTLC))
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := tx.UpsertEdgePolicy(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node1Pub, node2Pub, isNode1,
|
return node1Pub, node2Pub, isNode1,
|
||||||
fmt.Errorf("unable to upsert edge policy: %w", err)
|
fmt.Errorf("unable to upsert edge policy: %w", err)
|
||||||
@@ -3527,10 +3542,13 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
|
|||||||
|
|
||||||
// Convert the flat extra opaque data into a map of TLV types to
|
// Convert the flat extra opaque data into a map of TLV types to
|
||||||
// values.
|
// values.
|
||||||
extra, err := marshalExtraOpaqueData(edge.ExtraOpaqueData)
|
extra := edge.ExtraSignedFields
|
||||||
if err != nil {
|
if version == lnwire.GossipVersion1 {
|
||||||
return node1Pub, node2Pub, false, fmt.Errorf("unable to "+
|
extra, err = marshalExtraOpaqueData(edge.ExtraOpaqueData)
|
||||||
"marshal extra opaque data: %w", err)
|
if err != nil {
|
||||||
|
return node1Pub, node2Pub, false, fmt.Errorf("unable to "+
|
||||||
|
"marshal extra opaque data: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the channel policy's extra signed fields.
|
// Update the channel policy's extra signed fields.
|
||||||
@@ -4713,14 +4731,14 @@ func getAndBuildChanPolicies(ctx context.Context, cfg *sqldb.QueryConfig,
|
|||||||
}
|
}
|
||||||
|
|
||||||
pol1, err := buildChanPolicyWithBatchData(
|
pol1, err := buildChanPolicyWithBatchData(
|
||||||
dbPol1, channelID, node2, batchData,
|
true, dbPol1, channelID, node2, batchData,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("unable to build policy1: %w", err)
|
return nil, nil, fmt.Errorf("unable to build policy1: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pol2, err := buildChanPolicyWithBatchData(
|
pol2, err := buildChanPolicyWithBatchData(
|
||||||
dbPol2, channelID, node1, batchData,
|
false, dbPol2, channelID, node1, batchData,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("unable to build policy2: %w", err)
|
return nil, nil, fmt.Errorf("unable to build policy2: %w", err)
|
||||||
@@ -4738,7 +4756,9 @@ func buildCachedChanPolicies(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
|
|||||||
|
|
||||||
var p1, p2 *models.CachedEdgePolicy
|
var p1, p2 *models.CachedEdgePolicy
|
||||||
if dbPol1 != nil {
|
if dbPol1 != nil {
|
||||||
policy1, err := buildChanPolicy(*dbPol1, channelID, nil, node2)
|
policy1, err := buildChanPolicy(
|
||||||
|
true, *dbPol1, channelID, nil, node2,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -4746,7 +4766,9 @@ func buildCachedChanPolicies(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
|
|||||||
p1 = models.NewCachedPolicy(policy1)
|
p1 = models.NewCachedPolicy(policy1)
|
||||||
}
|
}
|
||||||
if dbPol2 != nil {
|
if dbPol2 != nil {
|
||||||
policy2, err := buildChanPolicy(*dbPol2, channelID, nil, node1)
|
policy2, err := buildChanPolicy(
|
||||||
|
false, *dbPol2, channelID, nil, node1,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -4759,16 +4781,10 @@ func buildCachedChanPolicies(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
|
|||||||
|
|
||||||
// buildChanPolicy builds a models.ChannelEdgePolicy instance from the
|
// buildChanPolicy builds a models.ChannelEdgePolicy instance from the
|
||||||
// provided sqlc.GraphChannelPolicy and other required information.
|
// provided sqlc.GraphChannelPolicy and other required information.
|
||||||
func buildChanPolicy(dbPolicy sqlc.GraphChannelPolicy, channelID uint64,
|
func buildChanPolicy(isNode1 bool, dbPolicy sqlc.GraphChannelPolicy,
|
||||||
extras map[uint64][]byte,
|
channelID uint64, extras map[uint64][]byte,
|
||||||
toNode route.Vertex) (*models.ChannelEdgePolicy, error) {
|
toNode route.Vertex) (*models.ChannelEdgePolicy, error) {
|
||||||
|
|
||||||
recs, err := lnwire.CustomRecords(extras).Serialize()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to serialize extra signed "+
|
|
||||||
"fields: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var inboundFee fn.Option[lnwire.Fee]
|
var inboundFee fn.Option[lnwire.Fee]
|
||||||
if dbPolicy.InboundFeeRateMilliMsat.Valid ||
|
if dbPolicy.InboundFeeRateMilliMsat.Valid ||
|
||||||
dbPolicy.InboundBaseFeeMsat.Valid {
|
dbPolicy.InboundBaseFeeMsat.Valid {
|
||||||
@@ -4779,18 +4795,11 @@ func buildChanPolicy(dbPolicy sqlc.GraphChannelPolicy, channelID uint64,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return &models.ChannelEdgePolicy{
|
p := &models.ChannelEdgePolicy{
|
||||||
SigBytes: dbPolicy.Signature,
|
Version: lnwire.GossipVersion(dbPolicy.Version),
|
||||||
ChannelID: channelID,
|
SigBytes: dbPolicy.Signature,
|
||||||
LastUpdate: time.Unix(
|
ChannelID: channelID,
|
||||||
dbPolicy.LastUpdate.Int64, 0,
|
SecondPeer: !isNode1,
|
||||||
),
|
|
||||||
MessageFlags: sqldb.ExtractSqlInt16[lnwire.ChanUpdateMsgFlags](
|
|
||||||
dbPolicy.MessageFlags,
|
|
||||||
),
|
|
||||||
ChannelFlags: sqldb.ExtractSqlInt16[lnwire.ChanUpdateChanFlags](
|
|
||||||
dbPolicy.ChannelFlags,
|
|
||||||
),
|
|
||||||
TimeLockDelta: uint16(dbPolicy.Timelock),
|
TimeLockDelta: uint16(dbPolicy.Timelock),
|
||||||
MinHTLC: lnwire.MilliSatoshi(
|
MinHTLC: lnwire.MilliSatoshi(
|
||||||
dbPolicy.MinHtlcMsat,
|
dbPolicy.MinHtlcMsat,
|
||||||
@@ -4804,8 +4813,40 @@ func buildChanPolicy(dbPolicy sqlc.GraphChannelPolicy, channelID uint64,
|
|||||||
FeeProportionalMillionths: lnwire.MilliSatoshi(dbPolicy.FeePpm),
|
FeeProportionalMillionths: lnwire.MilliSatoshi(dbPolicy.FeePpm),
|
||||||
ToNode: toNode,
|
ToNode: toNode,
|
||||||
InboundFee: inboundFee,
|
InboundFee: inboundFee,
|
||||||
ExtraOpaqueData: recs,
|
}
|
||||||
}, nil
|
|
||||||
|
if p.Version == lnwire.GossipVersion1 {
|
||||||
|
recs, err := lnwire.CustomRecords(extras).Serialize()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to serialize extra "+
|
||||||
|
"signed fields: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.ExtraOpaqueData = recs
|
||||||
|
p.LastUpdate = time.Unix(dbPolicy.LastUpdate.Int64, 0)
|
||||||
|
//nolint:ll
|
||||||
|
p.MessageFlags = sqldb.ExtractSqlInt16[lnwire.ChanUpdateMsgFlags](
|
||||||
|
dbPolicy.MessageFlags,
|
||||||
|
)
|
||||||
|
//nolint:ll
|
||||||
|
p.ChannelFlags = sqldb.ExtractSqlInt16[lnwire.ChanUpdateChanFlags](
|
||||||
|
dbPolicy.ChannelFlags,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if dbPolicy.BlockHeight.Valid {
|
||||||
|
p.LastBlockHeight = uint32(
|
||||||
|
dbPolicy.BlockHeight.Int64,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:ll
|
||||||
|
p.DisableFlags = sqldb.ExtractSqlInt16[lnwire.ChanUpdateDisableFlags](
|
||||||
|
dbPolicy.DisableFlags,
|
||||||
|
)
|
||||||
|
p.ExtraSignedFields = extras
|
||||||
|
}
|
||||||
|
|
||||||
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractChannelPolicies extracts the sqlc.GraphChannelPolicy records from the give
|
// extractChannelPolicies extracts the sqlc.GraphChannelPolicy records from the give
|
||||||
@@ -5517,14 +5558,14 @@ func buildChanPoliciesWithBatchData(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
|
|||||||
*models.ChannelEdgePolicy, error) {
|
*models.ChannelEdgePolicy, error) {
|
||||||
|
|
||||||
pol1, err := buildChanPolicyWithBatchData(
|
pol1, err := buildChanPolicyWithBatchData(
|
||||||
dbPol1, channelID, node2, batchData,
|
true, dbPol1, channelID, node2, batchData,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("unable to build policy1: %w", err)
|
return nil, nil, fmt.Errorf("unable to build policy1: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pol2, err := buildChanPolicyWithBatchData(
|
pol2, err := buildChanPolicyWithBatchData(
|
||||||
dbPol2, channelID, node1, batchData,
|
false, dbPol2, channelID, node1, batchData,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("unable to build policy2: %w", err)
|
return nil, nil, fmt.Errorf("unable to build policy2: %w", err)
|
||||||
@@ -5535,9 +5576,10 @@ func buildChanPoliciesWithBatchData(dbPol1, dbPol2 *sqlc.GraphChannelPolicy,
|
|||||||
|
|
||||||
// buildChanPolicyWithBatchData builds a models.ChannelEdgePolicy instance from
|
// buildChanPolicyWithBatchData builds a models.ChannelEdgePolicy instance from
|
||||||
// the provided sqlc.GraphChannelPolicy and the provided batchChannelData.
|
// the provided sqlc.GraphChannelPolicy and the provided batchChannelData.
|
||||||
func buildChanPolicyWithBatchData(dbPol *sqlc.GraphChannelPolicy,
|
func buildChanPolicyWithBatchData(isNode1 bool,
|
||||||
channelID uint64, toNode route.Vertex,
|
dbPol *sqlc.GraphChannelPolicy, channelID uint64,
|
||||||
batchData *batchChannelData) (*models.ChannelEdgePolicy, error) {
|
toNode route.Vertex, batchData *batchChannelData) (
|
||||||
|
*models.ChannelEdgePolicy, error) {
|
||||||
|
|
||||||
if dbPol == nil {
|
if dbPol == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -5550,7 +5592,7 @@ func buildChanPolicyWithBatchData(dbPol *sqlc.GraphChannelPolicy,
|
|||||||
dbPol1Extras = make(map[uint64][]byte)
|
dbPol1Extras = make(map[uint64][]byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buildChanPolicy(*dbPol, channelID, dbPol1Extras, toNode)
|
return buildChanPolicy(isNode1, *dbPol, channelID, dbPol1Extras, toNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// batchChannelData holds all the related data for a batch of channels.
|
// batchChannelData holds all the related data for a batch of channels.
|
||||||
|
|||||||
Reference in new issue
Block a user