mirror of
https://github.com/callebtc/electronwall.git
synced 2025-12-17 15:14:25 +01:00
refactor
This commit is contained in:
@@ -67,25 +67,6 @@ func (app *App) interceptChannelEvents(ctx context.Context) error {
|
|||||||
log.Errorf(err.Error())
|
log.Errorf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine mode and list of channels to parse
|
|
||||||
var accept bool
|
|
||||||
var listToParse []string
|
|
||||||
if Configuration.ChannelMode == "allowlist" {
|
|
||||||
accept = false
|
|
||||||
listToParse = Configuration.ChannelAllowlist
|
|
||||||
} else if Configuration.ChannelMode == "denylist" {
|
|
||||||
accept = true
|
|
||||||
listToParse = Configuration.ChannelDenylist
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse and make decision
|
|
||||||
for _, pubkey := range listToParse {
|
|
||||||
if hex.EncodeToString(req.NodePubkey) == pubkey || pubkey == "*" {
|
|
||||||
accept = !accept
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var channel_info_string string
|
var channel_info_string string
|
||||||
if alias != "" {
|
if alias != "" {
|
||||||
channel_info_string = fmt.Sprintf("(%d sat) from %s (%s, %d sat capacity, %d channels)",
|
channel_info_string = fmt.Sprintf("(%d sat) from %s (%s, %d sat capacity, %d channels)",
|
||||||
@@ -114,6 +95,9 @@ func (app *App) interceptChannelEvents(ctx context.Context) error {
|
|||||||
"num_channels": info.NumChannels,
|
"num_channels": info.NumChannels,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// make decision
|
||||||
|
accept := app.channelAcceptDecision(req)
|
||||||
|
|
||||||
res := lnrpc.ChannelAcceptResponse{}
|
res := lnrpc.ChannelAcceptResponse{}
|
||||||
if accept {
|
if accept {
|
||||||
if Configuration.LogJson {
|
if Configuration.LogJson {
|
||||||
@@ -147,6 +131,29 @@ func (app *App) interceptChannelEvents(ctx context.Context) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *App) channelAcceptDecision(req lnrpc.ChannelAcceptRequest) bool {
|
||||||
|
// determine mode and list of channels to parse
|
||||||
|
var accept bool
|
||||||
|
var listToParse []string
|
||||||
|
if Configuration.ChannelMode == "allowlist" {
|
||||||
|
accept = false
|
||||||
|
listToParse = Configuration.ChannelAllowlist
|
||||||
|
} else if Configuration.ChannelMode == "denylist" {
|
||||||
|
accept = true
|
||||||
|
listToParse = Configuration.ChannelDenylist
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse and make decision
|
||||||
|
for _, pubkey := range listToParse {
|
||||||
|
if hex.EncodeToString(req.NodePubkey) == pubkey || pubkey == "*" {
|
||||||
|
accept = !accept
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return accept
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (app *App) logChannelEvents(ctx context.Context) error {
|
func (app *App) logChannelEvents(ctx context.Context) error {
|
||||||
stream, err := app.lnd.subscribeChannelEvents(ctx, &lnrpc.ChannelEventSubscription{})
|
stream, err := app.lnd.subscribeChannelEvents(ctx, &lnrpc.ChannelEventSubscription{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -205,16 +206,21 @@ func (app *App) logHtlcEvents(ctx context.Context) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
contextLogger := log.WithFields(log.Fields{
|
contextLogger := func(event *routerrpc.HtlcEvent) *log.Entry {
|
||||||
"event": "forward_event",
|
b, err := json.Marshal(event)
|
||||||
"chan_id": ParseChannelID(event.IncomingChannelId),
|
if err != nil {
|
||||||
"htlc_id": event.IncomingHtlcId,
|
panic(err)
|
||||||
|
}
|
||||||
|
return log.WithFields(log.Fields{
|
||||||
|
"type": "forward",
|
||||||
|
"event": string(b),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
switch event.Event.(type) {
|
switch event.Event.(type) {
|
||||||
case *routerrpc.HtlcEvent_SettleEvent:
|
case *routerrpc.HtlcEvent_SettleEvent:
|
||||||
if Configuration.LogJson {
|
if Configuration.LogJson {
|
||||||
contextLogger.Infof("SettleEvent")
|
contextLogger(event).Infof("SettleEvent")
|
||||||
// contextLogger.Debugf("[forward] Preimage: %s", hex.EncodeToString(event.GetSettleEvent().Preimage))
|
// contextLogger.Debugf("[forward] Preimage: %s", hex.EncodeToString(event.GetSettleEvent().Preimage))
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[forward] ⚡️ HTLC SettleEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
log.Infof("[forward] ⚡️ HTLC SettleEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
||||||
@@ -225,7 +231,7 @@ func (app *App) logHtlcEvents(ctx context.Context) error {
|
|||||||
|
|
||||||
case *routerrpc.HtlcEvent_ForwardFailEvent:
|
case *routerrpc.HtlcEvent_ForwardFailEvent:
|
||||||
if Configuration.LogJson {
|
if Configuration.LogJson {
|
||||||
contextLogger.Infof("ForwardFailEvent")
|
contextLogger(event).Infof("ForwardFailEvent")
|
||||||
// contextLogger.Debugf("[forward] Reason: %s", event.GetForwardFailEvent())
|
// contextLogger.Debugf("[forward] Reason: %s", event.GetForwardFailEvent())
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[forward] HTLC ForwardFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
log.Infof("[forward] HTLC ForwardFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
||||||
@@ -234,7 +240,7 @@ func (app *App) logHtlcEvents(ctx context.Context) error {
|
|||||||
|
|
||||||
case *routerrpc.HtlcEvent_ForwardEvent:
|
case *routerrpc.HtlcEvent_ForwardEvent:
|
||||||
if Configuration.LogJson {
|
if Configuration.LogJson {
|
||||||
contextLogger.Infof("ForwardEvent")
|
contextLogger(event).Infof("ForwardEvent")
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[forward] HTLC ForwardEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
log.Infof("[forward] HTLC ForwardEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
||||||
}
|
}
|
||||||
@@ -243,8 +249,8 @@ func (app *App) logHtlcEvents(ctx context.Context) error {
|
|||||||
|
|
||||||
case *routerrpc.HtlcEvent_LinkFailEvent:
|
case *routerrpc.HtlcEvent_LinkFailEvent:
|
||||||
if Configuration.LogJson {
|
if Configuration.LogJson {
|
||||||
contextLogger.Infof("LinkFailEvent")
|
contextLogger(event).Infof("LinkFailEvent")
|
||||||
contextLogger.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
|
// contextLogger(event).Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[forward] HTLC LinkFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
log.Infof("[forward] HTLC LinkFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
|
||||||
log.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
|
log.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
|
||||||
|
|||||||
Reference in New Issue
Block a user