github.com/decred/dcrlnd@v0.7.6/lntest/itest/lnd_channel_policy_test.go (about) 1 package itest 2 3 import ( 4 "context" 5 "math" 6 "strings" 7 "time" 8 9 "github.com/decred/dcrd/dcrutil/v4" 10 "github.com/decred/dcrd/wire" 11 "github.com/decred/dcrlnd/chainreg" 12 "github.com/decred/dcrlnd/lnrpc" 13 "github.com/decred/dcrlnd/lntest" 14 "github.com/decred/dcrlnd/lnwire" 15 "github.com/stretchr/testify/require" 16 ) 17 18 // assertPolicyUpdate checks that a given policy update has been received by a 19 // list of given nodes. 20 func assertPolicyUpdate(t *harnessTest, nodes []*lntest.HarnessNode, 21 advertisingNode string, policy *lnrpc.RoutingPolicy, 22 chanPoint *lnrpc.ChannelPoint) { 23 24 for _, node := range nodes { 25 assertChannelPolicyUpdate( 26 t.t, node, advertisingNode, policy, chanPoint, false, 27 ) 28 } 29 } 30 31 // testUpdateChannelPolicy tests that policy updates made to a channel 32 // gets propagated to other nodes in the network. 33 func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) { 34 ctxb := context.Background() 35 36 const ( 37 defaultFeeBase = 1000 38 defaultFeeRate = 1 39 defaultTimeLockDelta = chainreg.DefaultDecredTimeLockDelta 40 defaultMinHtlc = 1000 41 ) 42 43 chanAmt := defaultChanAmt 44 pushAmt := chanAmt / 2 45 defaultMaxHtlc := calculateMaxHtlc(chanAmt) 46 47 // Create a channel Alice->Bob. 48 chanPoint := openChannelAndAssert( 49 t, net, net.Alice, net.Bob, 50 lntest.OpenChannelParams{ 51 Amt: chanAmt, 52 PushAmt: pushAmt, 53 }, 54 ) 55 defer closeChannelAndAssert(t, net, net.Alice, chanPoint, false) 56 57 // We add all the nodes' update channels to a slice, such that we can 58 // make sure they all receive the expected updates. 59 nodes := []*lntest.HarnessNode{net.Alice, net.Bob} 60 61 // Alice and Bob should see each other's ChannelUpdates, advertising the 62 // default routing policies. 63 expectedPolicy := &lnrpc.RoutingPolicy{ 64 FeeBaseMAtoms: defaultFeeBase, 65 FeeRateMilliMAtoms: defaultFeeRate, 66 TimeLockDelta: defaultTimeLockDelta, 67 MinHtlc: defaultMinHtlc, 68 MaxHtlcMAtoms: defaultMaxHtlc, 69 } 70 71 assertPolicyUpdate( 72 t, nodes, net.Alice.PubKeyStr, expectedPolicy, chanPoint, 73 ) 74 assertPolicyUpdate( 75 t, nodes, net.Bob.PubKeyStr, expectedPolicy, chanPoint, 76 ) 77 78 // They should now know about the default policies. 79 for _, node := range nodes { 80 assertChannelPolicy( 81 t, node, net.Alice.PubKeyStr, expectedPolicy, chanPoint, 82 ) 83 assertChannelPolicy( 84 t, node, net.Bob.PubKeyStr, expectedPolicy, chanPoint, 85 ) 86 } 87 88 err := net.Alice.WaitForNetworkChannelOpen(chanPoint) 89 if err != nil { 90 t.Fatalf("alice didn't report channel: %v", err) 91 } 92 err = net.Bob.WaitForNetworkChannelOpen(chanPoint) 93 if err != nil { 94 t.Fatalf("bob didn't report channel: %v", err) 95 } 96 97 // Create Carol with options to rate limit channel updates up to 2 per 98 // day, and create a new channel Bob->Carol. 99 carol := net.NewNode( 100 t.t, "Carol", []string{ 101 "--gossip.max-channel-update-burst=2", 102 "--gossip.channel-update-interval=24h", 103 }, 104 ) 105 106 // Clean up carol's node when the test finishes. 107 defer shutdownAndAssert(net, t, carol) 108 109 nodes = append(nodes, carol) 110 111 // Send some coins to Carol that can be used for channel funding. 112 net.SendCoins(t.t, dcrutil.AtomsPerCoin, carol) 113 114 net.ConnectNodes(t.t, carol, net.Bob) 115 116 // Open the channel Carol->Bob with a custom min_htlc value set. Since 117 // Carol is opening the channel, she will require Bob to not forward 118 // HTLCs smaller than this value, and hence he should advertise it as 119 // part of his ChannelUpdate. 120 const customMinHtlc = 5000 121 chanPoint2 := openChannelAndAssert( 122 t, net, carol, net.Bob, 123 lntest.OpenChannelParams{ 124 Amt: chanAmt, 125 PushAmt: pushAmt, 126 MinHtlc: customMinHtlc, 127 }, 128 ) 129 defer closeChannelAndAssert(t, net, net.Bob, chanPoint2, false) 130 131 expectedPolicyBob := &lnrpc.RoutingPolicy{ 132 FeeBaseMAtoms: defaultFeeBase, 133 FeeRateMilliMAtoms: defaultFeeRate, 134 TimeLockDelta: defaultTimeLockDelta, 135 MinHtlc: customMinHtlc, 136 MaxHtlcMAtoms: defaultMaxHtlc, 137 } 138 expectedPolicyCarol := &lnrpc.RoutingPolicy{ 139 FeeBaseMAtoms: defaultFeeBase, 140 FeeRateMilliMAtoms: defaultFeeRate, 141 TimeLockDelta: defaultTimeLockDelta, 142 MinHtlc: defaultMinHtlc, 143 MaxHtlcMAtoms: defaultMaxHtlc, 144 } 145 146 assertPolicyUpdate( 147 t, nodes, net.Bob.PubKeyStr, expectedPolicyBob, chanPoint2, 148 ) 149 assertPolicyUpdate( 150 t, nodes, carol.PubKeyStr, expectedPolicyCarol, chanPoint2, 151 ) 152 153 // Check that all nodes now know about the updated policies. 154 for _, node := range nodes { 155 assertChannelPolicy( 156 t, node, net.Bob.PubKeyStr, expectedPolicyBob, 157 chanPoint2, 158 ) 159 assertChannelPolicy( 160 t, node, carol.PubKeyStr, expectedPolicyCarol, 161 chanPoint2, 162 ) 163 } 164 165 err = net.Alice.WaitForNetworkChannelOpen(chanPoint2) 166 if err != nil { 167 t.Fatalf("alice didn't report channel: %v", err) 168 } 169 err = net.Bob.WaitForNetworkChannelOpen(chanPoint2) 170 if err != nil { 171 t.Fatalf("bob didn't report channel: %v", err) 172 } 173 err = carol.WaitForNetworkChannelOpen(chanPoint2) 174 if err != nil { 175 t.Fatalf("carol didn't report channel: %v", err) 176 } 177 178 // First we'll try to send a payment from Alice to Carol with an amount 179 // less than the min_htlc value required by Carol. This payment should 180 // fail, as the channel Bob->Carol cannot carry HTLCs this small. 181 payAmt := dcrutil.Amount(4) 182 invoice := &lnrpc.Invoice{ 183 Memo: "testing", 184 Value: int64(payAmt), 185 } 186 ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) 187 resp, err := carol.AddInvoice(ctxt, invoice) 188 if err != nil { 189 t.Fatalf("unable to add invoice: %v", err) 190 } 191 192 err = completePaymentRequests( 193 net.Alice, net.Alice.RouterClient, 194 []string{resp.PaymentRequest}, true, 195 ) 196 197 // Alice knows about the channel policy of Carol and should therefore 198 // not be able to find a path during routing. 199 expErr := lnrpc.PaymentFailureReason_FAILURE_REASON_NO_ROUTE 200 if err.Error() != expErr.String() { 201 t.Fatalf("expected %v, instead got %v", expErr, err) 202 } 203 204 // Now we try to send a payment over the channel with a value too low 205 // to be accepted. First we query for a route to route a payment of 206 // 5000 milli-atoms, as this is accepted. 207 payAmt = dcrutil.Amount(5) 208 routesReq := &lnrpc.QueryRoutesRequest{ 209 PubKey: carol.PubKeyStr, 210 Amt: int64(payAmt), 211 FinalCltvDelta: defaultTimeLockDelta, 212 } 213 214 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 215 routes, err := net.Alice.QueryRoutes(ctxt, routesReq) 216 if err != nil { 217 t.Fatalf("unable to get route: %v", err) 218 } 219 220 if len(routes.Routes) != 1 { 221 t.Fatalf("expected to find 1 route, got %v", len(routes.Routes)) 222 } 223 224 // We change the route to carry a payment of 4000 milli-atoms instead of 5000 225 // milli-atoms. 226 payAmt = dcrutil.Amount(4) 227 amtAtoms := int64(payAmt) 228 amtMAtoms := int64(lnwire.NewMAtomsFromAtoms(payAmt)) 229 routes.Routes[0].Hops[0].AmtToForward = amtAtoms // nolint:staticcheck 230 routes.Routes[0].Hops[0].AmtToForwardMAtoms = amtMAtoms 231 routes.Routes[0].Hops[1].AmtToForward = amtAtoms // nolint:staticcheck 232 routes.Routes[0].Hops[1].AmtToForwardMAtoms = amtMAtoms 233 234 // Send the payment with the modified value. 235 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 236 alicePayStream, err := net.Alice.SendToRoute(ctxt) // nolint:staticcheck 237 if err != nil { 238 t.Fatalf("unable to create payment stream for alice: %v", err) 239 } 240 sendReq := &lnrpc.SendToRouteRequest{ 241 PaymentHash: resp.RHash, 242 Route: routes.Routes[0], 243 } 244 245 err = alicePayStream.Send(sendReq) 246 if err != nil { 247 t.Fatalf("unable to send payment: %v", err) 248 } 249 250 // We expect this payment to fail, and that the min_htlc value is 251 // communicated back to us, since the attempted HTLC value was too low. 252 sendResp, err := alicePayStream.Recv() 253 if err != nil { 254 t.Fatalf("unable to send payment: %v", err) 255 } 256 257 // Expected as part of the error message. 258 substrs := []string{ 259 "AmountBelowMinimum", 260 "HtlcMinimumMAtoms: (lnwire.MilliAtom) 5000 milli-atoms", 261 } 262 for _, s := range substrs { 263 if !strings.Contains(sendResp.PaymentError, s) { 264 t.Fatalf("expected error to contain \"%v\", instead "+ 265 "got %v", s, sendResp.PaymentError) 266 } 267 } 268 269 // Make sure sending using the original value succeeds. 270 payAmt = dcrutil.Amount(5) 271 amtAtoms = int64(payAmt) 272 amtMAtoms = int64(lnwire.NewMAtomsFromAtoms(payAmt)) 273 routes.Routes[0].Hops[0].AmtToForward = amtAtoms // nolint:staticcheck 274 routes.Routes[0].Hops[0].AmtToForwardMAtoms = amtMAtoms 275 routes.Routes[0].Hops[1].AmtToForward = amtAtoms // nolint:staticcheck 276 routes.Routes[0].Hops[1].AmtToForwardMAtoms = amtMAtoms 277 278 // Manually set the MPP payload a new for each payment since 279 // the payment addr will change with each invoice, although we 280 // can re-use the route itself. 281 route := routes.Routes[0] 282 route.Hops[len(route.Hops)-1].TlvPayload = true 283 route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{ 284 PaymentAddr: resp.PaymentAddr, 285 TotalAmtMAtoms: amtMAtoms, 286 } 287 288 sendReq = &lnrpc.SendToRouteRequest{ 289 PaymentHash: resp.RHash, 290 Route: route, 291 } 292 293 err = alicePayStream.Send(sendReq) 294 if err != nil { 295 t.Fatalf("unable to send payment: %v", err) 296 } 297 298 sendResp, err = alicePayStream.Recv() 299 if err != nil { 300 t.Fatalf("unable to send payment: %v", err) 301 } 302 303 if sendResp.PaymentError != "" { 304 t.Fatalf("expected payment to succeed, instead got %v", 305 sendResp.PaymentError) 306 } 307 308 // With our little cluster set up, we'll update the fees and the max htlc 309 // size for the Bob side of the Alice->Bob channel, and make sure 310 // all nodes learn about it. 311 baseFee := int64(1500) 312 feeRate := int64(12) 313 timeLockDelta := uint32(66) 314 maxHtlc := uint64(500000) 315 316 expectedPolicy = &lnrpc.RoutingPolicy{ 317 FeeBaseMAtoms: baseFee, 318 FeeRateMilliMAtoms: testFeeBase * feeRate, 319 TimeLockDelta: timeLockDelta, 320 MinHtlc: defaultMinHtlc, 321 MaxHtlcMAtoms: maxHtlc, 322 } 323 324 req := &lnrpc.PolicyUpdateRequest{ 325 BaseFeeMAtoms: baseFee, 326 FeeRate: float64(feeRate), 327 TimeLockDelta: timeLockDelta, 328 MaxHtlcMAtoms: maxHtlc, 329 Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{ 330 ChanPoint: chanPoint, 331 }, 332 } 333 334 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 335 if _, err := net.Bob.UpdateChannelPolicy(ctxt, req); err != nil { 336 t.Fatalf("unable to get alice's balance: %v", err) 337 } 338 339 // Wait for all nodes to have seen the policy update done by Bob. 340 assertPolicyUpdate( 341 t, nodes, net.Bob.PubKeyStr, expectedPolicy, chanPoint, 342 ) 343 344 // Check that all nodes now know about Bob's updated policy. 345 for _, node := range nodes { 346 assertChannelPolicy( 347 t, node, net.Bob.PubKeyStr, expectedPolicy, chanPoint, 348 ) 349 } 350 351 // Now that all nodes have received the new channel update, we'll try 352 // to send a payment from Alice to Carol to ensure that Alice has 353 // internalized this fee update. This shouldn't affect the route that 354 // Alice takes though: we updated the Alice -> Bob channel and she 355 // doesn't pay for transit over that channel as it's direct. 356 // Note that the payment amount is >= the min_htlc value for the 357 // channel Bob->Carol, so it should successfully be forwarded. 358 payAmt = dcrutil.Amount(5) 359 invoice = &lnrpc.Invoice{ 360 Memo: "testing", 361 Value: int64(payAmt), 362 } 363 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 364 resp, err = carol.AddInvoice(ctxt, invoice) 365 if err != nil { 366 t.Fatalf("unable to add invoice: %v", err) 367 } 368 369 err = completePaymentRequests( 370 net.Alice, net.Alice.RouterClient, 371 []string{resp.PaymentRequest}, true, 372 ) 373 if err != nil { 374 t.Fatalf("unable to send payment: %v", err) 375 } 376 377 // We'll now open a channel from Alice directly to Carol. 378 net.ConnectNodes(t.t, net.Alice, carol) 379 chanPoint3 := openChannelAndAssert( 380 t, net, net.Alice, carol, 381 lntest.OpenChannelParams{ 382 Amt: chanAmt, 383 PushAmt: pushAmt, 384 }, 385 ) 386 defer closeChannelAndAssert(t, net, net.Alice, chanPoint3, false) 387 388 err = net.Alice.WaitForNetworkChannelOpen(chanPoint3) 389 if err != nil { 390 t.Fatalf("alice didn't report channel: %v", err) 391 } 392 err = carol.WaitForNetworkChannelOpen(chanPoint3) 393 if err != nil { 394 t.Fatalf("bob didn't report channel: %v", err) 395 } 396 397 // Make a global update, and check that both channels' new policies get 398 // propagated. 399 baseFee = int64(800) 400 feeRate = int64(123) 401 timeLockDelta = uint32(22) 402 maxHtlc *= 2 403 404 expectedPolicy.FeeBaseMAtoms = baseFee 405 expectedPolicy.FeeRateMilliMAtoms = testFeeBase * feeRate 406 expectedPolicy.TimeLockDelta = timeLockDelta 407 expectedPolicy.MaxHtlcMAtoms = maxHtlc 408 409 req = &lnrpc.PolicyUpdateRequest{ 410 BaseFeeMAtoms: baseFee, 411 FeeRate: float64(feeRate), 412 TimeLockDelta: timeLockDelta, 413 MaxHtlcMAtoms: maxHtlc, 414 } 415 req.Scope = &lnrpc.PolicyUpdateRequest_Global{} 416 417 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 418 _, err = net.Alice.UpdateChannelPolicy(ctxt, req) 419 if err != nil { 420 t.Fatalf("unable to update alice's channel policy: %v", err) 421 } 422 423 // Wait for all nodes to have seen the policy updates for both of 424 // Alice's channels. 425 assertPolicyUpdate( 426 t, nodes, net.Alice.PubKeyStr, expectedPolicy, chanPoint, 427 ) 428 assertPolicyUpdate( 429 t, nodes, net.Alice.PubKeyStr, expectedPolicy, chanPoint3, 430 ) 431 432 // And finally check that all nodes remembers the policy update they 433 // received. 434 for _, node := range nodes { 435 assertChannelPolicy( 436 t, node, net.Alice.PubKeyStr, expectedPolicy, 437 chanPoint, chanPoint3, 438 ) 439 } 440 441 // Now, to test that Carol is properly rate limiting incoming updates, 442 // we'll send two more update from Alice. Carol should accept the first, 443 // but not the second, as she only allows two updates per day and a day 444 // has yet to elapse from the previous update. 445 const numUpdatesTilRateLimit = 2 446 for i := 0; i < numUpdatesTilRateLimit; i++ { 447 prevAlicePolicy := *expectedPolicy 448 baseFee *= 2 449 expectedPolicy.FeeBaseMAtoms = baseFee 450 req.BaseFeeMAtoms = baseFee 451 452 ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) 453 defer cancel() 454 _, err = net.Alice.UpdateChannelPolicy(ctxt, req) 455 require.NoError(t.t, err) 456 457 // Wait for all nodes to have seen the policy updates for both 458 // of Alice's channels. Carol will not see the last update as 459 // the limit has been reached. 460 assertPolicyUpdate( 461 t, []*lntest.HarnessNode{net.Alice, net.Bob}, 462 net.Alice.PubKeyStr, expectedPolicy, chanPoint, 463 ) 464 assertPolicyUpdate( 465 t, []*lntest.HarnessNode{net.Alice, net.Bob}, 466 net.Alice.PubKeyStr, expectedPolicy, chanPoint3, 467 ) 468 // Check that all nodes remembers the policy update 469 // they received. 470 assertChannelPolicy( 471 t, net.Alice, net.Alice.PubKeyStr, 472 expectedPolicy, chanPoint, chanPoint3, 473 ) 474 assertChannelPolicy( 475 t, net.Bob, net.Alice.PubKeyStr, 476 expectedPolicy, chanPoint, chanPoint3, 477 ) 478 479 // Carol was added last, which is why we check the last index. 480 // Since Carol didn't receive the last update, she still has 481 // Alice's old policy. 482 if i == numUpdatesTilRateLimit-1 { 483 expectedPolicy = &prevAlicePolicy 484 } 485 assertPolicyUpdate( 486 t, []*lntest.HarnessNode{carol}, 487 net.Alice.PubKeyStr, expectedPolicy, chanPoint, 488 ) 489 assertPolicyUpdate( 490 t, []*lntest.HarnessNode{carol}, 491 net.Alice.PubKeyStr, expectedPolicy, chanPoint3, 492 ) 493 assertChannelPolicy( 494 t, carol, net.Alice.PubKeyStr, 495 expectedPolicy, chanPoint, chanPoint3, 496 ) 497 } 498 } 499 500 // testSendUpdateDisableChannel ensures that a channel update with the disable 501 // flag set is sent once a channel has been either unilaterally or cooperatively 502 // closed. 503 func testSendUpdateDisableChannel(net *lntest.NetworkHarness, t *harnessTest) { 504 const ( 505 chanAmt = 100000 506 ) 507 508 // Open a channel between Alice and Bob and Alice and Carol. These will 509 // be closed later on in order to trigger channel update messages 510 // marking the channels as disabled. 511 chanPointAliceBob := openChannelAndAssert( 512 t, net, net.Alice, net.Bob, 513 lntest.OpenChannelParams{ 514 Amt: chanAmt, 515 }, 516 ) 517 518 carol := net.NewNode( 519 t.t, "Carol", []string{ 520 "--minbackoff=10s", 521 "--chan-enable-timeout=1.5s", 522 "--chan-disable-timeout=3s", 523 "--chan-status-sample-interval=.5s", 524 }) 525 defer shutdownAndAssert(net, t, carol) 526 527 net.ConnectNodes(t.t, net.Alice, carol) 528 chanPointAliceCarol := openChannelAndAssert( 529 t, net, net.Alice, carol, 530 lntest.OpenChannelParams{ 531 Amt: chanAmt, 532 }, 533 ) 534 535 // We create a new node Eve that has an inactive channel timeout of 536 // just 2 seconds (down from the default 20m). It will be used to test 537 // channel updates for channels going inactive. 538 eve := net.NewNode( 539 t.t, "Eve", []string{ 540 "--minbackoff=10s", 541 "--chan-enable-timeout=1.5s", 542 "--chan-disable-timeout=3s", 543 "--chan-status-sample-interval=.5s", 544 }) 545 defer shutdownAndAssert(net, t, eve) 546 547 // Give Eve some coins. 548 net.SendCoins(t.t, dcrutil.AtomsPerCoin, eve) 549 550 // Connect Eve to Carol and Bob, and open a channel to carol. 551 net.ConnectNodes(t.t, eve, carol) 552 net.ConnectNodes(t.t, eve, net.Bob) 553 554 chanPointEveCarol := openChannelAndAssert( 555 t, net, eve, carol, 556 lntest.OpenChannelParams{ 557 Amt: chanAmt, 558 }, 559 ) 560 561 // Launch a node for Dave which will connect to Bob in order to receive 562 // graph updates from. This will ensure that the channel updates are 563 // propagated throughout the network. 564 dave := net.NewNode(t.t, "Dave", nil) 565 defer shutdownAndAssert(net, t, dave) 566 567 net.ConnectNodes(t.t, net.Bob, dave) 568 569 // We should expect to see a channel update with the default routing 570 // policy, except that it should indicate the channel is disabled. 571 expectedPolicy := &lnrpc.RoutingPolicy{ 572 FeeBaseMAtoms: int64(chainreg.DefaultDecredBaseFeeMAtoms), 573 FeeRateMilliMAtoms: int64(chainreg.DefaultDecredFeeRate), 574 TimeLockDelta: chainreg.DefaultDecredTimeLockDelta, 575 MinHtlc: 1000, // default value 576 MaxHtlcMAtoms: calculateMaxHtlc(chanAmt), 577 Disabled: true, 578 } 579 580 // assertPolicyUpdate checks that the required policy update has 581 // happened on the given node. 582 assertPolicyUpdate := func(node *lntest.HarnessNode, 583 policy *lnrpc.RoutingPolicy, chanPoint *lnrpc.ChannelPoint) { 584 585 require.NoError( 586 t.t, dave.WaitForChannelPolicyUpdate( 587 node.PubKeyStr, policy, chanPoint, false, 588 ), "error while waiting for channel update", 589 ) 590 } 591 592 // Let Carol go offline. Since Eve has an inactive timeout of 2s, we 593 // expect her to send an update disabling the channel. 594 restartCarol, err := net.SuspendNode(carol) 595 if err != nil { 596 t.Fatalf("unable to suspend carol: %v", err) 597 } 598 599 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 600 601 // We restart Carol. Since the channel now becomes active again, Eve 602 // should send a ChannelUpdate setting the channel no longer disabled. 603 if err := restartCarol(); err != nil { 604 t.Fatalf("unable to restart carol: %v", err) 605 } 606 607 expectedPolicy.Disabled = false 608 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 609 610 // Now we'll test a long disconnection. Disconnect Carol and Eve and 611 // ensure they both detect each other as disabled. Their min backoffs 612 // are high enough to not interfere with disabling logic. 613 if err := net.DisconnectNodes(carol, eve); err != nil { 614 t.Fatalf("unable to disconnect Carol from Eve: %v", err) 615 } 616 617 // Wait for a disable from both Carol and Eve to come through. 618 expectedPolicy.Disabled = true 619 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 620 assertPolicyUpdate(carol, expectedPolicy, chanPointEveCarol) 621 622 // Reconnect Carol and Eve, this should cause them to reenable the 623 // channel from both ends after a short delay. 624 net.EnsureConnected(t.t, carol, eve) 625 626 expectedPolicy.Disabled = false 627 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 628 assertPolicyUpdate(carol, expectedPolicy, chanPointEveCarol) 629 630 // Now we'll test a short disconnection. Disconnect Carol and Eve, then 631 // reconnect them after one second so that their scheduled disables are 632 // aborted. One second is twice the status sample interval, so this 633 // should allow for the disconnect to be detected, but still leave time 634 // to cancel the announcement before the 3 second inactive timeout is 635 // hit. 636 if err := net.DisconnectNodes(carol, eve); err != nil { 637 t.Fatalf("unable to disconnect Carol from Eve: %v", err) 638 } 639 time.Sleep(time.Second) 640 net.EnsureConnected(t.t, eve, carol) 641 642 // Since the disable should have been canceled by both Carol and Eve, we 643 // expect no channel updates to appear on the network, which means we 644 // expect the polices stay unchanged(Disable == false). 645 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 646 assertPolicyUpdate(carol, expectedPolicy, chanPointEveCarol) 647 648 // Close Alice's channels with Bob and Carol cooperatively and 649 // unilaterally respectively. 650 _, _, err = net.CloseChannel(net.Alice, chanPointAliceBob, false) 651 if err != nil { 652 t.Fatalf("unable to close channel: %v", err) 653 } 654 655 _, _, err = net.CloseChannel(net.Alice, chanPointAliceCarol, true) 656 if err != nil { 657 t.Fatalf("unable to close channel: %v", err) 658 } 659 660 // Now that the channel close processes have been started, we should 661 // receive an update marking each as disabled. 662 expectedPolicy.Disabled = true 663 assertPolicyUpdate(net.Alice, expectedPolicy, chanPointAliceBob) 664 assertPolicyUpdate(net.Alice, expectedPolicy, chanPointAliceCarol) 665 666 // Finally, close the channels by mining the closing transactions. 667 mineBlocks(t, net, 1, 2) 668 669 // Also do this check for Eve's channel with Carol. 670 _, _, err = net.CloseChannel(eve, chanPointEveCarol, false) 671 if err != nil { 672 t.Fatalf("unable to close channel: %v", err) 673 } 674 675 assertPolicyUpdate(eve, expectedPolicy, chanPointEveCarol) 676 677 mineBlocks(t, net, 1, 1) 678 679 // And finally, clean up the force closed channel by mining the 680 // sweeping transaction. 681 cleanupForceClose(t, net, net.Alice, chanPointAliceCarol) 682 } 683 684 // testUpdateChannelPolicyForPrivateChannel tests when a private channel 685 // updates its channel edge policy, we will use the updated policy to send our 686 // payment. 687 // The topology is created as: Alice -> Bob -> Carol, where Alice -> Bob is 688 // public and Bob -> Carol is private. After an invoice is created by Carol, 689 // Bob will update the base fee via UpdateChannelPolicy, we will test that 690 // Alice will not fail the payment and send it using the updated channel 691 // policy. 692 func testUpdateChannelPolicyForPrivateChannel(net *lntest.NetworkHarness, 693 t *harnessTest) { 694 695 ctxb := context.Background() 696 defer ctxb.Done() 697 698 // We'll create the following topology first, 699 // Alice <--public:100k--> Bob <--private:100k--> Carol 700 const chanAmt = dcrutil.Amount(100000) 701 702 // Open a channel with 100k satoshis between Alice and Bob. 703 chanPointAliceBob := openChannelAndAssert( 704 t, net, net.Alice, net.Bob, 705 lntest.OpenChannelParams{ 706 Amt: chanAmt, 707 }, 708 ) 709 defer closeChannelAndAssert(t, net, net.Alice, chanPointAliceBob, false) 710 711 // Get Alice's funding point. 712 aliceChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointAliceBob) 713 require.NoError(t.t, err, "unable to get txid") 714 aliceFundPoint := wire.OutPoint{ 715 Hash: *aliceChanTXID, 716 Index: chanPointAliceBob.OutputIndex, 717 } 718 719 // Create a new node Carol. 720 carol := net.NewNode(t.t, "Carol", nil) 721 defer shutdownAndAssert(net, t, carol) 722 723 // Connect Carol to Bob. 724 net.ConnectNodes(t.t, carol, net.Bob) 725 726 // Open a channel with 100k satoshis between Bob and Carol. 727 chanPointBobCarol := openChannelAndAssert( 728 t, net, net.Bob, carol, 729 lntest.OpenChannelParams{ 730 Amt: chanAmt, 731 Private: true, 732 }, 733 ) 734 defer closeChannelAndAssert(t, net, net.Bob, chanPointBobCarol, false) 735 736 // Carol should be aware of the channel between Alice and Bob. 737 err = carol.WaitForNetworkChannelOpen(chanPointAliceBob) 738 require.NoError(t.t, err) 739 740 // Get Bob's funding point. 741 bobChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointBobCarol) 742 require.NoError(t.t, err, "unable to get txid") 743 bobFundPoint := wire.OutPoint{ 744 Hash: *bobChanTXID, 745 Index: chanPointBobCarol.OutputIndex, 746 } 747 748 // We should have the following topology now, 749 // Alice <--public:100k--> Bob <--private:100k--> Carol 750 // 751 // Now we will create an invoice for Carol. 752 const paymentAmt = 20000 753 invoice := &lnrpc.Invoice{ 754 Memo: "routing hints", 755 Value: paymentAmt, 756 Private: true, 757 } 758 ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) 759 resp, err := carol.AddInvoice(ctxt, invoice) 760 require.NoError(t.t, err, "unable to create invoice for carol") 761 762 // Track how many payments Alice has previously done in other tests. 763 oldPaymentsResp, err := net.Alice.ListPayments( 764 ctxt, &lnrpc.ListPaymentsRequest{}, 765 ) 766 require.NoError(t.t, err) 767 newPaymentsStartIdx := oldPaymentsResp.LastIndexOffset 768 769 // Bob now updates the channel edge policy for the private channel. 770 const ( 771 baseFeeMSat = 33000 772 ) 773 timeLockDelta := uint32(chainreg.DefaultDecredTimeLockDelta) 774 updateFeeReq := &lnrpc.PolicyUpdateRequest{ 775 BaseFeeMAtoms: baseFeeMSat, 776 TimeLockDelta: timeLockDelta, 777 Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{ 778 ChanPoint: chanPointBobCarol, 779 }, 780 } 781 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 782 _, err = net.Bob.UpdateChannelPolicy(ctxt, updateFeeReq) 783 require.NoError(t.t, err, "unable to update chan policy") 784 785 // Alice pays the invoices. She will use the updated baseFeeMSat in the 786 // payment 787 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 788 payReqs := []string{resp.PaymentRequest} 789 require.NoError(t.t, 790 completePaymentRequests( 791 net.Alice, net.Alice.RouterClient, payReqs, true, 792 ), "unable to send payment", 793 ) 794 795 // Check that Alice did make the payment with two HTLCs, one failed and 796 // one succeeded. 797 ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) 798 paymentsResp, err := net.Alice.ListPayments( 799 ctxt, &lnrpc.ListPaymentsRequest{IndexOffset: newPaymentsStartIdx}, 800 ) 801 require.NoError(t.t, err, "failed to obtain payments for Alice") 802 require.Equal(t.t, 1, len(paymentsResp.Payments), "expected 1 payment") 803 804 htlcs := paymentsResp.Payments[0].Htlcs 805 require.Equal(t.t, 2, len(htlcs), "expected to have 2 HTLCs") 806 require.Equal( 807 t.t, lnrpc.HTLCAttempt_FAILED, htlcs[0].Status, 808 "the first HTLC attempt should fail", 809 ) 810 require.Equal( 811 t.t, lnrpc.HTLCAttempt_SUCCEEDED, htlcs[1].Status, 812 "the second HTLC attempt should succeed", 813 ) 814 815 // Carol should have received 20k satoshis from Bob. 816 assertAmountPaid(t, "Carol(remote) [<=private] Bob(local)", 817 carol, bobFundPoint, 0, paymentAmt) 818 819 // Bob should have sent 20k satoshis to Carol. 820 assertAmountPaid(t, "Bob(local) [private=>] Carol(remote)", 821 net.Bob, bobFundPoint, paymentAmt, 0) 822 823 // Calcuate the amount in satoshis. 824 amtExpected := int64(paymentAmt + baseFeeMSat/1000) 825 826 // Bob should have received 20k satoshis + fee from Alice. 827 assertAmountPaid(t, "Bob(remote) <= Alice(local)", 828 net.Bob, aliceFundPoint, 0, amtExpected) 829 830 // Alice should have sent 20k satoshis + fee to Bob. 831 assertAmountPaid(t, "Alice(local) => Bob(remote)", 832 net.Alice, aliceFundPoint, amtExpected, 0) 833 } 834 835 // testUpdateChannelPolicyFeeRateAccuracy tests that updating the channel policy 836 // rounds fee rate values correctly as well as setting fee rate with ppm works 837 // as expected. 838 func testUpdateChannelPolicyFeeRateAccuracy(net *lntest.NetworkHarness, 839 t *harnessTest) { 840 841 chanAmt := defaultChanAmt 842 pushAmt := chanAmt / 2 843 844 // Create a channel Alice -> Bob. 845 chanPoint := openChannelAndAssert( 846 t, net, net.Alice, net.Bob, 847 lntest.OpenChannelParams{ 848 Amt: chanAmt, 849 PushAmt: pushAmt, 850 }, 851 ) 852 defer closeChannelAndAssert(t, net, net.Alice, chanPoint, false) 853 854 // Nodes that we need to make sure receive the channel updates. 855 nodes := []*lntest.HarnessNode{net.Alice, net.Bob} 856 857 baseFee := int64(1500) 858 timeLockDelta := uint32(66) 859 maxHtlc := uint64(500000) 860 defaultMinHtlc := int64(1000) 861 862 // Originally LND did not properly round up fee rates which caused 863 // inaccuracy where fee rates were simply rounded down due to the 864 // integer conversion. 865 // 866 // We'll use a fee rate of 0.031337 which without rounding up would 867 // have resulted in a fee rate ppm of 31336. 868 feeRate := 0.031337 869 870 // Expected fee rate will be rounded up. 871 expectedFeeRateMilliMAtoms := int64(math.Round(testFeeBase * feeRate)) 872 873 expectedPolicy := &lnrpc.RoutingPolicy{ 874 FeeBaseMAtoms: baseFee, 875 FeeRateMilliMAtoms: expectedFeeRateMilliMAtoms, 876 TimeLockDelta: timeLockDelta, 877 MinHtlc: defaultMinHtlc, 878 MaxHtlcMAtoms: maxHtlc, 879 } 880 881 req := &lnrpc.PolicyUpdateRequest{ 882 BaseFeeMAtoms: baseFee, 883 FeeRate: feeRate, 884 TimeLockDelta: timeLockDelta, 885 MaxHtlcMAtoms: maxHtlc, 886 Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{ 887 ChanPoint: chanPoint, 888 }, 889 } 890 891 ctxb := context.Background() 892 ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) 893 if _, err := net.Alice.UpdateChannelPolicy(ctxt, req); err != nil { 894 t.Fatalf("unable to get alice's balance: %v", err) 895 } 896 897 // Make sure that both Alice and Bob sees the same policy after update. 898 assertPolicyUpdate( 899 t, nodes, net.Alice.PubKeyStr, expectedPolicy, chanPoint, 900 ) 901 902 // Now use the new PPM feerate field and make sure that the feerate is 903 // correctly set. 904 feeRatePPM := uint32(32337) 905 req.FeeRate = 0 // Can't set both at the same time. 906 req.FeeRatePpm = feeRatePPM 907 expectedPolicy.FeeRateMilliMAtoms = int64(feeRatePPM) 908 909 ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) 910 if _, err := net.Alice.UpdateChannelPolicy(ctxt, req); err != nil { 911 t.Fatalf("unable to get alice's balance: %v", err) 912 } 913 914 // Make sure that both Alice and Bob sees the same policy after update. 915 assertPolicyUpdate( 916 t, nodes, net.Alice.PubKeyStr, expectedPolicy, chanPoint, 917 ) 918 919 }