Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set response fields when evaluating the policy #10

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

Check warning on line 1 in main.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

import (
"context"
Expand Down Expand Up @@ -105,7 +105,7 @@

node, err := client.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return resp, errors.Wrap(err, "getting node information")
return resp, errors.New("Internal server error")
}

getPeerInfoReq := &lnrpc.NodeInfoRequest{
Expand All @@ -119,20 +119,9 @@
slog.Debug("Peer node information", slog.Any("node", peer))

for _, policy := range config.Policies {
if err := policy.Evaluate(req, node, peer); err != nil {
if err := policy.Evaluate(req, resp, node, peer); err != nil {
return resp, err
}

if policy.MinAcceptDepth != nil {
resp.MinAcceptDepth = *policy.MinAcceptDepth
}
}

if req.WantsZeroConf && len(config.Policies) != 0 {
// The initiator requested a zero conf channel and it was explicitly accepted, set the
// fields required to open it
resp.ZeroConf = true
resp.MinAcceptDepth = 0
}

return resp, nil
Expand Down
32 changes: 22 additions & 10 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ type Policy struct {
// Evaluate set of policies.
func (p *Policy) Evaluate(
req *lnrpc.ChannelAcceptRequest,
resp *lnrpc.ChannelAcceptResponse,
node *lnrpc.GetInfoResponse,
peer *lnrpc.NodeInfo,
) error {
if p.Conditions != nil && !p.Conditions.Match(req, node, peer) {
return nil
}

if p.MinAcceptDepth != nil {
resp.MinAcceptDepth = *p.MinAcceptDepth
}

if !p.checkRejectAll() {
return errors.New("No new channels are accepted")
}
Expand All @@ -50,7 +55,7 @@ func (p *Policy) Evaluate(
return errors.New("Private channels are not accepted")
}

if !p.checkZeroConf(peer.Node.PubKey, req.WantsZeroConf) {
if !p.checkZeroConf(peer.Node.PubKey, req.WantsZeroConf, resp) {
return errors.New("Zero conf channels are not accepted")
}

Expand Down Expand Up @@ -101,7 +106,11 @@ func (p *Policy) checkPrivate(private bool) bool {
return private && !*p.RejectPrivateChannels
}

func (p *Policy) checkZeroConf(publicKey string, wantsZeroConf bool) bool {
func (p *Policy) checkZeroConf(
publicKey string,
wantsZeroConf bool,
resp *lnrpc.ChannelAcceptResponse,
) bool {
if !wantsZeroConf {
return true
}
Expand All @@ -110,15 +119,18 @@ func (p *Policy) checkZeroConf(publicKey string, wantsZeroConf bool) bool {
return false
}

if p.ZeroConfList != nil {
for _, pubKey := range *p.ZeroConfList {
if publicKey == pubKey {
return true
}
}
resp.ZeroConf = true
resp.MinAcceptDepth = 0

return false
if p.ZeroConfList == nil {
return true
}

return true
for _, pubKey := range *p.ZeroConfList {
if publicKey == pubKey {
return true
}
}

return false
}
39 changes: 37 additions & 2 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestEvaluatePolicy(t *testing.T) {
tru := true
fals := false
max := uint64(1)
depth := uint32(10)

cases := []struct {
policy Policy
Expand Down Expand Up @@ -138,11 +139,20 @@ func TestEvaluatePolicy(t *testing.T) {
},
fail: true,
},
{
desc: "Min accept depth",
policy: Policy{
MinAcceptDepth: &depth,
},
req: defaultReq,
peer: defaultPeer,
fail: false,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
err := tc.policy.Evaluate(tc.req, node, tc.peer)
err := tc.policy.Evaluate(tc.req, &lnrpc.ChannelAcceptResponse{}, node, tc.peer)
if tc.fail {
assert.NotNil(t, err)
} else {
Expand All @@ -152,6 +162,25 @@ func TestEvaluatePolicy(t *testing.T) {
}
}

func TestMinAcceptDepth(t *testing.T) {
n := uint32(2)
policy := Policy{
MinAcceptDepth: &n,
}
resp := &lnrpc.ChannelAcceptResponse{}
node := &lnrpc.NodeInfo{Node: &lnrpc.LightningNode{PubKey: ""}}

err := policy.Evaluate(
&lnrpc.ChannelAcceptRequest{},
resp,
&lnrpc.GetInfoResponse{},
node,
)
assert.NoError(t, err)

assert.Equal(t, n, resp.MinAcceptDepth)
}

func TestCheckRejectAll(t *testing.T) {
cases := []struct {
desc string
Expand Down Expand Up @@ -373,8 +402,14 @@ func TestCheckZeroConf(t *testing.T) {
ZeroConfList: tc.zeroConfList,
}

actual := policy.checkZeroConf(tc.publicKey, tc.wantsZeroConf)
resp := &lnrpc.ChannelAcceptResponse{}
actual := policy.checkZeroConf(tc.publicKey, tc.wantsZeroConf, resp)
assert.Equal(t, tc.expected, actual)

if tc.wantsZeroConf && tc.expected {
assert.True(t, resp.ZeroConf)
assert.Zero(t, resp.MinAcceptDepth)
}
})
}
}
Loading