Libbitcoin Consensus

From Bitcoin Wiki
Revision as of 10:17, 14 May 2015 by Evoskuil (talk | contribs) (c++ include link)
Jump to navigation Jump to search

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

Example

#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" \
    "00001976a91458b7a60f11a904feef35a639b6048de8dd4d9f1c88ac00000000"

#define PREVOUT_SCRIPT \
    "76a914c564c740c6900b93afc9f1bdaef0a9d466adf6ee88ac"

// 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_standard | verify_flags_mandatory;

    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;
}

Background

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] 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.[4]

Verification

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.

Bindings

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.

Dependencies

See Also

References