@@ -105,90 +105,55 @@ func NewNetworkFromSnapshot(
105
105
return net , err
106
106
}
107
107
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 {}
128
117
for nodeName , node := range ln .nodes {
129
118
nodeConfig := node .config
130
119
// depending on how the user generated the config, different nodes config flags
131
120
// may point to the same map, so we made a copy to avoid always modifying the same value
132
121
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
138
123
nodeConfig .Flags [config .HTTPPortKey ] = ln .nodes [nodeName ].GetAPIPort ()
139
124
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
147
126
if nodeConfig .ConfigFile != "" {
148
127
var err error
149
128
nodeConfig .ConfigFile , err = utils .SetJSONKey (nodeConfig .ConfigFile , config .LogsDirKey , "" )
150
129
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
152
135
}
153
136
}
154
- delete (nodeConfig .Flags , config .DataDirKey )
155
137
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 )
172
140
}
173
141
// save network conf
174
142
networkConfig := network.Config {
175
143
Genesis : string (ln .genesis ),
176
144
Flags : networkConfigFlags ,
177
- NodeConfigs : []node. Config {} ,
145
+ NodeConfigs : nodeConfigs ,
178
146
BinaryPath : ln .binaryPath ,
179
147
ChainConfigFiles : ln .chainConfigFiles ,
180
148
UpgradeConfigFiles : ln .upgradeConfigFiles ,
181
149
SubnetConfigFiles : ln .subnetConfigFiles ,
182
150
}
183
-
184
- // no need to save this, will be generated automatically on snapshot load
185
- networkConfig .NodeConfigs = append (networkConfig .NodeConfigs , maps .Values (nodesConfig )... )
186
151
networkConfigJSON , err := json .MarshalIndent (networkConfig , "" , " " )
187
152
if err != nil {
188
- return "" , err
153
+ return err
189
154
}
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
192
157
}
193
158
// save dynamic part of network not available on blockchain
194
159
subnetID2ElasticSubnetID := map [string ]string {}
@@ -201,11 +166,39 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
201
166
}
202
167
networkStateJSON , err := json .MarshalIndent (networkState , "" , " " )
203
168
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 {
204
192
return "" , err
205
193
}
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 {
207
196
return "" , err
208
197
}
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
+ }
209
202
return snapshotDir , nil
210
203
}
211
204
@@ -375,7 +368,7 @@ func (ln *localNetwork) loadSnapshot(
375
368
ln .log .Warn (err .Error ())
376
369
}
377
370
}
378
- return nil
371
+ return ln . persistNetwork ()
379
372
}
380
373
381
374
// Remove network snapshot
0 commit comments