Browse Source

Accept alternative nonce.

cl-refactor
Gav Wood 10 years ago
parent
commit
46666c8450
  1. 13
      eth/main.cpp
  2. 26
      libethereum/CanonBlockChain.cpp
  3. 28
      libethereum/CanonBlockChain.h
  4. 7
      libethereum/Client.cpp

13
eth/main.cpp

@ -116,6 +116,7 @@ void help()
#endif #endif
<< " -K,--kill First kill the blockchain." << endl << " -K,--kill First kill the blockchain." << endl
<< " -R,--rebuild Rebuild the blockchain from the existing database." << endl << " -R,--rebuild Rebuild the blockchain from the existing database." << endl
<< " --genesis-nonce <nonce> Set the Genesis Nonce to the given hex nonce." << endl
<< " -s,--import-secret <secret> Import a secret key into the key store and use as the default." << endl << " -s,--import-secret <secret> Import a secret key into the key store and use as the default." << endl
<< " -S,--import-session-secret <secret> Import a secret key into the key store and use as the default for this session only." << endl << " -S,--import-session-secret <secret> Import a secret key into the key store and use as the default for this session only." << endl
<< " --sign-key <address> Sign all transactions with the key of the given address." << endl << " --sign-key <address> Sign all transactions with the key of the given address." << endl
@ -468,6 +469,18 @@ int main(int argc, char** argv)
} }
else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc)
dbPath = argv[++i]; dbPath = argv[++i];
else if (arg == "--genesis-nonce" && i + 1 < argc)
{
try
{
CanonBlockChain::setGenesisNonce(Nonce(argv[++i]));
}
catch (...)
{
cerr << "Bad " << arg << " option: " << argv[i] << endl;
return -1;
}
}
/* else if ((arg == "-B" || arg == "--block-fees") && i + 1 < argc) /* else if ((arg == "-B" || arg == "--block-fees") && i + 1 < argc)
{ {
try try

26
libethereum/CanonBlockChain.cpp

@ -72,6 +72,7 @@ std::unordered_map<Address, Account> const& dev::eth::genesisState()
std::unique_ptr<BlockInfo> CanonBlockChain::s_genesis; std::unique_ptr<BlockInfo> CanonBlockChain::s_genesis;
boost::shared_mutex CanonBlockChain::x_genesis; boost::shared_mutex CanonBlockChain::x_genesis;
Nonce CanonBlockChain::s_nonce(u64(42));
bytes CanonBlockChain::createGenesisBlock() bytes CanonBlockChain::createGenesisBlock()
{ {
@ -87,12 +88,33 @@ bytes CanonBlockChain::createGenesisBlock()
} }
block.appendList(15) block.appendList(15)
<< h256() << EmptyListSHA3 << h160() << stateRoot << EmptyTrie << EmptyTrie << LogBloom() << c_genesisDifficulty << 0 << c_genesisGasLimit << 0 << (unsigned)0 << string() << h256() << Nonce(u64(42)); << h256() << EmptyListSHA3 << h160() << stateRoot << EmptyTrie << EmptyTrie << LogBloom() << c_genesisDifficulty << 0 << c_genesisGasLimit << 0 << (unsigned)0 << string() << h256() << s_nonce;
block.appendRaw(RLPEmptyList); block.appendRaw(RLPEmptyList);
block.appendRaw(RLPEmptyList); block.appendRaw(RLPEmptyList);
return block.out(); return block.out();
} }
CanonBlockChain::CanonBlockChain(std::string const& _path, WithExisting _we, ProgressCallback const& _pc): BlockChain(CanonBlockChain::createGenesisBlock(), _path, _we, _pc) CanonBlockChain::CanonBlockChain(std::string const& _path, WithExisting _we, ProgressCallback const& _pc):
BlockChain(createGenesisBlock(), _path, _we, _pc)
{ {
} }
void CanonBlockChain::setGenesisNonce(Nonce const& _n)
{
WriteGuard l(x_genesis);
s_nonce = _n;
s_genesis.reset();
}
BlockInfo const& CanonBlockChain::genesis()
{
UpgradableGuard l(x_genesis);
if (!s_genesis)
{
auto gb = createGenesisBlock();
UpgradeGuard ul(l);
s_genesis.reset(new BlockInfo);
s_genesis->populate(&gb);
}
return *s_genesis;
}

28
libethereum/CanonBlockChain.h

@ -54,21 +54,27 @@ std::unordered_map<Address, Account> const& genesisState();
class CanonBlockChain: public BlockChain class CanonBlockChain: public BlockChain
{ {
public: public:
CanonBlockChain(WithExisting _we = WithExisting::Trust, ProgressCallback const& _pc = ProgressCallback()): CanonBlockChain(std::string(), _we, _pc) {} CanonBlockChain(WithExisting _we = WithExisting::Trust, ProgressCallback const& _pc = ProgressCallback()): CanonBlockChain(std::string(), _we, _pc) {}
CanonBlockChain(std::string const& _path, WithExisting _we = WithExisting::Trust, ProgressCallback const& _pc = ProgressCallback()); CanonBlockChain(std::string const& _path, WithExisting _we = WithExisting::Trust, ProgressCallback const& _pc = ProgressCallback());
~CanonBlockChain() {} ~CanonBlockChain() {}
/// @returns the genesis block header. /// @returns the genesis block header.
static BlockInfo const& genesis() { UpgradableGuard l(x_genesis); if (!s_genesis) { auto gb = createGenesisBlock(); UpgradeGuard ul(l); s_genesis.reset(new BlockInfo); s_genesis->populate(&gb); } return *s_genesis; } static BlockInfo const& genesis();
/// @returns the genesis block as its RLP-encoded byte array. /// @returns the genesis block as its RLP-encoded byte array.
/// @note This is slow as it's constructed anew each call. Consider genesis() instead. /// @note This is slow as it's constructed anew each call. Consider genesis() instead.
static bytes createGenesisBlock(); static bytes createGenesisBlock();
/// 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);
private: private:
/// Static genesis info and its lock. /// Static genesis info and its lock.
static boost::shared_mutex x_genesis; static boost::shared_mutex x_genesis;
static std::unique_ptr<BlockInfo> s_genesis; static std::unique_ptr<BlockInfo> s_genesis;
static Nonce s_nonce;
}; };
} }

7
libethereum/Client.cpp

@ -47,8 +47,11 @@ VersionChecker::VersionChecker(string const& _dbPath):
(void)protocolVersion; (void)protocolVersion;
auto minorProtocolVersion = (unsigned)status[1]; auto minorProtocolVersion = (unsigned)status[1];
auto databaseVersion = (unsigned)status[2]; auto databaseVersion = (unsigned)status[2];
h256 ourGenesisHash = CanonBlockChain::genesis().hash();
auto genesisHash = status.itemCount() > 3 ? (h256)status[3] : ourGenesisHash;
m_action = m_action =
databaseVersion != c_databaseVersion ? databaseVersion != c_databaseVersion || genesisHash != ourGenesisHash ?
WithExisting::Kill WithExisting::Kill
: minorProtocolVersion != eth::c_minorProtocolVersion ? : minorProtocolVersion != eth::c_minorProtocolVersion ?
WithExisting::Verify WithExisting::Verify
@ -73,7 +76,7 @@ void VersionChecker::setOk()
{ {
cwarn << "Unhandled exception! Failed to create directory: " << m_path << "\n" << boost::current_exception_diagnostic_information(); cwarn << "Unhandled exception! Failed to create directory: " << m_path << "\n" << boost::current_exception_diagnostic_information();
} }
writeFile(m_path + "/status", rlpList(eth::c_protocolVersion, eth::c_minorProtocolVersion, c_databaseVersion)); writeFile(m_path + "/status", rlpList(eth::c_protocolVersion, eth::c_minorProtocolVersion, c_databaseVersion, CanonBlockChain::genesis().hash()));
} }
} }

Loading…
Cancel
Save