|
1 | 1 | package itest |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
4 | 5 | "fmt" |
5 | 6 | "strings" |
6 | 7 | "testing" |
@@ -654,6 +655,135 @@ func testUpdateNodeAnnouncement(ht *lntest.HarnessTest) { |
654 | 655 | dave.RPC.UpdateNodeAnnouncementErr(nodeAnnReq) |
655 | 656 | } |
656 | 657 |
|
| 658 | +// testSelfNodeAnnouncementPersistence tests that the node announcement configs |
| 659 | +// are persisted correctly and reused when the node is restarted using the |
| 660 | +// correct hierarchy (config > source node > defaults). |
| 661 | +func testSelfNodeAnnouncementPersistence(ht *lntest.HarnessTest) { |
| 662 | + // Start Alice with default node announcement options. |
| 663 | + alice := ht.NewNode("Alice", nil) |
| 664 | + |
| 665 | + // assertAddrs is a helper function to assert that the node info |
| 666 | + // contains the correct addresses. |
| 667 | + assertAddrs := func(addrsFound []string, targetAddrs ...string) error { |
| 668 | + addrs := make(map[string]struct{}, len(addrsFound)) |
| 669 | + for _, addr := range addrsFound { |
| 670 | + addr = strings.Split(addr, "@")[1] |
| 671 | + addrs[addr] = struct{}{} |
| 672 | + } |
| 673 | + |
| 674 | + for _, addr := range targetAddrs { |
| 675 | + _, ok := addrs[addr] |
| 676 | + if !ok { |
| 677 | + return fmt.Errorf("address %v not found in "+ |
| 678 | + "node announcement", addr) |
| 679 | + } |
| 680 | + } |
| 681 | + |
| 682 | + return nil |
| 683 | + } |
| 684 | + |
| 685 | + // assertNodeInfo is a helper function to assert that the node info |
| 686 | + // contains the correct values. |
| 687 | + assertNodeInfo := func(resp *lnrpc.GetInfoResponse, expectedAlias, |
| 688 | + expectedColor string, expectedAddrs ...string) { |
| 689 | + |
| 690 | + require.Equal(ht, expectedAlias, resp.Alias) |
| 691 | + require.Equal(ht, expectedColor, resp.Color) |
| 692 | + err := assertAddrs(resp.Uris, expectedAddrs...) |
| 693 | + require.NoError(ht, err) |
| 694 | + } |
| 695 | + |
| 696 | + // Get the node info and verify that the default values are used for |
| 697 | + // alias and color. |
| 698 | + resp := alice.RPC.GetInfo() |
| 699 | + |
| 700 | + // The alias should be the first 10 bytes of the serialized public key. |
| 701 | + defaultAlias := hex.EncodeToString(alice.PubKey[:10]) |
| 702 | + |
| 703 | + // Assert that the default values are used for alias and color. |
| 704 | + assertNodeInfo(resp, defaultAlias, "#3399ff") |
| 705 | + |
| 706 | + // Update the node announcement and set an alias, color, and addresses. |
| 707 | + nodeAnnReq := &peersrpc.NodeAnnouncementUpdateRequest{ |
| 708 | + Alias: "alice", |
| 709 | + Color: "#eeeeee", |
| 710 | + AddressUpdates: []*peersrpc.UpdateAddressAction{ |
| 711 | + { |
| 712 | + Action: peersrpc.UpdateAction_ADD, |
| 713 | + Address: "192.168.1.10:8333", |
| 714 | + }, |
| 715 | + { |
| 716 | + Action: peersrpc.UpdateAction_ADD, |
| 717 | + Address: "192.168.1.11:8333", |
| 718 | + }, |
| 719 | + }, |
| 720 | + } |
| 721 | + |
| 722 | + response := alice.RPC.UpdateNodeAnnouncement(nodeAnnReq) |
| 723 | + |
| 724 | + expectedOps := map[string]int{ |
| 725 | + "alias": 1, |
| 726 | + "color": 1, |
| 727 | + "addresses": 2, |
| 728 | + } |
| 729 | + assertUpdateNodeAnnouncementResponse(ht, response, expectedOps) |
| 730 | + |
| 731 | + resp = alice.RPC.GetInfo() |
| 732 | + assertNodeInfo( |
| 733 | + resp, "alice", "#eeeeee", "192.168.1.10:8333", |
| 734 | + "192.168.1.11:8333", |
| 735 | + ) |
| 736 | + |
| 737 | + // Restart Alice. |
| 738 | + ht.RestartNode(alice) |
| 739 | + |
| 740 | + // After restarting, the node info should contain the values that were |
| 741 | + // set in the update request since the updated values take precedence |
| 742 | + // over the default values. |
| 743 | + resp = alice.RPC.GetInfo() |
| 744 | + assertNodeInfo( |
| 745 | + resp, "alice", "#eeeeee", "192.168.1.10:8333", |
| 746 | + "192.168.1.11:8333", |
| 747 | + ) |
| 748 | + |
| 749 | + // Test that we can still remove an address. |
| 750 | + removeAddrReq := &peersrpc.NodeAnnouncementUpdateRequest{ |
| 751 | + AddressUpdates: []*peersrpc.UpdateAddressAction{ |
| 752 | + { |
| 753 | + Action: peersrpc.UpdateAction_REMOVE, |
| 754 | + Address: "192.168.1.10:8333", |
| 755 | + }, |
| 756 | + }, |
| 757 | + } |
| 758 | + response = alice.RPC.UpdateNodeAnnouncement(removeAddrReq) |
| 759 | + expectedOps = map[string]int{ |
| 760 | + "addresses": 1, |
| 761 | + } |
| 762 | + assertUpdateNodeAnnouncementResponse(ht, response, expectedOps) |
| 763 | + |
| 764 | + // Now we restart the node with custom values in the config. |
| 765 | + lndArgs := []string{ |
| 766 | + "--externalip=192.168.1.12:8333", |
| 767 | + "--externalip=192.168.1.13:8333", |
| 768 | + "--alias=alice-updated", |
| 769 | + "--color=#ffffff", |
| 770 | + } |
| 771 | + ht.RestartNodeWithExtraArgs(alice, lndArgs) |
| 772 | + |
| 773 | + // Get the node info and verify that the values are the same as the |
| 774 | + // ones we set in the config (and not the updated values). |
| 775 | + // The addresses should be the same as the ones we set in the config |
| 776 | + // plus the ones we set in the update request earlier. |
| 777 | + resp = alice.RPC.GetInfo() |
| 778 | + assertNodeInfo( |
| 779 | + resp, "alice-updated", "#ffffff", "192.168.1.11:8333", |
| 780 | + "192.168.1.12:8333", "192.168.1.13:8333", |
| 781 | + ) |
| 782 | + |
| 783 | + // The address we removed earlier should not be present. |
| 784 | + require.Error(ht, assertAddrs(resp.Uris, "192.168.1.10:8333")) |
| 785 | +} |
| 786 | + |
657 | 787 | // assertSyncType asserts that the peer has an expected syncType. |
658 | 788 | // |
659 | 789 | // NOTE: only made for tests in this file. |
|
0 commit comments