diff --git a/bin/start-libbitcoind.js b/bin/start-libbitcoind.js index 23511601..a6299a33 100644 --- a/bin/start-libbitcoind.js +++ b/bin/start-libbitcoind.js @@ -12,7 +12,7 @@ process.title = 'libbitcoind'; */ var daemon = require('../').daemon({ datadir: process.env.BITCORENODE_DIR || '~/.bitcoin', - network: process.env.BITCORENODE_NETWORK || 'testnet' + network: process.env.BITCORENODE_NETWORK || 'livenet' }); daemon.on('ready', function() { diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index c243c763..6aaec133 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -30,7 +30,7 @@ extern int64_t nTimeBestReceived; */ static void -txmon(uv_async_t *handle); +tx_notifier(uv_async_t *handle); static void async_tip_update(uv_work_t *req); @@ -81,7 +81,10 @@ static void async_get_tx_and_info_after(uv_work_t *req); static bool -process_messages(CNode* pfrom); +scan_messages(CNode* pfrom); + +static bool +scan_messages_after(CNode* pfrom); extern "C" void init(Handle); @@ -90,7 +93,7 @@ init(Handle); * Private Global Variables * Used only by bitcoindjs functions. */ -static std::vector txmon_messages; +static std::vector txmon_messages; static uv_async_t txmon_async; static Eternal txmon_callback; @@ -222,15 +225,16 @@ NAN_METHOD(StartTxMon) { txmon_callback = cb; CNodeSignals& nodeSignals = GetNodeSignals(); - nodeSignals.ProcessMessages.connect(&process_messages); + nodeSignals.ProcessMessages.connect(&scan_messages, boost::signals2::at_front); + nodeSignals.ProcessMessages.connect(&scan_messages_after, boost::signals2::at_back); - uv_async_init(uv_default_loop(), &txmon_async, txmon); + uv_async_init(uv_default_loop(), &txmon_async, tx_notifier); NanReturnValue(Undefined(isolate)); }; static void -txmon(uv_async_t *handle) { +tx_notifier(uv_async_t *handle) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); @@ -241,8 +245,32 @@ txmon(uv_async_t *handle) { Local results = Array::New(isolate); int arrayIndex = 0; - BOOST_FOREACH(const std::string& message, txmon_messages) { - results->Set(arrayIndex, NanNew(message)); + BOOST_FOREACH(CDataStream& vRecvCopy, txmon_messages) { + + std::string vRecvStr = vRecvCopy.str(); + + Local txBuffer = node::Buffer::New(isolate, vRecvStr.c_str(), vRecvStr.size()); + + CTransaction tx; + vRecvCopy >> tx; + uint256 hash = tx.GetHash(); + + Local obj = NanNew(); + + bool existsInMempool = false; + + CTransaction mtx; + + if (mempool.lookup(hash, mtx)) + { + existsInMempool = true; + } + + obj->Set(NanNew("buffer"), txBuffer); + obj->Set(NanNew("hash"), NanNew(hash.GetHex())); + obj->Set(NanNew("mempool"), NanNew(existsInMempool)); + + results->Set(arrayIndex, obj); arrayIndex++; } @@ -262,7 +290,15 @@ txmon(uv_async_t *handle) { } static bool -process_messages(CNode* pfrom) { +scan_messages_after(CNode* pfrom) { + if(txmon_messages.size() > 0) { + uv_async_send(&txmon_async); + } + return true; +} + +static bool +scan_messages(CNode* pfrom) { bool fOk = true; @@ -312,15 +348,12 @@ process_messages(CNode* pfrom) { continue; } - CTransaction tx; - vRecv >> tx; - - string txHash = tx.GetHash().GetHex(); + // Copy the stream so that it can later be processed into the mempool + CDataStream vRecvCopy(vRecv.begin(), vRecv.end(), vRecv.GetType(), vRecv.GetVersion()); { LOCK(cs_main); - txmon_messages.push_back(txHash); - uv_async_send(&txmon_async); + txmon_messages.push_back(vRecvCopy); } }