From 19867a365234a17f46dffbba2329fd9adc48ce16 Mon Sep 17 00:00:00 2001 From: goldenman-kmd <33863358+goldenman-kmd@users.noreply.github.com> Date: Tue, 1 May 2018 01:34:12 +0900 Subject: [PATCH] Create minedBlockCounter.cpp --- .../MinedBlockCounter/minedBlockCounter.cpp | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 goldenman/MinedBlockCounter/minedBlockCounter.cpp diff --git a/goldenman/MinedBlockCounter/minedBlockCounter.cpp b/goldenman/MinedBlockCounter/minedBlockCounter.cpp new file mode 100644 index 0000000..f964b4f --- /dev/null +++ b/goldenman/MinedBlockCounter/minedBlockCounter.cpp @@ -0,0 +1,78 @@ +/**** + * Please read READMD.md + * + * install lib : $ sudo apt-get install libjsoncpp-dev + * + * how to compile : + * $ g++ -o exefileName sourceFile.cpp -ljsoncpp + * ex) $ g++ -o nodeMined minedBlockCounter.cpp -ljsoncpp + * + * how to run + * $ ./nodeMined + */ + +#include +#include +#include +#include +#include /* printf */ +#include +#include + +using namespace std; + +string myExec(const char* cmd) { + char buffer[512]; + std::string result = ""; + FILE* pipe = popen(cmd, "r"); + if (!pipe) throw std::runtime_error("popen() failed!"); + try { + while (!feof(pipe)) { + if (fgets(buffer, 128, pipe) != NULL) + result += buffer; + } + } catch (...) { + pclose(pipe); + throw; + } + pclose(pipe); + return result; +} + +int main() { + + Json::Reader reader; + Json::Value obj; + + cout << "Loading blockchain info...please wait..." << endl; + + string jsonOutput = myExec("~/komodo/src/komodo-cli listsinceblock 01d2c8f63c0c4b0da415a928a94f05b8c1a6070d092e3800ab8bbb37f36b842d"); // since block 814000 + reader.parse(jsonOutput, obj); // Reader can also read strings + + int size = obj["transactions"].size(); + int i=0, j=1; + + double total = 0; + + cout << fixed; + cout.precision(8); + + for (i=0; i< size; i++) + { + if (obj["transactions"][i]["generated"].asBool() == false) { + // cout << "generated : false" << endl; + continue; + } + + double amountIn = obj["transactions"][i]["amount"].asDouble();; + string hash = obj["transactions"][i]["blockhash"].asString(); + + cout << j << " - blockHash : " << hash << "\tAmount : " << amountIn << endl; + + total += amountIn; + j++; + } + + cout << "Total : " << total << endl; + return 1; +}