Skip to content

Commit 1edd5c5

Browse files
committed
fixup: Respond to more reviewer feedback
1 parent 9d3759a commit 1edd5c5

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

tests/fixture/tmpnet/network.go

+25-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ava-labs/avalanchego/genesis"
2929
"github.com/ava-labs/avalanchego/ids"
3030
"github.com/ava-labs/avalanchego/subnets"
31+
"github.com/ava-labs/avalanchego/utils/constants"
3132
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
3233
"github.com/ava-labs/avalanchego/utils/logging"
3334
"github.com/ava-labs/avalanchego/utils/perms"
@@ -106,7 +107,7 @@ type Network struct {
106107
Genesis *genesis.UnparsedConfig
107108

108109
// Configuration for primary subnets
109-
PrimarySubnetConfigs map[ids.ID]subnets.Config
110+
PrimarySubnetConfig *subnets.Config
110111

111112
// Configuration for primary network chains (P, X, C)
112113
PrimaryChainConfigs map[string]FlagsMap
@@ -197,6 +198,9 @@ func ReadNetwork(dir string) (*Network, error) {
197198
if err := network.Read(); err != nil {
198199
return nil, fmt.Errorf("failed to read network: %w", err)
199200
}
201+
if network.DefaultFlags == nil {
202+
network.DefaultFlags = FlagsMap{}
203+
}
200204
return network, nil
201205
}
202206

@@ -212,17 +216,9 @@ func (n *Network) EnsureDefaultConfig(log logging.Logger, avalancheGoPath string
212216
n.UUID = uuid.NewString()
213217
}
214218

215-
// Ensure default flags
216219
if n.DefaultFlags == nil {
217220
n.DefaultFlags = FlagsMap{}
218221
}
219-
n.DefaultFlags.SetDefaults(DefaultTmpnetFlags())
220-
221-
if len(n.Nodes) == 1 {
222-
// Sybil protection needs to be disabled for a single node network to start
223-
log.Info("starting a single-node network with sybil protection disabled")
224-
n.DefaultFlags[config.SybilProtectionEnabledKey] = false
225-
}
226222

227223
// Only configure the plugin dir with a non-empty value to ensure
228224
// the use of the default value (`[datadir]/plugins`) when
@@ -505,7 +501,7 @@ func (n *Network) StartNode(ctx context.Context, log logging.Logger, node *Node)
505501
return err
506502
}
507503

508-
if err := n.writeNodeFlags(node); err != nil {
504+
if err := n.writeNodeFlags(log, node); err != nil {
509505
return fmt.Errorf("writing node flags: %w", err)
510506
}
511507

@@ -769,12 +765,12 @@ func (n *Network) CreateSubnets(ctx context.Context, log logging.Logger, apiURI
769765
return err
770766
}
771767

772-
// Persist the subnet
773768
if err := subnet.Write(n.GetSubnetDir()); err != nil {
774769
return err
775770
}
776-
log.Info("wrote subnet",
771+
log.Info("wrote subnet configuration",
777772
zap.String("name", subnet.Name),
773+
zap.Stringer("id", subnet.SubnetID),
778774
)
779775

780776
// If one or more of the subnets chains have explicit configuration, the
@@ -877,7 +873,11 @@ func (n *Network) GetGenesisFileContent() (string, error) {
877873
// GetSubnetConfigContent returns the base64-encoded and
878874
// JSON-marshaled map of subnetID to subnet configuration.
879875
func (n *Network) GetSubnetConfigContent() (string, error) {
880-
subnetConfigs := maps.Clone(n.PrimarySubnetConfigs)
876+
subnetConfigs := map[ids.ID]subnets.Config{}
877+
878+
if n.PrimarySubnetConfig != nil {
879+
subnetConfigs[constants.PrimaryNetworkID] = *n.PrimarySubnetConfig
880+
}
881881

882882
// Collect configuration for non-primary subnets
883883
for _, subnet := range n.Subnets {
@@ -940,22 +940,24 @@ func (n *Network) GetChainConfigContent() (string, error) {
940940

941941
// writeNodeFlags determines the set of flags that should be used to
942942
// start the given node and writes them to a file in the node path.
943-
func (n *Network) writeNodeFlags(node *Node) error {
943+
func (n *Network) writeNodeFlags(log logging.Logger, node *Node) error {
944944
flags := maps.Clone(node.Flags)
945945

946946
// Convert the network id to a string to ensure consistency in JSON round-tripping.
947-
flags[config.NetworkNameKey] = strconv.FormatUint(uint64(n.GetNetworkID()), 10)
947+
flags.SetDefault(config.NetworkNameKey, strconv.FormatUint(uint64(n.GetNetworkID()), 10))
948948

949-
// Set the network defaults
950-
flags.SetDefaults(n.DefaultFlags)
949+
if n.Genesis != nil && len(n.Genesis.InitialStakers) == 1 {
950+
log.Info("disabling sybil protection to enable a single-node network to start")
951+
flags[config.SybilProtectionEnabledKey] = false
952+
}
951953

952954
// Set the bootstrap configuration
953955
bootstrapIPs, bootstrapIDs, err := n.GetBootstrapIPsAndIDs(node)
954956
if err != nil {
955957
return fmt.Errorf("failed to determine bootstrap configuration: %w", err)
956958
}
957-
flags[config.BootstrapIDsKey] = strings.Join(bootstrapIDs, ",")
958-
flags[config.BootstrapIPsKey] = strings.Join(bootstrapIPs, ",")
959+
flags.SetDefault(config.BootstrapIDsKey, strings.Join(bootstrapIDs, ","))
960+
flags.SetDefault(config.BootstrapIPsKey, strings.Join(bootstrapIPs, ","))
959961

960962
// TODO(marun) Maybe avoid computing content flags for each node start?
961963

@@ -983,6 +985,10 @@ func (n *Network) writeNodeFlags(node *Node) error {
983985
flags.SetDefault(config.ChainConfigContentKey, chainConfigContent)
984986
}
985987

988+
// Set the network and tmpnet defaults last to ensure they can be overridden
989+
flags.SetDefaults(n.DefaultFlags)
990+
flags.SetDefaults(DefaultTmpnetFlags())
991+
986992
// Write the flags to disk
987993
return node.writeFlags(flags)
988994
}

tests/fixture/tmpnet/network_config.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"path/filepath"
1212

1313
"github.com/ava-labs/avalanchego/genesis"
14-
"github.com/ava-labs/avalanchego/ids"
1514
"github.com/ava-labs/avalanchego/subnets"
1615
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
1716
"github.com/ava-labs/avalanchego/utils/perms"
@@ -123,20 +122,20 @@ func (n *Network) readConfig() error {
123122

124123
// The subset of network fields to store in the network config file.
125124
type serializedNetworkConfig struct {
126-
UUID string
127-
Owner string
128-
PrimarySubnetConfigs map[ids.ID]subnets.Config
129-
PrimaryChainConfigs map[string]FlagsMap
130-
DefaultFlags FlagsMap
131-
DefaultRuntimeConfig NodeRuntimeConfig
132-
PreFundedKeys []*secp256k1.PrivateKey
125+
UUID string `json:",omitempty"`
126+
Owner string `json:",omitempty"`
127+
PrimarySubnetConfig *subnets.Config `json:",omitempty"`
128+
PrimaryChainConfigs map[string]FlagsMap `json:",omitempty"`
129+
DefaultFlags FlagsMap `json:",omitempty"`
130+
DefaultRuntimeConfig NodeRuntimeConfig `json:",omitempty"`
131+
PreFundedKeys []*secp256k1.PrivateKey `json:",omitempty"`
133132
}
134133

135134
func (n *Network) writeNetworkConfig() error {
136135
config := &serializedNetworkConfig{
137136
UUID: n.UUID,
138137
Owner: n.Owner,
139-
PrimarySubnetConfigs: n.PrimarySubnetConfigs,
138+
PrimarySubnetConfig: n.PrimarySubnetConfig,
140139
PrimaryChainConfigs: n.PrimaryChainConfigs,
141140
DefaultFlags: n.DefaultFlags,
142141
DefaultRuntimeConfig: n.DefaultRuntimeConfig,

tests/fixture/tmpnet/network_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import (
88

99
"github.com/stretchr/testify/require"
1010

11+
"github.com/ava-labs/avalanchego/ids"
12+
"github.com/ava-labs/avalanchego/subnets"
1113
"github.com/ava-labs/avalanchego/utils/logging"
14+
"github.com/ava-labs/avalanchego/utils/set"
1215
)
1316

1417
func TestNetworkSerialization(t *testing.T) {
@@ -17,6 +20,11 @@ func TestNetworkSerialization(t *testing.T) {
1720
tmpDir := t.TempDir()
1821

1922
network := NewDefaultNetwork("testnet")
23+
// Validate round-tripping of primary subnet configuration
24+
network.PrimarySubnetConfig = &subnets.Config{
25+
ValidatorOnly: true,
26+
AllowedNodes: set.Set[ids.NodeID]{},
27+
}
2028
require.NoError(network.EnsureDefaultConfig(logging.NoLog{}, "/path/to/avalanche/go", ""))
2129
require.NoError(network.Create(tmpDir))
2230
// Ensure node runtime is initialized

0 commit comments

Comments
 (0)