Changeset View
Changeset View
Standalone View
Standalone View
src/test/sigopcount_tests.cpp
// Copyright (c) 2012-2016 The Bitcoin Core developers | // Copyright (c) 2012-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "consensus/consensus.h" | #include "consensus/consensus.h" | ||||
#include "consensus/validation.h" | #include "consensus/validation.h" | ||||
#include "key.h" | #include "key.h" | ||||
#include "policy/policy.h" // For STANDARD_CHECKDATASIG_VERIFY_FLAGS. | |||||
#include "pubkey.h" | #include "pubkey.h" | ||||
#include "script/interpreter.h" | #include "script/interpreter.h" | ||||
#include "script/script.h" | #include "script/script.h" | ||||
#include "script/standard.h" | #include "script/standard.h" | ||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
#include <limits> | #include <limits> | ||||
#include <vector> | #include <vector> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
// Helpers: | // Helpers: | ||||
static std::vector<uint8_t> Serialize(const CScript &s) { | static std::vector<uint8_t> Serialize(const CScript &s) { | ||||
std::vector<uint8_t> sSerialized(s.begin(), s.end()); | std::vector<uint8_t> sSerialized(s.begin(), s.end()); | ||||
return sSerialized; | return sSerialized; | ||||
} | } | ||||
BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup) | ||||
void CheckScriptSigOps(const CScript &script, uint32_t accurate_sigops, | void CheckScriptSigOps(const CScript &script, uint32_t accurate_sigops, | ||||
uint32_t inaccurate_sigops) { | uint32_t inaccurate_sigops) { | ||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(false), inaccurate_sigops); | const uint32_t flags = STANDARD_CHECKDATASIG_VERIFY_FLAGS; | ||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(true), accurate_sigops); | |||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(flags, false), inaccurate_sigops); | |||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(flags, true), accurate_sigops); | |||||
const CScript p2sh = GetScriptForDestination(CScriptID(script)); | const CScript p2sh = GetScriptForDestination(CScriptID(script)); | ||||
const CScript scriptSig = CScript() << OP_0 << Serialize(script); | const CScript scriptSig = CScript() << OP_0 << Serialize(script); | ||||
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), accurate_sigops); | BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(flags, scriptSig), accurate_sigops); | ||||
// Check that GetSigOpCount do not report sigops in the P2SH script when the | |||||
// P2SH flags isn't passed in. | |||||
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(SCRIPT_VERIFY_NONE, scriptSig), 0U); | |||||
// Check that GetSigOpCount report the exact count when not passed a P2SH. | // Check that GetSigOpCount report the exact count when not passed a P2SH. | ||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(p2sh), accurate_sigops); | BOOST_CHECK_EQUAL(script.GetSigOpCount(flags, p2sh), accurate_sigops); | ||||
BOOST_CHECK_EQUAL(script.GetSigOpCount(SCRIPT_VERIFY_NONE, p2sh), | |||||
accurate_sigops); | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(GetSigOpCount) { | BOOST_AUTO_TEST_CASE(GetSigOpCount) { | ||||
// Test CScript::GetSigOpCount() | // Test CScript::GetSigOpCount() | ||||
CheckScriptSigOps(CScript(), 0, 0); | CheckScriptSigOps(CScript(), 0, 0); | ||||
uint160 dummy; | uint160 dummy; | ||||
const CScript s1 = CScript() | const CScript s1 = CScript() | ||||
Show All 15 Lines | BOOST_AUTO_TEST_CASE(GetSigOpCount) { | ||||
CheckScriptSigOps(s3, 3, 20); | CheckScriptSigOps(s3, 3, 20); | ||||
const CScript p2sh = GetScriptForDestination(CScriptID(s3)); | const CScript p2sh = GetScriptForDestination(CScriptID(s3)); | ||||
CheckScriptSigOps(p2sh, 0, 0); | CheckScriptSigOps(p2sh, 0, 0); | ||||
CScript scriptSig2; | CScript scriptSig2; | ||||
scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) | scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) | ||||
<< Serialize(s3); | << Serialize(s3); | ||||
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U); | BOOST_CHECK_EQUAL( | ||||
p2sh.GetSigOpCount(STANDARD_CHECKDATASIG_VERIFY_FLAGS, scriptSig2), 3U); | |||||
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(SCRIPT_VERIFY_NONE, scriptSig2), 0U); | |||||
} | } | ||||
/** | /** | ||||
* Verifies script execution of the zeroth scriptPubKey of tx output and zeroth | * Verifies script execution of the zeroth scriptPubKey of tx output and zeroth | ||||
* scriptSig and witness of tx input. | * scriptSig and witness of tx input. | ||||
*/ | */ | ||||
ScriptError VerifyWithFlag(const CTransaction &output, | ScriptError VerifyWithFlag(const CTransaction &output, | ||||
const CMutableTransaction &input, int flags) { | const CMutableTransaction &input, int flags) { | ||||
▲ Show 20 Lines • Show All 172 Lines • Show Last 20 Lines |