Fabcoin Core  0.16.2
P2P Digital Currency
BlockChainLoader.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 #include <json/writer.h>
23 #include <libethashseal/Ethash.h>
24 #include <libethereum/BlockChain.h>
25 #include "BlockChainLoader.h"
26 #include "Common.h"
27 using namespace std;
28 using namespace dev;
29 using namespace dev::test;
30 using namespace dev::eth;
31 
32 BlockChainLoader::BlockChainLoader(Json::Value const& _json, eth::Network _sealEngineNetwork):
33  m_block(Block::Null)
34 {
35  // load genesisBlock
36  bytes genesisBlock = fromHex(_json["genesisRLP"].asString());
37 
38  Json::FastWriter a;
39  m_bc.reset(new BlockChain(ChainParams(genesisInfo(_sealEngineNetwork), genesisBlock, jsonToAccountMap(a.write(_json["pre"]))), m_dir.path(), WithExisting::Kill));
40 
41  // load pre state
42  m_block = m_bc->genesisBlock(State::openDB(m_dir.path(), m_bc->genesisHash(), WithExisting::Kill));
43 
44  assert(m_block.rootHash() == m_bc->info().stateRoot());
45 
46  // load blocks
47  for (auto const& block: _json["blocks"])
48  {
49  bytes rlp = fromHex(block["rlp"].asString());
50  m_bc->import(rlp, state().db());
51  }
52 
53  // sync state
54  m_block.sync(*m_bc);
55 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::pair< TransactionReceipts, bool > sync(BlockChain const &_bc, TransactionQueue &_tq, GasPricer const &_gp, unsigned _msTimeout=100)
Sync our transactions, killing those from the queue that we have and assimilating those that we don&#39;t...
Definition: Block.cpp:301
Implements the blockchain database.
Definition: BlockChain.h:105
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
Network
The network id.
Definition: GenesisInfo.h:34
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
#define a(i)
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
Config::Value_type Value
eth::State const & state() const
std::vector< byte > bytes
Definition: Common.h:75
eth::Block const & block() const
std::string const & genesisInfo(Network _n)
Definition: GenesisInfo.cpp:37
std::unique_ptr< eth::BlockChain > m_bc
std::string const & path() const
AccountMap jsonToAccountMap(std::string const &_json, u256 const &_defaultNonce=0, AccountMaskMap *o_mask=nullptr, PrecompiledContractMap *o_precompiled=nullptr)
Definition: Account.cpp:50
h256 rootHash() const
The hash of the root of our state tree.
Definition: Block.h:169
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Definition: CommonData.h:79