From c11bae38fdb1963c40815e049819d5ae84b7f6fc Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 23 Aug 2022 23:08:56 +0200 Subject: [PATCH] refactor --- channelAcceptor.go | 45 ++++++++++++++++++++++++++------------------- htlcInterceptor.go | 26 ++++++++++++++++---------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/channelAcceptor.go b/channelAcceptor.go index 4197484..abd1cea 100644 --- a/channelAcceptor.go +++ b/channelAcceptor.go @@ -67,25 +67,6 @@ func (app *App) interceptChannelEvents(ctx context.Context) 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 if alias != "" { 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, }) + // make decision + accept := app.channelAcceptDecision(req) + res := lnrpc.ChannelAcceptResponse{} if accept { 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 { stream, err := app.lnd.subscribeChannelEvents(ctx, &lnrpc.ChannelEventSubscription{}) if err != nil { diff --git a/htlcInterceptor.go b/htlcInterceptor.go index c166f33..688ef4a 100644 --- a/htlcInterceptor.go +++ b/htlcInterceptor.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/hex" + "encoding/json" "fmt" "strings" "sync" @@ -205,16 +206,21 @@ func (app *App) logHtlcEvents(ctx context.Context) error { continue } - contextLogger := log.WithFields(log.Fields{ - "event": "forward_event", - "chan_id": ParseChannelID(event.IncomingChannelId), - "htlc_id": event.IncomingHtlcId, - }) + contextLogger := func(event *routerrpc.HtlcEvent) *log.Entry { + b, err := json.Marshal(event) + if err != nil { + panic(err) + } + return log.WithFields(log.Fields{ + "type": "forward", + "event": string(b), + }) + } switch event.Event.(type) { case *routerrpc.HtlcEvent_SettleEvent: if Configuration.LogJson { - contextLogger.Infof("SettleEvent") + contextLogger(event).Infof("SettleEvent") // contextLogger.Debugf("[forward] Preimage: %s", hex.EncodeToString(event.GetSettleEvent().Preimage)) } else { 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: if Configuration.LogJson { - contextLogger.Infof("ForwardFailEvent") + contextLogger(event).Infof("ForwardFailEvent") // contextLogger.Debugf("[forward] Reason: %s", event.GetForwardFailEvent()) } else { 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: if Configuration.LogJson { - contextLogger.Infof("ForwardEvent") + contextLogger(event).Infof("ForwardEvent") } else { 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: if Configuration.LogJson { - contextLogger.Infof("LinkFailEvent") - contextLogger.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString) + contextLogger(event).Infof("LinkFailEvent") + // contextLogger(event).Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString) } else { log.Infof("[forward] HTLC LinkFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId) log.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)