Skip to content

Commit

Permalink
lnrpc/peers: skeleton logic for updateNodeAnnouncement
Browse files Browse the repository at this point in the history
Basic logic for the endpoint:

- Get the current nodeAnn information
- Calculate modifications
- Apply modifications
- Return changes
  • Loading branch information
positiveblue committed Apr 2, 2022
1 parent 5ab0cbd commit e4e0935
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 8 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ run:
- chainrpc
- dev
- invoicesrpc
- peersrpc
- signrpc
- walletrpc
- watchtowerrpc
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/peersrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lnwallet"
Expand Down Expand Up @@ -516,6 +517,7 @@ func DefaultConfig() Config {
SubRPCServers: &subRPCServerConfigs{
SignRPC: &signrpc.Config{},
RouterRPC: routerrpc.DefaultConfig(),
PeersRPC: &peersrpc.Config{},
},
Autopilot: &lncfg.AutoPilot{
MaxChannels: 5,
Expand Down
2 changes: 1 addition & 1 deletion dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COPY . /go/src/github.com/lightningnetwork/lnd

RUN cd /go/src/github.com/lightningnetwork/lnd \
&& make \
&& make install tags="signrpc walletrpc chainrpc invoicesrpc"
&& make install tags="signrpc walletrpc chainrpc invoicesrpc peersrpc"

# Start a new, final image to reduce size.
FROM alpine as final
Expand Down
25 changes: 24 additions & 1 deletion lnrpc/peersrpc/config_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,27 @@

package peersrpc

type Config struct{}
import (
"net"

"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/netann"
)

// Config is the primary configuration struct for the peers RPC subserver.
// It contains all the items required for the server to carry out its duties.
// The fields with struct tags are meant to be parsed as normal configuration
// options, while if able to be populated, the latter fields MUST also be
// specified.
type Config struct {
// GetNodeAnnouncement is used to send our retrieve the current
// node announcement information.
GetNodeAnnouncement func() (lnwire.NodeAnnouncement, error)

// ParseAddr parses an address from its string format to a net.Addr.
ParseAddr func(addr string) (net.Addr, error)

// UpdateNodeAnnouncement updates our node announcement applying the
// given NodeAnnModifiers and broadcasts the new version to the network.
UpdateNodeAnnouncement func(...netann.NodeAnnModifier) error
}
167 changes: 167 additions & 0 deletions lnrpc/peersrpc/peers.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion lnrpc/peersrpc/peers.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,41 @@
"produces": [
"application/json"
],
"paths": {},
"paths": {
"/v2/peers/nodeannouncement": {
"post": {
"summary": "lncli: peers updatenodeannouncement\nUpdateNodeAnnouncement allows the caller to update the node parameters\nand broadcasts a new version of the node announcement to its peers.",
"operationId": "Peers_UpdateNodeAnnouncement",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/peersrpcNodeAnnouncementUpdateResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/peersrpcNodeAnnouncementUpdateRequest"
}
}
],
"tags": [
"Peers"
]
}
}
},
"definitions": {
"lnrpcFeatureBit": {
"type": "string",
Expand Down Expand Up @@ -62,6 +96,33 @@
}
}
},
"peersrpcNodeAnnouncementUpdateRequest": {
"type": "object",
"properties": {
"feature_updates": {
"type": "array",
"items": {
"$ref": "#/definitions/peersrpcUpdateFeatureAction"
},
"description": "Set of changes for the features that the node supports."
},
"color": {
"type": "string",
"description": "Color is the node's color in hex code format."
},
"alias": {
"type": "string",
"description": "Alias or nick name of the node."
},
"address_updates": {
"type": "array",
"items": {
"$ref": "#/definitions/peersrpcUpdateAddressAction"
},
"description": "Set of changes for the node's known addresses."
}
}
},
"peersrpcNodeAnnouncementUpdateResponse": {
"type": "object",
"properties": {
Expand Down
6 changes: 6 additions & 0 deletions lnrpc/peersrpc/peers.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
type: google.api.Service
config_version: 3

http:
rules:
- selector: peersrpc.Peers.UpdateNodeAnnouncement
post: "/v2/peers/nodeannouncement"
body: "*"
55 changes: 54 additions & 1 deletion lnrpc/peersrpc/peers_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package peersrpc

import (
"context"
"fmt"
"sync/atomic"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/netann"
"google.golang.org/grpc"
"gopkg.in/macaroon-bakery.v2/bakery"
)
Expand All @@ -23,7 +25,12 @@ const (

var (
// macPermissions maps RPC calls to the permissions they require.
macPermissions = map[string][]bakery.Op{}
macPermissions = map[string][]bakery.Op{
"/peersrpc.Peers/UpdateNodeAnnouncement": {{
Entity: "peers",
Action: "write",
}},
}
)

// ServerShell is a shell struct holding a reference to the actual sub-server.
Expand Down Expand Up @@ -119,6 +126,17 @@ func (r *ServerShell) RegisterWithRootServer(grpcServer *grpc.Server) error {
func (r *ServerShell) RegisterWithRestServer(ctx context.Context,
mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {

// We make sure that we register it with the main REST server to ensure
// all our methods are routed properly.
err := RegisterPeersHandlerFromEndpoint(ctx, mux, dest, opts)
if err != nil {
log.Errorf("Could not register Peers REST server "+
"with root REST server: %v", err)
return err
}

log.Debugf("Peers REST server successfully registered with " +
"root REST server")
return nil
}

Expand All @@ -140,3 +158,38 @@ func (r *ServerShell) CreateSubServer(configRegistry lnrpc.SubServerConfigDispat
r.PeersServer = subServer
return subServer, macPermissions, nil
}

// UpdateNodeAnnouncement allows the caller to update the node parameters
// and broadcasts a new version of the node announcement to its peers.
func (s *Server) UpdateNodeAnnouncement(_ context.Context,
req *NodeAnnouncementUpdateRequest) (
*NodeAnnouncementUpdateResponse, error) {

resp := &NodeAnnouncementUpdateResponse{}
nodeModifiers := make([]netann.NodeAnnModifier, 0)

_, err := s.cfg.GetNodeAnnouncement()
if err != nil {
return nil, fmt.Errorf("unable to get current node "+
"announcement: %v", err)
}

// TODO(positiveblue): apply feature bit modifications

// TODO(positiveblue): apply color modifications

// TODO(positiveblue): apply alias modifications

// TODO(positiveblue): apply addresses modifications

if len(nodeModifiers) == 0 {
return nil, fmt.Errorf("unable detect any new values to " +
"update the node announcement")
}

if err := s.cfg.UpdateNodeAnnouncement(nodeModifiers...); err != nil {
return nil, err
}

return resp, nil
}
Loading

0 comments on commit e4e0935

Please sign in to comment.