Skip to content

Commit

Permalink
Merge pull request #285 from skalenetwork/enhancement/SKALE-2586-remo…
Browse files Browse the repository at this point in the history
…ve-config

Enhancement/skale 2586 remove config
  • Loading branch information
DmytroNazarenko authored Jun 3, 2020
2 parents e16b1fb + a8139d5 commit 1b7e035
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 162 deletions.
4 changes: 2 additions & 2 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ bool Client::isTimeToDoSnapshot( uint64_t _timestamp ) const {
this->last_snapshot_time / uint64_t( snapshotIntervalMs );
}

void Client::setRestartOrExitTime( uint64_t _timestamp, bool _isExit ) const {
m_instanceMonitor->initRotationParams( _timestamp, _isExit );
void Client::setSchainExitTime( uint64_t _timestamp ) const {
m_instanceMonitor->initRotationParams( _timestamp );
}

void Client::onChainChanged( ImportRoute const& _ir ) {
Expand Down
4 changes: 2 additions & 2 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ class Client : public ClientBase, protected Worker {
return path;
}

// set restarting or exiting time for node rotation
void setRestartOrExitTime( uint64_t _timestamp, bool _isExit ) const;
// set exiting time for node rotation
void setSchainExitTime( uint64_t _timestamp ) const;

dev::h256 getSnapshotHash( unsigned _blockNumber ) const {
return this->m_snapshotManager->getSnapshotHash( _blockNumber );
Expand Down
31 changes: 7 additions & 24 deletions libethereum/InstanceMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,34 @@ using namespace dev;
namespace fs = boost::filesystem;

const std::string InstanceMonitor::rotation_file_name = "rotation.txt";
const std::string InstanceMonitor::temp_config_ext = ".tmp";

void InstanceMonitor::performRotation() {
if ( m_isExit ) {
fs::remove( m_rotationFilePath );
ExitHandler::exitHandler( SIGTERM );
} else {
fs::path newConfigPath = m_configPath;
newConfigPath += temp_config_ext;
if ( !fs::exists( newConfigPath ) ) {
throw std::runtime_error( "New config not found" );
}
fs::remove( m_configPath );
fs::rename( newConfigPath, m_configPath );
fs::remove( m_rotationFilePath );
ExitHandler::exitHandler( SIGTERM );
}
fs::remove( m_rotationFilePath );
ExitHandler::exitHandler( SIGTERM );
}

void InstanceMonitor::initRotationParams( uint64_t _timestamp, bool _isExit ) {
void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) {
nlohmann::json rotationJson = nlohmann::json::object();
rotationJson["timestamp"] = _timestamp;
rotationJson["isExit"] = _isExit;
rotationJson["timestamp"] = _finishTimestamp;

std::ofstream rotationFile( m_rotationFilePath.string() );
rotationFile << rotationJson;

m_finishTimestamp = _timestamp;
m_isExit = _isExit;
m_finishTimestamp = _finishTimestamp;
}

bool InstanceMonitor::isTimeToRotate( uint64_t _timestamp ) {
bool InstanceMonitor::isTimeToRotate( uint64_t _finishTimestamp ) {
if ( !fs::exists( m_rotationFilePath ) ) {
return false;
}
return m_finishTimestamp <= _timestamp;
return m_finishTimestamp <= _finishTimestamp;
}

void InstanceMonitor::restoreRotationParams() {
if ( fs::exists( m_rotationFilePath ) ) {
std::ifstream rotateFile( m_rotationFilePath.string() );
auto rotateJson = nlohmann::json::parse( rotateFile );
auto rotateTimestamp = rotateJson["timestamp"].get< uint64_t >();
auto isExit = rotateJson["isExit"].get< bool >();
m_finishTimestamp = rotateTimestamp;
m_isExit = isExit;
}
}
20 changes: 6 additions & 14 deletions libethereum/InstanceMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,24 @@ namespace fs = boost::filesystem;

class InstanceMonitor {
public:
explicit InstanceMonitor( const boost::filesystem::path& _configPath )
: m_configPath( _configPath ),
m_finishTimestamp( 0 ),
m_isExit( false ),
m_rotationFilePath( dev::getDataDir() / rotation_file_name ) {
explicit InstanceMonitor()
: m_finishTimestamp( 0 ), m_rotationFilePath( dev::getDataDir() / rotation_file_name ) {
restoreRotationParams();
}
void performRotation();
void initRotationParams( uint64_t _timestamp, bool _isExit );
bool isTimeToRotate( uint64_t _timestamp );
void initRotationParams( uint64_t _finishTimestamp );
bool isTimeToRotate( uint64_t _finishTimestamp );

protected:
void restoreRotationParams();
[[nodiscard]] uint64_t finishTimestamp() const { return m_finishTimestamp; }

[[nodiscard]] bool isExit() const {
return m_isExit;
[[nodiscard]] fs::path rotationFilePath() const {
return m_rotationFilePath;
}

[[nodiscard]] fs::path rotationFilePath() const { return m_rotationFilePath; }

private : boost::filesystem::path const m_configPath;
uint64_t m_finishTimestamp;
bool m_isExit;
const fs::path m_rotationFilePath;

static const std::string rotation_file_name;
static const std::string temp_config_ext;
};
69 changes: 18 additions & 51 deletions libskale/httpserveroverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2688,20 +2688,19 @@ bool SkaleServerOverride::handleProtocolSpecificRequest( SkaleServerHelper& sse,
}

const SkaleServerOverride::protocol_rpc_map_t SkaleServerOverride::g_protocol_rpc_map = {
{"eth_setRestartOrExitTime", &SkaleServerOverride::eth_setRestartOrExitTime},
{"setRestartOrExitTime", &SkaleServerOverride::eth_setRestartOrExitTime},
{"setSchainExitTime", &SkaleServerOverride::setSchainExitTime},
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SkaleServerOverride::eth_setRestartOrExitTime( SkaleServerHelper& /*sse*/,
void SkaleServerOverride::setSchainExitTime( SkaleServerHelper& /*sse*/,
const std::string& strOrigin, const nlohmann::json& joRequest, nlohmann::json& joResponse ) {
SkaleServerOverride* pSO = this;
try {
// check "params" in joRequest and it's JSON object { }
if ( !skale::server::helper::checkParamsIsObject(
"eth_setRestartOrExitTime", joRequest, joResponse ) )
"setSchainExitTime", joRequest, joResponse ) )
return;
const nlohmann::json& joParams = joRequest["params"];
// parse value of "finishTime"
Expand All @@ -2717,71 +2716,40 @@ void SkaleServerOverride::eth_setRestartOrExitTime( SkaleServerHelper& /*sse*/,
// no "finishTime" present in "params"
throw std::runtime_error( "missing the \"finishTime\" parameter" );
}
// parse value of "isExit"
bool isExit = false;
if ( joParams.count( "isExit" ) > 0 ) {
const nlohmann::json& joValue = joParams["isExit"];
if ( joValue.is_boolean() )
isExit = joValue.get< bool >();
else if ( joValue.is_number() )
isExit = joValue.get< int >() ? true : false;
else if ( joValue.is_string() ) {
std::string s = skutils::tools::to_lower(
skutils::tools::trim_copy( joValue.get< std::string >() ) );
if ( s.length() > 0 ) {
char c = s[0];
if ( c == 't' || c == 'y' ) // "true", "yes"
isExit = true;
else if ( '0' <= c && c <= '9' )
isExit = atoi( s.c_str() ) ? true : false;
else
throw std::runtime_error(
"invalid value in the \"isExit\" parameter, need boolean flag" );
}
} else
throw std::runtime_error(
"invalid value in the \"isExit\" parameter, need boolean flag" );
}

// get connection info
skutils::url u( strOrigin );
std::string strIP = u.host();
bool isLocalAddress = skutils::is_local_private_network_address(
strIP ); // NOTICE: supports both IPv4 and IPv6
// print info about this method call into log output
clog( dev::VerbosityInfo, cc::warn( "ADMIN-CALL" ) )
<< ( cc::debug( "Got " ) + cc::info( "eth_setRestartOrExitTime" ) +
<< ( cc::debug( "Got " ) + cc::info( "setSchainExitTime" ) +
cc::debug( " call with " ) + cc::notice( "finishTime" ) + cc::debug( "=" ) +
cc::size10( finishTime ) + cc::debug( ", " ) + cc::notice( "isExit" ) +
cc::debug( "=" ) + cc::yn( isExit ) + cc::debug( ", " ) +
cc::notice( "origin" ) + cc::debug( "=" ) + cc::notice( strOrigin ) +
cc::debug( ", " ) + cc::notice( "remote IP" ) + cc::debug( "=" ) +
cc::notice( strIP ) + cc::debug( ", " ) + cc::notice( "isLocalAddress" ) +
cc::debug( "=" ) + cc::yn( isLocalAddress ) );
cc::size10( finishTime ) + cc::debug( ", " ) + cc::notice( "origin" ) +
cc::debug( "=" ) + cc::notice( strOrigin ) + cc::debug( ", " ) +
cc::notice( "remote IP" ) + cc::debug( "=" ) + cc::notice( strIP ) +
cc::debug( ", " ) + cc::notice( "isLocalAddress" ) + cc::debug( "=" ) +
cc::yn( isLocalAddress ) );
// return call error if call from outside of local network
if ( !isLocalAddress )
throw std::runtime_error(
"caller have no permition for this action" ); // NOTICE: just throw exception and
// RPC call will extract text from it
// and return it as call error
//
// TO-DO: handle call to eth_setRestartOrExitTime or setRestartOrExitTime - both names are
// supported
//
// TO-DO: optionally, put some data into joResponse which represents return value JSON
//
// TESTING: you can use wascat console
// npm install -g
// wscat -c 127.0.0.1:15020
// {"jsonrpc":"2.0","id":12345,"method":"eth_setRestartOrExitTime","params":{}}
// {"jsonrpc":"2.0","id":12345,"method":"eth_setRestartOrExitTime","params":{"finishTime":123,"isExit":true}}
// {"jsonrpc":"2.0","id":12345,"method":"setSchainExitTime","params":{}}
// {"jsonrpc":"2.0","id":12345,"method":"setSchainExitTime","params":{"finishTime":123}}
//
// or specify JSON right in command line...
//
// wscat -c ws://127.0.0.1:15020 -w 1 -x
// '{"jsonrpc":"2.0","id":12345,"method":"eth_setRestartOrExitTime","params":{"finishTime":123,"isExit":true}}'
// wscat -c ws://127.0.0.1:15020 -w 1 -x
// '{"jsonrpc":"2.0","id":12345,"method":"setRestartOrExitTime","params":{"finishTime":123,"isExit":true}}'
//
// '{"jsonrpc":"2.0","id":12345,"method":"setSchainExitTime","params":{"finishTime":123}}'

// Result
std::string strRequest = joRequest.dump();
Expand All @@ -2792,30 +2760,29 @@ void SkaleServerOverride::eth_setRestartOrExitTime( SkaleServerHelper& /*sse*/,
dev::eth::Client* pClient = dynamic_cast< dev::eth::Client* >( pEthereum );
if ( !pClient )
throw std::runtime_error( "internal error, no client interface found" );
pClient->setRestartOrExitTime( uint64_t( finishTime ), isExit );
pClient->setSchainExitTime( uint64_t( finishTime ) );
joResponse = nlohmann::json::parse( strResponse );
} catch ( const std::exception& ex ) {
if ( pSO->m_bTraceCalls )
clog( dev::Verbosity::VerbosityError,
cc::debug( " during call from " ) + cc::u( strOrigin ) )
<< ( " " + cc::error( "error in " ) + cc::warn( "eth_setRestartOrExitTime" ) +
<< ( " " + cc::error( "error in " ) + cc::warn( "setSchainExitTime" ) +
cc::error( " rpc method, exception " ) + cc::warn( ex.what() ) );
nlohmann::json joError = nlohmann::json::object();
joError["code"] = -32602;
joError["message"] =
std::string( "error in \"eth_setRestartOrExitTime\" rpc method, exception: " ) +
ex.what();
std::string( "error in \"setSchainExitTime\" rpc method, exception: " ) + ex.what();
joResponse["error"] = joError;
return;
} catch ( ... ) {
if ( pSO->m_bTraceCalls )
clog( dev::Verbosity::VerbosityError,
cc::debug( " during call from " ) + cc::u( strOrigin ) )
<< ( " " + cc::error( "error in " ) + cc::warn( "eth_setRestartOrExitTime" ) +
<< ( " " + cc::error( "error in " ) + cc::warn( "setSchainExitTime" ) +
cc::error( " rpc method, unknown exception " ) );
nlohmann::json joError = nlohmann::json::object();
joError["code"] = -32602;
joError["message"] = "error in \"eth_setRestartOrExitTime\" rpc method, unknown exception";
joError["message"] = "error in \"setSchainExitTime\" rpc method, unknown exception";
joResponse["error"] = joError;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion libskale/httpserveroverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class SkaleServerOverride : public jsonrpc::AbstractServerConnector,
typedef std::map< std::string, rpc_method_t > protocol_rpc_map_t;
static const protocol_rpc_map_t g_protocol_rpc_map;

void eth_setRestartOrExitTime( SkaleServerHelper& sse, const std::string& strOrigin,
void setSchainExitTime( SkaleServerHelper& sse, const std::string& strOrigin,
const nlohmann::json& joRequest, nlohmann::json& joResponse );

friend class SkaleRelayWS;
Expand Down
5 changes: 2 additions & 3 deletions libweb3jsonrpc/Eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,9 @@ Json::Value Eth::eth_unsubscribe( Json::Value const& /*_transaction*/ ) {
}
}

Json::Value Eth::eth_setRestartOrExitTime( Json::Value const& /*_transaction*/ ) {
Json::Value Eth::setSchainExitTime( Json::Value const& /*_transaction*/ ) {
try {
throw JsonRpcException(
"eth_setRestartOrExitTime() API is not supported yet over HTTP(S)" );
throw JsonRpcException( "setSchainExitTime() API is not supported yet over HTTP(S)" );
} catch ( Exception const& ) {
throw JsonRpcException( exceptionToErrorMessage() );
}
Expand Down
2 changes: 1 addition & 1 deletion libweb3jsonrpc/Eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Eth : public dev::rpc::EthFace {
virtual std::string eth_chainId() override;
virtual Json::Value eth_subscribe( Json::Value const& _transaction ) override;
virtual Json::Value eth_unsubscribe( Json::Value const& _transaction ) override;
virtual Json::Value eth_setRestartOrExitTime( Json::Value const& _transaction ) override;
virtual Json::Value setSchainExitTime( Json::Value const& _transaction ) override;

void setTransactionDefaults( eth::TransactionSkeleton& _t );

Expand Down
15 changes: 5 additions & 10 deletions libweb3jsonrpc/EthFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,9 @@ class EthFace : public ServerInterface< EthFace > {
&dev::rpc::EthFace::eth_unsubscribeI );

this->bindAndAddMethod(
jsonrpc::Procedure( "eth_setRestartOrExitTime", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::Procedure( "setSchainExitTime", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_OBJECT, NULL ),
&dev::rpc::EthFace::eth_setRestartOrExitTimeI );
this->bindAndAddMethod(
jsonrpc::Procedure( "setRestartOrExitTime", jsonrpc::PARAMS_BY_POSITION,
jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_OBJECT, NULL ),
&dev::rpc::EthFace::eth_setRestartOrExitTimeI );
&dev::rpc::EthFace::setSchainExitTimeI );

this->bindAndAddMethod(
jsonrpc::Procedure( "eth_inspectTransaction", jsonrpc::PARAMS_BY_POSITION,
Expand Down Expand Up @@ -396,9 +392,8 @@ class EthFace : public ServerInterface< EthFace > {
inline virtual void eth_unsubscribeI( const Json::Value& request, Json::Value& response ) {
response = this->eth_unsubscribe( request[0u] );
}
inline virtual void eth_setRestartOrExitTimeI(
const Json::Value& request, Json::Value& response ) {
response = this->eth_setRestartOrExitTime( request[0u] );
inline virtual void setSchainExitTimeI( const Json::Value& request, Json::Value& response ) {
response = this->setSchainExitTime( request[0u] );
}
inline virtual void eth_signTransactionI( const Json::Value& request, Json::Value& response ) {
response = this->eth_signTransaction( request[0u] );
Expand Down Expand Up @@ -481,7 +476,7 @@ class EthFace : public ServerInterface< EthFace > {
virtual Json::Value eth_signTransaction( const Json::Value& param1 ) = 0;
virtual Json::Value eth_subscribe( const Json::Value& param1 ) = 0;
virtual Json::Value eth_unsubscribe( const Json::Value& param1 ) = 0;
virtual Json::Value eth_setRestartOrExitTime( const Json::Value& param1 ) = 0;
virtual Json::Value setSchainExitTime( const Json::Value& param1 ) = 0;
virtual Json::Value eth_inspectTransaction( const std::string& param1 ) = 0;
virtual std::string eth_sendRawTransaction( const std::string& param1 ) = 0;
virtual bool eth_notePassword( const std::string& param1 ) = 0;
Expand Down
3 changes: 1 addition & 2 deletions libweb3jsonrpc/eth.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
{ "name": "eth_signTransaction", "params": [{}], "order": [], "returns": {}},
{ "name": "eth_subscribe", "params": [{}], "order": [], "returns": {}},
{ "name": "eth_unsubscribe", "params": [{}], "order": [], "returns": {}},
{ "name": "eth_setRestartOrExitTime", "params": [{}], "order": [], "returns": {}},
{ "name": "setRestartOrExitTime", "params": [{}], "order": [], "returns": {}},
{ "name": "setSchainExitTime", "params": [{}], "order": [], "returns": {}},
{ "name": "eth_inspectTransaction", "params": [""], "order": [], "returns": {}},
{ "name": "eth_sendRawTransaction", "params": [""], "order": [], "returns": ""},
{ "name": "eth_notePassword", "params": [""], "order": [], "returns": true},
Expand Down
2 changes: 1 addition & 1 deletion skaled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ int main( int argc, char** argv ) try {
std::shared_ptr< GasPricer > gasPricer;
std::shared_ptr< InstanceMonitor > instanceMonitor;

instanceMonitor.reset( new InstanceMonitor( configPath ) );
instanceMonitor.reset( new InstanceMonitor() );

if ( getDataDir().size() )
Defaults::setDBPath( getDataDir() );
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/libethereum/ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class TestClientFixture : public TestOutputHelperFixture {
// ), dir,
// dir, chainParams, WithExisting::Kill, {"eth"}, testingMode ) );

auto monitor = make_shared< InstanceMonitor >("test");
auto monitor = make_shared< InstanceMonitor >();
m_ethereum.reset( new eth::ClientTest( chainParams, ( int ) chainParams.networkID,
shared_ptr< GasPricer >(), NULL, monitor, dir, WithExisting::Kill ) );

Expand Down Expand Up @@ -189,7 +189,7 @@ class TestClientSnapshotsFixture : public TestOutputHelperFixture, public Fixtur
std::shared_ptr< SnapshotManager > mgr;
mgr.reset( new SnapshotManager( fs::path( BTRFS_DIR_PATH ), {"vol1", "vol2"} ) );

auto monitor = make_shared< InstanceMonitor >("test");
auto monitor = make_shared< InstanceMonitor >();
m_ethereum.reset( new eth::ClientTest( chainParams, ( int ) chainParams.networkID,
shared_ptr< GasPricer >(), mgr, monitor, dir, WithExisting::Kill ) );

Expand Down
Loading

0 comments on commit 1b7e035

Please sign in to comment.