Merge pull request #110 from braydonf/txsignal

Fix bug with transaction message processing
This commit is contained in:
Chris Kleeschulte 2015-08-07 12:51:18 -04:00
commit 0307919544
2 changed files with 49 additions and 16 deletions

View File

@ -12,7 +12,7 @@ process.title = 'libbitcoind';
*/ */
var daemon = require('../').daemon({ var daemon = require('../').daemon({
datadir: process.env.BITCORENODE_DIR || '~/.bitcoin', datadir: process.env.BITCORENODE_DIR || '~/.bitcoin',
network: process.env.BITCORENODE_NETWORK || 'testnet' network: process.env.BITCORENODE_NETWORK || 'livenet'
}); });
daemon.on('ready', function() { daemon.on('ready', function() {

View File

@ -30,7 +30,7 @@ extern int64_t nTimeBestReceived;
*/ */
static void static void
txmon(uv_async_t *handle); tx_notifier(uv_async_t *handle);
static void static void
async_tip_update(uv_work_t *req); async_tip_update(uv_work_t *req);
@ -81,7 +81,10 @@ static void
async_get_tx_and_info_after(uv_work_t *req); async_get_tx_and_info_after(uv_work_t *req);
static bool static bool
process_messages(CNode* pfrom); scan_messages(CNode* pfrom);
static bool
scan_messages_after(CNode* pfrom);
extern "C" void extern "C" void
init(Handle<Object>); init(Handle<Object>);
@ -90,7 +93,7 @@ init(Handle<Object>);
* Private Global Variables * Private Global Variables
* Used only by bitcoindjs functions. * Used only by bitcoindjs functions.
*/ */
static std::vector<std::string> txmon_messages; static std::vector<CDataStream> txmon_messages;
static uv_async_t txmon_async; static uv_async_t txmon_async;
static Eternal<Function> txmon_callback; static Eternal<Function> txmon_callback;
@ -222,15 +225,16 @@ NAN_METHOD(StartTxMon) {
txmon_callback = cb; txmon_callback = cb;
CNodeSignals& nodeSignals = GetNodeSignals(); 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)); NanReturnValue(Undefined(isolate));
}; };
static void static void
txmon(uv_async_t *handle) { tx_notifier(uv_async_t *handle) {
Isolate* isolate = Isolate::GetCurrent(); Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate); HandleScope scope(isolate);
@ -241,8 +245,32 @@ txmon(uv_async_t *handle) {
Local<Array> results = Array::New(isolate); Local<Array> results = Array::New(isolate);
int arrayIndex = 0; int arrayIndex = 0;
BOOST_FOREACH(const std::string& message, txmon_messages) { BOOST_FOREACH(CDataStream& vRecvCopy, txmon_messages) {
results->Set(arrayIndex, NanNew<String>(message));
std::string vRecvStr = vRecvCopy.str();
Local<Value> txBuffer = node::Buffer::New(isolate, vRecvStr.c_str(), vRecvStr.size());
CTransaction tx;
vRecvCopy >> tx;
uint256 hash = tx.GetHash();
Local<Object> obj = NanNew<Object>();
bool existsInMempool = false;
CTransaction mtx;
if (mempool.lookup(hash, mtx))
{
existsInMempool = true;
}
obj->Set(NanNew<String>("buffer"), txBuffer);
obj->Set(NanNew<String>("hash"), NanNew<String>(hash.GetHex()));
obj->Set(NanNew<String>("mempool"), NanNew<Boolean>(existsInMempool));
results->Set(arrayIndex, obj);
arrayIndex++; arrayIndex++;
} }
@ -262,7 +290,15 @@ txmon(uv_async_t *handle) {
} }
static bool 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; bool fOk = true;
@ -312,15 +348,12 @@ process_messages(CNode* pfrom) {
continue; continue;
} }
CTransaction tx; // Copy the stream so that it can later be processed into the mempool
vRecv >> tx; CDataStream vRecvCopy(vRecv.begin(), vRecv.end(), vRecv.GetType(), vRecv.GetVersion());
string txHash = tx.GetHash().GetHex();
{ {
LOCK(cs_main); LOCK(cs_main);
txmon_messages.push_back(txHash); txmon_messages.push_back(vRecvCopy);
uv_async_send(&txmon_async);
} }
} }