LCOV - code coverage report
Current view: top level - src/script - bitcoinconsensus.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 52 57 91.2 %
Date: 2021-06-29 14:35:33 Functions: 14 15 93.3 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 14 16 87.5 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2018 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <script/bitcoinconsensus.h>
#       7                 :            : 
#       8                 :            : #include <primitives/transaction.h>
#       9                 :            : #include <pubkey.h>
#      10                 :            : #include <script/interpreter.h>
#      11                 :            : #include <version.h>
#      12                 :            : 
#      13                 :            : namespace {
#      14                 :            : 
#      15                 :            : /** A class that deserializes a single CTransaction one time. */
#      16                 :            : class TxInputStream
#      17                 :            : {
#      18                 :            : public:
#      19                 :            :     TxInputStream(int nVersionIn, const unsigned char *txTo, size_t txToLen) :
#      20                 :            :     m_version(nVersionIn),
#      21                 :            :     m_data(txTo),
#      22                 :            :     m_remaining(txToLen)
#      23                 :       1148 :     {}
#      24                 :            : 
#      25                 :            :     void read(char* pch, size_t nSize)
#      26                 :      13840 :     {
#      27         [ +  + ]:      13840 :         if (nSize > m_remaining)
#      28                 :          2 :             throw std::ios_base::failure(std::string(__func__) + ": end of data");
#      29                 :            : 
#      30         [ -  + ]:      13838 :         if (pch == nullptr)
#      31                 :          0 :             throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
#      32                 :            : 
#      33         [ -  + ]:      13838 :         if (m_data == nullptr)
#      34                 :          0 :             throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
#      35                 :            : 
#      36                 :      13838 :         memcpy(pch, m_data, nSize);
#      37                 :      13838 :         m_remaining -= nSize;
#      38                 :      13838 :         m_data += nSize;
#      39                 :      13838 :     }
#      40                 :            : 
#      41                 :            :     template<typename T>
#      42                 :            :     TxInputStream& operator>>(T&& obj)
#      43                 :       5176 :     {
#      44                 :       5176 :         ::Unserialize(*this, obj);
#      45                 :       5176 :         return *this;
#      46                 :       5176 :     }
#      47                 :            : 
#      48                 :       1148 :     int GetVersion() const { return m_version; }
#      49                 :            : private:
#      50                 :            :     const int m_version;
#      51                 :            :     const unsigned char* m_data;
#      52                 :            :     size_t m_remaining;
#      53                 :            : };
#      54                 :            : 
#      55                 :            : inline int set_error(bitcoinconsensus_error* ret, bitcoinconsensus_error serror)
#      56                 :       1152 : {
#      57         [ +  + ]:       1152 :     if (ret)
#      58                 :         12 :         *ret = serror;
#      59                 :       1152 :     return 0;
#      60                 :       1152 : }
#      61                 :            : 
#      62                 :            : struct ECCryptoClosure
#      63                 :            : {
#      64                 :            :     ECCVerifyHandle handle;
#      65                 :            : };
#      66                 :            : 
#      67                 :            : ECCryptoClosure instance_of_eccryptoclosure;
#      68                 :            : } // namespace
#      69                 :            : 
#      70                 :            : /** Check that all specified flags are part of the libconsensus interface. */
#      71                 :            : static bool verify_flags(unsigned int flags)
#      72                 :       1150 : {
#      73                 :       1150 :     return (flags & ~(bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
#      74                 :       1150 : }
#      75                 :            : 
#      76                 :            : static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
#      77                 :            :                                     const unsigned char *txTo        , unsigned int txToLen,
#      78                 :            :                                     unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
#      79                 :       1150 : {
#      80         [ +  + ]:       1150 :     if (!verify_flags(flags)) {
#      81                 :          2 :         return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
#      82                 :          2 :     }
#      83                 :       1148 :     try {
#      84                 :       1148 :         TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
#      85                 :       1148 :         CTransaction tx(deserialize, stream);
#      86         [ +  + ]:       1148 :         if (nIn >= tx.vin.size())
#      87                 :          2 :             return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
#      88         [ +  + ]:       1146 :         if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
#      89                 :          2 :             return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
#      90                 :            : 
#      91                 :            :         // Regardless of the verification result, the tx did not error.
#      92                 :       1144 :         set_error(err, bitcoinconsensus_ERR_OK);
#      93                 :            : 
#      94                 :       1144 :         PrecomputedTransactionData txdata(tx);
#      95                 :       1144 :         return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
#      96                 :       1144 :     } catch (const std::exception&) {
#      97                 :          2 :         return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
#      98                 :          2 :     }
#      99                 :       1148 : }
#     100                 :            : 
#     101                 :            : int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
#     102                 :            :                                     const unsigned char *txTo        , unsigned int txToLen,
#     103                 :            :                                     unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
#     104                 :        650 : {
#     105                 :        650 :     CAmount am(amount);
#     106                 :        650 :     return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
#     107                 :        650 : }
#     108                 :            : 
#     109                 :            : 
#     110                 :            : int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
#     111                 :            :                                    const unsigned char *txTo        , unsigned int txToLen,
#     112                 :            :                                    unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
#     113                 :        502 : {
#     114         [ +  + ]:        502 :     if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
#     115                 :          2 :         return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
#     116                 :          2 :     }
#     117                 :            : 
#     118                 :        500 :     CAmount am(0);
#     119                 :        500 :     return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
#     120                 :        500 : }
#     121                 :            : 
#     122                 :            : unsigned int bitcoinconsensus_version()
#     123                 :          0 : {
#     124                 :            :     // Just use the API version for now
#     125                 :          0 :     return BITCOINCONSENSUS_API_VER;
#     126                 :          0 : }

Generated by: LCOV version 1.14