Browse Source

Allow provision of any genesis block.

Listen to ~/.ethereum/genesis.json.
cl-refactor
Gav Wood 9 years ago
parent
commit
8268185169
  1. 3
      alethzero/MainWin.cpp
  2. 8
      eth/main.cpp
  3. 2
      libethcore/Params.cpp
  4. 40
      libethereum/CanonBlockChain.cpp
  5. 7
      libethereum/CanonBlockChain.h
  6. 34
      libethereum/GenesisInfo.cpp

3
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<Ethash>::setGenesis(contentsString(getDataDir() + "/genesis.json"));
cerr << "State root: " << CanonBlockChain<Ethash>::genesis().stateRoot() << endl;
auto block = CanonBlockChain<Ethash>::createGenesisBlock();
cerr << "Block Hash: " << CanonBlockChain<Ethash>::genesis().hash() << endl;

8
eth/main.cpp

@ -1148,6 +1148,10 @@ int main(int argc, char** argv)
beneficiary = config[1].toHash<Address>();
}
// do this here so that --genesis-json can actually override it.
if (!contents(getDataDir() + "/genesis.json").empty())
CanonBlockChain<Ethash>::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<Ethash>::setGenesisNonce(Nonce(argv[++i]));
CanonBlockChain<Ethash>::setGenesis(contentsString(argv[++i]));
}
catch (...)
{

2
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;

40
libethereum/CanonBlockChain.cpp

@ -63,8 +63,35 @@ bytes CanonBlockChain<Ethash>::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<u256>(fromHex(genesis["difficulty"].get_str()));
u256 gasLimit = fromBigEndian<u256>(fromHex(genesis["gasLimit"].get_str()));
u256 timestamp = fromBigEndian<u256>(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<Address, Account> CanonBlockChain<Ethash>::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<Address, Account> CanonBlockChain<Ethash>::createGenesisState()
return s_ret;
}
void CanonBlockChain<Ethash>::setGenesisState(std::string const& _json)
void CanonBlockChain<Ethash>::setGenesis(std::string const& _json)
{
WriteGuard l(x_genesis);
s_genesisStateJSON = _json;
s_genesis.reset();
}
void CanonBlockChain<Ethash>::setGenesisNonce(Nonce const& _n)
{
WriteGuard l(x_genesis);
s_nonce = _n;
s_genesis.reset();
}
Ethash::BlockHeader const& CanonBlockChain<Ethash>::genesis()
{
UpgradableGuard l(x_genesis);

7
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<Address, Account> 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,

34
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";

Loading…
Cancel
Save