From 6d01f140d93a12506077909f0d2fbbe032201edb Mon Sep 17 00:00:00 2001 From: Elliott Jin Date: Sat, 13 Feb 2021 00:14:54 -0800 Subject: [PATCH] routerrpc: implement UpdateChanStatus RPC in router server Update UpdateChanStatus stub implementation to call into exposed ChanStatusManager methods in RouterBackend. --- lnrpc/routerrpc/router_server.go | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lnrpc/routerrpc/router_server.go b/lnrpc/routerrpc/router_server.go index c7d1496d..e9233716 100644 --- a/lnrpc/routerrpc/router_server.go +++ b/lnrpc/routerrpc/router_server.go @@ -10,6 +10,7 @@ import ( "sync/atomic" "time" + "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/lightningnetwork/lnd/channeldb" @@ -115,6 +116,10 @@ var ( Entity: "offchain", Action: "write", }}, + "/routerrpc.Router/UpdateChanStatus": {{ + Entity: "offchain", + Action: "write", + }}, } // DefaultRouterMacFilename is the default name of the router macaroon @@ -698,9 +703,43 @@ func (s *Server) HtlcInterceptor(stream Router_HtlcInterceptorServer) error { return newForwardInterceptor(s, stream).run() } +func extractOutPoint(req *UpdateChanStatusRequest) (*wire.OutPoint, error) { + chanPoint := req.GetChanPoint() + txid, err := lnrpc.GetChanPointFundingTxid(chanPoint) + if err != nil { + return nil, err + } + index := chanPoint.OutputIndex + return wire.NewOutPoint(txid, index), nil +} + // UpdateChanStatus allows channel state to be set manually. func (s *Server) UpdateChanStatus(ctx context.Context, req *UpdateChanStatusRequest) (*UpdateChanStatusResponse, error) { - return nil, fmt.Errorf("unimplemented") + outPoint, err := extractOutPoint(req) + if err != nil { + return nil, err + } + + action := req.GetAction() + + log.Debugf("UpdateChanStatus called for channel(%v) with "+ + "action %v", outPoint, action) + + switch action { + case ChanStatusAction_ENABLE: + err = s.cfg.RouterBackend.SetChannelEnabled(*outPoint) + case ChanStatusAction_DISABLE: + err = s.cfg.RouterBackend.SetChannelDisabled(*outPoint) + case ChanStatusAction_AUTO: + err = s.cfg.RouterBackend.SetChannelAuto(*outPoint) + default: + err = fmt.Errorf("unrecognized ChannelStatusAction %v", action) + } + + if err != nil { + return nil, err + } + return &UpdateChanStatusResponse{}, nil }