Merge pull request #3155 from halseth/sendtoroute-zero-value

routing/router: correct SendToRoute's amount record in DB
This commit is contained in:
Wilmer Paulino 2019-06-04 14:57:41 -07:00 committed by GitHub
commit e6c0ddc825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 17 deletions

@ -1637,26 +1637,31 @@ func (r *ChannelRouter) SendToRoute(hash lntypes.Hash, route *route.Route) (
// Create a payment session for just this route. // Create a payment session for just this route.
paySession := r.cfg.MissionControl.NewPaymentSessionForRoute(route) paySession := r.cfg.MissionControl.NewPaymentSessionForRoute(route)
// Create a (mostly) dummy payment, as the created payment session is // Calculate amount paid to receiver.
// not going to do path finding. amt := route.TotalAmount - route.TotalFees()
payment := &LightningPayment{
PaymentHash: hash,
}
// Record this payment hash with the ControlTower, ensuring it is not // Record this payment hash with the ControlTower, ensuring it is not
// already in-flight. // already in-flight.
info := &channeldb.PaymentCreationInfo{ info := &channeldb.PaymentCreationInfo{
PaymentHash: payment.PaymentHash, PaymentHash: hash,
Value: payment.Amount, Value: amt,
CreationDate: time.Now(), CreationDate: time.Now(),
PaymentRequest: nil, PaymentRequest: nil,
} }
err := r.cfg.Control.InitPayment(payment.PaymentHash, info) err := r.cfg.Control.InitPayment(hash, info)
if err != nil { if err != nil {
return [32]byte{}, err return [32]byte{}, err
} }
// Create a (mostly) dummy payment, as the created payment session is
// not going to do path finding.
// TODO(halseth): sendPayment doesn't relly need LightningPayment, make
// it take just needed fields instead.
payment := &LightningPayment{
PaymentHash: hash,
}
// Since this is the first time this payment is being made, we pass nil // Since this is the first time this payment is being made, we pass nil
// for the existing attempt. // for the existing attempt.
preimage, _, err := r.sendPayment(nil, payment, paySession) preimage, _, err := r.sendPayment(nil, payment, paySession)

@ -3175,27 +3175,31 @@ func TestSendToRouteStructuredError(t *testing.T) {
} }
defer cleanUp() defer cleanUp()
// Set up an init channel for the control tower, such that we can make
// sure the payment is initiated correctly.
init := make(chan initArgs, 1)
ctx.router.cfg.Control.(*mockControlTower).init = init
// Setup a route from source a to destination c. The route will be used // Setup a route from source a to destination c. The route will be used
// in a call to SendToRoute. SendToRoute also applies channel updates, // in a call to SendToRoute. SendToRoute also applies channel updates,
// but it saves us from including RequestRoute in the test scope too. // but it saves us from including RequestRoute in the test scope too.
const payAmt = lnwire.MilliSatoshi(10000)
hop1 := ctx.aliases["b"] hop1 := ctx.aliases["b"]
hop2 := ctx.aliases["c"] hop2 := ctx.aliases["c"]
hops := []*route.Hop{ hops := []*route.Hop{
{ {
ChannelID: 1, ChannelID: 1,
PubKeyBytes: hop1, PubKeyBytes: hop1,
AmtToForward: payAmt,
}, },
{ {
ChannelID: 2, ChannelID: 2,
PubKeyBytes: hop2, PubKeyBytes: hop2,
AmtToForward: payAmt,
}, },
} }
rt, err := route.NewRouteFromHops( rt, err := route.NewRouteFromHops(payAmt, 100, ctx.aliases["a"], hops)
lnwire.MilliSatoshi(10000), 100,
ctx.aliases["a"], hops,
)
if err != nil { if err != nil {
t.Fatalf("unable to create route: %v", err) t.Fatalf("unable to create route: %v", err)
} }
@ -3239,4 +3243,14 @@ func TestSendToRouteStructuredError(t *testing.T) {
if _, ok := fErr.FailureMessage.(*lnwire.FailFeeInsufficient); !ok { if _, ok := fErr.FailureMessage.(*lnwire.FailFeeInsufficient); !ok {
t.Fatalf("expected fee insufficient error") t.Fatalf("expected fee insufficient error")
} }
// Check that the correct values were used when initiating the payment.
select {
case initVal := <-init:
if initVal.c.Value != payAmt {
t.Fatalf("expected %v, got %v", payAmt, initVal.c.Value)
}
case <-time.After(100 * time.Millisecond):
t.Fatalf("initPayment not called")
}
} }