Fabcoin Core  0.16.2
P2P Digital Currency
ClientTest.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 <libethereum/ClientTest.h>
24 
25 using namespace std;
26 using namespace dev;
27 using namespace dev::eth;
28 using namespace p2p;
29 
31 {
32  return dynamic_cast<ClientTest&>(_c);
33 }
34 
36 {
37  return &dynamic_cast<ClientTest&>(*_c);
38 }
39 
40 ClientTest::ClientTest(
41  ChainParams const& _params,
42  int _networkID,
43  p2p::Host* _host,
44  std::shared_ptr<GasPricer> _gpForAdoption,
45  std::string const& _dbPath,
46  WithExisting _forceAction,
47  TransactionQueue::Limits const& _limits
48 ):
49  Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _forceAction, _limits)
50 {}
51 
52 void ClientTest::setChainParams(string const& _genesis)
53 {
54  ChainParams params;
55  try
56  {
57  params = params.loadConfig(_genesis);
58  if (params.sealEngineName != "NoProof")
59  BOOST_THROW_EXCEPTION(ChainParamsNotNoProof() << errinfo_comment("Provided configuration is not well formatted."));
60 
62  setAuthor(params.author); //for some reason author is not being set
63  }
64  catch (...)
65  {
66  BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment("Provided configuration is not well formatted."));
67  }
68 }
69 
70 bool ClientTest::addBlock(string const& _rlp)
71 {
72  if (auto h = m_host.lock())
73  h->noteNewBlocks();
74 
75  bytes rlpBytes = fromHex(_rlp, WhenError::Throw);
76  RLP blockRLP(rlpBytes);
77  return (m_bq.import(blockRLP.data(), true) == ImportResult::Success);
78 }
79 
80 void ClientTest::modifyTimestamp(u256 const& _timestamp)
81 {
82  Block block(chainParams().accountStartNonce);
84  block = m_preSeal;
85 
88  transactions = m_postSeal.pending();
89  block.resetCurrent(_timestamp);
90 
92  m_preSeal = block;
93 
94  auto lastHashes = bc().lastHashes();
95  for (auto const& t: transactions)
96  block.execute(lastHashes, t);
97 
99  m_working = block;
101  m_postSeal = block;
102 
104 }
105 
106 void ClientTest::mineBlocks(unsigned _count)
107 {
108  m_blocksToMine = _count;
109  startSealing();
110 }
111 
112 void ClientTest::onNewBlocks(h256s const& _blocks, h256Hash& io_changed)
113 {
114  Client::onNewBlocks(_blocks, io_changed);
115 
116  if(--m_blocksToMine <= 0)
117  stopSealing();
118 }
119 
121 {
122  auto h = m_host.lock();
123  if (!h)
124  return false;
125 
126  h->completeSync();
127  return true;
128 }
void stopSealing() override
Stop sealing.
Definition: Client.h:161
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Block m_working
The state of the client which we&#39;re sealing (i.e. it&#39;ll have all the rewards added), while we&#39;re actually working on it.
Definition: Client.h:304
void onPostStateChanged()
Called when the post state has changed (i.e.
Definition: Client.cpp:576
void modifyTimestamp(u256 const &_timestamp)
Definition: ClientTest.cpp:80
dev::eth::Block block(h256 const &_blockHash, PopulationStatistics *o_stats) const
Get the block.
Definition: Client.cpp:760
bytesConstRef data() const
The bare data of the RLP.
Definition: RLP.h:97
Block m_postSeal
The state of the client which we&#39;re sealing (i.e. it&#39;ll have all the rewards added).
Definition: Client.h:302
#define h(i)
Definition: sha.cpp:736
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
ImportResult import(bytesConstRef _block, bool _isOurs=false)
Import a block into the queue.
Definition: BlockQueue.cpp:175
bool addBlock(std::string const &_rlp)
Definition: ClientTest.cpp:70
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:121
void reopenChain(ChainParams const &_p, WithExisting _we=WithExisting::Trust)
Reloads the blockchain. Just for debug use.
Definition: Client.cpp:239
LastHashes lastHashes() const
Get the last N hashes for a given block. (N is determined by the LastHashes type.) ...
Definition: BlockChain.h:186
SharedMutex x_preSeal
Lock on m_preSeal.
Definition: Client.h:299
std::hash for asio::adress
Definition: Common.h:323
void mineBlocks(unsigned _count)
Definition: ClientTest.cpp:106
ChainParams const & chainParams() const
Get information on this chain.
Definition: Client.h:91
ChainParams loadConfig(std::string const &_json, h256 const &_stateRoot=h256()) const
load config/genesis
Definition: ChainParams.cpp:54
virtual Transactions transactions(h256 _blockHash) const override
Definition: ClientBase.cpp:437
BlockQueue m_bq
Maintains a list of incoming blocks not yet on the blockchain (to be imported).
Definition: Client.h:295
WithExisting
Definition: Common.h:310
ClientTest & asClientTest(Interface &_c)
Definition: ClientTest.cpp:30
Active model of a block within the block chain.
Definition: Block.h:73
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
BlockChain & bc() override
InterfaceStub methods.
Definition: Client.h:211
#define DEV_WRITE_GUARDED(MUTEX)
Definition: Guards.h:148
#define DEV_READ_GUARDED(MUTEX)
Definition: Guards.h:146
std::vector< byte > bytes
Definition: Common.h:75
virtual void onNewBlocks(h256s const &_blocks, h256Hash &io_changed) override
Called on chain changes.
Definition: ClientTest.cpp:112
Main API hub for interfacing with Ethereum.
Definition: Client.h:75
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
void resetCurrent(u256 const &_timestamp=u256(utcTime()))
Sets m_currentBlock to a clean state, (i.e.
Definition: Block.cpp:113
Transactions const & pending() const
Get the list of pending transactions.
Definition: Block.h:188
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
virtual void onNewBlocks(h256s const &_blocks, h256Hash &io_changed)
Called on chain changes.
Definition: Client.cpp:484
LastHashes lastHashes(u256 _currentBlockNumber)
Definition: TestHelper.cpp:544
std::string sealEngineName
The chain sealer name: e.g. Ethash, NoProof, BasicAuthority.
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:78
Block m_preSeal
The present state of the client.
Definition: Client.h:300
std::weak_ptr< EthereumHost > m_host
Our Ethereum Host. Don&#39;t do anything if we can&#39;t lock.
Definition: Client.h:311
ExecutionResult execute(LastHashes const &_lh, Transaction const &_t, Permanence _p=Permanence::Committed, OnOpFunc const &_onOp=OnOpFunc())
Execute a given transaction.
Definition: Block.cpp:649
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:349
unsigned m_blocksToMine
Definition: ClientTest.h:57
void startSealing() override
Start sealing.
Definition: Client.cpp:583
SharedMutex x_working
Lock on m_working.
Definition: Client.h:303
std::vector< h256 > h256s
Definition: FixedHash.h:345
SharedMutex x_postSeal
Lock on m_postSeal.
Definition: Client.h:301
virtual void setAuthor(Address const &_us) override
Set the block author address.
Definition: Client.h:145
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void setChainParams(std::string const &_genesis)
Definition: ClientTest.cpp:52