@@ -169,32 +169,32 @@ class KernelNotifications : public kernel::Notifications
169
169
170
170
kernel::InterruptResult blockTip (SynchronizationState state, CBlockIndex& index) override
171
171
{
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));
173
173
return {};
174
174
}
175
175
void headerTip (SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
176
176
{
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);
178
178
}
179
179
void progress (const bilingual_str& title, int progress_percent, bool resume_possible) override
180
180
{
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);
182
182
}
183
183
void warningSet (kernel::Warning id, const bilingual_str& message) override
184
184
{
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 ());
186
186
}
187
187
void warningUnset (kernel::Warning id) override
188
188
{
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));
190
190
}
191
191
void flushError (const bilingual_str& message) override
192
192
{
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 ());
194
194
}
195
195
void fatalError (const bilingual_str& message) override
196
196
{
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 ());
198
198
}
199
199
};
200
200
@@ -209,7 +209,7 @@ class KernelValidationInterface final : public CValidationInterface
209
209
void BlockChecked (const CBlock& block, const BlockValidationState& stateIn) override
210
210
{
211
211
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 ,
213
213
reinterpret_cast <const kernel_BlockPointer*>(&block),
214
214
reinterpret_cast <const kernel_BlockValidationState*>(&stateIn));
215
215
}
@@ -271,6 +271,31 @@ class Context
271
271
}
272
272
};
273
273
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
+
274
299
const CTransaction* cast_transaction (const kernel_Transaction* transaction)
275
300
{
276
301
assert (transaction);
@@ -319,28 +344,16 @@ const Context* cast_const_context(const kernel_Context* context)
319
344
return reinterpret_cast <const Context*>(context);
320
345
}
321
346
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)
329
348
{
330
349
assert (options);
331
- return reinterpret_cast <ChainstateManager::Options *>(options);
350
+ return reinterpret_cast <const ChainstateManagerOptions *>(options);
332
351
}
333
352
334
- const node::BlockManager::Options* cast_const_block_manager_options ( const kernel_BlockManagerOptions * options)
353
+ ChainstateManagerOptions* cast_chainstate_manager_options (kernel_ChainstateManagerOptions * options)
335
354
{
336
355
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);
344
357
}
345
358
346
359
ChainstateManager* cast_chainstate_manager (kernel_ChainstateManager* chainman)
@@ -349,18 +362,6 @@ ChainstateManager* cast_chainstate_manager(kernel_ChainstateManager* chainman)
349
362
return reinterpret_cast <ChainstateManager*>(chainman);
350
363
}
351
364
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
-
364
365
std::shared_ptr<CBlock>* cast_cblocksharedpointer (kernel_Block* block)
365
366
{
366
367
assert (block);
@@ -453,12 +454,12 @@ void kernel_transaction_output_destroy(kernel_TransactionOutput* output)
453
454
}
454
455
455
456
bool 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)
462
463
{
463
464
const CAmount amount{amount_};
464
465
const auto & script_pubkey{*cast_script_pubkey (script_pubkey_)};
@@ -545,7 +546,7 @@ kernel_LoggingConnection* kernel_logging_connection_create(kernel_LogCallback ca
545
546
LogInstance ().m_log_sourcelocations = options.log_sourcelocations ;
546
547
LogInstance ().m_always_print_category_level = options.always_print_category_levels ;
547
548
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 ()); })};
549
550
550
551
try {
551
552
// Only start logging if we just added the connection.
@@ -709,17 +710,15 @@ kernel_BlockValidationResult kernel_get_block_validation_result_from_block_valid
709
710
assert (false );
710
711
}
711
712
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 )
713
714
{
714
715
try {
715
716
fs::path abs_data_dir{fs::absolute (fs::PathFromString ({data_dir, data_dir_len}))};
716
717
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);
717
720
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));
723
722
} catch (const std::exception& e) {
724
723
LogError (" Failed to create chainstate manager options: %s" , e.what ());
725
724
return nullptr ;
@@ -729,7 +728,7 @@ kernel_ChainstateManagerOptions* kernel_chainstate_manager_options_create(const
729
728
void kernel_chainstate_manager_options_set_worker_threads_num (kernel_ChainstateManagerOptions* opts_, int worker_threads)
730
729
{
731
730
auto opts{cast_chainstate_manager_options (opts_)};
732
- opts->worker_threads_num = worker_threads;
731
+ opts->m_chainman_options . worker_threads_num = worker_threads;
733
732
}
734
733
735
734
void kernel_chainstate_manager_options_destroy (kernel_ChainstateManagerOptions* options)
@@ -739,111 +738,52 @@ void kernel_chainstate_manager_options_destroy(kernel_ChainstateManagerOptions*
739
738
}
740
739
}
741
740
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)
769
742
{
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 ;
772
746
}
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 ;
773
751
}
774
752
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_,
799
755
bool block_tree_db_in_memory)
800
756
{
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;
803
759
}
804
760
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_,
807
763
bool chainstate_db_in_memory)
808
764
{
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;
818
767
}
819
768
820
769
kernel_ChainstateManager* kernel_chainstate_manager_create (
821
770
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_)
825
772
{
826
773
auto chainman_opts{cast_const_chainstate_manager_options (chainman_opts_)};
827
- auto blockman_opts{cast_const_block_manager_options (blockman_opts_)};
828
774
auto context{cast_const_context (context_)};
829
775
830
776
ChainstateManager* chainman;
831
777
832
778
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 };
834
780
} catch (const std::exception& e) {
835
781
LogError (" Failed to create chainstate manager: %s" , e.what ());
836
782
return nullptr ;
837
783
}
838
784
839
785
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 };
847
787
848
788
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
849
789
auto [status, chainstate_err]{node::LoadChainstate (*chainman, cache_sizes, chainstate_load_opts)};
0 commit comments