Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
script_P2SH_tests.cpp
Go to the documentation of this file.
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 
17 // Helpers:
18 static std::vector<unsigned char>
19 Serialize(const CScript& s)
20 {
21  std::vector<unsigned char> sSerialized(s);
22  return sSerialized;
23 }
24 
25 static bool
26 Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict)
27 {
28  // Create dummy to/from transactions:
29  CTransaction txFrom;
30  txFrom.vout.resize(1);
31  txFrom.vout[0].scriptPubKey = scriptPubKey;
32 
33  CTransaction txTo;
34  txTo.vin.resize(1);
35  txTo.vout.resize(1);
36  txTo.vin[0].prevout.n = 0;
37  txTo.vin[0].prevout.hash = txFrom.GetHash();
38  txTo.vin[0].scriptSig = scriptSig;
39  txTo.vout[0].nValue = 1;
40 
41  return VerifyScript(scriptSig, scriptPubKey, txTo, 0, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, 0);
42 }
43 
44 
45 BOOST_AUTO_TEST_SUITE(script_P2SH_tests)
46 
48 {
49  // Pay-to-script-hash looks like this:
50  // scriptSig: <sig> <sig...> <serialized_script>
51  // scriptPubKey: HASH160 <hash> EQUAL
52 
53  // Test SignSignature() (and therefore the version of Solver() that signs transactions)
54  CBasicKeyStore keystore;
55  CKey key[4];
56  for (int i = 0; i < 4; i++)
57  {
58  key[i].MakeNewKey(true);
59  keystore.AddKey(key[i]);
60  }
61 
62  // 8 Scripts: checking all combinations of
63  // different keys, straight/P2SH, pubkey/pubkeyhash
64  CScript standardScripts[4];
65  standardScripts[0] << key[0].GetPubKey() << OP_CHECKSIG;
66  standardScripts[1].SetDestination(key[1].GetPubKey().GetID());
67  standardScripts[2] << key[1].GetPubKey() << OP_CHECKSIG;
68  standardScripts[3].SetDestination(key[2].GetPubKey().GetID());
69  CScript evalScripts[4];
70  for (int i = 0; i < 4; i++)
71  {
72  keystore.AddCScript(standardScripts[i]);
73  evalScripts[i].SetDestination(standardScripts[i].GetID());
74  }
75 
76  CTransaction txFrom; // Funding transaction:
77  txFrom.vout.resize(8);
78  for (int i = 0; i < 4; i++)
79  {
80  txFrom.vout[i].scriptPubKey = evalScripts[i];
81  txFrom.vout[i].nValue = COIN;
82  txFrom.vout[i+4].scriptPubKey = standardScripts[i];
83  txFrom.vout[i+4].nValue = COIN;
84  }
85  BOOST_CHECK(txFrom.IsStandard());
86 
87  CTransaction txTo[8]; // Spending transactions
88  for (int i = 0; i < 8; i++)
89  {
90  txTo[i].vin.resize(1);
91  txTo[i].vout.resize(1);
92  txTo[i].vin[0].prevout.n = i;
93  txTo[i].vin[0].prevout.hash = txFrom.GetHash();
94  txTo[i].vout[0].nValue = 1;
95  BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
96  }
97  for (int i = 0; i < 8; i++)
98  {
99  BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
100  }
101  // All of the above should be OK, and the txTos have valid signatures
102  // Check to make sure signature verification fails if we use the wrong ScriptSig:
103  for (int i = 0; i < 8; i++)
104  for (int j = 0; j < 8; j++)
105  {
106  CScript sigSave = txTo[i].vin[0].scriptSig;
107  txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
108  bool sigOK = VerifySignature(CCoins(txFrom, 0), txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, 0);
109  if (i == j)
110  BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
111  else
112  BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
113  txTo[i].vin[0].scriptSig = sigSave;
114  }
115 }
116 
118 {
119  // Make sure only the outer pay-to-script-hash does the
120  // extra-validation thing:
121  CScript invalidAsScript;
122  invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
123 
124  CScript p2sh;
125  p2sh.SetDestination(invalidAsScript.GetID());
126 
127  CScript scriptSig;
128  scriptSig << Serialize(invalidAsScript);
129 
130  // Should not verify, because it will try to execute OP_INVALIDOPCODE
131  BOOST_CHECK(!Verify(scriptSig, p2sh, true));
132 
133  // Try to recur, and verification should succeed because
134  // the inner HASH160 <> EQUAL should only check the hash:
135  CScript p2sh2;
136  p2sh2.SetDestination(p2sh.GetID());
137  CScript scriptSig2;
138  scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
139 
140  BOOST_CHECK(Verify(scriptSig2, p2sh2, true));
141 }
142 
144 {
145  // Test the CScript::Set* methods
146  CBasicKeyStore keystore;
147  CKey key[4];
148  std::vector<CPubKey> keys;
149  for (int i = 0; i < 4; i++)
150  {
151  key[i].MakeNewKey(true);
152  keystore.AddKey(key[i]);
153  keys.push_back(key[i].GetPubKey());
154  }
155 
156  CScript inner[4];
157  inner[0].SetDestination(key[0].GetPubKey().GetID());
158  inner[1].SetMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
159  inner[2].SetMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
160  inner[3].SetMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
161 
162  CScript outer[4];
163  for (int i = 0; i < 4; i++)
164  {
165  outer[i].SetDestination(inner[i].GetID());
166  keystore.AddCScript(inner[i]);
167  }
168 
169  CTransaction txFrom; // Funding transaction:
170  txFrom.vout.resize(4);
171  for (int i = 0; i < 4; i++)
172  {
173  txFrom.vout[i].scriptPubKey = outer[i];
174  txFrom.vout[i].nValue = CENT;
175  }
176  BOOST_CHECK(txFrom.IsStandard());
177 
178  CTransaction txTo[4]; // Spending transactions
179  for (int i = 0; i < 4; i++)
180  {
181  txTo[i].vin.resize(1);
182  txTo[i].vout.resize(1);
183  txTo[i].vin[0].prevout.n = i;
184  txTo[i].vin[0].prevout.hash = txFrom.GetHash();
185  txTo[i].vout[0].nValue = 1*CENT;
186  txTo[i].vout[0].scriptPubKey = inner[i];
187  BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
188  }
189  for (int i = 0; i < 4; i++)
190  {
191  BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0), strprintf("SignSignature %d", i));
192  BOOST_CHECK_MESSAGE(txTo[i].IsStandard(), strprintf("txTo[%d].IsStandard", i));
193  }
194 }
195 
197 {
198  // Test CScript::IsPayToScriptHash()
199  uint160 dummy;
200  CScript p2sh;
201  p2sh << OP_HASH160 << dummy << OP_EQUAL;
202  BOOST_CHECK(p2sh.IsPayToScriptHash());
203 
204  // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
205  static const unsigned char direct[] = { OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
206  BOOST_CHECK(CScript(direct, direct+sizeof(direct)).IsPayToScriptHash());
207  static const unsigned char pushdata1[] = { OP_HASH160, OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
208  BOOST_CHECK(!CScript(pushdata1, pushdata1+sizeof(pushdata1)).IsPayToScriptHash());
209  static const unsigned char pushdata2[] = { OP_HASH160, OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
210  BOOST_CHECK(!CScript(pushdata2, pushdata2+sizeof(pushdata2)).IsPayToScriptHash());
211  static const unsigned char pushdata4[] = { OP_HASH160, OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
212  BOOST_CHECK(!CScript(pushdata4, pushdata4+sizeof(pushdata4)).IsPayToScriptHash());
213 
214  CScript not_p2sh;
215  BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
216 
217  not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << dummy << OP_EQUAL;
218  BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
219 
220  not_p2sh.clear(); not_p2sh << OP_NOP << dummy << OP_EQUAL;
221  BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
222 
223  not_p2sh.clear(); not_p2sh << OP_HASH160 << dummy << OP_CHECKSIG;
224  BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
225 }
226 
228 {
229  // Test switch over code
230  CScript notValid;
231  notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
232  CScript scriptSig;
233  scriptSig << Serialize(notValid);
234 
235  CScript fund;
236  fund.SetDestination(notValid.GetID());
237 
238 
239  // Validation should succeed under old rules (hash is correct):
240  BOOST_CHECK(Verify(scriptSig, fund, false));
241  // Fail under new:
242  BOOST_CHECK(!Verify(scriptSig, fund, true));
243 }
244 
245 BOOST_AUTO_TEST_CASE(AreInputsStandard)
246 {
247  CCoinsView coinsDummy;
248  CCoinsViewCache coins(coinsDummy);
249  CBasicKeyStore keystore;
250  CKey key[3];
251  vector<CPubKey> keys;
252  for (int i = 0; i < 3; i++)
253  {
254  key[i].MakeNewKey(true);
255  keystore.AddKey(key[i]);
256  keys.push_back(key[i].GetPubKey());
257  }
258 
259  CTransaction txFrom;
260  txFrom.vout.resize(6);
261 
262  // First three are standard:
263  CScript pay1; pay1.SetDestination(key[0].GetPubKey().GetID());
264  keystore.AddCScript(pay1);
265  CScript payScriptHash1; payScriptHash1.SetDestination(pay1.GetID());
266  CScript pay1of3; pay1of3.SetMultisig(1, keys);
267 
268  txFrom.vout[0].scriptPubKey = payScriptHash1;
269  txFrom.vout[0].nValue = 1000;
270  txFrom.vout[1].scriptPubKey = pay1;
271  txFrom.vout[1].nValue = 2000;
272  txFrom.vout[2].scriptPubKey = pay1of3;
273  txFrom.vout[2].nValue = 3000;
274 
275  // Last three non-standard:
276  CScript empty;
277  keystore.AddCScript(empty);
278  txFrom.vout[3].scriptPubKey = empty;
279  txFrom.vout[3].nValue = 4000;
280  // Can't use SetPayToScriptHash, it checks for the empty Script. So:
281  txFrom.vout[4].scriptPubKey << OP_HASH160 << Hash160(empty) << OP_EQUAL;
282  txFrom.vout[4].nValue = 5000;
283  CScript oneOfEleven;
284  oneOfEleven << OP_1;
285  for (int i = 0; i < 11; i++)
286  oneOfEleven << key[0].GetPubKey();
287  oneOfEleven << OP_11 << OP_CHECKMULTISIG;
288  txFrom.vout[5].scriptPubKey.SetDestination(oneOfEleven.GetID());
289  txFrom.vout[5].nValue = 6000;
290 
291  coins.SetCoins(txFrom.GetHash(), CCoins(txFrom, 0));
292 
293  CTransaction txTo;
294  txTo.vout.resize(1);
295  txTo.vout[0].scriptPubKey.SetDestination(key[1].GetPubKey().GetID());
296 
297  txTo.vin.resize(3);
298  txTo.vin[0].prevout.n = 0;
299  txTo.vin[0].prevout.hash = txFrom.GetHash();
300  BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0));
301  txTo.vin[1].prevout.n = 1;
302  txTo.vin[1].prevout.hash = txFrom.GetHash();
303  BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1));
304  txTo.vin[2].prevout.n = 2;
305  txTo.vin[2].prevout.hash = txFrom.GetHash();
306  BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2));
307 
308  BOOST_CHECK(txTo.AreInputsStandard(coins));
309  BOOST_CHECK_EQUAL(txTo.GetP2SHSigOpCount(coins), 1U);
310 
311  // Make sure adding crap to the scriptSigs makes them non-standard:
312  for (int i = 0; i < 3; i++)
313  {
314  CScript t = txTo.vin[i].scriptSig;
315  txTo.vin[i].scriptSig = (CScript() << 11) + t;
316  BOOST_CHECK(!txTo.AreInputsStandard(coins));
317  txTo.vin[i].scriptSig = t;
318  }
319 
320  CTransaction txToNonStd;
321  txToNonStd.vout.resize(1);
322  txToNonStd.vout[0].scriptPubKey.SetDestination(key[1].GetPubKey().GetID());
323  txToNonStd.vout[0].nValue = 1000;
324  txToNonStd.vin.resize(2);
325  txToNonStd.vin[0].prevout.n = 4;
326  txToNonStd.vin[0].prevout.hash = txFrom.GetHash();
327  txToNonStd.vin[0].scriptSig << Serialize(empty);
328  txToNonStd.vin[1].prevout.n = 5;
329  txToNonStd.vin[1].prevout.hash = txFrom.GetHash();
330  txToNonStd.vin[1].scriptSig << OP_0 << Serialize(oneOfEleven);
331 
332  BOOST_CHECK(!txToNonStd.AreInputsStandard(coins));
333  BOOST_CHECK_EQUAL(txToNonStd.GetP2SHSigOpCount(coins), 11U);
334 
335  txToNonStd.vin[0].scriptSig.clear();
336  BOOST_CHECK(!txToNonStd.AreInputsStandard(coins));
337 }
338 
339 BOOST_AUTO_TEST_SUITE_END()
#define strprintf(format,...)
Definition: util.h:169
BOOST_AUTO_TEST_CASE(sign)
bool IsPayToScriptHash() const
Definition: script.cpp:1734
void Serialize(Stream &s, char a, int, int=0)
Definition: serialize.h:120
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:1477
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
Definition: script.cpp:1519
uint256 GetHash() const
Definition: main.h:515
bool AreInputsStandard(CCoinsViewCache &mapInputs) const
Check for standard transaction types.
Definition: main.cpp:434
uint256 SignatureHash(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
Definition: script.cpp:966
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: main.h:896
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:109
Definition: script.h:89
void SetDestination(const CTxDestination &address)
Definition: script.cpp:1768
virtual bool AddCScript(const CScript &redeemScript)
Definition: keystore.cpp:29
bool VerifySignature(const CCoins &txFrom, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Verify a signature.
Definition: main.cpp:1470
unsigned int GetP2SHSigOpCount(CCoinsViewCache &mapInputs) const
Count ECDSA signature operations in pay-to-script-hash inputs.
Definition: main.cpp:1411
int std::string int dummy
Definition: util.h:164
Abstract view on the open txout dataset.
Definition: main.h:2146
CPubKey GetPubKey() const
Definition: key.cpp:312
std::vector< CTxOut > vout
Definition: main.h:485
std::vector< CTxIn > vin
Definition: main.h:484
Definition: script.h:77
void MakeNewKey(bool fCompressed)
Definition: key.cpp:285
Definition: script.h:96
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:18
256-bit unsigned integer
Definition: uint256.h:537
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
Definition: script.cpp:1773
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: script.cpp:1378
CScriptID GetID() const
Definition: script.h:589
bool IsStandard(const CScript &scriptPubKey)
Definition: script.cpp:1332
160-bit unsigned integer
Definition: uint256.h:422
bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: main.cpp:232
An encapsulated private key.
Definition: key.h:172
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: main.h:477
Definition: script.h:88
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: main.h:2194
bool IsStandard(std::string &strReason) const
Check for standard transaction types.
Definition: script.h:70
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:43