multi: make OpenChannel take in openChanReq

This commit is contained in:
Wilmer Paulino committed 2018-08-16 20:19:26 -07:00
1 parent d76f97d497
commit 5c414bb1d0
3 files changed
+59 -68

No files matched your search

+13 -3
View File
@@ -37,10 +37,20 @@ func (c *chanController) OpenChannel(target *btcec.PublicKey,
// TODO(halseth): make configurable? // TODO(halseth): make configurable?
minHtlc := lnwire.NewMSatFromSatoshis(1) minHtlc := lnwire.NewMSatFromSatoshis(1)
updateStream, errChan := c.server.OpenChannel( // Construct the open channel request and send it to the server to begin
target, amt, 0, minHtlc, feePerKw, c.private, 0, // the funding workflow.
) req := &openChanReq{
targetPubkey: target,
chainHash: *activeNetParams.GenesisHash,
localFundingAmt: amt,
pushAmt: 0,
minHtlc: minHtlc,
fundingFeePerKw: feePerKw,
private: c.private,
remoteCsvDelay: 0,
}
updateStream, errChan := c.server.OpenChannel(req)
select { select {
case err := <-errChan: case err := <-errChan:
// If we were not able to actually open a channel to the peer // If we were not able to actually open a channel to the peer
+23 -10
View File
@@ -813,11 +813,18 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest,
// Instruct the server to trigger the necessary events to attempt to // Instruct the server to trigger the necessary events to attempt to
// open a new channel. A stream is returned in place, this stream will // open a new channel. A stream is returned in place, this stream will
// be used to consume updates of the state of the pending channel. // be used to consume updates of the state of the pending channel.
updateChan, errChan := r.server.OpenChannel( req := &openChanReq{
nodePubKey, localFundingAmt, targetPubkey: nodePubKey,
lnwire.NewMSatFromSatoshis(remoteInitialBalance), chainHash: *activeNetParams.GenesisHash,
minHtlc, feeRate, in.Private, remoteCsvDelay, localFundingAmt: localFundingAmt,
) pushAmt: lnwire.NewMSatFromSatoshis(remoteInitialBalance),
minHtlc: minHtlc,
fundingFeePerKw: feeRate,
private: in.Private,
remoteCsvDelay: remoteCsvDelay,
}
updateChan, errChan := r.server.OpenChannel(req)
var outpoint wire.OutPoint var outpoint wire.OutPoint
out: out:
@@ -941,12 +948,18 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context,
rpcsLog.Tracef("[openchannel] target sat/kw for funding tx: %v", rpcsLog.Tracef("[openchannel] target sat/kw for funding tx: %v",
int64(feeRate)) int64(feeRate))
updateChan, errChan := r.server.OpenChannel( req := &openChanReq{
nodepubKey, localFundingAmt, targetPubkey: nodepubKey,
lnwire.NewMSatFromSatoshis(remoteInitialBalance), chainHash: *activeNetParams.GenesisHash,
minHtlc, feeRate, in.Private, remoteCsvDelay, localFundingAmt: localFundingAmt,
) pushAmt: lnwire.NewMSatFromSatoshis(remoteInitialBalance),
minHtlc: minHtlc,
fundingFeePerKw: feeRate,
private: in.Private,
remoteCsvDelay: remoteCsvDelay,
}
updateChan, errChan := r.server.OpenChannel(req)
select { select {
// If an error occurs them immediately return the error to the client. // If an error occurs them immediately return the error to the client.
case err := <-errChan: case err := <-errChan:
+23 -55
View File
@@ -2735,77 +2735,45 @@ func (s *server) DisconnectPeer(pubKey *btcec.PublicKey) error {
// peer identified by nodeKey with the passed channel funding parameters. // peer identified by nodeKey with the passed channel funding parameters.
// //
// NOTE: This function is safe for concurrent access. // NOTE: This function is safe for concurrent access.
func (s *server) OpenChannel(nodeKey *btcec.PublicKey, func (s *server) OpenChannel(
localAmt btcutil.Amount, pushAmt, minHtlc lnwire.MilliSatoshi, req *openChanReq) (chan *lnrpc.OpenStatusUpdate, chan error) {
fundingFeePerKw lnwallet.SatPerKWeight, private bool,
remoteCsvDelay uint16) (chan *lnrpc.OpenStatusUpdate, chan error) {
// The updateChan will have a buffer of 2, since we expect a // The updateChan will have a buffer of 2, since we expect a ChanPending
// ChanPending + a ChanOpen update, and we want to make sure the // + a ChanOpen update, and we want to make sure the funding process is
// funding process is not blocked if the caller is not reading the // not blocked if the caller is not reading the updates.
// updates. req.updates = make(chan *lnrpc.OpenStatusUpdate, 2)
updateChan := make(chan *lnrpc.OpenStatusUpdate, 2) req.err = make(chan error, 1)
errChan := make(chan error, 1)
var (
targetPeer *peer
pubKeyBytes []byte
err error
)
// If the user is targeting the peer by public key, then we'll need to
// convert that into a string for our map. Otherwise, we expect them to
// target by peer ID instead.
if nodeKey != nil {
pubKeyBytes = nodeKey.SerializeCompressed()
}
// First attempt to locate the target peer to open a channel with, if // First attempt to locate the target peer to open a channel with, if
// we're unable to locate the peer then this request will fail. // we're unable to locate the peer then this request will fail.
pubKeyBytes := req.targetPubkey.SerializeCompressed()
s.mu.RLock() s.mu.RLock()
if peer, ok := s.peersByPub[string(pubKeyBytes)]; ok { peer, ok := s.peersByPub[string(pubKeyBytes)]
targetPeer = peer if !ok {
req.err <- fmt.Errorf("peer %x is not online", pubKeyBytes)
return req.updates, req.err
} }
s.mu.RUnlock() s.mu.RUnlock()
if targetPeer == nil {
errChan <- fmt.Errorf("peer is not connected NodeKey(%x)", pubKeyBytes)
return updateChan, errChan
}
// If the fee rate wasn't specified, then we'll use a default // If the fee rate wasn't specified, then we'll use a default
// confirmation target. // confirmation target.
if fundingFeePerKw == 0 { if req.fundingFeePerKw == 0 {
estimator := s.cc.feeEstimator estimator := s.cc.feeEstimator
fundingFeePerKw, err = estimator.EstimateFeePerKW(6) feeRate, err := estimator.EstimateFeePerKW(6)
if err != nil { if err != nil {
errChan <- err req.err <- err
return updateChan, errChan return req.updates, req.err
} }
req.fundingFeePerKw = feeRate
} }
// Spawn a goroutine to send the funding workflow request to the // Spawn a goroutine to send the funding workflow request to the funding
// funding manager. This allows the server to continue handling queries // manager. This allows the server to continue handling queries instead
// instead of blocking on this request which is exported as a // of blocking on this request which is exported as a synchronous
// synchronous request to the outside world. // request to the outside world.
req := &openChanReq{ go s.fundingMgr.initFundingWorkflow(peer, req)
targetPubkey: nodeKey,
chainHash: *activeNetParams.GenesisHash,
localFundingAmt: localAmt,
fundingFeePerKw: fundingFeePerKw,
pushAmt: pushAmt,
private: private,
minHtlc: minHtlc,
remoteCsvDelay: remoteCsvDelay,
updates: updateChan,
err: errChan,
}
// TODO(roasbeef): pass in chan that's closed if/when funding succeeds return req.updates, req.err
// so can track as persistent peer?
go s.fundingMgr.initFundingWorkflow(targetPeer, req)
return updateChan, errChan
} }
// Peers returns a slice of all active peers. // Peers returns a slice of all active peers.