mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
lnd: implement bi-directional streaming SendPayment in rpcserver
This commit is contained in:
parent
88949e181a
commit
91509681df
52
rpcserver.go
52
rpcserver.go
@ -3,12 +3,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/lightningnetwork/lnd/lndc"
|
"github.com/lightningnetwork/lnd/lndc"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
"github.com/roasbeef/btcd/wire"
|
"github.com/roasbeef/btcd/wire"
|
||||||
"github.com/roasbeef/btcutil"
|
"github.com/roasbeef/btcutil"
|
||||||
@ -418,3 +420,53 @@ func (r *rpcServer) PendingChannels(ctx context.Context,
|
|||||||
PendingChannels: pendingChannels,
|
PendingChannels: pendingChannels,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendPayment dispatches a bi-directional streaming RPC for sending payments
|
||||||
|
// through the Lightning Network. A single RPC invocation creates a persistent
|
||||||
|
// bi-directional stream allowing clients to rapidly send payments through the
|
||||||
|
// Lightning Network with a single persistent connection.
|
||||||
|
func (r *rpcServer) SendPayment(paymentStream lnrpc.Lightning_SendPaymentServer) error {
|
||||||
|
for {
|
||||||
|
// Receive the next pending payment within the stream sent by
|
||||||
|
// the client. If we read the EOF sentinel, then the client has
|
||||||
|
// closed the stream, and we can exit normally.
|
||||||
|
nextPayment, err := paymentStream.Recv()
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Craft an HTLC packet to send to the routing sub-system. The
|
||||||
|
// meta-data within this packet will be used to route the
|
||||||
|
// payment through the network.
|
||||||
|
htlcAdd := &lnwire.HTLCAddRequest{
|
||||||
|
Amount: lnwire.CreditsAmount(nextPayment.Amt),
|
||||||
|
RedemptionHashes: [][32]byte{debugHash},
|
||||||
|
}
|
||||||
|
destAddr, err := wire.NewShaHash(nextPayment.Dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
htlcPkt := &htlcPacket{
|
||||||
|
dest: *destAddr,
|
||||||
|
msg: htlcAdd,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, send this next packet to the routing layer in order
|
||||||
|
// to complete the next payment.
|
||||||
|
// TODO(roasbeef): this should go through the L3 router once
|
||||||
|
// multi-hop is in place.
|
||||||
|
if err := r.server.htlcSwitch.SendHTLC(htlcPkt); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(roasbeef): proper responses
|
||||||
|
resp := &lnrpc.SendResponse{}
|
||||||
|
if err := paymentStream.Send(resp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user