Libbitcoin Network: Difference between revisions
Jump to navigation
Jump to search
m →Dependencies: user libbitcoin-system |
|||
(7 intermediate revisions by the same user not shown) | |||
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[]) | ||
{ | { | ||
// Decode a base16-encoded Bitcoin transaction. | // Decode a base16-encoded Bitcoin transaction. | ||
data_chunk decoded; | bc::data_chunk decoded; | ||
if (argc < 1 || !decode_base16(decoded, argv[0])) | if (argc < 1 || !bc::decode_base16(decoded, argv[0])) | ||
return -1; | return -1; | ||
// Parse the decoded transaction. | // Parse the decoded transaction. | ||
const auto tx = chain::transaction::factory_from_data(decoded); | const auto tx = bc::chain::transaction::factory_from_data(decoded); | ||
// Configure the P2P network session for best performance. | // Configure the P2P network session for best performance. | ||
auto settings = network::settings::mainnet | auto settings = bc::network::settings(bc::config::settings::mainnet); | ||
settings.outbound_connections = 0; | settings.outbound_connections = 0; | ||
settings. | settings.manual_attempt_limit = 3; | ||
// Start a network session. | // Start a network session. | ||
network::p2p network(settings); | bc::network::p2p network(settings); | ||
// Declare completion signal. | // Declare completion signal. | ||
std::promise< | std::promise<bc::code> complete; | ||
const auto send_handler = [&complete](const code& ec) | const auto send_handler = [&complete](const bc::code& ec) | ||
{ | { | ||
complete.set_value(ec); | complete.set_value(ec); | ||
}; | }; | ||
const auto connect_handler = [&complete, &tx , &send_handler]( | const auto connect_handler = [&complete, &tx, &send_handler]( | ||
const code& ec, network::channel::ptr node) | const bc::code& ec, bc::network::channel::ptr node) | ||
{ | { | ||
if (ec) | if (ec) | ||
Line 44: | Line 40: | ||
node->send(tx, send_handler); | node->send(tx, send_handler); | ||
}; | }; | ||
// Connect to the one specified host with retry. | |||
network.connect("localhost", 8333, connect_handler); | |||
// Wait for completion and return result. | // Wait for completion and return result. | ||
Line 52: | Line 51: | ||
* The P2P protocol was originally contained within the [[Libbitcoin_Common|libbitcoin]] library. | * The P2P protocol was originally contained within the [[Libbitcoin_Common|libbitcoin]] library. | ||
* In | * In late 2014 [[William_Swanson|William Swanson]] reorganized libbitcoin sources<ref>[https://lists.dyne.org/lurker/message/20141201.054416.c146b7bc.en.html Libbitcoin network refactoring]</ref> in accordance with the roadmap<ref>[https://lists.dyne.org/lurker/message/20140924.201619.e3930350.en.html Libbitcoin roadmap summary]</ref> agreed in Toronto earlier that year. Later sources were moved into the libbitcoin::network namespace. | ||
* | * By early 2016 [[Eric_Voskuil|Eric Voskuil]] completed the version 3 redesign to improve reliability, performance and readability. The namespace was forked into its own repository and integrated with dependent repositories. | ||
==Dependencies== | ==Dependencies== | ||
* [http://www.boost.org boost] | * [http://www.boost.org boost] | ||
* [[Libbitcoin_Common|libbitcoin]] | * [[Libbitcoin_Common|libbitcoin-system]] | ||
==See Also== | ==See Also== |
Latest revision as of 01:32, 9 March 2021
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[]) { // Decode a base16-encoded Bitcoin transaction. bc::data_chunk decoded; if (argc < 1 || !bc::decode_base16(decoded, argv[0])) return -1; // Parse the decoded transaction. const auto tx = bc::chain::transaction::factory_from_data(decoded); // Configure the P2P network session for best performance. auto settings = bc::network::settings(bc::config::settings::mainnet); settings.outbound_connections = 0; settings.manual_attempt_limit = 3; // Start a network session. bc::network::p2p network(settings); // Declare completion signal. std::promise<bc::code> complete; const auto send_handler = [&complete](const bc::code& ec) { complete.set_value(ec); }; const auto connect_handler = [&complete, &tx, &send_handler]( const bc::code& ec, bc::network::channel::ptr node) { if (ec) complete.set_value(ec); else node->send(tx, send_handler); }; // Connect to the one specified host with retry. network.connect("localhost", 8333, connect_handler); // Wait for completion and return result. return complete.get_future().get() ? -1 : 0; }
History
- The P2P protocol was originally contained within the libbitcoin library.
- In late 2014 William Swanson reorganized libbitcoin sources[1] in accordance with the roadmap[2] agreed in Toronto earlier that year. Later sources were moved into the libbitcoin::network namespace.
- By early 2016 Eric Voskuil completed the version 3 redesign to improve reliability, performance and readability. The namespace was forked into its own repository and integrated with dependent repositories.