TransactionTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file transactionTests.cpp
  15. * @author Dmitrii Khokhlov <winsvega@mail.ru>
  16. * @date 2015
  17. * Transaction test functions.
  18. */
  19. #include <libethcore/SealEngine.h>
  20. #include <libethashseal/GenesisInfo.h>
  21. #include <libethereum/ChainParams.h>
  22. #include <test/libtestutils/Common.h>
  23. #include <test/TestHelper.h>
  24. #include <test/fuzzTesting/fuzzHelper.h>
  25. using namespace std;
  26. using namespace json_spirit;
  27. using namespace dev;
  28. using namespace dev::eth;
  29. namespace dev { namespace test {
  30. void doTransactionTests(json_spirit::mValue& _v, bool _fillin)
  31. {
  32. TestOutputHelper::initTest(_v);
  33. for (auto& i: _v.get_obj())
  34. {
  35. string testname = i.first;
  36. json_spirit::mObject& o = i.second.get_obj();
  37. if (!TestOutputHelper::passTest(o, testname))
  38. continue;
  39. eth::Network sealEngineNet;
  40. BOOST_REQUIRE(o.count("blocknumber") > 0);
  41. u256 transactionBlock = toInt(o["blocknumber"].get_str());
  42. if (transactionBlock >= dev::test::c_testHomesteadBlock)
  43. sealEngineNet = eth::Network::HomesteadTest;
  44. else
  45. sealEngineNet = eth::Network::FrontierTest;
  46. unique_ptr<SealEngineFace> se(ChainParams(genesisInfo(sealEngineNet)).createSealEngine());
  47. BlockHeader bh;
  48. bh.setNumber(transactionBlock);
  49. if (_fillin)
  50. {
  51. BOOST_REQUIRE(o.count("transaction") > 0);
  52. mObject tObj = o["transaction"].get_obj();
  53. //Construct Rlp of the given transaction
  54. RLPStream rlpStream = createRLPStreamFromTransactionFields(tObj);
  55. o["rlp"] = toHex(rlpStream.out(), 2, HexPrefix::Add);
  56. try
  57. {
  58. Transaction txFromFields(rlpStream.out(), CheckTransaction::Everything);
  59. if (!txFromFields.signature().isValid())
  60. BOOST_THROW_EXCEPTION(Exception() << errinfo_comment(testname + "transaction from RLP signature is invalid") );
  61. se->verifyTransaction(ImportRequirements::Everything, txFromFields, bh);
  62. if (o.count("sender") > 0)
  63. {
  64. string expectSender = toString(o["sender"].get_str());
  65. BOOST_CHECK_MESSAGE(toString(txFromFields.sender()) == expectSender, "Error filling transaction test " + TestOutputHelper::testName() + ": expected another sender address! (got: " + toString(txFromFields.sender()) + "), expected: (" + expectSender + ")");
  66. }
  67. o["sender"] = toString(txFromFields.sender());
  68. o["transaction"] = ImportTest::makeAllFieldsHex(tObj);
  69. o["hash"] = toString(txFromFields.sha3());
  70. }
  71. catch(Exception const& _e)
  72. {
  73. //Transaction is InValid
  74. cnote << "Transaction Exception: " << diagnostic_information(_e);
  75. o.erase(o.find("transaction"));
  76. if (o.count("expect") > 0)
  77. {
  78. bool expectInValid = (o["expect"].get_str() == "invalid");
  79. if (Options::get().checkState)
  80. BOOST_CHECK_MESSAGE(expectInValid, testname + "Check state: Transaction '" << i.first << "' is expected to be valid!");
  81. else
  82. BOOST_WARN_MESSAGE(expectInValid, testname + "Check state: Transaction '" << i.first << "' is expected to be valid!");
  83. o.erase(o.find("expect"));
  84. }
  85. }
  86. //Transaction is Valid
  87. if (o.count("expect") > 0)
  88. {
  89. bool expectValid = (o["expect"].get_str() == "valid");
  90. if (Options::get().checkState)
  91. BOOST_CHECK_MESSAGE(expectValid, testname + "Check state: Transaction '" << i.first << "' is expected to be invalid!");
  92. else
  93. BOOST_WARN_MESSAGE(expectValid, testname + "Check state: Transaction '" << i.first << "' is expected to be invalid!");
  94. o.erase(o.find("expect"));
  95. }
  96. }
  97. else
  98. {
  99. BOOST_REQUIRE(o.count("rlp") > 0);
  100. Transaction txFromRlp;
  101. try
  102. {
  103. bytes stream = importByteArray(o["rlp"].get_str());
  104. RLP rlp(stream);
  105. txFromRlp = Transaction(rlp.data(), CheckTransaction::Everything);
  106. se->verifyTransaction(ImportRequirements::Everything, txFromRlp, bh);
  107. if (!txFromRlp.signature().isValid())
  108. BOOST_THROW_EXCEPTION(Exception() << errinfo_comment(testname + "transaction from RLP signature is invalid") );
  109. }
  110. catch(Exception const& _e)
  111. {
  112. cnote << i.first;
  113. cnote << "Transaction Exception: " << diagnostic_information(_e);
  114. BOOST_CHECK_MESSAGE(o.count("transaction") == 0, testname + "A transaction object should not be defined because the RLP is invalid!");
  115. continue;
  116. }
  117. catch(...)
  118. {
  119. BOOST_CHECK_MESSAGE(o.count("transaction") == 0, testname + "A transaction object should not be defined because the RLP is invalid!");
  120. continue;
  121. }
  122. BOOST_REQUIRE_MESSAGE(o.count("transaction") > 0, testname + "Expected a valid transaction!");
  123. mObject tObj = o["transaction"].get_obj();
  124. h256 txSha3Expected = h256(o["hash"].get_str());
  125. Transaction txFromFields(createRLPStreamFromTransactionFields(tObj).out(), CheckTransaction::Everything);
  126. //Check the fields restored from RLP to original fields
  127. BOOST_CHECK_MESSAGE(txFromFields.data() == txFromRlp.data(), testname + "Data in given RLP not matching the Transaction data!");
  128. BOOST_CHECK_MESSAGE(txFromFields.value() == txFromRlp.value(), testname + "Value in given RLP not matching the Transaction value!");
  129. BOOST_CHECK_MESSAGE(txFromFields.gasPrice() == txFromRlp.gasPrice(), testname + "GasPrice in given RLP not matching the Transaction gasPrice!");
  130. BOOST_CHECK_MESSAGE(txFromFields.gas() == txFromRlp.gas(), testname + "Gas in given RLP not matching the Transaction gas!");
  131. BOOST_CHECK_MESSAGE(txFromFields.nonce() == txFromRlp.nonce(), testname + "Nonce in given RLP not matching the Transaction nonce!");
  132. BOOST_CHECK_MESSAGE(txFromFields.receiveAddress() == txFromRlp.receiveAddress(), testname + "Receive address in given RLP not matching the Transaction 'to' address!");
  133. BOOST_CHECK_MESSAGE(txFromFields.sender() == txFromRlp.sender(), testname + "Transaction sender address in given RLP not matching the Transaction 'vrs' signature!");
  134. BOOST_CHECK_MESSAGE(txFromFields.sha3() == txFromRlp.sha3(), testname + "Transaction sha3 hash in given RLP not matching the Transaction 'vrs' signature!");
  135. BOOST_CHECK_MESSAGE(txFromFields.sha3() == txSha3Expected, testname + "Expected different transaction hash!");
  136. BOOST_CHECK_MESSAGE(txFromFields == txFromRlp, testname + "However, txFromFields != txFromRlp!");
  137. BOOST_REQUIRE (o.count("sender") > 0);
  138. Address addressReaded = Address(o["sender"].get_str());
  139. BOOST_CHECK_MESSAGE(txFromFields.sender() == addressReaded || txFromRlp.sender() == addressReaded, testname + "Signature address of sender does not match given sender address!");
  140. }
  141. }//for
  142. }//doTransactionTests
  143. } }// Namespace Close
  144. BOOST_AUTO_TEST_SUITE(TransactionTests)
  145. BOOST_AUTO_TEST_CASE(ttTransactionTestEip155VitaliksTests)
  146. {
  147. dev::test::executeTests("ttTransactionTestEip155VitaliksTests", "/TransactionTests/EIP155", "/TransactionTestsFiller/EIP155", dev::test::doTransactionTests);
  148. }
  149. BOOST_AUTO_TEST_CASE(ttTransactionTestEip155VCheck)
  150. {
  151. dev::test::executeTests("ttTransactionTestVRule", "/TransactionTests/EIP155", "/TransactionTestsFiller/EIP155", dev::test::doTransactionTests);
  152. }
  153. BOOST_AUTO_TEST_CASE(ttTransactionTestEip155)
  154. {
  155. dev::test::executeTests("ttTransactionTest", "/TransactionTests/EIP155", "/TransactionTestsFiller/EIP155", dev::test::doTransactionTests);
  156. }
  157. BOOST_AUTO_TEST_CASE(ttTransactionTestEip155VitaliksTestsHomestead)
  158. {
  159. dev::test::executeTests("ttTransactionTestEip155VitaliksTests", "/TransactionTests/Homestead", "/TransactionTestsFiller/Homestead", dev::test::doTransactionTests);
  160. }
  161. BOOST_AUTO_TEST_CASE(ttTransactionTestHomestead)
  162. {
  163. dev::test::executeTests("ttTransactionTest", "/TransactionTests/Homestead", "/TransactionTestsFiller/Homestead", dev::test::doTransactionTests);
  164. }
  165. BOOST_AUTO_TEST_CASE(ttTransactionTest)
  166. {
  167. dev::test::executeTests("ttTransactionTest", "/TransactionTests", "/TransactionTestsFiller", dev::test::doTransactionTests);
  168. }
  169. BOOST_AUTO_TEST_CASE(ttWrongRLPTransactionHomestead)
  170. {
  171. std::string fillersPath = dev::test::getTestPath() + "/src/TransactionTestsFiller/Homestead";
  172. if (!dev::test::Options::get().fillTests)
  173. dev::test::executeTests("ttWrongRLPTransaction", "/TransactionTests", "/TransactionTestsFiller/Homestead", dev::test::doTransactionTests);
  174. else
  175. {
  176. dev::test::TestOutputHelper::initTest();
  177. dev::test::copyFile(fillersPath + "/ttWrongRLPTransaction.json", dev::test::getTestPath() + "/TransactionTests/Homestead/ttWrongRLPTransaction.json");
  178. }
  179. }
  180. BOOST_AUTO_TEST_CASE(ttWrongRLPTransaction)
  181. {
  182. std::string fillersPath = dev::test::getTestPath() + "/src/TransactionTestsFiller";
  183. if (!dev::test::Options::get().fillTests)
  184. dev::test::executeTests("ttWrongRLPTransaction", "/TransactionTests", "/TransactionTestsFiller", dev::test::doTransactionTests);
  185. else
  186. {
  187. dev::test::TestOutputHelper::initTest();
  188. dev::test::copyFile(fillersPath + "/ttWrongRLPTransaction.json", dev::test::getTestPath() + "/TransactionTests/ttWrongRLPTransaction.json");
  189. }
  190. }
  191. BOOST_AUTO_TEST_CASE(tt10mbDataFieldHomestead)
  192. {
  193. if (test::Options::get().bigData)
  194. {
  195. auto start = chrono::steady_clock::now();
  196. dev::test::executeTests("tt10mbDataField", "/TransactionTests/Homestead", "/TransactionTestsFiller/Homestead", dev::test::doTransactionTests);
  197. auto end = chrono::steady_clock::now();
  198. auto duration(chrono::duration_cast<chrono::milliseconds>(end - start));
  199. cnote << "test duration: " << duration.count() << " milliseconds.\n";
  200. }
  201. }
  202. BOOST_AUTO_TEST_CASE(tt10mbDataField)
  203. {
  204. if (test::Options::get().bigData)
  205. {
  206. auto start = chrono::steady_clock::now();
  207. dev::test::executeTests("tt10mbDataField", "/TransactionTests", "/TransactionTestsFiller", dev::test::doTransactionTests);
  208. auto end = chrono::steady_clock::now();
  209. auto duration(chrono::duration_cast<chrono::milliseconds>(end - start));
  210. cnote << "test duration: " << duration.count() << " milliseconds.\n";
  211. }
  212. }
  213. BOOST_AUTO_TEST_CASE(userDefinedFile)
  214. {
  215. dev::test::userDefinedTest(dev::test::doTransactionTests);
  216. }
  217. BOOST_AUTO_TEST_SUITE_END()