Skip to content

Commit ea75c47

Browse files
committed
[tmpnet] Misc cleanup to support xsvm warp test PR
- update tmpnet.ReadFlagsMap to avoid returning an unnecessary pointer - add tmpnet.InterfaceToFlagsMap helper to simplify conversion of subnet configuration to a FlagsMap for nicer serialization - update tmpnet.NewNodes to initialize tls and signing keys so that nodes returned by this method can be used to extract the validator IDs for a subnet - switch tmpnet.Chain.Genesis back to a byte array. A previous attempt to improve the readability of tmpnet subnet configuration by converting to FlagsMap ignored that genesis byte format is VM-specific.
1 parent 9a0c852 commit ea75c47

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

tests/fixture/tmpnet/flags.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,34 @@ import (
1818
type FlagsMap map[string]interface{}
1919

2020
// Utility function simplifying construction of a FlagsMap from a file.
21-
func ReadFlagsMap(path string, description string) (*FlagsMap, error) {
21+
func ReadFlagsMap(path string, description string) (FlagsMap, error) {
2222
bytes, err := os.ReadFile(path)
2323
if err != nil {
2424
return nil, fmt.Errorf("failed to read %s: %w", description, err)
2525
}
26-
flagsMap := &FlagsMap{}
27-
if err := json.Unmarshal(bytes, flagsMap); err != nil {
26+
flagsMap := FlagsMap{}
27+
if err := json.Unmarshal(bytes, &flagsMap); err != nil {
2828
return nil, fmt.Errorf("failed to unmarshal %s: %w", description, err)
2929
}
3030
return flagsMap, nil
3131
}
3232

33+
// Round-trips the provided interface through JSON to convert to a
34+
// FlagsMap. This allows for a cleaner serialization of embedded types
35+
// such as chain configuration.
36+
func InterfaceToFlagsMap(v interface{}) (FlagsMap, error) {
37+
bytes, err := json.Marshal(v)
38+
if err != nil {
39+
return nil, err
40+
}
41+
flagsMap := FlagsMap{}
42+
err = json.Unmarshal(bytes, &flagsMap)
43+
if err != nil {
44+
return nil, err
45+
}
46+
return flagsMap, nil
47+
}
48+
3349
// SetDefaults ensures the effectiveness of flag overrides by only
3450
// setting values supplied in the defaults map that are not already
3551
// explicitly set.

tests/fixture/tmpnet/network.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ func (n *Network) EnsureDefaultConfig(w io.Writer, avalancheGoPath string, plugi
213213

214214
// Ensure nodes are created
215215
if len(n.Nodes) == 0 {
216-
n.Nodes = NewNodes(nodeCount)
216+
nodes, err := NewNodes(nodeCount)
217+
if err != nil {
218+
return err
219+
}
220+
n.Nodes = nodes
217221
}
218222

219223
// Ensure nodes are configured

tests/fixture/tmpnet/network_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (n *Network) readChainConfigs() error {
140140
if err != nil {
141141
return err
142142
}
143-
n.ChainConfigs[chainAlias] = *chainConfig
143+
n.ChainConfigs[chainAlias] = chainConfig
144144
}
145145

146146
return nil

tests/fixture/tmpnet/node.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@ func NewNode(dataDir string) *Node {
9595
}
9696

9797
// Initializes the specified number of nodes.
98-
func NewNodes(count int) []*Node {
98+
func NewNodes(count int) ([]*Node, error) {
9999
nodes := make([]*Node, count)
100100
for i := range nodes {
101-
nodes[i] = NewNode("")
101+
node := NewNode("")
102+
if err := node.EnsureKeys(); err != nil {
103+
return nil, err
104+
}
105+
nodes[i] = node
102106
}
103-
return nodes
107+
return nodes, nil
104108
}
105109

106110
// Reads a node's configuration from the specified directory.

tests/fixture/tmpnet/subnet.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Chain struct {
3131
// Set statically
3232
VMID ids.ID
3333
Config FlagsMap
34-
Genesis FlagsMap
34+
Genesis []byte
3535

3636
// Set at runtime
3737
ChainID ids.ID
@@ -138,13 +138,9 @@ func (s *Subnet) CreateChains(ctx context.Context, w io.Writer, uri string) erro
138138
}
139139

140140
for _, chain := range s.Chains {
141-
genesisBytes, err := DefaultJSONMarshal(chain.Genesis)
142-
if err != nil {
143-
return fmt.Errorf("failed to marshal genesis for chain %s: %w", chain.VMID, err)
144-
}
145141
createChainTx, err := pWallet.IssueCreateChainTx(
146142
s.SubnetID,
147-
genesisBytes,
143+
chain.Genesis,
148144
chain.VMID,
149145
nil,
150146
"",

0 commit comments

Comments
 (0)