SKALE-3023 add getLatestCreatedKey

parent 591d503b
......@@ -181,7 +181,9 @@ stringstream LevelDB::getAllKeys() {
Json::Value key_data;
Json::Reader reader;
reader.parse(it->value().ToString().c_str(), key_data);
value = " VALUE: " + key_data["value"].asString() + ", TIMESTAMP: " + key_data["timestamp"].asString();
string timestamp_to_date_command = "date -d @" + key_data["timestamp"].asString();
value = " VALUE: " + key_data["value"].asString() + ", TIMESTAMP: " + exec(timestamp_to_date_command.c_str());
} else {
// old style keys
value = " VALUE: " + it->value().ToString();
......@@ -194,7 +196,29 @@ stringstream LevelDB::getAllKeys() {
}
pair<string, uint64_t> LevelDB::getLatestCreatedKey() {
leveldb::Iterator *it = db->NewIterator(readOptions);
uint64_t latest_timestamp = 0;
string latest_created_key_name = "";
for (it->SeekToFirst(); it->Valid(); it->Next()) {
if (it->value().ToString()[0] == '{') {
// new style keys
Json::Value key_data;
Json::Reader reader;
reader.parse(it->value().ToString().c_str(), key_data);
if (key_data["timestamp"].asUInt64() > latest_timestamp) {
latest_timestamp = key_data["timestamp"].asUInt64();
latest_created_key_name = it->key().ToString();
}
} else {
// old style keys
// assuming server has at least one new-style key created
continue;
}
}
return {latest_created_key_name, latest_timestamp};
}
......
......@@ -56,7 +56,7 @@ Json::Value SGXInfoServer::getAllKeysInfo() {
RETURN_SUCCESS(result)
}
Json::Value SGXInfoServer::getLastCreatedKey() {
Json::Value SGXInfoServer::getLatestCreatedKey() {
Json::Value result;
try {
......
......@@ -49,7 +49,7 @@ public:
virtual Json::Value getAllKeysInfo();
virtual Json::Value getLastCreatedKey();
virtual Json::Value getLatestCreatedKey();
virtual Json::Value getServerConfiguration();
......
......@@ -62,26 +62,7 @@
uint32_t enclaveLogLevel = 0;
using namespace std;
// Copy from libconsensus
string exec( const char* cmd ) {
CHECK_STATE( cmd );
std::array< char, 128 > buffer;
std::string result;
std::unique_ptr< FILE, decltype( &pclose ) > pipe( popen( cmd, "r" ), pclose );
if ( !pipe ) {
BOOST_THROW_EXCEPTION( std::runtime_error( "popen() failed!" ) );
}
while ( fgets( buffer.data(), buffer.size(), pipe.get() ) != nullptr ) {
result += buffer.data();
}
return result;
}
using namespace std;
void systemHealthCheck() {
string ulimit;
......
......@@ -96,6 +96,24 @@ BOOST_THROW_EXCEPTION(runtime_error(__ERR_STRING__)); \
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#define SAFE_UINT8_BUF(__X__, __Y__) ;uint8_t __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
// Copy from libconsensus
inline string exec( const char* cmd ) {
CHECK_STATE( cmd );
std::array< char, 128 > buffer;
std::string result;
std::unique_ptr< FILE, decltype( &pclose ) > pipe( popen( cmd, "r" ), pclose );
if ( !pipe ) {
BOOST_THROW_EXCEPTION( std::runtime_error( "popen() failed!" ) );
}
while ( fgets( buffer.data(), buffer.size(), pipe.get() ) != nullptr ) {
result += buffer.data();
}
return result;
}
#include <shared_mutex>
extern std::shared_timed_mutex sgxInitMutex;
......
......@@ -52,11 +52,11 @@ void getAllKeysInfo() {
exit(0);
}
void getLastCreatedKey() {
void getLatestCreatedKey() {
jsonrpc::HttpClient client("http://localhost:1030");
StubClient c(client, jsonrpc::JSONRPC_CLIENT_V2);
std::cout << "Info client inited" << std::endl;
Json::Value lastCreatedKey = c.getLastCreatedKey();
Json::Value lastCreatedKey = c.getLatestCreatedKey();
std::cout << "Last created key name: " << lastCreatedKey["keyName"] << std::endl;
std::cout << "Last created key creation time: " << lastCreatedKey["creationTime"] << std::endl;
exit(0);
......
......@@ -324,11 +324,11 @@ class StubClient : public jsonrpc::Client
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getLastCreatedKey()
Json::Value getLatestCreatedKey()
{
Json::Value p;
p = Json::nullValue;
Json::Value result = this->CallMethod("getLastCreatedKey", p);
Json::Value result = this->CallMethod("getLatestCreatedKey", p);
if (result.isObject())
return result;
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment