Skip to content

Commit 00b6831

Browse files
committed
always persist network conf
1 parent 69de78b commit 00b6831

File tree

3 files changed

+103
-77
lines changed

3 files changed

+103
-77
lines changed

local/blockchain.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (ln *localNetwork) CreateBlockchains(
118118
return nil, err
119119
}
120120

121-
if err := ln.RegisterBlockchainAliases(ctx, chainInfos, chainSpecs); err != nil {
121+
if err := ln.registerBlockchainAliases(ctx, chainInfos, chainSpecs); err != nil {
122122
return nil, err
123123
}
124124

@@ -127,11 +127,11 @@ func (ln *localNetwork) CreateBlockchains(
127127
chainIDs = append(chainIDs, chainInfo.blockchainID)
128128
}
129129

130-
return chainIDs, nil
130+
return chainIDs, ln.persistNetwork()
131131
}
132132

133133
// if alias is defined in blockchain-specs, registers an alias for the previously created blockchain
134-
func (ln *localNetwork) RegisterBlockchainAliases(
134+
func (ln *localNetwork) registerBlockchainAliases(
135135
ctx context.Context,
136136
chainInfos []blockchainInfo,
137137
chainSpecs []network.BlockchainSpec,
@@ -179,7 +179,10 @@ func (ln *localNetwork) AddSubnetValidators(
179179
ln.lock.Lock()
180180
defer ln.lock.Unlock()
181181

182-
return ln.addSubnetValidators(ctx, subnetSpecs)
182+
if err := ln.addSubnetValidators(ctx, subnetSpecs); err != nil {
183+
return err
184+
}
185+
return ln.persistNetwork()
183186
}
184187

185188
func (ln *localNetwork) RemoveSubnetValidators(
@@ -189,7 +192,10 @@ func (ln *localNetwork) RemoveSubnetValidators(
189192
ln.lock.Lock()
190193
defer ln.lock.Unlock()
191194

192-
return ln.removeSubnetValidators(ctx, subnetSpecs)
195+
if err := ln.removeSubnetValidators(ctx, subnetSpecs); err != nil {
196+
return err
197+
}
198+
return ln.persistNetwork()
193199
}
194200

195201
func (ln *localNetwork) AddPermissionlessValidators(
@@ -199,7 +205,10 @@ func (ln *localNetwork) AddPermissionlessValidators(
199205
ln.lock.Lock()
200206
defer ln.lock.Unlock()
201207

202-
return ln.addPermissionlessValidators(ctx, validatorSpec)
208+
if err := ln.addPermissionlessValidators(ctx, validatorSpec); err != nil {
209+
return err
210+
}
211+
return ln.persistNetwork()
203212
}
204213

205214
func (ln *localNetwork) AddPermissionlessDelegators(
@@ -209,7 +218,10 @@ func (ln *localNetwork) AddPermissionlessDelegators(
209218
ln.lock.Lock()
210219
defer ln.lock.Unlock()
211220

212-
return ln.addPermissionlessDelegators(ctx, delegatorSpecs)
221+
if err := ln.addPermissionlessDelegators(ctx, delegatorSpecs); err != nil {
222+
return err
223+
}
224+
return ln.persistNetwork()
213225
}
214226

215227
func (ln *localNetwork) TransformSubnet(
@@ -219,7 +231,11 @@ func (ln *localNetwork) TransformSubnet(
219231
ln.lock.Lock()
220232
defer ln.lock.Unlock()
221233

222-
return ln.transformToElasticSubnets(ctx, elasticSubnetConfig)
234+
elasticSubnetIDs, assetIDs, err := ln.transformToElasticSubnets(ctx, elasticSubnetConfig)
235+
if err != nil {
236+
return elasticSubnetIDs, assetIDs, err
237+
}
238+
return elasticSubnetIDs, assetIDs, ln.persistNetwork()
223239
}
224240

225241
func (ln *localNetwork) CreateSubnets(
@@ -229,7 +245,11 @@ func (ln *localNetwork) CreateSubnets(
229245
ln.lock.Lock()
230246
defer ln.lock.Unlock()
231247

232-
return ln.installSubnets(ctx, subnetSpecs)
248+
subnetIDs, err := ln.installSubnets(ctx, subnetSpecs)
249+
if err != nil {
250+
return subnetIDs, err
251+
}
252+
return subnetIDs, ln.persistNetwork()
233253
}
234254

235255
// provisions local cluster and install custom chains if applicable

local/network.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,11 @@ func (ln *localNetwork) AddNode(nodeConfig node.Config) (node.Node, error) {
464464
return nil, network.ErrStopped
465465
}
466466

467-
return ln.addNode(nodeConfig)
467+
node, err := ln.addNode(nodeConfig)
468+
if err != nil {
469+
return node, err
470+
}
471+
return node, ln.persistNetwork()
468472
}
469473

470474
// Assumes [ln.lock] is held and [ln.Stop] hasn't been called.
@@ -765,7 +769,10 @@ func (ln *localNetwork) RemoveNode(ctx context.Context, nodeName string) error {
765769
if ln.stopCalled() {
766770
return network.ErrStopped
767771
}
768-
return ln.removeNode(ctx, nodeName)
772+
if err := ln.removeNode(ctx, nodeName); err != nil {
773+
return err
774+
}
775+
return ln.persistNetwork()
769776
}
770777

771778
// Assumes [ln.lock] is held.
@@ -800,7 +807,10 @@ func (ln *localNetwork) PauseNode(ctx context.Context, nodeName string) error {
800807
if ln.stopCalled() {
801808
return network.ErrStopped
802809
}
803-
return ln.pauseNode(ctx, nodeName)
810+
if err := ln.pauseNode(ctx, nodeName); err != nil {
811+
return err
812+
}
813+
return ln.persistNetwork()
804814
}
805815

806816
// Assumes [ln.lock] is held.
@@ -831,10 +841,10 @@ func (ln *localNetwork) ResumeNode(
831841
ln.lock.Lock()
832842
defer ln.lock.Unlock()
833843

834-
return ln.resumeNode(
835-
ctx,
836-
nodeName,
837-
)
844+
if err := ln.resumeNode(ctx, nodeName); err != nil {
845+
return err
846+
}
847+
return ln.persistNetwork()
838848
}
839849

840850
// Assumes [ln.lock] is held.
@@ -876,7 +886,7 @@ func (ln *localNetwork) RestartNode(
876886
ln.lock.Lock()
877887
defer ln.lock.Unlock()
878888

879-
return ln.restartNode(
889+
if err := ln.restartNode(
880890
ctx,
881891
nodeName,
882892
binaryPath,
@@ -885,7 +895,10 @@ func (ln *localNetwork) RestartNode(
885895
chainConfigs,
886896
upgradeConfigs,
887897
subnetConfigs,
888-
)
898+
); err != nil {
899+
return err
900+
}
901+
return ln.persistNetwork()
889902
}
890903

891904
func (ln *localNetwork) restartNode(

local/snapshot.go

+52-59
Original file line numberDiff line numberDiff line change
@@ -105,90 +105,55 @@ func NewNetworkFromSnapshot(
105105
return net, err
106106
}
107107

108-
// Save network snapshot
109-
// Network is stopped in order to do a safe preservation
110-
func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (string, error) {
111-
ln.lock.Lock()
112-
defer ln.lock.Unlock()
113-
114-
if ln.stopCalled() {
115-
return "", network.ErrStopped
116-
}
117-
if len(snapshotName) == 0 {
118-
return "", fmt.Errorf("invalid snapshotName %q", snapshotName)
119-
}
120-
// check if snapshot already exists
121-
snapshotDir := filepath.Join(ln.snapshotsDir, snapshotPrefix+snapshotName)
122-
if _, err := os.Stat(snapshotDir); err == nil {
123-
return "", fmt.Errorf("snapshot %q already exists", snapshotName)
124-
}
125-
// keep copy of node info that will be removed by stop
126-
nodesConfig := map[string]node.Config{}
127-
nodesDataDir := map[string]string{}
108+
// Save network conf + state into json at root dir
109+
func (ln *localNetwork) persistNetwork() error {
110+
// clone network flags
111+
networkConfigFlags := maps.Clone(ln.flags)
112+
// remove data dir, log dir references
113+
delete(networkConfigFlags, config.DataDirKey)
114+
delete(networkConfigFlags, config.LogsDirKey)
115+
// clone node info
116+
nodeConfigs := []node.Config{}
128117
for nodeName, node := range ln.nodes {
129118
nodeConfig := node.config
130119
// depending on how the user generated the config, different nodes config flags
131120
// may point to the same map, so we made a copy to avoid always modifying the same value
132121
nodeConfig.Flags = maps.Clone(nodeConfig.Flags)
133-
nodesConfig[nodeName] = nodeConfig
134-
nodesDataDir[nodeName] = node.GetDataDir()
135-
}
136-
// we change nodeConfig.Flags so as to preserve in snapshot the current node ports
137-
for nodeName, nodeConfig := range nodesConfig {
122+
// preserve the current node ports
138123
nodeConfig.Flags[config.HTTPPortKey] = ln.nodes[nodeName].GetAPIPort()
139124
nodeConfig.Flags[config.StakingPortKey] = ln.nodes[nodeName].GetP2PPort()
140-
}
141-
// make copy of network flags
142-
networkConfigFlags := maps.Clone(ln.flags)
143-
// remove all data dir, log dir references
144-
delete(networkConfigFlags, config.DataDirKey)
145-
delete(networkConfigFlags, config.LogsDirKey)
146-
for nodeName, nodeConfig := range nodesConfig {
125+
// remove data dir, log dir references
147126
if nodeConfig.ConfigFile != "" {
148127
var err error
149128
nodeConfig.ConfigFile, err = utils.SetJSONKey(nodeConfig.ConfigFile, config.LogsDirKey, "")
150129
if err != nil {
151-
return "", err
130+
return err
131+
}
132+
nodeConfig.ConfigFile, err = utils.SetJSONKey(nodeConfig.ConfigFile, config.DataDirKey, "")
133+
if err != nil {
134+
return err
152135
}
153136
}
154-
delete(nodeConfig.Flags, config.DataDirKey)
155137
delete(nodeConfig.Flags, config.LogsDirKey)
156-
nodesConfig[nodeName] = nodeConfig
157-
}
158-
159-
// stop network to safely save snapshot
160-
if err := ln.stop(ctx); err != nil {
161-
return "", err
162-
}
163-
// create main snapshot dirs
164-
if err := os.MkdirAll(snapshotDir, os.ModePerm); err != nil {
165-
return "", err
166-
}
167-
// save data dir
168-
for nodeName, nodeDataDir := range nodesDataDir {
169-
if err := dircopy.Copy(nodeDataDir, filepath.Join(snapshotDir, nodeName)); err != nil {
170-
return "", fmt.Errorf("failure saving node %q data dir: %w", nodeName, err)
171-
}
138+
delete(nodeConfig.Flags, config.DataDirKey)
139+
nodeConfigs = append(nodeConfigs, nodeConfig)
172140
}
173141
// save network conf
174142
networkConfig := network.Config{
175143
Genesis: string(ln.genesis),
176144
Flags: networkConfigFlags,
177-
NodeConfigs: []node.Config{},
145+
NodeConfigs: nodeConfigs,
178146
BinaryPath: ln.binaryPath,
179147
ChainConfigFiles: ln.chainConfigFiles,
180148
UpgradeConfigFiles: ln.upgradeConfigFiles,
181149
SubnetConfigFiles: ln.subnetConfigFiles,
182150
}
183-
184-
// no need to save this, will be generated automatically on snapshot load
185-
networkConfig.NodeConfigs = append(networkConfig.NodeConfigs, maps.Values(nodesConfig)...)
186151
networkConfigJSON, err := json.MarshalIndent(networkConfig, "", " ")
187152
if err != nil {
188-
return "", err
153+
return err
189154
}
190-
if err := createFileAndWrite(filepath.Join(snapshotDir, "network.json"), networkConfigJSON); err != nil {
191-
return "", err
155+
if err := createFileAndWrite(filepath.Join(ln.rootDir, "network.json"), networkConfigJSON); err != nil {
156+
return err
192157
}
193158
// save dynamic part of network not available on blockchain
194159
subnetID2ElasticSubnetID := map[string]string{}
@@ -201,11 +166,39 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
201166
}
202167
networkStateJSON, err := json.MarshalIndent(networkState, "", " ")
203168
if err != nil {
169+
return err
170+
}
171+
return createFileAndWrite(filepath.Join(ln.rootDir, "state.json"), networkStateJSON)
172+
}
173+
174+
// Save network snapshot
175+
// Network is stopped in order to do a safe preservation
176+
func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (string, error) {
177+
ln.lock.Lock()
178+
defer ln.lock.Unlock()
179+
180+
if ln.stopCalled() {
181+
return "", network.ErrStopped
182+
}
183+
if len(snapshotName) == 0 {
184+
return "", fmt.Errorf("invalid snapshotName %q", snapshotName)
185+
}
186+
// check if snapshot already exists
187+
snapshotDir := filepath.Join(ln.snapshotsDir, snapshotPrefix+snapshotName)
188+
if _, err := os.Stat(snapshotDir); err == nil {
189+
return "", fmt.Errorf("snapshot %q already exists", snapshotName)
190+
}
191+
if err := ln.persistNetwork(); err != nil {
204192
return "", err
205193
}
206-
if err := createFileAndWrite(filepath.Join(snapshotDir, "state.json"), networkStateJSON); err != nil {
194+
// stop network to safely save snapshot
195+
if err := ln.stop(ctx); err != nil {
207196
return "", err
208197
}
198+
// copy all info
199+
if err := dircopy.Copy(ln.rootDir, snapshotDir); err != nil {
200+
return "", fmt.Errorf("failure saving data dir %s: %w", ln.rootDir, err)
201+
}
209202
return snapshotDir, nil
210203
}
211204

@@ -375,7 +368,7 @@ func (ln *localNetwork) loadSnapshot(
375368
ln.log.Warn(err.Error())
376369
}
377370
}
378-
return nil
371+
return ln.persistNetwork()
379372
}
380373

381374
// Remove network snapshot

0 commit comments

Comments
 (0)