github.com/ava-labs/avalanchego@v1.11.11/chains/subnets_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package chains 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/ava-labs/avalanchego/ids" 12 "github.com/ava-labs/avalanchego/subnets" 13 "github.com/ava-labs/avalanchego/utils/constants" 14 ) 15 16 func TestNewSubnets(t *testing.T) { 17 require := require.New(t) 18 config := map[ids.ID]subnets.Config{ 19 constants.PrimaryNetworkID: {}, 20 } 21 22 subnets, err := NewSubnets(ids.EmptyNodeID, config) 23 require.NoError(err) 24 25 subnet, ok := subnets.GetOrCreate(constants.PrimaryNetworkID) 26 require.False(ok) 27 require.Equal(config[constants.PrimaryNetworkID], subnet.Config()) 28 } 29 30 func TestNewSubnetsNoPrimaryNetworkConfig(t *testing.T) { 31 require := require.New(t) 32 config := map[ids.ID]subnets.Config{} 33 34 _, err := NewSubnets(ids.EmptyNodeID, config) 35 require.ErrorIs(err, ErrNoPrimaryNetworkConfig) 36 } 37 38 func TestSubnetsGetOrCreate(t *testing.T) { 39 testSubnetID := ids.GenerateTestID() 40 41 type args struct { 42 subnetID ids.ID 43 want bool 44 } 45 46 tests := []struct { 47 name string 48 args []args 49 }{ 50 { 51 name: "adding duplicate subnet is a noop", 52 args: []args{ 53 { 54 subnetID: testSubnetID, 55 want: true, 56 }, 57 { 58 subnetID: testSubnetID, 59 }, 60 }, 61 }, 62 { 63 name: "adding unique subnets succeeds", 64 args: []args{ 65 { 66 subnetID: ids.GenerateTestID(), 67 want: true, 68 }, 69 { 70 subnetID: ids.GenerateTestID(), 71 want: true, 72 }, 73 { 74 subnetID: ids.GenerateTestID(), 75 want: true, 76 }, 77 }, 78 }, 79 } 80 81 for _, tt := range tests { 82 t.Run(tt.name, func(t *testing.T) { 83 require := require.New(t) 84 config := map[ids.ID]subnets.Config{ 85 constants.PrimaryNetworkID: {}, 86 } 87 subnets, err := NewSubnets(ids.EmptyNodeID, config) 88 require.NoError(err) 89 90 for _, arg := range tt.args { 91 _, got := subnets.GetOrCreate(arg.subnetID) 92 require.Equal(arg.want, got) 93 } 94 }) 95 } 96 } 97 98 func TestSubnetConfigs(t *testing.T) { 99 testSubnetID := ids.GenerateTestID() 100 101 tests := []struct { 102 name string 103 config map[ids.ID]subnets.Config 104 subnetID ids.ID 105 want subnets.Config 106 }{ 107 { 108 name: "default to primary network config", 109 config: map[ids.ID]subnets.Config{ 110 constants.PrimaryNetworkID: {}, 111 }, 112 subnetID: testSubnetID, 113 want: subnets.Config{}, 114 }, 115 { 116 name: "use subnet config", 117 config: map[ids.ID]subnets.Config{ 118 constants.PrimaryNetworkID: {}, 119 testSubnetID: { 120 ValidatorOnly: true, 121 }, 122 }, 123 subnetID: testSubnetID, 124 want: subnets.Config{ 125 ValidatorOnly: true, 126 }, 127 }, 128 } 129 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 require := require.New(t) 133 134 subnets, err := NewSubnets(ids.EmptyNodeID, tt.config) 135 require.NoError(err) 136 137 subnet, ok := subnets.GetOrCreate(tt.subnetID) 138 require.True(ok) 139 140 require.Equal(tt.want, subnet.Config()) 141 }) 142 } 143 } 144 145 func TestSubnetsBootstrapping(t *testing.T) { 146 require := require.New(t) 147 148 config := map[ids.ID]subnets.Config{ 149 constants.PrimaryNetworkID: {}, 150 } 151 152 subnets, err := NewSubnets(ids.EmptyNodeID, config) 153 require.NoError(err) 154 155 subnetID := ids.GenerateTestID() 156 chainID := ids.GenerateTestID() 157 158 subnet, ok := subnets.GetOrCreate(subnetID) 159 require.True(ok) 160 161 // Start bootstrapping 162 subnet.AddChain(chainID) 163 bootstrapping := subnets.Bootstrapping() 164 require.Contains(bootstrapping, subnetID) 165 166 // Finish bootstrapping 167 subnet.Bootstrapped(chainID) 168 require.Empty(subnets.Bootstrapping()) 169 }