From 82681851692f920a0a4405f6d5229809acd4fdc9 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 23 Jul 2015 23:05:36 +0200 Subject: [PATCH] Allow provision of any genesis block. Listen to ~/.ethereum/genesis.json. --- alethzero/MainWin.cpp | 3 +++ eth/main.cpp | 8 +++++-- libethcore/Params.cpp | 2 +- libethereum/CanonBlockChain.cpp | 40 ++++++++++++++++++++++++--------- libethereum/CanonBlockChain.h | 7 +----- libethereum/GenesisInfo.cpp | 34 ++++++++++++++++++---------- 6 files changed, 63 insertions(+), 31 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index cdde0f147..12c36d586 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -189,6 +189,9 @@ Main::Main(QWidget *parent) : #endif m_servers.append(QString::fromStdString(Host::pocHost() + ":30303")); + if (!dev::contents(getDataDir() + "/genesis.json").empty()) + CanonBlockChain::setGenesis(contentsString(getDataDir() + "/genesis.json")); + cerr << "State root: " << CanonBlockChain::genesis().stateRoot() << endl; auto block = CanonBlockChain::createGenesisBlock(); cerr << "Block Hash: " << CanonBlockChain::genesis().hash() << endl; diff --git a/eth/main.cpp b/eth/main.cpp index 45902c96c..af3085f82 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -1148,6 +1148,10 @@ int main(int argc, char** argv) beneficiary = config[1].toHash
(); } + // do this here so that --genesis-json can actually override it. + if (!contents(getDataDir() + "/genesis.json").empty()) + CanonBlockChain::setGenesis(contentsString(getDataDir() + "/genesis.json")); + MinerCLI m(MinerCLI::OperationMode::None); for (int i = 1; i < argc; ++i) @@ -1306,11 +1310,11 @@ int main(int argc, char** argv) } else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) dbPath = argv[++i]; - else if (arg == "--genesis-nonce" && i + 1 < argc) + else if (arg == "--genesis-json" && i + 1 < argc) { try { - CanonBlockChain::setGenesisNonce(Nonce(argv[++i])); + CanonBlockChain::setGenesis(contentsString(argv[++i])); } catch (...) { diff --git a/libethcore/Params.cpp b/libethcore/Params.cpp index 0fea39b30..0031feeab 100644 --- a/libethcore/Params.cpp +++ b/libethcore/Params.cpp @@ -30,7 +30,7 @@ namespace eth //--- BEGIN: AUTOGENERATED FROM github.com/ethereum/common/params.json u256 const c_genesisDifficulty = 131072; -u256 const c_maximumExtraDataSize = 1024; +u256 const c_maximumExtraDataSize = 32; u256 const c_genesisGasLimit = c_network == Network::Turbo ? 100000000 : 3141592; u256 const c_minGasLimit = c_network == Network::Turbo ? 100000000 : 125000; u256 const c_gasLimitBoundDivisor = 1024; diff --git a/libethereum/CanonBlockChain.cpp b/libethereum/CanonBlockChain.cpp index eee4b98b7..63d42bccd 100644 --- a/libethereum/CanonBlockChain.cpp +++ b/libethereum/CanonBlockChain.cpp @@ -63,8 +63,35 @@ bytes CanonBlockChain::createGenesisBlock() stateRoot = state.root(); } + js::mValue val; + json_spirit::read_string(s_genesisStateJSON.empty() ? c_genesisInfo : s_genesisStateJSON, val); + js::mObject genesis = val.get_obj(); + + h256 mixHash(genesis["mixhash"].get_str()); + h256 parentHash(genesis["parentHash"].get_str()); + h160 beneficiary(genesis["coinbase"].get_str()); + u256 difficulty = fromBigEndian(fromHex(genesis["difficulty"].get_str())); + u256 gasLimit = fromBigEndian(fromHex(genesis["gasLimit"].get_str())); + u256 timestamp = fromBigEndian(fromHex(genesis["timestamp"].get_str())); + bytes extraData = fromHex(genesis["extraData"].get_str()); + h64 nonce(genesis["nonce"].get_str()); + block.appendList(15) - << h256() << EmptyListSHA3 << h160() << stateRoot << EmptyTrie << EmptyTrie << LogBloom() << c_genesisDifficulty << 0 << c_genesisGasLimit << 0 << (unsigned)0 << string() << h256() << s_nonce; + << parentHash + << EmptyListSHA3 // sha3(uncles) + << beneficiary + << stateRoot + << EmptyTrie // transactions + << EmptyTrie // receipts + << LogBloom() + << difficulty + << 0 // number + << gasLimit + << 0 // gasUsed + << timestamp + << extraData + << mixHash + << nonce; block.appendRaw(RLPEmptyList); block.appendRaw(RLPEmptyList); return block.out(); @@ -78,7 +105,7 @@ unordered_map CanonBlockChain::createGenesisState() { js::mValue val; json_spirit::read_string(s_genesisStateJSON.empty() ? c_genesisInfo : s_genesisStateJSON, val); - for (auto account: val.get_obj()) + for (auto account: val.get_obj()["alloc"].get_obj()) { u256 balance; if (account.second.get_obj().count("wei")) @@ -97,20 +124,13 @@ unordered_map CanonBlockChain::createGenesisState() return s_ret; } -void CanonBlockChain::setGenesisState(std::string const& _json) +void CanonBlockChain::setGenesis(std::string const& _json) { WriteGuard l(x_genesis); s_genesisStateJSON = _json; s_genesis.reset(); } -void CanonBlockChain::setGenesisNonce(Nonce const& _n) -{ - WriteGuard l(x_genesis); - s_nonce = _n; - s_genesis.reset(); -} - Ethash::BlockHeader const& CanonBlockChain::genesis() { UpgradableGuard l(x_genesis); diff --git a/libethereum/CanonBlockChain.h b/libethereum/CanonBlockChain.h index d79926703..66518d4c1 100644 --- a/libethereum/CanonBlockChain.h +++ b/libethereum/CanonBlockChain.h @@ -89,16 +89,11 @@ public: /// @note This is slow as it's constructed anew each call. Consider genesis() instead. static std::unordered_map createGenesisState(); - /// Alter the value of the genesis block's nonce. - /// @warning Unless you're very careful, make sure you call this right at the start of the - /// program, before anything has had the chance to use this class at all. - static void setGenesisNonce(Nonce const& _n); - /// Alter all the genesis block's state by giving a JSON string with account details. /// @TODO implement. /// @warning Unless you're very careful, make sure you call this right at the start of the /// program, before anything has had the chance to use this class at all. - static void setGenesisState(std::string const&); + static void setGenesis(std::string const& _genesisInfoJSON); // TODO: setGenesisTimestamp, ...ExtraData, ...Difficulty, ...GasLimit, diff --git a/libethereum/GenesisInfo.cpp b/libethereum/GenesisInfo.cpp index 4e6a48284..c1e0388e1 100644 --- a/libethereum/GenesisInfo.cpp +++ b/libethereum/GenesisInfo.cpp @@ -24,17 +24,27 @@ std::string const dev::eth::c_genesisInfo = R"ETHEREUM( { - "0000000000000000000000000000000000000001": { "wei": "1" }, - "0000000000000000000000000000000000000002": { "wei": "1" }, - "0000000000000000000000000000000000000003": { "wei": "1" }, - "0000000000000000000000000000000000000004": { "wei": "1" }, - "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "e6716f9544a56c530d868e4bfbacb172315bdead": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "b9c015918bdaba24b4ff057a92a3873d6eb201be": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "2ef47100e0787b915105fd5e3f4ff6752079d5cb": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "6c386a4b26f73c802f34673f7248bb118f97424a": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, - "e4157b34ea9615cfbde6b4fda419828124b70c78": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "nonce": "0x000000000000002a", + "difficulty": "0x20000", + "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "coinbase": "0x0000000000000000000000000000000000000000", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x", + "gasLimit": "0x2FEFD8", + "alloc": { + "0000000000000000000000000000000000000001": { "wei": "1" }, + "0000000000000000000000000000000000000002": { "wei": "1" }, + "0000000000000000000000000000000000000003": { "wei": "1" }, + "0000000000000000000000000000000000000004": { "wei": "1" }, + "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "e6716f9544a56c530d868e4bfbacb172315bdead": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "b9c015918bdaba24b4ff057a92a3873d6eb201be": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "2ef47100e0787b915105fd5e3f4ff6752079d5cb": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "cd2a3d9f938e13cd947ec05abc7fe734df8dd826": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "6c386a4b26f73c802f34673f7248bb118f97424a": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, + "e4157b34ea9615cfbde6b4fda419828124b70c78": { "wei": "1606938044258990275541962092341162602522202993782792835301376" } + } } )ETHEREUM";