You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sigopcount_tests.cpp 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2012-2013 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. #include "pubkey.h"
  5. #include "key.h"
  6. #include "script/script.h"
  7. #include "script/standard.h"
  8. #include "uint256.h"
  9. #include "test/test_bitcoin.h"
  10. #include <vector>
  11. #include <boost/foreach.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. using namespace std;
  14. // Helpers:
  15. static std::vector<unsigned char>
  16. Serialize(const CScript& s)
  17. {
  18. std::vector<unsigned char> sSerialized(s);
  19. return sSerialized;
  20. }
  21. BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup)
  22. BOOST_AUTO_TEST_CASE(GetSigOpCount)
  23. {
  24. // Test CScript::GetSigOpCount()
  25. CScript s1;
  26. BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
  27. BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
  28. uint160 dummy;
  29. s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
  30. BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
  31. s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
  32. BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
  33. BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
  34. CScript p2sh = GetScriptForDestination(CScriptID(s1));
  35. CScript scriptSig;
  36. scriptSig << OP_0 << Serialize(s1);
  37. BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
  38. std::vector<CPubKey> keys;
  39. for (int i = 0; i < 3; i++)
  40. {
  41. CKey k;
  42. k.MakeNewKey(true);
  43. keys.push_back(k.GetPubKey());
  44. }
  45. CScript s2 = GetScriptForMultisig(1, keys);
  46. BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
  47. BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
  48. p2sh = GetScriptForDestination(CScriptID(s2));
  49. BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
  50. BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
  51. CScript scriptSig2;
  52. scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
  53. BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
  54. }
  55. BOOST_AUTO_TEST_SUITE_END()