Skip to content

Commit

Permalink
Add subscribe-tx and subscribe-block commands
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodefactory committed Apr 6, 2018
1 parent ff88084 commit b59b0c6
Show file tree
Hide file tree
Showing 20 changed files with 919 additions and 45 deletions.
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ src_libbitcoin_explorer_la_SOURCES = \
src/commands/stealth-public.cpp \
src/commands/stealth-secret.cpp \
src/commands/stealth-shared.cpp \
src/commands/subscribe-block.cpp \
src/commands/subscribe-tx.cpp \
src/commands/token-new.cpp \
src/commands/tx-decode.cpp \
src/commands/tx-encode.cpp \
Expand Down Expand Up @@ -258,6 +260,8 @@ test_libbitcoin_explorer_test_SOURCES = \
test/commands/stealth-public.cpp \
test/commands/stealth-secret.cpp \
test/commands/stealth-shared.cpp \
test/commands/subscribe-tx.cpp \
test/commands/subscribe-block.cpp \
test/commands/token-new.cpp \
test/commands/tx-decode.cpp \
test/commands/tx-encode.cpp \
Expand Down
2 changes: 2 additions & 0 deletions data/bx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ _bx()
stealth-public
stealth-secret
stealth-shared
subscribe-block
subscribe-tx
token-new
tx-decode
tx-encode
Expand Down
46 changes: 46 additions & 0 deletions include/bitcoin/explorer/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ class BCX_API command
value<bc::config::endpoint>(&setting_.server.url)->default_value({ "tcp://mainnet.libbitcoin.net:9091" }),
"The URL of the Libbitcoin server."
)
(
"server.block_url",
value<bc::config::endpoint>(&setting_.server.block_url)->default_value({ "tcp://mainnet.libbitcoin.net:9093" }),
"The URL of the Libbitcoin server."
)
(
"server.transaction_url",
value<bc::config::endpoint>(&setting_.server.transaction_url)->default_value({ "tcp://mainnet.libbitcoin.net:9094" }),
"The URL of the Libbitcoin server."
)
(
"server.socks_proxy",
value<bc::config::authority>(&setting_.server.socks_proxy)->default_value({ "0.0.0.0:0" }),
Expand Down Expand Up @@ -580,6 +590,38 @@ class BCX_API command
setting_.server.url = value;
}

/**
* Get the value of the server.block_url setting.
*/
virtual bc::config::endpoint get_server_block_url_setting() const
{
return setting_.server.block_url;
}

/**
* Set the value of the server.block_url setting.
*/
virtual void set_server_block_url_setting(bc::config::endpoint value)
{
setting_.server.block_url = value;
}

/**
* Get the value of the server.transaction_url setting.
*/
virtual bc::config::endpoint get_server_transaction_url_setting() const
{
return setting_.server.transaction_url;
}

/**
* Set the value of the server.transaction_url setting.
*/
virtual void set_server_transaction_url_setting(bc::config::endpoint value)
{
setting_.server.transaction_url = value;
}

/**
* Get the value of the server.socks_proxy setting.
*/
Expand Down Expand Up @@ -750,6 +792,8 @@ class BCX_API command
{
server()
: url(),
block_url(),
transaction_url(),
socks_proxy(),
connect_retries(),
connect_timeout_seconds(),
Expand All @@ -759,6 +803,8 @@ class BCX_API command
}

bc::config::endpoint url;
bc::config::endpoint block_url;
bc::config::endpoint transaction_url;
bc::config::authority socks_proxy;
explorer::config::byte connect_retries;
uint16_t connect_timeout_seconds;
Expand Down
270 changes: 270 additions & 0 deletions include/bitcoin/explorer/commands/subscribe-block.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/**
* Copyright (c) 2011-2017 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BX_SUBSCRIBE_BLOCK_HPP
#define BX_SUBSCRIBE_BLOCK_HPP

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include <bitcoin/bitcoin.hpp>
#include <bitcoin/explorer/command.hpp>
#include <bitcoin/explorer/define.hpp>
#include <bitcoin/explorer/generated.hpp>
#include <bitcoin/explorer/config/address.hpp>
#include <bitcoin/explorer/config/algorithm.hpp>
#include <bitcoin/explorer/config/btc.hpp>
#include <bitcoin/explorer/config/byte.hpp>
#include <bitcoin/explorer/config/cert_key.hpp>
#include <bitcoin/explorer/config/ec_private.hpp>
#include <bitcoin/explorer/config/electrum.hpp>
#include <bitcoin/explorer/config/encoding.hpp>
#include <bitcoin/explorer/config/endorsement.hpp>
#include <bitcoin/explorer/config/hashtype.hpp>
#include <bitcoin/explorer/config/hd_key.hpp>
#include <bitcoin/explorer/config/language.hpp>
#include <bitcoin/explorer/config/raw.hpp>
#include <bitcoin/explorer/config/signature.hpp>
#include <bitcoin/explorer/config/wrapper.hpp>
#include <bitcoin/explorer/utility.hpp>

/********* GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY **********/

namespace libbitcoin {
namespace explorer {
namespace commands {

/**
* Various localizable strings.
*/
#define BX_SUBSCRIBE_BLOCK_FAILED \
"Failed to subscribe to transaction service: %1%"

/**
* Class to implement the subscribe-block command.
*/
class BCX_API subscribe_block
: public command
{
public:

/**
* The symbolic (not localizable) command name, lower case.
*/
static const char* symbol()
{
return "subscribe-block";
}


/**
* The member symbolic (not localizable) command name, lower case.
*/
virtual const char* name()
{
return subscribe_block::symbol();
}

/**
* The localizable command category name, upper case.
*/
virtual const char* category()
{
return "ONLINE";
}

/**
* The localizable command description.
*/
virtual const char* description()
{
return "Watch the network for all blocks. Requires a Libbitcoin server connection.";
}

/**
* Load program argument definitions.
* A value of -1 indicates that the number of instances is unlimited.
* @return The loaded program argument definitions.
*/
virtual arguments_metadata& load_arguments()
{
return get_argument_metadata()
.add("server-url", 1);
}

/**
* Load parameter fallbacks from file or input as appropriate.
* @param[in] input The input stream for loading the parameters.
* @param[in] The loaded variables.
*/
virtual void load_fallbacks(std::istream& input,
po::variables_map& variables)
{
}

/**
* Load program option definitions.
* BUGBUG: see boost bug/fix: svn.boost.org/trac/boost/ticket/8009
* @return The loaded program option definitions.
*/
virtual options_metadata& load_options()
{
using namespace po;
options_description& options = get_option_metadata();
options.add_options()
(
BX_HELP_VARIABLE ",h",
value<bool>()->zero_tokens(),
"Get a description and instructions for this command."
)
(
BX_CONFIG_VARIABLE ",c",
value<boost::filesystem::path>(),
"The path to the configuration settings file."
)
(
"duration,d",
value<uint32_t>(&option_.duration)->default_value(600),
"The duration of the subscription in seconds, defaults to 600."
)
(
"format,f",
value<explorer::config::encoding>(&option_.format),
"The output format. Options are 'info', 'json' and 'xml', defaults to 'info'."
)
(
"server-url",
value<std::string>(&argument_.server_url),
"The URL of the Libbitcoin server to use. If not specified the URL is obtained from configuration settings or defaults."
);

return options;
}

/**
* Set variable defaults from configuration variable values.
* @param[in] variables The loaded variables.
*/
virtual void set_defaults_from_config(po::variables_map& variables)
{
}

/**
* Invoke the command.
* @param[out] output The input stream for the command execution.
* @param[out] error The input stream for the command execution.
* @return The appropriate console return code { -1, 0, 1 }.
*/
virtual console_result invoke(std::ostream& output,
std::ostream& cerr);

/* Properties */

/**
* Get the value of the server-url argument.
*/
virtual std::string& get_server_url_argument()
{
return argument_.server_url;
}

/**
* Set the value of the server-url argument.
*/
virtual void set_server_url_argument(
const std::string& value)
{
argument_.server_url = value;
}

/**
* Get the value of the duration option.
*/
virtual uint32_t& get_duration_option()
{
return option_.duration;
}

/**
* Set the value of the duration option.
*/
virtual void set_duration_option(
const uint32_t& value)
{
option_.duration = value;
}

/**
* Get the value of the format option.
*/
virtual explorer::config::encoding& get_format_option()
{
return option_.format;
}

/**
* Set the value of the format option.
*/
virtual void set_format_option(
const explorer::config::encoding& value)
{
option_.format = value;
}

private:

/**
* Command line argument bound variables.
* Uses cross-compiler safe constructor-based zeroize.
* Zeroize for unit test consistency with program_options initialization.
*/
struct argument
{
argument()
: server_url()
{
}

std::string server_url;
} argument_;

/**
* Command line option bound variables.
* Uses cross-compiler safe constructor-based zeroize.
* Zeroize for unit test consistency with program_options initialization.
*/
struct option
{
option()
: duration(),
format()
{
}

uint32_t duration;
explorer::config::encoding format;
} option_;
};

} // namespace commands
} // namespace explorer
} // namespace libbitcoin

#endif
Loading

0 comments on commit b59b0c6

Please sign in to comment.