@@ -440,7 +440,9 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
440440 Interval : 10 * time .Second ,
441441 }
442442 )
443- migChanPolicy := func (policy * models.ChannelEdgePolicy ) error {
443+ migChanPolicy := func (dbChanInfo * dbChanInfo ,
444+ policy * models.ChannelEdgePolicy ) error {
445+
444446 // If the policy is nil, we can skip it.
445447 if policy == nil {
446448 return nil
@@ -454,7 +456,7 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
454456
455457 policyCount ++
456458
457- _ , _ , _ , err := insertChanEdgePolicyMig (ctx , sqlDB , policy )
459+ err := insertChanEdgePolicyMig (ctx , sqlDB , dbChanInfo , policy )
458460 if err != nil {
459461 return fmt .Errorf ("could not migrate channel " +
460462 "policy %d: %w" , policy .ChannelID , err )
@@ -531,12 +533,12 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
531533 }
532534
533535 // Now, migrate the two channel policies for the channel.
534- err = migChanPolicy (policy1 )
536+ err = migChanPolicy (dbChanInfo , policy1 )
535537 if err != nil {
536538 return fmt .Errorf ("could not migrate policy1(%d): %w" ,
537539 scid , err )
538540 }
539- err = migChanPolicy (policy2 )
541+ err = migChanPolicy (dbChanInfo , policy2 )
540542 if err != nil {
541543 return fmt .Errorf ("could not migrate policy2(%d): %w" ,
542544 scid , err )
@@ -1564,45 +1566,14 @@ func insertChannelMig(ctx context.Context, db SQLQueries,
15641566// insertChanEdgePolicyMig inserts the channel policy info we have stored for
15651567// a channel we already know of. This is used during the SQL migration
15661568// process to insert channel policies.
1567- //
1568- // TODO(elle): update this function to be more performant in the migration
1569- // setting. For the sake of keeping the commit that introduced this function
1570- // simple, this is for now mostly the same as updateChanEdgePolicy.
15711569func insertChanEdgePolicyMig (ctx context.Context , tx SQLQueries ,
1572- edge * models.ChannelEdgePolicy ) (route.Vertex , route.Vertex , bool ,
1573- error ) {
1574-
1575- var (
1576- node1Pub , node2Pub route.Vertex
1577- isNode1 bool
1578- chanIDB = channelIDToBytes (edge .ChannelID )
1579- )
1580-
1581- // Check that this edge policy refers to a channel that we already
1582- // know of. We do this explicitly so that we can return the appropriate
1583- // ErrEdgeNotFound error if the channel doesn't exist, rather than
1584- // abort the transaction which would abort the entire batch.
1585- dbChan , err := tx .GetChannelAndNodesBySCID (
1586- ctx , sqlc.GetChannelAndNodesBySCIDParams {
1587- Scid : chanIDB ,
1588- Version : int16 (ProtocolV1 ),
1589- },
1590- )
1591- if errors .Is (err , sql .ErrNoRows ) {
1592- return node1Pub , node2Pub , false , ErrEdgeNotFound
1593- } else if err != nil {
1594- return node1Pub , node2Pub , false , fmt .Errorf ("unable to " +
1595- "fetch channel(%v): %w" , edge .ChannelID , err )
1596- }
1597-
1598- copy (node1Pub [:], dbChan .Node1PubKey )
1599- copy (node2Pub [:], dbChan .Node2PubKey )
1570+ dbChan * dbChanInfo , edge * models.ChannelEdgePolicy ) error {
16001571
16011572 // Figure out which node this edge is from.
1602- isNode1 = edge .ChannelFlags & lnwire .ChanUpdateDirection == 0
1603- nodeID := dbChan .NodeID1
1573+ isNode1 : = edge .ChannelFlags & lnwire .ChanUpdateDirection == 0
1574+ nodeID := dbChan .node1ID
16041575 if ! isNode1 {
1605- nodeID = dbChan .NodeID2
1576+ nodeID = dbChan .node2ID
16061577 }
16071578
16081579 var (
@@ -1616,7 +1587,7 @@ func insertChanEdgePolicyMig(ctx context.Context, tx SQLQueries,
16161587
16171588 id , err := tx .InsertEdgePolicyMig (ctx , sqlc.InsertEdgePolicyMigParams {
16181589 Version : int16 (ProtocolV1 ),
1619- ChannelID : dbChan .ID ,
1590+ ChannelID : dbChan .channelID ,
16201591 NodeID : nodeID ,
16211592 Timelock : int32 (edge .TimeLockDelta ),
16221593 FeePpm : int64 (edge .FeeProportionalMillionths ),
@@ -1638,24 +1609,32 @@ func insertChanEdgePolicyMig(ctx context.Context, tx SQLQueries,
16381609 Signature : edge .SigBytes ,
16391610 })
16401611 if err != nil {
1641- return node1Pub , node2Pub , isNode1 ,
1642- fmt .Errorf ("unable to upsert edge policy: %w" , err )
1612+ return fmt .Errorf ("unable to upsert edge policy: %w" , err )
16431613 }
16441614
16451615 // Convert the flat extra opaque data into a map of TLV types to
16461616 // values.
16471617 extra , err := marshalExtraOpaqueData (edge .ExtraOpaqueData )
16481618 if err != nil {
1649- return node1Pub , node2Pub , false , fmt .Errorf ("unable to " +
1650- "marshal extra opaque data: %w" , err )
1619+ return fmt .Errorf ("unable to marshal extra opaque data: %w" ,
1620+ err )
16511621 }
16521622
1653- // Update the channel policy's extra signed fields.
1654- err = upsertChanPolicyExtraSignedFields (ctx , tx , id , extra )
1655- if err != nil {
1656- return node1Pub , node2Pub , false , fmt .Errorf ("inserting chan " +
1657- "policy extra TLVs: %w" , err )
1623+ // Insert all new extra signed fields for the channel policy.
1624+ for tlvType , value := range extra {
1625+ err = tx .UpsertChanPolicyExtraType (
1626+ ctx , sqlc.UpsertChanPolicyExtraTypeParams {
1627+ ChannelPolicyID : id ,
1628+ Type : int64 (tlvType ),
1629+ Value : value ,
1630+ },
1631+ )
1632+ if err != nil {
1633+ return fmt .Errorf ("unable to insert " +
1634+ "channel_policy(%d) extra signed field(%v): %w" ,
1635+ id , tlvType , err )
1636+ }
16581637 }
16591638
1660- return node1Pub , node2Pub , isNode1 , nil
1639+ return nil
16611640}
0 commit comments