Fabcoin Core  0.16.2
P2P Digital Currency
BlockChainHelper.h
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 */
20 #pragma once
21 #include "JsonSpiritHeaders.h"
22 #include <libethereum/BlockChain.h>
26 #include <libethashseal/Ethash.h>
27 
28 using namespace std;
29 using namespace json_spirit;
30 using namespace dev;
31 using namespace dev::eth;
32 
33 
34 namespace dev { namespace test {
35 
36 struct BlockStateUndefined : virtual Exception {};
37 class TestTransaction;
38 class TestBlock;
39 class TestBlockChain;
40 
42 {
43 public:
45  TestTransaction(Transaction const& _tr) : m_transaction(_tr) {}
46  Transaction const& transaction() const { return m_transaction; }
47  json_spirit::mObject& jsonObject() { return m_jsonTransaction; }
48  static TestTransaction defaultTransaction(u256 const& _nonce = 1, u256 const& _gasPrice = 1, u256 const& _gasLimit = 50000, bytes const& _data = bytes());
49 
50 private:
53 };
54 
55 class TestBlock
56 {
57 public:
58  TestBlock();
59  TestBlock(std::string const& _blockRlp);
60  TestBlock(mObject const& _blockObj, mObject const& _stateObj);
61  TestBlock(TestBlock const& _original);
62  TestBlock& operator=(TestBlock const& _original);
63 
64  void addTransaction(TestTransaction const& _tr);
65  void addUncle(TestBlock const& _uncle);
66  void setUncles(vector<TestBlock> const& _uncles);
67  void setPremine(std::string const& _parameter) { m_premineUpdate[_parameter] = true; }
68  void noteDirty() { m_dirty = true; }
69  void mine(TestBlockChain const& _bc);
70  void updateNonce(TestBlockChain const& _bc);
71  void verify(TestBlockChain const& _bc) const;
72 
73  void setBlockHeader(BlockHeader const& _header);
74  void setState(State const& _state);
75  void clearState();
76 
77  BlockHeader const& premineHeader() { return m_premineHeader; } //should return fields according to m_premineUpdate. this is needed to check that premine chanes was not lost during mining .
78  dev::bytes const& bytes() const { return m_bytes; }
79  bytesConstRef receipts() const { return bytesConstRef(&m_receipts.out()[0], m_receipts.out().size()); }
80  AccountMap const& accountMap() const { return m_accountMap; }
81  State const& state() const { if (m_state.get() == 0) BOOST_THROW_EXCEPTION(BlockStateUndefined() << errinfo_comment("Block State is Nulled")); return *m_state.get(); }
82  BlockHeader const& blockHeader() const { return m_blockHeader;}
83  TransactionQueue const& transactionQueue() const { return m_transactionQueue; }
84  TransactionQueue & transactionQueue() { return m_transactionQueue; }
85  vector<TestTransaction> const& testTransactions() const { return m_testTransactions; }
86  vector<TestBlock> const& uncles() const { return m_uncles; }
87  Address const& beneficiary() const { return m_blockHeader.author(); }
88 
89 private:
90  BlockHeader constructBlock(mObject const& _o, h256 const& _stateRoot);
91  dev::bytes createBlockRLPFromFields(mObject const& _tObj, h256 const& _stateRoot = h256{});
92  void recalcBlockHeaderBytes();
93  void copyStateFrom(State const& _state);
94  void populateFrom(TestBlock const& _original);
95  void premineUpdate(BlockHeader& info);
96 
97  bool m_dirty;
99  vector<TestBlock> m_uncles;
100  std::unique_ptr<State> m_state;
104  std::unique_ptr<TransientDirectory> m_tempDirState;
105  vector<TestTransaction> m_testTransactions;
106  std::map<std::string, bool> m_premineUpdate; //Test Header alterate options. TODO: Do we really need this?
108  AccountMap m_accountMap; //Needed for genesis state
110 };
111 
113 {
114 public:
115  TestBlockChain(): TestBlockChain(defaultGenesisBlock()) {}
116  TestBlockChain(TestBlock const& _genesisBlock);
117 
118  void reset(TestBlock const& _genesisBlock);
119  bool addBlock(TestBlock const& _block);
120  vector<TestBlock> syncUncles(vector<TestBlock> const& _uncles);
121  TestBlock const& topBlock() { return m_lastBlock; }
122  BlockChain const& interface() const { return *m_blockChain;}
123  BlockChain& interfaceUnsafe() const { return *m_blockChain;}
124  TestBlock const& testGenesis() const { return m_genesisBlock; }
125 
126  static json_spirit::mObject defaultGenesisBlockJson();
127  static TestBlock defaultGenesisBlock(u256 const& _gasLimit = DefaultBlockGasLimit);
128  static AccountMap defaultAccountMap();
130 private:
131 
132  std::unique_ptr<BlockChain> m_blockChain;
135  std::unique_ptr<TransientDirectory> m_tempDirBlockchain;
136 };
137 
138 }}
vector< TestTransaction > m_testTransactions
BlockHeader const & premineHeader()
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
TestBlock const & topBlock()
std::map< std::string, bool > m_premineUpdate
A queue of Transactions, each stored as RLP.
std::unordered_map< Address, Account > AccountMap
Definition: Account.h:239
Implements the blockchain database.
Definition: BlockChain.h:105
Network
The network id.
Definition: GenesisInfo.h:34
TransactionQueue & transactionQueue()
Encapsulation of a block header.
Definition: BlockHeader.h:95
std::unique_ptr< BlockChain > m_blockChain
BlockChain const & interface() const
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:255
std::hash for asio::adress
Definition: Common.h:323
BlockHeader const & blockHeader() const
static eth::Network s_sealEngineNetwork
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
TransactionQueue m_transactionQueue
TestBlock const & testGenesis() const
Base class for all exceptions.
Definition: Exceptions.h:39
vector< TestTransaction > const & testTransactions() const
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
bytesConstRef receipts() const
A queue of blocks.
Definition: BlockQueue.h:225
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
mConfig::Object_type mObject
void setPremine(std::string const &_parameter)
std::unique_ptr< TransientDirectory > m_tempDirBlockchain
TransactionQueue const & transactionQueue() const
State const & state() const
json_spirit::mObject & jsonObject()
vector< TestBlock > const & uncles() const
std::unique_ptr< State > m_state
BlockChain & interfaceUnsafe() const
dev::bytes const & bytes() const
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:78
json_spirit::mObject m_jsonTransaction
std::unique_ptr< TransientDirectory > m_tempDirState
Transaction const & transaction() const
Address const & beneficiary() const
Class for writing to an RLP bytestream.
Definition: RLP.h:383
void mine(Client &c, int numBlocks)
Definition: TestHelper.cpp:39
TestTransaction(Transaction const &_tr)
AccountMap const & accountMap() const
vector< TestBlock > m_uncles