|
| 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 | + "fmt" |
| 9 | + |
| 10 | + ginkgo "github.com/onsi/ginkgo/v2" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/ava-labs/avalanchego/api/info" |
| 15 | + "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 | + node1 := e2e.AddEphemeralNode(network, testnet.FlagsMap{}) |
| 31 | + e2e.WaitForHealthy(node1) |
| 32 | + |
| 33 | + ginkgo.By("checking that the new node is connected to its peers") |
| 34 | + checkConnectedPeers(nodes, node1) |
| 35 | + |
| 36 | + ginkgo.By("creating a second new node with the same staking keypair as the first new node") |
| 37 | + node1Flags := node1.GetConfig().Flags |
| 38 | + node2Flags := testnet.FlagsMap{ |
| 39 | + config.StakingTLSKeyContentKey: node1Flags[config.StakingTLSKeyContentKey], |
| 40 | + config.StakingCertContentKey: node1Flags[config.StakingCertContentKey], |
| 41 | + // Construct a unique data dir to ensure the two nodes' data will be stored |
| 42 | + // separately. Usually the dir name is the node ID but in this one case the nodes have |
| 43 | + // the same node ID. |
| 44 | + config.DataDirKey: fmt.Sprintf("%s-second", node1Flags[config.DataDirKey]), |
| 45 | + } |
| 46 | + node2 := e2e.AddEphemeralNode(network, node2Flags) |
| 47 | + |
| 48 | + ginkgo.By("checking that the second new node fails to become healthy before timeout") |
| 49 | + err := testnet.WaitForHealthy(e2e.DefaultContext(), node2) |
| 50 | + require.ErrorIs(err, context.DeadlineExceeded) |
| 51 | + |
| 52 | + ginkgo.By("stopping the first new node") |
| 53 | + require.NoError(node1.Stop()) |
| 54 | + |
| 55 | + ginkgo.By("checking that the second new node becomes healthy within timeout") |
| 56 | + e2e.WaitForHealthy(node2) |
| 57 | + |
| 58 | + ginkgo.By("checking that the second new node is connected to its peers") |
| 59 | + checkConnectedPeers(nodes, node2) |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +// Check that a new node is connected to existing nodes and vice versa |
| 64 | +func checkConnectedPeers(existingNodes []testnet.Node, newNode testnet.Node) { |
| 65 | + require := require.New(ginkgo.GinkgoT()) |
| 66 | + |
| 67 | + // Collect the node ids of the new node's peers |
| 68 | + infoClient := info.NewClient(newNode.GetProcessContext().URI) |
| 69 | + peers, err := infoClient.Peers(context.Background()) |
| 70 | + require.NoError(err) |
| 71 | + peerIDs := set.NewSet[ids.NodeID](len(existingNodes)) |
| 72 | + for _, peer := range peers { |
| 73 | + peerIDs.Add(peer.ID) |
| 74 | + } |
| 75 | + |
| 76 | + newNodeID := newNode.GetID() |
| 77 | + for _, existingNode := range existingNodes { |
| 78 | + // Check that the existing node is a peer of the new node |
| 79 | + require.True(peerIDs.Contains(existingNode.GetID())) |
| 80 | + |
| 81 | + // Check that the new node is a peer |
| 82 | + infoClient := info.NewClient(existingNode.GetProcessContext().URI) |
| 83 | + peers, err := infoClient.Peers(context.Background()) |
| 84 | + require.NoError(err) |
| 85 | + isPeer := false |
| 86 | + for _, peer := range peers { |
| 87 | + if peer.ID == newNodeID { |
| 88 | + isPeer = true |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + require.True(isPeer) |
| 93 | + } |
| 94 | +} |
0 commit comments