-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update-versions-v1.11.0-fuji
- Loading branch information
Showing
60 changed files
with
2,463 additions
and
744 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package chains | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/subnets" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
) | ||
|
||
var ErrNoPrimaryNetworkConfig = errors.New("no subnet config for primary network found") | ||
|
||
// Subnets holds the currently running subnets on this node | ||
type Subnets struct { | ||
nodeID ids.NodeID | ||
configs map[ids.ID]subnets.Config | ||
|
||
lock sync.RWMutex | ||
subnets map[ids.ID]subnets.Subnet | ||
} | ||
|
||
// GetOrCreate returns a subnet running on this node, or creates one if it was | ||
// not running before. Returns the subnet and if the subnet was created. | ||
func (s *Subnets) GetOrCreate(subnetID ids.ID) (subnets.Subnet, bool) { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
if subnet, ok := s.subnets[subnetID]; ok { | ||
return subnet, false | ||
} | ||
|
||
// Default to the primary network config if a subnet config was not | ||
// specified | ||
config, ok := s.configs[subnetID] | ||
if !ok { | ||
config = s.configs[constants.PrimaryNetworkID] | ||
} | ||
|
||
subnet := subnets.New(s.nodeID, config) | ||
s.subnets[subnetID] = subnet | ||
|
||
return subnet, true | ||
} | ||
|
||
// Bootstrapping returns the subnetIDs of any chains that are still | ||
// bootstrapping. | ||
func (s *Subnets) Bootstrapping() []ids.ID { | ||
s.lock.RLock() | ||
defer s.lock.RUnlock() | ||
|
||
subnetsBootstrapping := make([]ids.ID, 0, len(s.subnets)) | ||
for subnetID, subnet := range s.subnets { | ||
if !subnet.IsBootstrapped() { | ||
subnetsBootstrapping = append(subnetsBootstrapping, subnetID) | ||
} | ||
} | ||
|
||
return subnetsBootstrapping | ||
} | ||
|
||
// NewSubnets returns an instance of Subnets | ||
func NewSubnets( | ||
nodeID ids.NodeID, | ||
configs map[ids.ID]subnets.Config, | ||
) (*Subnets, error) { | ||
if _, ok := configs[constants.PrimaryNetworkID]; !ok { | ||
return nil, ErrNoPrimaryNetworkConfig | ||
} | ||
|
||
s := &Subnets{ | ||
nodeID: nodeID, | ||
configs: configs, | ||
subnets: make(map[ids.ID]subnets.Subnet), | ||
} | ||
|
||
_, _ = s.GetOrCreate(constants.PrimaryNetworkID) | ||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package chains | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/subnets" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
) | ||
|
||
func TestNewSubnets(t *testing.T) { | ||
require := require.New(t) | ||
config := map[ids.ID]subnets.Config{ | ||
constants.PrimaryNetworkID: {}, | ||
} | ||
|
||
subnets, err := NewSubnets(ids.EmptyNodeID, config) | ||
require.NoError(err) | ||
|
||
subnet, ok := subnets.GetOrCreate(constants.PrimaryNetworkID) | ||
require.False(ok) | ||
require.Equal(config[constants.PrimaryNetworkID], subnet.Config()) | ||
} | ||
|
||
func TestNewSubnetsNoPrimaryNetworkConfig(t *testing.T) { | ||
require := require.New(t) | ||
config := map[ids.ID]subnets.Config{} | ||
|
||
_, err := NewSubnets(ids.EmptyNodeID, config) | ||
require.ErrorIs(err, ErrNoPrimaryNetworkConfig) | ||
} | ||
|
||
func TestSubnetsGetOrCreate(t *testing.T) { | ||
testSubnetID := ids.GenerateTestID() | ||
|
||
type args struct { | ||
subnetID ids.ID | ||
want bool | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args []args | ||
}{ | ||
{ | ||
name: "adding duplicate subnet is a noop", | ||
args: []args{ | ||
{ | ||
subnetID: testSubnetID, | ||
want: true, | ||
}, | ||
{ | ||
subnetID: testSubnetID, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "adding unique subnets succeeds", | ||
args: []args{ | ||
{ | ||
subnetID: ids.GenerateTestID(), | ||
want: true, | ||
}, | ||
{ | ||
subnetID: ids.GenerateTestID(), | ||
want: true, | ||
}, | ||
{ | ||
subnetID: ids.GenerateTestID(), | ||
want: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
config := map[ids.ID]subnets.Config{ | ||
constants.PrimaryNetworkID: {}, | ||
} | ||
subnets, err := NewSubnets(ids.EmptyNodeID, config) | ||
require.NoError(err) | ||
|
||
for _, arg := range tt.args { | ||
_, got := subnets.GetOrCreate(arg.subnetID) | ||
require.Equal(arg.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSubnetConfigs(t *testing.T) { | ||
testSubnetID := ids.GenerateTestID() | ||
|
||
tests := []struct { | ||
name string | ||
config map[ids.ID]subnets.Config | ||
subnetID ids.ID | ||
want subnets.Config | ||
}{ | ||
{ | ||
name: "default to primary network config", | ||
config: map[ids.ID]subnets.Config{ | ||
constants.PrimaryNetworkID: {}, | ||
}, | ||
subnetID: testSubnetID, | ||
want: subnets.Config{}, | ||
}, | ||
{ | ||
name: "use subnet config", | ||
config: map[ids.ID]subnets.Config{ | ||
constants.PrimaryNetworkID: {}, | ||
testSubnetID: { | ||
GossipConfig: subnets.GossipConfig{ | ||
AcceptedFrontierValidatorSize: 123456789, | ||
}, | ||
}, | ||
}, | ||
subnetID: testSubnetID, | ||
want: subnets.Config{ | ||
GossipConfig: subnets.GossipConfig{ | ||
AcceptedFrontierValidatorSize: 123456789, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
subnets, err := NewSubnets(ids.EmptyNodeID, tt.config) | ||
require.NoError(err) | ||
|
||
subnet, ok := subnets.GetOrCreate(tt.subnetID) | ||
require.True(ok) | ||
|
||
require.Equal(tt.want, subnet.Config()) | ||
}) | ||
} | ||
} | ||
|
||
func TestSubnetsBootstrapping(t *testing.T) { | ||
require := require.New(t) | ||
|
||
config := map[ids.ID]subnets.Config{ | ||
constants.PrimaryNetworkID: {}, | ||
} | ||
|
||
subnets, err := NewSubnets(ids.EmptyNodeID, config) | ||
require.NoError(err) | ||
|
||
subnetID := ids.GenerateTestID() | ||
chainID := ids.GenerateTestID() | ||
|
||
subnet, ok := subnets.GetOrCreate(subnetID) | ||
require.True(ok) | ||
|
||
// Start bootstrapping | ||
subnet.AddChain(chainID) | ||
bootstrapping := subnets.Bootstrapping() | ||
require.Contains(bootstrapping, subnetID) | ||
|
||
// Finish bootstrapping | ||
subnet.Bootstrapped(chainID) | ||
require.Empty(subnets.Bootstrapping()) | ||
} |
Oops, something went wrong.