BlockChainLoader.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file BlockChainLoader.cpp
  15. * @author Marek Kotewicz <marek@ethdev.com>
  16. * @date 2015
  17. */
  18. #include <json/writer.h>
  19. #include <libethashseal/Ethash.h>
  20. #include <libethereum/BlockChain.h>
  21. #include "BlockChainLoader.h"
  22. #include "Common.h"
  23. using namespace std;
  24. using namespace dev;
  25. using namespace dev::test;
  26. using namespace dev::eth;
  27. BlockChainLoader::BlockChainLoader(Json::Value const& _json, eth::Network _sealEngineNetwork):
  28. m_block(Block::Null)
  29. {
  30. // load genesisBlock
  31. bytes genesisBlock = fromHex(_json["genesisRLP"].asString());
  32. Json::FastWriter a;
  33. m_bc.reset(new BlockChain(ChainParams(genesisInfo(_sealEngineNetwork), genesisBlock, jsonToAccountMap(a.write(_json["pre"]))), m_dir.path(), WithExisting::Kill));
  34. // load pre state
  35. m_block = m_bc->genesisBlock(State::openDB(m_dir.path(), m_bc->genesisHash(), WithExisting::Kill));
  36. assert(m_block.rootHash() == m_bc->info().stateRoot());
  37. // load blocks
  38. for (auto const& block: _json["blocks"])
  39. {
  40. bytes rlp = fromHex(block["rlp"].asString());
  41. m_bc->import(rlp, state().db());
  42. }
  43. // sync state
  44. m_block.sync(*m_bc);
  45. }