htlcswitch/test: create preparePayment function
This commit is contained in:
parent
e7907e0e7c
commit
9cac0efab7
@ -686,9 +686,39 @@ func makePayment(sendingPeer, receivingPeer lnpeer.Peer,
|
|||||||
timelock uint32) *paymentResponse {
|
timelock uint32) *paymentResponse {
|
||||||
|
|
||||||
paymentErr := make(chan error, 1)
|
paymentErr := make(chan error, 1)
|
||||||
|
|
||||||
var rhash lntypes.Hash
|
var rhash lntypes.Hash
|
||||||
|
|
||||||
|
invoice, payFunc, err := preparePayment(sendingPeer, receivingPeer,
|
||||||
|
firstHop, hops, invoiceAmt, htlcAmt, timelock,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
paymentErr <- err
|
||||||
|
return &paymentResponse{
|
||||||
|
rhash: rhash,
|
||||||
|
err: paymentErr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rhash = invoice.Terms.PaymentPreimage.Hash()
|
||||||
|
|
||||||
|
// Send payment and expose err channel.
|
||||||
|
go func() {
|
||||||
|
paymentErr <- payFunc()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return &paymentResponse{
|
||||||
|
rhash: rhash,
|
||||||
|
err: paymentErr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// preparePayment creates an invoice at the receivingPeer and returns a function
|
||||||
|
// that, when called, launches the payment from the sendingPeer.
|
||||||
|
func preparePayment(sendingPeer, receivingPeer lnpeer.Peer,
|
||||||
|
firstHop lnwire.ShortChannelID, hops []ForwardingInfo,
|
||||||
|
invoiceAmt, htlcAmt lnwire.MilliSatoshi,
|
||||||
|
timelock uint32) (*channeldb.Invoice, func() error, error) {
|
||||||
|
|
||||||
sender := sendingPeer.(*mockServer)
|
sender := sendingPeer.(*mockServer)
|
||||||
receiver := receivingPeer.(*mockServer)
|
receiver := receivingPeer.(*mockServer)
|
||||||
|
|
||||||
@ -696,45 +726,27 @@ func makePayment(sendingPeer, receivingPeer lnpeer.Peer,
|
|||||||
// htlc add request.
|
// htlc add request.
|
||||||
blob, err := generateRoute(hops...)
|
blob, err := generateRoute(hops...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
paymentErr <- err
|
return nil, nil, err
|
||||||
return &paymentResponse{
|
|
||||||
rhash: rhash,
|
|
||||||
err: paymentErr,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate payment: invoice and htlc.
|
// Generate payment: invoice and htlc.
|
||||||
invoice, htlc, err := generatePayment(invoiceAmt, htlcAmt, timelock, blob)
|
invoice, htlc, err := generatePayment(invoiceAmt, htlcAmt, timelock, blob)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
paymentErr <- err
|
return nil, nil, err
|
||||||
return &paymentResponse{
|
|
||||||
rhash: rhash,
|
|
||||||
err: paymentErr,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
rhash = fastsha256.Sum256(invoice.Terms.PaymentPreimage[:])
|
|
||||||
|
|
||||||
// Check who is last in the route and add invoice to server registry.
|
// Check who is last in the route and add invoice to server registry.
|
||||||
if err := receiver.registry.AddInvoice(*invoice); err != nil {
|
if err := receiver.registry.AddInvoice(*invoice); err != nil {
|
||||||
paymentErr <- err
|
return nil, nil, err
|
||||||
return &paymentResponse{
|
|
||||||
rhash: rhash,
|
|
||||||
err: paymentErr,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send payment and expose err channel.
|
// Send payment and expose err channel.
|
||||||
go func() {
|
return invoice, func() error {
|
||||||
_, err := sender.htlcSwitch.SendHTLC(
|
_, err := sender.htlcSwitch.SendHTLC(
|
||||||
firstHop, htlc, newMockDeobfuscator(),
|
firstHop, htlc, newMockDeobfuscator(),
|
||||||
)
|
)
|
||||||
paymentErr <- err
|
return err
|
||||||
}()
|
}, nil
|
||||||
|
|
||||||
return &paymentResponse{
|
|
||||||
rhash: rhash,
|
|
||||||
err: paymentErr,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start starts the three hop network alice,bob,carol servers.
|
// start starts the three hop network alice,bob,carol servers.
|
||||||
|
Loading…
Reference in New Issue
Block a user