Skip to content

Commit a813297

Browse files
authored
Merge pull request #731 from ava-labs/make-fuji-snapshot-to-work
add snapshot save/load for fuji
2 parents 57301db + c098336 commit a813297

File tree

7 files changed

+362
-314
lines changed

7 files changed

+362
-314
lines changed

client/client.go

+3
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ func (c *client) LoadSnapshot(ctx context.Context, snapshotName string, inPlace
399399
if ret.globalNodeConfig != "" {
400400
req.GlobalNodeConfig = &ret.globalNodeConfig
401401
}
402+
if ret.walletPrivateKey != "" {
403+
req.WalletPrivateKey = ret.walletPrivateKey
404+
}
402405
req.ReassignPortsIfUsed = &ret.reassignPortsIfUsed
403406
return c.controlc.LoadSnapshot(ctx, &req)
404407
}

cmd/control/control.go

+42-19
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ func newStartCommand() *cobra.Command {
289289
return cmd
290290
}
291291

292+
func setWalletPrivateKeyOptions(opts *[]client.OpOption) error {
293+
if walletPrivateKeyPath != "" && walletPrivateKey != "" {
294+
return fmt.Errorf("only one of wallet-private-key and wallet-private-key-path can be provided")
295+
}
296+
if walletPrivateKey != "" {
297+
ux.Print(log, logging.Yellow.Wrap("funding wallet private key provided: %s"), walletPrivateKey)
298+
*opts = append(*opts, client.WithWalletPrivateKey(walletPrivateKey))
299+
}
300+
if walletPrivateKeyPath != "" {
301+
ux.Print(log, logging.Yellow.Wrap("funding wallet private key path provided: %s"), walletPrivateKeyPath)
302+
// validate if it's a valid private key
303+
if _, err := os.Stat(walletPrivateKey); err != nil {
304+
return fmt.Errorf("wallet private key doesn't exist: %w", err)
305+
}
306+
// read the private key
307+
keyBytes, err := os.ReadFile(walletPrivateKey)
308+
if err != nil {
309+
return fmt.Errorf("failed to read wallet private key: %w", err)
310+
}
311+
*opts = append(*opts, client.WithWalletPrivateKey(string(keyBytes)))
312+
}
313+
return nil
314+
}
315+
292316
func startFunc(*cobra.Command, []string) error {
293317
cli, err := newClient()
294318
if err != nil {
@@ -309,25 +333,8 @@ func startFunc(*cobra.Command, []string) error {
309333
client.WithNetworkID(networkID),
310334
}
311335

312-
if walletPrivateKeyPath != "" && walletPrivateKey != "" {
313-
return fmt.Errorf("only one of wallet-private-key and wallet-private-key-path can be provided")
314-
}
315-
if walletPrivateKey != "" {
316-
ux.Print(log, logging.Yellow.Wrap("funding wallet private key provided: %s"), walletPrivateKey)
317-
opts = append(opts, client.WithWalletPrivateKey(walletPrivateKey))
318-
}
319-
if walletPrivateKeyPath != "" {
320-
ux.Print(log, logging.Yellow.Wrap("funding wallet private key path provided: %s"), walletPrivateKeyPath)
321-
// validate if it's a valid private key
322-
if _, err := os.Stat(walletPrivateKey); err != nil {
323-
return fmt.Errorf("wallet private key doesn't exist: %w", err)
324-
}
325-
// read the private key
326-
keyBytes, err := os.ReadFile(walletPrivateKey)
327-
if err != nil {
328-
return fmt.Errorf("failed to read wallet private key: %w", err)
329-
}
330-
opts = append(opts, client.WithWalletPrivateKey(string(keyBytes)))
336+
if err := setWalletPrivateKeyOptions(&opts); err != nil {
337+
return err
331338
}
332339

333340
if globalNodeConfig != "" {
@@ -1341,6 +1348,18 @@ func newLoadSnapshotCommand() *cobra.Command {
13411348
false,
13421349
"load snapshot in place, so as it always auto save",
13431350
)
1351+
cmd.PersistentFlags().StringVar(
1352+
&walletPrivateKey,
1353+
"wallet-private-key",
1354+
"",
1355+
"[optional] funding wallet private key. Please consider using `wallet-private-key-path` if security is a concern.",
1356+
)
1357+
cmd.PersistentFlags().StringVar(
1358+
&walletPrivateKeyPath,
1359+
"wallet-private-key-path",
1360+
"",
1361+
"[optional] funding wallet private key path",
1362+
)
13441363
return cmd
13451364
}
13461365

@@ -1358,6 +1377,10 @@ func loadSnapshotFunc(_ *cobra.Command, args []string) error {
13581377
client.WithReassignPortsIfUsed(reassignPortsIfUsed),
13591378
}
13601379

1380+
if err := setWalletPrivateKeyOptions(&opts); err != nil {
1381+
return err
1382+
}
1383+
13611384
if chainConfigs != "" {
13621385
chainConfigsMap := make(map[string]string)
13631386
if err := json.Unmarshal([]byte(chainConfigs), &chainConfigsMap); err != nil {

local/snapshot.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (ln *localNetwork) persistNetwork() error {
152152
}
153153
// save network conf
154154
networkConfig := network.Config{
155+
NetworkID: ln.networkID,
155156
Genesis: string(ln.genesis),
156157
Flags: networkConfigFlags,
157158
NodeConfigs: nodeConfigs,
@@ -388,18 +389,20 @@ func (ln *localNetwork) loadSnapshot(
388389
}
389390
}
390391
// add aliases for blockchain names
391-
node := ln.getNode()
392-
blockchains, err := node.GetAPIClient().PChainAPI().GetBlockchains(ctx)
393-
if err != nil {
394-
return err
395-
}
396-
for _, blockchain := range blockchains {
397-
if blockchain.Name == "C-Chain" || blockchain.Name == "X-Chain" {
398-
continue
392+
if !utils.IsPublicNetwork(ln.networkID) {
393+
node := ln.getNode()
394+
blockchains, err := node.GetAPIClient().PChainAPI().GetBlockchains(ctx)
395+
if err != nil {
396+
return err
399397
}
400-
if err := ln.setBlockchainAlias(ctx, blockchain.ID.String(), blockchain.Name); err != nil {
401-
// non fatal error: not required by user
402-
ln.log.Warn(err.Error())
398+
for _, blockchain := range blockchains {
399+
if blockchain.Name == "C-Chain" || blockchain.Name == "X-Chain" {
400+
continue
401+
}
402+
if err := ln.setBlockchainAlias(ctx, blockchain.ID.String(), blockchain.Name); err != nil {
403+
// non fatal error: not required by user
404+
ln.log.Warn(err.Error())
405+
}
403406
}
404407
}
405408
return ln.persistNetwork()

0 commit comments

Comments
 (0)