Line data Source code
1 : // Copyright (c) 2012-2021 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <coins.h> 6 : #include <key.h> 7 : #include <pubkey.h> 8 : #include <script/script.h> 9 : #include <script/standard.h> 10 : #include <test/util/setup_common.h> 11 : #include <uint256.h> 12 : 13 : #include <vector> 14 : 15 : #include <boost/test/unit_test.hpp> 16 : 17 : // Helpers: 18 : static std::vector<unsigned char> 19 2 : Serialize(const CScript& s) 20 : { 21 2 : std::vector<unsigned char> sSerialized(s.begin(), s.end()); 22 2 : return sSerialized; 23 2 : } 24 : 25 146 : BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup) 26 : 27 149 : BOOST_AUTO_TEST_CASE(GetSigOpCount) 28 : { 29 : // Test CScript::GetSigOpCount() 30 1 : CScript s1; 31 1 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U); 32 1 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U); 33 : 34 1 : uint160 dummy; 35 1 : s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG; 36 1 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U); 37 1 : s1 << OP_IF << OP_CHECKSIG << OP_ENDIF; 38 1 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U); 39 1 : BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U); 40 : 41 1 : CScript p2sh = GetScriptForDestination(ScriptHash(s1)); 42 1 : CScript scriptSig; 43 1 : scriptSig << OP_0 << Serialize(s1); 44 1 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U); 45 : 46 1 : std::vector<CPubKey> keys; 47 4 : for (int i = 0; i < 3; i++) 48 : { 49 3 : CKey k = GenerateRandomKey(); 50 3 : keys.push_back(k.GetPubKey()); 51 3 : } 52 1 : CScript s2 = GetScriptForMultisig(1, keys); 53 1 : BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U); 54 1 : BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U); 55 : 56 1 : p2sh = GetScriptForDestination(ScriptHash(s2)); 57 1 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U); 58 1 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U); 59 1 : CScript scriptSig2; 60 1 : scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2); 61 1 : BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U); 62 1 : } 63 : 64 146 : BOOST_AUTO_TEST_SUITE_END()