txTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 txTest.cpp
  15. * @author Marko Simovic <markobarko@gmail.com>
  16. * @date 2014
  17. * Simple peer transaction send test.
  18. */
  19. #include <boost/test/unit_test.hpp>
  20. #include <boost/filesystem/operations.hpp>
  21. #include <libethereum/Client.h>
  22. #include <libethereum/BlockChain.h>
  23. #include <libethereum/EthereumHost.h>
  24. #include "TestHelper.h"
  25. using namespace std;
  26. using namespace dev;
  27. using namespace dev::eth;
  28. // Disabled since tests shouldn't block. Need a short cut to avoid real mining.
  29. /*
  30. BOOST_AUTO_TEST_CASE(mine_local_simple_tx)
  31. {
  32. KeyPair kp1 = KeyPair::create();
  33. KeyPair kp2 = KeyPair::create();
  34. Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string());
  35. //mine some blocks so that client 1 has a balance
  36. mine(c1, 1);
  37. auto c1bal = c1.state().balance(kp1.address());
  38. BOOST_REQUIRE(c1bal > 0);
  39. //send c2 some eth from c1
  40. auto txAmount = c1bal / 2u;
  41. auto gasPrice = 10 * szabo;
  42. auto gas = eth::EVMSchedule().callGas;
  43. c1.submitTransaction(kp1.secret(), txAmount, kp2.address(), bytes(), gas, gasPrice);
  44. //mine some more to include the transaction on chain
  45. mine(c1, 1);
  46. auto c2bal = c1.state().balance(kp2.address());
  47. BOOST_REQUIRE(c2bal > 0);
  48. BOOST_REQUIRE(c2bal == txAmount);
  49. }
  50. BOOST_AUTO_TEST_CASE(mine_and_send_to_peer)
  51. {
  52. KeyPair kp1 = KeyPair::create();
  53. KeyPair kp2 = KeyPair::create();
  54. Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string());
  55. Client c2("TestClient2", kp2.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string());
  56. connectClients(c1, c2);
  57. //mine some blocks so that client 1 has a balance
  58. mine(c1, 1);
  59. auto c1bal = c1.state().balance(kp1.address());
  60. BOOST_REQUIRE(c1bal > 0);
  61. //send c2 some eth from c1
  62. auto txAmount = c1bal / 2u;
  63. auto gasPrice = 10 * szabo;
  64. auto gas = eth::EVMSchedule().callGas;
  65. c1.submitTransaction(kp1.secret(), txAmount, kp2.address(), bytes(), gas, gasPrice);
  66. //mine some more to include the transaction on chain
  67. mine(c1, 1);
  68. auto c2bal = c2.state().balance(kp2.address());
  69. BOOST_REQUIRE(c2bal > 0);
  70. BOOST_REQUIRE(c2bal == txAmount);
  71. }
  72. BOOST_AUTO_TEST_CASE(mine_and_send_to_peer_fee_check)
  73. {
  74. KeyPair kp1 = KeyPair::create();
  75. KeyPair kp2 = KeyPair::create();
  76. Client c1("TestClient1", kp1.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string());
  77. Client c2("TestClient2", kp2.address(), (boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()).string());
  78. connectClients(c1, c2);
  79. //mine some blocks so that client 1 has a balance
  80. mine(c1, 1);
  81. auto c1StartBalance = c1.state().balance(kp1.address());
  82. auto c2StartBalance = c2.state().balance(kp2.address());
  83. BOOST_REQUIRE(c1StartBalance > 0);
  84. BOOST_REQUIRE(c2StartBalance == 0);
  85. //send c2 some eth from c1
  86. auto txAmount = c1StartBalance / 2u;
  87. auto gasPrice = 10 * szabo;
  88. auto gas = eth::EVMSchedule().callGas;
  89. c1.submitTransaction(kp1.secret(), txAmount, c2.address(), bytes(), gas, gasPrice);
  90. //mine some more, this time with second client (so he can get fees from first client's tx)
  91. mine(c2, 1);
  92. auto c1EndBalance = c1.state().balance(kp1.address());
  93. auto c2EndBalance = c2.state().balance(kp2.address());
  94. BOOST_REQUIRE(c1EndBalance > 0);
  95. BOOST_REQUIRE(c1EndBalance == c1StartBalance - txAmount - gasPrice * gas);
  96. BOOST_REQUIRE(c2EndBalance > 0);
  97. }
  98. */