From 5b1cc0f9e6fb91bf996a5906fc8588178914d341 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 24 Aug 2022 10:56:40 +0200 Subject: [PATCH] works --- channelAcceptor.go | 77 ++++++++++++++++------------ htlcInterceptor.go | 32 +++++------- rules/ChannelAccept.js | 2 + rules/ChannelAcceptRequest.js | 2 - rules/ForwardHtlcInterceptRequest.js | 2 - rules/HtlcForward.js | 2 + rules/rules.go | 24 +++++---- 7 files changed, 77 insertions(+), 64 deletions(-) create mode 100644 rules/ChannelAccept.js delete mode 100644 rules/ChannelAcceptRequest.js delete mode 100644 rules/ForwardHtlcInterceptRequest.js create mode 100644 rules/HtlcForward.js diff --git a/channelAcceptor.go b/channelAcceptor.go index 673eadc..ff07d2d 100644 --- a/channelAcceptor.go +++ b/channelAcceptor.go @@ -7,18 +7,37 @@ import ( "sync" "github.com/callebtc/electronwall/rules" + "github.com/callebtc/electronwall/types" "github.com/lightningnetwork/lnd/lnrpc" log "github.com/sirupsen/logrus" ) +func (app *App) getChannelAcceptEvent(ctx context.Context, req lnrpc.ChannelAcceptRequest) (types.ChannelAcceptEvent, error) { + // print the incoming channel request + alias, err := app.lnd.getNodeAlias(ctx, hex.EncodeToString(req.NodePubkey)) + if err != nil { + log.Errorf(err.Error()) + } + + info, err := app.lnd.getNodeInfo(ctx, hex.EncodeToString(req.NodePubkey)) + if err != nil { + log.Errorf(err.Error()) + } + return types.ChannelAcceptEvent{ + PubkeyFrom: hex.EncodeToString(req.NodePubkey), + AliasFrom: alias, + NodeInfo: info, + Event: &req, + }, nil +} + // DispatchChannelAcceptor is the channel acceptor event loop func (app *App) DispatchChannelAcceptor(ctx context.Context) { // the channel event logger go func() { err := app.logChannelEvents(ctx) if err != nil { - log.Error("channel event logger error", - "err", err) + log.Errorf("channel event logger error: %v", err) } }() @@ -26,8 +45,7 @@ func (app *App) DispatchChannelAcceptor(ctx context.Context) { go func() { err := app.interceptChannelEvents(ctx) if err != nil { - log.Error("channel interceptor error", - "err", err) + log.Errorf("channel interceptor error: %v", err) } // release wait group for channel acceptor ctx.Value(ctxKeyWaitGroup).(*sync.WaitGroup).Done() @@ -50,55 +68,50 @@ func (app *App) interceptChannelEvents(ctx context.Context) error { return err } - // print the incoming channel request - alias, err := app.lnd.getNodeAlias(ctx, hex.EncodeToString(req.NodePubkey)) + channelAcceptEvent, err := app.getChannelAcceptEvent(ctx, req) if err != nil { - log.Errorf(err.Error()) + return err } + var node_info_string string - if alias != "" { - node_info_string = fmt.Sprintf("%s (%s)", alias, hex.EncodeToString(req.NodePubkey)) + if channelAcceptEvent.AliasFrom != "" { + node_info_string = fmt.Sprintf("%s (%s)", channelAcceptEvent.AliasFrom, hex.EncodeToString(channelAcceptEvent.Event.NodePubkey)) } else { - node_info_string = hex.EncodeToString(req.NodePubkey) + node_info_string = hex.EncodeToString(channelAcceptEvent.Event.NodePubkey) } log.Debugf("[channel] New channel request from %s", node_info_string) - info, err := app.lnd.getNodeInfo(ctx, hex.EncodeToString(req.NodePubkey)) - if err != nil { - log.Errorf(err.Error()) - } - var channel_info_string string - if alias != "" { + if channelAcceptEvent.AliasFrom != "" { channel_info_string = fmt.Sprintf("(%d sat) from %s (%s, %d sat capacity, %d channels)", - req.FundingAmt, - alias, - trimPubKey(req.NodePubkey), - info.TotalCapacity, - info.NumChannels, + channelAcceptEvent.Event.FundingAmt, + channelAcceptEvent.AliasFrom, + trimPubKey(channelAcceptEvent.Event.NodePubkey), + channelAcceptEvent.NodeInfo.TotalCapacity, + channelAcceptEvent.NodeInfo.NumChannels, ) } else { channel_info_string = fmt.Sprintf("(%d sat) from %s (%d sat capacity, %d channels)", - req.FundingAmt, - trimPubKey(req.NodePubkey), - info.TotalCapacity, - info.NumChannels, + channelAcceptEvent.Event.FundingAmt, + trimPubKey(channelAcceptEvent.Event.NodePubkey), + channelAcceptEvent.NodeInfo.TotalCapacity, + channelAcceptEvent.NodeInfo.NumChannels, ) } contextLogger := log.WithFields(log.Fields{ "event": "channel_request", - "amount": req.FundingAmt, - "alias": alias, - "pubkey": hex.EncodeToString(req.NodePubkey), - "pending_chan_id": hex.EncodeToString(req.PendingChanId), - "total_capacity": info.TotalCapacity, - "num_channels": info.NumChannels, + "amount": channelAcceptEvent.Event.FundingAmt, + "alias": channelAcceptEvent.AliasFrom, + "pubkey": hex.EncodeToString(channelAcceptEvent.Event.NodePubkey), + "pending_chan_id": hex.EncodeToString(channelAcceptEvent.Event.PendingChanId), + "total_capacity": channelAcceptEvent.NodeInfo.TotalCapacity, + "num_channels": channelAcceptEvent.NodeInfo.NumChannels, }) // make decision decision_chan := make(chan bool, 1) - rules_decision, err := rules.Apply(req, decision_chan) + rules_decision, err := rules.Apply(channelAcceptEvent, decision_chan) if err != nil { return err } diff --git a/htlcInterceptor.go b/htlcInterceptor.go index e45482f..6f805f3 100644 --- a/htlcInterceptor.go +++ b/htlcInterceptor.go @@ -9,20 +9,14 @@ import ( "sync" "github.com/callebtc/electronwall/rules" + "github.com/callebtc/electronwall/types" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" log "github.com/sirupsen/logrus" ) -type HtlcForwardEvent struct { - pubkeyFrom string - aliasFrom string - pubkeyTo string - aliasTo string -} - // getHtlcForwardEvent returns a struct containing relevant information about the current // forward request that the decision engine can then use -func (app *App) getHtlcForwardEvent(ctx context.Context, event *routerrpc.ForwardHtlcInterceptRequest) (HtlcForwardEvent, error) { +func (app *App) getHtlcForwardEvent(ctx context.Context, event *routerrpc.ForwardHtlcInterceptRequest) (types.HtlcForwardEvent, error) { channelEdge, err := app.lnd.getPubKeyFromChannel(ctx, event.IncomingCircuitKey.ChanId) if err != nil { log.Errorf("[forward] Error getting pubkey for channel %s", ParseChannelID(event.IncomingCircuitKey.ChanId)) @@ -55,11 +49,12 @@ func (app *App) getHtlcForwardEvent(ctx context.Context, event *routerrpc.Forwar log.Errorf("[forward] Error getting alias for node %s", aliasTo) } - return HtlcForwardEvent{ - pubkeyFrom: pubkeyFrom, - aliasFrom: aliasFrom, - pubkeyTo: pubkeyTo, - aliasTo: aliasTo, + return types.HtlcForwardEvent{ + PubkeyFrom: pubkeyFrom, + AliasFrom: aliasFrom, + PubkeyTo: pubkeyTo, + AliasTo: aliasTo, + Event: event, }, nil } @@ -108,8 +103,8 @@ func (app *App) interceptHtlcEvents(ctx context.Context) error { forward_info_string := fmt.Sprintf( "from %s to %s (%d sat, chan_id:%s->%s, htlc_id:%d)", - htlcForwardEvent.aliasFrom, - htlcForwardEvent.aliasTo, + htlcForwardEvent.AliasFrom, + htlcForwardEvent.AliasTo, event.IncomingAmountMsat/1000, ParseChannelID(event.IncomingCircuitKey.ChanId), ParseChannelID(event.OutgoingRequestedChanId), @@ -118,8 +113,8 @@ func (app *App) interceptHtlcEvents(ctx context.Context) error { contextLogger := log.WithFields(log.Fields{ "event": "forward_request", - "in_alias": htlcForwardEvent.aliasFrom, - "out_alias": htlcForwardEvent.aliasTo, + "in_alias": htlcForwardEvent.AliasFrom, + "out_alias": htlcForwardEvent.AliasTo, "amount": event.IncomingAmountMsat / 1000, "in_chan_id": ParseChannelID(event.IncomingCircuitKey.ChanId), "out_chan_id": ParseChannelID(event.OutgoingRequestedChanId), @@ -133,8 +128,9 @@ func (app *App) interceptHtlcEvents(ctx context.Context) error { if err != nil { return } - rules_decision, err := rules.Apply(event, decision_chan) + rules_decision, err := rules.Apply(htlcForwardEvent, decision_chan) if err != nil { + fmt.Printf("script error: %v", err) return } diff --git a/rules/ChannelAccept.js b/rules/ChannelAccept.js new file mode 100644 index 0000000..3b5e594 --- /dev/null +++ b/rules/ChannelAccept.js @@ -0,0 +1,2 @@ +ChannelAccept.Event.FundingAmt >= 750000; + diff --git a/rules/ChannelAcceptRequest.js b/rules/ChannelAcceptRequest.js deleted file mode 100644 index fbbcdb4..0000000 --- a/rules/ChannelAcceptRequest.js +++ /dev/null @@ -1,2 +0,0 @@ -ChannelAcceptRequest.FundingAmt >= 750000; - diff --git a/rules/ForwardHtlcInterceptRequest.js b/rules/ForwardHtlcInterceptRequest.js deleted file mode 100644 index 1b9dcfd..0000000 --- a/rules/ForwardHtlcInterceptRequest.js +++ /dev/null @@ -1,2 +0,0 @@ -ForwardHtlcInterceptRequest.OutgoingAmountMsat >= 100000 - diff --git a/rules/HtlcForward.js b/rules/HtlcForward.js new file mode 100644 index 0000000..efa69bd --- /dev/null +++ b/rules/HtlcForward.js @@ -0,0 +1,2 @@ +HtlcForward.Event.OutgoingAmountMsat >= 100000 + diff --git a/rules/rules.go b/rules/rules.go index 7faa209..ba0f086 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -5,31 +5,35 @@ import ( "log" "os" + "github.com/callebtc/electronwall/types" "github.com/dop251/goja" - "github.com/lightningnetwork/lnd/lnrpc" - "github.com/lightningnetwork/lnd/lnrpc/routerrpc" ) func Apply(s interface{}, decision_chan chan bool) (accept bool, err error) { vm := goja.New() + var js_script []byte // load script according to event type switch s.(type) { - case lnrpc.ChannelAcceptRequest: - vm.Set("ChannelAcceptRequest", s) - js_script, err = os.ReadFile("rules/ChannelAcceptRequest.js") + case types.HtlcForwardEvent: + vm.Set("HtlcForward", s) + js_script, err = os.ReadFile("rules/HtlcForward.js") if err != nil { log.Fatal(err) } - fmt.Println(string(js_script)) - case *routerrpc.ForwardHtlcInterceptRequest: - vm.Set("ForwardHtlcInterceptRequest", s) - js_script, err = os.ReadFile("rules/ForwardHtlcInterceptRequest.js") + case types.ChannelAcceptEvent: + vm.Set("ChannelAccept", s) + js_script, err = os.ReadFile("rules/ChannelAccept.js") if err != nil { log.Fatal(err) } - fmt.Println(string(js_script)) + // case *routerrpc.ForwardHtlcInterceptRequest: + // vm.Set("HtlcForwardEvent", s) + // js_script, err = os.ReadFile("rules/ForwardHtlcInterceptRequest.js") + // if err != nil { + // log.Fatal(err) + // } default: return false, fmt.Errorf("no rule found for event type") }