From 0a20c6a16e92178fdbc64285a2a73b66b943cc36 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 23 Jul 2016 23:24:34 -0700 Subject: [PATCH] The fetch-stealth call should only return tx hash (issue #37). --- .../bitcoin/server/interface/blockchain.hpp | 10 +++- src/interface/blockchain.cpp | 51 +++++++++++++++++++ src/workers/query_worker.cpp | 8 ++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/include/bitcoin/server/interface/blockchain.hpp b/include/bitcoin/server/interface/blockchain.hpp index 15818141..5371cdeb 100644 --- a/include/bitcoin/server/interface/blockchain.hpp +++ b/include/bitcoin/server/interface/blockchain.hpp @@ -67,10 +67,14 @@ class BCS_API blockchain static void fetch_block_height(server_node& node, const message& request, send_handler handler); - /// Fetch the blockchain history of a stealth address by its prefix filter. + /// Fetch the history of a stealth address by its prefix filter. static void fetch_stealth(server_node& node, const message& request, send_handler handler); + /// Fetch the transactions of a stealth address by its prefix filter. + static void fetch_stealth2(server_node& node, + const message& request, send_handler handler); + private: static void last_height_fetched(const code& ec, size_t last_height, const message& request, send_handler handler); @@ -108,6 +112,10 @@ class BCS_API blockchain static void stealth_fetched(const code& ec, const chain::stealth_compact::list& stealth_results, const message& request, send_handler handler); + + static void stealth_fetched2(const code& ec, + const chain::stealth_compact::list& stealth_results, + const message& request, send_handler handler); }; } // namespace server diff --git a/src/interface/blockchain.cpp b/src/interface/blockchain.cpp index 55ba35f9..660c24fa 100644 --- a/src/interface/blockchain.cpp +++ b/src/interface/blockchain.cpp @@ -390,5 +390,56 @@ void blockchain::stealth_fetched(const code& ec, handler(message(request, result)); } +void blockchain::fetch_stealth2(server_node& node, const message& request, + send_handler handler) +{ + const auto& data = request.data(); + + if (data.empty()) + { + handler(message(request, error::bad_stream)); + return; + } + + auto deserial = make_deserializer(data.begin(), data.end()); + + // number_bits + const auto bitsize = deserial.read_byte(); + + if (data.size() != sizeof(uint8_t) + binary::blocks_size(bitsize) + + sizeof(uint32_t)) + { + handler(message(request, error::bad_stream)); + return; + } + + // actual bitfield data + const auto blocks = deserial.read_data(binary::blocks_size(bitsize)); + const binary prefix(bitsize, blocks); + + // from_height + const uint64_t from_height = deserial.read_4_bytes_little_endian(); + + node.chain().fetch_stealth(prefix, from_height, + std::bind(&blockchain::stealth_fetched2, + _1, _2, request, handler)); +} + +void blockchain::stealth_fetched2(const code& ec, + const stealth_compact::list& stealth_results, const message& request, + send_handler handler) +{ + // [ code:4 ] + // [[ tx_hash:32 ]...] + data_chunk result(code_size + hash_size * stealth_results.size()); + auto serial = make_serializer(result.begin()); + serial.write_error_code(ec); + + for (const auto& row: stealth_results) + serial.write_hash(row.transaction_hash); + + handler(message(request, result)); +} + } // namespace server } // namespace libbitcoin diff --git a/src/workers/query_worker.cpp b/src/workers/query_worker.cpp index c5a2d2ce..5549694d 100644 --- a/src/workers/query_worker.cpp +++ b/src/workers/query_worker.cpp @@ -203,8 +203,11 @@ void query_worker::attach(const std::string& command, // address.subscribe2 is new in v3, also call for renew. // address.unsubscribe2 is new in v3 (there was never an address.unsubscribe). //----------------------------------------------------------------------------- -//// protocol.broadcast_transaction is deprecated in v3 (deferred). -//// transaction_pool.broadcast (with radar) is new in v3 (deferred). +///protocol.fetch_stealth is deprecated in v3. +// protocol.fetch_stealth2 is new in v3. +//----------------------------------------------------------------------------- +// blockchain.broadcast_transaction is deprecated in v3 (deferred). +// transaction_pool.broadcast (with radar) is new in v3 (deferred). //============================================================================= // Interface class.method names must match protocol (do not change). void query_worker::attach_interface() @@ -223,6 +226,7 @@ void query_worker::attach_interface() ATTACH(blockchain, fetch_transaction_index, node_); ATTACH(blockchain, fetch_spend, node_); ATTACH(blockchain, fetch_stealth, node_); + ATTACH(blockchain, fetch_stealth2, node_); ATTACH(transaction_pool, fetch_transaction, node_); ATTACH(transaction_pool, validate, node_); ////ATTACH(transaction_pool, broadcast, node_);