2014-08-12 12:03:04 -07:00
|
|
|
/**
|
2015-06-10 14:24:11 -07:00
|
|
|
* bitcoind.js - a binding for node.js which links to libbitcoind.so/dylib.
|
|
|
|
* Copyright (c) 2015, BitPay (MIT License)
|
2014-08-12 12:03:04 -07:00
|
|
|
*
|
|
|
|
* bitcoindjs.cc:
|
|
|
|
* A bitcoind node.js binding.
|
|
|
|
*/
|
|
|
|
|
2014-08-29 16:20:38 -07:00
|
|
|
#include "bitcoindjs.h"
|
|
|
|
|
2014-09-17 12:14:20 -07:00
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
2015-06-11 05:34:38 -07:00
|
|
|
using namespace node;
|
|
|
|
using namespace v8;
|
2014-09-17 12:14:20 -07:00
|
|
|
|
2014-10-15 15:27:14 -07:00
|
|
|
/**
|
|
|
|
* Bitcoin Globals
|
|
|
|
*/
|
|
|
|
|
|
|
|
// These global functions and variables are
|
|
|
|
// required to be defined/exposed here.
|
2014-10-06 11:05:52 -07:00
|
|
|
|
2015-07-13 13:34:29 -07:00
|
|
|
extern void WaitForShutdown(boost::thread_group* threadGroup);
|
2014-12-10 11:42:33 -08:00
|
|
|
static termios orig_termios;
|
|
|
|
|
2014-10-15 15:27:14 -07:00
|
|
|
/**
|
|
|
|
* Node.js Internal Function Templates
|
|
|
|
*/
|
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static void
|
2014-10-06 08:10:23 -07:00
|
|
|
async_start_node(uv_work_t *req);
|
2014-08-19 16:40:19 -07:00
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static void
|
2014-09-02 19:00:31 -07:00
|
|
|
async_start_node_after(uv_work_t *req);
|
2014-08-12 12:03:04 -07:00
|
|
|
|
2015-07-07 14:02:03 -07:00
|
|
|
static void
|
|
|
|
async_blocks_ready(uv_work_t *req);
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_blocks_ready_after(uv_work_t *req);
|
|
|
|
|
2014-09-11 17:18:36 -07:00
|
|
|
static void
|
2014-10-06 08:10:23 -07:00
|
|
|
async_stop_node(uv_work_t *req);
|
2014-09-11 17:18:36 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
async_stop_node_after(uv_work_t *req);
|
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static int
|
|
|
|
start_node(void);
|
|
|
|
|
2014-09-17 14:08:26 -07:00
|
|
|
static void
|
|
|
|
start_node_thread(void);
|
2014-09-17 12:52:35 -07:00
|
|
|
|
2014-09-19 13:53:55 -07:00
|
|
|
static void
|
|
|
|
async_get_block(uv_work_t *req);
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_block_after(uv_work_t *req);
|
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
static void
|
|
|
|
async_get_tx(uv_work_t *req);
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_tx_after(uv_work_t *req);
|
|
|
|
|
2014-12-04 15:38:35 -08:00
|
|
|
static int
|
2014-12-08 13:36:54 -08:00
|
|
|
get_tx(uint256 txid, uint256& blockhash, CTransaction& ctx);
|
2014-12-04 15:38:35 -08:00
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
extern "C" void
|
|
|
|
init(Handle<Object>);
|
|
|
|
|
2014-09-19 13:53:55 -07:00
|
|
|
/**
|
2014-10-15 15:27:14 -07:00
|
|
|
* Private Global Variables
|
|
|
|
* Used only by bitcoindjs functions.
|
2014-09-19 13:53:55 -07:00
|
|
|
*/
|
|
|
|
|
2014-10-15 15:37:29 -07:00
|
|
|
static volatile bool shutdown_complete = false;
|
2014-10-15 15:36:35 -07:00
|
|
|
static char *g_data_dir = NULL;
|
2014-10-17 12:06:40 -07:00
|
|
|
static bool g_rpc = false;
|
2014-10-20 09:38:35 -07:00
|
|
|
static bool g_testnet = false;
|
2014-12-01 20:43:44 -08:00
|
|
|
static bool g_txindex = false;
|
2014-09-19 13:53:55 -07:00
|
|
|
|
2014-10-06 11:05:52 -07:00
|
|
|
/**
|
|
|
|
* Private Structs
|
|
|
|
* Used for async functions and necessary linked lists at points.
|
|
|
|
*/
|
|
|
|
|
2015-07-07 14:02:03 -07:00
|
|
|
/**
|
|
|
|
* async_node_data
|
|
|
|
* Where the uv async request data resides.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct async_block_ready_data {
|
|
|
|
std::string err_msg;
|
|
|
|
std::string result;
|
|
|
|
Eternal<Function> callback;
|
|
|
|
};
|
|
|
|
|
2014-08-20 17:00:09 -07:00
|
|
|
/**
|
2014-09-02 19:00:31 -07:00
|
|
|
* async_node_data
|
2014-08-20 17:00:09 -07:00
|
|
|
* Where the uv async request data resides.
|
|
|
|
*/
|
|
|
|
|
2014-09-02 19:00:31 -07:00
|
|
|
struct async_node_data {
|
2014-09-29 12:18:29 -07:00
|
|
|
std::string err_msg;
|
|
|
|
std::string result;
|
2014-10-15 15:36:35 -07:00
|
|
|
std::string datadir;
|
2014-10-17 12:06:40 -07:00
|
|
|
bool rpc;
|
2014-10-20 09:38:35 -07:00
|
|
|
bool testnet;
|
2014-12-01 20:43:44 -08:00
|
|
|
bool txindex;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-08-19 16:40:19 -07:00
|
|
|
};
|
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
/**
|
|
|
|
* async_block_data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct async_block_data {
|
|
|
|
std::string err_msg;
|
2015-07-14 12:17:45 -07:00
|
|
|
std::string hash;
|
2014-11-04 16:08:31 -08:00
|
|
|
int64_t height;
|
2015-07-07 20:24:22 -07:00
|
|
|
char* buffer;
|
|
|
|
uint32_t size;
|
2014-11-04 16:08:31 -08:00
|
|
|
CBlock cblock;
|
|
|
|
CBlockIndex* cblock_index;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-09-22 13:21:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* async_tx_data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct async_tx_data {
|
|
|
|
std::string err_msg;
|
2015-07-14 12:17:45 -07:00
|
|
|
std::string txid;
|
|
|
|
std::string blockhash;
|
2014-10-03 11:23:04 -07:00
|
|
|
CTransaction ctx;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-09-22 13:21:42 -07:00
|
|
|
};
|
|
|
|
|
2014-12-03 12:14:23 -08:00
|
|
|
/**
|
|
|
|
* async_block_tx_data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct async_block_tx_data {
|
|
|
|
std::string err_msg;
|
|
|
|
std::string txid;
|
|
|
|
CBlock cblock;
|
|
|
|
CBlockIndex* cblock_index;
|
2014-12-10 16:22:30 -08:00
|
|
|
CTransaction ctx;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-12-03 12:14:23 -08:00
|
|
|
};
|
|
|
|
|
2014-12-03 12:46:48 -08:00
|
|
|
/**
|
|
|
|
* async_block_time_data
|
|
|
|
*/
|
|
|
|
|
2014-12-04 11:12:29 -08:00
|
|
|
typedef struct _cblocks_list {
|
|
|
|
CBlock cblock;
|
|
|
|
CBlockIndex* cblock_index;
|
|
|
|
struct _cblocks_list *next;
|
|
|
|
std::string err_msg;
|
|
|
|
} cblocks_list;
|
|
|
|
|
2014-12-03 12:46:48 -08:00
|
|
|
struct async_block_time_data {
|
|
|
|
std::string err_msg;
|
|
|
|
uint32_t gte;
|
|
|
|
uint32_t lte;
|
2014-12-04 11:12:29 -08:00
|
|
|
int64_t limit;
|
|
|
|
cblocks_list *cblocks;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-12-03 12:46:48 -08:00
|
|
|
};
|
|
|
|
|
2014-11-04 16:08:31 -08:00
|
|
|
/**
|
|
|
|
* async_addrtx_data
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct _ctx_list {
|
|
|
|
CTransaction ctx;
|
2014-11-05 11:56:37 -08:00
|
|
|
uint256 blockhash;
|
2014-11-04 16:08:31 -08:00
|
|
|
struct _ctx_list *next;
|
2014-12-01 15:12:01 -08:00
|
|
|
std::string err_msg;
|
2014-11-04 16:08:31 -08:00
|
|
|
} ctx_list;
|
|
|
|
|
|
|
|
struct async_addrtx_data {
|
|
|
|
std::string err_msg;
|
|
|
|
std::string addr;
|
|
|
|
ctx_list *ctxs;
|
2014-12-03 20:53:31 -08:00
|
|
|
int64_t blockheight;
|
2014-12-03 21:16:47 -08:00
|
|
|
int64_t blocktime;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-11-04 16:08:31 -08:00
|
|
|
};
|
|
|
|
|
2014-09-25 12:05:39 -07:00
|
|
|
/**
|
2014-09-25 12:38:42 -07:00
|
|
|
* async_broadcast_tx_data
|
2014-09-25 12:05:39 -07:00
|
|
|
*/
|
|
|
|
|
2014-09-25 12:38:42 -07:00
|
|
|
struct async_broadcast_tx_data {
|
2014-09-25 12:05:39 -07:00
|
|
|
std::string err_msg;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Object> jstx;
|
2014-10-03 11:23:04 -07:00
|
|
|
CTransaction ctx;
|
2014-12-08 11:31:00 -08:00
|
|
|
std::string txid;
|
2014-09-25 12:05:39 -07:00
|
|
|
bool override_fees;
|
2014-09-25 13:12:28 -07:00
|
|
|
bool own_only;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-09-25 12:05:39 -07:00
|
|
|
};
|
2014-09-23 13:57:49 -07:00
|
|
|
|
2014-12-16 14:41:00 -08:00
|
|
|
/**
|
|
|
|
* async_from_tx_data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct async_from_tx_data {
|
|
|
|
std::string err_msg;
|
|
|
|
std::string txid;
|
|
|
|
ctx_list *ctxs;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> callback;
|
2014-12-16 14:41:00 -08:00
|
|
|
};
|
|
|
|
|
2014-12-10 10:44:01 -08:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2014-12-10 11:42:33 -08:00
|
|
|
set_cooked(void);
|
2014-12-10 10:44:01 -08:00
|
|
|
|
2014-10-15 15:27:14 -07:00
|
|
|
/**
|
|
|
|
* Functions
|
|
|
|
*/
|
|
|
|
|
2015-07-07 14:02:03 -07:00
|
|
|
NAN_METHOD(OnBlocksReady) {
|
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
|
|
|
Local<Function> callback;
|
|
|
|
callback = Local<Function>::Cast(args[0]);
|
|
|
|
|
|
|
|
async_block_ready_data *data = new async_block_ready_data();
|
|
|
|
data->err_msg = std::string("");
|
|
|
|
data->result = std::string("");
|
|
|
|
|
|
|
|
Eternal<Function> eternal(isolate, callback);
|
|
|
|
|
|
|
|
data->callback = eternal;
|
|
|
|
uv_work_t *req = new uv_work_t();
|
|
|
|
req->data = data;
|
|
|
|
|
|
|
|
int status = uv_queue_work(uv_default_loop(),
|
|
|
|
req, async_blocks_ready,
|
|
|
|
(uv_after_work_cb)async_blocks_ready_after);
|
|
|
|
|
|
|
|
assert(status == 0);
|
|
|
|
|
|
|
|
NanReturnValue(Undefined(isolate));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* async_start_node()
|
|
|
|
* Call start_node() and start all our boost threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_blocks_ready(uv_work_t *req) {
|
|
|
|
async_block_ready_data *data = static_cast<async_block_ready_data*>(req->data);
|
|
|
|
data->result = std::string("");
|
|
|
|
|
|
|
|
while(!chainActive.Tip()) {
|
2015-07-07 17:28:48 -07:00
|
|
|
usleep(1E6);
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlockIndex* tip = chainActive.Tip();
|
|
|
|
uint256 tipHash = tip->GetBlockHash();
|
|
|
|
|
|
|
|
while(mapBlockIndex.count(tipHash) == 0) {
|
|
|
|
usleep(1E6);
|
2015-07-07 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_blocks_ready_after(uv_work_t *req) {
|
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
async_block_ready_data *data = static_cast<async_block_ready_data*>(req->data);
|
|
|
|
|
|
|
|
Local<Function> cb = data->callback.Get(isolate);
|
|
|
|
if (data->err_msg != "") {
|
|
|
|
Local<Value> err = Exception::Error(NanNew<String>(data->err_msg));
|
|
|
|
const unsigned argc = 1;
|
|
|
|
Local<Value> argv[argc] = { err };
|
|
|
|
TryCatch try_catch;
|
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const unsigned argc = 2;
|
|
|
|
Local<Value> argv[argc] = {
|
|
|
|
v8::Null(isolate),
|
|
|
|
Local<Value>::New(isolate, NanNew<String>(data->result))
|
|
|
|
};
|
|
|
|
TryCatch try_catch;
|
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete data;
|
|
|
|
delete req;
|
|
|
|
}
|
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
/**
|
2014-10-06 11:05:52 -07:00
|
|
|
* StartBitcoind()
|
2014-08-12 12:03:04 -07:00
|
|
|
* bitcoind.start(callback)
|
2014-10-06 11:05:52 -07:00
|
|
|
* Start the bitcoind node with AppInit2() on a separate thread.
|
2014-08-12 12:03:04 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(StartBitcoind) {
|
2015-06-09 03:57:58 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
HandleScope scope(isolate);
|
2014-08-12 12:03:04 -07:00
|
|
|
|
2014-10-15 15:36:35 -07:00
|
|
|
Local<Function> callback;
|
|
|
|
std::string datadir = std::string("");
|
2014-10-17 12:06:40 -07:00
|
|
|
bool rpc = false;
|
2014-10-20 09:38:35 -07:00
|
|
|
bool testnet = false;
|
2014-12-01 20:43:44 -08:00
|
|
|
bool txindex = false;
|
2014-10-15 16:38:10 -07:00
|
|
|
|
|
|
|
if (args.Length() >= 2 && args[0]->IsObject() && args[1]->IsFunction()) {
|
2014-10-15 15:36:35 -07:00
|
|
|
Local<Object> options = Local<Object>::Cast(args[0]);
|
2014-10-15 16:38:10 -07:00
|
|
|
if (options->Get(NanNew<String>("datadir"))->IsString()) {
|
|
|
|
String::Utf8Value datadir_(options->Get(NanNew<String>("datadir"))->ToString());
|
|
|
|
datadir = std::string(*datadir_);
|
2014-10-15 15:36:35 -07:00
|
|
|
}
|
2014-10-17 12:06:40 -07:00
|
|
|
if (options->Get(NanNew<String>("rpc"))->IsBoolean()) {
|
|
|
|
rpc = options->Get(NanNew<String>("rpc"))->ToBoolean()->IsTrue();
|
|
|
|
}
|
2014-10-20 09:38:35 -07:00
|
|
|
if (options->Get(NanNew<String>("testnet"))->IsBoolean()) {
|
|
|
|
testnet = options->Get(NanNew<String>("testnet"))->ToBoolean()->IsTrue();
|
|
|
|
}
|
2014-12-01 20:43:44 -08:00
|
|
|
if (options->Get(NanNew<String>("txindex"))->IsBoolean()) {
|
|
|
|
txindex = options->Get(NanNew<String>("txindex"))->ToBoolean()->IsTrue();
|
|
|
|
}
|
2014-10-15 16:38:10 -07:00
|
|
|
callback = Local<Function>::Cast(args[1]);
|
2014-10-16 11:24:21 -07:00
|
|
|
} else if (args.Length() >= 2
|
2014-10-15 16:38:10 -07:00
|
|
|
&& (args[0]->IsUndefined() || args[0]->IsNull())
|
|
|
|
&& args[1]->IsFunction()) {
|
|
|
|
callback = Local<Function>::Cast(args[1]);
|
2014-10-16 11:24:21 -07:00
|
|
|
} else if (args.Length() >= 1 && args[0]->IsFunction()) {
|
2014-10-15 15:36:35 -07:00
|
|
|
callback = Local<Function>::Cast(args[0]);
|
2014-10-15 16:38:10 -07:00
|
|
|
} else {
|
|
|
|
return NanThrowError(
|
|
|
|
"Usage: bitcoind.start(callback)");
|
2014-08-12 12:03:04 -07:00
|
|
|
}
|
|
|
|
|
2014-09-02 19:00:31 -07:00
|
|
|
//
|
|
|
|
// Run bitcoind's StartNode() on a separate thread.
|
|
|
|
//
|
2014-08-19 16:40:19 -07:00
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
async_node_data *data = new async_node_data();
|
|
|
|
data->err_msg = std::string("");
|
|
|
|
data->result = std::string("");
|
2014-10-15 15:36:35 -07:00
|
|
|
data->datadir = datadir;
|
2014-10-17 12:06:40 -07:00
|
|
|
data->rpc = rpc;
|
2014-12-01 20:43:44 -08:00
|
|
|
data->testnet = testnet;
|
|
|
|
data->txindex = txindex;
|
2015-06-08 14:18:06 -07:00
|
|
|
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> eternal(isolate, callback);
|
2014-08-19 16:40:19 -07:00
|
|
|
|
2015-06-10 12:38:21 -07:00
|
|
|
data->callback = eternal;
|
2014-09-29 12:18:29 -07:00
|
|
|
uv_work_t *req = new uv_work_t();
|
|
|
|
req->data = data;
|
2014-08-19 16:40:19 -07:00
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
int status = uv_queue_work(uv_default_loop(),
|
2014-10-06 08:10:23 -07:00
|
|
|
req, async_start_node,
|
2014-09-02 19:00:31 -07:00
|
|
|
(uv_after_work_cb)async_start_node_after);
|
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
assert(status == 0);
|
2014-08-12 12:03:04 -07:00
|
|
|
|
2015-06-08 14:18:06 -07:00
|
|
|
NanReturnValue(Undefined(isolate));
|
2014-08-12 12:03:04 -07:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:00:09 -07:00
|
|
|
/**
|
2014-10-06 08:10:23 -07:00
|
|
|
* async_start_node()
|
2014-08-20 17:00:09 -07:00
|
|
|
* Call start_node() and start all our boost threads.
|
|
|
|
*/
|
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static void
|
2014-10-06 08:10:23 -07:00
|
|
|
async_start_node(uv_work_t *req) {
|
2014-09-29 12:18:29 -07:00
|
|
|
async_node_data *data = static_cast<async_node_data*>(req->data);
|
2014-11-11 11:38:51 -08:00
|
|
|
if (data->datadir != "") {
|
2014-10-16 11:24:21 -07:00
|
|
|
g_data_dir = (char *)data->datadir.c_str();
|
2014-11-11 13:36:08 -08:00
|
|
|
} else {
|
|
|
|
g_data_dir = (char *)malloc(sizeof(char) * 512);
|
|
|
|
snprintf(g_data_dir, sizeof(char) * 512, "%s/.bitcoind.js", getenv("HOME"));
|
2014-10-15 15:36:35 -07:00
|
|
|
}
|
2014-10-17 12:06:40 -07:00
|
|
|
g_rpc = (bool)data->rpc;
|
2014-10-20 09:38:35 -07:00
|
|
|
g_testnet = (bool)data->testnet;
|
2014-12-01 20:43:44 -08:00
|
|
|
g_txindex = (bool)data->txindex;
|
2014-12-10 11:42:33 -08:00
|
|
|
tcgetattr(STDIN_FILENO, &orig_termios);
|
2014-09-05 15:07:38 -07:00
|
|
|
start_node();
|
2014-12-10 10:47:02 -08:00
|
|
|
data->result = std::string("bitcoind opened.");
|
2014-08-19 16:40:19 -07:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:00:09 -07:00
|
|
|
/**
|
2014-09-02 19:00:31 -07:00
|
|
|
* async_start_node_after()
|
2014-08-20 17:00:09 -07:00
|
|
|
* Execute our callback.
|
|
|
|
*/
|
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static void
|
2014-09-02 19:00:31 -07:00
|
|
|
async_start_node_after(uv_work_t *req) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
async_node_data *data = static_cast<async_node_data*>(req->data);
|
2014-08-19 16:40:19 -07:00
|
|
|
|
2015-06-10 12:38:21 -07:00
|
|
|
Local<Function> cb = data->callback.Get(isolate);
|
2014-11-11 11:38:51 -08:00
|
|
|
if (data->err_msg != "") {
|
2014-11-11 11:36:20 -08:00
|
|
|
Local<Value> err = Exception::Error(NanNew<String>(data->err_msg));
|
2014-08-19 16:40:19 -07:00
|
|
|
const unsigned argc = 1;
|
2014-09-02 19:00:31 -07:00
|
|
|
Local<Value> argv[argc] = { err };
|
2014-08-19 16:40:19 -07:00
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-08-19 16:40:19 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const unsigned argc = 2;
|
2014-09-02 19:00:31 -07:00
|
|
|
Local<Value> argv[argc] = {
|
2015-06-10 12:38:21 -07:00
|
|
|
v8::Null(isolate),
|
|
|
|
Local<Value>::New(isolate, NanNew<String>(data->result))
|
2014-08-19 16:40:19 -07:00
|
|
|
};
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-08-19 16:40:19 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
delete data;
|
2014-08-19 16:40:19 -07:00
|
|
|
delete req;
|
2014-08-12 12:03:04 -07:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:00:09 -07:00
|
|
|
/**
|
|
|
|
* start_node(void)
|
2014-10-06 11:05:52 -07:00
|
|
|
* Start AppInit2() on a separate thread, wait for
|
|
|
|
* Unfortunately, we need to wait for the initialization
|
|
|
|
* to unhook the signal handlers so we can use them
|
|
|
|
* from node.js in javascript.
|
2014-08-20 17:00:09 -07:00
|
|
|
*/
|
2014-09-02 19:00:31 -07:00
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
static int
|
|
|
|
start_node(void) {
|
2014-10-13 14:00:01 -07:00
|
|
|
SetupEnvironment();
|
|
|
|
|
2014-09-17 12:14:20 -07:00
|
|
|
noui_connect();
|
|
|
|
|
2015-06-10 14:24:11 -07:00
|
|
|
new boost::thread(boost::bind(&start_node_thread));
|
2014-09-17 12:14:20 -07:00
|
|
|
|
2014-10-06 11:05:52 -07:00
|
|
|
// Drop the bitcoind signal handlers: we want our own.
|
2014-09-17 14:08:26 -07:00
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGHUP, SIG_DFL);
|
2014-09-30 15:14:53 -07:00
|
|
|
signal(SIGQUIT, SIG_DFL);
|
2014-09-17 12:14:20 -07:00
|
|
|
|
2014-09-17 12:52:35 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2014-09-17 12:14:20 -07:00
|
|
|
|
2014-09-17 14:08:26 -07:00
|
|
|
static void
|
|
|
|
start_node_thread(void) {
|
2014-09-17 12:52:35 -07:00
|
|
|
boost::thread_group threadGroup;
|
2015-07-13 13:34:29 -07:00
|
|
|
CScheduler scheduler;
|
2014-09-17 12:14:20 -07:00
|
|
|
|
2014-10-13 14:16:43 -07:00
|
|
|
// Workaround for AppInit2() arg parsing. Not ideal, but it works.
|
2014-10-15 15:36:35 -07:00
|
|
|
int argc = 0;
|
2014-12-01 20:43:44 -08:00
|
|
|
char **argv = (char **)malloc((4 + 1) * sizeof(char **));
|
2014-10-17 12:11:34 -07:00
|
|
|
|
2014-10-21 10:47:09 -07:00
|
|
|
argv[argc] = (char *)"bitcoind";
|
|
|
|
argc++;
|
2014-10-17 12:38:41 -07:00
|
|
|
|
2014-10-15 15:36:35 -07:00
|
|
|
if (g_data_dir) {
|
2014-10-21 18:06:17 -07:00
|
|
|
const int argl = 9 + strlen(g_data_dir) + 1;
|
|
|
|
char *arg = (char *)malloc(sizeof(char) * argl);
|
2014-10-16 14:56:41 -07:00
|
|
|
int w = snprintf(arg, argl, "-datadir=%s", g_data_dir);
|
2014-10-21 18:06:17 -07:00
|
|
|
if (w >= 10 && w <= argl) {
|
2014-10-21 10:47:09 -07:00
|
|
|
arg[w] = '\0';
|
|
|
|
argv[argc] = arg;
|
|
|
|
argc++;
|
|
|
|
} else {
|
2014-12-10 11:42:33 -08:00
|
|
|
if (set_cooked()) {
|
2014-12-10 10:44:01 -08:00
|
|
|
fprintf(stderr, "bitcoind.js: Bad -datadir value.\n");
|
|
|
|
}
|
2014-10-16 14:56:41 -07:00
|
|
|
}
|
2014-10-13 14:16:43 -07:00
|
|
|
}
|
2014-10-13 14:00:01 -07:00
|
|
|
|
2014-10-17 12:11:34 -07:00
|
|
|
if (g_rpc) {
|
|
|
|
argv[argc] = (char *)"-server";
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
2014-10-20 09:38:35 -07:00
|
|
|
if (g_testnet) {
|
|
|
|
argv[argc] = (char *)"-testnet";
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:43:44 -08:00
|
|
|
if (g_txindex) {
|
|
|
|
argv[argc] = (char *)"-txindex";
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
2014-10-17 12:11:34 -07:00
|
|
|
argv[argc] = NULL;
|
|
|
|
|
2014-10-13 14:00:01 -07:00
|
|
|
bool fRet = false;
|
|
|
|
try {
|
2014-10-15 15:36:35 -07:00
|
|
|
ParseParameters((const int)argc, (const char **)argv);
|
2014-10-13 14:00:01 -07:00
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(GetDataDir(false))) {
|
2014-12-10 11:42:33 -08:00
|
|
|
if (set_cooked()) {
|
2014-12-10 10:44:01 -08:00
|
|
|
fprintf(stderr,
|
|
|
|
"bitcoind.js: Specified data directory \"%s\" does not exist.\n",
|
|
|
|
mapArgs["-datadir"].c_str());
|
|
|
|
}
|
2014-12-10 11:12:31 -08:00
|
|
|
shutdown_complete = true;
|
2014-12-10 11:29:29 -08:00
|
|
|
_exit(1);
|
2014-10-13 14:00:01 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ReadConfigFile(mapArgs, mapMultiArgs);
|
|
|
|
} catch(std::exception &e) {
|
2014-12-10 11:42:33 -08:00
|
|
|
if (set_cooked()) {
|
2014-12-10 10:44:01 -08:00
|
|
|
fprintf(stderr,
|
|
|
|
"bitcoind.js: Error reading configuration file: %s\n", e.what());
|
|
|
|
}
|
2014-12-10 11:12:31 -08:00
|
|
|
shutdown_complete = true;
|
2014-12-10 11:29:29 -08:00
|
|
|
_exit(1);
|
2014-10-13 14:00:01 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SelectParamsFromCommandLine()) {
|
2014-12-10 11:42:33 -08:00
|
|
|
if (set_cooked()) {
|
2014-12-10 10:44:01 -08:00
|
|
|
fprintf(stderr,
|
|
|
|
"bitcoind.js: Invalid combination of -regtest and -testnet.\n");
|
|
|
|
}
|
2014-12-10 11:12:31 -08:00
|
|
|
shutdown_complete = true;
|
2014-12-10 11:29:29 -08:00
|
|
|
_exit(1);
|
2014-12-10 11:12:31 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 15:36:35 -07:00
|
|
|
CreatePidFile(GetPidFile(), getpid());
|
2014-10-13 14:00:01 -07:00
|
|
|
|
2015-07-13 13:34:29 -07:00
|
|
|
fRet = AppInit2(threadGroup, scheduler);
|
2015-06-26 13:03:43 -07:00
|
|
|
|
2014-10-13 14:00:01 -07:00
|
|
|
} catch (std::exception& e) {
|
2015-06-10 14:24:11 -07:00
|
|
|
if (set_cooked()) {
|
2015-07-13 13:34:29 -07:00
|
|
|
fprintf(stderr, "bitcoind.js: AppInit2(): std::exception\n");
|
2015-06-10 14:24:11 -07:00
|
|
|
}
|
2014-10-13 14:00:01 -07:00
|
|
|
} catch (...) {
|
2015-06-10 14:24:11 -07:00
|
|
|
if (set_cooked()) {
|
2015-07-13 13:34:29 -07:00
|
|
|
fprintf(stderr, "bitcoind.js: AppInit2(): other exception\n");
|
2014-10-13 14:00:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:34:29 -07:00
|
|
|
if (!fRet)
|
|
|
|
{
|
|
|
|
threadGroup.interrupt_all();
|
|
|
|
} else {
|
|
|
|
WaitForShutdown(&threadGroup);
|
2014-10-13 14:00:01 -07:00
|
|
|
}
|
|
|
|
Shutdown();
|
2014-10-15 15:37:29 -07:00
|
|
|
shutdown_complete = true;
|
2015-07-13 13:34:29 -07:00
|
|
|
|
2014-08-20 16:47:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-11 17:18:36 -07:00
|
|
|
/**
|
2014-10-06 11:05:52 -07:00
|
|
|
* StopBitcoind()
|
2014-09-11 17:18:36 -07:00
|
|
|
* bitcoind.stop(callback)
|
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(StopBitcoind) {
|
2015-07-08 12:46:55 -07:00
|
|
|
fprintf(stderr, "Stopping Bitcoind please wait!\n");
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-12-12 11:24:53 -08:00
|
|
|
|
2014-09-11 17:18:36 -07:00
|
|
|
if (args.Length() < 1 || !args[0]->IsFunction()) {
|
|
|
|
return NanThrowError(
|
2014-09-22 12:57:25 -07:00
|
|
|
"Usage: bitcoind.stop(callback)");
|
2014-09-11 17:18:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Local<Function> callback = Local<Function>::Cast(args[0]);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Run bitcoind's StartShutdown() on a separate thread.
|
|
|
|
//
|
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
async_node_data *data = new async_node_data();
|
|
|
|
data->err_msg = std::string("");
|
|
|
|
data->result = std::string("");
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> eternal(isolate, callback);
|
|
|
|
data->callback = eternal;
|
2014-09-11 17:18:36 -07:00
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
uv_work_t *req = new uv_work_t();
|
|
|
|
req->data = data;
|
2014-09-11 17:18:36 -07:00
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
int status = uv_queue_work(uv_default_loop(),
|
2014-10-06 08:10:23 -07:00
|
|
|
req, async_stop_node,
|
2014-09-11 17:18:36 -07:00
|
|
|
(uv_after_work_cb)async_stop_node_after);
|
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
assert(status == 0);
|
2015-06-08 14:18:06 -07:00
|
|
|
NanReturnValue(Undefined(isolate));
|
2014-09-11 17:18:36 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-06 08:10:23 -07:00
|
|
|
* async_stop_node()
|
2014-10-06 11:05:52 -07:00
|
|
|
* Call StartShutdown() to join the boost threads, which will call Shutdown()
|
2014-10-15 15:37:29 -07:00
|
|
|
* and set shutdown_complete to true to notify the main node.js thread.
|
2014-09-11 17:18:36 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2014-10-06 08:10:23 -07:00
|
|
|
async_stop_node(uv_work_t *req) {
|
2014-09-29 12:18:29 -07:00
|
|
|
async_node_data *data = static_cast<async_node_data*>(req->data);
|
2014-09-11 17:18:36 -07:00
|
|
|
StartShutdown();
|
2015-07-09 11:39:23 -07:00
|
|
|
while(!shutdown_complete) {
|
|
|
|
usleep(1E6);
|
|
|
|
}
|
2014-12-10 10:47:02 -08:00
|
|
|
data->result = std::string("bitcoind shutdown.");
|
2014-09-11 17:18:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* async_stop_node_after()
|
|
|
|
* Execute our callback.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_stop_node_after(uv_work_t *req) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-09-29 12:18:29 -07:00
|
|
|
async_node_data* data = static_cast<async_node_data*>(req->data);
|
2014-09-11 17:18:36 -07:00
|
|
|
|
2015-06-10 12:38:21 -07:00
|
|
|
Local<Function> cb = data->callback.Get(isolate);
|
2014-11-11 11:38:51 -08:00
|
|
|
if (data->err_msg != "") {
|
2014-11-11 11:36:20 -08:00
|
|
|
Local<Value> err = Exception::Error(NanNew<String>(data->err_msg));
|
2014-09-11 17:18:36 -07:00
|
|
|
const unsigned argc = 1;
|
|
|
|
Local<Value> argv[argc] = { err };
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-11 17:18:36 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const unsigned argc = 2;
|
|
|
|
Local<Value> argv[argc] = {
|
2015-06-08 16:08:18 -07:00
|
|
|
Local<Value>::New(isolate, NanNull()),
|
2015-06-08 14:18:06 -07:00
|
|
|
Local<Value>::New(isolate, NanNew<String>(data->result))
|
2014-09-11 17:18:36 -07:00
|
|
|
};
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-11 17:18:36 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 12:18:29 -07:00
|
|
|
delete data;
|
2014-09-11 17:18:36 -07:00
|
|
|
delete req;
|
|
|
|
}
|
|
|
|
|
2014-10-06 11:06:23 -07:00
|
|
|
/**
|
|
|
|
* IsStopping()
|
|
|
|
* bitcoind.stopping()
|
|
|
|
* Check whether bitcoind is in the process of shutting down. This is polled
|
|
|
|
* from javascript.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(IsStopping) {
|
|
|
|
NanScope();
|
|
|
|
NanReturnValue(NanNew<Boolean>(ShutdownRequested()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsStopped()
|
|
|
|
* bitcoind.stopped()
|
|
|
|
* Check whether bitcoind has shutdown completely. This will be polled by
|
|
|
|
* javascript to check whether the libuv event loop is safe to stop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(IsStopped) {
|
|
|
|
NanScope();
|
2014-10-15 15:37:29 -07:00
|
|
|
NanReturnValue(NanNew<Boolean>(shutdown_complete));
|
2014-10-06 11:06:23 -07:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:53:55 -07:00
|
|
|
/**
|
2014-10-06 11:05:52 -07:00
|
|
|
* GetBlock()
|
2014-12-08 11:31:00 -08:00
|
|
|
* bitcoind.getBlock([blockhash,blockheight], callback)
|
2014-10-06 11:05:52 -07:00
|
|
|
* Read any block from disk asynchronously.
|
2014-09-19 13:53:55 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(GetBlock) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-09-19 13:53:55 -07:00
|
|
|
if (args.Length() < 2
|
2014-11-19 17:04:19 -08:00
|
|
|
|| (!args[0]->IsString() && !args[0]->IsNumber())
|
2014-09-19 13:53:55 -07:00
|
|
|
|| !args[1]->IsFunction()) {
|
|
|
|
return NanThrowError(
|
2014-12-08 11:31:00 -08:00
|
|
|
"Usage: bitcoindjs.getBlock([blockhash,blockheight], callback)");
|
2014-09-19 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
2014-11-04 16:08:31 -08:00
|
|
|
async_block_data *data = new async_block_data();
|
2014-09-19 13:53:55 -07:00
|
|
|
|
2014-11-04 16:08:31 -08:00
|
|
|
if (args[0]->IsNumber()) {
|
|
|
|
int64_t height = args[0]->IntegerValue();
|
|
|
|
data->err_msg = std::string("");
|
2015-07-14 12:17:45 -07:00
|
|
|
data->hash = std::string("");
|
2014-11-04 16:08:31 -08:00
|
|
|
data->height = height;
|
|
|
|
} else {
|
|
|
|
String::Utf8Value hash_(args[0]->ToString());
|
2015-07-14 12:17:45 -07:00
|
|
|
std::string hash = std::string(*hash_);
|
2014-11-04 16:08:31 -08:00
|
|
|
data->err_msg = std::string("");
|
2015-07-14 12:17:45 -07:00
|
|
|
data->hash = hash;
|
2014-11-04 16:08:31 -08:00
|
|
|
data->height = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Local<Function> callback = Local<Function>::Cast(args[1]);
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> eternal(isolate, callback);
|
|
|
|
data->callback = eternal;
|
2014-09-19 13:53:55 -07:00
|
|
|
|
|
|
|
uv_work_t *req = new uv_work_t();
|
|
|
|
req->data = data;
|
|
|
|
|
|
|
|
int status = uv_queue_work(uv_default_loop(),
|
|
|
|
req, async_get_block,
|
|
|
|
(uv_after_work_cb)async_get_block_after);
|
|
|
|
|
|
|
|
assert(status == 0);
|
|
|
|
|
2015-06-08 14:18:06 -07:00
|
|
|
NanReturnValue(Undefined(isolate));
|
2014-09-19 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_block(uv_work_t *req) {
|
|
|
|
async_block_data* data = static_cast<async_block_data*>(req->data);
|
2014-11-04 16:08:31 -08:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
CBlockIndex* pblockindex;
|
2015-07-13 13:34:29 -07:00
|
|
|
uint256 hash = uint256S(data->hash);
|
2014-11-04 16:08:31 -08:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
if (data->height != -1) {
|
|
|
|
pblockindex = chainActive[data->height];
|
2014-09-19 13:53:55 -07:00
|
|
|
} else {
|
2015-07-09 13:45:30 -07:00
|
|
|
if (mapBlockIndex.count(hash) == 0) {
|
|
|
|
data->err_msg = std::string("Block not found.");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
pblockindex = mapBlockIndex[hash];
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 12:36:17 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
const CDiskBlockPos& pos = pblockindex->GetBlockPos();
|
2015-07-07 12:36:17 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
// We can read directly from the file, and pass that, we don't need to
|
|
|
|
// deserialize the entire block only for it to then be serialized
|
|
|
|
// and then deserialized again in JavaScript
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
// Open history file to read
|
|
|
|
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
|
|
|
|
if (filein.IsNull()) {
|
|
|
|
data->err_msg = std::string("ReadBlockFromDisk: OpenBlockFile failed");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
// Get the actual file, seeked position and rewind a uint32_t
|
|
|
|
FILE* blockFile = filein.release();
|
|
|
|
long int filePos = ftell(blockFile);
|
|
|
|
fseek(blockFile, filePos - sizeof(uint32_t), SEEK_SET);
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
// Read the size of the block
|
|
|
|
uint32_t size = 0;
|
|
|
|
fread(&size, sizeof(uint32_t), 1, blockFile);
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
// Read block
|
|
|
|
char* buffer = (char *)malloc(sizeof(char) * size);
|
|
|
|
fread((void *)buffer, sizeof(char), size, blockFile);
|
|
|
|
fclose(blockFile);
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2015-07-09 13:45:30 -07:00
|
|
|
data->buffer = buffer;
|
|
|
|
data->size = size;
|
|
|
|
data->cblock_index = pblockindex;
|
2015-07-07 20:24:22 -07:00
|
|
|
|
2014-09-19 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_block_after(uv_work_t *req) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate *isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-09-19 13:53:55 -07:00
|
|
|
async_block_data* data = static_cast<async_block_data*>(req->data);
|
2015-06-10 12:38:21 -07:00
|
|
|
Local<Function> cb = data->callback.Get(isolate);
|
2014-09-19 13:53:55 -07:00
|
|
|
|
2014-11-11 11:38:51 -08:00
|
|
|
if (data->err_msg != "") {
|
2014-11-11 11:36:20 -08:00
|
|
|
Local<Value> err = Exception::Error(NanNew<String>(data->err_msg));
|
2014-09-19 13:53:55 -07:00
|
|
|
const unsigned argc = 1;
|
|
|
|
Local<Value> argv[argc] = { err };
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-19 13:53:55 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-19 15:39:05 -07:00
|
|
|
|
2015-07-07 20:24:22 -07:00
|
|
|
Local<Value> rawNodeBuffer = node::Buffer::New(isolate, data->buffer, data->size);
|
2014-09-19 15:39:05 -07:00
|
|
|
|
2015-07-09 14:34:37 -07:00
|
|
|
delete data->buffer;
|
|
|
|
data->buffer = NULL;
|
|
|
|
|
2014-09-19 13:53:55 -07:00
|
|
|
const unsigned argc = 2;
|
|
|
|
Local<Value> argv[argc] = {
|
2015-06-08 16:08:18 -07:00
|
|
|
Local<Value>::New(isolate, NanNull()),
|
2015-07-07 20:24:22 -07:00
|
|
|
rawNodeBuffer
|
2014-09-19 13:53:55 -07:00
|
|
|
};
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-19 13:53:55 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete data;
|
|
|
|
delete req;
|
|
|
|
}
|
|
|
|
|
2014-09-22 10:19:37 -07:00
|
|
|
/**
|
2014-12-03 10:57:56 -08:00
|
|
|
* GetTransaction()
|
2014-12-08 11:31:00 -08:00
|
|
|
* bitcoind.getTransaction(txid, [blockhash], callback)
|
2014-10-06 11:05:52 -07:00
|
|
|
* Read any transaction from disk asynchronously.
|
2014-09-22 10:19:37 -07:00
|
|
|
*/
|
|
|
|
|
2014-12-03 10:57:56 -08:00
|
|
|
NAN_METHOD(GetTransaction) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-09-26 11:23:21 -07:00
|
|
|
if (args.Length() < 3
|
2014-09-22 13:21:42 -07:00
|
|
|
|| !args[0]->IsString()
|
|
|
|
|| !args[1]->IsString()
|
2014-12-08 13:36:54 -08:00
|
|
|
|| !args[2]->IsFunction()) {
|
2014-09-22 13:21:42 -07:00
|
|
|
return NanThrowError(
|
2014-12-08 13:36:54 -08:00
|
|
|
"Usage: bitcoindjs.getTransaction(txid, [blockhash], callback)");
|
2014-09-22 13:21:42 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 11:31:00 -08:00
|
|
|
String::Utf8Value txid_(args[0]->ToString());
|
|
|
|
String::Utf8Value blockhash_(args[1]->ToString());
|
2014-12-08 13:36:54 -08:00
|
|
|
Local<Function> callback = Local<Function>::Cast(args[2]);
|
2014-09-22 13:21:42 -07:00
|
|
|
|
|
|
|
async_tx_data *data = new async_tx_data();
|
2015-07-13 13:34:29 -07:00
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
data->err_msg = std::string("");
|
2015-07-14 12:17:45 -07:00
|
|
|
data->txid = std::string("");
|
|
|
|
|
|
|
|
std::string txid = std::string(*txid_);
|
|
|
|
std::string blockhash = std::string(*blockhash_);
|
|
|
|
|
|
|
|
data->txid = txid;
|
2015-06-10 12:38:21 -07:00
|
|
|
Eternal<Function> eternal(isolate, callback);
|
|
|
|
data->callback = eternal;
|
2014-09-22 13:21:42 -07:00
|
|
|
|
2015-07-14 12:17:45 -07:00
|
|
|
if (blockhash == "") {
|
|
|
|
data->blockhash = uint256().GetHex();
|
2015-07-13 13:34:29 -07:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
uv_work_t *req = new uv_work_t();
|
|
|
|
req->data = data;
|
|
|
|
|
|
|
|
int status = uv_queue_work(uv_default_loop(),
|
|
|
|
req, async_get_tx,
|
|
|
|
(uv_after_work_cb)async_get_tx_after);
|
|
|
|
|
|
|
|
assert(status == 0);
|
|
|
|
|
2015-06-08 14:18:06 -07:00
|
|
|
NanReturnValue(Undefined(isolate));
|
2014-09-22 13:21:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_tx(uv_work_t *req) {
|
|
|
|
async_tx_data* data = static_cast<async_tx_data*>(req->data);
|
|
|
|
|
2015-07-13 13:34:29 -07:00
|
|
|
uint256 hash = uint256S(data->txid);
|
2015-07-15 11:16:15 -07:00
|
|
|
uint256 blockhash;
|
2014-10-02 13:24:18 -07:00
|
|
|
CTransaction ctx;
|
2015-07-15 11:16:15 -07:00
|
|
|
|
|
|
|
if (!GetTransaction(hash, ctx, blockhash, true)) {
|
2014-12-10 10:47:02 -08:00
|
|
|
data->err_msg = std::string("Transaction not found.");
|
2015-07-15 11:16:15 -07:00
|
|
|
} else {
|
|
|
|
data->ctx = ctx;
|
|
|
|
data->blockhash = blockhash.GetHex();
|
2014-12-04 15:38:35 -08:00
|
|
|
}
|
2015-07-15 11:16:15 -07:00
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_get_tx_after(uv_work_t *req) {
|
2015-06-08 14:18:06 -07:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2015-06-09 03:57:58 -07:00
|
|
|
HandleScope scope(isolate);
|
2014-09-22 13:21:42 -07:00
|
|
|
async_tx_data* data = static_cast<async_tx_data*>(req->data);
|
|
|
|
|
2014-10-03 11:23:04 -07:00
|
|
|
CTransaction ctx = data->ctx;
|
2015-06-10 12:38:21 -07:00
|
|
|
Local<Function> cb = data->callback.Get(isolate);
|
2014-09-22 13:21:42 -07:00
|
|
|
|
2014-11-11 11:38:51 -08:00
|
|
|
if (data->err_msg != "") {
|
2014-11-11 11:36:20 -08:00
|
|
|
Local<Value> err = Exception::Error(NanNew<String>(data->err_msg));
|
2014-09-22 13:21:42 -07:00
|
|
|
const unsigned argc = 1;
|
|
|
|
Local<Value> argv[argc] = { err };
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-22 13:21:42 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2015-07-15 11:16:15 -07:00
|
|
|
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssTx << ctx;
|
|
|
|
std::string stx = ssTx.str();
|
|
|
|
Local<Value> rawNodeBuffer = node::Buffer::New(isolate, stx.c_str(), stx.size());
|
|
|
|
|
2014-09-22 13:21:42 -07:00
|
|
|
const unsigned argc = 2;
|
|
|
|
Local<Value> argv[argc] = {
|
2015-06-08 16:08:18 -07:00
|
|
|
Local<Value>::New(isolate, NanNull()),
|
2015-07-15 11:16:15 -07:00
|
|
|
rawNodeBuffer
|
2014-09-22 13:21:42 -07:00
|
|
|
};
|
|
|
|
TryCatch try_catch;
|
2015-06-10 12:38:21 -07:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2014-09-22 13:21:42 -07:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
node::FatalException(try_catch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete data;
|
|
|
|
delete req;
|
|
|
|
}
|
2014-09-22 10:19:37 -07:00
|
|
|
|
2015-07-15 14:45:36 -07:00
|
|
|
/**
|
|
|
|
* IsSpent()
|
|
|
|
* bitcoindjs.isSpent()
|
|
|
|
* Determine if an outpoint is spent
|
|
|
|
*/
|
|
|
|
NAN_METHOD(IsSpent) {
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
if (args.Length() > 3) {
|
|
|
|
return NanThrowError(
|
|
|
|
"Usage: bitcoindjs.isSpent(txid, outputIndex, queryMempool)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: include the mempool in the coins viewcache
|
|
|
|
|
|
|
|
String::Utf8Value arg(args[0]->ToString());
|
|
|
|
std::string argStr = std::string(*arg);
|
|
|
|
const uint256 txid = uint256S(argStr);
|
|
|
|
int outputIndex = args[1]->IntegerValue();
|
|
|
|
bool queryMempool = args[2]->BooleanValue();
|
|
|
|
|
|
|
|
CCoinsViewCache &view = *pcoinsTip;
|
|
|
|
|
|
|
|
if (view.HaveCoins(txid)) {
|
|
|
|
const CCoins* coins = view.AccessCoins(txid);
|
|
|
|
if (!coins || !coins->IsAvailable(outputIndex)) {
|
|
|
|
NanReturnValue(NanNew<Boolean>(false));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NanReturnValue(NanNew<Boolean>(false));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NanReturnValue(NanNew<Boolean>(true));
|
|
|
|
};
|
|
|
|
|
2014-10-17 13:26:27 -07:00
|
|
|
/**
|
|
|
|
* GetInfo()
|
2014-10-17 16:12:57 -07:00
|
|
|
* bitcoindjs.getInfo()
|
2014-10-17 13:26:27 -07:00
|
|
|
* Get miscellaneous information
|
|
|
|
*/
|
|
|
|
|
|
|
|
NAN_METHOD(GetInfo) {
|
|
|
|
NanScope();
|
|
|
|
|
|
|
|
if (args.Length() > 0) {
|
|
|
|
return NanThrowError(
|
|
|
|
"Usage: bitcoindjs.getInfo()");
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:47:56 -07:00
|
|
|
Local<Object> obj = NanNew<Object>();
|
2014-10-17 13:26:27 -07:00
|
|
|
|
|
|
|
proxyType proxy;
|
|
|
|
GetProxy(NET_IPV4, proxy);
|
|
|
|
|
2014-10-17 13:47:56 -07:00
|
|
|
obj->Set(NanNew<String>("version"), NanNew<Number>(CLIENT_VERSION));
|
|
|
|
obj->Set(NanNew<String>("protocolversion"), NanNew<Number>(PROTOCOL_VERSION));
|
|
|
|
obj->Set(NanNew<String>("blocks"), NanNew<Number>((int)chainActive.Height())->ToInt32());
|
|
|
|
obj->Set(NanNew<String>("timeoffset"), NanNew<Number>(GetTimeOffset()));
|
|
|
|
obj->Set(NanNew<String>("connections"), NanNew<Number>((int)vNodes.size())->ToInt32());
|
|
|
|
obj->Set(NanNew<String>("difficulty"), NanNew<Number>((double)GetDifficulty()));
|
2014-12-01 13:16:01 -08:00
|
|
|
obj->Set(NanNew<String>("testnet"), NanNew<Boolean>(Params().NetworkIDString() == "test"));
|
2014-10-17 13:47:56 -07:00
|
|
|
obj->Set(NanNew<String>("relayfee"), NanNew<Number>(::minRelayTxFee.GetFeePerK())); // double
|
2014-10-17 14:00:12 -07:00
|
|
|
obj->Set(NanNew<String>("errors"), NanNew<String>(GetWarnings("statusbar")));
|
2014-10-17 13:26:27 -07:00
|
|
|
|
|
|
|
NanReturnValue(obj);
|
|
|
|
}
|
|
|
|
|
2014-12-10 10:44:01 -08:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2014-12-10 11:42:33 -08:00
|
|
|
set_cooked(void) {
|
|
|
|
uv_tty_t tty;
|
|
|
|
tty.mode = 1;
|
|
|
|
tty.orig_termios = orig_termios;
|
|
|
|
|
|
|
|
if (!uv_tty_set_mode(&tty, 0)) {
|
|
|
|
printf("\x1b[H\x1b[J");
|
|
|
|
return true;
|
2014-12-10 10:44:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-13 11:56:15 -08:00
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
/**
|
2014-10-06 11:05:52 -07:00
|
|
|
* Init()
|
|
|
|
* Initialize the singleton object known as bitcoindjs.
|
2014-08-12 12:03:04 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
init(Handle<Object> target) {
|
|
|
|
NanScope();
|
2014-09-29 13:38:27 -07:00
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
NODE_SET_METHOD(target, "start", StartBitcoind);
|
2015-07-07 14:02:03 -07:00
|
|
|
NODE_SET_METHOD(target, "onBlocksReady", OnBlocksReady);
|
2014-09-11 17:18:36 -07:00
|
|
|
NODE_SET_METHOD(target, "stop", StopBitcoind);
|
2014-09-17 14:08:26 -07:00
|
|
|
NODE_SET_METHOD(target, "stopping", IsStopping);
|
2014-09-17 12:52:35 -07:00
|
|
|
NODE_SET_METHOD(target, "stopped", IsStopped);
|
2014-09-18 15:32:19 -07:00
|
|
|
NODE_SET_METHOD(target, "getBlock", GetBlock);
|
2014-12-03 11:03:54 -08:00
|
|
|
NODE_SET_METHOD(target, "getTransaction", GetTransaction);
|
2014-10-17 13:26:27 -07:00
|
|
|
NODE_SET_METHOD(target, "getInfo", GetInfo);
|
2015-07-15 14:45:36 -07:00
|
|
|
NODE_SET_METHOD(target, "isSpent", IsSpent);
|
2014-09-26 12:42:04 -07:00
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(bitcoindjs, init)
|