Browse Source

Merge pull request #2775 from LefterisJP/add_js_console_exit

Add a JS console command for exiting the client
cl-refactor
Gav Wood 10 years ago
parent
commit
bc0fbf02bc
  1. 22
      eth/main.cpp
  2. 6
      libethereum/Client.cpp
  3. 5
      libethereum/Client.h
  4. 6
      libjsconsole/JSConsole.h
  5. 5
      libjsqrc/admin.js
  6. 8
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  7. 1
      libweb3jsonrpc/WebThreeStubServerBase.h
  8. 6
      libweb3jsonrpc/abstractwebthreestubserver.h
  9. 1
      libweb3jsonrpc/spec.json
  10. 10
      test/libweb3jsonrpc/webthreestubclient.h

22
eth/main.cpp

@ -219,8 +219,6 @@ string pretty(h160 _a, dev::eth::State const& _st)
return ns;
}
bool g_exit = false;
inline bool isPrime(unsigned _number)
{
if (((!(_number & 1)) && _number != 2 ) || (_number < 2) || (_number % 3 == 0 && _number != 3))
@ -231,11 +229,6 @@ inline bool isPrime(unsigned _number)
return true;
}
void sighandler(int)
{
g_exit = true;
}
enum class NodeMode
{
PeerServer,
@ -1028,9 +1021,9 @@ int main(int argc, char** argv)
if (!remoteHost.empty())
web3.addNode(p2p::NodeId(), remoteHost + ":" + toString(remotePort));
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);
signal(SIGABRT, &Client::exitHandler);
signal(SIGTERM, &Client::exitHandler);
signal(SIGINT, &Client::exitHandler);
if (c)
{
@ -1044,17 +1037,20 @@ int main(int argc, char** argv)
shared_ptr<dev::WebThreeStubServer> rpcServer = make_shared<dev::WebThreeStubServer>(*console.connector(), web3, make_shared<SimpleAccountHolder>([&](){ return web3.ethereum(); }, getAccountPassword, keyManager), vector<KeyPair>(), keyManager, *gasPricer);
string sessionKey = rpcServer->newSession(SessionPermissions{{Privilege::Admin}});
console.eval("web3.admin.setSessionKey('" + sessionKey + "')");
while (console.readExpression())
while (!Client::shouldExit())
{
console.readExpression();
stopMiningAfterXBlocks(c, n, mining);
}
rpcServer->StopListening();
#endif
}
else
while (!g_exit)
while (!Client::shouldExit())
stopMiningAfterXBlocks(c, n, mining);
}
else
while (!g_exit)
while (!Client::shouldExit())
this_thread::sleep_for(chrono::milliseconds(1000));
#if ETH_JSONRPC

6
libethereum/Client.cpp

@ -67,6 +67,12 @@ const char* ClientTrace::name() { return EthTeal "⧫" EthGray " ◎"; }
const char* ClientDetail::name() { return EthTeal "" EthCoal ""; }
#endif
bool Client::s_shouldExit = false;
void Client::exitHandler(int)
{
s_shouldExit = true;
}
static const Addresses c_canaries =
{
Address("4bb7e8ae99b645c2b7860b8f3a2328aae28bd80a"), // gav

5
libethereum/Client.h

@ -121,6 +121,8 @@ public:
BlockQueue const& blockQueue() const { return m_bq; }
/// Get the block queue.
OverlayDB const& stateDB() const { return m_stateDB; }
/// Handles a request to exit the client with a specific signal
static void exitHandler(int signal);
/// Freeze worker thread and sync some of the block queue.
std::tuple<ImportRoute, bool, unsigned> syncQueue(unsigned _max = 1);
@ -144,6 +146,8 @@ public:
/// Enable/disable precomputing of the DAG for next epoch
void setShouldPrecomputeDAG(bool _precompute);
/// Check to see if we should exit
static bool shouldExit() { return s_shouldExit; }
/// Check to see if we'd mine on an apparently bad chain.
bool mineOnBadChain() const { return m_mineOnBadChain; }
/// Set true if you want to mine even when the canary says you're on the wrong chain.
@ -324,6 +328,7 @@ protected:
bool m_forceMining = false; ///< Mine even when there are no transactions pending?
bool m_mineOnBadChain = false; ///< Mine even when the canary says it's a bad chain.
bool m_paranoia = false; ///< Should we be paranoid about our state?
static bool s_shouldExit; ///< Exit requested?
mutable std::chrono::system_clock::time_point m_lastGarbageCollection;
///< When did we last both doing GC on the watches?

6
libjsconsole/JSConsole.h

@ -41,7 +41,7 @@ public:
JSConsole(): m_engine(Engine()), m_printer(Printer(m_engine)) {}
~JSConsole() {}
bool readExpression() const
void readExpression() const
{
std::string cmd = "";
g_logPost = [](std::string const& a, char const*)
@ -83,9 +83,6 @@ public:
}
} while (openBrackets > 0);
if (cmd == "quit")
return false;
if (!isEmpty)
{
#if ETH_READLINE
@ -95,7 +92,6 @@ public:
std::string result = m_printer.prettyPrint(value).cstr();
std::cout << result << std::endl;
}
return true;
}
void eval(std::string const& _expression) { m_engine.eval(_expression.c_str()); }

5
libjsqrc/admin.js

@ -35,6 +35,11 @@ web3._extend({
call: 'admin_eth_blockQueueStatus',
inputFormatter: [getSessionKey],
params: 1
}), new web3._extend.Method({
name: 'eth.exit',
call: 'admin_eth_exit',
inputFormatter: [getSessionKey],
params: 1
}), new web3._extend.Method({
name: 'net.nodeInfo',
call: 'admin_net_nodeInfo',

8
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -23,6 +23,7 @@
#include "WebThreeStubServerBase.h"
#include <signal.h>
// Make sure boost/asio.hpp is included before windows.h.
#include <boost/asio.hpp>
@ -575,6 +576,13 @@ Json::Value WebThreeStubServerBase::admin_net_nodeInfo(const string& _session)
return ret;
}
bool WebThreeStubServerBase::admin_eth_exit(string const& _session)
{
ADMIN;
Client::exitHandler(SIGTERM);
return true;
}
bool WebThreeStubServerBase::admin_eth_setMining(bool _on, std::string const& _session)
{
ADMIN;

1
libweb3jsonrpc/WebThreeStubServerBase.h

@ -167,6 +167,7 @@ public:
virtual Json::Value admin_net_peers(std::string const& _session);
virtual Json::Value admin_net_nodeInfo(std::string const& _session);
virtual bool admin_eth_exit(std::string const& _session);
virtual bool admin_eth_setMining(bool _on, std::string const& _session);
virtual Json::Value admin_eth_blockQueueStatus(std::string const& _session) { (void)_session; return Json::Value(); }
virtual bool admin_eth_setAskPrice(std::string const& _wei, std::string const& _session) { (void)_wei; (void)_session; return false; }

6
libweb3jsonrpc/abstractwebthreestubserver.h

@ -85,6 +85,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(jsonrpc::Procedure("admin_net_peers", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_net_peersI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_eth_blockQueueStatus", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_eth_blockQueueStatusI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_net_nodeInfo", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_net_nodeInfoI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_eth_exit", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_eth_exitI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_eth_setAskPrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_eth_setAskPriceI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_eth_setBidPrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_eth_setBidPriceI);
this->bindAndAddMethod(jsonrpc::Procedure("admin_eth_setReferencePrice", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::admin_eth_setReferencePriceI);
@ -412,6 +413,10 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
{
response = this->admin_net_nodeInfo(request[0u].asString());
}
inline virtual void admin_eth_exitI(const Json::Value &request, Json::Value &response)
{
response = this->admin_eth_exit(request[0u].asString());
}
inline virtual void admin_eth_setAskPriceI(const Json::Value &request, Json::Value &response)
{
response = this->admin_eth_setAskPrice(request[0u].asString(), request[1u].asString());
@ -549,6 +554,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
virtual Json::Value admin_net_peers(const std::string& param1) = 0;
virtual Json::Value admin_eth_blockQueueStatus(const std::string& param1) = 0;
virtual Json::Value admin_net_nodeInfo(const std::string& param1) = 0;
virtual bool admin_eth_exit(const std::string& param1) = 0;
virtual bool admin_eth_setAskPrice(const std::string& param1, const std::string& param2) = 0;
virtual bool admin_eth_setBidPrice(const std::string& param1, const std::string& param2) = 0;
virtual bool admin_eth_setReferencePrice(const std::string& param1, const std::string& param2) = 0;

1
libweb3jsonrpc/spec.json

@ -77,6 +77,7 @@
{ "name": "admin_net_peers", "params": [""], "returns": [] },
{ "name": "admin_eth_blockQueueStatus", "params": [""], "returns": {}},
{ "name": "admin_net_nodeInfo", "params": [""], "returns": {}},
{ "name": "admin_eth_exit", "params": [""], "returns": true},
{ "name": "admin_eth_setAskPrice", "params": ["", ""], "returns": true },
{ "name": "admin_eth_setBidPrice", "params": ["", ""], "returns": true },
{ "name": "admin_eth_setReferencePrice", "params": ["", ""], "returns": true },

10
test/libweb3jsonrpc/webthreestubclient.h

@ -763,6 +763,16 @@ class WebThreeStubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool admin_eth_exit(const std::string& param1) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p.append(param1);
Json::Value result = this->CallMethod("admin_eth_exit",p);
if (result.isBool())
return result.asBool();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
bool admin_eth_setAskPrice(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException)
{
Json::Value p;

Loading…
Cancel
Save