@@ -169,32 +169,32 @@ class KernelNotifications : public kernel::Notifications
169169
170170 kernel::InterruptResult blockTip (SynchronizationState state, CBlockIndex& index) override
171171 {
172- if (m_cbs.block_tip ) m_cbs.block_tip ((void *) m_cbs.user_data , cast_state (state), reinterpret_cast <const kernel_BlockIndex*>(&index));
172+ if (m_cbs.block_tip ) m_cbs.block_tip ((void *)m_cbs.user_data , cast_state (state), reinterpret_cast <const kernel_BlockIndex*>(&index));
173173 return {};
174174 }
175175 void headerTip (SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
176176 {
177- if (m_cbs.header_tip ) m_cbs.header_tip ((void *) m_cbs.user_data , cast_state (state), height, timestamp, presync);
177+ if (m_cbs.header_tip ) m_cbs.header_tip ((void *)m_cbs.user_data , cast_state (state), height, timestamp, presync);
178178 }
179179 void progress (const bilingual_str& title, int progress_percent, bool resume_possible) override
180180 {
181- if (m_cbs.progress ) m_cbs.progress ((void *) m_cbs.user_data , title.original .c_str (), title.original .length (), progress_percent, resume_possible);
181+ if (m_cbs.progress ) m_cbs.progress ((void *)m_cbs.user_data , title.original .c_str (), title.original .length (), progress_percent, resume_possible);
182182 }
183183 void warningSet (kernel::Warning id, const bilingual_str& message) override
184184 {
185- if (m_cbs.warning_set ) m_cbs.warning_set ((void *) m_cbs.user_data , cast_kernel_warning (id), message.original .c_str (), message.original .length ());
185+ if (m_cbs.warning_set ) m_cbs.warning_set ((void *)m_cbs.user_data , cast_kernel_warning (id), message.original .c_str (), message.original .length ());
186186 }
187187 void warningUnset (kernel::Warning id) override
188188 {
189- if (m_cbs.warning_unset ) m_cbs.warning_unset ((void *) m_cbs.user_data , cast_kernel_warning (id));
189+ if (m_cbs.warning_unset ) m_cbs.warning_unset ((void *)m_cbs.user_data , cast_kernel_warning (id));
190190 }
191191 void flushError (const bilingual_str& message) override
192192 {
193- if (m_cbs.flush_error ) m_cbs.flush_error ((void *) m_cbs.user_data , message.original .c_str (), message.original .length ());
193+ if (m_cbs.flush_error ) m_cbs.flush_error ((void *)m_cbs.user_data , message.original .c_str (), message.original .length ());
194194 }
195195 void fatalError (const bilingual_str& message) override
196196 {
197- if (m_cbs.fatal_error ) m_cbs.fatal_error ((void *) m_cbs.user_data , message.original .c_str (), message.original .length ());
197+ if (m_cbs.fatal_error ) m_cbs.fatal_error ((void *)m_cbs.user_data , message.original .c_str (), message.original .length ());
198198 }
199199};
200200
@@ -209,7 +209,7 @@ class KernelValidationInterface final : public CValidationInterface
209209 void BlockChecked (const CBlock& block, const BlockValidationState& stateIn) override
210210 {
211211 if (m_cbs.block_checked ) {
212- m_cbs.block_checked ((void *) m_cbs.user_data ,
212+ m_cbs.block_checked ((void *)m_cbs.user_data ,
213213 reinterpret_cast <const kernel_BlockPointer*>(&block),
214214 reinterpret_cast <const kernel_BlockValidationState*>(&stateIn));
215215 }
@@ -271,6 +271,31 @@ class Context
271271 }
272272};
273273
274+ // ! Helper struct to wrap the ChainstateManager-related Options
275+ struct ChainstateManagerOptions {
276+ ChainstateManager::Options m_chainman_options;
277+ node::BlockManager::Options m_blockman_options;
278+ node::ChainstateLoadOptions m_chainstate_load_options;
279+
280+ ChainstateManagerOptions (const Context* context, const fs::path& data_dir, const fs::path& blocks_dir)
281+ : m_chainman_options{ChainstateManager::Options{
282+ .chainparams = *context->m_chainparams ,
283+ .datadir = data_dir,
284+ .notifications = *context->m_notifications ,
285+ .signals = context->m_signals .get ()}},
286+ m_blockman_options{node::BlockManager::Options{
287+ .chainparams = *context->m_chainparams ,
288+ .blocks_dir = blocks_dir,
289+ .notifications = *context->m_notifications ,
290+ .block_tree_db_params = DBParams{
291+ .path = data_dir / " blocks" / " index" ,
292+ .cache_bytes = kernel::CacheSizes{DEFAULT_KERNEL_CACHE}.block_tree_db ,
293+ }}},
294+ m_chainstate_load_options{node::ChainstateLoadOptions{}}
295+ {
296+ }
297+ };
298+
274299const CTransaction* cast_transaction (const kernel_Transaction* transaction)
275300{
276301 assert (transaction);
@@ -319,28 +344,16 @@ const Context* cast_const_context(const kernel_Context* context)
319344 return reinterpret_cast <const Context*>(context);
320345}
321346
322- const ChainstateManager::Options* cast_const_chainstate_manager_options (const kernel_ChainstateManagerOptions* options)
323- {
324- assert (options);
325- return reinterpret_cast <const ChainstateManager::Options*>(options);
326- }
327-
328- ChainstateManager::Options* cast_chainstate_manager_options (kernel_ChainstateManagerOptions* options)
347+ const ChainstateManagerOptions* cast_const_chainstate_manager_options (const kernel_ChainstateManagerOptions* options)
329348{
330349 assert (options);
331- return reinterpret_cast <ChainstateManager::Options *>(options);
350+ return reinterpret_cast <const ChainstateManagerOptions *>(options);
332351}
333352
334- const node::BlockManager::Options* cast_const_block_manager_options ( const kernel_BlockManagerOptions * options)
353+ ChainstateManagerOptions* cast_chainstate_manager_options (kernel_ChainstateManagerOptions * options)
335354{
336355 assert (options);
337- return reinterpret_cast <const node::BlockManager::Options*>(options);
338- }
339-
340- node::BlockManager::Options* cast_block_manager_options (kernel_BlockManagerOptions* options)
341- {
342- assert (options);
343- return reinterpret_cast <node::BlockManager::Options*>(options);
356+ return reinterpret_cast <ChainstateManagerOptions*>(options);
344357}
345358
346359ChainstateManager* cast_chainstate_manager (kernel_ChainstateManager* chainman)
@@ -349,18 +362,6 @@ ChainstateManager* cast_chainstate_manager(kernel_ChainstateManager* chainman)
349362 return reinterpret_cast <ChainstateManager*>(chainman);
350363}
351364
352- node::ChainstateLoadOptions* cast_chainstate_load_options (kernel_ChainstateLoadOptions* options)
353- {
354- assert (options);
355- return reinterpret_cast <node::ChainstateLoadOptions*>(options);
356- }
357-
358- const node::ChainstateLoadOptions* cast_const_chainstate_load_options (const kernel_ChainstateLoadOptions* options)
359- {
360- assert (options);
361- return reinterpret_cast <const node::ChainstateLoadOptions*>(options);
362- }
363-
364365std::shared_ptr<CBlock>* cast_cblocksharedpointer (kernel_Block* block)
365366{
366367 assert (block);
@@ -453,12 +454,12 @@ void kernel_transaction_output_destroy(kernel_TransactionOutput* output)
453454}
454455
455456bool kernel_verify_script (const kernel_ScriptPubkey* script_pubkey_,
456- const int64_t amount_,
457- const kernel_Transaction* tx_to,
458- const kernel_TransactionOutput** spent_outputs_, size_t spent_outputs_len,
459- const unsigned int input_index,
460- const unsigned int flags,
461- kernel_ScriptVerifyStatus* status)
457+ const int64_t amount_,
458+ const kernel_Transaction* tx_to,
459+ const kernel_TransactionOutput** spent_outputs_, size_t spent_outputs_len,
460+ const unsigned int input_index,
461+ const unsigned int flags,
462+ kernel_ScriptVerifyStatus* status)
462463{
463464 const CAmount amount{amount_};
464465 const auto & script_pubkey{*cast_script_pubkey (script_pubkey_)};
@@ -545,7 +546,7 @@ kernel_LoggingConnection* kernel_logging_connection_create(kernel_LogCallback ca
545546 LogInstance ().m_log_sourcelocations = options.log_sourcelocations ;
546547 LogInstance ().m_always_print_category_level = options.always_print_category_levels ;
547548
548- auto connection{LogInstance ().PushBackCallback ([callback, user_data](const std::string& str) { callback ((void *) user_data, str.c_str (), str.length ()); })};
549+ auto connection{LogInstance ().PushBackCallback ([callback, user_data](const std::string& str) { callback ((void *)user_data, str.c_str (), str.length ()); })};
549550
550551 try {
551552 // Only start logging if we just added the connection.
@@ -709,17 +710,15 @@ kernel_BlockValidationResult kernel_get_block_validation_result_from_block_valid
709710 assert (false );
710711}
711712
712- kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create (const kernel_Context* context_, const char * data_dir, size_t data_dir_len)
713+ kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create (const kernel_Context* context_, const char * data_dir, size_t data_dir_len, const char * blocks_dir, size_t blocks_dir_len )
713714{
714715 try {
715716 fs::path abs_data_dir{fs::absolute (fs::PathFromString ({data_dir, data_dir_len}))};
716717 fs::create_directories (abs_data_dir);
718+ fs::path abs_blocks_dir{fs::absolute (fs::PathFromString ({blocks_dir, blocks_dir_len}))};
719+ fs::create_directories (abs_blocks_dir);
717720 auto context{cast_const_context (context_)};
718- return reinterpret_cast <kernel_ChainstateManagerOptions*>(new ChainstateManager::Options{
719- .chainparams = *context->m_chainparams ,
720- .datadir = abs_data_dir,
721- .notifications = *context->m_notifications ,
722- .signals = context->m_signals .get ()});
721+ return reinterpret_cast <kernel_ChainstateManagerOptions*>(new ChainstateManagerOptions (context, abs_data_dir, abs_blocks_dir));
723722 } catch (const std::exception& e) {
724723 LogError (" Failed to create chainstate manager options: %s" , e.what ());
725724 return nullptr ;
@@ -729,7 +728,7 @@ kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(const
729728void kernel_chainstate_manager_options_set_worker_threads_num (kernel_ChainstateManagerOptions* opts_, int worker_threads)
730729{
731730 auto opts{cast_chainstate_manager_options (opts_)};
732- opts->worker_threads_num = worker_threads;
731+ opts->m_chainman_options . worker_threads_num = worker_threads;
733732}
734733
735734void kernel_chainstate_manager_options_destroy (kernel_ChainstateManagerOptions* options)
@@ -739,111 +738,52 @@ void kernel_chainstate_manager_options_destroy(kernel_ChainstateManagerOptions*
739738 }
740739}
741740
742- kernel_BlockManagerOptions* kernel_block_manager_options_create (const kernel_Context* context_, const char * data_dir, size_t data_dir_len, const char * blocks_dir, size_t blocks_dir_len)
743- {
744- try {
745- fs::path abs_blocks_dir{fs::absolute (fs::PathFromString ({blocks_dir, blocks_dir_len}))};
746- fs::create_directories (abs_blocks_dir);
747- fs::path abs_data_dir{fs::absolute (fs::PathFromString ({data_dir, data_dir_len}))};
748- fs::create_directories (abs_data_dir);
749- auto context{cast_const_context (context_)};
750- if (!context) {
751- return nullptr ;
752- }
753- kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
754- return reinterpret_cast <kernel_BlockManagerOptions*>(new node::BlockManager::Options{
755- .chainparams = *context->m_chainparams ,
756- .blocks_dir = abs_blocks_dir,
757- .notifications = *context->m_notifications ,
758- .block_tree_db_params = DBParams{
759- .path = abs_data_dir / " blocks" / " index" ,
760- .cache_bytes = cache_sizes.block_tree_db ,
761- }});
762- } catch (const std::exception& e) {
763- LogError (" Failed to create block manager options; %s" , e.what ());
764- return nullptr ;
765- }
766- }
767-
768- void kernel_block_manager_options_destroy (kernel_BlockManagerOptions* options)
741+ bool kernel_chainstate_manager_options_set_wipe_dbs (kernel_ChainstateManagerOptions* chainman_opts_, bool wipe_block_tree_db, bool wipe_chainstate_db)
769742{
770- if (options) {
771- delete cast_const_block_manager_options (options);
743+ if (wipe_block_tree_db && !wipe_chainstate_db) {
744+ LogError (" Wiping the block tree db without also wiping the chainstate db is currently unsupported." );
745+ return false ;
772746 }
747+ auto chainman_opts{cast_chainstate_manager_options (chainman_opts_)};
748+ chainman_opts->m_blockman_options .block_tree_db_params .wipe_data = wipe_block_tree_db;
749+ chainman_opts->m_chainstate_load_options .wipe_chainstate_db = wipe_chainstate_db;
750+ return true ;
773751}
774752
775- kernel_ChainstateLoadOptions* kernel_chainstate_load_options_create ()
776- {
777- return reinterpret_cast <kernel_ChainstateLoadOptions*>(new node::ChainstateLoadOptions);
778- }
779-
780-
781- void kernel_block_manager_options_set_wipe_block_tree_db (
782- kernel_BlockManagerOptions* block_manager_options_,
783- bool wipe_block_tree_db)
784- {
785- auto block_manager_options{cast_block_manager_options (block_manager_options_)};
786- block_manager_options->block_tree_db_params .wipe_data = wipe_block_tree_db;
787- }
788-
789- void kernel_chainstate_load_options_set_wipe_chainstate_db (
790- kernel_ChainstateLoadOptions* chainstate_load_opts_,
791- bool wipe_chainstate_db)
792- {
793- auto chainstate_load_opts{cast_chainstate_load_options (chainstate_load_opts_)};
794- chainstate_load_opts->wipe_chainstate_db = wipe_chainstate_db;
795- }
796-
797- void kernel_block_manager_options_set_block_tree_db_in_memory (
798- kernel_BlockManagerOptions* chainstate_load_opts_,
753+ void kernel_chainstate_manager_options_set_block_tree_db_in_memory (
754+ kernel_ChainstateManagerOptions* chainstate_load_opts_,
799755 bool block_tree_db_in_memory)
800756{
801- auto block_manager_options{ cast_block_manager_options (chainstate_load_opts_)};
802- block_manager_options-> block_tree_db_params .memory_only = block_tree_db_in_memory;
757+ auto chainman_opts{ cast_chainstate_manager_options (chainstate_load_opts_)};
758+ chainman_opts-> m_blockman_options . block_tree_db_params .memory_only = block_tree_db_in_memory;
803759}
804760
805- void kernel_chainstate_load_options_set_chainstate_db_in_memory (
806- kernel_ChainstateLoadOptions * chainstate_load_opts_,
761+ void kernel_chainstate_manager_options_set_chainstate_db_in_memory (
762+ kernel_ChainstateManagerOptions * chainstate_load_opts_,
807763 bool chainstate_db_in_memory)
808764{
809- auto chainstate_load_opts{cast_chainstate_load_options (chainstate_load_opts_)};
810- chainstate_load_opts->coins_db_in_memory = chainstate_db_in_memory;
811- }
812-
813- void kernel_chainstate_load_options_destroy (kernel_ChainstateLoadOptions* chainstate_load_opts)
814- {
815- if (chainstate_load_opts) {
816- delete cast_const_chainstate_load_options (chainstate_load_opts);
817- }
765+ auto chainman_opts{cast_chainstate_manager_options (chainstate_load_opts_)};
766+ chainman_opts->m_chainstate_load_options .coins_db_in_memory = chainstate_db_in_memory;
818767}
819768
820769kernel_ChainstateManager* kernel_chainstate_manager_create (
821770 const kernel_Context* context_,
822- const kernel_ChainstateManagerOptions* chainman_opts_,
823- const kernel_BlockManagerOptions* blockman_opts_,
824- const kernel_ChainstateLoadOptions* chainstate_load_opts_)
771+ const kernel_ChainstateManagerOptions* chainman_opts_)
825772{
826773 auto chainman_opts{cast_const_chainstate_manager_options (chainman_opts_)};
827- auto blockman_opts{cast_const_block_manager_options (blockman_opts_)};
828774 auto context{cast_const_context (context_)};
829775
830776 ChainstateManager* chainman;
831777
832778 try {
833- chainman = new ChainstateManager{*context->m_interrupt , * chainman_opts, *blockman_opts };
779+ chainman = new ChainstateManager{*context->m_interrupt , chainman_opts-> m_chainman_options , chainman_opts-> m_blockman_options };
834780 } catch (const std::exception& e) {
835781 LogError (" Failed to create chainstate manager: %s" , e.what ());
836782 return nullptr ;
837783 }
838784
839785 try {
840- const auto & chainstate_load_opts{*cast_const_chainstate_load_options (chainstate_load_opts_)};
841-
842- if (blockman_opts->block_tree_db_params .wipe_data && !chainstate_load_opts.wipe_chainstate_db ) {
843- LogWarning (" Wiping the block tree db without also wiping the chainstate db is currently unsupported." );
844- kernel_chainstate_manager_destroy (reinterpret_cast <kernel_ChainstateManager*>(chainman), context_);
845- return nullptr ;
846- }
786+ const auto & chainstate_load_opts{chainman_opts->m_chainstate_load_options };
847787
848788 kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
849789 auto [status, chainstate_err]{node::LoadChainstate (*chainman, cache_sizes, chainstate_load_opts)};
0 commit comments