Repository: bitcoin/bitcoin
File: src/kernel/bitcoinkernel.cpp (C++)

Code before:

const btck_TransactionOutput* btck_coin_get_output(const btck_Coin* coin)
{
    return btck_TransactionOutput::ref(&btck_Coin::get(coin).out);
}

void btck_coin_destroy(btck_Coin* coin)
{
    delete coin;
}

int btck_chainstate_manager_process_block(
    btck_ChainstateManager* chainman,
    const btck_Block* block,
    int* _new_block)
{
    bool new_block;
    auto result = btck_ChainstateManager::get(chainman).m_chainman->ProcessNewBlock(btck_Block::get(block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
    if (_new_block) {
        *_new_block = new_block ? 1 : 0;
    }
    return result ? 0 : -1;
}

const btck_Chain* btck_chainstate_manager_get_active_chain(const btck_ChainstateManager* chainman)
{
    return btck_Chain::ref(&WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(), return btck_ChainstateManager::get(chainman).m_chainman->ActiveChain()));
}

int btck_chain_get_height(const btck_Chain* chain)
{
    LOCK(::cs_main);
    return btck_Chain::get(chain).Height();
}

const btck_BlockTreeEntry* btck_chain_get_by_height(const btck_Chain* chain, int height)
{
    LOCK(::cs_main);
    return btck_BlockTreeEntry::ref(btck_Chain::get(chain)[height]);
}

int btck_chain_contains(const btck_Chain* chain, const btck_BlockTreeEntry* entry)
{
    LOCK(::cs_main);
    return btck_Chain::get(chain).Contains(&btck_BlockTreeEntry::get(entry)) ? 1 : 0;
}


Review comment (refactor):
One thing that could be considered here is returning the `BlockValidationState` directly instead of having an in/out param. To safely do that I think we'd need to refactor `ProcessNewBlockHeaders` though, similarly to what was done in https://github.com/bitcoin/bitcoin/commit/74690f4ed82b1584abb07c0387db0d924c4c0cab. Not sure that is worth it.

Code after:
}

size_t btck_transaction_spent_outputs_count(const btck_TransactionSpentOutputs* transaction_spent_outputs)
{
    return btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size();
}

void btck_transaction_spent_outputs_destroy(btck_TransactionSpentOutputs* transaction_spent_outputs)
{
    delete transaction_spent_outputs;
}

const btck_Coin* btck_transaction_spent_outputs_get_coin_at(const btck_TransactionSpentOutputs* transaction_spent_outputs, size_t coin_index)
{
    assert(coin_index < btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size());
    const Coin* coin{&btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.at(coin_index)};
    return btck_Coin::ref(coin);
}

btck_Coin* btck_coin_copy(const btck_Coin* coin)
{
    return btck_Coin::copy(coin);
}

uint32_t btck_coin_confirmation_height(const btck_Coin* coin)
{
    return btck_Coin::get(coin).nHeight;
}

int btck_coin_is_coinbase(const btck_Coin* coin)
{
    return btck_Coin::get(coin).IsCoinBase() ? 1 : 0;
}

const btck_TransactionOutput* btck_coin_get_output(const btck_Coin* coin)
{
    return btck_TransactionOutput::ref(&btck_Coin::get(coin).out);
}

void btck_coin_destroy(btck_Coin* coin)
{
    delete coin;
}

int btck_chainstate_manager_process_block(
    btck_ChainstateManager* chainman,
    const btck_Block* block,
    int* _new_block)
{
    bool new_block;
    auto result = btck_ChainstateManager::get(chainman).m_chainman->ProcessNewBlock(btck_Block::get(block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);