Skip to content

Commit c4d116f

Browse files
authored
Merge pull request #947 from evoskuil/master
Stub in stratum_v1 interface.
2 parents cd72115 + cf91b46 commit c4d116f

File tree

10 files changed

+527
-113
lines changed

10 files changed

+527
-113
lines changed

include/bitcoin/node/define.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,37 @@ using type_id = network::messages::peer::inventory_item::type_id;
102102
} // namespace node
103103
} // namespace libbitcoin
104104

105+
/// Augment limited xcode placeholder defines (10 vs. common 20).
106+
/// ---------------------------------------------------------------------------
107+
#if defined (HAVE_XCODE)
108+
109+
/// Define custom placeholder types in the global namespace to avoid conflicts.
110+
struct placeholder_11 {};
111+
struct placeholder_12 {};
112+
struct placeholder_13 {};
113+
114+
/// Specialize std::is_placeholder within the std namespace.
115+
namespace std
116+
{
117+
template <>
118+
struct is_placeholder<::placeholder_11> : integral_constant<int, 11> {};
119+
template <>
120+
struct is_placeholder<::placeholder_12> : integral_constant<int, 12> {};
121+
template <>
122+
struct is_placeholder<::placeholder_13> : integral_constant<int, 13> {};
123+
}
124+
125+
/// Add instances to std::placeholders for standard usage syntax.
126+
namespace std::placeholders
127+
{
128+
inline constexpr ::placeholder_11 _11{};
129+
inline constexpr ::placeholder_12 _12{};
130+
inline constexpr ::placeholder_13 _13{};
131+
}
132+
133+
#endif // HAVE_XCODE
134+
/// ---------------------------------------------------------------------------
135+
105136
#endif
106137

107138
// define.hpp is the common include for /node.

include/bitcoin/node/interfaces/electrum.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ struct electrum_methods
5555
method<"server.features">{},
5656
method<"server.peers.subscribe">{},
5757
method<"server.ping">{},
58-
method<"server.version", string_t, value_t>{ "client_name", "protocol_version" },
58+
method<"server.version", string_t, optional<empty::value>>{ "client_name", "protocol_version" },
5959

6060
/// Mempool methods.
6161
method<"mempool.get_fee_histogram">{}
6262
};
6363

6464
template <typename... Args>
65-
using subscriber = network::unsubscriber<Args...>;
65+
using subscriber = network::subscriber<Args...>;
6666

6767
template <size_t Index>
6868
using at = method_at<methods, Index>;

include/bitcoin/node/interfaces/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ using number_t = network::rpc::number_t;
4545
using object_t = network::rpc::object_t;
4646
using array_t = network::rpc::array_t;
4747
using value_t = network::rpc::value_t;
48+
using null_t = network::rpc::null_t;
4849

4950
namespace empty { constexpr auto array = network::rpc::empty::array; };
5051
namespace empty { constexpr auto object = network::rpc::empty::object; };

include/bitcoin/node/protocols/protocol_electrum.hpp

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_ELECTRUM_HPP
2121

2222
#include <memory>
23+
#include <unordered_map>
2324
#include <bitcoin/node/channels/channels.hpp>
2425
#include <bitcoin/node/define.hpp>
2526
#include <bitcoin/node/interfaces/interfaces.hpp>
@@ -48,70 +49,138 @@ class BCN_API protocol_electrum
4849

4950
protected:
5051
/// Handlers (blockchain).
51-
bool handle_blockchain_block_header(const code& ec,
52+
void handle_blockchain_block_header(const code& ec,
5253
rpc_interface::blockchain_block_header, double height,
5354
double cp_height) NOEXCEPT;
54-
bool handle_blockchain_block_headers(const code& ec,
55+
void handle_blockchain_block_headers(const code& ec,
5556
rpc_interface::blockchain_block_headers, double start_height,
5657
double count, double cp_height) NOEXCEPT;
57-
bool handle_blockchain_headers_subscribe(const code& ec,
58+
void handle_blockchain_headers_subscribe(const code& ec,
5859
rpc_interface::blockchain_headers_subscribe) NOEXCEPT;
59-
bool handle_blockchain_estimatefee(const code& ec,
60+
void handle_blockchain_estimatefee(const code& ec,
6061
rpc_interface::blockchain_estimatefee, double) NOEXCEPT;
61-
bool handle_blockchain_relayfee(const code& ec,
62+
void handle_blockchain_relayfee(const code& ec,
6263
rpc_interface::blockchain_relayfee) NOEXCEPT;
63-
bool handle_blockchain_scripthash_get_balance(const code& ec,
64+
void handle_blockchain_scripthash_get_balance(const code& ec,
6465
rpc_interface::blockchain_scripthash_get_balance,
6566
const std::string& scripthash) NOEXCEPT;
66-
bool handle_blockchain_scripthash_get_history(const code& ec,
67+
void handle_blockchain_scripthash_get_history(const code& ec,
6768
rpc_interface::blockchain_scripthash_get_history,
6869
const std::string& scripthash) NOEXCEPT;
69-
bool handle_blockchain_scripthash_get_mempool(const code& ec,
70+
void handle_blockchain_scripthash_get_mempool(const code& ec,
7071
rpc_interface::blockchain_scripthash_get_mempool,
7172
const std::string& scripthash) NOEXCEPT;
72-
bool handle_blockchain_scripthash_listunspent(const code& ec,
73+
void handle_blockchain_scripthash_listunspent(const code& ec,
7374
rpc_interface::blockchain_scripthash_listunspent,
7475
const std::string& scripthash) NOEXCEPT;
75-
bool handle_blockchain_scripthash_subscribe(const code& ec,
76+
void handle_blockchain_scripthash_subscribe(const code& ec,
7677
rpc_interface::blockchain_scripthash_subscribe,
7778
const std::string& scripthash) NOEXCEPT;
78-
bool handle_blockchain_scripthash_unsubscribe(const code& ec,
79+
void handle_blockchain_scripthash_unsubscribe(const code& ec,
7980
rpc_interface::blockchain_scripthash_unsubscribe,
8081
const std::string& scripthash) NOEXCEPT;
81-
bool handle_blockchain_transaction_broadcast(const code& ec,
82+
void handle_blockchain_transaction_broadcast(const code& ec,
8283
rpc_interface::blockchain_transaction_broadcast,
8384
const std::string& raw_tx) NOEXCEPT;
84-
bool handle_blockchain_transaction_get(const code& ec,
85+
void handle_blockchain_transaction_get(const code& ec,
8586
rpc_interface::blockchain_transaction_get, const std::string& tx_hash,
8687
bool verbose) NOEXCEPT;
87-
bool handle_blockchain_transaction_get_merkle(const code& ec,
88+
void handle_blockchain_transaction_get_merkle(const code& ec,
8889
rpc_interface::blockchain_transaction_get_merkle,
8990
const std::string& tx_hash, double height) NOEXCEPT;
90-
bool handle_blockchain_transaction_id_from_pos(const code& ec,
91+
void handle_blockchain_transaction_id_from_pos(const code& ec,
9192
rpc_interface::blockchain_transaction_id_from_pos, double height,
9293
double tx_pos, bool merkle) NOEXCEPT;
9394

9495
/// Handlers (server).
95-
bool handle_server_add_peer(const code& ec,
96+
void handle_server_add_peer(const code& ec,
9697
rpc_interface::server_add_peer,
97-
const network::rpc::object_t& features) NOEXCEPT;
98-
bool handle_server_banner(const code& ec,
98+
const interface::object_t& features) NOEXCEPT;
99+
void handle_server_banner(const code& ec,
99100
rpc_interface::server_banner) NOEXCEPT;
100-
bool handle_server_donation_address(const code& ec,
101+
void handle_server_donation_address(const code& ec,
101102
rpc_interface::server_donation_address) NOEXCEPT;
102-
bool handle_server_features(const code& ec,
103+
void handle_server_features(const code& ec,
103104
rpc_interface::server_features) NOEXCEPT;
104-
bool handle_server_peers_subscribe(const code& ec,
105+
void handle_server_peers_subscribe(const code& ec,
105106
rpc_interface::server_peers_subscribe) NOEXCEPT;
106-
bool handle_server_ping(const code& ec,
107+
void handle_server_ping(const code& ec,
107108
rpc_interface::server_ping) NOEXCEPT;
108-
bool handle_server_version(const code& ec,
109+
void handle_server_version(const code& ec,
109110
rpc_interface::server_version, const std::string& client_name,
110-
const network::rpc::value_t& protocol_version) NOEXCEPT;
111+
const interface::value_t& protocol_version) NOEXCEPT;
111112

112113
/// Handlers (mempool).
113-
bool handle_mempool_get_fee_histogram(const code& ec,
114+
void handle_mempool_get_fee_histogram(const code& ec,
114115
rpc_interface::mempool_get_fee_histogram) NOEXCEPT;
116+
117+
protected:
118+
enum class protocol_version
119+
{
120+
/// Invalid version.
121+
v0_0,
122+
123+
/// 2011, initial protocol negotiation.
124+
v0_6,
125+
126+
/// 2012, enhanced protocol negotiation.
127+
v0_8,
128+
129+
/// 2012, added pruning limits and transport indicators.
130+
v0_9,
131+
132+
/// 2013, baseline for core methods in the official specification.
133+
v0_10,
134+
135+
/// 2014, 1.x series, deprecations of utxo and block number methods.
136+
v1_0,
137+
138+
/// 2015, updated version response and introduced scripthash methods.
139+
v1_1,
140+
141+
/// 2017, added optional parameters for transactions and headers.
142+
v1_2,
143+
144+
/// 2018, defaulted raw headers and introduced new block methods.
145+
v1_3,
146+
147+
/// 2019, removed deserialized headers and added merkle proof features.
148+
v1_4,
149+
150+
/// 2019, modifications for auxiliary proof-of-work handling.
151+
v1_4_1,
152+
153+
/// 2020, added scripthash unsubscribe functionality.
154+
v1_4_2,
155+
156+
/// 2022, updated response formats and added fee estimation modes.
157+
v1_6
158+
};
159+
160+
static constexpr protocol_version minimum = protocol_version::v1_4;
161+
static constexpr protocol_version maximum = protocol_version::v1_4_2;
162+
163+
protocol_version version() const NOEXCEPT;
164+
std::string_view get_version() const NOEXCEPT;
165+
bool is_version(protocol_version version) const NOEXCEPT;
166+
bool set_version(const interface::value_t& version) NOEXCEPT;
167+
bool get_versions(protocol_version& min, protocol_version& max,
168+
const interface::value_t& version) NOEXCEPT;
169+
170+
static std::string_view get_server() NOEXCEPT;
171+
std::string_view get_client() const NOEXCEPT;
172+
std::string escape_client(const std::string& in) NOEXCEPT;
173+
bool set_client(const std::string& name) NOEXCEPT;
174+
175+
private:
176+
static std::string_view version_to_string(
177+
protocol_version version) NOEXCEPT;
178+
static protocol_version version_from_string(
179+
const std::string_view& version) NOEXCEPT;
180+
181+
// These are protected by strand.
182+
protocol_version version_{ protocol_version::v0_0 };
183+
std::string name_{};
115184
};
116185

117186
} // namespace node

include/bitcoin/node/protocols/protocol_stratum_v1.hpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,43 @@ class BCN_API protocol_stratum_v1
4646
void start() NOEXCEPT override;
4747

4848
protected:
49-
/// Handlers.
49+
/// Handlers (client requests).
50+
bool handle_mining_subscribe(const code& ec,
51+
rpc_interface::mining_subscribe, const std::string& user_agent,
52+
double extranonce1_size) NOEXCEPT;
53+
bool handle_mining_authorize(const code& ec,
54+
rpc_interface::mining_authorize, const std::string& username,
55+
const std::string& password) NOEXCEPT;
56+
bool handle_mining_submit(const code& ec,
57+
rpc_interface::mining_submit, const std::string& worker_name,
58+
const std::string& job_id, const std::string& extranonce2,
59+
double ntime, const std::string& nonce) NOEXCEPT;
5060
bool handle_mining_extranonce_subscribe(const code& ec,
5161
rpc_interface::mining_extranonce_subscribe) NOEXCEPT;
62+
bool handle_mining_extranonce_unsubscribe(const code& ec,
63+
rpc_interface::mining_extranonce_unsubscribe, double id) NOEXCEPT;
64+
65+
/// Handlers (server notifications).
66+
bool handle_mining_configure(const code& ec,
67+
rpc_interface::mining_configure,
68+
const interface::object_t& extensions) NOEXCEPT;
69+
bool handle_mining_set_difficulty(const code& ec,
70+
rpc_interface::mining_set_difficulty, double difficulty) NOEXCEPT;
71+
bool handle_mining_notify(const code& ec,
72+
rpc_interface::mining_notify, const std::string& job_id,
73+
const std::string& prevhash, const std::string& coinb1,
74+
const std::string& coinb2, const interface::array_t& merkle_branch,
75+
double version, double nbits, double ntime, bool clean_jobs,
76+
bool hash1, bool hash2) NOEXCEPT;
77+
bool handle_client_reconnect(const code& ec,
78+
rpc_interface::client_reconnect, const std::string& url, double port,
79+
double id) NOEXCEPT;
80+
bool handle_client_hello(const code& ec,
81+
rpc_interface::client_hello,
82+
const interface::object_t& protocol) NOEXCEPT;
83+
bool handle_client_rejected(const code& ec,
84+
rpc_interface::client_rejected, const std::string& job_id,
85+
const std::string& reject_reason) NOEXCEPT;
5286
};
5387

5488
} // namespace node

include/bitcoin/node/sessions/session_server.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ class session_server
9292
}
9393

9494
/// Override to implement a connection handshake as required. By default
95-
/// this is bypassed, which applies to basic http services. A handshake
96-
/// is used to implement TLS and WebSocket upgrade from http (for example).
97-
/// Handshake protocol(s) must invoke handler one time at completion.
98-
/// Use std::dynamic_pointer_cast<channel_t>(channel) to obtain channel_t.
95+
/// this is bypassed, which applies to basic http services. Handshake
96+
/// protocol(s) must invoke handler one time at completion. Use
97+
/// std::dynamic_pointer_cast<channel_t>(channel) to obtain channel_t.
9998
inline void attach_handshake(const channel_ptr& channel,
10099
network::result_handler&& handler) NOEXCEPT override
101100
{

src/protocols/protocol_bitcoind_rest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ void protocol_bitcoind_rest::handle_receive_get(const code& ec,
9898
// Handlers.
9999
// ----------------------------------------------------------------------------
100100

101-
constexpr auto data = to_value(media_type::application_octet_stream);
102-
constexpr auto json = to_value(media_type::application_json);
103-
constexpr auto text = to_value(media_type::text_plain);
101+
////constexpr auto data = to_value(media_type::application_octet_stream);
102+
////constexpr auto json = to_value(media_type::application_json);
103+
////constexpr auto text = to_value(media_type::text_plain);
104104

105105
bool protocol_bitcoind_rest::handle_get_block(const code& ec,
106106
rest_interface::block, uint8_t , system::hash_cptr ) NOEXCEPT

0 commit comments

Comments
 (0)