diff --git a/package.json b/package.json index d3e2990b..bc517cb1 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "liftoff": "^2.1.0", "memdown": "^1.0.0", "mkdirp": "0.5.0", - "nan": "1.3.0", + "nan": "^2.0.9", "npm": "^2.14.1", "semver": "^5.0.1", "socket.io": "^1.3.6", diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index 6fefa711..9d3838dc 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -12,6 +12,13 @@ using namespace std; using namespace boost; using namespace node; using namespace v8; +using Nan::New; +using Nan::Null; +using Nan::Set; +using Nan::ThrowError; +using Nan::GetCurrentContext; +using Nan::GetFunction; +using v8::FunctionTemplate; /** * Bitcoin Globals @@ -197,50 +204,43 @@ NAN_METHOD(SyncPercentage) { const CChainParams& chainParams = Params(); float progress = 0; progress = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()); - NanReturnValue(NanNew(progress * 100)); + info.GetReturnValue().Set(progress * 100); }; NAN_METHOD(GetTxOutSetInfo) { - Isolate* isolate = args.GetIsolate(); - HandleScope scope(isolate); { - LOCK(cs_main); - CCoinsStats stats; FlushStateToDisk(); if (pcoinsTip->GetStats(stats)) { - Local obj = NanNew(); - obj->Set(NanNew("height"), NanNew((int64_t)stats.nHeight)); - obj->Set(NanNew("bestblock"), NanNew(stats.hashBlock.GetHex())); - obj->Set(NanNew("transactions"), NanNew((int64_t)stats.nTransactions)); - obj->Set(NanNew("txouts"), NanNew((int64_t)stats.nTransactionOutputs)); - obj->Set(NanNew("bytes_serialized"), NanNew((int64_t)stats.nSerializedSize)); - obj->Set(NanNew("hash_serialized"), NanNew(stats.hashSerialized.GetHex())); - obj->Set(NanNew("total_amount"), NanNew(stats.nTotalAmount)); - NanReturnValue(obj); + Local obj = New(); + Set(obj, New("height").ToLocalChecked(), New((int64_t)stats.nHeight)); + Set(obj, New("bestblock").ToLocalChecked(), New(stats.hashBlock.GetHex().c_str()).ToLocalChecked()); + Set(obj, New("transactions").ToLocalChecked(), New((int64_t)stats.nTransactions)); + Set(obj, New("txouts").ToLocalChecked(), New((int64_t)stats.nTransactionOutputs)); + Set(obj, New("bytes_serialized").ToLocalChecked(), New((int64_t)stats.nSerializedSize)); + Set(obj, New("hash_serialized").ToLocalChecked(), New(stats.hashSerialized.GetHex()).ToLocalChecked()); + Set(obj, New("total_amount").ToLocalChecked(), New(stats.nTotalAmount)); + info.GetReturnValue().Set(obj); + return; } } - - NanReturnValue(NanNull()); - + info.GetReturnValue().Set(Null()); }; NAN_METHOD(GetBestBlockHash) { - { - LOCK(cs_main); - NanReturnValue(NanNew(chainActive.Tip()->GetBlockHash().GetHex())); - } + LOCK(cs_main); + info.GetReturnValue().Set(New(chainActive.Tip()->GetBlockHash().GetHex()).ToLocalChecked()); } NAN_METHOD(GetNextBlockHash) { - if (args.Length() < 1 || !args[0]->IsString()) { - return NanThrowError("Usage: bitcoind.getNextBlockHash(blockhash)"); + if (info.Length() < 1 || !info[0]->IsString()) { + return ThrowError("Usage: bitcoind.getNextBlockHash(blockhash)"); } CBlockIndex* pblockindex; - v8::String::Utf8Value param1(args[0]->ToString()); + v8::String::Utf8Value param1(info[0]->ToString()); std::string *hash = new std::string(*param1); uint256 shash = uint256S(*hash); pblockindex = mapBlockIndex[shash]; @@ -248,9 +248,9 @@ NAN_METHOD(GetNextBlockHash) { if (pnextblockindex) { uint256 nexthash = pnextblockindex->GetBlockHash(); std::string rethash = nexthash.ToString(); - NanReturnValue(NanNew(rethash)); + info.GetReturnValue().Set(New(rethash).ToLocalChecked()); } else { - NanReturnValue(NanNull()); + info.GetReturnValue().Set(Null()); } } @@ -262,14 +262,12 @@ NAN_METHOD(GetNextBlockHash) { */ NAN_METHOD(IsSynced) { bool isDownloading = IsInitialBlockDownload(); - NanReturnValue(NanNew(!isDownloading)); + info.GetReturnValue().Set(New(!isDownloading)); }; NAN_METHOD(StartTxMon) { - Isolate* isolate = Isolate::GetCurrent(); - HandleScope scope(isolate); - - Local callback = Local::Cast(args[0]); + Isolate* isolate = info.GetIsolate(); + Local callback = Local::Cast(info[0]); Eternal cb(isolate, callback); txmon_callback = cb; txmon_callback_available = true; @@ -279,12 +277,12 @@ NAN_METHOD(StartTxMon) { uv_async_init(uv_default_loop(), &txmon_async, tx_notifier); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); }; static void tx_notifier(uv_async_t *handle) { - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = GetCurrentContext()->GetIsolate(); HandleScope scope(isolate); Local results = Array::New(isolate); @@ -300,11 +298,11 @@ tx_notifier(uv_async_t *handle) { uint256 hash = tx.GetHash(); - Local obj = NanNew(); + Local obj = New(); - obj->Set(NanNew("buffer"), txBuffer); - obj->Set(NanNew("hash"), NanNew(hash.GetHex())); - obj->Set(NanNew("mempool"), NanNew(true)); + Set(obj, New("buffer").ToLocalChecked(), txBuffer); + Set(obj, New("hash").ToLocalChecked(), New(hash.GetHex()).ToLocalChecked()); + Set(obj, New("mempool").ToLocalChecked(), New(true)); results->Set(arrayIndex, obj); arrayIndex++; @@ -335,12 +333,12 @@ queueTx(const CTransaction& tx) { */ NAN_METHOD(OnTipUpdate) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); async_tip_update_data *req = new async_tip_update_data(); - Local callback = Local::Cast(args[0]); + Local callback = Local::Cast(info[0]); req->callback.Reset(isolate, callback); req->req.data = req; req->isolate = isolate; @@ -351,8 +349,7 @@ NAN_METHOD(OnTipUpdate) { assert(status == 0); - NanReturnValue(Undefined(isolate)); - + info.GetReturnValue().Set(Null()); } static void @@ -380,7 +377,7 @@ async_tip_update_after(uv_work_t *r) { Local result = Undefined(isolate); if (!shutdown_complete) { - result = NanNew(req->result); + result = New(req->result); } Local argv[1] = { Local::New(isolate, result) @@ -395,7 +392,7 @@ async_tip_update_after(uv_work_t *r) { } NAN_METHOD(OnBlocksReady) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); async_block_ready_data *req = new async_block_ready_data(); @@ -404,7 +401,7 @@ NAN_METHOD(OnBlocksReady) { req->req.data = req; req->isolate = isolate; - Local callback = Local::Cast(args[0]); + Local callback = Local::Cast(info[0]); req->callback.Reset(isolate, callback); int status = uv_queue_work(uv_default_loop(), @@ -413,7 +410,7 @@ NAN_METHOD(OnBlocksReady) { assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } /** @@ -471,13 +468,13 @@ async_blocks_ready_after(uv_work_t *r) { Local cb = Local::New(isolate, req->callback); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { Local argv[2] = { v8::Null(isolate), - Local::New(isolate, NanNew(req->result)) + Local::New(isolate, New(req->result).ToLocalChecked()) }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); } @@ -496,7 +493,7 @@ async_blocks_ready_after(uv_work_t *r) { * Start the bitcoind node with AppInit2() on a separate thread. */ NAN_METHOD(StartBitcoind) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); Local callback; @@ -506,17 +503,17 @@ NAN_METHOD(StartBitcoind) { bool regtest = false; bool txindex = false; - if (args.Length() >= 2 && args[0]->IsObject() && args[1]->IsFunction()) { - Local options = Local::Cast(args[0]); - if (options->Get(NanNew("datadir"))->IsString()) { - String::Utf8Value datadir_(options->Get(NanNew("datadir"))->ToString()); + if (info.Length() >= 2 && info[0]->IsObject() && info[1]->IsFunction()) { + Local options = Local::Cast(info[0]); + if (options->Get(New("datadir").ToLocalChecked())->IsString()) { + String::Utf8Value datadir_(options->Get(New("datadir").ToLocalChecked())->ToString()); datadir = std::string(*datadir_); } - if (options->Get(NanNew("rpc"))->IsBoolean()) { - rpc = options->Get(NanNew("rpc"))->ToBoolean()->IsTrue(); + if (options->Get(New("rpc").ToLocalChecked())->IsBoolean()) { + rpc = options->Get(New("rpc").ToLocalChecked())->ToBoolean()->IsTrue(); } - if (options->Get(NanNew("network"))->IsString()) { - String::Utf8Value network_(options->Get(NanNew("network"))->ToString()); + if (options->Get(New("network").ToLocalChecked())->IsString()) { + String::Utf8Value network_(options->Get(New("network").ToLocalChecked())->ToString()); std::string network = std::string(*network_); if (network == "testnet") { testnet = true; @@ -524,18 +521,18 @@ NAN_METHOD(StartBitcoind) { regtest = true; } } - if (options->Get(NanNew("txindex"))->IsBoolean()) { - txindex = options->Get(NanNew("txindex"))->ToBoolean()->IsTrue(); + if (options->Get(New("txindex").ToLocalChecked())->IsBoolean()) { + txindex = options->Get(New("txindex").ToLocalChecked())->ToBoolean()->IsTrue(); } - callback = Local::Cast(args[1]); - } else if (args.Length() >= 2 - && (args[0]->IsUndefined() || args[0]->IsNull()) - && args[1]->IsFunction()) { - callback = Local::Cast(args[1]); - } else if (args.Length() >= 1 && args[0]->IsFunction()) { - callback = Local::Cast(args[0]); + callback = Local::Cast(info[1]); + } else if (info.Length() >= 2 + && (info[0]->IsUndefined() || info[0]->IsNull()) + && info[1]->IsFunction()) { + callback = Local::Cast(info[1]); + } else if (info.Length() >= 1 && info[0]->IsFunction()) { + callback = Local::Cast(info[0]); } else { - return NanThrowError( + return ThrowError( "Usage: bitcoind.start(callback)"); } @@ -562,7 +559,7 @@ NAN_METHOD(StartBitcoind) { assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } /** @@ -603,13 +600,13 @@ async_start_node_after(uv_work_t *r) { Local cb = Local::New(isolate, req->callback); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { Local argv[2] = { v8::Null(isolate), - Local::New(isolate, NanNew(req->result)) + Local::New(isolate, New(req->result).ToLocalChecked()) }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); } @@ -754,15 +751,15 @@ start_node_thread(void) { */ NAN_METHOD(StopBitcoind) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); - if (args.Length() < 1 || !args[0]->IsFunction()) { - return NanThrowError( + if (info.Length() < 1 || !info[0]->IsFunction()) { + return ThrowError( "Usage: bitcoind.stop(callback)"); } - Local callback = Local::Cast(args[0]); + Local callback = Local::Cast(info[0]); // // Run bitcoind's StartShutdown() on a separate thread. @@ -780,7 +777,7 @@ NAN_METHOD(StopBitcoind) { (uv_after_work_cb)async_stop_node_after); assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } @@ -817,13 +814,13 @@ async_stop_node_after(uv_work_t *r) { Local cb = Local::New(isolate, req->callback); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { Local argv[2] = { - Local::New(isolate, NanNull()), - Local::New(isolate, NanNew(req->result)) + Local::New(isolate, Null()), + Local::New(isolate, New(req->result).ToLocalChecked()) }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); } @@ -842,31 +839,31 @@ async_stop_node_after(uv_work_t *r) { */ NAN_METHOD(GetBlock) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); - if (args.Length() < 2 - || (!args[0]->IsString() && !args[0]->IsNumber()) - || !args[1]->IsFunction()) { - return NanThrowError( + if (info.Length() < 2 + || (!info[0]->IsString() && !info[0]->IsNumber()) + || !info[1]->IsFunction()) { + return ThrowError( "Usage: bitcoind.getBlock([blockhash,blockheight], callback)"); } async_block_data *req = new async_block_data(); - if (args[0]->IsNumber()) { - int64_t height = args[0]->IntegerValue(); + if (info[0]->IsNumber()) { + int64_t height = info[0]->IntegerValue(); req->err_msg = std::string(""); req->hash = std::string(""); req->height = height; } else { - String::Utf8Value hash_(args[0]->ToString()); + String::Utf8Value hash_(info[0]->ToString()); std::string hash = std::string(*hash_); req->err_msg = std::string(""); req->hash = hash; req->height = -1; } - Local callback = Local::Cast(args[1]); + Local callback = Local::Cast(info[1]); req->req.data = req; req->isolate = isolate; req->callback.Reset(isolate, callback); @@ -877,7 +874,7 @@ NAN_METHOD(GetBlock) { assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } static void @@ -945,7 +942,7 @@ async_get_block_after(uv_work_t *r) { Local cb = Local::New(isolate, req->callback); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { @@ -956,7 +953,7 @@ async_get_block_after(uv_work_t *r) { req->buffer = NULL; Local argv[2] = { - Local::New(isolate, NanNull()), + Local::New(isolate, Null()), rawNodeBuffer }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); @@ -977,19 +974,19 @@ async_get_block_after(uv_work_t *r) { */ NAN_METHOD(GetTransaction) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); - if (args.Length() < 3 - || !args[0]->IsString() - || !args[1]->IsBoolean() - || !args[2]->IsFunction()) { - return NanThrowError( + if (info.Length() < 3 + || !info[0]->IsString() + || !info[1]->IsBoolean() + || !info[2]->IsFunction()) { + return ThrowError( "Usage: daemon.getTransaction(txid, queryMempool, callback)"); } - String::Utf8Value txid_(args[0]->ToString()); - bool queryMempool = args[1]->BooleanValue(); - Local callback = Local::Cast(args[2]); + String::Utf8Value txid_(info[0]->ToString()); + bool queryMempool = info[1]->BooleanValue(); + Local callback = Local::Cast(info[2]); async_tx_data *req = new async_tx_data(); @@ -1010,7 +1007,7 @@ NAN_METHOD(GetTransaction) { assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } static void @@ -1068,12 +1065,12 @@ async_get_tx_after(uv_work_t *r) { Local cb = Local::New(isolate, req->callback); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { - Local result = Local::New(isolate, NanNull()); + Local result = Local::New(isolate, Null()); if (!ctx.IsNull()) { CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); @@ -1083,7 +1080,7 @@ async_get_tx_after(uv_work_t *r) { } Local argv[2] = { - Local::New(isolate, NanNull()), + Local::New(isolate, Null()), result }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); @@ -1104,19 +1101,19 @@ async_get_tx_after(uv_work_t *r) { */ NAN_METHOD(GetTransactionWithBlockInfo) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); - if (args.Length() < 3 - || !args[0]->IsString() - || !args[1]->IsBoolean() - || !args[2]->IsFunction()) { - return NanThrowError( + if (info.Length() < 3 + || !info[0]->IsString() + || !info[1]->IsBoolean() + || !info[2]->IsFunction()) { + return ThrowError( "Usage: bitcoind.getTransactionWithBlockInfo(txid, queryMempool, callback)"); } - String::Utf8Value txid_(args[0]->ToString()); - bool queryMempool = args[1]->BooleanValue(); - Local callback = Local::Cast(args[2]); + String::Utf8Value txid_(info[0]->ToString()); + bool queryMempool = info[1]->BooleanValue(); + Local callback = Local::Cast(info[2]); async_tx_data *req = new async_tx_data(); @@ -1137,7 +1134,7 @@ NAN_METHOD(GetTransactionWithBlockInfo) { assert(status == 0); - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } static void @@ -1208,10 +1205,10 @@ async_get_tx_and_info_after(uv_work_t *r) { CTransaction ctx = req->ctx; TryCatch try_catch; Local cb = Local::New(isolate, req->callback); - Local obj = NanNew(); + Local obj = New(); if (req->err_msg != "") { - Local err = Exception::Error(NanNew(req->err_msg)); + Local err = Exception::Error(New(req->err_msg).ToLocalChecked()); Local argv[1] = { err }; cb->Call(isolate->GetCurrentContext()->Global(), 1, argv); } else { @@ -1221,13 +1218,13 @@ async_get_tx_and_info_after(uv_work_t *r) { std::string stx = ssTx.str(); Local rawNodeBuffer = node::Buffer::New(isolate, stx.c_str(), stx.size()); - obj->Set(NanNew("blockHash"), NanNew(req->blockHash)); - obj->Set(NanNew("height"), NanNew(req->height)); - obj->Set(NanNew("timestamp"), NanNew(req->nTime)); - obj->Set(NanNew("buffer"), rawNodeBuffer); + Set(obj, New("blockHash").ToLocalChecked(), New(req->blockHash).ToLocalChecked()); + Set(obj, New("height").ToLocalChecked(), New(req->height)); + Set(obj, New("timestamp").ToLocalChecked(), New(req->nTime)); + Set(obj, New("buffer").ToLocalChecked(), rawNodeBuffer); Local argv[2] = { - Local::New(isolate, NanNull()), + Local::New(isolate, Null()), obj }; cb->Call(isolate->GetCurrentContext()->Global(), 2, argv); @@ -1245,17 +1242,15 @@ async_get_tx_and_info_after(uv_work_t *r) { * Determine if an outpoint is spent */ NAN_METHOD(IsSpent) { - NanScope(); - - if (args.Length() > 2) { - return NanThrowError( + if (info.Length() > 2) { + return ThrowError( "Usage: bitcoind.isSpent(txid, outputIndex)"); } - String::Utf8Value arg(args[0]->ToString()); + String::Utf8Value arg(info[0]->ToString()); std::string argStr = std::string(*arg); const uint256 txid = uint256S(argStr); - int outputIndex = args[1]->IntegerValue(); + int outputIndex = info[1]->IntegerValue(); CCoinsView dummy; CCoinsViewCache view(&dummy); @@ -1266,11 +1261,11 @@ NAN_METHOD(IsSpent) { if (view.HaveCoins(txid)) { const CCoins* coins = view.AccessCoins(txid); if (coins && coins->IsAvailable(outputIndex)) { - NanReturnValue(NanNew(false)); + info.GetReturnValue().Set(New(false)); return; } } - NanReturnValue(NanNew(true)); + info.GetReturnValue().Set(New(true)); }; /** @@ -1287,42 +1282,42 @@ NAN_METHOD(GetBlockIndex) { CBlockIndex* blockIndex; - if (args[0]->IsNumber()) { - int64_t height = args[0]->IntegerValue(); + if (info[0]->IsNumber()) { + int64_t height = info[0]->IntegerValue(); blockIndex = chainActive[height]; if (blockIndex == NULL) { - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } } else { - String::Utf8Value hash_(args[0]->ToString()); + String::Utf8Value hash_(info[0]->ToString()); std::string hashStr = std::string(*hash_); uint256 hash = uint256S(hashStr); if (mapBlockIndex.count(hash) == 0) { - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } else { blockIndex = mapBlockIndex[hash]; } } - Local obj = NanNew(); + Local obj = New(); arith_uint256 cw = blockIndex->nChainWork; CBlockIndex* prevBlockIndex = blockIndex->pprev; if (&prevBlockIndex->phashBlock != 0) { const uint256* prevHash = prevBlockIndex->phashBlock; - obj->Set(NanNew("prevHash"), NanNew(prevHash->GetHex())); + Set(obj, New("prevHash").ToLocalChecked(), New(prevHash->GetHex()).ToLocalChecked()); } else { - obj->Set(NanNew("prevHash"), NanNull()); + Set(obj, New("prevHash").ToLocalChecked(), Null()); } - obj->Set(NanNew("hash"), NanNew(blockIndex->phashBlock->GetHex())); - obj->Set(NanNew("chainWork"), NanNew(cw.GetHex())); + Set(obj, New("hash").ToLocalChecked(), New(blockIndex->phashBlock->GetHex()).ToLocalChecked()); + Set(obj, New("chainWork").ToLocalChecked(), New(cw.GetHex()).ToLocalChecked()); - obj->Set(NanNew("height"), NanNew(blockIndex->nHeight)); + Set(obj, New("height").ToLocalChecked(), New(blockIndex->nHeight)); - NanReturnValue(obj); + info.GetReturnValue().Set(obj); }; @@ -1339,19 +1334,19 @@ NAN_METHOD(IsMainChain) { CBlockIndex* blockIndex; - String::Utf8Value hash_(args[0]->ToString()); + String::Utf8Value hash_(info[0]->ToString()); std::string hashStr = std::string(*hash_); uint256 hash = uint256S(hashStr); if (mapBlockIndex.count(hash) == 0) { - NanReturnValue(Undefined(isolate)); + info.GetReturnValue().Set(Null()); } else { blockIndex = mapBlockIndex[hash]; } if (chainActive.Contains(blockIndex)) { - NanReturnValue(NanNew(true)); + info.GetReturnValue().Set(New(true)); } else { - NanReturnValue(NanNew(false)); + info.GetReturnValue().Set(New(false)); } } @@ -1362,29 +1357,27 @@ NAN_METHOD(IsMainChain) { */ NAN_METHOD(GetInfo) { - NanScope(); - - if (args.Length() > 0) { - return NanThrowError( + if (info.Length() > 0) { + return ThrowError( "Usage: bitcoind.getInfo()"); } - Local obj = NanNew(); + Local obj = New(); proxyType proxy; GetProxy(NET_IPV4, proxy); - obj->Set(NanNew("version"), NanNew(CLIENT_VERSION)); - obj->Set(NanNew("protocolversion"), NanNew(PROTOCOL_VERSION)); - 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("difficulty"), NanNew((double)GetDifficulty())); - obj->Set(NanNew("testnet"), NanNew(Params().NetworkIDString() == "test")); - obj->Set(NanNew("relayfee"), NanNew(::minRelayTxFee.GetFeePerK())); // double - obj->Set(NanNew("errors"), NanNew(GetWarnings("statusbar"))); + Set(obj, New("version").ToLocalChecked(), New(CLIENT_VERSION)); + Set(obj, New("protocolversion").ToLocalChecked(), New(PROTOCOL_VERSION)); + Set(obj, New("blocks").ToLocalChecked(), New((int)chainActive.Height())->ToInt32()); + Set(obj, New("timeoffset").ToLocalChecked(), New(GetTimeOffset())); + Set(obj, New("connections").ToLocalChecked(), New((int)vNodes.size())->ToInt32()); + Set(obj, New("difficulty").ToLocalChecked(), New((double)GetDifficulty())); + Set(obj, New("testnet").ToLocalChecked(), New(Params().NetworkIDString() == "test")); + Set(obj, New("relayfee").ToLocalChecked(), New(::minRelayTxFee.GetFeePerK())); // double + Set(obj, New("errors").ToLocalChecked(), New(GetWarnings("statusbar")).ToLocalChecked()); - NanReturnValue(obj); + info.GetReturnValue().Set(obj); } /** @@ -1396,7 +1389,7 @@ NAN_METHOD(EstimateFee) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); - int nBlocks = args[0]->NumberValue(); + int nBlocks = info[0]->NumberValue(); if (nBlocks < 1) { nBlocks = 1; } @@ -1404,13 +1397,13 @@ NAN_METHOD(EstimateFee) { CFeeRate feeRate = mempool.estimateFee(nBlocks); if (feeRate == CFeeRate(0)) { - NanReturnValue(NanNew(-1.0)); + info.GetReturnValue().Set(New(-1.0)); return; } CAmount nFee = feeRate.GetFeePerK(); - NanReturnValue(NanNew(nFee)); + info.GetReturnValue().Set(New(nFee)); } @@ -1428,18 +1421,18 @@ NAN_METHOD(SendTransaction) { LOCK(cs_main); // Decode the transaction - v8::String::Utf8Value param1(args[0]->ToString()); + v8::String::Utf8Value param1(info[0]->ToString()); std::string *input = new std::string(*param1); CTransaction tx; if (!DecodeHexTx(tx, *input)) { - return NanThrowError("TX decode failed"); + return ThrowError("TX decode failed"); } uint256 hashTx = tx.GetHash(); // Skip absurdly high fee check bool allowAbsurdFees = false; - if (args.Length() > 1) { - allowAbsurdFees = args[1]->BooleanValue(); + if (info.Length() > 1) { + allowAbsurdFees = info[1]->BooleanValue(); } CCoinsViewCache &view = *pcoinsTip; @@ -1453,22 +1446,22 @@ NAN_METHOD(SendTransaction) { // Attempt to add the transaction to the mempool if (!AcceptToMemoryPool(mempool, state, tx, false, &fMissingInputs, !allowAbsurdFees)) { if (state.IsInvalid()) { - return NanThrowError((boost::lexical_cast(state.GetRejectCode()) + ": " + state.GetRejectReason()).c_str()); + return ThrowError((boost::lexical_cast(state.GetRejectCode()) + ": " + state.GetRejectReason()).c_str()); } else { if (fMissingInputs) { - return NanThrowError("Missing inputs"); + return ThrowError("Missing inputs"); } - return NanThrowError(state.GetRejectReason().c_str()); + return ThrowError(state.GetRejectReason().c_str()); } } } else if (fHaveChain) { - return NanThrowError("transaction already in block chain"); + return ThrowError("transaction already in block chain"); } // Relay the transaction connect peers RelayTransaction(tx); - NanReturnValue(Local::New(isolate, NanNew(hashTx.GetHex()))); + info.GetReturnValue().Set(Local::New(isolate, New(hashTx.GetHex()).ToLocalChecked())); } /** @@ -1477,7 +1470,7 @@ NAN_METHOD(SendTransaction) { * Will return an array of transaction buffers. */ NAN_METHOD(GetMempoolTransactions) { - Isolate* isolate = args.GetIsolate(); + Isolate* isolate = info.GetIsolate(); HandleScope scope(isolate); Local transactions = Array::New(isolate); @@ -1503,7 +1496,7 @@ NAN_METHOD(GetMempoolTransactions) { } } - NanReturnValue(transactions); + info.GetReturnValue().Set(transactions); } @@ -1511,17 +1504,15 @@ NAN_METHOD(GetMempoolTransactions) { * AddMempoolUncheckedTransaction */ NAN_METHOD(AddMempoolUncheckedTransaction) { - NanScope(); - - v8::String::Utf8Value param1(args[0]->ToString()); + v8::String::Utf8Value param1(info[0]->ToString()); std::string *input = new std::string(*param1); CTransaction tx; if (!DecodeHexTx(tx, *input)) { - return NanThrowError("could not decode tx"); + return ThrowError("could not decode tx"); } bool added = mempool.addUnchecked(tx.GetHash(), CTxMemPoolEntry(tx, 0, 0, 0.0, 1)); - NanReturnValue(NanNew(added)); + info.GetReturnValue().Set(New(added)); } @@ -1548,32 +1539,28 @@ set_cooked(void) { * Initialize the singleton object known as bitcoind. */ -extern "C" void -init(Handle target) { - NanScope(); - - NODE_SET_METHOD(target, "start", StartBitcoind); - NODE_SET_METHOD(target, "onBlocksReady", OnBlocksReady); - NODE_SET_METHOD(target, "onTipUpdate", OnTipUpdate); - NODE_SET_METHOD(target, "stop", StopBitcoind); - NODE_SET_METHOD(target, "getBlock", GetBlock); - NODE_SET_METHOD(target, "getTransaction", GetTransaction); - NODE_SET_METHOD(target, "getTransactionWithBlockInfo", GetTransactionWithBlockInfo); - NODE_SET_METHOD(target, "getInfo", GetInfo); - NODE_SET_METHOD(target, "isSpent", IsSpent); - NODE_SET_METHOD(target, "getBlockIndex", GetBlockIndex); - NODE_SET_METHOD(target, "isMainChain", IsMainChain); - NODE_SET_METHOD(target, "getMempoolTransactions", GetMempoolTransactions); - NODE_SET_METHOD(target, "addMempoolUncheckedTransaction", AddMempoolUncheckedTransaction); - NODE_SET_METHOD(target, "sendTransaction", SendTransaction); - NODE_SET_METHOD(target, "estimateFee", EstimateFee); - NODE_SET_METHOD(target, "startTxMon", StartTxMon); - NODE_SET_METHOD(target, "syncPercentage", SyncPercentage); - NODE_SET_METHOD(target, "isSynced", IsSynced); - NODE_SET_METHOD(target, "getTxOutSetInfo", GetTxOutSetInfo); - NODE_SET_METHOD(target, "getBestBlockHash", GetBestBlockHash); - NODE_SET_METHOD(target, "getNextBlockHash", GetNextBlockHash); - +NAN_MODULE_INIT(init) { + Set(target, New("start").ToLocalChecked(), GetFunction(New(StartBitcoind)).ToLocalChecked()); + Set(target, New("onBlocksReady").ToLocalChecked(), GetFunction(New(OnBlocksReady)).ToLocalChecked()); + Set(target, New("onTipUpdate").ToLocalChecked(), GetFunction(New(OnTipUpdate)).ToLocalChecked()); + Set(target, New("stop").ToLocalChecked(), GetFunction(New(StopBitcoind)).ToLocalChecked()); + Set(target, New("getBlock").ToLocalChecked(), GetFunction(New(GetBlock)).ToLocalChecked()); + Set(target, New("getTransaction").ToLocalChecked(), GetFunction(New(GetTransaction)).ToLocalChecked()); + Set(target, New("getTransactionWithBlockInfo").ToLocalChecked(), GetFunction(New(GetTransactionWithBlockInfo)).ToLocalChecked()); + Set(target, New("getInfo").ToLocalChecked(), GetFunction(New(GetInfo)).ToLocalChecked()); + Set(target, New("isSpent").ToLocalChecked(), GetFunction(New(IsSpent)).ToLocalChecked()); + Set(target, New("getBlockIndex").ToLocalChecked(), GetFunction(New(GetBlockIndex)).ToLocalChecked()); + Set(target, New("isMainChain").ToLocalChecked(), GetFunction(New(IsMainChain)).ToLocalChecked()); + Set(target, New("getMempoolTransactions").ToLocalChecked(), GetFunction(New(GetMempoolTransactions)).ToLocalChecked()); + Set(target, New("addMempoolUncheckedTransaction").ToLocalChecked(), GetFunction(New(AddMempoolUncheckedTransaction)).ToLocalChecked()); + Set(target, New("sendTransaction").ToLocalChecked(), GetFunction(New(SendTransaction)).ToLocalChecked()); + Set(target, New("estimateFee").ToLocalChecked(), GetFunction(New(EstimateFee)).ToLocalChecked()); + Set(target, New("startTxMon").ToLocalChecked(), GetFunction(New(StartTxMon)).ToLocalChecked()); + Set(target, New("syncPercentage").ToLocalChecked(), GetFunction(New(SyncPercentage)).ToLocalChecked()); + Set(target, New("isSynced").ToLocalChecked(), GetFunction(New(IsSynced)).ToLocalChecked()); + Set(target, New("getTxOutSetInfo").ToLocalChecked(), GetFunction(New(GetTxOutSetInfo)).ToLocalChecked()); + Set(target, New("getBestBlockHash").ToLocalChecked(), GetFunction(New(GetBestBlockHash)).ToLocalChecked()); + Set(target, New("getNextBlockHash").ToLocalChecked(), GetFunction(New(GetNextBlockHash)).ToLocalChecked()); } -NODE_MODULE(libbitcoind, init) +NODE_MODULE(libbitcoind, init); diff --git a/src/libbitcoind.h b/src/libbitcoind.h index d7a971d3..90d2ca97 100644 --- a/src/libbitcoind.h +++ b/src/libbitcoind.h @@ -17,25 +17,3 @@ #ifdef ENABLE_WALLET #include "wallet/wallet.h" #endif - -NAN_METHOD(StartBitcoind); -NAN_METHOD(OnBlocksReady); -NAN_METHOD(OnTipUpdate); -NAN_METHOD(StopBitcoind); -NAN_METHOD(GetBlock); -NAN_METHOD(GetBlockIndex); -NAN_METHOD(IsMainChain); -NAN_METHOD(GetTransaction); -NAN_METHOD(GetInfo); -NAN_METHOD(IsSpent); -NAN_METHOD(GetBlockIndex); -NAN_METHOD(GetMempoolTransactions); -NAN_METHOD(AddMempoolUncheckedTransaction); -NAN_METHOD(SendTransaction); -NAN_METHOD(EstimateFee); -NAN_METHOD(StartTxMon); -NAN_METHOD(SyncPercentage); -NAN_METHOD(IsSynced); -NAN_METHOD(GetTxOutSetInfo); -NAN_METHOD(GetBestBlockHash); -NAN_METHOD(GetNextBlockHash);