Skip to content

Commit 8037572

Browse files
authored
Merge pull request #780 from evoskuil/master
Emit "stripped" field on native configuration method.
2 parents fc999f8 + 36f3d1e commit 8037572

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

include/bitcoin/server/protocols/protocol_native.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ class BCS_API protocol_native
251251
// Utilities.
252252
// ------------------------------------------------------------------------
253253

254+
size_t get_stripped_height() NOEXCEPT;
255+
size_t get_active_height(const system::hash_digest& hash) NOEXCEPT;
256+
254257
void inject(boost::json::value& out, std::optional<uint32_t> height,
255258
const database::header_link& link) const NOEXCEPT;
256259

src/protocols/native/protocol_native_configuration.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bool protocol_native::handle_get_configuration(const code& ec,
5050
{ "witness", network_settings().witness_node() },
5151
{ "retarget", system_settings().forks.retarget },
5252
{ "difficult", system_settings().forks.difficult },
53+
{ "stripped", get_stripped_height() },
5354
};
5455
BC_POP_WARNING()
5556

@@ -101,5 +102,40 @@ bool protocol_native::handle_get_event_subscribe(const code& ec,
101102
return true;
102103
}
103104

105+
// utility
106+
// ----------------------------------------------------------------------------
107+
108+
size_t protocol_native::get_stripped_height() NOEXCEPT
109+
{
110+
if (!network_settings().pruned_node() ||
111+
!network_settings().witness_node())
112+
return {};
113+
114+
const auto& system_ = system_settings();
115+
const auto milestone = get_active_height(system_.milestone.hash());
116+
const auto checkpoint = get_active_height(system_.top_checkpoint().hash());
117+
return std::max(milestone, checkpoint);
118+
}
119+
120+
size_t protocol_native::get_active_height(
121+
const system::hash_digest& hash) NOEXCEPT
122+
{
123+
const auto& query = archive();
124+
const auto link = query.to_header(hash);
125+
system::chain::context ctx{};
126+
127+
// This uses candidate header vs. confirmed block so that stripping will
128+
// show at heights below top checkpoint and milestone while still syncing.
129+
// This can result in a weak branch milestone block implying a pruned
130+
// height that doesn't confirm (ok), which can't happen for checkpoints.
131+
// This can be mitigated by setting stripped on the object when it's a
132+
// milestone or checked object and pruning is enabled. Pruning can be
133+
// enabled after storage of non-stripped blocks, which does not strip them.
134+
// This will return false is bip141 is not active by the milestone.
135+
return query.get_context(ctx, link) &&
136+
ctx.is_enabled(system::chain::bip141_rule) &&
137+
query.is_candidate_header(link) ? ctx.height : zero;
138+
}
139+
104140
} // namespace server
105141
} // namespace libbitcoin

0 commit comments

Comments
 (0)