From 965d4198011a507379155668a2e0bf50f8305d54 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 17 Oct 2014 13:47:56 -0700 Subject: [PATCH] add GetPeerInfo(). fix GetInfo(). --- example/index.js | 4 +- lib/bitcoind.js | 4 ++ src/bitcoindjs.cc | 94 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 84 insertions(+), 18 deletions(-) diff --git a/example/index.js b/example/index.js index 4fb5c301..db844032 100755 --- a/example/index.js +++ b/example/index.js @@ -45,8 +45,10 @@ bitcoind.on('open', function(status) { argv['on-block'] = true; setTimeout(function() { argv['on-block'] = false; + print(bitcoind.getInfo()); + print(bitcoind.getPeerInfo()); print(bitcoind.wallet.listAccounts()); - }, 5000); + }, 7000); } if (argv.list) { diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 52caf035..6f92faa0 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -386,6 +386,10 @@ Bitcoin.prototype.getInfo = function() { return bitcoindjs.getInfo(); }; +Bitcoin.prototype.getPeerInfo = function() { + return bitcoindjs.getPeerInfo(); +}; + Bitcoin.prototype.log = Bitcoin.prototype.info = function() { if (typeof arguments[0] !== 'string') { diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index ed3cbde7..e14eafd5 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -167,6 +167,7 @@ NAN_METHOD(VerifyBlock); NAN_METHOD(VerifyTransaction); NAN_METHOD(FillTransaction); NAN_METHOD(GetInfo); +NAN_METHOD(GetPeerInfo); NAN_METHOD(GetBlockHex); NAN_METHOD(GetTxHex); NAN_METHOD(BlockFromHex); @@ -1491,41 +1492,99 @@ NAN_METHOD(GetInfo) { "Usage: bitcoindjs.getInfo()"); } - Local obj = NanNew(); + Local obj = NanNew(); proxyType proxy; GetProxy(NET_IPV4, proxy); - obj->Set("version", NanNew(CLIENT_VERSION)); - obj->Set("protocolversion", NanNew(PROTOCOL_VERSION)); + obj->Set(NanNew("version"), NanNew(CLIENT_VERSION)); + obj->Set(NanNew("protocolversion"), NanNew(PROTOCOL_VERSION)); #ifdef ENABLE_WALLET if (pwalletMain) { - obj->Set("walletversion", NanNewGetVersion())); - obj->Set("balance", NanNew(pwalletMain->GetBalance())); // double + obj->Set(NanNew("walletversion"), NanNewGetVersion())); + obj->Set(NanNew("balance"), NanNew(pwalletMain->GetBalance())); // double } #endif - obj->Set("blocks", NanNew((int)chainActive.Height())->ToInt32()); - obj->Set("timeoffset", NanNew(GetTimeOffset())); - obj->Set("connections", NanNew((int)vNodes.size())->ToInt32()); - obj->Set("proxy", NanNew(proxy.IsValid() ? proxy.ToStringIPPort() : std::string(""))); - obj->Set("difficulty", NanNew((double)GetDifficulty())); - obj->Set("testnet", NanNew(Params().NetworkID() == CBaseChainParams::TESTNET)); + obj->Set(NanNew("blocks"), NanNew((int)chainActive.Height())->ToInt32()); + obj->Set(NanNew("timeoffset"), NanNew(GetTimeOffset())); + obj->Set(NanNew("connections"), NanNew((int)vNodes.size())->ToInt32()); + obj->Set(NanNew("proxy", NanNew(proxy.IsValid() ? proxy.ToStringIPPort() : std::string("")))); + obj->Set(NanNew("difficulty"), NanNew((double)GetDifficulty())); + obj->Set(NanNew("testnet"), NanNew(Params().NetworkID() == CBaseChainParams::TESTNET)); #ifdef ENABLE_WALLET if (pwalletMain) { - obj->Set("keypoololdest", NanNew(pwalletMain->GetOldestKeyPoolTime())); - obj->Set("keypoolsize", NanNew((int)pwalletMain->GetKeyPoolSize())->ToInt32()); + obj->Set(NanNew("keypoololdest"), NanNew(pwalletMain->GetOldestKeyPoolTime())); + obj->Set(NanNew("keypoolsize"), NanNew((int)pwalletMain->GetKeyPoolSize())->ToInt32()); } if (pwalletMain && pwalletMain->IsCrypted()) { - obj->Set("unlocked_until", NanNew(nWalletUnlockTime)); + obj->Set(NanNew("unlocked_until"), NanNew(nWalletUnlockTime)); } - obj->Set("paytxfee", NanNew(payTxFee.GetFeePerK())); // double + obj->Set(NanNew("paytxfee"), NanNew(payTxFee.GetFeePerK())); // double #endif - obj->Set("relayfee", NanNew(::minRelayTxFee.GetFeePerK())); // double - obj->Set("errors", NanNew(GetWarnings("statusbar"))); + obj->Set(NanNew("relayfee"), NanNew(::minRelayTxFee.GetFeePerK())); // double + obj->Set(NanNew("errors", NanNew(GetWarnings("statusbar")))); NanReturnValue(obj); } +/** + * GetPeerInfo() + * bitcoindjs.GetPeerInfo() + * Get peer information + */ + +NAN_METHOD(GetPeerInfo) { + NanScope(); + + if (args.Length() > 0) { + return NanThrowError( + "Usage: bitcoindjs.getPeerInfo()"); + } + + Local array = NanNew(); + int i = 0; + + vector vstats; + CopyNodeStats(vstats); + + BOOST_FOREACH(const CNodeStats& stats, vstats) { + Local obj = NanNew(); + + CNodeStateStats statestats; + bool fStateStats = GetNodeStateStats(stats.nodeid, statestats); + obj->Set(NanNew("id"), NanNew(stats.nodeid)); + obj->Set(NanNew("addr"), NanNew(stats.addrName)); + if (!(stats.addrLocal.empty())) { + obj->Set(NanNew("addrlocal"), NanNew(stats.addrLocal)); + } + obj->Set(NanNew("services"), NanNew(strprintf("%016x", stats.nServices))); + obj->Set(NanNew("lastsend"), NanNew(stats.nLastSend)); + obj->Set(NanNew("lastrecv"), NanNew(stats.nLastRecv)); + obj->Set(NanNew("bytessent"), NanNew(stats.nSendBytes)); + obj->Set(NanNew("bytesrecv"), NanNew(stats.nRecvBytes)); + obj->Set(NanNew("conntime"), NanNew(stats.nTimeConnected)); + obj->Set(NanNew("pingtime"), NanNew(stats.dPingTime)); // double + if (stats.dPingWait > 0.0) { + obj->Set(NanNew("pingwait"), NanNew(stats.dPingWait)); // double + } + obj->Set(NanNew("version"), NanNew(stats.nVersion)); + obj->Set(NanNew("subver"), NanNew(stats.cleanSubVer)); + obj->Set(NanNew("inbound"), NanNew(stats.fInbound)); + obj->Set(NanNew("startingheight"), NanNew(stats.nStartingHeight)); + if (fStateStats) { + obj->Set(NanNew("banscore"), NanNew(statestats.nMisbehavior)); + obj->Set(NanNew("syncheight"), NanNew(statestats.nSyncHeight)); + } + obj->Set(NanNew("syncnode"), NanNew(stats.fSyncNode)); + obj->Set(NanNew("whitelisted"), NanNew(stats.fWhitelisted)); + + array->Set(i, obj); + i++; + } + + NanReturnValue(array); +} + /** * GetBlockHex() * bitcoindjs.getBlockHex(callback) @@ -3224,6 +3283,7 @@ init(Handle target) { NODE_SET_METHOD(target, "verifyTransaction", VerifyTransaction); NODE_SET_METHOD(target, "fillTransaction", FillTransaction); NODE_SET_METHOD(target, "getInfo", GetInfo); + NODE_SET_METHOD(target, "getPeerInfo", GetPeerInfo); NODE_SET_METHOD(target, "getBlockHex", GetBlockHex); NODE_SET_METHOD(target, "getTxHex", GetTxHex); NODE_SET_METHOD(target, "blockFromHex", BlockFromHex);