Fabcoin Core  0.16.2
P2P Digital Currency
BasicGasPricer.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 */
22 #pragma warning(push)
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wunused-parameter"
25 #include <boost/math/distributions/normal.hpp>
26 #pragma warning(pop)
27 #pragma GCC diagnostic pop
28 #include "BasicGasPricer.h"
29 #include "BlockChain.h"
30 using namespace std;
31 using namespace dev;
32 using namespace dev::eth;
33 
34 void BasicGasPricer::update(BlockChain const& _bc)
35 {
36  unsigned c = 0;
37  h256 p = _bc.currentHash();
38  m_gasPerBlock = _bc.info(p).gasLimit();
39 
40  map<u256, u256> dist;
41  u256 total = 0;
42 
43  // make gasPrice versus gasUsed distribution for the last 1000 blocks
44  while (c < 1000 && p)
45  {
46  BlockHeader bi = _bc.info(p);
47  if (bi.transactionsRoot() != EmptyTrie)
48  {
49  auto bb = _bc.block(p);
50  RLP r(bb);
51  BlockReceipts brs(_bc.receipts(bi.hash()));
52  size_t i = 0;
53  for (auto const& tr: r[1])
54  {
55  Transaction tx(tr.data(), CheckTransaction::None);
56  u256 gu = brs.receipts[i].gasUsed();
57  dist[tx.gasPrice()] += gu;
58  total += gu;
59  i++;
60  }
61  }
62  p = bi.parentHash();
63  ++c;
64  }
65 
66  // fill m_octiles with weighted gasPrices
67  if (total > 0)
68  {
69  m_octiles[0] = dist.begin()->first;
70 
71  // calc mean
72  u256 mean = 0;
73  for (auto const& i: dist)
74  mean += i.first * i.second;
75  mean /= total;
76 
77  // calc standard deviation
78  u256 sdSquared = 0;
79  for (auto const& i: dist)
80  sdSquared += i.second * (i.first - mean) * (i.first - mean);
81  sdSquared /= total;
82 
83  if (sdSquared)
84  {
85  long double sd = sqrt(sdSquared.convert_to<long double>());
86  long double normalizedSd = sd / mean.convert_to<long double>();
87 
88  // calc octiles normalized to gaussian distribution
89  boost::math::normal gauss(1.0, (normalizedSd > 0.01) ? normalizedSd : 0.01);
90  for (size_t i = 1; i < 8; i++)
91  m_octiles[i] = u256(mean.convert_to<long double>() * boost::math::quantile(gauss, i / 8.0));
92  m_octiles[8] = dist.rbegin()->first;
93  }
94  else
95  {
96  for (size_t i = 0; i < 9; i++)
97  m_octiles[i] = (i + 1) * mean / 5;
98  }
99  }
100 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
BlockHeader info(h256 const &_hash) const
Get the partial-header of a block (or the most recent mined if none given). Thread-safe.
Definition: BlockChain.h:144
Implements the blockchain database.
Definition: BlockChain.h:105
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:64
Encapsulation of a block header.
Definition: BlockHeader.h:95
h256 const & transactionsRoot() const
Definition: BlockHeader.h:159
BlockReceipts receipts(h256 const &_hash) const
Get the transactions&#39; receipts of a block (or the most recent mined if none given).
Definition: BlockChain.h:165
TransactionReceipts receipts
Definition: BlockDetails.h:85
#define c(i)
h256 const EmptyTrie
Definition: OverlayDB.cpp:33
std::hash for asio::adress
Definition: Common.h:323
h256 const & parentHash() const
Definition: BlockHeader.h:154
h256 currentHash() const
Get a given block (RLP format). Thread-safe.
Definition: BlockChain.h:229
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
u256 const & gasLimit() const
Definition: BlockHeader.h:163
bytes block(h256 const &_hash) const
Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe.
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64