Skip to content

Commit 990a648

Browse files
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from 73acb3ff8a04..f157b0cbc7d9
f157b0cbc7d9 kernel: Add pure kernel bitcoin-chainstate 692d1c23c272 kernel: Add functions to get the block hash from a block aad02fb561ae kernel: Add block index utility functions to C header 13f4911b0646 kernel: Add function to read block undo data from disk to C header 29fdbf260348 kernel: Add functions to read block from disk to C header 2ca304c1def7 kernel: Add function for copying block data to C header 705c7f125fd9 kernel: Add functions for the block validation state to C header 92363c9469c0 kernel: Add validation interface to C header 7c539908113b kernel: Add interrupt function to C header 522d0886d8ff kernel: Add import blocks function to C header 1ae86e2ffe12 kernel: Add chainstate load options for in-memory dbs in C header 09620eeeae6c kernel: Add options for reindexing in C header b4fbf1931725 kernel: Add block validation to C header ce6ddde95eee Kernel: Add chainstate loading to kernel C header f5d21c94dc5a kernel: Add chainstate manager option for setting worker threads 783f56f0a290 kernel: Add chainstate manager object to C header 262039e4094c kernel: Add notifications context option to C header dc0d406dd5e9 kerenl: Add chain params context option to C header b5f84de7ad2d kernel: Add kernel library context object dad0009c86c1 kernel: Add logging to kernel library C header 27e25aa941c6 kernel: Introduce initial kernel C header API REVERT: 73acb3ff8a04 kernel: Add pure kernel bitcoin-chainstate REVERT: 6c26da3dc494 kernel: Add functions to get the block hash from a block REVERT: 99d302a824f5 kernel: Add block index utility functions to C header REVERT: 7d1583ec269f kernel: Add function to read block undo data from disk to C header REVERT: 7af127e174e8 kernel: Add functions to read block from disk to C header REVERT: 700087ca1112 kernel: Add function for copying block data to C header REVERT: db9b5fa92db4 kernel: Add functions for the block validation state to C header REVERT: ae178c846341 kernel: Add validation interface to C header REVERT: f5ecbabe94d9 kernel: Add interrupt function to C header REVERT: 08a52f02bf84 kernel: Add import blocks function to C header REVERT: 3ced859f0f9f kernel: Add chainstate load options for in-memory dbs in C header REVERT: b3a1f0c65ec0 kernel: Add options for reindexing in C header REVERT: bde14cb16090 kernel: Add block validation to C header REVERT: 3ebee13826d3 Kernel: Add chainstate loading to kernel C header REVERT: 593999ab9a6c kernel: Add chainstate manager option for setting worker threads REVERT: 78dda8e7474f kernel: Add chainstate manager object to C header REVERT: 436db528893b kernel: Add notifications context option to C header REVERT: 2e4e30d45c39 kerenl: Add chain params context option to C header REVERT: 20f1d3996ec6 kernel: Add kernel library context object REVERT: f3ab2302a1c3 kernel: Add logging to kernel library C header REVERT: 20f5156bd517 kernel: Introduce initial kernel C header API git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: f157b0cbc7d90075858a6522d13a7bc4f0b25a5f
1 parent 29507a3 commit 990a648

File tree

5 files changed

+256
-145
lines changed

5 files changed

+256
-145
lines changed

src/kernel/bitcoin-chainstate.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ int main(int argc, char* argv[])
166166
chainman_opts.SetWorkerThreads(4);
167167
BlockManagerOptions blockman_opts{context, abs_datadir / "blocks"};
168168
assert(blockman_opts);
169+
ChainstateLoadOptions chainstate_load_opts{};
169170

170-
auto chainman{std::make_unique<ChainMan>(context, chainman_opts, blockman_opts)};
171+
auto chainman{std::make_unique<ChainMan>(context, chainman_opts, blockman_opts, chainstate_load_opts)};
171172
assert(chainman);
172173

173-
ChainstateLoadOptions chainstate_load_opts{};
174-
assert(chainman->LoadChainstate(chainstate_load_opts));
175-
176174
std::cout << "Enter the block you want to validate on the next line:" << std::endl;
177175

178176
for (std::string line; std::getline(std::cin, line);) {

src/kernel/bitcoinkernel.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -757,23 +757,6 @@ void kernel_block_manager_options_destroy(kernel_BlockManagerOptions* options)
757757
}
758758
}
759759

760-
kernel_ChainstateManager* kernel_chainstate_manager_create(
761-
const kernel_Context* context_,
762-
const kernel_ChainstateManagerOptions* chainman_opts_,
763-
const kernel_BlockManagerOptions* blockman_opts_)
764-
{
765-
auto chainman_opts{cast_const_chainstate_manager_options(chainman_opts_)};
766-
auto blockman_opts{cast_const_block_manager_options(blockman_opts_)};
767-
auto context{cast_const_context(context_)};
768-
769-
try {
770-
return reinterpret_cast<kernel_ChainstateManager*>(new ChainstateManager{*context->m_interrupt, *chainman_opts, *blockman_opts});
771-
} catch (const std::exception& e) {
772-
LogError("Failed to create chainstate manager: %s", e.what());
773-
return nullptr;
774-
}
775-
}
776-
777760
kernel_ChainstateLoadOptions* kernel_chainstate_load_options_create()
778761
{
779762
return reinterpret_cast<kernel_ChainstateLoadOptions*>(new node::ChainstateLoadOptions);
@@ -819,46 +802,65 @@ void kernel_chainstate_load_options_destroy(kernel_ChainstateLoadOptions* chains
819802
}
820803
}
821804

822-
bool kernel_chainstate_manager_load_chainstate(const kernel_Context* context_,
823-
const kernel_ChainstateLoadOptions* chainstate_load_opts_,
824-
kernel_ChainstateManager* chainman_)
805+
kernel_ChainstateManager* kernel_chainstate_manager_create(
806+
const kernel_Context* context_,
807+
const kernel_ChainstateManagerOptions* chainman_opts_,
808+
const kernel_BlockManagerOptions* blockman_opts_,
809+
const kernel_ChainstateLoadOptions* chainstate_load_opts_)
825810
{
811+
auto chainman_opts{cast_const_chainstate_manager_options(chainman_opts_)};
812+
auto blockman_opts{cast_const_block_manager_options(blockman_opts_)};
813+
auto context{cast_const_context(context_)};
814+
815+
ChainstateManager* chainman;
816+
817+
try {
818+
chainman = new ChainstateManager{*context->m_interrupt, *chainman_opts, *blockman_opts};
819+
} catch (const std::exception& e) {
820+
LogError("Failed to create chainstate manager: %s", e.what());
821+
return nullptr;
822+
}
823+
826824
try {
827825
const auto& chainstate_load_opts{*cast_const_chainstate_load_options(chainstate_load_opts_)};
828-
auto& chainman{*cast_chainstate_manager(chainman_)};
829826

830827
if (chainstate_load_opts.wipe_block_tree_db && !chainstate_load_opts.wipe_chainstate_db) {
831828
LogWarning("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
832-
return false;
829+
kernel_chainstate_manager_destroy(reinterpret_cast<kernel_ChainstateManager*>(chainman), context_);
830+
return nullptr;
833831
}
834832

835833
node::CacheSizes cache_sizes;
836834
cache_sizes.block_tree_db = 2 << 20;
837835
cache_sizes.coins_db = 2 << 22;
838836
cache_sizes.coins = (450 << 20) - (2 << 20) - (2 << 22);
839-
auto [status, chainstate_err]{node::LoadChainstate(chainman, cache_sizes, chainstate_load_opts)};
837+
auto [status, chainstate_err]{node::LoadChainstate(*chainman, cache_sizes, chainstate_load_opts)};
840838
if (status != node::ChainstateLoadStatus::SUCCESS) {
841839
LogError("Failed to load chain state from your data directory: %s", chainstate_err.original);
842-
return false;
840+
kernel_chainstate_manager_destroy(reinterpret_cast<kernel_ChainstateManager*>(chainman), context_);
841+
return nullptr;
843842
}
844-
std::tie(status, chainstate_err) = node::VerifyLoadedChainstate(chainman, chainstate_load_opts);
843+
std::tie(status, chainstate_err) = node::VerifyLoadedChainstate(*chainman, chainstate_load_opts);
845844
if (status != node::ChainstateLoadStatus::SUCCESS) {
846845
LogError("Failed to verify loaded chain state from your datadir: %s", chainstate_err.original);
847-
return false;
846+
kernel_chainstate_manager_destroy(reinterpret_cast<kernel_ChainstateManager*>(chainman), context_);
847+
return nullptr;
848848
}
849849

850-
for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
850+
for (Chainstate* chainstate : WITH_LOCK(::cs_main, return chainman->GetAll())) {
851851
BlockValidationState state;
852852
if (!chainstate->ActivateBestChain(state, nullptr)) {
853853
LogError("Failed to connect best block: %s", state.ToString());
854-
return false;
854+
kernel_chainstate_manager_destroy(reinterpret_cast<kernel_ChainstateManager*>(chainman), context_);
855+
return nullptr;
855856
}
856857
}
857858
} catch (const std::exception& e) {
858859
LogError("Failed to load chainstate: %s", e.what());
859-
return false;
860+
return nullptr;
860861
}
861-
return true;
862+
863+
return reinterpret_cast<kernel_ChainstateManager*>(chainman);
862864
}
863865

864866
void kernel_chainstate_manager_destroy(kernel_ChainstateManager* chainman_, const kernel_Context* context_)

0 commit comments

Comments
 (0)