Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tx subscription, and add openbsd support to installer. #147

Merged
merged 4 commits into from
Jan 23, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/bitcoin/blockchain/organizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ class BCB_API organizer
void replace_chain(size_t fork_index, block_detail_list& orphan_chain);
void clip_orphans(block_detail_list& orphan_chain, size_t orphan_index,
const std::error_code& invalid_reason);

void notify_stop();
void notify_reorganize(size_t fork_point,
const block_detail_list& orphan_chain,
const block_detail_list& replaced_chain);
void notify_stop();

orphans_pool& orphans_;
simple_chain& chain_;
Expand Down
23 changes: 21 additions & 2 deletions include/bitcoin/blockchain/transaction_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ namespace chain {

struct BCB_API transaction_entry_info
{
typedef std::function<void (const std::error_code&)> confirm_handler;
typedef std::function<void(const std::error_code&, const transaction_type& tx)>
confirm_handler;

hash_digest hash;
transaction_type tx;
confirm_handler handle_confirm;
Expand Down Expand Up @@ -74,6 +76,9 @@ class BCB_API transaction_pool
typedef std::function<void(const std::error_code&, bool)> exists_handler;
typedef transaction_entry_info::confirm_handler confirm_handler;

typedef std::function<bool(const std::error_code&, const index_list&,
const transaction_type&)> transaction_handler;

transaction_pool(threadpool& pool, blockchain& chain, size_t capacity,
bool consistency);
~transaction_pool();
Expand Down Expand Up @@ -150,7 +155,8 @@ class BCB_API transaction_pool
* becomes confirmed.
* @code
* void handle_confirm(
* const std::error_code& ec // Status of operation
* const std::error_code& ec // Status of operation
* const transaction_type& tx // The confirmed transaction
* );
* @endcode
* @param[in] handle_validate Completion handler for
Expand Down Expand Up @@ -211,6 +217,9 @@ class BCB_API transaction_pool
/// Deprecated, unsafe after startup, use constructor.
void set_capacity(size_t capacity);

/// Subscribe to transaction acceptance into the mempool.
void subscribe_transaction(transaction_handler handle_transaction);

protected:
typedef std::error_code code;
typedef std::function<bool (const transaction_input_type&)>
Expand Down Expand Up @@ -249,6 +258,16 @@ class BCB_API transaction_pool
pool_buffer buffer_;
bool stopped_;
const bool maintain_consistency_;

private:
typedef resubscriber<const std::error_code&, const index_list&,
const transaction_type&> transaction_subscriber;

transaction_subscriber::ptr subscriber_;

void notify_stop();
void notify_transaction(const index_list& unconfirmed,
const transaction_type& tx);
};

} // namespace chain
Expand Down
16 changes: 12 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ set -e
#------------------------------------------------------------------------------
SEQUENTIAL=1
OS=`uname -s`
if [[ $TRAVIS == true ]]; then
if [[ $PARALLEL ]]; then
echo "Using shell-defined PARALLEL value."
elif [[ $TRAVIS == true ]]; then
PARALLEL=$SEQUENTIAL
elif [[ $OS == Linux ]]; then
PARALLEL=`nproc`
elif [[ $OS == Darwin ]]; then
elif [[ ($OS == Darwin) || ($OS == OpenBSD) ]]; then
PARALLEL=`sysctl -n hw.ncpu`
else
echo "Unsupported system: $OS"
Expand All @@ -81,14 +83,20 @@ if [[ $OS == Darwin ]]; then
export CC="clang"
export CXX="clang++"
LIBC="libc++"

# Always initialize prefix on OSX so default is useful.
PREFIX="/usr/local"
elif [[ $OS == OpenBSD ]]; then
make() { gmake "$@"; }
export CC="egcc"
export CXX="eg++"
LIBC="libestdc++"
else
LIBC="libstdc++"
fi
echo "Make with cc: $CC"
echo "Make with cxx: $CXX"
echo "Make with stdlib: $LIBC"

# Define compiler specific settings.
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -318,7 +326,7 @@ initialize_boost_icu()

initialize_icu_packages()
{
if [[ $OS == Darwin && !($BUILD_ICU) ]]; then
if [[ ($OS == Darwin) && !($BUILD_ICU) ]]; then
# Update PKG_CONFIG_PATH for ICU package installations on OSX.
# OSX provides libicucore.dylib with no pkgconfig and doesn't support
# renaming or important features, so we can't use that.
Expand Down
12 changes: 6 additions & 6 deletions src/organizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ void organizer::clip_orphans(block_detail_list& orphan_chain,
orphan_chain.erase(orphan_start, orphan_chain.end());
}

void organizer::notify_stop()
{
subscriber_->stop();
subscriber_->relay(error::service_stopped, 0, {}, {});
}

void organizer::notify_reorganize(size_t fork_point,
const block_detail_list& orphan_chain,
const block_detail_list& replaced_chain)
Expand Down Expand Up @@ -228,11 +234,5 @@ void organizer::subscribe_reorganize(
subscriber_->subscribe(handle_reorganize);
}

void organizer::notify_stop()
{
subscriber_->stop();
subscriber_->relay(error::service_stopped, 0, {}, {});
}

} // namespace chain
} // namespace libbitcoin
42 changes: 31 additions & 11 deletions src/transaction_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <cstddef>
#include <memory>
#include <system_error>
#include <bitcoin/bitcoin.hpp>
#include <bitcoin/blockchain/blockchain.hpp>
Expand All @@ -41,7 +42,8 @@ transaction_pool::transaction_pool(threadpool& pool, blockchain& chain,
blockchain_(chain),
buffer_(capacity),
stopped_(true),
maintain_consistency_(consistency)
maintain_consistency_(consistency),
subscriber_(std::make_shared<transaction_subscriber>(pool))
{
}

Expand Down Expand Up @@ -75,10 +77,7 @@ bool transaction_pool::start()

bool transaction_pool::stop()
{
// Stop doesn't need to be called externally and could be made private.
// This will arise from a reorg shutdown message, so transaction_pool
// is automatically registered for shutdown in the following sequence.
// blockchain->organizer(orphan/block pool)->transaction_pool
notify_stop();
stopped_ = true;
return true;
}
Expand Down Expand Up @@ -179,6 +178,9 @@ void transaction_pool::store(const transaction_type& tx,
{
add(tx, handle_confirm);

// Notify subscribers that a tx has been accepted into the memory pool.
notify_transaction(unconfirmed, tx);

log_debug(LOG_BLOCKCHAIN)
<< "Transaction saved to mempool (" << buffer_.size() << ")";
}
Expand Down Expand Up @@ -262,15 +264,13 @@ bool transaction_pool::reorganize(const std::error_code& ec,
{
log_debug(LOG_BLOCKCHAIN)
<< "Stopping transaction pool: " << ec.message();
stop();
return false;
}

if (ec)
{
log_debug(LOG_BLOCKCHAIN)
<< "Failure in tx pool reorganize handler: " << ec.message();
stop();
return false;
}

Expand Down Expand Up @@ -317,7 +317,7 @@ void transaction_pool::add(const transaction_type& tx, confirm_handler handler)
void transaction_pool::clear(const std::error_code& ec)
{
for (const auto& entry: buffer_)
entry.handle_confirm(ec);
entry.handle_confirm(ec, entry.tx);

buffer_.clear();
}
Expand Down Expand Up @@ -414,8 +414,8 @@ void transaction_pool::delete_package(const std::error_code& ec)
return;

const auto& oldest = buffer_.front();
oldest.handle_confirm(ec);
const auto hash = oldest.hash;
oldest.handle_confirm(ec, oldest.tx);
delete_package(hash, ec);
}

Expand Down Expand Up @@ -447,7 +447,7 @@ bool transaction_pool::delete_single(const hash_digest& tx_hash,
if (it == buffer_.end())
return false;

it->handle_confirm(ec);
it->handle_confirm(ec, it->tx);
buffer_.erase(it);
return true;
}
Expand All @@ -466,6 +466,26 @@ void transaction_pool::set_capacity(size_t capacity)
buffer_.set_capacity(capacity);
}

void transaction_pool::notify_stop()
{
subscriber_->stop();
subscriber_->relay(error::service_stopped, {}, {});
}

void transaction_pool::notify_transaction(const index_list& unconfirmed,
const transaction_type& tx)
{
subscriber_->relay(error::success, unconfirmed, tx);
}

void transaction_pool::subscribe_transaction(
transaction_handler handle_transaction)
{
if (stopped())
handle_transaction(error::service_stopped, {}, {});
else
subscriber_->subscribe(handle_transaction);
}

} // namespace chain
} // namespace libbitcoin

2 changes: 1 addition & 1 deletion test/transaction_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class transaction_pool_fixture
const size_t tx##number##_id = number; \
auto hash##number = hash_transaction(tx##number); \
std::error_code result##number(error::unknown); \
const auto handle_confirm##number = [&result##number](const std::error_code& ec) \
const auto handle_confirm##number = [&result##number](const std::error_code& ec, const transaction_type&) \
{ \
result##number = ec; \
BOOST_CHECK_EQUAL(ec.value(), code); \
Expand Down