htlcswitch: remove lnrpc dependency

This commits removes an unwanted dependency that prevents
moving parts of rpcserver to a sub server.
This commit is contained in:
Joost Jager 2018-12-20 18:52:27 +01:00
parent 237f2b6d4b
commit 6c027e2bc9
4 changed files with 68 additions and 41 deletions

@ -17,7 +17,6 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/ticker" "github.com/lightningnetwork/lnd/ticker"
@ -119,7 +118,7 @@ type ChanClose struct {
// Updates is used by request creator to receive the notifications about // Updates is used by request creator to receive the notifications about
// execution of the close channel request. // execution of the close channel request.
Updates chan *lnrpc.CloseStatusUpdate Updates chan interface{}
// Err is used by request creator to receive request execution error. // Err is used by request creator to receive request execution error.
Err chan error Err chan error
@ -1415,11 +1414,11 @@ func (s *Switch) teardownCircuit(pkt *htlcPacket) error {
// then the last parameter should be the ideal fee-per-kw that will be used as // then the last parameter should be the ideal fee-per-kw that will be used as
// a starting point for close negotiation. // a starting point for close negotiation.
func (s *Switch) CloseLink(chanPoint *wire.OutPoint, closeType ChannelCloseType, func (s *Switch) CloseLink(chanPoint *wire.OutPoint, closeType ChannelCloseType,
targetFeePerKw lnwallet.SatPerKWeight) (chan *lnrpc.CloseStatusUpdate, targetFeePerKw lnwallet.SatPerKWeight) (chan interface{},
chan error) { chan error) {
// TODO(roasbeef) abstract out the close updates. // TODO(roasbeef) abstract out the close updates.
updateChan := make(chan *lnrpc.CloseStatusUpdate, 2) updateChan := make(chan interface{}, 2)
errChan := make(chan error, 1) errChan := make(chan error, 1)
command := &ChanClose{ command := &ChanClose{

31
peer.go

@ -23,7 +23,6 @@ import (
"github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/ticker" "github.com/lightningnetwork/lnd/ticker"
@ -85,6 +84,18 @@ type chanSnapshotReq struct {
resp chan []*channeldb.ChannelSnapshot resp chan []*channeldb.ChannelSnapshot
} }
// pendingUpdate describes the pending state of a closing channel.
type pendingUpdate struct {
Txid []byte
OutputIndex uint32
}
// channelCloseUpdate contains the outcome of the close channel operation.
type channelCloseUpdate struct {
ClosingTxid []byte
Success bool
}
// peer is an active peer on the Lightning Network. This struct is responsible // peer is an active peer on the Lightning Network. This struct is responsible
// for managing any channel state related to this peer. To do so, it has // for managing any channel state related to this peer. To do so, it has
// several helper goroutines to handle events such as HTLC timeouts, new // several helper goroutines to handle events such as HTLC timeouts, new
@ -2037,12 +2048,8 @@ func (p *peer) finalizeChanClosure(chanCloser *channelCloser) {
// If this is a locally requested shutdown, update the caller with a // If this is a locally requested shutdown, update the caller with a
// new event detailing the current pending state of this request. // new event detailing the current pending state of this request.
if closeReq != nil { if closeReq != nil {
closeReq.Updates <- &lnrpc.CloseStatusUpdate{ closeReq.Updates <- &pendingUpdate{
Update: &lnrpc.CloseStatusUpdate_ClosePending{ Txid: closingTxid[:],
ClosePending: &lnrpc.PendingUpdate{
Txid: closingTxid[:],
},
},
} }
} }
@ -2052,13 +2059,9 @@ func (p *peer) finalizeChanClosure(chanCloser *channelCloser) {
// Respond to the local subsystem which requested the // Respond to the local subsystem which requested the
// channel closure. // channel closure.
if closeReq != nil { if closeReq != nil {
closeReq.Updates <- &lnrpc.CloseStatusUpdate{ closeReq.Updates <- &channelCloseUpdate{
Update: &lnrpc.CloseStatusUpdate_ChanClose{ ClosingTxid: closingTxid[:],
ChanClose: &lnrpc.ChannelCloseUpdate{ Success: true,
ClosingTxid: closingTxid[:],
Success: true,
},
},
} }
} }
}) })

@ -10,7 +10,6 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@ -121,7 +120,7 @@ func TestPeerChannelClosureAcceptFeeInitiator(t *testing.T) {
defer cleanUp() defer cleanUp()
// We make the initiator send a shutdown request. // We make the initiator send a shutdown request.
updateChan := make(chan *lnrpc.CloseStatusUpdate, 1) updateChan := make(chan interface{}, 1)
errChan := make(chan error, 1) errChan := make(chan error, 1)
closeCommand := &htlcswitch.ChanClose{ closeCommand := &htlcswitch.ChanClose{
CloseType: htlcswitch.CloseRegular, CloseType: htlcswitch.CloseRegular,
@ -410,7 +409,7 @@ func TestPeerChannelClosureFeeNegotiationsInitiator(t *testing.T) {
defer cleanUp() defer cleanUp()
// We make the initiator send a shutdown request. // We make the initiator send a shutdown request.
updateChan := make(chan *lnrpc.CloseStatusUpdate, 1) updateChan := make(chan interface{}, 1)
errChan := make(chan error, 1) errChan := make(chan error, 1)
closeCommand := &htlcswitch.ChanClose{ closeCommand := &htlcswitch.ChanClose{
CloseType: htlcswitch.CloseRegular, CloseType: htlcswitch.CloseRegular,

@ -1419,7 +1419,7 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
chanPoint, force) chanPoint, force)
var ( var (
updateChan chan *lnrpc.CloseStatusUpdate updateChan chan interface{}
errChan chan error errChan chan error
) )
@ -1472,13 +1472,9 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
// With the transaction broadcast, we send our first update to // With the transaction broadcast, we send our first update to
// the client. // the client.
updateChan = make(chan *lnrpc.CloseStatusUpdate, 2) updateChan = make(chan interface{}, 2)
updateChan <- &lnrpc.CloseStatusUpdate{ updateChan <- &pendingUpdate{
Update: &lnrpc.CloseStatusUpdate_ClosePending{ Txid: closingTxid[:],
ClosePending: &lnrpc.PendingUpdate{
Txid: closingTxid[:],
},
},
} }
errChan = make(chan error, 1) errChan = make(chan error, 1)
@ -1487,13 +1483,9 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
&closingTxid, closingTx.TxOut[0].PkScript, func() { &closingTxid, closingTx.TxOut[0].PkScript, func() {
// Respond to the local subsystem which // Respond to the local subsystem which
// requested the channel closure. // requested the channel closure.
updateChan <- &lnrpc.CloseStatusUpdate{ updateChan <- &channelCloseUpdate{
Update: &lnrpc.CloseStatusUpdate_ChanClose{ ClosingTxid: closingTxid[:],
ChanClose: &lnrpc.ChannelCloseUpdate{ Success: true,
ClosingTxid: closingTxid[:],
Success: true,
},
},
} }
}) })
} else { } else {
@ -1544,18 +1536,26 @@ out:
"ChannelPoint(%v): %v", chanPoint, err) "ChannelPoint(%v): %v", chanPoint, err)
return err return err
case closingUpdate := <-updateChan: case closingUpdate := <-updateChan:
rpcClosingUpdate, err := createRPCCloseUpdate(
closingUpdate,
)
if err != nil {
return err
}
rpcsLog.Tracef("[closechannel] sending update: %v", rpcsLog.Tracef("[closechannel] sending update: %v",
closingUpdate) rpcClosingUpdate)
if err := updateStream.Send(closingUpdate); err != nil {
if err := updateStream.Send(rpcClosingUpdate); err != nil {
return err return err
} }
// If a final channel closing updates is being sent, // If a final channel closing updates is being sent,
// then we can break out of our dispatch loop as we no // then we can break out of our dispatch loop as we no
// longer need to process any further updates. // longer need to process any further updates.
switch closeUpdate := closingUpdate.Update.(type) { switch closeUpdate := closingUpdate.(type) {
case *lnrpc.CloseStatusUpdate_ChanClose: case *channelCloseUpdate:
h, _ := chainhash.NewHash(closeUpdate.ChanClose.ClosingTxid) h, _ := chainhash.NewHash(closeUpdate.ClosingTxid)
rpcsLog.Infof("[closechannel] close completed: "+ rpcsLog.Infof("[closechannel] close completed: "+
"txid(%v)", h) "txid(%v)", h)
break out break out
@ -1568,6 +1568,32 @@ out:
return nil return nil
} }
func createRPCCloseUpdate(update interface{}) (
*lnrpc.CloseStatusUpdate, error) {
switch u := update.(type) {
case *channelCloseUpdate:
return &lnrpc.CloseStatusUpdate{
Update: &lnrpc.CloseStatusUpdate_ChanClose{
ChanClose: &lnrpc.ChannelCloseUpdate{
ClosingTxid: u.ClosingTxid,
},
},
}, nil
case *pendingUpdate:
return &lnrpc.CloseStatusUpdate{
Update: &lnrpc.CloseStatusUpdate_ClosePending{
ClosePending: &lnrpc.PendingUpdate{
Txid: u.Txid,
OutputIndex: u.OutputIndex,
},
},
}, nil
}
return nil, errors.New("unknown close status update")
}
// AbandonChannel removes all channel state from the database except for a // AbandonChannel removes all channel state from the database except for a
// close summary. This method can be used to get rid of permanently unusable // close summary. This method can be used to get rid of permanently unusable
// channels due to bugs fixed in newer versions of lnd. // channels due to bugs fixed in newer versions of lnd.