Skip to content

Commit aec9f18

Browse files
authored
Merge pull request #706 from evoskuil/master
Make chaser strand() and stranded() virtual, update neutrino.
2 parents b8e4426 + 1b92c4e commit aec9f18

File tree

12 files changed

+207
-150
lines changed

12 files changed

+207
-150
lines changed

console/executor_store.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool executor::check_store_path(bool create) const
5959
if (create)
6060
{
6161
logger(format(BN_INITIALIZING_CHAIN) % store);
62-
if (auto ec = database::file::create_directory_ex(store))
62+
if (const auto ec = database::file::create_directory_ex(store))
6363
{
6464
logger(format(BN_INITCHAIN_DIRECTORY_ERROR) % store % ec.message());
6565
return false;

include/bitcoin/node/chasers/chaser.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ class BCN_API chaser
107107
virtual void notify_one(object_key key, const code& ec, chase event_,
108108
event_value value) const NOEXCEPT;
109109

110+
/// Strand.
111+
/// -----------------------------------------------------------------------
112+
113+
/// The chaser's strand (on the network threadpool).
114+
virtual network::asio::strand& strand() NOEXCEPT;
115+
116+
/// True if the current thread is on the chaser strand.
117+
virtual bool stranded() const NOEXCEPT;
118+
110119
/// Properties.
111120
/// -----------------------------------------------------------------------
112121

@@ -116,12 +125,6 @@ class BCN_API chaser
116125
/// Thread safe synchronous archival interface.
117126
query& archive() const NOEXCEPT;
118127

119-
/// The chaser's strand.
120-
network::asio::strand& strand() NOEXCEPT;
121-
122-
/// True if the current thread is on the chaser strand.
123-
bool stranded() const NOEXCEPT;
124-
125128
/// Top candidate is within configured span from current time.
126129
bool is_current() const NOEXCEPT;
127130

@@ -153,6 +156,9 @@ class BCN_API chaser
153156
#define SUBSCRIBE_EVENTS(method, ...) \
154157
subscribe_events(BIND(method, __VA_ARGS__))
155158

159+
#define PARALLEL(method, ...) \
160+
boost::asio::post(threadpool_.service(), BIND(method, __VA_ARGS__));
161+
156162
} // namespace node
157163
} // namespace libbitcoin
158164

include/bitcoin/node/chasers/chaser_confirm.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,20 @@ class BCN_API chaser_confirm
5959
////virtual void do_organize(header_links& fork, const header_links& popped,
6060
//// size_t fork_point) NOEXCEPT;
6161

62+
// Override base class strand because it sits on the network thread pool.
63+
network::asio::strand& strand() NOEXCEPT override;
64+
bool stranded() const NOEXCEPT override;
65+
6266
private:
67+
struct neutrino_header
68+
{
69+
system::hash_digest head{};
70+
database::header_link link{};
71+
};
72+
73+
void reset_position(size_t confirmed_height) NOEXCEPT;
74+
bool update_neutrino(const database::header_link& link) NOEXCEPT;
75+
6376
bool set_organized(const database::header_link& link,
6477
height_t height) NOEXCEPT;
6578
bool reset_organized(const database::header_link& link,
@@ -74,12 +87,14 @@ class BCN_API chaser_confirm
7487
bool get_is_strong(bool& strong, const uint256_t& fork_work,
7588
size_t fork_point) const NOEXCEPT;
7689

77-
// This is thread safe.
90+
// These are thread safe.
7891
const bool concurrent_;
92+
network::asio::strand independent_strand_;
7993

8094
// These are protected by strand.
8195
network::threadpool threadpool_;
82-
network::asio::strand strand_;
96+
neutrino_header neutrino_{};
97+
bool filters_{};
8398
bool mature_{};
8499
};
85100

include/bitcoin/node/chasers/chaser_validate.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,33 @@ class BCN_API chaser_validate
5656
virtual void complete_block(const code& ec,
5757
const database::header_link& link, size_t height) NOEXCEPT;
5858

59+
// Override base class strand because it sits on the network thread pool.
60+
network::asio::strand& strand() NOEXCEPT override;
61+
bool stranded() const NOEXCEPT override;
62+
5963
private:
60-
// neutrino
61-
void update_position(size_t height) NOEXCEPT;
62-
system::hash_digest get_neutrino(size_t height) const NOEXCEPT;
63-
bool update_neutrino(const database::header_link& link) NOEXCEPT;
64-
bool update_neutrino(const database::header_link& link,
64+
inline bool unfilled() const NOEXCEPT
65+
{
66+
return backlog_ < maximum_backlog_;
67+
}
68+
69+
bool set_neutrino(const database::header_link& link,
70+
const system::chain::block& block) NOEXCEPT;
71+
72+
bool set_prevouts(size_t height,
6573
const system::chain::block& block) NOEXCEPT;
6674

6775
// These are thread safe.
6876
const bool concurrent_;
6977
const size_t maximum_backlog_;
7078
const uint64_t initial_subsidy_;
7179
const uint32_t subsidy_interval_;
80+
network::asio::strand independent_strand_;
7281

7382
// These are protected by strand.
7483
network::threadpool threadpool_;
75-
network::asio::strand strand_;
76-
system::hash_digest neutrino_{};
77-
size_t validation_backlog_{};
84+
size_t backlog_{};
85+
bool filters_{};
7886
bool mature_{};
7987
};
8088

include/bitcoin/node/error.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ enum error_t : uint8_t
8282
validate4,
8383
validate5,
8484
validate6,
85+
validate7,
8586
confirm1,
8687
confirm2,
8788
confirm3,

include/bitcoin/node/settings.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ class BCN_API settings
8888
uint32_t threads;
8989

9090
/// Helpers.
91+
virtual size_t threads_() const NOEXCEPT;
9192
virtual size_t maximum_height_() const NOEXCEPT;
9293
virtual size_t maximum_concurrency_() const NOEXCEPT;
9394
virtual network::steady_clock::duration sample_period() const NOEXCEPT;
9495
virtual network::wall_clock::duration currency_window() const NOEXCEPT;
96+
virtual network::thread_priority priority_() const NOEXCEPT;
9597
};
9698

9799
} // namespace node

src/chasers/chaser.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,30 @@ void chaser::notify_one(object_key key, const code& ec, chase event_,
102102
node_.notify_one(key, ec, event_, value);
103103
}
104104

105-
// Properties.
105+
// Strand.
106106
// ----------------------------------------------------------------------------
107107

108-
const node::configuration& chaser::config() const NOEXCEPT
108+
asio::strand& chaser::strand() NOEXCEPT
109109
{
110-
return node_.config();
110+
return strand_;
111111
}
112112

113-
query& chaser::archive() const NOEXCEPT
113+
bool chaser::stranded() const NOEXCEPT
114114
{
115-
return node_.archive();
115+
return strand_.running_in_this_thread();
116116
}
117117

118-
asio::strand& chaser::strand() NOEXCEPT
118+
// Properties.
119+
// ----------------------------------------------------------------------------
120+
121+
const node::configuration& chaser::config() const NOEXCEPT
119122
{
120-
return strand_;
123+
return node_.config();
121124
}
122125

123-
bool chaser::stranded() const NOEXCEPT
126+
query& chaser::archive() const NOEXCEPT
124127
{
125-
return strand_.running_in_this_thread();
128+
return node_.archive();
126129
}
127130

128131
bool chaser::is_current() const NOEXCEPT

src/chasers/chaser_confirm.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
4242
chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT
4343
: chaser(node),
4444
concurrent_(node.config().node.concurrent_confirmation),
45-
threadpool_(1_u32, node.config().node.priority_validation ?
46-
network::thread_priority::high : network::thread_priority::normal),
47-
strand_(threadpool_.service().get_executor())
45+
threadpool_(one, node.config().node.priority_()),
46+
independent_strand_(threadpool_.service().get_executor())
4847
{
4948
}
5049

5150
code chaser_confirm::start() NOEXCEPT
5251
{
53-
set_position(archive().get_fork());
52+
const auto& query = archive();
53+
filters_ = query.neutrino_enabled();
54+
reset_position(query.get_fork());
5455
SUBSCRIBE_EVENTS(handle_event, _1, _2, _3);
5556
return error::success;
5657
}
@@ -71,9 +72,6 @@ void chaser_confirm::stop() NOEXCEPT
7172
}
7273
}
7374

74-
// Protected
75-
// ----------------------------------------------------------------------------
76-
7775
bool chaser_confirm::handle_event(const code&, chase event_,
7876
event_value value) NOEXCEPT
7977
{
@@ -106,9 +104,7 @@ bool chaser_confirm::handle_event(const code&, chase event_,
106104
{
107105
if (concurrent_ || mature_)
108106
{
109-
////POST(do_bump, height_t{});
110-
boost::asio::post(strand_,
111-
BIND(do_bump, height_t{}));
107+
POST(do_bump, height_t{});
112108
}
113109

114110
break;
@@ -120,27 +116,21 @@ bool chaser_confirm::handle_event(const code&, chase event_,
120116

121117
if (concurrent_ || mature_)
122118
{
123-
////POST(do_validated, std::get<height_t>(value));
124-
boost::asio::post(strand_,
125-
BIND(do_validated, std::get<height_t>(value)));
119+
POST(do_validated, std::get<height_t>(value));
126120
}
127121

128122
break;
129123
}
130124
case chase::regressed:
131125
{
132126
BC_ASSERT(std::holds_alternative<height_t>(value));
133-
////POST(do_regressed, std::get<height_t>(value));
134-
boost::asio::post(strand_,
135-
BIND(do_regressed, std::get<height_t>(value)));
127+
POST(do_regressed, std::get<height_t>(value));
136128
break;
137129
}
138130
case chase::disorganized:
139131
{
140132
BC_ASSERT(std::holds_alternative<height_t>(value));
141-
////POST(do_regressed, std::get<height_t>(value));
142-
boost::asio::post(strand_,
143-
BIND(do_regressed, std::get<height_t>(value)));
133+
POST(do_regressed, std::get<height_t>(value));
144134
break;
145135
}
146136
case chase::stop:
@@ -156,6 +146,9 @@ bool chaser_confirm::handle_event(const code&, chase event_,
156146
return true;
157147
}
158148

149+
// track validation
150+
// ----------------------------------------------------------------------------
151+
159152
void chaser_confirm::do_regressed(height_t branch_point) NOEXCEPT
160153
{
161154
BC_ASSERT(stranded());
@@ -170,14 +163,14 @@ void chaser_confirm::do_regressed(height_t branch_point) NOEXCEPT
170163
}
171164
}
172165

173-
set_position(branch_point);
166+
reset_position(branch_point);
174167
}
175168

169+
// Candidate block validated at given height, if next then confirm/advance.
176170
void chaser_confirm::do_validated(height_t height) NOEXCEPT
177171
{
178172
BC_ASSERT(stranded());
179173

180-
// Candidate block was validated at the given height, confirm/advance.
181174
if (height == add1(position()))
182175
do_bump(height);
183176
}
@@ -244,9 +237,8 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
244237
<< ec.message());
245238
return;
246239
}
247-
248-
// TODO: fees.
249-
if (!query.set_block_confirmable(link, {}))
240+
241+
if (!query.set_block_confirmable(link))
250242
{
251243
fault(error::confirm6);
252244
return;
@@ -283,6 +275,7 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
283275
return;
284276
}
285277

278+
update_neutrino(link);
286279
set_position(height);
287280
}
288281
}
@@ -617,6 +610,56 @@ bool chaser_confirm::get_is_strong(bool& strong, const uint256_t& fork_work,
617610
return true;
618611
}
619612

613+
// neutrino
614+
// ----------------------------------------------------------------------------
615+
616+
// This can only fail if prevouts are not fully populated.
617+
bool chaser_confirm::update_neutrino(const header_link& link) NOEXCEPT
618+
{
619+
// neutrino_.link is only used for this assertion, should compile away.
620+
BC_ASSERT(archive().get_height(link) ==
621+
add1(archive().get_height(neutrino_.link)));
622+
623+
if (!filters_)
624+
return true;
625+
626+
data_chunk filter{};
627+
auto& query = archive();
628+
if (!query.get_filter_body(filter, link))
629+
return false;
630+
631+
neutrino_.link = link;
632+
neutrino_.head = neutrino::compute_filter_header(neutrino_.head, filter);
633+
return query.set_filter_head(link, neutrino_.head);
634+
}
635+
636+
// Expects confirmed height.
637+
// Use for startup and regression, to read current filter header from store.
638+
void chaser_confirm::reset_position(size_t confirmed_height) NOEXCEPT
639+
{
640+
set_position(confirmed_height);
641+
642+
if (filters_)
643+
{
644+
const auto& query = archive();
645+
neutrino_.link = query.to_confirmed(confirmed_height);
646+
query.get_filter_head(neutrino_.head, neutrino_.link);
647+
}
648+
}
649+
650+
// Strand.
651+
// ----------------------------------------------------------------------------
652+
653+
network::asio::strand& chaser_confirm::strand() NOEXCEPT
654+
{
655+
return independent_strand_;
656+
}
657+
658+
bool chaser_confirm::stranded() const NOEXCEPT
659+
{
660+
return independent_strand_.running_in_this_thread();
661+
}
662+
620663
BC_POP_WARNING()
621664

622665
} // namespace node

0 commit comments

Comments
 (0)