Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
transaction_tests.cpp
Go to the documentation of this file.
1 #include <map>
2 #include <string>
3 #include <boost/test/unit_test.hpp>
5 
6 #include "main.h"
7 #include "wallet.h"
8 
9 using namespace std;
10 using namespace json_spirit;
11 
12 // In script_tests.cpp
13 extern Array read_json(const std::string& filename);
14 extern CScript ParseScript(string s);
15 
16 BOOST_AUTO_TEST_SUITE(transaction_tests)
17 
19 {
20  // Read tests from test/data/tx_valid.json
21  // Format is an array of arrays
22  // Inner arrays are either [ "comment" ]
23  // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
24  // ... where all scripts are stringified scripts.
25  Array tests = read_json("tx_valid.json");
26 
27  BOOST_FOREACH(Value& tv, tests)
28  {
29  Array test = tv.get_array();
30  string strTest = write_string(tv, false);
31  if (test[0].type() == array_type)
32  {
33  if (test.size() != 3 || test[1].type() != str_type || test[2].type() != bool_type)
34  {
35  BOOST_ERROR("Bad test: " << strTest);
36  continue;
37  }
38 
39  map<COutPoint, CScript> mapprevOutScriptPubKeys;
40  Array inputs = test[0].get_array();
41  bool fValid = true;
42  BOOST_FOREACH(Value& input, inputs)
43  {
44  if (input.type() != array_type)
45  {
46  fValid = false;
47  break;
48  }
49  Array vinput = input.get_array();
50  if (vinput.size() != 3)
51  {
52  fValid = false;
53  break;
54  }
55 
56  mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
57  }
58  if (!fValid)
59  {
60  BOOST_ERROR("Bad test: " << strTest);
61  continue;
62  }
63 
64  string transaction = test[1].get_str();
65  CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION);
66  CTransaction tx;
67  stream >> tx;
68 
70  BOOST_CHECK_MESSAGE(tx.CheckTransaction(state), strTest);
71  BOOST_CHECK(state.IsValid());
72 
73  for (unsigned int i = 0; i < tx.vin.size(); i++)
74  {
75  if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
76  {
77  BOOST_ERROR("Bad test: " << strTest);
78  break;
79  }
80 
81  BOOST_CHECK_MESSAGE(VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout], tx, i, test[2].get_bool() ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, 0), strTest);
82  }
83  }
84  }
85 }
86 
88 {
89  // Read tests from test/data/tx_invalid.json
90  // Format is an array of arrays
91  // Inner arrays are either [ "comment" ]
92  // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
93  // ... where all scripts are stringified scripts.
94  Array tests = read_json("tx_invalid.json");
95 
96  BOOST_FOREACH(Value& tv, tests)
97  {
98  Array test = tv.get_array();
99  string strTest = write_string(tv, false);
100  if (test[0].type() == array_type)
101  {
102  if (test.size() != 3 || test[1].type() != str_type || test[2].type() != bool_type)
103  {
104  BOOST_ERROR("Bad test: " << strTest);
105  continue;
106  }
107 
108  map<COutPoint, CScript> mapprevOutScriptPubKeys;
109  Array inputs = test[0].get_array();
110  bool fValid = true;
111  BOOST_FOREACH(Value& input, inputs)
112  {
113  if (input.type() != array_type)
114  {
115  fValid = false;
116  break;
117  }
118  Array vinput = input.get_array();
119  if (vinput.size() != 3)
120  {
121  fValid = false;
122  break;
123  }
124 
125  mapprevOutScriptPubKeys[COutPoint(uint256(vinput[0].get_str()), vinput[1].get_int())] = ParseScript(vinput[2].get_str());
126  }
127  if (!fValid)
128  {
129  BOOST_ERROR("Bad test: " << strTest);
130  continue;
131  }
132 
133  string transaction = test[1].get_str();
134  CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION);
135  CTransaction tx;
136  stream >> tx;
137 
139  fValid = tx.CheckTransaction(state) && state.IsValid();
140 
141  for (unsigned int i = 0; i < tx.vin.size() && fValid; i++)
142  {
143  if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
144  {
145  BOOST_ERROR("Bad test: " << strTest);
146  break;
147  }
148 
149  fValid = VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout], tx, i, test[2].get_bool() ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, 0);
150  }
151 
152  BOOST_CHECK_MESSAGE(!fValid, strTest);
153  }
154  }
155 }
156 
157 BOOST_AUTO_TEST_CASE(basic_transaction_tests)
158 {
159  // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
160  unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
161  vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
162  CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
163  CTransaction tx;
164  stream >> tx;
166  BOOST_CHECK_MESSAGE(tx.CheckTransaction(state) && state.IsValid(), "Simple deserialized transaction should be valid.");
167 
168  // Check that duplicate txins fail
169  tx.vin.push_back(tx.vin[0]);
170  BOOST_CHECK_MESSAGE(!tx.CheckTransaction(state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
171 }
172 
173 //
174 // Helper: create two dummy transactions, each with
175 // two outputs. The first has 11 and 50 CENT outputs
176 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
177 // paid to a TX_PUBKEYHASH.
178 //
179 static std::vector<CTransaction>
180 SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsView & coinsRet)
181 {
182  std::vector<CTransaction> dummyTransactions;
183  dummyTransactions.resize(2);
184 
185  // Add some keys to the keystore:
186  CKey key[4];
187  for (int i = 0; i < 4; i++)
188  {
189  key[i].MakeNewKey(i % 2);
190  keystoreRet.AddKey(key[i]);
191  }
192 
193  // Create some dummy input transactions
194  dummyTransactions[0].vout.resize(2);
195  dummyTransactions[0].vout[0].nValue = 11*CENT;
196  dummyTransactions[0].vout[0].scriptPubKey << key[0].GetPubKey() << OP_CHECKSIG;
197  dummyTransactions[0].vout[1].nValue = 50*CENT;
198  dummyTransactions[0].vout[1].scriptPubKey << key[1].GetPubKey() << OP_CHECKSIG;
199  coinsRet.SetCoins(dummyTransactions[0].GetHash(), CCoins(dummyTransactions[0], 0));
200 
201  dummyTransactions[1].vout.resize(2);
202  dummyTransactions[1].vout[0].nValue = 21*CENT;
203  dummyTransactions[1].vout[0].scriptPubKey.SetDestination(key[2].GetPubKey().GetID());
204  dummyTransactions[1].vout[1].nValue = 22*CENT;
205  dummyTransactions[1].vout[1].scriptPubKey.SetDestination(key[3].GetPubKey().GetID());
206  coinsRet.SetCoins(dummyTransactions[1].GetHash(), CCoins(dummyTransactions[1], 0));
207 
208  return dummyTransactions;
209 }
210 
212 {
213  CBasicKeyStore keystore;
214  CCoinsView coinsDummy;
215  CCoinsViewCache coins(coinsDummy);
216  std::vector<CTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
217 
218  CTransaction t1;
219  t1.vin.resize(3);
220  t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
221  t1.vin[0].prevout.n = 1;
222  t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
223  t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
224  t1.vin[1].prevout.n = 0;
225  t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
226  t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
227  t1.vin[2].prevout.n = 1;
228  t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
229  t1.vout.resize(2);
230  t1.vout[0].nValue = 90*CENT;
231  t1.vout[0].scriptPubKey << OP_1;
232 
233  BOOST_CHECK(t1.AreInputsStandard(coins));
234  BOOST_CHECK_EQUAL(t1.GetValueIn(coins), (50+21+22)*CENT);
235 
236  // Adding extra junk to the scriptSig should make it non-standard:
237  t1.vin[0].scriptSig << OP_11;
238  BOOST_CHECK(!t1.AreInputsStandard(coins));
239 
240  // ... as should not having enough:
241  t1.vin[0].scriptSig = CScript();
242  BOOST_CHECK(!t1.AreInputsStandard(coins));
243 }
244 
245 BOOST_AUTO_TEST_CASE(test_IsStandard)
246 {
247  CBasicKeyStore keystore;
248  CCoinsView coinsDummy;
249  CCoinsViewCache coins(coinsDummy);
250  std::vector<CTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
251 
252  CTransaction t;
253  t.vin.resize(1);
254  t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
255  t.vin[0].prevout.n = 1;
256  t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
257  t.vout.resize(1);
258  t.vout[0].nValue = 90*CENT;
259  CKey key;
260  key.MakeNewKey(true);
261  t.vout[0].scriptPubKey.SetDestination(key.GetPubKey().GetID());
262 
263  BOOST_CHECK(t.IsStandard());
264 
265  t.vout[0].nValue = 5011; // dust
266  // Feathercoin does not enforce isDust(). Per dust fees are considered sufficient as deterrant.
267  // BOOST_CHECK(!t.IsStandard());
268 
269  t.vout[0].nValue = 6011; // not dust
270  BOOST_CHECK(t.IsStandard());
271 
272  t.vout[0].scriptPubKey = CScript() << OP_1;
273  BOOST_CHECK(!t.IsStandard());
274 }
275 
276 BOOST_AUTO_TEST_SUITE_END()
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:1477
bool AreInputsStandard(CCoinsViewCache &mapInputs) const
Check for standard transaction types.
Definition: main.cpp:434
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:799
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: main.h:896
const Array & get_array() const
Value_type type() const
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
MTState * state
Definition: db_test.cc:1708
Definition: script.h:77
void MakeNewKey(bool fCompressed)
Definition: key.cpp:285
Config::Array_type Array
int64 GetValueIn(CCoinsViewCache &mapInputs) const
Amount of bitcoins coming in to this transaction Note that lightweight clients may not know anything ...
Definition: main.cpp:1399
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: main.h:278
DBTest * test
Definition: db_test.cc:1701
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:18
BOOST_AUTO_TEST_CASE(tx_valid)
Capture information about block/transaction validation.
Definition: main.h:1907
256-bit unsigned integer
Definition: uint256.h:537
Value_type::String_type write_string(const Value_type &value, bool pretty)
bool IsValid()
Definition: main.h:1937
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
Array read_json(const std::string &filename)
CScript ParseScript(string s)
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.
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
CKeyID GetID() const
Definition: key.h:129
vector< unsigned char > ParseHex(const char *psz)
Definition: util.cpp:500
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:43
virtual bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: main.cpp:182