1414 limitations under the License.
1515*/
1616
17- #include " db_checklist .hpp"
17+ #include " chain_data_init .hpp"
1818
1919#include < regex>
2020
2424#include < silkworm/db/stages.hpp>
2525#include < silkworm/infra/common/log.hpp>
2626
27- namespace silkworm ::cmd::common {
28-
29- void run_db_checklist (NodeSettings& node_settings, bool init_if_empty) {
30- node_settings.data_directory ->deploy (); // Ensures all subdirs are present
31- bool chaindata_exclusive{node_settings.chaindata_env_config .exclusive }; // Save setting
32- {
33- auto & config = node_settings.chaindata_env_config ;
34- config.path = node_settings.data_directory ->chaindata ().path ().string ();
35- config.create =
36- !std::filesystem::exists (db::get_datafile_path (node_settings.data_directory ->chaindata ().path ()));
37- config.exclusive = true ; // Will be cleared after this phase
38- }
27+ namespace silkworm ::db {
28+
29+ ChainConfig chain_data_init (const ChainDataInitSettings& node_settings) {
30+ // Output mdbx build info
31+ log::Debug (
32+ " libmdbx" ,
33+ {
34+ " version" ,
35+ mdbx::get_version ().git .describe ,
36+ " build" ,
37+ mdbx::get_build ().target ,
38+ " compiler" ,
39+ mdbx::get_build ().compiler ,
40+ });
41+
42+ auto chaindata_env_config = node_settings.chaindata_env_config ;
43+ chaindata_env_config.create = !std::filesystem::exists (db::get_datafile_path (chaindata_env_config.path ));
44+ chaindata_env_config.exclusive = true ;
3945
4046 // Open chaindata environment and check tables are consistent
41- log::Info (" Opening database" , {" path" , node_settings. data_directory -> chaindata (). path (). string () });
42- auto chaindata_env{ db::open_env (node_settings. chaindata_env_config )} ;
47+ log::Info (" Opening database" , {" path" , chaindata_env_config. path });
48+ mdbx::env_managed chaindata_env = db::open_env (chaindata_env_config);
4349 db::RWTxnManaged tx (chaindata_env);
4450
4551 // Ensures all tables are present
@@ -51,9 +57,10 @@ void run_db_checklist(NodeSettings& node_settings, bool init_if_empty) {
5157 const auto header_download_progress{db::stages::read_stage_progress (tx, db::stages::kHeadersKey )};
5258
5359 // Check db is initialized with chain config
60+ std::optional<ChainConfig> chain_config;
5461 {
55- node_settings. chain_config = db::read_chain_config (tx);
56- if (!node_settings. chain_config .has_value () && init_if_empty) {
62+ chain_config = db::read_chain_config (tx);
63+ if (!chain_config.has_value () && node_settings. init_if_empty ) {
5764 auto source_data{read_genesis_data (node_settings.network_id )};
5865 auto genesis_json = nlohmann::json::parse (source_data, nullptr , /* allow_exceptions = */ false );
5966 if (genesis_json.is_discarded ()) {
@@ -63,26 +70,26 @@ void run_db_checklist(NodeSettings& node_settings, bool init_if_empty) {
6370 log::Message (" Priming database" , {" network id" , std::to_string (node_settings.network_id )});
6471 db::initialize_genesis (tx, genesis_json, /* allow_exceptions=*/ true );
6572 tx.commit_and_renew ();
66- node_settings. chain_config = db::read_chain_config (tx);
73+ chain_config = db::read_chain_config (tx);
6774 }
6875
69- if (!node_settings. chain_config .has_value ()) {
76+ if (!chain_config.has_value ()) {
7077 throw std::runtime_error (" Unable to retrieve chain configuration" );
7178 }
7279
73- const ChainId chain_id{node_settings. chain_config ->chain_id } ;
80+ const ChainId chain_id = chain_config->chain_id ;
7481 if (chain_id != node_settings.network_id ) {
7582 throw std::runtime_error (" Incompatible network id. Command line expects " +
7683 std::to_string (node_settings.network_id ) + " ; Database has " +
7784 std::to_string (chain_id));
7885 }
7986
8087 const auto known_chain{kKnownChainConfigs .find (chain_id)};
81- if (known_chain && **known_chain != *(node_settings. chain_config ) ) {
88+ if (known_chain && **known_chain != *chain_config) {
8289 // If loaded config is known we must ensure is up-to-date with hardcoded one
8390 // Loop all respective JSON members to find discrepancies
8491 auto known_chain_config_json{(*known_chain)->to_json ()};
85- auto active_chain_config_json{node_settings. chain_config ->to_json ()} ;
92+ auto active_chain_config_json = chain_config->to_json ();
8693 bool new_members_added{false };
8794 bool old_members_changed (false );
8895 for (auto & [known_key, known_value] : known_chain_config_json.items ()) {
@@ -152,17 +159,17 @@ void run_db_checklist(NodeSettings& node_settings, bool init_if_empty) {
152159 if (new_members_added || old_members_changed) {
153160 db::update_chain_config (tx, **known_chain);
154161 tx.commit_and_renew ();
155- node_settings. chain_config = **known_chain;
162+ chain_config = **known_chain;
156163 }
157164 }
158165
159166 // Load genesis_hash
160- node_settings. chain_config ->genesis_hash = db::read_canonical_header_hash (tx, 0 );
161- if (!node_settings. chain_config ->genesis_hash .has_value ())
167+ chain_config->genesis_hash = db::read_canonical_header_hash (tx, 0 );
168+ if (!chain_config->genesis_hash .has_value ())
162169 throw std::runtime_error (" Could not load genesis hash" );
163170
164171 log::Info (" Starting Silkworm" , {" chain" , (known_chain ? std::to_string (chain_id) : " unknown/custom" ),
165- " config" , node_settings. chain_config ->to_json ().dump ()});
172+ " config" , chain_config->to_json ().dump ()});
166173 }
167174
168175 // Detect prune-mode and verify is compatible
@@ -176,15 +183,14 @@ void run_db_checklist(NodeSettings& node_settings, bool init_if_empty) {
176183 db_prune_mode.to_string () + " got " + node_settings.prune_mode .to_string ());
177184 }
178185 db::write_prune_mode (*tx, node_settings.prune_mode );
179- node_settings.prune_mode = db::PruneMode (db::read_prune_mode (*tx));
180186 }
181187 log::Info (" Effective pruning" , {" mode" , node_settings.prune_mode .to_string ()});
182188 }
183189
184190 tx.commit_and_stop ();
185191 chaindata_env.close ();
186- node_settings. chaindata_env_config . exclusive = chaindata_exclusive;
187- node_settings. chaindata_env_config . create = false ; // Has already been created
192+
193+ return std::move (*chain_config);
188194}
189195
190- } // namespace silkworm::cmd::common
196+ } // namespace silkworm::db
0 commit comments