Skip to content

Commit bbe7f55

Browse files
committed
fix: remove dead code in CLN backend from ListTransactions
1 parent ee7bdcc commit bbe7f55

File tree

1 file changed

+1
-353
lines changed

1 file changed

+1
-353
lines changed

lnclient/cln/cln.go

Lines changed: 1 addition & 353 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,258 +1196,7 @@ func (c *CLNService) ListPeers(ctx context.Context) ([]lnclient.PeerDetails, err
11961196
}
11971197

11981198
func (c *CLNService) ListTransactions(ctx context.Context, from uint64, until uint64, limit uint64, offset uint64, unpaid bool, invoiceType string) (transactions []lnclient.Transaction, err error) {
1199-
logger.Logger.WithFields(logrus.Fields{
1200-
"from": from,
1201-
"until": until,
1202-
"limit": limit,
1203-
"offset": offset,
1204-
"unpaid": unpaid,
1205-
"invoiceType": invoiceType,
1206-
}).Debug("List Transactions")
1207-
1208-
pageSize := uint32(200)
1209-
1210-
fetchInvoices := func() ([]lnclient.Transaction, error) {
1211-
var out []lnclient.Transaction
1212-
seen := make(map[uint64]struct{})
1213-
1214-
waitReq := &clngrpc.WaitRequest{
1215-
Subsystem: clngrpc.WaitRequest_INVOICES,
1216-
Indexname: clngrpc.WaitRequest_CREATED,
1217-
Nextvalue: 0,
1218-
}
1219-
waitResp, err := c.client.Wait(ctx, waitReq)
1220-
if err != nil {
1221-
logger.Logger.WithError(err).Error("wait failed")
1222-
return nil, fmt.Errorf("wait failed: %w", err)
1223-
}
1224-
1225-
if waitResp == nil || waitResp.Created == nil {
1226-
return nil, fmt.Errorf("wait result missing created: %w", err)
1227-
}
1228-
currentIndex := uint64(0)
1229-
if *waitResp.Created > uint64(pageSize)-1 {
1230-
currentIndex -= uint64(pageSize) - 1
1231-
}
1232-
1233-
firstStart := uint64(0)
1234-
firstLimit := uint32(1)
1235-
firstReq := &clngrpc.ListinvoicesRequest{
1236-
Index: clngrpc.ListinvoicesRequest_CREATED.Enum(),
1237-
Start: &firstStart,
1238-
Limit: &firstLimit,
1239-
}
1240-
firstResp, err := c.client.ListInvoices(ctx, firstReq)
1241-
if err != nil {
1242-
logger.Logger.WithError(err).Error("listinvoices failed")
1243-
return nil, fmt.Errorf("listinvoices failed: %w", err)
1244-
}
1245-
firstIndex := uint64(0)
1246-
if firstResp != nil && len(firstResp.Invoices) == 1 && firstResp.Invoices[0].UpdatedIndex != nil {
1247-
firstIndex = *firstResp.Invoices[0].UpdatedIndex
1248-
1249-
}
1250-
1251-
myLimit := pageSize
1252-
1253-
for {
1254-
req := &clngrpc.ListinvoicesRequest{
1255-
Index: clngrpc.ListinvoicesRequest_CREATED.Enum(),
1256-
Start: &currentIndex,
1257-
Limit: &myLimit,
1258-
}
1259-
1260-
resp, err := c.client.ListInvoices(ctx, req)
1261-
if err != nil {
1262-
logger.Logger.WithError(err).Error("listinvoices failed")
1263-
return nil, fmt.Errorf("listinvoices failed: %w", err)
1264-
}
1265-
1266-
for _, invoice := range resp.Invoices {
1267-
idx := uint64(0)
1268-
if invoice.CreatedIndex != nil {
1269-
idx = *invoice.CreatedIndex
1270-
}
1271-
1272-
if idx != 0 {
1273-
if _, ok := seen[idx]; ok {
1274-
continue
1275-
}
1276-
seen[idx] = struct{}{}
1277-
}
1278-
1279-
if !unpaid && invoice.Status != clngrpc.ListinvoicesInvoices_PAID {
1280-
continue
1281-
}
1282-
1283-
tx, err := c.clnInvoiceToTransaction(ctx, invoice)
1284-
if err != nil {
1285-
logger.Logger.WithError(err).Error("failed to convert invoice to transaction")
1286-
return nil, fmt.Errorf("failed to convert invoice to transaction: %w", err)
1287-
}
1288-
out = append(out, *tx)
1289-
}
1290-
1291-
if currentIndex <= 1 || currentIndex <= firstIndex {
1292-
break
1293-
}
1294-
1295-
if uint64(len(out)) >= offset+limit {
1296-
break
1297-
}
1298-
1299-
myLimit = min(pageSize, uint32(currentIndex))
1300-
1301-
if currentIndex > uint64(pageSize) {
1302-
currentIndex -= uint64(pageSize)
1303-
} else {
1304-
currentIndex = 0
1305-
}
1306-
}
1307-
1308-
return out, nil
1309-
}
1310-
1311-
fetchPays := func() ([]lnclient.Transaction, error) {
1312-
var out []lnclient.Transaction
1313-
seen := make(map[uint64]struct{})
1314-
1315-
waitReq := &clngrpc.WaitRequest{
1316-
Subsystem: clngrpc.WaitRequest_SENDPAYS,
1317-
Indexname: clngrpc.WaitRequest_CREATED,
1318-
Nextvalue: 0,
1319-
}
1320-
waitResp, err := c.client.Wait(ctx, waitReq)
1321-
if err != nil {
1322-
logger.Logger.WithError(err).Error("wait failed")
1323-
return nil, fmt.Errorf("wait failed: %w", err)
1324-
}
1325-
1326-
if waitResp == nil || waitResp.Created == nil {
1327-
return nil, fmt.Errorf("wait result missing created: %w", err)
1328-
}
1329-
currentIndex := uint64(0)
1330-
if *waitResp.Created > uint64(pageSize)-1 {
1331-
currentIndex -= uint64(pageSize) - 1
1332-
}
1333-
1334-
firstStart := uint64(0)
1335-
firstLimit := uint32(1)
1336-
firstReq := &clngrpc.ListpaysRequest{
1337-
Index: clngrpc.ListpaysRequest_CREATED.Enum(),
1338-
Start: &firstStart,
1339-
Limit: &firstLimit,
1340-
}
1341-
firstResp, err := c.client.ListPays(ctx, firstReq)
1342-
if err != nil {
1343-
logger.Logger.WithError(err).Error("listpays failed")
1344-
return nil, fmt.Errorf("listpays failed: %w", err)
1345-
}
1346-
firstIndex := uint64(0)
1347-
if firstResp != nil && len(firstResp.Pays) == 1 && firstResp.Pays[0].UpdatedIndex != nil {
1348-
firstIndex = *firstResp.Pays[0].UpdatedIndex
1349-
1350-
}
1351-
1352-
myLimit := pageSize
1353-
1354-
for {
1355-
req := &clngrpc.ListpaysRequest{
1356-
Start: &currentIndex,
1357-
Limit: &myLimit,
1358-
}
1359-
1360-
if !unpaid {
1361-
req.Status = clngrpc.ListpaysRequest_COMPLETE.Enum()
1362-
}
1363-
1364-
resp, err := c.client.ListPays(ctx, req)
1365-
if err != nil {
1366-
logger.Logger.WithError(err).Error("listpays failed")
1367-
return nil, fmt.Errorf("listpays failed: %w", err)
1368-
}
1369-
1370-
for _, pay := range resp.Pays {
1371-
idx := uint64(0)
1372-
if pay.CreatedIndex != nil {
1373-
idx = *pay.CreatedIndex
1374-
}
1375-
1376-
if idx != 0 {
1377-
if _, ok := seen[idx]; ok {
1378-
continue
1379-
}
1380-
seen[idx] = struct{}{}
1381-
}
1382-
1383-
if !unpaid && pay.Status != clngrpc.ListpaysPays_COMPLETE {
1384-
continue
1385-
}
1386-
1387-
tx, err := c.clnPayToTransaction(ctx, pay)
1388-
if err != nil {
1389-
logger.Logger.WithError(err).Error("failed to convert pay to transaction")
1390-
return nil, fmt.Errorf("failed to convert pay to transaction: %w", err)
1391-
}
1392-
out = append(out, *tx)
1393-
}
1394-
1395-
if currentIndex <= 1 || currentIndex <= firstIndex {
1396-
break
1397-
}
1398-
1399-
if uint64(len(out)) >= offset+limit {
1400-
break
1401-
}
1402-
1403-
myLimit = min(pageSize, uint32(currentIndex))
1404-
1405-
if currentIndex > uint64(pageSize) {
1406-
currentIndex -= uint64(pageSize)
1407-
} else {
1408-
currentIndex = 0
1409-
}
1410-
}
1411-
1412-
return out, nil
1413-
}
1414-
1415-
if invoiceType == "" || invoiceType == "incoming" {
1416-
invoices, err := fetchInvoices()
1417-
if err != nil {
1418-
return nil, err
1419-
}
1420-
1421-
transactions = append(transactions, invoices...)
1422-
1423-
}
1424-
1425-
if invoiceType == "" || invoiceType == "outgoing" {
1426-
pays, err := fetchPays()
1427-
if err != nil {
1428-
return nil, err
1429-
}
1430-
1431-
transactions = append(transactions, pays...)
1432-
1433-
}
1434-
1435-
sort.Slice(transactions, func(i, j int) bool {
1436-
return transactions[i].CreatedAt > transactions[j].CreatedAt
1437-
})
1438-
1439-
l := len(transactions)
1440-
1441-
var lim int
1442-
if limit > uint64(l) {
1443-
lim = l
1444-
} else {
1445-
lim = int(limit)
1446-
}
1447-
1448-
transactions = transactions[:lim]
1449-
1450-
return transactions, nil
1199+
return nil, fmt.Errorf("obsolete function")
14511200
}
14521201

14531202
func (c *CLNService) LookupInvoice(ctx context.Context, paymentHash string) (transaction *lnclient.Transaction, err error) {
@@ -1542,107 +1291,6 @@ func (c *CLNService) clnInvoiceToTransaction(ctx context.Context, invoice *clngr
15421291
return transaction, nil
15431292
}
15441293

1545-
func (c *CLNService) clnPayToTransaction(ctx context.Context, pay *clngrpc.ListpaysPays) (*lnclient.Transaction, error) {
1546-
1547-
var invString string
1548-
if pay.Bolt11 != nil {
1549-
invString = *pay.Bolt11
1550-
} else if pay.Bolt12 != nil {
1551-
invString = *pay.Bolt12
1552-
} else {
1553-
invString = ""
1554-
}
1555-
1556-
descriptionHash := ""
1557-
var amount int64
1558-
var decodedInvoice *clngrpc.DecodeResponse
1559-
var err error
1560-
1561-
if invString != "" {
1562-
decodedInvoice, err = c.client.Decode(ctx, &clngrpc.DecodeRequest{String_: invString})
1563-
if err != nil {
1564-
logger.Logger.WithError(err).Error("decode failed")
1565-
return nil, fmt.Errorf("decode failed: %w", err)
1566-
}
1567-
if decodedInvoice == nil {
1568-
return nil, fmt.Errorf("decoded invoice is nil")
1569-
}
1570-
1571-
if !decodedInvoice.Valid {
1572-
return nil, fmt.Errorf("decoded invoice is not valid")
1573-
}
1574-
1575-
switch decodedInvoice.ItemType {
1576-
case clngrpc.DecodeResponse_BOLT11_INVOICE:
1577-
descriptionHash = hex.EncodeToString(decodedInvoice.DescriptionHash)
1578-
case clngrpc.DecodeResponse_BOLT12_INVOICE:
1579-
descriptionHash = ""
1580-
default:
1581-
return nil, fmt.Errorf("not an invoice")
1582-
}
1583-
1584-
}
1585-
1586-
if pay.AmountMsat != nil {
1587-
amount = int64(pay.AmountMsat.Msat)
1588-
} else if decodedInvoice != nil {
1589-
switch decodedInvoice.ItemType {
1590-
case clngrpc.DecodeResponse_BOLT11_INVOICE:
1591-
amount = int64(decodedInvoice.GetAmountMsat().Msat)
1592-
case clngrpc.DecodeResponse_BOLT12_INVOICE:
1593-
amount = int64(decodedInvoice.GetInvoiceAmountMsat().Msat)
1594-
default:
1595-
return nil, fmt.Errorf("not an invoice")
1596-
}
1597-
} else {
1598-
return nil, fmt.Errorf("amount and invoice missing from payment")
1599-
}
1600-
1601-
var description string
1602-
if pay.Description != nil {
1603-
description = *pay.Description
1604-
} else {
1605-
switch decodedInvoice.ItemType {
1606-
case clngrpc.DecodeResponse_BOLT11_INVOICE:
1607-
description = decodedInvoice.GetDescription()
1608-
case clngrpc.DecodeResponse_BOLT12_INVOICE:
1609-
description = decodedInvoice.GetOfferDescription()
1610-
default:
1611-
return nil, fmt.Errorf("not an invoice")
1612-
}
1613-
}
1614-
1615-
var feesPaid int64
1616-
if pay.AmountSentMsat != nil {
1617-
feesPaid = int64(pay.AmountSentMsat.Msat) - amount
1618-
} else {
1619-
feesPaid = 0
1620-
}
1621-
1622-
var settledAt *int64
1623-
if pay.CompletedAt != nil {
1624-
completedAt := int64(*pay.CompletedAt)
1625-
settledAt = &completedAt
1626-
}
1627-
1628-
transaction := &lnclient.Transaction{
1629-
Type: "outgoing",
1630-
Invoice: invString,
1631-
Description: description,
1632-
DescriptionHash: descriptionHash,
1633-
Preimage: hex.EncodeToString(pay.Preimage),
1634-
PaymentHash: hex.EncodeToString(pay.PaymentHash),
1635-
Amount: amount,
1636-
FeesPaid: feesPaid,
1637-
CreatedAt: int64(pay.CreatedAt),
1638-
ExpiresAt: nil,
1639-
SettledAt: settledAt,
1640-
Metadata: lnclient.Metadata{},
1641-
SettleDeadline: nil,
1642-
}
1643-
return transaction, nil
1644-
}
1645-
16461294
func (c *CLNService) MakeHoldInvoice(ctx context.Context, amount int64, description string, descriptionHash string, expiry int64, paymentHash string) (transaction *lnclient.Transaction, err error) {
16471295
if c.clientHold == nil {
16481296
return nil, errors.New("hold plugin not configured")

0 commit comments

Comments
 (0)