View in context in the libbitcoin Slack #general mirror.
stopping_ is an atomic bool that gets set via socket (or process) termination on the main channel strand, passed into the expensive query for cancellation. sub.cursor is now a height-based cursor, and r/w parameter to the query, which updates each read to the top of the chain, so most reads will only accumulate one block of confirmed related txs. The limit parameter is from config, and will terminate the query with an error if the number of address outputs exceeds that value (though it can really handle any volume). turbo_ is also config, allowing the store query to be run on all available threads (across the set of potentially millions of outputs). The hash accumulator sub.accumulator is stored with the cursor and the last status hash sub.status on the subscription (144 bytes + 32 byte scripthash key for each subscription of the channel). The accumulator is copied after the confirmed txs are appended to it, and the unconfirmed are then accumulated and flushed as the next sub.status, which is then sent as the notification if it has changed.
code protocol_electrum::get_scripthash_history(address_subscription& sub,
const hash_digest& hash, size_t limit) NOEXCEPT
{
BC_ASSERT(notification_strand_.running_in_this_thread());
histories history{};
const auto& query = archive();
if (const auto ec = query.get_history(stopping_, sub.cursor, history,
hash, limit, turbo_))
return ec;
if (history.empty())
return error::success;
auto it = history.cbegin();
while (it != history.cend() && it->confirmed())
write_status(sub.accumulator, *it++);
midstate copy = sub.accumulator;
while (it != history.cend())
write_status(copy, *it++);
sub.status = copy.flush();
return error::success;
}