forked from libbitcoin/libbitcoin-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol_electrum.cpp
More file actions
128 lines (106 loc) · 4.27 KB
/
protocol_electrum.cpp
File metadata and controls
128 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/server/protocols/protocol_electrum.hpp>
#include <atomic>
#include <variant>
#include <bitcoin/server/define.hpp>
#include <bitcoin/server/interfaces/interfaces.hpp>
#include <bitcoin/server/protocols/protocol_rpc.hpp>
namespace libbitcoin {
namespace server {
#define CLASS protocol_electrum
using namespace std::placeholders;
// Start.
// ----------------------------------------------------------------------------
// github.com/spesmilo/electrum-protocol/blob/master/docs/protocol-methods.rst
void protocol_electrum::start() NOEXCEPT
{
BC_ASSERT(stranded());
if (started())
return;
// Events subscription is asynchronous, events may be missed.
subscribe_events(BIND(handle_event, _1, _2, _3));
// Header methods.
SUBSCRIBE_RPC(handle_blockchain_block_header, _1, _2, _3, _4);
SUBSCRIBE_RPC(handle_blockchain_block_headers, _1, _2, _3, _4, _5);
SUBSCRIBE_RPC(handle_blockchain_headers_subscribe, _1, _2);
// Fee methods.
SUBSCRIBE_RPC(handle_blockchain_estimate_fee, _1, _2, _3, _4);
SUBSCRIBE_RPC(handle_blockchain_relay_fee, _1, _2);
// Address methods.
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_balance, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_history, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_scripthash_get_mempool, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_scripthash_list_unspent, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_scripthash_subscribe, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_scripthash_unsubscribe, _1, _2, _3);
// Transaction methods.
SUBSCRIBE_RPC(handle_blockchain_transaction_broadcast, _1, _2, _3);
SUBSCRIBE_RPC(handle_blockchain_transaction_broadcast_package, _1, _2, _3, _4);
SUBSCRIBE_RPC(handle_blockchain_transaction_get, _1, _2, _3, _4);
SUBSCRIBE_RPC(handle_blockchain_transaction_get_merkle, _1, _2, _3, _4);
SUBSCRIBE_RPC(handle_blockchain_transaction_id_from_pos, _1, _2, _3, _4, _5);
// Server methods.
SUBSCRIBE_RPC(handle_server_add_peer, _1, _2, _3);
SUBSCRIBE_RPC(handle_server_banner, _1, _2);
SUBSCRIBE_RPC(handle_server_donation_address, _1, _2);
SUBSCRIBE_RPC(handle_server_features, _1, _2);
SUBSCRIBE_RPC(handle_server_peers_subscribe, _1, _2);
SUBSCRIBE_RPC(handle_server_ping, _1, _2);
////SUBSCRIBE_RPC(handle_server_version, _1, _2, _3, _4);
// Mempool methods.
SUBSCRIBE_RPC(handle_mempool_get_fee_histogram, _1, _2);
SUBSCRIBE_RPC(handle_mempool_get_info, _1, _2);
protocol_rpc<channel_electrum>::start();
}
void protocol_electrum::stopping(const code& ec) NOEXCEPT
{
BC_ASSERT(stranded());
// Unsubscription is asynchronous, race is ok.
unsubscribe_events();
protocol_rpc<channel_electrum>::stopping(ec);
}
// Handlers (event subscription).
// ----------------------------------------------------------------------------
bool protocol_electrum::handle_event(const code&, node::chase event_,
node::event_value value) NOEXCEPT
{
// Do not pass ec to stopped as it is not a call status.
if (stopped())
return false;
switch (event_)
{
case node::chase::organized:
{
if (subscribed_.load(std::memory_order_relaxed))
{
BC_ASSERT(std::holds_alternative<node::header_t>(value));
POST(do_header, std::get<node::header_t>(value));
}
break;
}
default:
{
break;
}
}
return true;
}
} // namespace server
} // namespace libbitcoin