Difference between revisions of "Libbitcoin Consensus"

From Bitcoin Wiki
Jump to: navigation, search
(Initial content.)
(Add background and bindings reasoning.)
Line 41: Line 41:
 
     return result == verify_result_eval_true ? 0 : 1;
 
     return result == verify_result_eval_true ? 0 : 1;
 
  }
 
  }
 +
 +
==Background==
 +
The consensus library exists to provide consensus validation identical in outcome to [[Bitcoind|bitcoind]]. The integral [[Libbitcoin_Common|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 this concern is a red herring<ref>[https://blog.conformal.com/the-bitcoin-consensus-red-herring The Bitcoin Consensus Red Herring]</ref> it was decided to give libbitcoin developers and users the option. Following much deliberation<ref>[https://lists.dyne.org/lurker/message/20150214.111556.61403f4c.en.html Libbitcoinconsensus Discussion on Libbitcoin Mailing List]</ref> and a recommendation by [[Peter_Todd|Peter Todd]]<ref>[https://lists.dyne.org/lurker/message/20150325.083041.77f52cd3.en.html Response to Peter Todd "Twinkle Fingers" Post on Bitcoin-Dev]</ref> 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.<ref>[https://github.com/libbitcoin/libbitcoin-consensus/blob/master/README.md Instructions for Building libbitcoin-consensus]</ref>
 +
 +
==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 [https://github.com/bitcoin/bitcoin/blob/master/src/Makefile.am libbitcoinconsensus] build.
  
 
==Bindings==
 
==Bindings==
The library exposes a C++ interface and includes [http://www.swig.org SWIG] generated bindings for other languages.
+
The library exposes a C++ interface and includes [http://www.swig.org 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.
 
* C++ (native)
 
* C++ (native)
 
* Java
 
* Java
Line 55: Line 63:
 
==See Also==
 
==See Also==
 
* [[Libbitcoin]]
 
* [[Libbitcoin]]
 +
 +
==References==

Revision as of 08:08, 14 May 2015

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 this concern 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.

  • C++ (native)
  • Java
  • Python

Dependencies

See Also

References