|
| 1 | +// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package faultinjection |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + ginkgo "github.com/onsi/ginkgo/v2" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/ava-labs/avalanchego/api/health" |
| 14 | + "github.com/ava-labs/avalanchego/api/info" |
| 15 | + cfg "github.com/ava-labs/avalanchego/config" |
| 16 | + "github.com/ava-labs/avalanchego/ids" |
| 17 | + "github.com/ava-labs/avalanchego/tests/e2e" |
| 18 | + "github.com/ava-labs/avalanchego/tests/fixture/testnet" |
| 19 | + "github.com/ava-labs/avalanchego/utils/set" |
| 20 | +) |
| 21 | + |
| 22 | +var _ = ginkgo.Describe("Duplicate node handling", func() { |
| 23 | + require := require.New(ginkgo.GinkgoT()) |
| 24 | + |
| 25 | + ginkgo.It("should ensure that a given Node ID (i.e. staking keypair) can be used at most once on a network", func() { |
| 26 | + network := e2e.Env.GetNetwork() |
| 27 | + nodes := network.GetNodes() |
| 28 | + |
| 29 | + ginkgo.By("creating new node") |
| 30 | + newNode1 := e2e.AddTemporaryNode(network, testnet.FlagsMap{}, true /* waitForHealthy */) |
| 31 | + |
| 32 | + ginkgo.By("checking that the new node is connected to its peers") |
| 33 | + checkConnectedPeers(nodes, newNode1) |
| 34 | + |
| 35 | + ginkgo.By("creating a second new node with the same staking keypair as the first new node") |
| 36 | + node1Flags := newNode1.GetConfig().Flags |
| 37 | + node2Flags := testnet.FlagsMap{ |
| 38 | + cfg.StakingTLSKeyContentKey: node1Flags[cfg.StakingTLSKeyContentKey], |
| 39 | + cfg.StakingCertContentKey: node1Flags[cfg.StakingCertContentKey], |
| 40 | + } |
| 41 | + newNode2 := e2e.AddTemporaryNode(network, node2Flags, false /* waitForHealthy */) |
| 42 | + healthClient2 := health.NewClient(newNode2.GetRuntimeState().URI) |
| 43 | + |
| 44 | + ginkgo.By("checking that the second new node fails to become healthy within timeout") |
| 45 | + require.Never(func() bool { |
| 46 | + health, err := healthClient2.Health(context.Background(), nil) |
| 47 | + require.NoError(err) |
| 48 | + return health.Healthy |
| 49 | + }, e2e.DefaultBootstrapTimeout, time.Second, "node2 was able to bootstrap") |
| 50 | + |
| 51 | + ginkgo.By("stopping the first new node") |
| 52 | + require.NoError(newNode1.Stop()) |
| 53 | + |
| 54 | + ginkgo.By("checking that the second new node becomes healthy within timeout") |
| 55 | + require.Eventually(func() bool { |
| 56 | + health, err := healthClient2.Health(context.Background(), nil) |
| 57 | + require.NoError(err) |
| 58 | + return health.Healthy |
| 59 | + }, e2e.DefaultBootstrapTimeout, time.Second, "node2 was not able to bootstrap") |
| 60 | + |
| 61 | + ginkgo.By("checking that the second new node is connected to its peers") |
| 62 | + checkConnectedPeers(nodes, newNode2) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +// Check that a new node is connected to existing nodes and vice versa |
| 67 | +func checkConnectedPeers(existingNodes []testnet.Node, newNode testnet.Node) { |
| 68 | + require := require.New(ginkgo.GinkgoT()) |
| 69 | + |
| 70 | + // Collect the node ids of the new node's peers |
| 71 | + infoClient := info.NewClient(newNode.GetRuntimeState().URI) |
| 72 | + peers, err := infoClient.Peers(context.Background()) |
| 73 | + require.NoError(err) |
| 74 | + peerIDs := set.NewSet[ids.NodeID](len(existingNodes)) |
| 75 | + for _, peer := range peers { |
| 76 | + peerIDs.Add(peer.ID) |
| 77 | + } |
| 78 | + |
| 79 | + newNodeID := newNode.GetID().String() |
| 80 | + for _, existingNode := range existingNodes { |
| 81 | + // Check that the existing node is a peer of the new node |
| 82 | + require.True(peerIDs.Contains(existingNode.GetID())) |
| 83 | + |
| 84 | + // Check that the new node is a peer |
| 85 | + infoClient := info.NewClient(existingNode.GetRuntimeState().URI) |
| 86 | + peers, err := infoClient.Peers(context.Background()) |
| 87 | + require.NoError(err) |
| 88 | + isPeer := false |
| 89 | + for _, peer := range peers { |
| 90 | + if peer.ID.String() == newNodeID { |
| 91 | + isPeer = true |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + require.True(isPeer) |
| 96 | + } |
| 97 | +} |
0 commit comments