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