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
39 changes: 36 additions & 3 deletions src/protocols/protocol_electrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ void protocol_electrum::handle_blockchain_transaction_get(const code& ec,
}

const auto& query = archive();
const auto tx = query.get_transaction(query.to_tx(hash), true);
const auto link = query.to_tx(hash);
const auto tx = query.get_transaction(link, true);
if (!tx)
{
send_code(error::not_found);
Expand All @@ -474,9 +475,41 @@ void protocol_electrum::handle_blockchain_transaction_get(const code& ec,
const auto size = tx->serialized_size(true);
if (verbose)
{
// TODO: inject contextual tx properties.
auto value = value_from(bitcoind(*tx));
if (!value.is_object())
{
send_code(error::server_error);
return;
}

if (const auto header = query.to_strong(link); !header.is_terminal())
{
using namespace system;
const auto top = query.get_top_confirmed();
const auto height = query.get_height(header);
const auto block_hash = query.get_header_key(header);

uint32_t timestamp{};
if (height.is_terminal() || (block_hash == null_hash) ||
!query.get_timestamp(timestamp, header))
{
send_code(error::server_error);
return;
}

// Floor manages race between getting confirmed top and height.
const auto confirmations = add1(floored_subtract(top, height.value));

auto& transaction = value.as_object();
transaction["in_active_chain"] = true;
transaction["blockhash"] = encode_hash(block_hash);
transaction["confirmations"] = confirmations;
transaction["blocktime"] = timestamp;
transaction["time"] = timestamp;
}

// Verbose means whatever bitcoind returns for getrawtransaction, lolz.
send_result(value_from(bitcoind(*tx)), two * size, BIND(complete, _1));
send_result(std::move(value), two * size, BIND(complete, _1));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion test/protocols/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const chain::block block9{ block9_data, true };
const server::settings::embedded_pages admin{};
const server::settings::embedded_pages native{};

bool setup_eight_block_store(query_t& query) NOEXCEPT
bool setup_ten_block_store(query_t& query) NOEXCEPT
{
using namespace database;
return query.initialize(genesis) &&
Expand Down
2 changes: 1 addition & 1 deletion test/protocols/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ extern const system::chain::block block7;
extern const system::chain::block block8;
extern const system::chain::block block9;

bool setup_eight_block_store(query_t& query) NOEXCEPT;
bool setup_ten_block_store(query_t& query) NOEXCEPT;

#endif
2 changes: 1 addition & 1 deletion test/protocols/electrum/electrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ electrum_setup_fixture::electrum_setup_fixture()

// Create and populate the store.
BOOST_REQUIRE_MESSAGE(!ec, ec.message());
BOOST_REQUIRE_MESSAGE(setup_eight_block_store(query_), "electrum initialize");
BOOST_REQUIRE_MESSAGE(setup_ten_block_store(query_), "electrum initialize");

// Run the server.
std::promise<code> running{};
Expand Down
15 changes: 13 additions & 2 deletions test/protocols/electrum/electrum_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ BOOST_AUTO_TEST_CASE(electrum__blockchain_transaction_get__genesis_coinbase_verb
BOOST_CHECK_EQUAL(response.at("result").as_string(), encode_base16(coinbase.to_data(true)));
}

// TODO: the expectation doesn't acocunt for contextual tx properties.
BOOST_AUTO_TEST_CASE(electrum__blockchain_transaction_get__genesis_coinbase_verbose_true__expected)
{
BOOST_CHECK(handshake());
Expand All @@ -135,7 +134,19 @@ BOOST_AUTO_TEST_CASE(electrum__blockchain_transaction_get__genesis_coinbase_verb
const auto tx_hash = encode_hash(coinbase.hash(false));
const auto request = R"({"id":83,"method":"blockchain.transaction.get","params":["%1%",true]})" "\n";
const auto response = get((boost::format(request) % tx_hash).str());
BOOST_CHECK_EQUAL(response.at("result").as_object(), value_from(bitcoind(coinbase)));

auto expected = value_from(bitcoind(coinbase));
BOOST_CHECK(expected.is_object());

// The test store is ten confirmed blocks, so top height of nine.
constexpr auto top = 9u;
auto& tx = expected.as_object();
tx["in_active_chain"] = true;
tx["blockhash"] = encode_hash(genesis.hash());
tx["confirmations"] = add1(top);
tx["blocktime"] = genesis.header().timestamp();
tx["time"] = genesis.header().timestamp();
BOOST_CHECK_EQUAL(response.at("result").as_object(), expected);
}

// blockchain.transaction.get_merkle
Expand Down