Skip to content

Commit 0554b91

Browse files
authored
[tmpnet] Switch FlagsMap from map[string]any to map[string]string (#3884)
1 parent 3400f10 commit 0554b91

File tree

14 files changed

+72
-143
lines changed

14 files changed

+72
-143
lines changed

tests/antithesis/compose.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11-
"strconv"
1211

1312
"github.com/compose-spec/compose-go/types"
1413
"gopkg.in/yaml.v3"
@@ -97,10 +96,7 @@ func initComposeConfig(
9796
targetPath string,
9897
) error {
9998
// Generate a compose project for the specified network
100-
project, err := newComposeProject(network, nodeImageName, workloadImageName)
101-
if err != nil {
102-
return fmt.Errorf("failed to create compose project: %w", err)
103-
}
99+
project := newComposeProject(network, nodeImageName, workloadImageName)
104100

105101
absPath, err := filepath.Abs(targetPath)
106102
if err != nil {
@@ -135,7 +131,7 @@ func initComposeConfig(
135131

136132
// Create a new docker compose project for an antithesis test setup
137133
// for the provided network configuration.
138-
func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadImageName string) (*types.Project, error) {
134+
func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadImageName string) *types.Project {
139135
networkName := "avalanche-testnet"
140136
baseNetworkAddress := "10.0.20"
141137

@@ -148,18 +144,9 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm
148144
for i, node := range network.Nodes {
149145
address := fmt.Sprintf("%s.%d", baseNetworkAddress, 3+i)
150146

151-
tlsKey, err := node.Flags.GetStringVal(config.StakingTLSKeyContentKey)
152-
if err != nil {
153-
return nil, err
154-
}
155-
tlsCert, err := node.Flags.GetStringVal(config.StakingCertContentKey)
156-
if err != nil {
157-
return nil, err
158-
}
159-
signerKey, err := node.Flags.GetStringVal(config.StakingSignerKeyContentKey)
160-
if err != nil {
161-
return nil, err
162-
}
147+
tlsKey := node.Flags[config.StakingTLSKeyContentKey]
148+
tlsCert := node.Flags[config.StakingCertContentKey]
149+
signerKey := node.Flags[config.StakingSignerKeyContentKey]
163150

164151
env := types.Mapping{
165152
config.NetworkNameKey: constants.LocalName,
@@ -175,14 +162,7 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm
175162

176163
// Apply configuration appropriate to a test network
177164
for k, v := range tmpnet.DefaultTestFlags() {
178-
switch value := v.(type) {
179-
case string:
180-
env[k] = value
181-
case bool:
182-
env[k] = strconv.FormatBool(value)
183-
default:
184-
return nil, fmt.Errorf("unable to convert unsupported type %T to string", v)
185-
}
165+
env[k] = v
186166
}
187167

188168
serviceName := getServiceName(i)
@@ -195,10 +175,7 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm
195175
},
196176
}
197177

198-
trackSubnets, err := node.Flags.GetStringVal(config.TrackSubnetsKey)
199-
if err != nil {
200-
return nil, err
201-
}
178+
trackSubnets := node.Flags[config.TrackSubnetsKey]
202179
if len(trackSubnets) > 0 {
203180
env[config.TrackSubnetsKey] = trackSubnets
204181
if i == bootstrapIndex {
@@ -282,7 +259,7 @@ func newComposeProject(network *tmpnet.Network, nodeImageName string, workloadIm
282259
},
283260
},
284261
Services: services,
285-
}, nil
262+
}
286263
}
287264

288265
// Convert a mapping of avalanche config keys to a mapping of env vars

tests/e2e/p/interchain_workflow.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/onsi/ginkgo/v2"
11-
"github.com/spf13/cast"
1211
"github.com/stretchr/testify/require"
1312

1413
"github.com/ava-labs/avalanchego/api/info"
@@ -44,8 +43,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
4443
)
4544

4645
tc.By("checking that the network has a compatible minimum stake duration", func() {
47-
minStakeDuration := cast.ToDuration(network.DefaultFlags[config.MinStakeDurationKey])
48-
require.Equal(tmpnet.DefaultMinStakeDuration, minStakeDuration)
46+
require.Equal(tmpnet.DefaultMinStakeDuration, network.DefaultFlags[config.MinStakeDurationKey])
4947
})
5048

5149
tc.By("creating wallet with a funded key to send from and recipient key to deliver to")

tests/e2e/p/staking_rewards.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/mitchellh/mapstructure"
1010
"github.com/onsi/ginkgo/v2"
11-
"github.com/spf13/cast"
1211
"github.com/stretchr/testify/require"
1312
"go.uber.org/zap"
1413

@@ -50,8 +49,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
5049
)
5150

5251
tc.By("checking that the network has a compatible minimum stake duration", func() {
53-
minStakeDuration := cast.ToDuration(network.DefaultFlags[config.MinStakeDurationKey])
54-
require.Equal(tmpnet.DefaultMinStakeDuration, minStakeDuration)
52+
require.Equal(tmpnet.DefaultMinStakeDuration, network.DefaultFlags[config.MinStakeDurationKey])
5553
})
5654

5755
tc.By("adding alpha node, whose uptime should result in a staking reward")

tests/fixture/subnet/xsvm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewXSVMOrPanic(name string, key *secp256k1.PrivateKey, nodes ...*tmpnet.Nod
3333

3434
return &tmpnet.Subnet{
3535
Name: name,
36-
Config: tmpnet.FlagsMap{
36+
Config: tmpnet.ConfigMap{
3737
// Reducing this from the 1s default speeds up tx acceptance
3838
"proposerMinBlockDelay": 0,
3939
},

tests/fixture/tmpnet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ the following non-test files:
6969
| defaults.go | | Defines common default configuration |
7070
| detached_process_default.go | | Configures detached processes for darwin and linux |
7171
| detached_process_windows.go | | No-op detached process configuration for windows |
72-
| flags.go | FlagsMap | Simplifies configuration of avalanchego flags |
72+
| flagsmap.go | FlagsMap | Simplifies configuration of avalanchego flags |
7373
| genesis.go | | Creates test genesis |
7474
| kube.go | | Library for Kubernetes interaction |
7575
| local_network.go | | Defines configuration for the default local network |

tests/fixture/tmpnet/check_monitoring.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ func getSelectors(networkUUID string) (string, error) {
273273
selectors := []string{}
274274
githubLabels := githubLabelsFromEnv()
275275
for label := range githubLabels {
276-
value, err := githubLabels.GetStringVal(label)
277-
if err != nil {
278-
return "", err
279-
}
276+
value := githubLabels[label]
280277
if len(value) == 0 {
281278
continue
282279
}

tests/fixture/tmpnet/defaults.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030
DefaultPreFundedKeyCount = 50
3131

3232
// A short minimum stake duration enables testing of staking logic.
33-
DefaultMinStakeDuration = time.Second
33+
DefaultMinStakeDuration = "1s"
3434

3535
defaultConfigFilename = "config.json"
3636
)
@@ -41,8 +41,8 @@ func DefaultTestFlags() FlagsMap {
4141
config.NetworkPeerListPullGossipFreqKey: "250ms",
4242
config.NetworkMaxReconnectDelayKey: "1s",
4343
config.HealthCheckFreqKey: "2s",
44-
config.AdminAPIEnabledKey: true,
45-
config.IndexEnabledKey: true,
44+
config.AdminAPIEnabledKey: "true",
45+
config.IndexEnabledKey: "true",
4646
}
4747
}
4848

@@ -60,7 +60,7 @@ func DefaultTmpnetFlags() FlagsMap {
6060
config.LogDisplayLevelKey: logging.Off.String(), // Display logging not needed since nodes run headless
6161
config.LogLevelKey: logging.Debug.String(),
6262
// Specific to e2e testing
63-
config.ProposerVMUseCurrentHeightKey: true,
63+
config.ProposerVMUseCurrentHeightKey: "true",
6464
// Reducing this from the 1s default speeds up tx acceptance
6565
config.ProposerVMMinBlockDelayKey: "0s",
6666
}
@@ -69,14 +69,14 @@ func DefaultTmpnetFlags() FlagsMap {
6969
}
7070

7171
// A set of chain configurations appropriate for testing.
72-
func DefaultChainConfigs() map[string]FlagsMap {
73-
return map[string]FlagsMap{
72+
func DefaultChainConfigs() map[string]ConfigMap {
73+
return map[string]ConfigMap{
7474
// Supply only non-default configuration to ensure that default
7575
// values will be used. Available C-Chain configuration options are
7676
// defined in the `github.com/ava-labs/coreth/evm` package.
7777
"C": {
7878
"warp-api-enabled": true,
79-
"log-level": "trace",
79+
"log-level": logging.Trace.String(),
8080
},
8181
}
8282
}

tests/fixture/tmpnet/flags.go renamed to tests/fixture/tmpnet/flagsmap.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import (
88
"fmt"
99
"os"
1010

11-
"github.com/spf13/cast"
12-
1311
"github.com/ava-labs/avalanchego/utils/perms"
1412
)
1513

1614
// Defines a mapping of flag keys to values intended to be supplied to
1715
// an invocation of an AvalancheGo node.
18-
type FlagsMap map[string]interface{}
16+
type FlagsMap map[string]string
1917

2018
// Utility function simplifying construction of a FlagsMap from a file.
2119
func ReadFlagsMap(path string, description string) (FlagsMap, error) {
@@ -32,7 +30,7 @@ func ReadFlagsMap(path string, description string) (FlagsMap, error) {
3230

3331
// SetDefault ensures the effectiveness of a flag override by only
3432
// setting a value supplied whose key is not already explicitly set.
35-
func (f FlagsMap) SetDefault(key string, value any) {
33+
func (f FlagsMap) SetDefault(key string, value string) {
3634
if _, ok := f[key]; !ok {
3735
f[key] = value
3836
}
@@ -47,34 +45,6 @@ func (f FlagsMap) SetDefaults(defaults FlagsMap) {
4745
}
4846
}
4947

50-
// GetStringVal simplifies retrieving a map value as a string.
51-
func (f FlagsMap) GetStringVal(key string) (string, error) {
52-
rawVal, ok := f[key]
53-
if !ok {
54-
return "", nil
55-
}
56-
57-
val, err := cast.ToStringE(rawVal)
58-
if err != nil {
59-
return "", fmt.Errorf("failed to cast value for %q: %w", key, err)
60-
}
61-
return val, nil
62-
}
63-
64-
// GetBoolVal simplifies retrieving a map value as a bool.
65-
func (f FlagsMap) GetBoolVal(key string, defaultVal bool) (bool, error) {
66-
rawVal, ok := f[key]
67-
if !ok {
68-
return defaultVal, nil
69-
}
70-
71-
val, err := cast.ToBoolE(rawVal)
72-
if err != nil {
73-
return false, fmt.Errorf("failed to cast value for %q: %w", key, err)
74-
}
75-
return val, nil
76-
}
77-
7848
// Write simplifies writing a FlagsMap to the provided path. The
7949
// description is used in error messages.
8050
func (f FlagsMap) Write(path string, description string) error {

tests/fixture/tmpnet/network.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func init() {
7979
}
8080
}
8181

82+
// ConfigMap enables defining configuration in a format appropriate
83+
// for round-tripping through JSON back to golang structs.
84+
type ConfigMap map[string]any
85+
8286
// Collects the configuration for running a temporary avalanchego network
8387
type Network struct {
8488
// Uniquely identifies the temporary network for metrics
@@ -107,10 +111,10 @@ type Network struct {
107111
Genesis *genesis.UnparsedConfig
108112

109113
// Configuration for primary subnets
110-
PrimarySubnetConfig FlagsMap
114+
PrimarySubnetConfig ConfigMap
111115

112116
// Configuration for primary network chains (P, X, C)
113-
PrimaryChainConfigs map[string]FlagsMap
117+
PrimaryChainConfigs map[string]ConfigMap
114118

115119
// Default configuration to use when creating new nodes
116120
DefaultFlags FlagsMap
@@ -229,14 +233,19 @@ func (n *Network) EnsureDefaultConfig(log logging.Logger) error {
229233

230234
// Ensure primary chains are configured
231235
if n.PrimaryChainConfigs == nil {
232-
n.PrimaryChainConfigs = map[string]FlagsMap{}
236+
n.PrimaryChainConfigs = map[string]ConfigMap{}
233237
}
234238
defaultChainConfigs := DefaultChainConfigs()
235-
for alias, chainConfig := range defaultChainConfigs {
239+
for alias, defaultChainConfig := range defaultChainConfigs {
236240
if _, ok := n.PrimaryChainConfigs[alias]; !ok {
237-
n.PrimaryChainConfigs[alias] = FlagsMap{}
241+
n.PrimaryChainConfigs[alias] = ConfigMap{}
242+
}
243+
primaryChainConfig := n.PrimaryChainConfigs[alias]
244+
for key, value := range defaultChainConfig {
245+
if _, ok := primaryChainConfig[key]; !ok {
246+
primaryChainConfig[key] = value
247+
}
238248
}
239-
n.PrimaryChainConfigs[alias].SetDefaults(chainConfig)
240249
}
241250

242251
return nil
@@ -377,8 +386,8 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
377386
// The node that will be used to create subnets and bootstrap the network
378387
bootstrapNode := n.Nodes[0]
379388

380-
// Whether sybil protection will need to be re-enabled after subnet creation
381-
reEnableSybilProtection := false
389+
// An existing sybil protection value that may need to be restored after subnet creation
390+
var existingSybilProtectionValue *string
382391

383392
if len(n.Nodes) > 1 {
384393
// Reduce the cost of subnet creation for a network of multiple nodes by
@@ -389,14 +398,11 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
389398
log.Info("starting a single-node network with sybil protection disabled for quicker subnet creation")
390399

391400
// If sybil protection is enabled, it should be re-enabled before the node is used to bootstrap the other nodes
392-
var err error
393-
reEnableSybilProtection, err = bootstrapNode.Flags.GetBoolVal(config.SybilProtectionEnabledKey, true)
394-
if err != nil {
395-
return fmt.Errorf("failed to read sybil protection flag: %w", err)
401+
if value, ok := bootstrapNode.Flags[config.SybilProtectionEnabledKey]; ok {
402+
existingSybilProtectionValue = &value
396403
}
397-
398404
// Ensure sybil protection is disabled for the bootstrap node.
399-
bootstrapNode.Flags[config.SybilProtectionEnabledKey] = false
405+
bootstrapNode.Flags[config.SybilProtectionEnabledKey] = "false"
400406
}
401407

402408
if err := n.StartNodes(ctx, log, bootstrapNode); err != nil {
@@ -413,11 +419,17 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
413419
return err
414420
}
415421

416-
if reEnableSybilProtection {
422+
if existingSybilProtectionValue == nil {
417423
log.Info("re-enabling sybil protection",
418424
zap.Stringer("nodeID", bootstrapNode.NodeID),
419425
)
420426
delete(bootstrapNode.Flags, config.SybilProtectionEnabledKey)
427+
} else {
428+
log.Info("restoring previous sybil protection value",
429+
zap.Stringer("nodeID", bootstrapNode.NodeID),
430+
zap.String("sybilProtectionEnabled", *existingSybilProtectionValue),
431+
)
432+
bootstrapNode.Flags[config.SybilProtectionEnabledKey] = *existingSybilProtectionValue
421433
}
422434

423435
log.Info("restarting bootstrap node",
@@ -640,10 +652,7 @@ func (n *Network) CreateSubnets(ctx context.Context, log logging.Logger, apiURI
640652

641653
reconfiguredNodes := []*Node{}
642654
for _, node := range n.Nodes {
643-
existingTrackedSubnets, err := node.Flags.GetStringVal(config.TrackSubnetsKey)
644-
if err != nil {
645-
return err
646-
}
655+
existingTrackedSubnets := node.Flags[config.TrackSubnetsKey]
647656
trackedSubnets := n.TrackedSubnetsForNode(node.NodeID)
648657
if existingTrackedSubnets == trackedSubnets {
649658
continue
@@ -801,7 +810,7 @@ func (n *Network) GetGenesisFileContent() (string, error) {
801810
// GetSubnetConfigContent returns the base64-encoded and
802811
// JSON-marshaled map of subnetID to subnet configuration.
803812
func (n *Network) GetSubnetConfigContent() (string, error) {
804-
subnetConfigs := map[ids.ID]FlagsMap{}
813+
subnetConfigs := map[ids.ID]ConfigMap{}
805814

806815
if len(n.PrimarySubnetConfig) > 0 {
807816
subnetConfigs[constants.PrimaryNetworkID] = n.PrimarySubnetConfig
@@ -894,7 +903,7 @@ func (n *Network) writeNodeFlags(log logging.Logger, node *Node) error {
894903
isSingleNodeNetwork := (len(n.Nodes) == 1 && len(n.Genesis.InitialStakers) == 1)
895904
if isSingleNodeNetwork {
896905
log.Info("defaulting to sybil protection disabled to enable a single-node network to start")
897-
flags.SetDefault(config.SybilProtectionEnabledKey, false)
906+
flags.SetDefault(config.SybilProtectionEnabledKey, "false")
898907
}
899908
}
900909

tests/fixture/tmpnet/network_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ func (n *Network) readConfig() error {
152152
type serializedNetworkConfig struct {
153153
UUID string `json:"uuid,omitempty"`
154154
Owner string `json:"owner,omitempty"`
155-
PrimarySubnetConfig FlagsMap `json:"primarySubnetConfig,omitempty"`
156-
PrimaryChainConfigs map[string]FlagsMap `json:"primaryChainConfigs,omitempty"`
155+
PrimarySubnetConfig ConfigMap `json:"primarySubnetConfig,omitempty"`
156+
PrimaryChainConfigs map[string]ConfigMap `json:"primaryChainConfigs,omitempty"`
157157
DefaultFlags FlagsMap `json:"defaultFlags,omitempty"`
158158
DefaultRuntimeConfig NodeRuntimeConfig `json:"defaultRuntimeConfig,omitempty"`
159159
PreFundedKeys []*secp256k1.PrivateKey `json:"preFundedKeys,omitempty"`

0 commit comments

Comments
 (0)