11import os
22import shutil
33
4+ from argparse import Namespace , ArgumentParser
5+ from typing import List , Tuple , Dict , Any
46from blspy import ExtendedPrivateKey
57from src .types .BLSSignature import BLSPublicKey
68from src .consensus .coinbase import create_puzzlehash_for_pk
9+ from src .util .config import unflatten_properties
710from pathlib import Path
811
912from src .util .config import (
1922from src .ssl .create_ssl import generate_selfsigned_cert
2023
2124
22- def make_parser (parser ):
25+ def make_parser (parser : ArgumentParser ):
2326 parser .set_defaults (function = init )
2427
2528
26- def dict_add_new_default (updated , default ):
29+ def dict_add_new_default (
30+ updated : Dict , default : Dict , do_not_migrate_keys : Dict [str , Any ]
31+ ):
2732 for k , v in default .items ():
2833 if isinstance (v , dict ) and k in updated :
29- dict_add_new_default (updated [k ], default [k ])
30- elif k not in updated :
31- updated [k ] = default [k ]
34+ # If there is an intermediate key with empty string value, do not migrate all decendants
35+ if do_not_migrate_keys .get (k , None ) == "" :
36+ do_not_migrate_keys [k ] = v
37+ dict_add_new_default (updated [k ], default [k ], do_not_migrate_keys .get (k , {}))
38+ elif k not in updated or k in do_not_migrate_keys :
39+ updated [k ] = v
3240
3341
34- def migrate_from (old_root , new_root , manifest ):
42+ def migrate_from (
43+ old_root : Path , new_root : Path , manifest : List [str ], do_not_migrate_keys : List [str ]
44+ ):
3545 """
3646 Copy all the files in "manifest" to the new config directory.
3747 """
@@ -55,26 +65,28 @@ def migrate_from(old_root, new_root, manifest):
5565 not_found .append (f )
5666 print (f"{ old_path } not found, skipping" )
5767 # update config yaml with new keys
58- config = load_config (new_root , "config.yaml" )
59- config_str = initial_config_file ("config.yaml" )
60- default_config = yaml .safe_load (config_str )
61- dict_add_new_default (config , default_config )
68+ config : Dict = load_config (new_root , "config.yaml" )
69+ config_str : str = initial_config_file ("config.yaml" )
70+ default_config : Dict = yaml .safe_load (config_str )
71+ flattened_keys = unflatten_properties ({k : "" for k in do_not_migrate_keys })
72+ dict_add_new_default (config , default_config , flattened_keys )
6273
6374 save_config (new_root , "config.yaml" , config )
75+
6476 # migrate plots
6577 # for now, we simply leave them where they are
6678 # and make what may have been relative paths absolute
6779 if "config/trusted.key" in not_found or "config/trusted.key" in not_found :
6880 initialize_ssl (new_root )
6981
70- plots_config = load_config (new_root , "plots.yaml" )
82+ plots_config : Dict = load_config (new_root , "plots.yaml" )
7183
7284 plot_root = (
7385 load_config (new_root , "config.yaml" ).get ("harvester" , {}).get ("plot_root" , "." )
7486 )
7587
76- old_plots_root = path_from_root (old_root , plot_root )
77- new_plots_root = path_from_root (new_root , plot_root )
88+ old_plots_root : Path = path_from_root (old_root , plot_root )
89+ new_plots_root : Path = path_from_root (new_root , plot_root )
7890
7991 old_plot_paths = plots_config .get ("plots" , {})
8092 if len (old_plot_paths ) == 0 :
@@ -83,13 +95,13 @@ def migrate_from(old_root, new_root, manifest):
8395
8496 print ("\n migrating plots.yaml" )
8597
86- new_plot_paths = {}
98+ new_plot_paths : Dict = {}
8799 for path , values in old_plot_paths .items ():
88100 old_path_full = path_from_root (old_plots_root , path )
89101 new_path_relative = make_path_relative (old_path_full , new_plots_root )
90102 print (f"rewriting { path } \n as { new_path_relative } " )
91103 new_plot_paths [str (new_path_relative )] = values
92- plots_config_new = {"plots" : new_plot_paths }
104+ plots_config_new : Dict = {"plots" : new_plot_paths }
93105 save_config (new_root , "plots.yaml" , plots_config_new )
94106 print ("\n Updated plots.yaml to point to where your existing plots are." )
95107 print (
@@ -117,7 +129,7 @@ def migrate_from(old_root, new_root, manifest):
117129 return 1
118130
119131
120- def initialize_ssl (root_path ):
132+ def initialize_ssl (root_path : Path ):
121133 cert , key = generate_selfsigned_cert ()
122134 path_crt = config_path_for_filename (root_path , "trusted.crt" )
123135 path_key = config_path_for_filename (root_path , "trusted.key" )
@@ -127,32 +139,39 @@ def initialize_ssl(root_path):
127139 f .write (key )
128140
129141
130- def init (args , parser ):
142+ def init (args : Namespace , parser : ArgumentParser ):
131143 return chia_init (args )
132144
133145
134- def chia_init (args ):
135- root_path = args .root_path
146+ def chia_init (args : Namespace ):
147+ root_path : Path = args .root_path
136148 print (f"migrating to { root_path } " )
137149 if root_path .is_dir ():
138150 print (f"{ root_path } already exists, no action taken" )
139151 return - 1
140152
141- MANIFEST = [
153+ # These are the config keys that will not be migrated, and instead the default is used
154+ DO_NOT_MIGRATE_KEYS : List [str ] = [
155+ "full_node.introducer_peer" ,
156+ "wallet.introducer_peer" ,
157+ ]
158+
159+ # These are the files that will be migrated
160+ MANIFEST : List [str ] = [
142161 "config/config.yaml" ,
143162 "config/plots.yaml" ,
144163 "config/keys.yaml" ,
145164 "config/trusted.crt" ,
146165 "config/trusted.key" ,
147166 ]
148167
149- PATH_MANIFEST_LIST = [
168+ PATH_MANIFEST_LIST : List [ Tuple [ Path , List [ str ]]] = [
150169 (Path (os .path .expanduser ("~/.chia/beta-%s" % _ )), MANIFEST )
151170 for _ in ["1.0b3" , "1.0b2" , "1.0b1" ]
152171 ]
153172
154173 for old_path , manifest in PATH_MANIFEST_LIST :
155- r = migrate_from (old_path , root_path , manifest )
174+ r = migrate_from (old_path , root_path , manifest , DO_NOT_MIGRATE_KEYS )
156175 if r :
157176 break
158177 else :
0 commit comments