Skip to content

Commit 8322f85

Browse files
committed
Make weak block association (to_block) protected.
1 parent 50561a9 commit 8322f85

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

include/bitcoin/database/impl/query/archive_read.ipp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,14 @@ inline hash_digest CLASS::get_point_hash(const point_link& link) const NOEXCEPT
156156
TEMPLATE
157157
bool CLASS::get_tx_height(size_t& out, const tx_link& link) const NOEXCEPT
158158
{
159-
// to_block is strong but not necessarily confirmed.
160-
const auto fk = to_block(link);
159+
const auto fk = to_strong(link);
161160
return is_confirmed_block(fk) && get_height(out, fk);
162161
}
163162

164163
TEMPLATE
165164
bool CLASS::get_tx_position(size_t& out, const tx_link& link) const NOEXCEPT
166165
{
167-
// to_block is strong but not necessarily confirmed.
168-
const auto fk = to_block(link);
166+
const auto fk = to_strong(link);
169167
if (!is_confirmed_block(fk))
170168
return false;
171169

include/bitcoin/database/impl/query/confirm.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ TEMPLATE
6868
bool CLASS::is_confirmed_tx(const tx_link& link) const NOEXCEPT
6969
{
7070
// The tx is strong *and* its block is confirmed (by height).
71-
const auto fk = to_block(link);
71+
const auto fk = to_strong(link);
7272
return !fk.is_terminal() && is_confirmed_block(fk);
7373
}
7474

include/bitcoin/database/impl/query/translate.ipp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,47 @@ output_link CLASS::to_prevout(const point_link& link) const NOEXCEPT
165165
// block/tx to block (reverse navigation)
166166
// ----------------------------------------------------------------------------
167167

168+
TEMPLATE
169+
tx_link CLASS::to_strong_tx(const tx_link& link) const NOEXCEPT
170+
{
171+
return to_strong_tx(get_tx_key(link));
172+
}
173+
174+
TEMPLATE
175+
tx_link CLASS::to_strong_tx(const hash_digest& tx_hash) const NOEXCEPT
176+
{
177+
// Get all tx links for tx_hash.
178+
tx_links txs{};
179+
for (auto it = store_.tx.it(tx_hash); it; ++it)
180+
txs.push_back(*it);
181+
182+
// Find the first strong tx of the set and return its link.
183+
for (const auto& tx : txs)
184+
if (!to_block(tx).is_terminal())
185+
return tx;
186+
187+
return {};
188+
}
189+
190+
// protected (weak association)
168191
// Required for confirmation processing.
169192
TEMPLATE
170-
header_link CLASS::to_block(const tx_link& key) const NOEXCEPT
193+
header_link CLASS::to_block(const tx_link& link) const NOEXCEPT
171194
{
172195
table::strong_tx::record strong{};
173-
if (!store_.strong_tx.find(key, strong) || !strong.positive())
196+
if (!store_.strong_tx.find(link, strong) || !strong.positive())
174197
return {};
175198

176199
// Terminal implies not in strong block (reorganized).
177200
return strong.header_fk();
178201
}
179202

203+
TEMPLATE
204+
header_link CLASS::to_strong(const tx_link& link) const NOEXCEPT
205+
{
206+
return to_strong(get_tx_key(link));
207+
}
208+
180209
// Required for confirmation processing.
181210
TEMPLATE
182211
header_link CLASS::to_strong(const hash_digest& tx_hash) const NOEXCEPT
@@ -188,14 +217,12 @@ header_link CLASS::to_strong(const hash_digest& tx_hash) const NOEXCEPT
188217

189218
// Find the first strong tx of the set and return its block.
190219
for (const auto& tx: txs)
191-
{
192-
const auto block = to_block(tx);
193-
if (!block.is_terminal())
220+
if (const auto block = to_block(tx); !block.is_terminal())
194221
return block;
195-
}
196222

197223
return {};
198224
}
225+
199226
TEMPLATE
200227
header_link CLASS::to_parent(const header_link& link) const NOEXCEPT
201228
{

include/bitcoin/database/query.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ class query
263263
output_link to_prevout(const point_link& link) const NOEXCEPT;
264264

265265
/// block/tx to block (reverse navigation)
266-
header_link to_block(const tx_link& key) const NOEXCEPT;
266+
tx_link to_strong_tx(const tx_link& link) const NOEXCEPT;
267+
tx_link to_strong_tx(const hash_digest& tx_hash) const NOEXCEPT;
268+
header_link to_strong(const tx_link& link) const NOEXCEPT;
267269
header_link to_strong(const hash_digest& tx_hash) const NOEXCEPT;
268270
header_link to_parent(const header_link& link) const NOEXCEPT;
269271

@@ -604,6 +606,7 @@ class query
604606

605607
/// Translate.
606608
/// -----------------------------------------------------------------------
609+
header_link to_block(const tx_link& link) const NOEXCEPT;
607610
uint32_t to_input_index(const tx_link& parent_fk,
608611
const point_link& point_fk) const NOEXCEPT;
609612
uint32_t to_output_index(const tx_link& parent_fk,

0 commit comments

Comments
 (0)