mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-16 17:48:59 +02:00
Merge bitcoin/bitcoin#33856: kernel: Refactor process_block_header to return btck_BlockValidationState
88d9bc5aa4kernel: Return btck_BlockValidationState from process_block_header API (yuvicc) Pull request description: This PR refactors `btck_chainstate_manager_process_block_header` to return `btck_BlockValidationState` by value instead of using out-parameters or boolean returns. ACKs for top commit: optout21: ACK88d9bc5aa4stickies-v: ACK88d9bc5aa4w0xlt: reACK88d9bc5aa4hodlinator: re-ACK88d9bc5aa4Tree-SHA512: f86b6e85aedafd78ae250930cbe34dc666c14d800e43cf8582d49aecb97faab801eff8dcc0250082ceebc3e8d32949839e030cf9f0023b56b23c8f7b7a741e49
This commit is contained in:
@@ -1340,19 +1340,20 @@ int btck_chainstate_manager_process_block(
|
||||
return result ? 0 : -1;
|
||||
}
|
||||
|
||||
int btck_chainstate_manager_process_block_header(
|
||||
btck_BlockValidationState* btck_chainstate_manager_process_block_header(
|
||||
btck_ChainstateManager* chainstate_manager,
|
||||
const btck_BlockHeader* header,
|
||||
btck_BlockValidationState* state)
|
||||
const btck_BlockHeader* header)
|
||||
{
|
||||
try {
|
||||
auto& chainman = btck_ChainstateManager::get(chainstate_manager).m_chainman;
|
||||
auto result = chainman->ProcessNewBlockHeaders({&btck_BlockHeader::get(header), 1}, /*min_pow_checked=*/true, btck_BlockValidationState::get(state), /*ppindex=*/nullptr);
|
||||
|
||||
return result ? 0 : -1;
|
||||
auto state = btck_BlockValidationState::create();
|
||||
bool result{chainman->ProcessNewBlockHeaders({&btck_BlockHeader::get(header), 1}, /*min_pow_checked=*/true, btck_BlockValidationState::get(state))};
|
||||
assert(result == btck_BlockValidationState::get(state).IsValid());
|
||||
return state;
|
||||
} catch (const std::exception& e) {
|
||||
LogError("Failed to process block header: %s", e.what());
|
||||
return -1;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1243,13 +1243,11 @@ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT bt
|
||||
*
|
||||
* @param[in] chainstate_manager Non-null.
|
||||
* @param[in] header Non-null btck_BlockHeader to be validated.
|
||||
* @param[out] block_validation_state The result of the btck_BlockHeader validation.
|
||||
* @return 0 if btck_BlockHeader processing completed successfully, non-zero on error.
|
||||
* @return The btck_BlockValidationState containing validation result, or null on error.
|
||||
*/
|
||||
BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_process_block_header(
|
||||
BITCOINKERNEL_API btck_BlockValidationState* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_process_block_header(
|
||||
btck_ChainstateManager* chainstate_manager,
|
||||
const btck_BlockHeader* header,
|
||||
btck_BlockValidationState* block_validation_state) BITCOINKERNEL_ARG_NONNULL(1, 2, 3);
|
||||
const btck_BlockHeader* header) BITCOINKERNEL_ARG_NONNULL(1, 2);
|
||||
|
||||
/**
|
||||
* @brief Triggers the start of a reindex if the wipe options were previously
|
||||
|
||||
@@ -1005,7 +1005,9 @@ class BlockValidationState : public Handle<btck_BlockValidationState, btck_block
|
||||
public:
|
||||
explicit BlockValidationState() : Handle{btck_block_validation_state_create()} {}
|
||||
|
||||
BlockValidationState(const BlockValidationStateView& view) : Handle{view} {}
|
||||
explicit BlockValidationState(const BlockValidationStateView& view) : Handle{view} {}
|
||||
|
||||
explicit BlockValidationState(btck_BlockValidationState* state) : Handle{state} {}
|
||||
};
|
||||
|
||||
inline bool Block::Check(const ConsensusParamsView& consensus_params,
|
||||
@@ -1317,9 +1319,10 @@ public:
|
||||
return res == 0;
|
||||
}
|
||||
|
||||
bool ProcessBlockHeader(const BlockHeader& header, BlockValidationState& state)
|
||||
BlockValidationState ProcessBlockHeader(const BlockHeader& header)
|
||||
{
|
||||
return btck_chainstate_manager_process_block_header(get(), header.get(), state.get()) == 0;
|
||||
auto state = btck_chainstate_manager_process_block_header(get(), header.get());
|
||||
return BlockValidationState{state};
|
||||
}
|
||||
|
||||
ChainView GetChain() const
|
||||
|
||||
@@ -1102,10 +1102,9 @@ BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
|
||||
for (const auto& data : REGTEST_BLOCK_DATA) {
|
||||
Block block{hex_string_to_byte_vec(data)};
|
||||
BlockHeader header = block.GetHeader();
|
||||
BlockValidationState state{};
|
||||
BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::UNSET);
|
||||
BOOST_CHECK(chainman->ProcessBlockHeader(header, state));
|
||||
BlockValidationState state = chainman->ProcessBlockHeader(header);
|
||||
BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
|
||||
BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::UNSET);
|
||||
BlockTreeEntry entry{*chainman->GetBlockTreeEntry(header.Hash())};
|
||||
BOOST_CHECK(!chainman->GetChain().Contains(entry));
|
||||
BlockTreeEntry best_entry{chainman->GetBestEntry()};
|
||||
|
||||
Reference in New Issue
Block a user