Libbitcoin Consensus

From Bitcoin Wiki
Jump to: navigation, search

The libbitcoin-consensus library is an optional dependency of libbitcoin-blockchain. It is conceptually redundant with native functionality within libbitcoin.


#include <cstdint>
#include <string>
#include <vector>
#include <bitcoin/consensus.hpp>

// For bc::decode_base16 utility.
#include <bitcoin/bitcoin.hpp>

#define VERIFY_TX \
    "01000000017d01943c40b7f3d8a00a2d62fa1d560bf739a2368c180615b0a793" \
    "7c0e883e7c000000006b4830450221008f66d188c664a8088893ea4ddd968902" \
    "4ea5593877753ecc1e9051ed58c15168022037109f0d06e6068b7447966f751d" \
    "e8474641ad2b15ec37f4a9d159b02af68174012103e208f5403383c77d5832a2" \
    "68c9f71480f6e7bfbdfa44904becacfad66163ea31ffffffff01c8af00000000" \


// Validate a transaction against a single previous output script.
int main()
    using namespace libbitcoin::consensus;

    constexpr uint32_t tx_input_index = 0;
    constexpr uint32_t flags = verify_flags_p2sh |
       verify_flags_checklocktimeverify | verify_flags_dersig;

    const std::string tx(VERIFY_TX);
    const std::string ps(PREVOUT_SCRIPT);

    std::vector<uint8_t> tx_data, ps_data;
    /* bool */ bc::decode_base16(tx_data, tx);
    /* bool */ bc::decode_base16(ps_data, ps);

    const auto result = verify_script(&tx_data[0], tx_data.size(),
        &ps_data[0], ps_data.size(), tx_input_index, flags);

    return result == verify_result_eval_true ? 0 : 1;


The consensus library exists to provide consensus validation identical in outcome to bitcoind. The integral libbitcoin script engine provides the same checks. However given the complexity of the validation code it is effectively impossible to prove that two implementations are identical in outcome, even with extensive testing. This effectively makes the bitcoind implementation a standard (as opposed to an RFC or BIP).

That has helped stifle the adoption of alternative full node implementations. While it has been argued that consensus risk from any particular implementation is a red herring[1] it was decided to give Libbitcoin developers and users the option. Following much deliberation[2] and a recommendation by Peter Todd[3][4] it was decided to package this library independently in order to provide better consistency, a leaner dependency, and more formal isolation from bitcoind. The library is free of both libbitcoin and bitcoind dependencies.[5]


This library incorporates the files that have been identified as "consensus critical" in bitcoind. It is straightforward to verify the implementation against bitcoind by overlaying the bitcoind sources and performing a difference option. With each tagged release of bitcoind this library is updated with the files identified in the libbitcoinconsensus build.


The library exposes a C++ interface and includes SWIG generated bindings for other languages. Libbitcoin is a C++ toolkit, the underlying bitcoind sources are C++, and SWIG bindings are easily generated over C++. For these reasons the native interface is C++, in contrast to the libbitcoinconsensus interface which is C.


See Also