11import os
22import shutil
33
4+ from argparse import Namespace , ArgumentParser
5+ from typing import List , Tuple , Dict , Optional , 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+ ):
32+ print (
33+ "Calling with " ,
34+ updated .keys (),
35+ "and" ,
36+ default .keys (),
37+ do_not_migrate_keys .keys (),
38+ )
2739 for k , v in default .items ():
2840 if isinstance (v , dict ) and k in updated :
29- dict_add_new_default (updated [k ], default [k ])
30- elif k not in updated :
41+ dict_add_new_default (updated [k ], default [k ], do_not_migrate_keys . get ( k , {}) )
42+ elif k not in updated or k in do_not_migrate_keys :
3143 updated [k ] = default [k ]
3244
3345
34- def migrate_from (old_root , new_root , manifest ):
46+ def migrate_from (
47+ old_root : Path , new_root : Path , manifest : List [str ], do_not_migrate_keys : List [str ]
48+ ):
3549 """
3650 Copy all the files in "manifest" to the new config directory.
3751 """
@@ -55,26 +69,29 @@ def migrate_from(old_root, new_root, manifest):
5569 not_found .append (f )
5670 print (f"{ old_path } not found, skipping" )
5771 # 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 )
72+ config : Dict = load_config (new_root , "config.yaml" )
73+ config_str : str = initial_config_file ("config.yaml" )
74+ default_config : Dict = yaml .safe_load (config_str )
75+ flattened_keys = unflatten_properties ({k : "" for k in do_not_migrate_keys })
76+ print ("FLATTENED" , flattened_keys )
77+ dict_add_new_default (config , default_config , flattened_keys )
6278
6379 save_config (new_root , "config.yaml" , config )
80+
6481 # migrate plots
6582 # for now, we simply leave them where they are
6683 # and make what may have been relative paths absolute
6784 if "config/trusted.key" in not_found or "config/trusted.key" in not_found :
6885 initialize_ssl (new_root )
6986
70- plots_config = load_config (new_root , "plots.yaml" )
87+ plots_config : Dict = load_config (new_root , "plots.yaml" )
7188
7289 plot_root = (
7390 load_config (new_root , "config.yaml" ).get ("harvester" , {}).get ("plot_root" , "." )
7491 )
7592
76- old_plots_root = path_from_root (old_root , plot_root )
77- new_plots_root = path_from_root (new_root , plot_root )
93+ old_plots_root : Path = path_from_root (old_root , plot_root )
94+ new_plots_root : Path = path_from_root (new_root , plot_root )
7895
7996 old_plot_paths = plots_config .get ("plots" , {})
8097 if len (old_plot_paths ) == 0 :
@@ -83,13 +100,13 @@ def migrate_from(old_root, new_root, manifest):
83100
84101 print ("\n migrating plots.yaml" )
85102
86- new_plot_paths = {}
103+ new_plot_paths : Dict = {}
87104 for path , values in old_plot_paths .items ():
88105 old_path_full = path_from_root (old_plots_root , path )
89106 new_path_relative = make_path_relative (old_path_full , new_plots_root )
90107 print (f"rewriting { path } \n as { new_path_relative } " )
91108 new_plot_paths [str (new_path_relative )] = values
92- plots_config_new = {"plots" : new_plot_paths }
109+ plots_config_new : Dict = {"plots" : new_plot_paths }
93110 save_config (new_root , "plots.yaml" , plots_config_new )
94111 print ("\n Updated plots.yaml to point to where your existing plots are." )
95112 print (
@@ -117,7 +134,7 @@ def migrate_from(old_root, new_root, manifest):
117134 return 1
118135
119136
120- def initialize_ssl (root_path ):
137+ def initialize_ssl (root_path : Path ):
121138 cert , key = generate_selfsigned_cert ()
122139 path_crt = config_path_for_filename (root_path , "trusted.crt" )
123140 path_key = config_path_for_filename (root_path , "trusted.key" )
@@ -127,32 +144,39 @@ def initialize_ssl(root_path):
127144 f .write (key )
128145
129146
130- def init (args , parser ):
147+ def init (args : Namespace , parser : ArgumentParser ):
131148 return chia_init (args )
132149
133150
134- def chia_init (args ):
135- root_path = args .root_path
151+ def chia_init (args : Namespace ):
152+ root_path : Path = args .root_path
136153 print (f"migrating to { root_path } " )
137154 if root_path .is_dir ():
138155 print (f"{ root_path } already exists, no action taken" )
139156 return - 1
140157
141- MANIFEST = [
158+ # These are the config keys that will not be migrated, and instead the default is used
159+ DO_NOT_MIGRATE_KEYS : List [str ] = [
160+ "full_node.introducer_peer.host" ,
161+ "wallet.introducer_peer.host" ,
162+ ]
163+
164+ # These are the files that will be migrated
165+ MANIFEST : List [str ] = [
142166 "config/config.yaml" ,
143167 "config/plots.yaml" ,
144168 "config/keys.yaml" ,
145169 "config/trusted.crt" ,
146170 "config/trusted.key" ,
147171 ]
148172
149- PATH_MANIFEST_LIST = [
173+ PATH_MANIFEST_LIST : List [ Tuple [ Path , List [ str ]]] = [
150174 (Path (os .path .expanduser ("~/.chia/beta-%s" % _ )), MANIFEST )
151175 for _ in ["1.0b3" , "1.0b2" , "1.0b1" ]
152176 ]
153177
154178 for old_path , manifest in PATH_MANIFEST_LIST :
155- r = migrate_from (old_path , root_path , manifest )
179+ r = migrate_from (old_path , root_path , manifest , DO_NOT_MIGRATE_KEYS )
156180 if r :
157181 break
158182 else :
0 commit comments