lnd: implement open+close channel workflow in daemon
This commit adds the necessary plumbing within the server, peer, and rpcServer to handle opening and cooperatively closing a channel with a remote peer. Many new data structures have been added to the peer in order to allow it to efficiently manage opening+.losing new/existing lightning channels. Additional documentation has been added to several methods within the peer struct, with some minor renaming along with way. The peer has also gained a dedicated goroutine whose job it is to manage any requests pertaining to opening, or closing any channels with the remote peer. The messages have been added to lnrpc define the requests and responses to channel open+close messages. Additional channel logic has been added between the rpcServer, peer, and server in order to properly manage the necessary synchronization.
This commit is contained in:
6 files changed
+633
-67
No files matched your search
+53
-3
@@ -149,16 +149,66 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
|
||||
rpcsLog.Debugf("Connected to peer: %v", peerAddr.String())
|
||||
return &lnrpc.ConnectPeerResponse{peerID}, nil
|
||||
}
|
||||
|
||||
// OpenChannel attempts to open a singly funded channel specified in the
|
||||
// request to a remote peer.
|
||||
func (r *rpcServer) OpenChannel(ctx context.Context,
|
||||
in *lnrpc.OpenChannelRequest) (*lnrpc.OpenChannelResponse, error) {
|
||||
|
||||
rpcsLog.Tracef("Recieved request to openchannel to peerid(%v) "+
|
||||
"allocation(us=%v, them=%v) numconfs=%v", in.TargetPeerId,
|
||||
in.LocalFundingAmount, in.RemoteFundingAmount, in.NumConfs)
|
||||
|
||||
localFundingAmt := btcutil.Amount(in.LocalFundingAmount)
|
||||
remoteFundingAmt := btcutil.Amount(in.RemoteFundingAmount)
|
||||
target := in.TargetPeerId
|
||||
numConfs := in.NumConfs
|
||||
resp, err := r.server.OpenChannel(target, localFundingAmt,
|
||||
remoteFundingAmt, numConfs)
|
||||
if err != nil {
|
||||
rpcsLog.Errorf("unable to open channel to peerid(%v): %v",
|
||||
target, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := r.server.ConnectToPeer(peerAddr); err != nil {
|
||||
rpcsLog.Tracef("Opened channel with peerid(%v), fundingtxid %v",
|
||||
in.TargetPeerId, resp)
|
||||
|
||||
return &lnrpc.OpenChannelResponse{
|
||||
&lnrpc.ChannelPoint{
|
||||
FundingTxid: resp.Hash[:],
|
||||
OutputIndex: resp.Index,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CloseChannel attempts to close an active channel identified by its channel
|
||||
// point. The actions of this method can additionally be augmented to attempt
|
||||
// a force close after a timeout period in the case of an inactive peer.
|
||||
func (r *rpcServer) CloseChannel(ctx context.Context,
|
||||
in *lnrpc.CloseChannelRequest) (*lnrpc.CloseChannelResponse, error) {
|
||||
|
||||
index := in.ChannelPoint.OutputIndex
|
||||
txid, err := wire.NewShaHash(in.ChannelPoint.FundingTxid)
|
||||
if err != nil {
|
||||
rpcsLog.Errorf("(closechannel) invalid txid: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
targetChannelPoint := wire.NewOutPoint(txid, index)
|
||||
|
||||
rpcsLog.Tracef("Recieved closechannel request for ChannelPoint(%v)",
|
||||
targetChannelPoint)
|
||||
|
||||
resp, err := r.server.CloseChannel(targetChannelPoint)
|
||||
if err != nil {
|
||||
rpcsLog.Errorf("Unable to close ChannelPoint(%v): %v",
|
||||
targetChannelPoint, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rpcsLog.Infof("Connected to peer: %v", peerAddr.String())
|
||||
return &lnrpc.ConnectPeerResponse{[]byte(peerAddr.String())}, nil
|
||||
return &lnrpc.CloseChannelResponse{resp}, nil
|
||||
}
|
||||
|
||||
// ListPeers returns a verbose listing of all currently active peers.
|
||||
func (r *rpcServer) ListPeers(ctx context.Context,
|
||||
in *lnrpc.ListPeersRequest) (*lnrpc.ListPeersResponse, error) {
|
||||
|
||||
Reference in new issue
Block a user