Skip to content

Commit 08ea314

Browse files
committed
kernel: remove btck_BlockTreeEntry
The previous commit introduced btck_Chain alongside btck_BlockTreeEntry and proved the two are equivalent. This removes the TreeEntry type entirely so the API exposes a single block abstraction. Removed: - the btck_BlockTreeEntry type and its accessors (get_previous, get_block_header, get_height, get_block_hash, equals, get_ancestor) - btck_chainstate_manager_get_best_entry and btck_chainstate_manager_get_block_tree_entry_by_hash - btck_chain_get_by_height and btck_chain_contains - the btck_chain_get_tip / btck_block_tree_entry_get_chain bridges that allowed the two APIs to interoperate during migration This makes the final state as described in purplekarrot.net/blog/drop-block-tree-entry-from-bitcoin-kernel.html.
1 parent 9674ee9 commit 08ea314

5 files changed

Lines changed: 125 additions & 606 deletions

File tree

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class TestValidationInterface : public ValidationInterface
110110
class TestKernelNotifications : public KernelNotifications
111111
{
112112
public:
113-
void BlockTipHandler(SynchronizationState, const BlockTreeEntry, double) override
113+
void BlockTipHandler(SynchronizationState, const ChainView, double) override
114114
{
115115
std::cout << "Block tip changed" << std::endl;
116116
}

src/kernel/bitcoinkernel.cpp

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,9 @@ struct Handle {
146146

147147
} // namespace
148148

149-
struct btck_BlockTreeEntry: Handle<btck_BlockTreeEntry, CBlockIndex> {};
150149
struct btck_Block : Handle<btck_Block, std::shared_ptr<const CBlock>> {};
151150
struct btck_BlockValidationState : Handle<btck_BlockValidationState, BlockValidationState> {};
152151
struct btck_TxValidationState : Handle<btck_TxValidationState, TxValidationState> {};
153-
// A chain is fully determined by its tip block, so it is represented by the
154-
// CBlockIndex of that tip - the same underlying type as a btck_BlockTreeEntry.
155152
struct btck_Chain : Handle<btck_Chain, CBlockIndex> {};
156153

157154
namespace {
@@ -306,7 +303,7 @@ class KernelNotifications final : public kernel::Notifications
306303

307304
kernel::InterruptResult blockTip(SynchronizationState state, const CBlockIndex& index, double verification_progress) override
308305
{
309-
if (m_cbs.block_tip) m_cbs.block_tip(m_cbs.user_data, cast_state(state), btck_BlockTreeEntry::ref(&index), verification_progress);
306+
if (m_cbs.block_tip) m_cbs.block_tip(m_cbs.user_data, cast_state(state), btck_Chain::ref(&index), verification_progress);
310307
return {};
311308
}
312309
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
@@ -366,7 +363,7 @@ class KernelValidationInterface final : public CValidationInterface
366363
if (m_cbs.pow_valid_block) {
367364
m_cbs.pow_valid_block(m_cbs.user_data,
368365
btck_Block::copy(btck_Block::ref(&block)),
369-
btck_BlockTreeEntry::ref(pindex));
366+
btck_Chain::ref(pindex));
370367
}
371368
}
372369

@@ -375,7 +372,7 @@ class KernelValidationInterface final : public CValidationInterface
375372
if (m_cbs.block_connected) {
376373
m_cbs.block_connected(m_cbs.user_data,
377374
btck_Block::copy(btck_Block::ref(&block)),
378-
btck_BlockTreeEntry::ref(pindex));
375+
btck_Chain::ref(pindex));
379376
}
380377
}
381378

@@ -384,7 +381,7 @@ class KernelValidationInterface final : public CValidationInterface
384381
if (m_cbs.block_disconnected) {
385382
m_cbs.block_disconnected(m_cbs.user_data,
386383
btck_Block::copy(btck_Block::ref(&block)),
387-
btck_BlockTreeEntry::ref(pindex));
384+
btck_Chain::ref(pindex));
388385
}
389386
}
390387
};
@@ -897,49 +894,6 @@ void btck_context_destroy(btck_Context* context)
897894
delete context;
898895
}
899896

900-
const btck_BlockTreeEntry* btck_block_tree_entry_get_previous(const btck_BlockTreeEntry* entry)
901-
{
902-
if (!btck_BlockTreeEntry::get(entry).pprev) {
903-
LogInfo("Genesis block has no previous.");
904-
return nullptr;
905-
}
906-
907-
return btck_BlockTreeEntry::ref(btck_BlockTreeEntry::get(entry).pprev);
908-
}
909-
910-
const btck_BlockTreeEntry* btck_block_tree_entry_get_ancestor(const btck_BlockTreeEntry* block_tree_entry, int32_t height)
911-
{
912-
const auto* ancestor{btck_BlockTreeEntry::get(block_tree_entry).GetAncestor(height)};
913-
assert(ancestor);
914-
return btck_BlockTreeEntry::ref(ancestor);
915-
}
916-
917-
btck_BlockHeader* btck_block_tree_entry_get_block_header(const btck_BlockTreeEntry* entry)
918-
{
919-
return btck_BlockHeader::create(btck_BlockTreeEntry::get(entry).GetBlockHeader());
920-
}
921-
922-
int32_t btck_block_tree_entry_get_height(const btck_BlockTreeEntry* entry)
923-
{
924-
return btck_BlockTreeEntry::get(entry).nHeight;
925-
}
926-
927-
const btck_BlockHash* btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry* entry)
928-
{
929-
return btck_BlockHash::ref(btck_BlockTreeEntry::get(entry).phashBlock);
930-
}
931-
932-
int btck_block_tree_entry_equals(const btck_BlockTreeEntry* entry1, const btck_BlockTreeEntry* entry2)
933-
{
934-
return &btck_BlockTreeEntry::get(entry1) == &btck_BlockTreeEntry::get(entry2);
935-
}
936-
937-
const btck_Chain* btck_block_tree_entry_get_chain(const btck_BlockTreeEntry* entry)
938-
{
939-
// A chain and a block tree entry share the same underlying CBlockIndex.
940-
return btck_Chain::ref(&btck_BlockTreeEntry::get(entry));
941-
}
942-
943897
btck_BlockValidationState* btck_block_validation_state_create()
944898
{
945899
return btck_BlockValidationState::create();
@@ -1101,29 +1055,12 @@ const btck_Chain* btck_chainstate_manager_find(const btck_ChainstateManager* cha
11011055
return btck_Chain::ref(block_index);
11021056
}
11031057

1104-
const btck_BlockTreeEntry* btck_chainstate_manager_get_block_tree_entry_by_hash(const btck_ChainstateManager* chainman, const btck_BlockHash* block_hash)
1105-
{
1106-
auto block_index = WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(),
1107-
return btck_ChainstateManager::get(chainman).m_chainman->m_blockman.LookupBlockIndex(btck_BlockHash::get(block_hash)));
1108-
if (!block_index) {
1109-
LogDebug(BCLog::KERNEL, "A block with the given hash is not indexed.");
1110-
return nullptr;
1111-
}
1112-
return btck_BlockTreeEntry::ref(block_index);
1113-
}
1114-
11151058
const btck_Chain* btck_chainstate_manager_get_best_chain(const btck_ChainstateManager* chainstate_manager)
11161059
{
11171060
auto& chainman = *btck_ChainstateManager::get(chainstate_manager).m_chainman;
11181061
return btck_Chain::ref(WITH_LOCK(chainman.GetMutex(), return chainman.m_best_header));
11191062
}
11201063

1121-
const btck_BlockTreeEntry* btck_chainstate_manager_get_best_entry(const btck_ChainstateManager* chainstate_manager)
1122-
{
1123-
auto& chainman = *btck_ChainstateManager::get(chainstate_manager).m_chainman;
1124-
return btck_BlockTreeEntry::ref(WITH_LOCK(chainman.GetMutex(), return chainman.m_best_header));
1125-
}
1126-
11271064
void btck_chainstate_manager_destroy(btck_ChainstateManager* chainman)
11281065
{
11291066
{
@@ -1231,10 +1168,10 @@ void btck_block_destroy(btck_Block* block)
12311168
delete block;
12321169
}
12331170

1234-
btck_Block* btck_block_read(const btck_ChainstateManager* chainman, const btck_BlockTreeEntry* entry)
1171+
btck_Block* btck_block_read(const btck_ChainstateManager* chainman, const btck_Chain* chain)
12351172
{
12361173
auto block{std::make_shared<CBlock>()};
1237-
if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlock(*block, btck_BlockTreeEntry::get(entry))) {
1174+
if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlock(*block, btck_Chain::get(chain))) {
12381175
LogError("Failed to read block.");
12391176
return nullptr;
12401177
}
@@ -1266,14 +1203,14 @@ void btck_block_hash_destroy(btck_BlockHash* hash)
12661203
delete hash;
12671204
}
12681205

1269-
btck_BlockSpentOutputs* btck_block_spent_outputs_read(const btck_ChainstateManager* chainman, const btck_BlockTreeEntry* entry)
1206+
btck_BlockSpentOutputs* btck_block_spent_outputs_read(const btck_ChainstateManager* chainman, const btck_Chain* chain)
12701207
{
12711208
auto block_undo{std::make_shared<CBlockUndo>()};
1272-
if (btck_BlockTreeEntry::get(entry).nHeight < 1) {
1209+
if (btck_Chain::get(chain).nHeight < 1) {
12731210
LogDebug(BCLog::KERNEL, "The genesis block does not have any spent outputs.");
12741211
return btck_BlockSpentOutputs::create(block_undo);
12751212
}
1276-
if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlockUndo(*block_undo, btck_BlockTreeEntry::get(entry))) {
1213+
if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlockUndo(*block_undo, btck_Chain::get(chain))) {
12771214
LogError("Failed to read block spent outputs data.");
12781215
return nullptr;
12791216
}
@@ -1385,30 +1322,9 @@ const btck_Chain* btck_chainstate_manager_get_active_chain(const btck_Chainstate
13851322

13861323
int32_t btck_chain_get_height(const btck_Chain* chain)
13871324
{
1388-
LOCK(::cs_main);
13891325
return btck_Chain::get(chain).nHeight;
13901326
}
13911327

1392-
const btck_BlockTreeEntry* btck_chain_get_tip(const btck_Chain* chain)
1393-
{
1394-
// A chain and a block tree entry share the same underlying CBlockIndex.
1395-
return btck_BlockTreeEntry::ref(&btck_Chain::get(chain));
1396-
}
1397-
1398-
const btck_BlockTreeEntry* btck_chain_get_by_height(const btck_Chain* chain, int32_t height)
1399-
{
1400-
LOCK(::cs_main);
1401-
return btck_BlockTreeEntry::ref(btck_Chain::get(chain).GetAncestor(height));
1402-
}
1403-
1404-
int btck_chain_contains(const btck_Chain* chain, const btck_BlockTreeEntry* entry)
1405-
{
1406-
LOCK(::cs_main);
1407-
const auto& tip{btck_Chain::get(chain)};
1408-
const auto& block{btck_BlockTreeEntry::get(entry)};
1409-
return tip.GetAncestor(block.nHeight) == &block ? 1 : 0;
1410-
}
1411-
14121328
btck_BlockHeader* btck_chain_get_block_header(const btck_Chain* chain, int32_t height)
14131329
{
14141330
const auto* index{btck_Chain::get(chain).GetAncestor(height)};

0 commit comments

Comments
 (0)