Skip to content

Commit

Permalink
Merge pull request #69 from Impa10r/v1.5.8
Browse files Browse the repository at this point in the history
v1.5.8
  • Loading branch information
Impa10r authored Jun 25, 2024
2 parents f2a60d5 + e3c9ade commit 33faf8f
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 75 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Versions

## 1.5.8

- Ignore forwards < 1000 sats in statistics and autofees
- Show 7 day Fee Log for individual channels

## 1.5.7

- AutoFee: Fix enable/disable all individual channels
Expand Down
16 changes: 12 additions & 4 deletions cmd/psweb/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,13 @@ func afHandler(w http.ResponseWriter, r *http.Request) {

var feeLog []FeeLog

// load the last 24 hours of fee changes
startTS := time.Now().Add(-24 * time.Hour).Unix()
// 24 hours fee log for all channels
days := 1
if channelId > 0 {
// or 7 days for a single one
days = 7
}
startTS := time.Now().AddDate(0, 0, -days).Unix()

for id := range ln.AutoFeeLog {
for _, event := range ln.AutoFeeLog[id] {
Expand Down Expand Up @@ -1147,8 +1152,11 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {

swapData += `<tr><td style="text-align: right">Swap Cost:</td><td>`
swapData += formatSigned(cost) + " sats"
swapData += `<tr><td style="text-align: right">Cost PPM:</td><td>`
swapData += formatSigned(ppm)

if swap.State == "State_ClaimedPreimage" {
swapData += `<tr><td style="text-align: right">Cost PPM:</td><td>`
swapData += formatSigned(ppm)
}
}

swapData += `</td></tr>
Expand Down
12 changes: 6 additions & 6 deletions cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func CacheForwards() {
forwardsLastIndex = newForwards.Forwards[n-1].CreatedIndex + 1
for _, f := range newForwards.Forwards {
chOut := ConvertClnToLndChannelId(f.OutChannel)
if f.Status == "settled" {
if f.Status == "settled" && f.OutMsat > ignoreForwardsMsat {
chIn := ConvertClnToLndChannelId(f.InChannel)
forwardsIn[chIn] = append(forwardsIn[chIn], f)
forwardsOut[chOut] = append(forwardsOut[chOut], f)
Expand Down Expand Up @@ -465,7 +465,7 @@ func GetForwardingStats(lndChannelId uint64) *ForwardingStats {
timestamp6m := float64(now.AddDate(0, -6, 0).Unix())

for _, e := range forwardsOut[lndChannelId] {
if e.ResolvedTime > timestamp6m {
if e.ResolvedTime > timestamp6m && e.OutMsat > ignoreForwardsMsat {
amountOut6m += e.OutMsat
feeMsat6m += e.FeeMsat
if e.ResolvedTime > timestamp30d {
Expand All @@ -479,7 +479,7 @@ func GetForwardingStats(lndChannelId uint64) *ForwardingStats {
}
}
for _, e := range forwardsIn[lndChannelId] {
if e.ResolvedTime > timestamp6m {
if e.ResolvedTime > timestamp6m && e.OutMsat > ignoreForwardsMsat {
amountIn6m += e.OutMsat
assistedMsat6m += e.FeeMsat
if e.ResolvedTime > timestamp30d {
Expand Down Expand Up @@ -620,13 +620,13 @@ func GetChannelStats(lndChannelId uint64, timeStamp uint64) *ChannelStats {
timeStampF := float64(timeStamp)

for _, e := range forwardsOut[lndChannelId] {
if e.ResolvedTime > timeStampF {
if e.ResolvedTime > timeStampF && e.OutMsat > ignoreForwardsMsat {
amountOut += e.OutMsat
feeMsat += e.FeeMsat
}
}
for _, e := range forwardsIn[lndChannelId] {
if e.ResolvedTime > timeStampF {
if e.ResolvedTime > timeStampF && e.OutMsat > ignoreForwardsMsat {
amountIn += e.OutMsat
assistedMsat += e.FeeMsat
}
Expand Down Expand Up @@ -1216,7 +1216,7 @@ func PlotPPM(channelId uint64) *[]DataPoint {

for _, e := range forwardsOut[channelId] {
// ignore small forwards
if e.OutMsat > 1000000 {
if e.OutMsat > ignoreForwardsMsat {
plot = append(plot, DataPoint{
TS: uint64(e.ResolvedTime),
Amount: e.OutMsat / 1000,
Expand Down
12 changes: 7 additions & 5 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ type DataPoint struct {
Label string
}

// ignore small forwards
const ignoreForwardsMsat = 1_000_000

var (
// lightning payments from swap out initiator to receiver
SwapRebates = make(map[string]int64)
Expand Down Expand Up @@ -270,15 +273,16 @@ func LoadDB() {

func calculateAutoFee(channelId uint64, params *AutoFeeParams, liqPct int, oldFee int) int {
newFee := oldFee
if liqPct > params.LowLiqPct {
if liqPct >= params.LowLiqPct {
// normal or high liquidity regime, check if fee can be dropped
lastUpdate := int64(0)
lastLog := LastAutoFeeLog(channelId, false)
if lastLog != nil {
lastUpdate = lastLog.TimeStamp
}
// see if cool-off period has passed
if lastUpdate < time.Now().Add(-time.Duration(params.CoolOffHours)*time.Hour).Unix() {
// check the last outbound timestamp
// check the inactivity period
if lastForwardTS[channelId] < time.Now().AddDate(0, 0, -params.InactivityDays).Unix() {
// decrease the fee
newFee -= params.InactivityDropPPM
Expand Down Expand Up @@ -345,7 +349,7 @@ func moveLowLiqThreshold(channelId uint64, bump int) {
*AutoFee[channelId] = AutoFeeDefaults
}

// do not alow exeeding high liquidity threshold
// do not allow reaching high liquidity threshold
if AutoFee[channelId].LowLiqPct+bump < AutoFee[channelId].ExcessPct {
AutoFee[channelId].LowLiqPct += bump
// persist to db
Expand All @@ -357,6 +361,4 @@ func moveLowLiqThreshold(channelId uint64, bump int) {
func saveSwapRabate(swapId string, rebate int64) {
// save rebate payment
SwapRebates[swapId] = rebate
// save to db
db.Save("Swaps", "SwapRebates", SwapRebates)
}
55 changes: 26 additions & 29 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ const (
// https://github.com/ElementsProject/peerswap/blob/master/peerswaprpc/server.go#L234
SwapFeeReserveLBTC = uint64(1000)
SwapFeeReserveBTC = uint64(2000)

// used in outbound forwards tracking for auto fees
forwardAmountThreshold = uint64(1000)
)

type InflightHTLC struct {
Expand Down Expand Up @@ -774,10 +771,9 @@ func downloadForwards(client lnrpc.LightningClient) {

// sort by in and out channels
for _, event := range res.ForwardingEvents {
forwardsIn[event.ChanIdIn] = append(forwardsIn[event.ChanIdIn], event)
forwardsOut[event.ChanIdOut] = append(forwardsOut[event.ChanIdOut], event)
// record for autofees
if event.AmtOut >= forwardAmountThreshold {
if event.AmtOutMsat > ignoreForwardsMsat {
forwardsIn[event.ChanIdIn] = append(forwardsIn[event.ChanIdIn], event)
forwardsOut[event.ChanIdOut] = append(forwardsOut[event.ChanIdOut], event)
lastForwardTS[event.ChanIdOut] = int64(event.TimestampNs / 1_000_000_000)
}
}
Expand Down Expand Up @@ -980,30 +976,31 @@ func subscribeForwards(ctx context.Context, client routerrpc.RouterClient) error
// find HTLC in queue
for _, htlc := range inflightHTLCs[htlcEvent.IncomingChannelId] {
if htlc.IncomingHtlcId == htlcEvent.IncomingHtlcId {
// add our stored forwards
forwardsIn[htlcEvent.IncomingChannelId] = append(forwardsIn[htlcEvent.IncomingChannelId], htlc.forwardingEvent)
// settled htlcEvent has no Outgoing info, take from queue
forwardsOut[htlc.OutgoingChannelId] = append(forwardsOut[htlc.OutgoingChannelId], htlc.forwardingEvent)
// store the last timestamp
lastForwardCreationTs = htlc.forwardingEvent.TimestampNs / 1_000_000_000
// delete from queue
removeInflightHTLC(htlcEvent.IncomingChannelId, htlcEvent.IncomingHtlcId)

// record for autofees
if htlc.forwardingEvent.AmtOut >= forwardAmountThreshold {
// ignore dust
if htlc.forwardingEvent.AmtOutMsat > ignoreForwardsMsat {
// add our stored forwards
forwardsIn[htlcEvent.IncomingChannelId] = append(forwardsIn[htlcEvent.IncomingChannelId], htlc.forwardingEvent)
// settled htlcEvent has no Outgoing info, take from queue
forwardsOut[htlc.OutgoingChannelId] = append(forwardsOut[htlc.OutgoingChannelId], htlc.forwardingEvent)
// TS for autofee
lastForwardTS[htlc.forwardingEvent.ChanIdOut] = int64(htlc.forwardingEvent.TimestampNs / 1_000_000_000)
}

// execute autofee
client, cleanup, err := GetClient()
if err != nil {
return err
}
defer cleanup()
// execute autofee
client, cleanup, err := GetClient()
if err != nil {
return err
}
defer cleanup()

// calculate with new balance
applyAutoFee(client, htlc.forwardingEvent.ChanIdOut, false)
break
// calculate with new balance
applyAutoFee(client, htlc.forwardingEvent.ChanIdOut, false)
break
}
}
}
}
Expand Down Expand Up @@ -1170,7 +1167,7 @@ func GetForwardingStats(channelId uint64) *ForwardingStats {
timestamp6m := uint64(now.AddDate(0, -6, 0).Unix()) * 1_000_000_000

for _, e := range forwardsOut[channelId] {
if e.TimestampNs > timestamp6m {
if e.TimestampNs > timestamp6m && e.AmtOutMsat > ignoreForwardsMsat {
result.AmountOut6m += e.AmtOut
feeMsat6m += e.FeeMsat
if e.TimestampNs > timestamp30d {
Expand All @@ -1185,7 +1182,7 @@ func GetForwardingStats(channelId uint64) *ForwardingStats {
}

for _, e := range forwardsIn[channelId] {
if e.TimestampNs > timestamp6m {
if e.TimestampNs > timestamp6m && e.AmtOutMsat > ignoreForwardsMsat {
result.AmountIn6m += e.AmtIn
assistedMsat6m += e.FeeMsat
if e.TimestampNs > timestamp30d {
Expand Down Expand Up @@ -1285,13 +1282,13 @@ func GetChannelStats(channelId uint64, timeStamp uint64) *ChannelStats {
timestampNs := timeStamp * 1_000_000_000

for _, e := range forwardsOut[channelId] {
if e.TimestampNs > timestampNs {
if e.TimestampNs > timestampNs && e.AmtOutMsat > ignoreForwardsMsat {
routedOutMsat += e.AmtOutMsat
feeMsat += e.FeeMsat
}
}
for _, e := range forwardsIn[channelId] {
if e.TimestampNs > timestampNs {
if e.TimestampNs > timestampNs && e.AmtOutMsat > ignoreForwardsMsat {
routedInMsat += e.AmtInMsat
assistedMsat += e.FeeMsat
}
Expand Down Expand Up @@ -1751,7 +1748,7 @@ func applyAutoFee(client lnrpc.LightningClient, channelId uint64, htlcFail bool)

liqPct := int(localBalance * 100 / r.Capacity)
if htlcFail {
if liqPct <= params.LowLiqPct {
if liqPct < params.LowLiqPct {
// increase fee to help prevent further failed HTLCs
newFee += params.FailedBumpPPM
} else {
Expand Down Expand Up @@ -1888,7 +1885,7 @@ func PlotPPM(channelId uint64) *[]DataPoint {

for _, e := range forwardsOut[channelId] {
// ignore small forwards
if e.AmtOut > 1000 {
if e.AmtOutMsat > ignoreForwardsMsat {
plot = append(plot, DataPoint{
TS: e.TimestampNs / 1_000_000_000,
Amount: e.AmtOut,
Expand Down
Loading

0 comments on commit 33faf8f

Please sign in to comment.