@@ -454,7 +454,7 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
454454
455455 policyCount ++
456456
457- _ , _ , _ , err := updateChanEdgePolicy (ctx , sqlDB , policy )
457+ _ , _ , _ , err := insertChanEdgePolicyMig (ctx , sqlDB , policy )
458458 if err != nil {
459459 return fmt .Errorf ("could not migrate channel " +
460460 "policy %d: %w" , policy .ChannelID , err )
@@ -1560,3 +1560,102 @@ func insertChannelMig(ctx context.Context, db SQLQueries,
15601560 node2ID : node2DBID ,
15611561 }, nil
15621562}
1563+
1564+ // insertChanEdgePolicyMig inserts the channel policy info we have stored for
1565+ // a channel we already know of. This is used during the SQL migration
1566+ // 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.
1571+ func 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 )
1600+
1601+ // Figure out which node this edge is from.
1602+ isNode1 = edge .ChannelFlags & lnwire .ChanUpdateDirection == 0
1603+ nodeID := dbChan .NodeID1
1604+ if ! isNode1 {
1605+ nodeID = dbChan .NodeID2
1606+ }
1607+
1608+ var (
1609+ inboundBase sql.NullInt64
1610+ inboundRate sql.NullInt64
1611+ )
1612+ edge .InboundFee .WhenSome (func (fee lnwire.Fee ) {
1613+ inboundRate = sqldb .SQLInt64 (fee .FeeRate )
1614+ inboundBase = sqldb .SQLInt64 (fee .BaseFee )
1615+ })
1616+
1617+ id , err := tx .InsertEdgePolicyMig (ctx , sqlc.InsertEdgePolicyMigParams {
1618+ Version : int16 (ProtocolV1 ),
1619+ ChannelID : dbChan .ID ,
1620+ NodeID : nodeID ,
1621+ Timelock : int32 (edge .TimeLockDelta ),
1622+ FeePpm : int64 (edge .FeeProportionalMillionths ),
1623+ BaseFeeMsat : int64 (edge .FeeBaseMSat ),
1624+ MinHtlcMsat : int64 (edge .MinHTLC ),
1625+ LastUpdate : sqldb .SQLInt64 (edge .LastUpdate .Unix ()),
1626+ Disabled : sql.NullBool {
1627+ Valid : true ,
1628+ Bool : edge .IsDisabled (),
1629+ },
1630+ MaxHtlcMsat : sql.NullInt64 {
1631+ Valid : edge .MessageFlags .HasMaxHtlc (),
1632+ Int64 : int64 (edge .MaxHTLC ),
1633+ },
1634+ MessageFlags : sqldb .SQLInt16 (edge .MessageFlags ),
1635+ ChannelFlags : sqldb .SQLInt16 (edge .ChannelFlags ),
1636+ InboundBaseFeeMsat : inboundBase ,
1637+ InboundFeeRateMilliMsat : inboundRate ,
1638+ Signature : edge .SigBytes ,
1639+ })
1640+ if err != nil {
1641+ return node1Pub , node2Pub , isNode1 ,
1642+ fmt .Errorf ("unable to upsert edge policy: %w" , err )
1643+ }
1644+
1645+ // Convert the flat extra opaque data into a map of TLV types to
1646+ // values.
1647+ extra , err := marshalExtraOpaqueData (edge .ExtraOpaqueData )
1648+ if err != nil {
1649+ return node1Pub , node2Pub , false , fmt .Errorf ("unable to " +
1650+ "marshal extra opaque data: %w" , err )
1651+ }
1652+
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 )
1658+ }
1659+
1660+ return node1Pub , node2Pub , isNode1 , nil
1661+ }
0 commit comments