Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Aug 24, 2022
1 parent c4b28a3 commit 5b1cc0f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 64 deletions.
77 changes: 45 additions & 32 deletions channelAcceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,45 @@ 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)
}
}()

// the channel event interceptor
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()
Expand All @@ -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
}
Expand Down
32 changes: 14 additions & 18 deletions htlcInterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions rules/ChannelAccept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ChannelAccept.Event.FundingAmt >= 750000;

2 changes: 0 additions & 2 deletions rules/ChannelAcceptRequest.js

This file was deleted.

2 changes: 0 additions & 2 deletions rules/ForwardHtlcInterceptRequest.js

This file was deleted.

2 changes: 2 additions & 0 deletions rules/HtlcForward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HtlcForward.Event.OutgoingAmountMsat >= 100000

24 changes: 14 additions & 10 deletions rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 5b1cc0f

Please sign in to comment.