Fix getbalance() bug
[novacoin.git] / src / rpcnet.cpp
index c1f3b06..3f9b46d 100644 (file)
@@ -9,6 +9,7 @@
 #include "walletdb.h"
 #include "net.h"
 #include "ntp.h"
+#include "timedata.h"
 
 using namespace json_spirit;
 using namespace std;
@@ -157,7 +158,7 @@ Value addnode(const Array& params, bool fHelp)
     }
 
     LOCK(cs_vAddedNodes);
-    vector<string>::iterator it = vAddedNodes.begin();
+    auto it = vAddedNodes.begin();
     for(; it != vAddedNodes.end(); it++)
         if (strNode == *it)
             break;
@@ -194,14 +195,14 @@ Value getaddednodeinfo(const Array& params, bool fHelp)
     if (params.size() == 1)
     {
         LOCK(cs_vAddedNodes);
-        for(string& strAddNode :  vAddedNodes)
+        for(auto& strAddNode :  vAddedNodes)
             laddedNodes.push_back(strAddNode);
     }
     else
     {
         string strNode = params[1].get_str();
         LOCK(cs_vAddedNodes);
-        for(string& strAddNode :  vAddedNodes)
+        for(auto& strAddNode :  vAddedNodes)
             if (strAddNode == strNode)
             {
                 laddedNodes.push_back(strAddNode);
@@ -222,7 +223,7 @@ Value getaddednodeinfo(const Array& params, bool fHelp)
         Array ret;
 
         list<pair<string, vector<CService> > > laddedAddreses(0);
-        for(string& strAddNode :  laddedNodes)
+        for(auto& strAddNode :  laddedNodes)
         {
             vector<CService> vservNode(0);
             if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0))
@@ -238,7 +239,7 @@ Value getaddednodeinfo(const Array& params, bool fHelp)
         }
 
     LOCK(cs_vNodes);
-    for (list<pair<string, vector<CService> > >::iterator it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
+    for (auto it = laddedAddreses.begin(); it != laddedAddreses.end(); it++)
     {
         Object obj;
         obj.push_back(Pair("addednode", it->first));
@@ -305,7 +306,7 @@ Value sendalert(const Array& params, bool fHelp)
     sMsg << (CUnsignedAlert)alert;
     alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end());
 
-    vector<unsigned char> vchPrivKey = ParseHex(params[1].get_str());
+    auto vchPrivKey = ParseHex(params[1].get_str());
     key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
     if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig))
         throw runtime_error(
@@ -347,28 +348,6 @@ Value getnettotals(const Array& params, bool fHelp)
     return obj;
 }
 
-/*
-05:53:45 ntptime
-05:53:48
-{
-"epoch" : 1442494427,
-"time" : "2015-09-17 12:53:47 UTC"
-}
-
-05:53:56 ntptime time.windows.com
-05:53:57
-{
-"epoch" : 1442494436,
-"time" : "2015-09-17 12:53:56 UTC"
-}
-
-05:54:33 ntptime time-a.nist.gov
-05:54:34
-{
-"epoch" : 1442494473,
-"time" : "2015-09-17 12:54:33 UTC"
-}*/
-
 Value ntptime(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() > 1)
@@ -406,3 +385,49 @@ Value ntptime(const Array& params, bool fHelp)
 
     return obj;
 }
+
+Value getnetworkinfo(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 0)
+        throw runtime_error(
+            "getnetworkinfo\n"
+            "Returns an object containing various state info regarding P2P networking.\n"
+            "\nResult:\n"
+            "{\n"
+            "  \"version\": xxxxx,           (numeric) the server version\n"
+            "  \"protocolversion\": xxxxx,   (numeric) the protocol version\n"
+            "  \"timeoffset\": xxxxx,        (numeric) the time offset\n"
+            "  \"connections\": xxxxx,       (numeric) the number of connections\n"
+            "  \"proxy\": \"host:port\",     (string, optional) the proxy used by the server\n"
+            "  \"localaddresses\": [,        (array) list of local addresses\n"
+            "    \"address\": \"xxxx\",      (string) network address\n"
+            "    \"port\": xxx,              (numeric) network port\n"
+            "    \"score\": xxx              (numeric) relative score\n"
+            "  ]\n"
+            "}\n"
+        );
+
+    proxyType proxy;
+    GetProxy(NET_IPV4, proxy);
+
+    Object obj;
+    obj.push_back(Pair("version",         CLIENT_VERSION));
+    obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
+    obj.push_back(Pair("timeoffset",      GetTimeOffset()));
+    obj.push_back(Pair("connections",     (int)vNodes.size()));
+    obj.push_back(Pair("proxy",           (proxy.IsValid() ? proxy.ToStringIPPort() : string())));
+    Array localAddresses;
+    {
+        LOCK(cs_mapLocalHost);
+        for(const auto &item : mapLocalHost)
+        {
+            Object rec;
+            rec.push_back(Pair("address", item.first.ToString()));
+            rec.push_back(Pair("port", (int)item.second.nPort));
+            rec.push_back(Pair("score", item.second.nScore));
+            localAddresses.push_back(rec);
+        }
+    }
+    obj.push_back(Pair("localaddresses", localAddresses));
+    return obj;
+}