Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/bitcoin/server/protocols/protocol_electrum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ class BCS_API protocol_electrum
array_t transform(const database::histories& histories) NOEXCEPT;
array_t transform(const database::unspents& unspents) NOEXCEPT;

// Shared get_status implementation.
bool send_get_status(const std::string& tx_hash, double txout_idx,
const std::string& spk_hint) NOEXCEPT;

// These are thread safe.
const options_t& options_;
const bool turbo_;
Expand Down
4 changes: 4 additions & 0 deletions src/protocols/electrum/protocol_electrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using namespace system;
using namespace network::rpc;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

// Electrum could be factored into protocols by version, with version-dependent
// protocol attachment and with protocol derivations (see p2p). Currently all
// methods apart from version are in one protocol class.
Expand Down Expand Up @@ -310,5 +312,7 @@ void protocol_electrum::do_scriptpubkey(node::address_t) NOEXCEPT
}, 128, BIND(handle_send, _1));
}

BC_POP_WARNING()

} // namespace server
} // namespace libbitcoin
3 changes: 2 additions & 1 deletion src/protocols/electrum/protocol_electrum_mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void protocol_electrum::handle_mempool_get_fee_histogram(const code& ec,
}

// TODO: Empty array (of tuples), could be simulated with block fees.
send_result(array_t{}, 42, BIND(complete, _1));
////send_result(array_t{}, 42, BIND(complete, _1));
send_code(error::not_implemented);
}

void protocol_electrum::handle_mempool_get_info(const code& ec,
Expand Down
149 changes: 78 additions & 71 deletions src/protocols/electrum/protocol_electrum_outputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <bitcoin/server/protocols/protocol_electrum.hpp>

#include <utility>
#include <bitcoin/server/define.hpp>

namespace libbitcoin {
Expand All @@ -29,6 +30,8 @@ using namespace system;
using namespace network::rpc;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

void protocol_electrum::handle_blockchain_utxo_get_address(const code& ec,
rpc_interface::blockchain_utxo_get_address, const std::string& tx_hash,
double index) NOEXCEPT
Expand Down Expand Up @@ -91,48 +94,32 @@ void protocol_electrum::handle_blockchain_outpoint_get_status(const code& ec,
return;
}

uint32_t index{};
hash_digest hash{};
if (!to_integer(index, txout_idx) ||
!decode_hash(hash, tx_hash))
{
send_code(error::invalid_argument);
send_get_status(tx_hash, txout_idx, spk_hint);
}

void protocol_electrum::handle_blockchain_outpoint_subscribe(const code& ec,
rpc_interface::blockchain_outpoint_subscribe, const std::string& tx_hash,
double txout_idx, const std::string& spk_hint) NOEXCEPT
{
if (stopped(ec))
return;
}

// script is advisory, and should match prevout script.
if (!spk_hint.empty())
if (!at_least(electrum::version::v1_7))
{
data_chunk bytes{};
if (!decode_base16(bytes, spk_hint))
{
send_code(error::invalid_argument);
return;
}

chain::script script{ std::move(bytes), false };
if (!script.is_valid())
{
send_code(error::invalid_argument);
return;
}
send_code(error::wrong_version);
return;
}

// TODO: implement outpoint status query.
chain::point prevout{ hash, index };
send_result(object_t
{
{ "height", 24 },
{ "spender_txhash", "<hash>" },
{ "spender_height", 42 }
if (!send_get_status(tx_hash, txout_idx, spk_hint))
return;

}, 16, BIND(complete, _1));
// TODO: collect the outpoint into a limited notification set.
subscribed_outpoint_.store(false, std::memory_order_relaxed);
}

// TODO: implement.
void protocol_electrum::handle_blockchain_outpoint_subscribe(const code& ec,
rpc_interface::blockchain_outpoint_subscribe, const std::string& tx_hash,
double txout_idx, const std::string& spk_hint) NOEXCEPT
void protocol_electrum::handle_blockchain_outpoint_unsubscribe(const code& ec,
rpc_interface::blockchain_outpoint_unsubscribe, const std::string& tx_hash,
double txout_idx) NOEXCEPT
{
if (stopped(ec))
return;
Expand All @@ -152,67 +139,87 @@ void protocol_electrum::handle_blockchain_outpoint_subscribe(const code& ec,
return;
}

// script is advisory, but should match prevout script.
// TODO: remove outpoint subscription from notification set.
chain::point prevout{ hash, index };
const auto previous = subscribed_scriptpubkey_.load(
std::memory_order_relaxed);

send_result(previous, 16, BIND(complete, _1));
}

// utility.
// ----------------------------------------------------------------------------

bool protocol_electrum::send_get_status(const std::string& tx_hash,
double txout_idx, const std::string& spk_hint) NOEXCEPT
{
uint32_t index{};
hash_digest hash{};
if (!to_integer(index, txout_idx) || !decode_hash(hash, tx_hash))
{
send_code(error::invalid_argument);
return false;
}

// This is parsed for correctness but is not used.
// Script is advisory, and should match output script.
if (!spk_hint.empty())
{
data_chunk bytes{};
if (!decode_base16(bytes, spk_hint))
{
send_code(error::invalid_argument);
return;
return false;
}

chain::script script{ std::move(bytes), false };
if (!script.is_valid())
{
send_code(error::invalid_argument);
return;
return false;
}
}

// TODO: collect the outpoint into a limited notification set.
subscribed_outpoint_.store(false, std::memory_order_relaxed);

// TODO: implement outpoint status query.
chain::point prevout{ hash, index };
send_result(object_t
const auto& query = archive();
const auto tx = query.to_tx(hash);
const auto output = query.to_output(tx, index);
if (output.is_terminal())
{
{ "height", 24 },
{ "spender_txhash", "<hash>" },
{ "spender_height", 42 }

}, 16, BIND(complete, _1));
}

void protocol_electrum::handle_blockchain_outpoint_unsubscribe(const code& ec,
rpc_interface::blockchain_outpoint_unsubscribe, const std::string& tx_hash,
double txout_idx) NOEXCEPT
{
if (stopped(ec))
return;
send_code(error::not_found);
return false;
}

if (!at_least(electrum::version::v1_7))
// TODO: database query.///////////////////////////////////////////////////
size_t height{ database::history::rooted_height };
if (const auto block = query.find_confirmed_block(tx); block.is_terminal())
{
send_code(error::wrong_version);
return;
if (!query.is_confirmed_all_prevouts(tx))
height = database::history::unrooted_height;
}

uint32_t index{};
hash_digest hash{};
if (!to_integer(index, txout_idx) ||
!decode_hash(hash, tx_hash))
else if (!query.get_height(height, block))
{
send_code(error::invalid_argument);
return;
send_code(error::server_error);
return false;
}
///////////////////////////////////////////////////////////////////////////

// TODO: remove outpoint subscription from notification set.
chain::point prevout{ hash, index };
const auto previous = subscribed_scriptpubkey_.load(
std::memory_order_relaxed);
// TODO: query tx spenders sorted history./////////////////////////////////
const database::histories spenders{};
///////////////////////////////////////////////////////////////////////////

send_result(previous, 16, BIND(complete, _1));
auto result = object_t{ { "height", to_unsigned(height) } };
if (!spenders.empty())
{
const auto& spender = spenders.front().tx;
result["spender_txhash"] = encode_hash(spender.hash());
result["spender_height"] = to_unsigned(spender.height());
}

send_result(std::move(result), 128, BIND(complete, _1));
return true;
}

BC_POP_WARNING()

} // namespace server
} // namespace libbitcoin
16 changes: 7 additions & 9 deletions src/protocols/electrum/protocol_electrum_scripthash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,19 @@ void protocol_electrum::handle_blockchain_scripthash_unsubscribe(const code& ec,
array_t protocol_electrum::transform(const database::histories& ins) NOEXCEPT
{
// Height is set to zero or max_size_t for unconfirmed history.
// to_signed() conversion is simple but sacrifices top height bit (ok).
static_assert(to_signed(max_size_t) == -1 && is_max(max_size_t));

array_t out(ins.size());
std::ranges::transform(ins, out.begin(), [](const auto& in) NOEXCEPT
{
const auto height = in.tx.height();
const bool unconfirmed = is_min(height) || is_max(height);

object_t object
{
{ "height", to_signed(height) },
{ "height", to_signed(in.tx.height()) },
{ "tx_hash", encode_hash(in.tx.hash()) }
};

if (unconfirmed)
if (!in.confirmed())
{
// A fee of max_uint64 implies missing prevout(s). This will happen
// for a block-downloaded tx queried during parallel block download
Expand All @@ -400,13 +398,13 @@ array_t protocol_electrum::transform(const database::unspents& ins) NOEXCEPT
array_t out(ins.size());
std::ranges::transform(ins, out.begin(), [](const auto& in) NOEXCEPT
{
const auto& tx = in.tx;
const auto& out = in.out;
return object_t
{
{ "tx_hash", encode_hash(tx.point().hash()) },
{ "tx_pos", tx.point().index() },
{ "tx_hash", encode_hash(out.point().hash()) },
{ "tx_pos", out.point().index() },
{ "height", in.height },
{ "value", tx.value() }
{ "value", out.value() }
};
});

Expand Down
7 changes: 5 additions & 2 deletions test/protocols/electrum/electrum_mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BOOST_FIXTURE_TEST_SUITE(electrum_tests, electrum_setup_fixture)

using namespace system;
static const code wrong_version{ server::error::wrong_version };
static const code not_implemented{ server::error::not_implemented };

// mempool.get_fee_histogram

Expand Down Expand Up @@ -51,12 +52,14 @@ BOOST_AUTO_TEST_CASE(electrum__mempool_get_fee_histogram__extra_param__dropped)
REQUIRE_NO_THROW_TRUE(response.at("dropped").as_bool());
}

BOOST_AUTO_TEST_CASE(electrum__mempool_get_fee_histogram__empty_params__empty_array)
BOOST_AUTO_TEST_CASE(electrum__mempool_get_fee_histogram__empty_params__not_implemented)
{
BOOST_REQUIRE(handshake(electrum::version::v1_2));

const auto response = get(R"({"id":603,"method":"mempool.get_fee_histogram","params":[]})" "\n");
REQUIRE_NO_THROW_TRUE(response.at("result").as_array().empty());
REQUIRE_NO_THROW_TRUE(response.at("error").as_object().at("code").is_int64());
BOOST_REQUIRE_EQUAL(response.at("error").as_object().at("code").as_int64(), not_implemented.value());
///REQUIRE_NO_THROW_TRUE(response.at("result").as_array().empty());
}

// mempool.get_info
Expand Down
Loading