GasPricer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 gasPricer.cpp
  15. * @author Christoph Jentzsch <cj@ethdev.com>
  16. * @date 2015
  17. * Gas pricer tests
  18. */
  19. #include <libethashseal/Ethash.h>
  20. #include <libethereum/BlockChain.h>
  21. #include <libethereum/ChainParams.h>
  22. #include <libethereum/GasPricer.h>
  23. #include <libethereum/BasicGasPricer.h>
  24. #include <test/TestHelper.h>
  25. #include <test/libtestutils/BlockChainLoader.h>
  26. using namespace std;
  27. using namespace dev;
  28. using namespace dev::eth;
  29. using namespace dev::test;
  30. namespace dev { namespace test {
  31. void executeGasPricerTest(string const& name, double _etherPrice, double _blockFee, string const& bcTestPath, TransactionPriority _txPrio, u256 _expectedAsk, u256 _expectedBid, eth::Network _sealEngineNetwork = eth::Network::Test)
  32. {
  33. BasicGasPricer gp(u256(double(ether / 1000) / _etherPrice), u256(_blockFee * 1000));
  34. Json::Value vJson = test::loadJsonFromFile(test::getTestPath() + bcTestPath);
  35. test::BlockChainLoader bcLoader(vJson[name], _sealEngineNetwork);
  36. BlockChain const& bc = bcLoader.bc();
  37. gp.update(bc);
  38. BOOST_CHECK(abs(gp.ask(Block(Block::Null)) - _expectedAsk ) < 100000000);
  39. BOOST_CHECK(abs(gp.bid(_txPrio) - _expectedBid ) < 100000000);
  40. }
  41. } }
  42. BOOST_FIXTURE_TEST_SUITE(GasPricer, TestOutputHelper)
  43. BOOST_AUTO_TEST_CASE(trivialGasPricer)
  44. {
  45. std::shared_ptr<dev::eth::GasPricer> gp(new TrivialGasPricer);
  46. BOOST_CHECK_EQUAL(gp->ask(Block(Block::Null)), DefaultGasPrice);
  47. BOOST_CHECK_EQUAL(gp->bid(), DefaultGasPrice);
  48. gp->update(BlockChain(eth::ChainParams(), TransientDirectory().path(), WithExisting::Kill));
  49. BOOST_CHECK_EQUAL(gp->ask(Block(Block::Null)), DefaultGasPrice);
  50. BOOST_CHECK_EQUAL(gp->bid(), DefaultGasPrice);
  51. }
  52. BOOST_AUTO_TEST_CASE(basicGasPricerNoUpdate)
  53. {
  54. BasicGasPricer gp(u256(double(ether / 1000) / 30.679), u256(15.0 * 1000));
  55. BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 103754996057);
  56. BOOST_CHECK_EQUAL(gp.bid(), 103754996057);
  57. gp.setRefPrice(u256(0));
  58. BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
  59. BOOST_CHECK_EQUAL(gp.bid(), 0);
  60. gp.setRefPrice(u256(1));
  61. gp.setRefBlockFees(u256(0));
  62. BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
  63. BOOST_CHECK_EQUAL(gp.bid(), 0);
  64. gp.setRefPrice(u256("0x100000000000000000000000000000000"));
  65. BOOST_CHECK_THROW(gp.setRefBlockFees(u256("0x100000000000000000000000000000000")), Overflow);
  66. BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
  67. BOOST_CHECK_EQUAL(gp.bid(), 0);
  68. gp.setRefPrice(1);
  69. gp.setRefBlockFees(u256("0x100000000000000000000000000000000"));
  70. BOOST_CHECK_THROW(gp.setRefPrice(u256("0x100000000000000000000000000000000")), Overflow);
  71. BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), u256("72210176012870430758964373780717"));
  72. BOOST_CHECK_EQUAL(gp.bid(), u256("72210176012870430758964373780717"));
  73. }
  74. BOOST_AUTO_TEST_CASE(basicGasPricer_RPC_API_Test_Frontier)
  75. {
  76. u256 _expectedAsk = 155632494086;
  77. u256 _expectedBid = 155633980282;
  78. dev::test::executeGasPricerTest("RPC_API_Test", 30.679, 15.0, "/BlockchainTests/bcRPC_API_Test.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  79. }
  80. BOOST_AUTO_TEST_CASE(basicGasPricer_RPC_API_Test_Homestead)
  81. {
  82. u256 _expectedAsk = 155633980282;
  83. u256 _expectedBid = 155633980282;
  84. dev::test::executeGasPricerTest("RPC_API_Test", 30.679, 15.0, "/BlockchainTests/Homestead/bcRPC_API_Test.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
  85. }
  86. BOOST_AUTO_TEST_CASE(basicGasPricer_bcValidBlockTest)
  87. {
  88. dev::test::executeGasPricerTest("SimpleTx", 30.679, 15.0, "/BlockchainTests/bcValidBlockTest.json", TransactionPriority::Medium, 155632494086, 10);
  89. }
  90. BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleTest_Frontier)
  91. {
  92. u256 _expectedAsk = 155632494086;
  93. u256 _expectedBid = 1;
  94. dev::test::executeGasPricerTest("twoUncle", 30.679, 15.0, "/BlockchainTests/bcUncleTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  95. }
  96. BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleTest_Homestead)
  97. {
  98. u256 _expectedAsk = 155633980282;
  99. u256 _expectedBid = 1;
  100. dev::test::executeGasPricerTest("twoUncle", 30.679, 15.0, "/BlockchainTests/Homestead/bcUncleTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
  101. }
  102. BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleHeaderValiditiy_Frontier)
  103. {
  104. u256 _expectedAsk = 155632494086;
  105. u256 _expectedBid = 1;
  106. dev::test::executeGasPricerTest("correct", 30.679, 15.0, "/BlockchainTests/bcUncleHeaderValiditiy.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  107. }
  108. BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleHeaderValiditiy_Homestead)
  109. {
  110. u256 _expectedAsk = 155633980282;
  111. u256 _expectedBid = 1;
  112. dev::test::executeGasPricerTest("correct", 30.679, 15.0, "/BlockchainTests/Homestead/bcUncleHeaderValiditiy.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
  113. }
  114. BOOST_AUTO_TEST_CASE(basicGasPricer_notxs_frontier)
  115. {
  116. u256 _expectedAsk = 155632494086;
  117. u256 _expectedBid = 155632494086;
  118. dev::test::executeGasPricerTest("notxs", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  119. }
  120. BOOST_AUTO_TEST_CASE(basicGasPricer_notxs_homestead)
  121. {
  122. u256 _expectedAsk = 155633980282;
  123. u256 _expectedBid = 155633980282;
  124. dev::test::executeGasPricerTest("notxs", 30.679, 15.0, "/BlockchainTests/Homestead/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
  125. }
  126. BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_LowestPrio)
  127. {
  128. u256 _expectedAsk = 15731282021;
  129. u256 _expectedBid = 10000000000000;
  130. dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Lowest, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  131. }
  132. BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_LowPrio)
  133. {
  134. u256 _expectedAsk = 15731282021;
  135. u256 _expectedBid = 15734152261884;
  136. dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Low, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  137. }
  138. BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_MediumPrio)
  139. {
  140. u256 _expectedAsk = 15731282021;
  141. u256 _expectedBid = 20000000000000;
  142. dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  143. }
  144. BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_HighPrio)
  145. {
  146. u256 _expectedAsk = 15731282021;
  147. u256 _expectedBid = 24265847738115;
  148. dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::High, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  149. }
  150. BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_HighestPrio)
  151. {
  152. u256 _expectedAsk = 15731282021;
  153. u256 _expectedBid = 30000000000000;
  154. dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Highest, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
  155. }
  156. BOOST_AUTO_TEST_SUITE_END()