Fabcoin Core  0.16.2
P2P Digital Currency
GasPricer.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #include <libethashseal/Ethash.h>
24 #include <libethereum/BlockChain.h>
26 #include <libethereum/GasPricer.h>
30 
31 using namespace std;
32 using namespace dev;
33 using namespace dev::eth;
34 using namespace dev::test;
35 
36 namespace dev { namespace test {
37 
38 void executeGasPricerTest(string const& name, double _etherPrice, double _blockFee, string const& bcTestPath, TransactionPriority _txPrio, u256 _expectedAsk, u256 _expectedBid, eth::Network _sealEngineNetwork = eth::Network::TransitionnetTest)
39 {
40  BasicGasPricer gp(u256(double(ether / 1000) / _etherPrice), u256(_blockFee * 1000));
41 
43  test::BlockChainLoader bcLoader(vJson[name], _sealEngineNetwork);
44  BlockChain const& bc = bcLoader.bc();
45 
46  gp.update(bc);
47  BOOST_CHECK(abs(gp.ask(Block(Block::Null)) - _expectedAsk ) < 100000000);
48  BOOST_CHECK(abs(gp.bid(_txPrio) - _expectedBid ) < 100000000);
49 }
50 } }
51 
53 
54 BOOST_AUTO_TEST_CASE(trivialGasPricer)
55 {
56  std::shared_ptr<dev::eth::GasPricer> gp(new TrivialGasPricer);
57  BOOST_CHECK_EQUAL(gp->ask(Block(Block::Null)), DefaultGasPrice);
58  BOOST_CHECK_EQUAL(gp->bid(), DefaultGasPrice);
59 
60  gp->update(BlockChain(eth::ChainParams(), TransientDirectory().path(), WithExisting::Kill));
61  BOOST_CHECK_EQUAL(gp->ask(Block(Block::Null)), DefaultGasPrice);
62  BOOST_CHECK_EQUAL(gp->bid(), DefaultGasPrice);
63 }
64 
65 BOOST_AUTO_TEST_CASE(basicGasPricerNoUpdate)
66 {
67  BasicGasPricer gp(u256(double(ether / 1000) / 30.679), u256(15.0 * 1000));
68  BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 103754996057);
69  BOOST_CHECK_EQUAL(gp.bid(), 103754996057);
70 
71  gp.setRefPrice(u256(0));
72  BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
73  BOOST_CHECK_EQUAL(gp.bid(), 0);
74 
75  gp.setRefPrice(u256(1));
76  gp.setRefBlockFees(u256(0));
77  BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
78  BOOST_CHECK_EQUAL(gp.bid(), 0);
79 
80  gp.setRefPrice(u256("0x100000000000000000000000000000000"));
81  BOOST_CHECK_THROW(gp.setRefBlockFees(u256("0x100000000000000000000000000000000")), Overflow);
82  BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), 0);
83  BOOST_CHECK_EQUAL(gp.bid(), 0);
84 
85  gp.setRefPrice(1);
86  gp.setRefBlockFees(u256("0x100000000000000000000000000000000"));
87  BOOST_CHECK_THROW(gp.setRefPrice(u256("0x100000000000000000000000000000000")), Overflow);
88  BOOST_CHECK_EQUAL(gp.ask(Block(Block::Null)), u256("72210176012870430758964373780717"));
89  BOOST_CHECK_EQUAL(gp.bid(), u256("72210176012870430758964373780717"));
90 }
91 
92 BOOST_AUTO_TEST_CASE(basicGasPricer_RPC_API_Test_Frontier)
93 {
94  u256 _expectedAsk = 155632494086;
95  u256 _expectedBid = 155633980282;
96  dev::test::executeGasPricerTest("RPC_API_Test", 30.679, 15.0, "/BlockchainTests/bcRPC_API_Test.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
97 }
98 
99 BOOST_AUTO_TEST_CASE(basicGasPricer_RPC_API_Test_Homestead)
100 {
101  u256 _expectedAsk = 155633980282;
102  u256 _expectedBid = 155633980282;
103  dev::test::executeGasPricerTest("RPC_API_Test", 30.679, 15.0, "/BlockchainTests/Homestead/bcRPC_API_Test.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
104 }
105 
106 BOOST_AUTO_TEST_CASE(basicGasPricer_bcValidBlockTest)
107 {
108  dev::test::executeGasPricerTest("SimpleTx", 30.679, 15.0, "/BlockchainTests/bcValidBlockTest.json", TransactionPriority::Medium, 155632494086, 10);
109 }
110 
111 BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleTest_Frontier)
112 {
113  u256 _expectedAsk = 155632494086;
114  u256 _expectedBid = 1;
115  dev::test::executeGasPricerTest("twoUncle", 30.679, 15.0, "/BlockchainTests/bcUncleTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
116 }
117 
118 BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleTest_Homestead)
119 {
120  u256 _expectedAsk = 155633980282;
121  u256 _expectedBid = 1;
122  dev::test::executeGasPricerTest("twoUncle", 30.679, 15.0, "/BlockchainTests/Homestead/bcUncleTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
123 }
124 
125 BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleHeaderValiditiy_Frontier)
126 {
127  u256 _expectedAsk = 155632494086;
128  u256 _expectedBid = 1;
129  dev::test::executeGasPricerTest("correct", 30.679, 15.0, "/BlockchainTests/bcUncleHeaderValiditiy.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
130 }
131 
132 BOOST_AUTO_TEST_CASE(basicGasPricer_bcUncleHeaderValiditiy_Homestead)
133 {
134  u256 _expectedAsk = 155633980282;
135  u256 _expectedBid = 1;
136  dev::test::executeGasPricerTest("correct", 30.679, 15.0, "/BlockchainTests/Homestead/bcUncleHeaderValiditiy.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
137 }
138 
139 BOOST_AUTO_TEST_CASE(basicGasPricer_notxs_frontier)
140 {
141  u256 _expectedAsk = 155632494086;
142  u256 _expectedBid = 155632494086;
143  dev::test::executeGasPricerTest("notxs", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
144 }
145 
146 BOOST_AUTO_TEST_CASE(basicGasPricer_notxs_homestead)
147 {
148  u256 _expectedAsk = 155633980282;
149  u256 _expectedBid = 155633980282;
150  dev::test::executeGasPricerTest("notxs", 30.679, 15.0, "/BlockchainTests/Homestead/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::HomesteadTest);
151 }
152 
153 BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_LowestPrio)
154 {
155  u256 _expectedAsk = 15731282021;
156  u256 _expectedBid = 10000000000000;
157  dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Lowest, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
158 }
159 
160 BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_LowPrio)
161 {
162  u256 _expectedAsk = 15731282021;
163  u256 _expectedBid = 15734152261884;
164  dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Low, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
165 }
166 
167 BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_MediumPrio)
168 {
169  u256 _expectedAsk = 15731282021;
170  u256 _expectedBid = 20000000000000;
171  dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Medium, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
172 }
173 
174 BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_HighPrio)
175 {
176  u256 _expectedAsk = 15731282021;
177  u256 _expectedBid = 24265847738115;
178  dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::High, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
179 }
180 
181 BOOST_AUTO_TEST_CASE(basicGasPricer_highGasUsage_HighestPrio)
182 {
183  u256 _expectedAsk = 15731282021;
184  u256 _expectedBid = 30000000000000;
185  dev::test::executeGasPricerTest("highGasUsage", 30.679, 15.0, "/BlockchainTests/bcGasPricerTest.json", TransactionPriority::Highest, _expectedAsk, _expectedBid, eth::Network::FrontierTest);
186 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void executeGasPricerTest(string const &name, double _etherPrice, double _blockFee, string const &bcTestPath, TransactionPriority _txPrio, u256 _expectedAsk, u256 _expectedBid, eth::Network _sealEngineNetwork=eth::Network::TransitionnetTest)
Definition: GasPricer.cpp:38
Json::Value loadJsonFromFile(std::string const &_path)
Definition: Common.cpp:58
BOOST_AUTO_TEST_CASE(trivialGasPricer)
Definition: GasPricer.cpp:54
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
Implements the blockchain database.
Definition: BlockChain.h:105
Network
The network id.
Definition: GenesisInfo.h:34
eth::BlockChain const & bc() const
u256 ask(Block const &) const override
std::hash for asio::adress
Definition: Common.h:323
TransactionPriority
Definition: GasPricer.h:34
Active model of a block within the block chain.
Definition: Block.h:73
void update(BlockChain const &_bc) override
Config::Value_type Value
const char * name
Definition: rest.cpp:36
Should be used to load test blockchain from json file Loads the blockchain from json, creates temporary directory to store it, removes the directory on dealloc.
std::string getTestPath()
Definition: Common.cpp:35
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
u256 bid(TransactionPriority _p=TransactionPriority::Medium) const override
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
void setRefPrice(u256 _weiPerRef)
temporary directory implementation It creates temporary directory in the given path.
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17
void setRefBlockFees(u256 _refsPerBlock)