From 066e2a1403fe306787a2ce0c8571aa9de57386cf Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Wed, 24 Sep 2014 22:24:46 -0400 Subject: [PATCH 1/4] script: move CScriptID to standard.h and add a ctor for creating them from CScripts This allows for a reversal of the current behavior. This: CScript foo; CScriptID bar(foo.GetID()); Becomes: CScript foo; CScriptID bar(foo); This way, CScript is no longer dependent on CScriptID or Hash(); --- src/crypter.cpp | 1 + src/key.h | 8 -------- src/keystore.cpp | 3 ++- src/keystore.h | 1 + src/rpcmisc.cpp | 2 +- src/rpcrawtransaction.cpp | 2 +- src/rpcwallet.cpp | 2 +- src/script/compressor.cpp | 1 + src/script/compressor.h | 2 ++ src/script/script.h | 6 ------ src/script/standard.cpp | 2 ++ src/script/standard.h | 9 +++++++++ src/test/miner_tests.cpp | 2 +- src/test/script_P2SH_tests.cpp | 20 ++++++++++---------- src/test/script_tests.cpp | 4 ++-- src/test/sigopcount_tests.cpp | 4 ++-- src/wallet.cpp | 2 +- 17 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/crypter.cpp b/src/crypter.cpp index a872df702..756538836 100644 --- a/src/crypter.cpp +++ b/src/crypter.cpp @@ -5,6 +5,7 @@ #include "crypter.h" #include "script/script.h" +#include "script/standard.h" #include "util.h" #include diff --git a/src/key.h b/src/key.h index f6f6d35d3..48b165253 100644 --- a/src/key.h +++ b/src/key.h @@ -30,14 +30,6 @@ public: CKeyID(const uint160& in) : uint160(in) {} }; -/** A reference to a CScript: the Hash160 of its serialization (see script.h) */ -class CScriptID : public uint160 -{ -public: - CScriptID() : uint160(0) {} - CScriptID(const uint160& in) : uint160(in) {} -}; - /** An encapsulated public key. */ class CPubKey { diff --git a/src/keystore.cpp b/src/keystore.cpp index 755defa26..039c69062 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -8,6 +8,7 @@ #include "crypter.h" #include "key.h" #include "script/script.h" +#include "script/standard.h" #include "util.h" #include @@ -38,7 +39,7 @@ bool CBasicKeyStore::AddCScript(const CScript& redeemScript) return error("CBasicKeyStore::AddCScript() : redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE); LOCK(cs_KeyStore); - mapScripts[redeemScript.GetID()] = redeemScript; + mapScripts[CScriptID(redeemScript)] = redeemScript; return true; } diff --git a/src/keystore.h b/src/keystore.h index d3478f767..4f8189c8f 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -13,6 +13,7 @@ #include class CScript; +class CScriptID; /** A virtual base class for key stores */ class CKeyStore diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 8be14b567..92ed1c3e2 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -292,7 +292,7 @@ Value createmultisig(const Array& params, bool fHelp) // Construct using pay-to-script-hash: CScript inner = _createmultisig_redeemScript(params); - CScriptID innerID = inner.GetID(); + CScriptID innerID(inner); CBitcoinAddress address(innerID); Object result; diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 78372da68..fdfcb59ee 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -480,7 +480,7 @@ Value decodescript(const Array& params, bool fHelp) } ScriptPubKeyToJSON(script, r, false); - r.push_back(Pair("p2sh", CBitcoinAddress(script.GetID()).ToString())); + r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString())); return r; } diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index d11455e38..68bb4068b 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -918,7 +918,7 @@ Value addmultisigaddress(const Array& params, bool fHelp) // Construct using pay-to-script-hash: CScript inner = _createmultisig_redeemScript(params); - CScriptID innerID = inner.GetID(); + CScriptID innerID(inner); pwalletMain->AddCScript(inner); pwalletMain->SetAddressBook(innerID, strAccount, "send"); diff --git a/src/script/compressor.cpp b/src/script/compressor.cpp index 51a3cf602..dbb4df8b7 100644 --- a/src/script/compressor.cpp +++ b/src/script/compressor.cpp @@ -4,6 +4,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "compressor.h" +#include "script/standard.h" bool CScriptCompressor::IsToKeyID(CKeyID &hash) const { diff --git a/src/script/compressor.h b/src/script/compressor.h index 53c6bf3ec..f48ecf500 100644 --- a/src/script/compressor.h +++ b/src/script/compressor.h @@ -8,6 +8,8 @@ #include "script/script.h" +class CScriptID; + /** Compact serializer for scripts. * * It detects common cases and encodes them much more efficiently. diff --git a/src/script/script.h b/src/script/script.h index caf176476..4d685f559 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -610,12 +610,6 @@ public: } return str; } - - CScriptID GetID() const - { - return CScriptID(Hash160(*this)); - } - void clear() { // The default std::vector::clear() does not release memory. diff --git a/src/script/standard.cpp b/src/script/standard.cpp index 53ae254d5..adbec01f2 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -14,6 +14,8 @@ using namespace std; typedef vector valtype; +CScriptID::CScriptID(const CScript& in) : uint160(in.size() ? Hash160(in.begin(), in.end()) : 0) {} + const char* GetTxnOutputType(txnouttype t) { switch (t) diff --git a/src/script/standard.h b/src/script/standard.h index ead79b82a..38d0b5988 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -13,6 +13,15 @@ class CScript; +/** A reference to a CScript: the Hash160 of its serialization (see script.h) */ +class CScriptID : public uint160 +{ +public: + CScriptID() : uint160(0) {} + CScriptID(const CScript& in); + CScriptID(const uint160& in) : uint160(in) {} +}; + static const unsigned int MAX_OP_RETURN_RELAY = 40; // bytes // Mandatory script verification flags that all new blocks must comply with for diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index bad5c13ac..93b7fe189 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -170,7 +170,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vin[0].scriptSig = CScript() << OP_1; tx.vout[0].nValue = 4900000000LL; script = CScript() << OP_0; - tx.vout[0].scriptPubKey = GetScriptForDestination(script.GetID()); + tx.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(script)); hash = tx.GetHash(); mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11)); tx.vin[0].prevout.hash = hash; diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp index f8361a0dc..a23792a4f 100644 --- a/src/test/script_P2SH_tests.cpp +++ b/src/test/script_P2SH_tests.cpp @@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(sign) for (int i = 0; i < 4; i++) { keystore.AddCScript(standardScripts[i]); - evalScripts[i] = GetScriptForDestination(standardScripts[i].GetID()); + evalScripts[i] = GetScriptForDestination(CScriptID(standardScripts[i])); } CMutableTransaction txFrom; // Funding transaction: @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(norecurse) CScript invalidAsScript; invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE; - CScript p2sh = GetScriptForDestination(invalidAsScript.GetID()); + CScript p2sh = GetScriptForDestination(CScriptID(invalidAsScript)); CScript scriptSig; scriptSig << Serialize(invalidAsScript); @@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(norecurse) // Try to recur, and verification should succeed because // the inner HASH160 <> EQUAL should only check the hash: - CScript p2sh2 = GetScriptForDestination(p2sh.GetID()); + CScript p2sh2 = GetScriptForDestination(CScriptID(p2sh)); CScript scriptSig2; scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh); @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(set) CScript outer[4]; for (int i = 0; i < 4; i++) { - outer[i] = GetScriptForDestination(inner[i].GetID()); + outer[i] = GetScriptForDestination(CScriptID(inner[i])); keystore.AddCScript(inner[i]); } @@ -242,7 +242,7 @@ BOOST_AUTO_TEST_CASE(switchover) CScript scriptSig; scriptSig << Serialize(notValid); - CScript fund = GetScriptForDestination(notValid.GetID()); + CScript fund = GetScriptForDestination(CScriptID(notValid)); // Validation should succeed under old rules (hash is correct): @@ -275,7 +275,7 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) keystore.AddCScript(pay1); CScript pay1of3 = GetScriptForMultisig(1, keys); - txFrom.vout[0].scriptPubKey = GetScriptForDestination(pay1.GetID()); // P2SH (OP_CHECKSIG) + txFrom.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(pay1)); // P2SH (OP_CHECKSIG) txFrom.vout[0].nValue = 1000; txFrom.vout[1].scriptPubKey = pay1; // ordinary OP_CHECKSIG txFrom.vout[1].nValue = 2000; @@ -290,7 +290,7 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) oneAndTwo << OP_2 << key[3].GetPubKey() << key[4].GetPubKey() << key[5].GetPubKey(); oneAndTwo << OP_3 << OP_CHECKMULTISIG; keystore.AddCScript(oneAndTwo); - txFrom.vout[3].scriptPubKey = GetScriptForDestination(oneAndTwo.GetID()); + txFrom.vout[3].scriptPubKey = GetScriptForDestination(CScriptID(oneAndTwo)); txFrom.vout[3].nValue = 4000; // vout[4] is max sigops: @@ -299,17 +299,17 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) fifteenSigops << key[i%3].GetPubKey(); fifteenSigops << OP_15 << OP_CHECKMULTISIG; keystore.AddCScript(fifteenSigops); - txFrom.vout[4].scriptPubKey = GetScriptForDestination(fifteenSigops.GetID()); + txFrom.vout[4].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops)); txFrom.vout[4].nValue = 5000; // vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG; keystore.AddCScript(sixteenSigops); - txFrom.vout[5].scriptPubKey = GetScriptForDestination(fifteenSigops.GetID()); + txFrom.vout[5].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops)); txFrom.vout[5].nValue = 5000; CScript twentySigops; twentySigops << OP_CHECKMULTISIG; keystore.AddCScript(twentySigops); - txFrom.vout[6].scriptPubKey = GetScriptForDestination(twentySigops.GetID()); + txFrom.vout[6].scriptPubKey = GetScriptForDestination(CScriptID(twentySigops)); txFrom.vout[6].nValue = 6000; coins.ModifyCoins(txFrom.GetHash())->FromTx(txFrom, 0); diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index a4b021249..cfb4fa6e7 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -162,7 +162,7 @@ public: TestBuilder(const CScript& redeemScript, const std::string& comment_, int flags_, bool P2SH = false) : scriptPubKey(redeemScript), havePush(false), comment(comment_), flags(flags_) { if (P2SH) { - creditTx = BuildCreditingTransaction(CScript() << OP_HASH160 << redeemScript.GetID() << OP_EQUAL); + creditTx = BuildCreditingTransaction(CScript() << OP_HASH160 << CScriptID(redeemScript) << OP_EQUAL); } else { creditTx = BuildCreditingTransaction(redeemScript); } @@ -697,7 +697,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs) // P2SH, single-signature case: CScript pkSingle; pkSingle << keys[0].GetPubKey() << OP_CHECKSIG; keystore.AddCScript(pkSingle); - scriptPubKey = GetScriptForDestination(pkSingle.GetID()); + scriptPubKey = GetScriptForDestination(CScriptID(pkSingle)); SignSignature(keystore, txFrom, txTo, 0); combined = CombineSignatures(scriptPubKey, txTo, 0, scriptSig, empty); BOOST_CHECK(combined == scriptSig); diff --git a/src/test/sigopcount_tests.cpp b/src/test/sigopcount_tests.cpp index 62a6cd63d..4ce2c87a0 100644 --- a/src/test/sigopcount_tests.cpp +++ b/src/test/sigopcount_tests.cpp @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount) BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U); BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U); - CScript p2sh = GetScriptForDestination(s1.GetID()); + CScript p2sh = GetScriptForDestination(CScriptID(s1)); CScript scriptSig; scriptSig << OP_0 << Serialize(s1); BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U); @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount) BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U); BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U); - p2sh = GetScriptForDestination(s2.GetID()); + p2sh = GetScriptForDestination(CScriptID(s2)); BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U); BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U); CScript scriptSig2; diff --git a/src/wallet.cpp b/src/wallet.cpp index 19e43f6ec..65944587f 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -158,7 +158,7 @@ bool CWallet::LoadCScript(const CScript& redeemScript) * these. Do not add them to the wallet and warn. */ if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) { - std::string strAddr = CBitcoinAddress(redeemScript.GetID()).ToString(); + std::string strAddr = CBitcoinAddress(CScriptID(redeemScript)).ToString(); LogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n", __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr); return true; From e9ca4280f3abb8b2b6fa35a41e881996278ebfff Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Wed, 24 Sep 2014 22:54:08 -0400 Subject: [PATCH 2/4] script: add ToByteVector() for converting anything with begin/end This should move to a util header once their dependencies are cleaned up. --- src/miner.cpp | 2 +- src/script/script.h | 29 +++-------- src/script/sign.cpp | 2 +- src/script/standard.cpp | 6 +-- src/test/multisig_tests.cpp | 42 ++++++++-------- src/test/script_P2SH_tests.cpp | 20 ++++---- src/test/script_tests.cpp | 88 +++++++++++++++++----------------- src/test/sigopcount_tests.cpp | 6 +-- src/test/transaction_tests.cpp | 4 +- 9 files changed, 91 insertions(+), 108 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index c2762bf44..6a2718e40 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -398,7 +398,7 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) if (!reservekey.GetReservedKey(pubkey)) return NULL; - CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG; + CScript scriptPubKey = CScript() << ToByteVector(pubkey) << OP_CHECKSIG; return CreateNewBlock(scriptPubKey); } diff --git a/src/script/script.h b/src/script/script.h index 4d685f559..6676e852a 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -16,6 +16,12 @@ static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes +template +std::vector ToByteVector(const T& in) +{ + return std::vector(in.begin(), in.end()); +} + /** Script opcodes */ enum opcodetype { @@ -358,7 +364,6 @@ public: CScript(int64_t b) { operator<<(b); } explicit CScript(opcodetype b) { operator<<(b); } - explicit CScript(const uint256& b) { operator<<(b); } explicit CScript(const CScriptNum& b) { operator<<(b); } explicit CScript(const std::vector& b) { operator<<(b); } @@ -373,28 +378,6 @@ public: return *this; } - CScript& operator<<(const uint160& b) - { - insert(end(), sizeof(b)); - insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b)); - return *this; - } - - CScript& operator<<(const uint256& b) - { - insert(end(), sizeof(b)); - insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b)); - return *this; - } - - CScript& operator<<(const CPubKey& key) - { - assert(key.size() < OP_PUSHDATA1); - insert(end(), (unsigned char)key.size()); - insert(end(), key.begin(), key.end()); - return *this; - } - CScript& operator<<(const CScriptNum& b) { *this << b.getvch(); diff --git a/src/script/sign.cpp b/src/script/sign.cpp index da77e7d1f..bf98c4039 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -78,7 +78,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash { CPubKey vch; keystore.GetPubKey(keyID, vch); - scriptSigRet << vch; + scriptSigRet << ToByteVector(vch); } return true; case TX_SCRIPTHASH: diff --git a/src/script/standard.cpp b/src/script/standard.cpp index adbec01f2..7e6b136a2 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -282,13 +282,13 @@ public: bool operator()(const CKeyID &keyID) const { script->clear(); - *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG; + *script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG; return true; } bool operator()(const CScriptID &scriptID) const { script->clear(); - *script << OP_HASH160 << scriptID << OP_EQUAL; + *script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL; return true; } }; @@ -308,7 +308,7 @@ CScript GetScriptForMultisig(int nRequired, const std::vector& keys) script << CScript::EncodeOP_N(nRequired); BOOST_FOREACH(const CPubKey& key, keys) - script << key; + script << ToByteVector(key); script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG; return script; } diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index 5a2ec1cb3..e9fc86779 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -51,13 +51,13 @@ BOOST_AUTO_TEST_CASE(multisig_verify) key[i].MakeNewKey(true); CScript a_and_b; - a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; CScript a_or_b; - a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; CScript escrow; - escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG; CMutableTransaction txFrom; // Funding transaction txFrom.vout.resize(3); @@ -138,28 +138,28 @@ BOOST_AUTO_TEST_CASE(multisig_IsStandard) txnouttype whichType; CScript a_and_b; - a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(::IsStandard(a_and_b, whichType)); CScript a_or_b; - a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(::IsStandard(a_or_b, whichType)); CScript escrow; - escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG; BOOST_CHECK(::IsStandard(escrow, whichType)); CScript one_of_four; - one_of_four << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << key[3].GetPubKey() << OP_4 << OP_CHECKMULTISIG; + one_of_four << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << ToByteVector(key[3].GetPubKey()) << OP_4 << OP_CHECKMULTISIG; BOOST_CHECK(!::IsStandard(one_of_four, whichType)); CScript malformed[6]; - malformed[0] << OP_3 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; - malformed[1] << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_3 << OP_CHECKMULTISIG; - malformed[2] << OP_0 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; - malformed[3] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_0 << OP_CHECKMULTISIG; - malformed[4] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_CHECKMULTISIG; - malformed[5] << OP_1 << key[0].GetPubKey() << key[1].GetPubKey(); + malformed[0] << OP_3 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; + malformed[1] << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_3 << OP_CHECKMULTISIG; + malformed[2] << OP_0 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; + malformed[3] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_0 << OP_CHECKMULTISIG; + malformed[4] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_CHECKMULTISIG; + malformed[5] << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()); for (int i = 0; i < 6; i++) BOOST_CHECK(!::IsStandard(malformed[i], whichType)); @@ -192,7 +192,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << key[0].GetPubKey() << OP_CHECKSIG; + s << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 1); CTxDestination addr; @@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_DUP << OP_HASH160 << key[0].GetPubKey().GetID() << OP_EQUALVERIFY << OP_CHECKSIG; + s << OP_DUP << OP_HASH160 << ToByteVector(key[0].GetPubKey().GetID()) << OP_EQUALVERIFY << OP_CHECKSIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 1); CTxDestination addr; @@ -222,7 +222,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + s << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK_EQUAL(solutions.size(), 4U); CTxDestination addr; @@ -237,7 +237,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + s << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK_EQUAL(solutions.size(), 4U); vector addrs; @@ -256,7 +256,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) vector solutions; txnouttype whichType; CScript s; - s << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + s << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG; BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 5); } @@ -274,13 +274,13 @@ BOOST_AUTO_TEST_CASE(multisig_Sign) } CScript a_and_b; - a_and_b << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_and_b << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; CScript a_or_b; - a_or_b << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << OP_2 << OP_CHECKMULTISIG; + a_or_b << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << OP_2 << OP_CHECKMULTISIG; CScript escrow; - escrow << OP_2 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey() << OP_3 << OP_CHECKMULTISIG; + escrow << OP_2 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()) << OP_3 << OP_CHECKMULTISIG; CMutableTransaction txFrom; // Funding transaction txFrom.vout.resize(3); diff --git a/src/test/script_P2SH_tests.cpp b/src/test/script_P2SH_tests.cpp index a23792a4f..fcab65278 100644 --- a/src/test/script_P2SH_tests.cpp +++ b/src/test/script_P2SH_tests.cpp @@ -67,9 +67,9 @@ BOOST_AUTO_TEST_CASE(sign) // 8 Scripts: checking all combinations of // different keys, straight/P2SH, pubkey/pubkeyhash CScript standardScripts[4]; - standardScripts[0] << key[0].GetPubKey() << OP_CHECKSIG; + standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; standardScripts[1] = GetScriptForDestination(key[1].GetPubKey().GetID()); - standardScripts[2] << key[1].GetPubKey() << OP_CHECKSIG; + standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; standardScripts[3] = GetScriptForDestination(key[2].GetPubKey().GetID()); CScript evalScripts[4]; for (int i = 0; i < 4; i++) @@ -206,9 +206,9 @@ BOOST_AUTO_TEST_CASE(set) BOOST_AUTO_TEST_CASE(is) { // Test CScript::IsPayToScriptHash() - uint160 dummy; + uint160 dummy(0); CScript p2sh; - p2sh << OP_HASH160 << dummy << OP_EQUAL; + p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(p2sh.IsPayToScriptHash()); // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes: @@ -224,13 +224,13 @@ BOOST_AUTO_TEST_CASE(is) CScript not_p2sh; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << dummy << OP_EQUAL; + not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_NOP << dummy << OP_EQUAL; + not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); - not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << OP_CHECKSIG; + not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG; BOOST_CHECK(!not_p2sh.IsPayToScriptHash()); } @@ -285,9 +285,9 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) // vout[3] is complicated 1-of-3 AND 2-of-3 // ... that is OK if wrapped in P2SH: CScript oneAndTwo; - oneAndTwo << OP_1 << key[0].GetPubKey() << key[1].GetPubKey() << key[2].GetPubKey(); + oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey()); oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY; - oneAndTwo << OP_2 << key[3].GetPubKey() << key[4].GetPubKey() << key[5].GetPubKey(); + oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey()); oneAndTwo << OP_3 << OP_CHECKMULTISIG; keystore.AddCScript(oneAndTwo); txFrom.vout[3].scriptPubKey = GetScriptForDestination(CScriptID(oneAndTwo)); @@ -296,7 +296,7 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard) // vout[4] is max sigops: CScript fifteenSigops; fifteenSigops << OP_1; for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++) - fifteenSigops << key[i%3].GetPubKey(); + fifteenSigops << ToByteVector(key[i%3].GetPubKey()); fifteenSigops << OP_15 << OP_CHECKMULTISIG; keystore.AddCScript(fifteenSigops); txFrom.vout[4].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops)); diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index cfb4fa6e7..d3fc673a7 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -162,7 +162,7 @@ public: TestBuilder(const CScript& redeemScript, const std::string& comment_, int flags_, bool P2SH = false) : scriptPubKey(redeemScript), havePush(false), comment(comment_), flags(flags_) { if (P2SH) { - creditTx = BuildCreditingTransaction(CScript() << OP_HASH160 << CScriptID(redeemScript) << OP_EQUAL); + creditTx = BuildCreditingTransaction(CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL); } else { creditTx = BuildCreditingTransaction(redeemScript); } @@ -270,135 +270,135 @@ BOOST_AUTO_TEST_CASE(script_build) std::vector good; std::vector bad; - good.push_back(TestBuilder(CScript() << keys.pubkey0 << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG, "P2PK", 0 ).PushSig(keys.key0)); - bad.push_back(TestBuilder(CScript() << keys.pubkey0 << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG, "P2PK, bad sig", 0 ).PushSig(keys.key0).DamagePush(10)); - good.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << keys.pubkey1C.GetID() << OP_EQUALVERIFY << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG, "P2PKH", 0 ).PushSig(keys.key1).Push(keys.pubkey1C)); - bad.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << keys.pubkey2C.GetID() << OP_EQUALVERIFY << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey2C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG, "P2PKH, bad pubkey", 0 ).PushSig(keys.key2).Push(keys.pubkey2C).DamagePush(5)); - good.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG, "P2PK anyonecanpay", 0 ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY)); - bad.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG, "P2PK anyonecanpay marked with normal hashtype", 0 ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY).EditPush(70, "81", "01")); - good.push_back(TestBuilder(CScript() << keys.pubkey0C << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG, "P2SH(P2PK)", SCRIPT_VERIFY_P2SH, true ).PushSig(keys.key0).PushRedeem()); - bad.push_back(TestBuilder(CScript() << keys.pubkey0C << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG, "P2SH(P2PK), bad redeemscript", SCRIPT_VERIFY_P2SH, true ).PushSig(keys.key0).PushRedeem().DamagePush(10)); - good.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << keys.pubkey1.GetID() << OP_EQUALVERIFY << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG, "P2SH(P2PKH), bad sig but no VERIFY_P2SH", 0, true ).PushSig(keys.key0).DamagePush(10).PushRedeem()); - bad.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << keys.pubkey1.GetID() << OP_EQUALVERIFY << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG, "P2SH(P2PKH), bad sig", SCRIPT_VERIFY_P2SH, true ).PushSig(keys.key0).DamagePush(10).PushRedeem()); - good.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + good.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "3-of-3", 0 ).Num(0).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2)); - bad.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + bad.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "3-of-3, 2 sigs", 0 ).Num(0).PushSig(keys.key0).PushSig(keys.key1).Num(0)); - good.push_back(TestBuilder(CScript() << OP_2 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + good.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "P2SH(2-of-3)", SCRIPT_VERIFY_P2SH, true ).Num(0).PushSig(keys.key1).PushSig(keys.key2).PushRedeem()); - bad.push_back(TestBuilder(CScript() << OP_2 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + bad.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "P2SH(2-of-3), 1 sig", SCRIPT_VERIFY_P2SH, true ).Num(0).PushSig(keys.key1).Num(0).PushRedeem()); - good.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too much R padding but no DERSIG", 0 ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000")); - bad.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too much R padding", SCRIPT_VERIFY_DERSIG ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000")); - good.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too much S padding but no DERSIG", 0 ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100")); - bad.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too much S padding", SCRIPT_VERIFY_DERSIG ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100")); - good.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too little R padding but no DERSIG", 0 ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220")); - bad.push_back(TestBuilder(CScript() << keys.pubkey1C << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG, "P2PK with too little R padding", SCRIPT_VERIFY_DERSIG ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220")); - good.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG << OP_NOT, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT, "P2PK NOT with bad sig with too much R padding but no DERSIG", 0 ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10)); - bad.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT, "P2PK NOT with bad sig with too much R padding", SCRIPT_VERIFY_DERSIG ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10)); - bad.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT, "P2PK NOT with too much R padding but no DERSIG", 0 ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000")); - bad.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT, "P2PK NOT with too much R padding", SCRIPT_VERIFY_DERSIG ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000")); - good.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, "P2PK with high S but no LOW_S", 0 ).PushSig(keys.key2, SIGHASH_ALL, 32, 33)); - bad.push_back(TestBuilder(CScript() << keys.pubkey2C << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, "P2PK with high S", SCRIPT_VERIFY_LOW_S ).PushSig(keys.key2, SIGHASH_ALL, 32, 33)); - good.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG, "P2PK with hybrid pubkey but no STRICTENC", 0 ).PushSig(keys.key0, SIGHASH_ALL)); - bad.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG, "P2PK with hybrid pubkey", SCRIPT_VERIFY_STRICTENC ).PushSig(keys.key0, SIGHASH_ALL)); - bad.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT, "P2PK NOT with hybrid pubkey but no STRICTENC", 0 ).PushSig(keys.key0, SIGHASH_ALL)); - good.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG << OP_NOT, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT, "P2PK NOT with hybrid pubkey", SCRIPT_VERIFY_STRICTENC ).PushSig(keys.key0, SIGHASH_ALL)); - good.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG << OP_NOT, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT, "P2PK NOT with invalid hybrid pubkey but no STRICTENC", 0 ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10)); - good.push_back(TestBuilder(CScript() << keys.pubkey0H << OP_CHECKSIG << OP_NOT, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT, "P2PK NOT with invalid hybrid pubkey", SCRIPT_VERIFY_STRICTENC ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10)); - good.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG, "P2PK with undefined hashtype but no STRICTENC", 0 ).PushSig(keys.key1, 5)); - bad.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG, "P2PK with undefined hashtype", SCRIPT_VERIFY_STRICTENC ).PushSig(keys.key1, 5)); - good.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG << OP_NOT, + good.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT, "P2PK NOT with invalid sig and undefined hashtype but no STRICTENC", 0 ).PushSig(keys.key1, 5).DamagePush(10)); - bad.push_back(TestBuilder(CScript() << keys.pubkey1 << OP_CHECKSIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT, "P2PK NOT with invalid sig and undefined hashtype", SCRIPT_VERIFY_STRICTENC ).PushSig(keys.key1, 5).DamagePush(10)); - good.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + good.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "3-of-3 with nonzero dummy but no NULLDUMMY", 0 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2)); - bad.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG, + bad.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG, "3-of-3 with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2)); - good.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG << OP_NOT, + good.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT, "3-of-3 NOT with invalid sig and nonzero dummy but no NULLDUMMY", 0 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10)); - bad.push_back(TestBuilder(CScript() << OP_3 << keys.pubkey0C << keys.pubkey1C << keys.pubkey2C << OP_3 << OP_CHECKMULTISIG << OP_NOT, + bad.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT, "3-of-3 NOT with invalid sig with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10)); @@ -582,7 +582,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12) key3.MakeNewKey(true); CScript scriptPubKey12; - scriptPubKey12 << OP_1 << key1.GetPubKey() << key2.GetPubKey() << OP_2 << OP_CHECKMULTISIG; + scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG; CMutableTransaction txFrom12 = BuildCreditingTransaction(scriptPubKey12); CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), txFrom12); @@ -608,7 +608,7 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23) key4.MakeNewKey(false); CScript scriptPubKey23; - scriptPubKey23 << OP_2 << key1.GetPubKey() << key2.GetPubKey() << key3.GetPubKey() << OP_3 << OP_CHECKMULTISIG; + scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG; CMutableTransaction txFrom23 = BuildCreditingTransaction(scriptPubKey23); CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), txFrom23); @@ -695,7 +695,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs) BOOST_CHECK(combined == scriptSigCopy || combined == scriptSig); // P2SH, single-signature case: - CScript pkSingle; pkSingle << keys[0].GetPubKey() << OP_CHECKSIG; + CScript pkSingle; pkSingle << ToByteVector(keys[0].GetPubKey()) << OP_CHECKSIG; keystore.AddCScript(pkSingle); scriptPubKey = GetScriptForDestination(CScriptID(pkSingle)); SignSignature(keystore, txFrom, txTo, 0); diff --git a/src/test/sigopcount_tests.cpp b/src/test/sigopcount_tests.cpp index 4ce2c87a0..7b27703b6 100644 --- a/src/test/sigopcount_tests.cpp +++ b/src/test/sigopcount_tests.cpp @@ -31,8 +31,8 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount) BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U); BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U); - uint160 dummy; - s1 << OP_1 << dummy << dummy << OP_2 << OP_CHECKMULTISIG; + uint160 dummy(0); + s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG; BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U); s1 << OP_IF << OP_CHECKSIG << OP_ENDIF; BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U); @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount) BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U); BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U); CScript scriptSig2; - scriptSig2 << OP_1 << dummy << dummy << Serialize(s2); + scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2); BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U); } diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 18cb8f3d1..41ccaaac9 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -259,9 +259,9 @@ SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet) // Create some dummy input transactions dummyTransactions[0].vout.resize(2); dummyTransactions[0].vout[0].nValue = 11*CENT; - dummyTransactions[0].vout[0].scriptPubKey << key[0].GetPubKey() << OP_CHECKSIG; + dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG; dummyTransactions[0].vout[1].nValue = 50*CENT; - dummyTransactions[0].vout[1].scriptPubKey << key[1].GetPubKey() << OP_CHECKSIG; + dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG; coinsRet.ModifyCoins(dummyTransactions[0].GetHash())->FromTx(dummyTransactions[0], 0); dummyTransactions[1].vout.resize(2); From db8eb54bd7239ff2b046fe34b1c8d692860c6b5b Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Tue, 30 Sep 2014 19:45:20 -0400 Subject: [PATCH 3/4] script: move ToString and ValueString out of the header --- src/script/script.cpp | 33 +++++++++++++++++++++++++++++++++ src/script/script.h | 30 +----------------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/script/script.cpp b/src/script/script.cpp index a5126e7cc..1ce8ddb6d 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -7,6 +7,16 @@ #include +namespace { +inline std::string ValueString(const std::vector& vch) +{ + if (vch.size() <= 4) + return strprintf("%d", CScriptNum(vch).getint()); + else + return HexStr(vch); +} +} // anon namespace + using namespace std; const char* GetOpName(opcodetype opcode) @@ -253,3 +263,26 @@ bool CScript::HasCanonicalPushes() const } return true; } + +std::string CScript::ToString() const +{ + std::string str; + opcodetype opcode; + std::vector vch; + const_iterator pc = begin(); + while (pc < end()) + { + if (!str.empty()) + str += " "; + if (!GetOp(pc, opcode, vch)) + { + str += "[error]"; + return str; + } + if (0 <= opcode && opcode <= OP_PUSHDATA4) + str += ValueString(vch); + else + str += GetOpName(opcode); + } + return str; +} diff --git a/src/script/script.h b/src/script/script.h index 6676e852a..4f6ee0c52 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -318,13 +318,6 @@ private: int64_t m_value; }; -inline std::string ValueString(const std::vector& vch) -{ - if (vch.size() <= 4) - return strprintf("%d", CScriptNum(vch).getint()); - else - return HexStr(vch); -} /** Serialized script, used inside transaction inputs and outputs */ class CScript : public std::vector @@ -571,28 +564,7 @@ public: return (size() > 0 && *begin() == OP_RETURN); } - std::string ToString() const - { - std::string str; - opcodetype opcode; - std::vector vch; - const_iterator pc = begin(); - while (pc < end()) - { - if (!str.empty()) - str += " "; - if (!GetOp(pc, opcode, vch)) - { - str += "[error]"; - return str; - } - if (0 <= opcode && opcode <= OP_PUSHDATA4) - str += ValueString(vch); - else - str += GetOpName(opcode); - } - return str; - } + std::string ToString() const; void clear() { // The default std::vector::clear() does not release memory. From 85c579e3a63cf505d6cedc454755265572e97d3e Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Wed, 24 Sep 2014 23:32:36 -0400 Subject: [PATCH 4/4] script: add a slew of includes all around and drop includes from script.h Lots of files ended up with indirect includes from script.h. --- src/chain.h | 1 + src/chainparams.cpp | 1 + src/core.cpp | 2 ++ src/core_read.cpp | 2 ++ src/core_write.cpp | 1 + src/main.h | 1 + src/protocol.cpp | 1 + src/qt/test/paymentservertests.cpp | 1 + src/script/compressor.cpp | 2 ++ src/script/compressor.h | 3 +++ src/script/interpreter.cpp | 1 + src/script/script.cpp | 3 ++- src/script/script.h | 12 ++++++------ src/script/standard.cpp | 1 + src/script/standard.h | 3 +++ src/test/base58_tests.cpp | 1 + src/txmempool.cpp | 1 + src/utilmoneystr.cpp | 1 + 18 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/chain.h b/src/chain.h index 0aafb40b9..961919d97 100644 --- a/src/chain.h +++ b/src/chain.h @@ -8,6 +8,7 @@ #include "core.h" #include "pow.h" +#include "tinyformat.h" #include "uint256.h" #include diff --git a/src/chainparams.cpp b/src/chainparams.cpp index f2a14b829..689607624 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -7,6 +7,7 @@ #include "random.h" #include "util.h" +#include "utilstrencodings.h" #include diff --git a/src/core.cpp b/src/core.cpp index 6a7a9ff37..73e6de88e 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -5,7 +5,9 @@ #include "core.h" +#include "hash.h" #include "tinyformat.h" +#include "utilstrencodings.h" std::string COutPoint::ToString() const { diff --git a/src/core_read.cpp b/src/core_read.cpp index 6bd3d9a4f..8b85a03c5 100644 --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -9,6 +9,8 @@ #include "serialize.h" #include "univalue/univalue.h" #include "util.h" +#include "utilstrencodings.h" +#include "version.h" #include #include diff --git a/src/core_write.cpp b/src/core_write.cpp index 40d547fb3..e42e0b62a 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -12,6 +12,7 @@ #include "univalue/univalue.h" #include "util.h" #include "utilmoneystr.h" +#include "utilstrencodings.h" #include diff --git a/src/main.h b/src/main.h index cad7eebfb..9f9401653 100644 --- a/src/main.h +++ b/src/main.h @@ -20,6 +20,7 @@ #include "script/sigcache.h" #include "script/standard.h" #include "sync.h" +#include "tinyformat.h" #include "txmempool.h" #include "uint256.h" diff --git a/src/protocol.cpp b/src/protocol.cpp index 0e28f3abb..72fdd753a 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -7,6 +7,7 @@ #include "chainparams.h" #include "util.h" +#include "utilstrencodings.h" #ifndef WIN32 # include diff --git a/src/qt/test/paymentservertests.cpp b/src/qt/test/paymentservertests.cpp index 5d7fe9628..84cab01c5 100644 --- a/src/qt/test/paymentservertests.cpp +++ b/src/qt/test/paymentservertests.cpp @@ -8,6 +8,7 @@ #include "paymentrequestdata.h" #include "util.h" +#include "utilstrencodings.h" #include #include diff --git a/src/script/compressor.cpp b/src/script/compressor.cpp index dbb4df8b7..af1acf48d 100644 --- a/src/script/compressor.cpp +++ b/src/script/compressor.cpp @@ -4,6 +4,8 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "compressor.h" + +#include "key.h" #include "script/standard.h" bool CScriptCompressor::IsToKeyID(CKeyID &hash) const diff --git a/src/script/compressor.h b/src/script/compressor.h index f48ecf500..154e0b266 100644 --- a/src/script/compressor.h +++ b/src/script/compressor.h @@ -7,7 +7,10 @@ #define H_BITCOIN_SCRIPT_COMPRESSOR #include "script/script.h" +#include "serialize.h" +class CKeyID; +class CPubKey; class CScriptID; /** Compact serializer for scripts. diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index ae66217b7..cd73b8821 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -9,6 +9,7 @@ #include "crypto/ripemd160.h" #include "crypto/sha1.h" #include "crypto/sha2.h" +#include "key.h" #include "script/script.h" #include "uint256.h" #include "util.h" diff --git a/src/script/script.cpp b/src/script/script.cpp index 1ce8ddb6d..3e19d0c2b 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -5,7 +5,8 @@ #include "script.h" -#include +#include "tinyformat.h" +#include "utilstrencodings.h" namespace { inline std::string ValueString(const std::vector& vch) diff --git a/src/script/script.h b/src/script/script.h index 4f6ee0c52..a68924c73 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -6,13 +6,13 @@ #ifndef H_BITCOIN_SCRIPT #define H_BITCOIN_SCRIPT -#include "key.h" -#include "tinyformat.h" -#include "utilstrencodings.h" - +#include +#include +#include #include - -#include +#include +#include +#include static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes diff --git a/src/script/standard.cpp b/src/script/standard.cpp index 7e6b136a2..05938961b 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -7,6 +7,7 @@ #include "script/script.h" #include "util.h" +#include "utilstrencodings.h" #include diff --git a/src/script/standard.h b/src/script/standard.h index 38d0b5988..961b214c8 100644 --- a/src/script/standard.h +++ b/src/script/standard.h @@ -6,9 +6,12 @@ #ifndef H_BITCOIN_SCRIPT_STANDARD #define H_BITCOIN_SCRIPT_STANDARD +#include "key.h" #include "script/script.h" #include "script/interpreter.h" +#include + #include class CScript; diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp index c298c805d..e495435b8 100644 --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -12,6 +12,7 @@ #include "script/script.h" #include "uint256.h" #include "util.h" +#include "utilstrencodings.h" #include #include diff --git a/src/txmempool.cpp b/src/txmempool.cpp index fa1802ad3..4522c6361 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -8,6 +8,7 @@ #include "core.h" #include "util.h" #include "utilmoneystr.h" +#include "version.h" #include diff --git a/src/utilmoneystr.cpp b/src/utilmoneystr.cpp index 1a5635bfb..95be06aa1 100644 --- a/src/utilmoneystr.cpp +++ b/src/utilmoneystr.cpp @@ -7,6 +7,7 @@ #include "core.h" #include "tinyformat.h" +#include "utilstrencodings.h" using namespace std;