From 008c400cfb2f57ea4cd018bda0ad6031811c1351 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 24 Jul 2015 14:37:40 +0200 Subject: [PATCH] Interactive is now a JS console. Network can be chosen at run time (--frontier, --olympic). Params slimmed, uses JSON. --- eth/main.cpp | 72 +++++++++++++++++++++++++------------- libethcore/BlockInfo.cpp | 18 +++++----- libethcore/BlockInfo.h | 1 + libethcore/Common.cpp | 17 +++++++-- libethcore/Common.h | 4 ++- libethcore/Params.cpp | 14 ++++---- libethcore/Params.h | 15 ++++---- libethereum/BlockChain.cpp | 5 +-- 8 files changed, 92 insertions(+), 54 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index af3085f82..e47d0b0fd 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -121,8 +121,13 @@ void help() << "Usage eth [OPTIONS]" << endl << "Options:" << endl << endl << "Client mode (default):" << endl + << " --olympic Use the Olympic (0.9) protocol." << endl + << " --frontier Use the Frontier (1.0) protocol." << endl + << " --private Use a private chain." << endl << " -o,--mode Start a full node or a peer node (default: full)." << endl +#if ETH_JSCONSOLE || !ETH_TRUE << " -i,--interactive Enter interactive mode (default: non-interactive)." << endl +#endif #if ETH_JSONRPC || !ETH_TRUE << " -j,--json-rpc Enable JSON-RPC server (default: off)." << endl << " --json-rpc-port Specify JSON-RPC server port (implies '-j', default: " << SensibleHttpPort << ")." << endl @@ -139,7 +144,6 @@ void help() << " --master Give the master password for the key store." << endl << " --password Give a password for a private key." << endl << " --sentinel Set the sentinel for reporting bad blocks or chain issues." << endl - << " --prime Specify n as the 6 digit prime number to start Frontier." << endl << endl << "Client transacting:" << endl /*<< " -B,--block-fees Set the block fee profit in the reference unit e.g. ยข (default: 15)." << endl @@ -168,10 +172,10 @@ void help() << " --listen Listen on the given port for incoming connections (default: 30303)." << endl << " -r,--remote (:) Connect to remote host (default: none)." << endl << " --port Connect to remote port (default: 30303)." << endl - << " --network-id Only connect to other hosts with this network id (default:0)." << endl + << " --network-id Only connect to other hosts with this network id." << endl << " --upnp Use UPnP for NAT (default: on)." << endl - << " --no-discovery Disable Node discovery. (experimental)" << endl - << " --pin Only connect to required (trusted) peers. (experimental)" << endl + << " --no-discovery Disable Node discovery." << endl + << " --pin Only connect to required (trusted) peers." << endl // << " --require-peers List of required (trusted) peers. (experimental)" << endl << endl; MinerCLI::streamHelp(cout); @@ -196,9 +200,6 @@ void help() << " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (default: 8)." << endl << " -V,--version Show the version and exit." << endl << " -h,--help Show this help message and exit." << endl -#if ETH_JSCONSOLE || !ETH_TRUE - << " --console Use interactive javascript console" << endl -#endif ; exit(0); } @@ -1068,8 +1069,8 @@ int main(int argc, char** argv) /// Operating mode. OperationMode mode = OperationMode::Node; string dbPath; - unsigned prime = 0; - bool yesIReallyKnowWhatImDoing = false; +// unsigned prime = 0; +// bool yesIReallyKnowWhatImDoing = false; /// File name for import/export. string filename; @@ -1083,10 +1084,14 @@ int main(int argc, char** argv) /// General params for Node operation NodeMode nodeMode = NodeMode::Full; bool interactive = false; -#if ETH_JSONRPC +#if ETH_JSONRPC || !ETH_TRUE int jsonrpc = -1; #endif string jsonAdmin; + string genesisJSON; + dev::eth::Network releaseNetwork = c_network; + string privateChain; + bool upnp = true; WithExisting withExisting = WithExisting::Trust; string sentinel; @@ -1148,10 +1153,6 @@ 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) @@ -1196,7 +1197,7 @@ int main(int argc, char** argv) mode = OperationMode::Export; filename = argv[++i]; } - else if (arg == "--prime" && i + 1 < argc) +/* else if (arg == "--prime" && i + 1 < argc) try { prime = stoi(argv[++i]); @@ -1208,7 +1209,7 @@ int main(int argc, char** argv) } else if (arg == "--yes-i-really-know-what-im-doing") yesIReallyKnowWhatImDoing = true; - else if (arg == "--sentinel" && i + 1 < argc) +*/ else if (arg == "--sentinel" && i + 1 < argc) sentinel = argv[++i]; else if (arg == "--mine-on-wrong-chain") mineOnWrongChain = true; @@ -1257,6 +1258,15 @@ int main(int argc, char** argv) cerr << "Bad " << arg << " option: " << argv[i] << endl; return -1; } + else if (arg == "--private" && i + 1 < argc) + try { + privateChain = argv[++i]; + } + catch (...) + { + cerr << "Bad " << arg << " option: " << argv[i] << endl; + return -1; + } else if (arg == "-K" || arg == "--kill-blockchain" || arg == "--kill") withExisting = WithExisting::Kill; else if (arg == "-R" || arg == "--rebuild") @@ -1314,7 +1324,7 @@ int main(int argc, char** argv) { try { - CanonBlockChain::setGenesis(contentsString(argv[++i])); + genesisJSON = contentsString(argv[++i]); } catch (...) { @@ -1322,6 +1332,10 @@ int main(int argc, char** argv) return -1; } } + else if (arg == "--frontier") + releaseNetwork = eth::Network::Frontier; + else if (arg == "--olympic") + releaseNetwork = eth::Network::Olympic; /* else if ((arg == "-B" || arg == "--block-fees") && i + 1 < argc) { try @@ -1416,9 +1430,9 @@ int main(int argc, char** argv) pinning = true; else if (arg == "-f" || arg == "--force-mining") forceMining = true; - else if (arg == "-i" || arg == "--interactive") + else if (arg == "--old-interactive") interactive = true; -#if ETH_JSONRPC +#if ETH_JSONRPC || !ETH_TRUE else if ((arg == "-j" || arg == "--json-rpc")) jsonrpc = jsonrpc == -1 ? SensibleHttpPort : jsonrpc; else if (arg == "--json-rpc-port" && i + 1 < argc) @@ -1426,8 +1440,8 @@ int main(int argc, char** argv) else if (arg == "--json-admin" && i + 1 < argc) jsonAdmin = argv[++i]; #endif -#if ETH_JSCONSOLE - else if (arg == "--console") +#if ETH_JSCONSOLE || !ETH_TRUE + else if (arg == "-i" || arg == "--interactive" || arg == "--console") useConsole = true; #endif else if ((arg == "-v" || arg == "--verbosity") && i + 1 < argc) @@ -1475,6 +1489,13 @@ int main(int argc, char** argv) } } + // Set up all the chain config stuff. + resetNetwork(releaseNetwork); + if (!privateChain.empty()) + CanonBlockChain::forceGenesisExtraData(sha3(privateChain).asBytes()); + if (!genesisJSON.empty()) + CanonBlockChain::setGenesis(genesisJSON); + if (g_logVerbosity > 0) { cout << EthGrayBold "(++)Ethereum" EthReset << endl; @@ -1533,8 +1554,9 @@ int main(int argc, char** argv) StructuredLogger::get().initialize(structuredLogging, structuredLoggingFormat, structuredLoggingURL); auto netPrefs = publicIP.empty() ? NetworkPreferences(listenIP ,listenPort, upnp) : NetworkPreferences(publicIP, listenIP ,listenPort, upnp); - netPrefs.discovery = !disableDiscovery; - netPrefs.pin = pinning; + netPrefs.discovery = privateChain.empty() && !disableDiscovery; + netPrefs.pin = pinning || !privateChain.empty(); + auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp"); dev::WebThreeDirect web3( WebThreeDirect::composeClientVersion("++eth", clientName), @@ -1636,7 +1658,7 @@ int main(int argc, char** argv) cout << imported << " imported in " << e << " seconds at " << (round(imported * 10 / e) / 10) << " blocks/s (#" << web3.ethereum()->number() << ")" << endl; return 0; } - +/* if (c_network == eth::Network::Frontier && !yesIReallyKnowWhatImDoing) { auto pd = contents(getDataDir() + "primes"); @@ -1656,7 +1678,7 @@ int main(int argc, char** argv) primes.insert(prime); writeFile(getDataDir() + "primes", rlp(primes)); } - +*/ if (keyManager.exists()) { if (masterPassword.empty() || !keyManager.load(masterPassword)) diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index 8e2e2c4c4..60fb13ba9 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -183,28 +183,30 @@ void BlockInfo::populateFromParent(BlockInfo const& _parent) { m_stateRoot = _parent.stateRoot(); m_number = _parent.m_number + 1; + m_parentHash = _parent.m_hash; m_gasLimit = selectGasLimit(_parent); m_gasUsed = 0; m_difficulty = calculateDifficulty(_parent); - m_parentHash = _parent.m_hash; } u256 BlockInfo::selectGasLimit(BlockInfo const& _parent) const { - if (!m_parentHash) - return c_genesisGasLimit; + static const u256 c_gasFloorTarget = 3141592; + + if (!m_number) + throw GenesisBlockCannotBeCalculated(); else // target minimum of 3141592 - if (_parent.m_gasLimit < c_genesisGasLimit) - return min(c_genesisGasLimit, _parent.m_gasLimit + _parent.m_gasLimit / c_gasLimitBoundDivisor - 1); + if (_parent.m_gasLimit < c_gasFloorTarget) + return min(c_gasFloorTarget, _parent.m_gasLimit + _parent.m_gasLimit / c_gasLimitBoundDivisor - 1); else - return max(c_genesisGasLimit, _parent.m_gasLimit - _parent.m_gasLimit / c_gasLimitBoundDivisor + 1 + (_parent.m_gasUsed * 6 / 5) / c_gasLimitBoundDivisor); + return max(c_gasFloorTarget, _parent.m_gasLimit - _parent.m_gasLimit / c_gasLimitBoundDivisor + 1 + (_parent.m_gasUsed * 6 / 5) / c_gasLimitBoundDivisor); } u256 BlockInfo::calculateDifficulty(BlockInfo const& _parent) const { - if (!m_parentHash) - return (u256)c_genesisDifficulty; + if (!m_number) + throw GenesisBlockCannotBeCalculated(); else return max(c_minimumDifficulty, m_timestamp >= _parent.m_timestamp + c_durationLimit ? _parent.m_difficulty - (_parent.m_difficulty / c_difficultyBoundDivisor) : (_parent.m_difficulty + (_parent.m_difficulty / c_difficultyBoundDivisor))); } diff --git a/libethcore/BlockInfo.h b/libethcore/BlockInfo.h index cf3a9820e..f48461254 100644 --- a/libethcore/BlockInfo.h +++ b/libethcore/BlockInfo.h @@ -53,6 +53,7 @@ enum BlockDataType }; DEV_SIMPLE_EXCEPTION(NoHashRecorded); +DEV_SIMPLE_EXCEPTION(GenesisBlockCannotBeCalculated); /** @brief Encapsulation of a block header. * Class to contain all of a block header's data. It is able to parse a block header and populate diff --git a/libethcore/Common.cpp b/libethcore/Common.cpp index 05cc45280..16f315756 100644 --- a/libethcore/Common.cpp +++ b/libethcore/Common.cpp @@ -29,6 +29,7 @@ #include #include #include "Exceptions.h" +#include "Params.h" #include "BlockInfo.h" using namespace std; using namespace dev; @@ -51,11 +52,23 @@ const unsigned c_databaseVersionModifier = 0; #endif #if ETH_FRONTIER -Network const c_network = Network::Frontier; +Network c_network = resetNetwork(Network::Frontier); #else -Network const c_network = Network::Olympic; +Network c_network = resetNetwork(Network::Olympic); #endif +Network resetNetwork(Network _n) +{ + c_network = _n; + c_maximumExtraDataSize = c_network == Network::Olympic ? 1024 : 32; + c_minGasLimit = c_network == Network::Turbo ? 100000000 : 125000; + c_gasLimitBoundDivisor = 1024; + c_minimumDifficulty = 131072; + c_difficultyBoundDivisor = 2048; + c_durationLimit = c_network == Network::Turbo ? 2 : c_network == Network::Olympic ? 8 : 12; + return _n; +} + const unsigned c_databaseVersion = c_databaseBaseVersion + (c_databaseVersionModifier << 8) + (23 << 9); vector> const& units() diff --git a/libethcore/Common.h b/libethcore/Common.h index 116e1d5ed..c59b28f15 100644 --- a/libethcore/Common.h +++ b/libethcore/Common.h @@ -50,7 +50,9 @@ enum class Network Frontier = 1, Turbo = 2 }; -extern const Network c_network; +extern Network c_network; + +Network resetNetwork(Network _n); /// User-friendly string representation of the amount _b in wei. std::string formatBalance(bigint const& _b); diff --git a/libethcore/Params.cpp b/libethcore/Params.cpp index de9024522..a3a061f31 100644 --- a/libethcore/Params.cpp +++ b/libethcore/Params.cpp @@ -29,14 +29,12 @@ namespace eth { //--- BEGIN: AUTOGENERATED FROM github.com/ethereum/common/params.json -u256 const c_genesisDifficulty = 131072; -u256 const c_maximumExtraDataSize = c_network == Network::Olympic ? 1024 : 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; -u256 const c_minimumDifficulty = 131072; -u256 const c_difficultyBoundDivisor = 2048; -u256 const c_durationLimit = c_network == Network::Turbo ? 2 : c_network == Network::Olympic ? 8 : 12; +u256 c_maximumExtraDataSize; +u256 c_minGasLimit; +u256 c_gasLimitBoundDivisor; +u256 c_minimumDifficulty; +u256 c_difficultyBoundDivisor; +u256 c_durationLimit; //--- END: AUTOGENERATED FROM /feeStructure.json } diff --git a/libethcore/Params.h b/libethcore/Params.h index 3520b2f1b..07cbc36d5 100644 --- a/libethcore/Params.h +++ b/libethcore/Params.h @@ -29,14 +29,13 @@ namespace eth { //--- BEGIN: AUTOGENERATED FROM /feeStructure.json -extern u256 const c_genesisGasLimit; -extern u256 const c_minGasLimit; -extern u256 const c_gasLimitBoundDivisor; -extern u256 const c_genesisDifficulty; -extern u256 const c_minimumDifficulty; -extern u256 const c_difficultyBoundDivisor; -extern u256 const c_durationLimit; -extern u256 const c_maximumExtraDataSize; +extern u256 c_minGasLimit; +extern u256 c_gasLimitBoundDivisor; +extern u256 c_minimumDifficulty; +extern u256 c_difficultyBoundDivisor; +extern u256 c_durationLimit; +extern u256 c_maximumExtraDataSize; +//--- END: AUTOGENERATED FROM /feeStructure.json } } diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 45a1e6663..805347aaf 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -226,8 +226,9 @@ unsigned BlockChain::openDatabase(std::string const& _path, WithExisting _we) if (_we != WithExisting::Verify && !details(m_genesisHash)) { + BlockInfo gb(m_genesisBlock); // Insert details of genesis block. - m_details[m_genesisHash] = BlockDetails(0, c_genesisDifficulty, h256(), {}); + m_details[m_genesisHash] = BlockDetails(0, gb.difficulty(), h256(), {}); auto r = m_details[m_genesisHash].rlp(); m_extrasDB->Put(m_writeOptions, toSlice(m_genesisHash, ExtraDetails), (ldb::Slice)dev::ref(r)); } @@ -309,7 +310,7 @@ void BlockChain::rebuild(std::string const& _path, std::functionPut(m_writeOptions, toSlice(m_lastBlockHash, ExtraDetails), (ldb::Slice)dev::ref(m_details[m_lastBlockHash].rlp()));