Libbitcoin Node: Difference between revisions

From Bitcoin Wiki
Jump to navigation Jump to search
Evoskuil (talk | contribs)
Split v2/v3 dependencies.
Evoskuil (talk | contribs)
→‎Dependencies (Version3): use libbitcoin-system
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
The [https://github.com/libbitcoin/libbitcoin-node libbitcoin-node] library provides an abstraction over the low level networking calls required to implement a full node on the Bitcoin peer-to-peer network. It was originally contained within [[Libbitcoin_Common|libbitcoin]].
The [https://github.com/libbitcoin/libbitcoin-node libbitcoin-node] library provides an abstraction over the low level networking calls required to implement a full node on the Bitcoin peer-to-peer network. It was originally contained within [[Libbitcoin_Common|libbitcoin]].


==Example==
==Example (Version3) ==
The example expects the blockchain to be initialized in "./blockchain/" (see [https://github.com/libbitcoin/libbitcoin-blockchain/blob/master/tools/initchain/initchain.cpp initchain]). The initialize_logging and display_result functions are omitted for brevity.
#include <future>
#include <iostream>
#include <bitcoin/node.hpp>
  int main()
  int main()
  {
  {
     std::string command;
     std::cout << "Starting up..." << std::endl;
     bc::payment_address address;
     initialize_logging("debug.log", "error.log");
     bc::threadpool pool(1);
    bc::node::configuration settings(bc::config::settings::mainnet);
     bc::node::full_node node(settings);
    std::promise<bc::code> started;
    const auto handle_started = [&started](const bc::code& ec)
    {
        started.set_value(ec);
    };
    // To also "run" the node on the p2p nework next call node.run(...).
    node.start(handle_started);
    const auto ec = started.get_future().get();
    if (ec)
    {
        std::cout << "The node failed to start: " << ec.message() << std::endl;
        return 1;
    }
    const auto display_history = [](const bc::code& code,
        const bc::chain::history_compact::list& history)
    {
        if (code)
        {
            std::cout << "Error: " << code.message();
            return;
        }
        for (const auto& entry : history)
        {
            auto output = (entry.kind == bc::chain::point_kind::output);
            auto kind = (output ? "output" : "spend");
            auto height = entry.height;
            auto hash = bc::encode_hash(entry.point.hash());
            auto index = entry.point.index();
   
   
    std::cout << "Starting up...";
            // The value for a spend is the entry.point.checksum() of the
    bc::node::fullnode node("blockchain");
            // output. This allows the spends to be correlated to outputs.
     node.start();
            std::cout << "History..." << std::endl;
            std::cout << "Kind: " << kind << std::endl;
            std::cout << "Height: " << height << std::endl;
            std::cout << "Point: " << hash << ":" << index << std::endl;
            std::cout << "Value: " << entry.value << std::endl;
            std::cout << std::endl;
        }
     };
   
   
    std::cout << "Type a bitcoin address or 'stop' to exit." << std::endl;
     while (true)
     while (true)
     {
     {
        std::cout << "Type a bitcoin address or 'stop' to exit." << std::endl;
        std::string command;
         std::getline(std::cin, command);
         std::getline(std::cin, command);
         if (command == "stop")
         if (command == "stop")
             break;
             break;
   
   
         if (!address.set_encoded(command))
        bc::wallet::payment_address address(command);
         if (!address)
         {
         {
             std::cout << "Invalid address." << std::endl;
             std::cout << "Invalid address: " << command << std::endl;
             continue;
             continue;
         }
         }
   
   
         const auto fetch_handler = [&address](const std::error_code& code,
         // Accept up to 1000 rows and search from block 0.
            const bc::chain::history_list& history)
         node.chain().fetch_history(address, 1000, 0, display_history);
         {
            display_result(code, history, address);
        };
        fetch_history(node.chain(), node.indexer(), address, fetch_handler);
     }
     }
   
   
     std::cout << "Shutting down...";
     std::cout << "Shutting down..." << std::endl;
    node.stop();
   
   
     return 0;
     return 0;
Line 57: Line 102:
* [http://www.boost.org boost]
* [http://www.boost.org boost]
* [https://github.com/bitcoin/secp256k1 secp256k1]
* [https://github.com/bitcoin/secp256k1 secp256k1]
* [[Libbitcoin_Common|libbitcoin]]
* [[Libbitcoin_Common|libbitcoin-system]]
* [[Libbitcoin_Blockchain|libbitcoin-network]]
* [[Libbitcoin_Blockchain|libbitcoin-network]]
* [[Libbitcoin_Blockchain|libbitcoin-database]]
* [[Libbitcoin_Blockchain|libbitcoin-database]]

Latest revision as of 01:32, 9 March 2021

The libbitcoin-node library provides an abstraction over the low level networking calls required to implement a full node on the Bitcoin peer-to-peer network. It was originally contained within libbitcoin.

Example (Version3)

#include <future>
#include <iostream>
#include <bitcoin/node.hpp>

int main()
{
    std::cout << "Starting up..." << std::endl;

    bc::threadpool pool(1);
    bc::node::configuration settings(bc::config::settings::mainnet);
    bc::node::full_node node(settings);
    std::promise<bc::code> started;

    const auto handle_started = [&started](const bc::code& ec)
    {
        started.set_value(ec);
    };

    // To also "run" the node on the p2p nework next call node.run(...).
    node.start(handle_started);

    const auto ec = started.get_future().get();

    if (ec)
    {
        std::cout << "The node failed to start: " << ec.message() << std::endl;
        return 1;
    }

    const auto display_history = [](const bc::code& code,
        const bc::chain::history_compact::list& history)
    {
        if (code)
        {
            std::cout << "Error: " << code.message();
            return;
        }

        for (const auto& entry : history)
        {
            auto output = (entry.kind == bc::chain::point_kind::output);
            auto kind = (output ? "output" : "spend");
            auto height = entry.height;
            auto hash = bc::encode_hash(entry.point.hash());
            auto index = entry.point.index();

            // The value for a spend is the entry.point.checksum() of the
            // output. This allows the spends to be correlated to outputs.
            std::cout << "History..." << std::endl;
            std::cout << "Kind: " << kind << std::endl;
            std::cout << "Height: " << height << std::endl;
            std::cout << "Point: " << hash << ":" << index << std::endl;
            std::cout << "Value: " << entry.value << std::endl;
            std::cout << std::endl;
        }
    };

    while (true)
    {
        std::cout << "Type a bitcoin address or 'stop' to exit." << std::endl;

        std::string command;
        std::getline(std::cin, command);

        if (command == "stop")
            break;

        bc::wallet::payment_address address(command);

        if (!address)
        {
            std::cout << "Invalid address: " << command << std::endl;
            continue;
        }

        // Accept up to 1000 rows and search from block 0.
        node.chain().fetch_history(address, 1000, 0, display_history);
    }

    std::cout << "Shutting down..." << std::endl;

    return 0;
}

Console Application

The library is accompanied by the console application Bitcoin Node (bn).

Design

Dependencies (Version2)

Dependencies (Version3)

See Also

References