1 #include <boost/assert.hpp>
2 #include <boost/assign/list_of.hpp>
3 #include <boost/assign/list_inserter.hpp>
4 #include <boost/assign/std/vector.hpp>
5 #include <boost/test/unit_test.hpp>
6 #include <boost/foreach.hpp>
10 #include "../wallet.h"
14 // Test routines internal to script.cpp:
15 extern uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
16 extern bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int& nSigOps,
17 int nHashType, bool fStrictOpEval);
19 BOOST_AUTO_TEST_SUITE(script_op_eval_tests)
21 BOOST_AUTO_TEST_CASE(script_op_eval1)
23 // OP_EVAL looks like this:
24 // scriptSig: <sig> <sig...> <serialized_script>
25 // scriptPubKey: DUP HASH160 <hash> EQUALVERIFY EVAL
27 // Test SignSignature() (and therefore the version of Solver() that signs transactions)
28 CBasicKeyStore keystore;
30 for (int i = 0; i < 4; i++)
33 keystore.AddKey(key[i]);
36 // 8 Scripts: checking all combinations of
37 // different keys, straight/EVAL, pubkey/pubkeyhash
38 CScript standardScripts[4];
39 standardScripts[0] << key[0].GetPubKey() << OP_CHECKSIG;
40 standardScripts[1].SetBitcoinAddress(key[1].GetPubKey());
41 standardScripts[2] << key[1].GetPubKey() << OP_CHECKSIG;
42 standardScripts[3].SetBitcoinAddress(key[2].GetPubKey());
43 CScript evalScripts[4];
44 uint160 sigScriptHashes[4];
45 for (int i = 0; i < 4; i++)
47 sigScriptHashes[i] = Hash160(standardScripts[i]);
48 keystore.AddCScript(sigScriptHashes[i], standardScripts[i]);
49 evalScripts[i] << OP_DUP << OP_HASH160 << sigScriptHashes[i] << OP_EQUALVERIFY << OP_EVAL;
52 CTransaction txFrom; // Funding transaction:
53 txFrom.vout.resize(8);
54 for (int i = 0; i < 4; i++)
56 txFrom.vout[i].scriptPubKey = evalScripts[i];
57 txFrom.vout[i+4].scriptPubKey = standardScripts[i];
59 BOOST_CHECK(txFrom.IsStandard());
61 CTransaction txTo[8]; // Spending transactions
62 for (int i = 0; i < 8; i++)
64 txTo[i].vin.resize(1);
65 txTo[i].vout.resize(1);
66 txTo[i].vin[0].prevout.n = i;
67 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
68 txTo[i].vout[0].nValue = 1;
69 BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
71 for (int i = 0; i < 8; i++)
73 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
75 // All of the above should be OK, and the txTos have valid signatures
76 // Check to make sure signature verification fails if we use the wrong ScriptSig:
77 for (int i = 0; i < 8; i++)
78 for (int j = 0; j < 8; j++)
80 CScript sigSave = txTo[i].vin[0].scriptSig;
81 txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
83 bool sigOK = VerifySignature(txFrom, txTo[i], 0, nUnused);
85 BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
87 BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
88 txTo[i].vin[0].scriptSig = sigSave;
92 BOOST_AUTO_TEST_CASE(script_op_eval2)
94 // Test OP_EVAL edge cases
97 recurse << OP_DUP << OP_EVAL;
99 uint160 recurseHash = Hash160(recurse);
102 fund << OP_DUP << OP_HASH160 << recurseHash << OP_EQUALVERIFY << OP_EVAL;
104 CTransaction txFrom; // Funding transaction:
105 txFrom.vout.resize(1);
106 txFrom.vout[0].scriptPubKey = fund;
108 BOOST_CHECK(txFrom.IsStandard()); // Looks like a standard transaction until you try to spend it
113 txTo.vin[0].prevout.n = 0;
114 txTo.vin[0].prevout.hash = txFrom.GetHash();
115 txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(recurse);
116 txTo.vout[0].nValue = 1;
119 BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
120 BOOST_CHECK(!VerifySignature(txFrom, txTo, 0, nUnused, true));
123 BOOST_AUTO_TEST_CASE(script_op_eval3)
125 // Test the CScript::Set* methods
126 CBasicKeyStore keystore;
128 std::vector<CKey> keys;
129 for (int i = 0; i < 4; i++)
132 keystore.AddKey(key[i]);
133 keys.push_back(key[i]);
137 inner[0].SetBitcoinAddress(key[0].GetPubKey());
138 inner[1].SetMultisig(2, std::vector<CKey>(keys.begin(), keys.begin()+2));
139 inner[2].SetMultisig(1, std::vector<CKey>(keys.begin(), keys.begin()+2));
140 inner[3].SetMultisig(2, std::vector<CKey>(keys.begin(), keys.begin()+3));
143 for (int i = 0; i < 4; i++)
145 outer[i].SetEval(inner[i]);
146 keystore.AddCScript(Hash160(inner[i]), inner[i]);
149 CTransaction txFrom; // Funding transaction:
150 txFrom.vout.resize(4);
151 for (int i = 0; i < 4; i++)
153 txFrom.vout[i].scriptPubKey = outer[i];
155 BOOST_CHECK(txFrom.IsStandard());
157 CTransaction txTo[4]; // Spending transactions
158 for (int i = 0; i < 4; i++)
160 txTo[i].vin.resize(1);
161 txTo[i].vout.resize(1);
162 txTo[i].vin[0].prevout.n = i;
163 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
164 txTo[i].vout[0].nValue = 1;
165 txTo[i].vout[0].scriptPubKey = inner[i];
166 BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
168 for (int i = 0; i < 4; i++)
170 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
171 BOOST_CHECK_MESSAGE(txTo[i].IsStandard(), strprintf("txTo[%d].IsStandard", i));
175 BOOST_AUTO_TEST_CASE(script_op_eval_backcompat1)
177 // Check backwards-incompatibility-testing code
178 CScript returnsEleven;
179 returnsEleven << OP_11;
181 // This should validate on new clients, but will
182 // be invalid on old clients (that interpret OP_EVAL as a no-op)
183 // ... except there's a special rule that makes new clients reject
186 fund << OP_EVAL << OP_11 << OP_EQUAL;
188 CTransaction txFrom; // Funding transaction:
189 txFrom.vout.resize(1);
190 txFrom.vout[0].scriptPubKey = fund;
195 txTo.vin[0].prevout.n = 0;
196 txTo.vin[0].prevout.hash = txFrom.GetHash();
197 txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(returnsEleven);
198 txTo.vout[0].nValue = 1;
201 BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
202 BOOST_CHECK(!VerifySignature(txFrom, txTo, 0, nUnused, true));
205 BOOST_AUTO_TEST_CASE(script_op_eval_switchover)
207 // Test OP_EVAL switchover code
209 notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
211 // This will be valid under old rules, invalid under new:
215 CTransaction txFrom; // Funding transaction:
216 txFrom.vout.resize(1);
217 txFrom.vout[0].scriptPubKey = fund;
222 txTo.vin[0].prevout.n = 0;
223 txTo.vin[0].prevout.hash = txFrom.GetHash();
224 txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(notValid);
225 txTo.vout[0].nValue = 1;
228 BOOST_CHECK(VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, false));
230 // Under strict op_eval switchover, it should be considered invalid:
231 BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
234 BOOST_AUTO_TEST_SUITE_END()