Skip to content

Commit

Permalink
Merge pull request #78 from Impa10r/v1.6.5
Browse files Browse the repository at this point in the history
v1.6.5
  • Loading branch information
Impa10r authored Jul 11, 2024
2 parents aee3698 + 71fcba1 commit 149b6ca
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 22 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Versions

## 1.6.5

- Reduce frequency of balance announcements to 24hr unless changed
- Pre-fill 0 if possible swap amount is below 100,000

## 1.6.4

- Implement advertizing L-BTC balance: LND send and receive, CLN send
- Implement advertising L-BTC balance: LND send and receive, CLN send
- Pre-fill swap amount to return the channel closer to 50/50 if viable
- Persist NodeId per ChannelId map to avoid *closed channel*
- Apply AutoFees even while HTLC is pending
Expand Down
15 changes: 10 additions & 5 deletions cmd/psweb/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
Filter: nodeId != "" || state != "" || role != "",
AutoSwapEnabled: config.Config.AutoSwapEnabled,
PeginPending: config.Config.PeginTxId != "" && config.Config.PeginClaimScript != "",
AdvertizeEnabled: advertizeLiquidBalance,
AdvertizeEnabled: ln.AdvertizeLiquidBalance,
}

// executing template named "homepage" with retries
Expand Down Expand Up @@ -416,6 +416,9 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {

// arbitrary haircut to avoid 'no matching outgoing channel available'
maxLiquidSwapIn := min(satAmount-2000, maxRemoteBalance-10000)
if maxLiquidSwapIn < 100_000 {
maxLiquidSwapIn = 0
}

peerLiquidBalance := int64(-1)
maxLiquidSwapOut := uint64(0)
Expand All @@ -428,6 +431,8 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-5000, peerLiquidBalance-20300)))
if maxLiquidSwapOut >= 100_000 {
selectedChannel = peer.Channels[maxLocalBalanceIndex].ChannelId
} else {
maxLiquidSwapOut = 0
}
}

Expand Down Expand Up @@ -1547,7 +1552,7 @@ func liquidHandler(w http.ResponseWriter, r *http.Request) {
AutoSwapThresholdPPM: config.Config.AutoSwapThresholdPPM,
AutoSwapTargetPct: config.Config.AutoSwapTargetPct,
AutoSwapCandidate: &candidate,
AdvertizeEnabled: advertizeLiquidBalance,
AdvertizeEnabled: ln.AdvertizeLiquidBalance,
}

// executing template named "liquid"
Expand All @@ -1573,12 +1578,12 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {

switch action {
case "advertizeLiquidBalance":
advertizeLiquidBalance = r.FormValue("enabled") == "on"
db.Save("Peers", "AdvertizeLiquidBalance", advertizeLiquidBalance)
ln.AdvertizeLiquidBalance = r.FormValue("enabled") == "on"
db.Save("Peers", "AdvertizeLiquidBalance", ln.AdvertizeLiquidBalance)

msg := "Broadcasting Liquid Balance is "

if advertizeLiquidBalance {
if ln.AdvertizeLiquidBalance {
msg += "Enabled"
} else {
msg += "Disabled"
Expand Down
12 changes: 12 additions & 0 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ var (

// received via custom messages, per peer nodeId
LiquidBalances = make(map[string]*BalanceInfo)

// sent via custom messages
SentLiquidBalances = make(map[string]*BalanceInfo)

// current LiquidBalance
LiquidBalance uint64

// global setting
AdvertizeLiquidBalance = false
)

func toSats(amount float64) int64 {
Expand Down Expand Up @@ -285,6 +294,9 @@ func LoadDB() {
db.Load("AutoFees", "AutoFee", &AutoFee)
db.Load("AutoFees", "AutoFeeDefaults", &AutoFeeDefaults)

// on or off
db.Load("Peers", "AdvertizeLiquidBalance", &AdvertizeLiquidBalance)

// drop non-array legacy log
var log map[uint64]interface{}
db.Load("AutoFees", "AutoFeeLog", &log)
Expand Down
25 changes: 22 additions & 3 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,25 @@ func subscribeMessages(ctx context.Context, client lnrpc.LightningClient) error

nodeId := hex.EncodeToString(data.Peer)

// received request for information
if msg.Memo == "poll" && msg.Asset == "lbtc" && AdvertizeLiquidBalance {
if SendCustomMessage(client, nodeId, &Message{
Version: MessageVersion,
Memo: "balance",
Asset: "lbtc",
Amount: LiquidBalance,
}) == nil {
// save announcement
ptr := SentLiquidBalances[nodeId]
if ptr == nil {
SentLiquidBalances[nodeId] = new(BalanceInfo)
}
SentLiquidBalances[nodeId].Amount = LiquidBalance
SentLiquidBalances[nodeId].TimeStamp = time.Now().Unix()
}
}

// received information
if msg.Memo == "balance" && msg.Asset == "lbtc" {
ts := time.Now().Unix()
if LiquidBalances[nodeId] == nil {
Expand Down Expand Up @@ -1514,7 +1533,7 @@ func ListPeers(client lnrpc.LightningClient, peerId string, excludeIds *[]string
if channel.RemotePubkey == lndPeer.PubKey {
peer.Channels = append(peer.Channels, &peerswaprpc.PeerSwapPeerChannel{
ChannelId: channel.ChanId,
LocalBalance: uint64(channel.LocalBalance + channel.UnsettledBalance),
LocalBalance: uint64(channel.LocalBalance),
RemoteBalance: uint64(channel.RemoteBalance),
Active: channel.Active,
})
Expand Down Expand Up @@ -1816,7 +1835,7 @@ func applyAutoFee(client lnrpc.LightningClient, channelId uint64, htlcFail bool)
localBalance := int64(0)
for _, ch := range res.Channels {
if ch.ChanId == channelId {
localBalance = ch.LocalBalance + ch.UnsettledBalance
localBalance = ch.LocalBalance
break
}
}
Expand Down Expand Up @@ -1910,7 +1929,7 @@ func ApplyAutoFees() {

oldFee := int(policy.FeeRateMilliMsat)
newFee := oldFee
liqPct := int((ch.LocalBalance + ch.UnsettledBalance) * 100 / r.Capacity)
liqPct := int(ch.LocalBalance * 100 / r.Capacity)

newFee = calculateAutoFee(ch.ChanId, params, liqPct, oldFee)

Expand Down
79 changes: 67 additions & 12 deletions cmd/psweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

const (
// App version tag
version = "v1.6.4"
version = "v1.6.5"
)

type SwapParams struct {
Expand Down Expand Up @@ -66,8 +66,6 @@ var (
autoSwapId string
// store peer pub mapped to channel Id
peerNodeId = make(map[uint64]string)
// global setting
advertizeLiquidBalance = false
)

func main() {
Expand Down Expand Up @@ -122,7 +120,6 @@ func main() {
// Load persisted data from database
ln.LoadDB()
db.Load("Peers", "NodeId", &peerNodeId)
db.Load("Peers", "AdvertizeLiquidBalance", &advertizeLiquidBalance)
db.Load("Swaps", "txFee", &txFee)

// fetch all chain costs
Expand Down Expand Up @@ -220,6 +217,11 @@ func main() {
// CLN: refresh forwarding stats
go ln.CacheForwards()

// request balance refresh from peers
if ln.Implementation == "LND" {
go pollBalances()
}

// Handle termination signals
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
Expand Down Expand Up @@ -1630,7 +1632,7 @@ func last(x int, a interface{}) bool {

func advertizeBalance() {

if !advertizeLiquidBalance {
if !ln.AdvertizeLiquidBalance {
return
}

Expand All @@ -1657,6 +1659,9 @@ func advertizeBalance() {

satAmount := res2.GetSatAmount()

// store for replies to poll requests
ln.LiquidBalance = satAmount

res3, err := ps.ListPeers(client)
if err != nil {
return
Expand All @@ -1669,18 +1674,68 @@ func advertizeBalance() {
defer clean()

for _, peer := range res3.GetPeers() {
ln.SendCustomMessage(cl, peer.NodeId, &ln.Message{
// refresh balances received over 24 hours ago
if ptr := ln.LiquidBalances[peer.NodeId]; ptr != nil {
if ptr.TimeStamp < time.Now().AddDate(0, 0, -1).Unix() {
ln.SendCustomMessage(cl, peer.NodeId, &ln.Message{
Version: ln.MessageVersion,
Memo: "poll",
Asset: "lbtc",
})
// delete stale information
ln.LiquidBalances[peer.NodeId] = nil
}
}

ptr := ln.SentLiquidBalances[peer.NodeId]
if ptr != nil {
if ptr.Amount == satAmount && ptr.TimeStamp > time.Now().AddDate(0, 0, -1).Unix() {
// do not repeat within 24 hours unless changed
continue
}
}

if ln.SendCustomMessage(cl, peer.NodeId, &ln.Message{
Version: ln.MessageVersion,
Memo: "balance",
Asset: "lbtc",
Amount: satAmount,
})

// delete stale received balances over 24 hours ago
if ptr := ln.LiquidBalances[peer.NodeId]; ptr != nil {
if ptr.TimeStamp < time.Now().AddDate(0, 0, -1).Unix() {
ln.LiquidBalances[peer.NodeId] = nil
}) == nil {
// save announcement details
if ptr == nil {
ln.SentLiquidBalances[peer.NodeId] = new(ln.BalanceInfo)
}
ln.SentLiquidBalances[peer.NodeId].Amount = satAmount
ln.SentLiquidBalances[peer.NodeId].TimeStamp = time.Now().Unix()
}
}
}

func pollBalances() {
client, cleanup, err := ps.GetClient(config.Config.RpcHost)
if err != nil {
return
}
defer cleanup()

res, err := ps.ListPeers(client)
if err != nil {
return
}

cl, clean, er := ln.GetClient()
if er != nil {
return
}
defer clean()

for _, peer := range res.GetPeers() {
if ln.SendCustomMessage(cl, peer.NodeId, &ln.Message{
Version: ln.MessageVersion,
Memo: "poll",
Asset: "lbtc",
}) != nil {
log.Println("Failed to poll L-BTC balance from", getNodeAlias(peer.NodeId))
}
}
}
2 changes: 1 addition & 1 deletion cmd/psweb/templates/liquid.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<div class="box has-text-left">
<div style="display: grid; grid-template-columns: auto auto;">
<div style="text-align: left;">
<h4 class="title is-4">Advertize L-BTC Balance</h4>
<h4 class="title is-4">Advertise L-BTC Balance</h4>
</div>
<div style="display: flex; justify-content: flex-end;">
<form id="toggleForm_0" action="/submit" method="post">
Expand Down

0 comments on commit 149b6ca

Please sign in to comment.