Revert "Use standard C99 (and Qt) types for 64-bit integers"
[novacoin.git] / src / test / script_op_eval_tests.cpp
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>
7
8 #include "../main.h"
9 #include "../script.h"
10 #include "../wallet.h"
11
12 using namespace std;
13
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);
18
19 BOOST_AUTO_TEST_SUITE(script_op_eval_tests)
20
21 BOOST_AUTO_TEST_CASE(script_op_eval1)
22 {
23     // OP_EVAL looks like this:
24     // scriptSig:    <sig> <sig...> <serialized_script>
25     // scriptPubKey: DUP HASH160 <hash> EQUALVERIFY EVAL
26
27     // Test SignSignature() (and therefore the version of Solver() that signs transactions)
28     CBasicKeyStore keystore;
29     CKey key[4];
30     for (int i = 0; i < 4; i++)
31     {
32         key[i].MakeNewKey();
33         keystore.AddKey(key[i]);
34     }
35
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++)
46     {
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;
50     }
51
52     CTransaction txFrom;  // Funding transaction:
53     txFrom.vout.resize(8);
54     for (int i = 0; i < 4; i++)
55     {
56         txFrom.vout[i].scriptPubKey = evalScripts[i];
57         txFrom.vout[i+4].scriptPubKey = standardScripts[i];
58     }
59     BOOST_CHECK(txFrom.IsStandard());
60
61     CTransaction txTo[8]; // Spending transactions
62     for (int i = 0; i < 8; i++)
63     {
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));
70     }
71     for (int i = 0; i < 8; i++)
72     {
73         BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
74     }
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++)
79         {
80             CScript sigSave = txTo[i].vin[0].scriptSig;
81             txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
82             int nUnused = 0;
83             bool sigOK = VerifySignature(txFrom, txTo[i], 0, nUnused);
84             if (i == j)
85                 BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
86             else
87                 BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
88             txTo[i].vin[0].scriptSig = sigSave;
89         }
90 }
91
92 BOOST_AUTO_TEST_CASE(script_op_eval2)
93 {
94     // Test OP_EVAL edge cases
95
96     CScript recurse;
97     recurse << OP_DUP << OP_EVAL;
98
99     uint160 recurseHash = Hash160(recurse);
100
101     CScript fund;
102     fund << OP_DUP << OP_HASH160 << recurseHash << OP_EQUALVERIFY << OP_EVAL;
103
104     CTransaction txFrom;  // Funding transaction:
105     txFrom.vout.resize(1);
106     txFrom.vout[0].scriptPubKey = fund;
107
108     BOOST_CHECK(txFrom.IsStandard()); // Looks like a standard transaction until you try to spend it
109
110     CTransaction txTo;
111     txTo.vin.resize(1);
112     txTo.vout.resize(1);
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;
117
118     int nUnused = 0;
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));
121 }
122
123 BOOST_AUTO_TEST_CASE(script_op_eval3)
124 {
125     // Test the CScript::Set* methods
126     CBasicKeyStore keystore;
127     CKey key[4];
128     std::vector<CKey> keys;
129     for (int i = 0; i < 4; i++)
130     {
131         key[i].MakeNewKey();
132         keystore.AddKey(key[i]);
133         keys.push_back(key[i]);
134     }
135
136     CScript inner[4];
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));
141
142     CScript outer[4];
143     for (int i = 0; i < 4; i++)
144     {
145         outer[i].SetEval(inner[i]);
146         keystore.AddCScript(Hash160(inner[i]), inner[i]);
147     }
148
149     CTransaction txFrom;  // Funding transaction:
150     txFrom.vout.resize(4);
151     for (int i = 0; i < 4; i++)
152     {
153         txFrom.vout[i].scriptPubKey = outer[i];
154     }
155     BOOST_CHECK(txFrom.IsStandard());
156
157     CTransaction txTo[4]; // Spending transactions
158     for (int i = 0; i < 4; i++)
159     {
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));
167     }
168     for (int i = 0; i < 4; i++)
169     {
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));
172     }
173 }
174
175 BOOST_AUTO_TEST_CASE(script_op_eval_backcompat1)
176 {
177     // Check backwards-incompatibility-testing code
178     CScript returnsEleven;
179     returnsEleven << OP_11;
180
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
184     // it.
185     CScript fund;
186     fund << OP_EVAL << OP_11 << OP_EQUAL;
187
188     CTransaction txFrom;  // Funding transaction:
189     txFrom.vout.resize(1);
190     txFrom.vout[0].scriptPubKey = fund;
191
192     CTransaction txTo;
193     txTo.vin.resize(1);
194     txTo.vout.resize(1);
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;
199
200     int nUnused = 0;
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));
203 }
204
205 BOOST_AUTO_TEST_CASE(script_op_eval_switchover)
206 {
207     // Test OP_EVAL switchover code
208     CScript notValid;
209     notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
210
211     // This will be valid under old rules, invalid under new:
212     CScript fund;
213     fund << OP_EVAL;
214
215     CTransaction txFrom;  // Funding transaction:
216     txFrom.vout.resize(1);
217     txFrom.vout[0].scriptPubKey = fund;
218
219     CTransaction txTo;
220     txTo.vin.resize(1);
221     txTo.vout.resize(1);
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;
226
227     int nUnused = 0;
228     BOOST_CHECK(VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, false));
229
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));
232 }
233
234 BOOST_AUTO_TEST_SUITE_END()