Skip to content

Commit

Permalink
DOT-44 system_health
Browse files Browse the repository at this point in the history
Signed-off-by: strmv <strmax20@gmail.com>
  • Loading branch information
str-mv committed Jul 21, 2019
1 parent 941d524 commit ca57022
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,26 @@ unique_ptr<FinalHead> CPolkaApi::createFinalHead(Json jsonObject) {
memset(result, 0, sizeof(FinalHead));
unique_ptr<FinalHead> fh(result);

/// cout << endl<< endl<< endl << jsonObject.dump() << endl<< endl<< endl<< endl;

strcpy(fh->blockHash, jsonObject.string_value().c_str());

return fh;
}

unique_ptr<SystemHealth> CPolkaApi::createSystemHealth(Json jsonObject) {

SystemHealth *result = new SystemHealth();
memset(result, 0, sizeof(SystemHealth));
unique_ptr<SystemHealth> sh(result);

// cout << endl<< endl<< endl << jsonObject.dump() << endl<< endl<< endl<< endl;

sh->peers = jsonObject["peers"].int_value();
sh->isSyncing = jsonObject["isSyncing"].bool_value();
sh->shouldHavePeers = jsonObject["shouldHavePeers"].bool_value();

return sh;
}

unique_ptr<Metadata> CPolkaApi::createMetadata(Json jsonObject) {
unique_ptr<Metadata> md(new Metadata);
MetadataFactory mdf(_logger);
Expand Down Expand Up @@ -414,6 +427,15 @@ unique_ptr<BlockHeader> CPolkaApi::getBlockHeader(unique_ptr<GetBlockParams> par
return move(deserialize<BlockHeader, &CPolkaApi::createBlockHeader>(response));
}

unique_ptr<SystemHealth> CPolkaApi::getSystemHealth() {

Json query = Json::object{{"method", "system_health"}, {"params", Json::array()}};

Json response = _jsonRpc->request(query);

return move(deserialize<SystemHealth, &CPolkaApi::createSystemHealth>(response));
}

unique_ptr<FinalHead> CPolkaApi::getFinalizedHead() {

Json query = Json::object{{"method", "chain_getFinalizedHead"}, {"params", Json::array()}};
Expand Down
2 changes: 2 additions & 0 deletions src/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CPolkaApi : public IApplication, IWebSocketMessageObserver {
unique_ptr<SignedBlock> createBlock(Json jsonObject);
unique_ptr<BlockHeader> createBlockHeader(Json jsonObject);
unique_ptr<FinalHead> createFinalHead(Json jsonObject);
unique_ptr<SystemHealth> createSystemHealth(Json jsonObject);
template <typename T, unique_ptr<T> (CPolkaApi::*F)(Json)> unique_ptr<T> deserialize(Json jsonObject);
Hasher getFuncHasher(unique_ptr<Metadata> &meta, const string &moduleName, const string &funcName);
int getModuleIndex(unique_ptr<Metadata> &meta, const string &moduleName, bool skipZeroCalls);
Expand Down Expand Up @@ -84,6 +85,7 @@ class CPolkaApi : public IApplication, IWebSocketMessageObserver {
virtual unique_ptr<SignedBlock> getBlock(unique_ptr<GetBlockParams> params);
virtual unique_ptr<BlockHeader> getBlockHeader(unique_ptr<GetBlockParams> params);
virtual unique_ptr<FinalHead> getFinalizedHead();
virtual unique_ptr<SystemHealth> getSystemHealth();
virtual unsigned long getAccountNonce(string address);

virtual void signAndSendTransfer(string sender, string privateKey, string recipient, unsigned __int128 amount,
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/iapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class IApplication {
*/
virtual unique_ptr<FinalHead> getFinalizedHead() = 0;

/**
* Return health status of the node
*
* @return SystemHealth struct with result
*/
virtual unique_ptr<SystemHealth> getSystemHealth() = 0;

/**
* Retreives the current nonce for specific address
*
Expand Down
1 change: 0 additions & 1 deletion src/polkacpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ extern "C" {
#include "structs/hasher.h"
#include "structs/signaturepayload.h"


#include "interfaces/ilogger.h"
#include "interfaces/imessageobserver.h"
#include "interfaces/iwsmessageobserver.h"
Expand Down
6 changes: 6 additions & 0 deletions src/structs/systeminfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ struct SystemInfo {
int tokenDecimals;
char tokenSymbol[STRING_SIZE];
};

struct SystemHealth{
long peers;
bool isSyncing;
bool shouldHavePeers;
};
20 changes: 20 additions & 0 deletions test/get_systemhealth_ok.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../src/polkadot.h"
#include "helpers/mockjsonrpc.h"
#undef NDEBUG
#include <cassert>

int main(int argc, char *argv[]) {

auto app = polkadot::api::getInstance()->app();
app->connect();

auto resp3 = app->getSystemHealth();

assert(resp3->peers > 0);

app->disconnect();

cout << "success" << endl;

return 0;
}

0 comments on commit ca57022

Please sign in to comment.