ChainParams.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 ChainParams.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #include "ChainParams.h"
  19. #include <json_spirit/JsonSpiritHeaders.h>
  20. #include <libdevcore/Log.h>
  21. #include <libdevcore/TrieDB.h>
  22. #include <libethcore/SealEngine.h>
  23. #include <libethcore/BlockHeader.h>
  24. #include <libethcore/Precompiled.h>
  25. #include "GenesisInfo.h"
  26. #include "State.h"
  27. #include "Account.h"
  28. using namespace std;
  29. using namespace dev;
  30. using namespace eth;
  31. namespace js = json_spirit;
  32. ChainParams::ChainParams()
  33. {
  34. for (unsigned i = 1; i <= 4; ++i)
  35. genesisState[Address(i)] = Account(0, 1);
  36. // Setup default precompiled contracts as equal to genesis of Frontier.
  37. precompiled.insert(make_pair(Address(1), PrecompiledContract(3000, 0, PrecompiledRegistrar::executor("ecrecover"))));
  38. precompiled.insert(make_pair(Address(2), PrecompiledContract(60, 12, PrecompiledRegistrar::executor("sha256"))));
  39. precompiled.insert(make_pair(Address(3), PrecompiledContract(600, 120, PrecompiledRegistrar::executor("ripemd160"))));
  40. precompiled.insert(make_pair(Address(4), PrecompiledContract(15, 3, PrecompiledRegistrar::executor("identity"))));
  41. }
  42. ChainParams::ChainParams(string const& _json, h256 const& _stateRoot)
  43. {
  44. *this = loadConfig(_json, _stateRoot);
  45. }
  46. ChainParams ChainParams::loadConfig(string const& _json, h256 const& _stateRoot) const
  47. {
  48. ChainParams cp(*this);
  49. js::mValue val;
  50. json_spirit::read_string(_json, val);
  51. js::mObject obj = val.get_obj();
  52. cp.sealEngineName = obj["sealEngine"].get_str();
  53. // params
  54. js::mObject params = obj["params"].get_obj();
  55. cp.accountStartNonce = u256(fromBigEndian<u256>(fromHex(params["accountStartNonce"].get_str())));
  56. cp.maximumExtraDataSize = u256(fromBigEndian<u256>(fromHex(params["maximumExtraDataSize"].get_str())));
  57. cp.tieBreakingGas = params.count("tieBreakingGas") ? params["tieBreakingGas"].get_bool() : true;
  58. cp.blockReward = u256(fromBigEndian<u256>(fromHex(params["blockReward"].get_str())));
  59. for (auto i: params)
  60. if (i.first != "accountStartNonce" && i.first != "maximumExtraDataSize" && i.first != "blockReward" && i.first != "tieBreakingGas")
  61. cp.otherParams[i.first] = i.second.get_str();
  62. // genesis
  63. string genesisStr = json_spirit::write_string(obj["genesis"], false);
  64. cp = cp.loadGenesis(genesisStr, _stateRoot);
  65. // genesis state
  66. string genesisStateStr = json_spirit::write_string(obj["accounts"], false);
  67. cp = cp.loadGenesisState(genesisStateStr, _stateRoot);
  68. return cp;
  69. }
  70. ChainParams ChainParams::loadGenesisState(string const& _json, h256 const& _stateRoot) const
  71. {
  72. ChainParams cp(*this);
  73. cp.genesisState = jsonToAccountMap(_json, cp.accountStartNonce, nullptr, &cp.precompiled);
  74. cp.stateRoot = _stateRoot ? _stateRoot : cp.calculateStateRoot(true);
  75. return cp;
  76. }
  77. ChainParams ChainParams::loadGenesis(string const& _json, h256 const& _stateRoot) const
  78. {
  79. ChainParams cp(*this);
  80. js::mValue val;
  81. json_spirit::read_string(_json, val);
  82. js::mObject genesis = val.get_obj();
  83. cp.parentHash = h256(genesis["parentHash"].get_str());
  84. cp.author = genesis.count("coinbase") ? h160(genesis["coinbase"].get_str()) : h160(genesis["author"].get_str());
  85. cp.difficulty = genesis.count("difficulty") ? u256(fromBigEndian<u256>(fromHex(genesis["difficulty"].get_str()))) : 0;
  86. cp.gasLimit = u256(fromBigEndian<u256>(fromHex(genesis["gasLimit"].get_str())));
  87. cp.gasUsed = genesis.count("gasUsed") ? u256(fromBigEndian<u256>(fromHex(genesis["gasUsed"].get_str()))) : 0;
  88. cp.timestamp = u256(fromBigEndian<u256>(fromHex(genesis["timestamp"].get_str())));
  89. cp.extraData = bytes(fromHex(genesis["extraData"].get_str()));
  90. // magic code for handling ethash stuff:
  91. if ((genesis.count("mixhash") || genesis.count("mixHash")) && genesis.count("nonce"))
  92. {
  93. h256 mixHash(genesis[genesis.count("mixhash") ? "mixhash" : "mixHash"].get_str());
  94. h64 nonce(genesis["nonce"].get_str());
  95. cp.sealFields = 2;
  96. cp.sealRLP = rlp(mixHash) + rlp(nonce);
  97. }
  98. cp.stateRoot = _stateRoot ? _stateRoot : cp.calculateStateRoot();
  99. return cp;
  100. }
  101. SealEngineFace* ChainParams::createSealEngine()
  102. {
  103. SealEngineFace* ret = SealEngineRegistrar::create(sealEngineName);
  104. assert(ret && "Seal engine not found");
  105. if (!ret)
  106. return nullptr;
  107. ret->setChainParams(*this);
  108. if (sealRLP.empty())
  109. {
  110. sealFields = ret->sealFields();
  111. sealRLP = ret->sealRLP();
  112. }
  113. return ret;
  114. }
  115. void ChainParams::populateFromGenesis(bytes const& _genesisRLP, AccountMap const& _state)
  116. {
  117. BlockHeader bi(_genesisRLP, RLP(&_genesisRLP)[0].isList() ? BlockData : HeaderData);
  118. parentHash = bi.parentHash();
  119. author = bi.author();
  120. difficulty = bi.difficulty();
  121. gasLimit = bi.gasLimit();
  122. gasUsed = bi.gasUsed();
  123. timestamp = bi.timestamp();
  124. extraData = bi.extraData();
  125. genesisState = _state;
  126. RLP r(_genesisRLP);
  127. sealFields = r[0].itemCount() - BlockHeader::BasicFields;
  128. sealRLP.clear();
  129. for (unsigned i = BlockHeader::BasicFields; i < r[0].itemCount(); ++i)
  130. sealRLP += r[0][i].data();
  131. calculateStateRoot(true);
  132. auto b = genesisBlock();
  133. if (b != _genesisRLP)
  134. {
  135. cdebug << "Block passed:" << bi.hash() << bi.hash(WithoutSeal);
  136. cdebug << "Genesis now:" << BlockHeader::headerHashFromBlock(b);
  137. cdebug << RLP(b);
  138. cdebug << RLP(_genesisRLP);
  139. throw 0;
  140. }
  141. }
  142. h256 ChainParams::calculateStateRoot(bool _force) const
  143. {
  144. MemoryDB db;
  145. SecureTrieDB<Address, MemoryDB> state(&db);
  146. state.init();
  147. if (!stateRoot || _force)
  148. {
  149. // TODO: use hash256
  150. //stateRoot = hash256(toBytesMap(gs));
  151. dev::eth::commit(genesisState, state);
  152. stateRoot = state.root();
  153. }
  154. return stateRoot;
  155. }
  156. bytes ChainParams::genesisBlock() const
  157. {
  158. RLPStream block(3);
  159. calculateStateRoot();
  160. block.appendList(BlockHeader::BasicFields + sealFields)
  161. << parentHash
  162. << EmptyListSHA3 // sha3(uncles)
  163. << author
  164. << stateRoot
  165. << EmptyTrie // transactions
  166. << EmptyTrie // receipts
  167. << LogBloom()
  168. << difficulty
  169. << 0 // number
  170. << gasLimit
  171. << gasUsed // gasUsed
  172. << timestamp
  173. << extraData;
  174. block.appendRaw(sealRLP, sealFields);
  175. block.appendRaw(RLPEmptyList);
  176. block.appendRaw(RLPEmptyList);
  177. return block.out();
  178. }