Libbitcoin Network: Difference between revisions
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
#include <future> | #include <future> | ||
#include <bitcoin/network.hpp> | #include <bitcoin/network.hpp> | ||
// Send a transaction to a single P2P node. | // Send a transaction to a single P2P node. | ||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
{ | { | ||
using namespace bc; | using namespace bc; | ||
data_chunk decoded; | data_chunk decoded; | ||
if (argc < 1 || !decode_base16(decoded, argv[0])) | if (argc < 1 || !decode_base16(decoded, argv[0])) | ||
return -1; | return -1; | ||
const auto tx = chain::transaction::factory_from_data(decoded); | const auto tx = chain::transaction::factory_from_data(decoded); | ||
auto settings = network::settings::mainnet; | auto settings = network::settings::mainnet; | ||
settings.inbound_port = 0; | settings.inbound_port = 0; | ||
Line 21: | Line 21: | ||
settings.outbound_connections = 0; | settings.outbound_connections = 0; | ||
settings.relay_transactions = false; | settings.relay_transactions = false; | ||
network::p2p network(settings); | network::p2p network(settings); | ||
std::promise<code> complete; | std::promise<code> complete; | ||
const auto send_handler = [&complete](const code& ec) | const auto send_handler = [&complete](const code& ec) | ||
{ | { | ||
complete.set_value(ec); | complete.set_value(ec); | ||
}; | }; | ||
const auto connect_handler = [&tx , &send_handler](const code& ec, | const auto connect_handler = [&tx , &send_handler](const code& ec, | ||
network::channel::ptr node) | network::channel::ptr node) | ||
Line 36: | Line 36: | ||
node->send(tx, send_handler); | node->send(tx, send_handler); | ||
}; | }; | ||
return complete.get_future().get() ? -1 : 0; | return complete.get_future().get() ? -1 : 0; | ||
} | } |
Revision as of 11:27, 4 February 2016
The libbitcoin-network library is a partial implementation of the Bitcoin P2P network protocol. Excluded are all sub-protocols that require access to a blockchain. The libbitcoin-node library extends this P2P networking capability and incorporates libbitcoin-blockchain in order to implement a full node. The libbitcoin-explorer library uses the P2P networking capability to post transactions to the P2P network.
Example
#include <future>
#include <bitcoin/network.hpp>
// Send a transaction to a single P2P node.
int main(int argc, char* argv[])
{
using namespace bc;
data_chunk decoded;
if (argc < 1 || !decode_base16(decoded, argv[0]))
return -1;
const auto tx = chain::transaction::factory_from_data(decoded);
auto settings = network::settings::mainnet;
settings.inbound_port = 0;
settings.host_pool_capacity = 0;
settings.outbound_connections = 0;
settings.relay_transactions = false;
network::p2p network(settings);
std::promise complete;
const auto send_handler = [&complete](const code& ec)
{
complete.set_value(ec);
};
const auto connect_handler = [&tx , &send_handler](const code& ec,
network::channel::ptr node)
{
if (!ec)
node->send(tx, send_handler);
};
return complete.get_future().get() ? -1 : 0;
}
History
- The P2P protocol was originally contained within the libbitcoin library.
- Later sources were moved into the libbitcoin::network namespace, and by early 2016 the namespace was forked into its own repository and integrated with dependent repositories as of version 3.
Dependencies
See Also
References