Merge pull request #1 from braydonf/getblock

Getblock
This commit is contained in:
Chris Kleeschulte 2015-07-07 17:06:48 -04:00
commit 99829024fe
5 changed files with 149 additions and 8 deletions

View File

@ -67,7 +67,9 @@ if test -e "${root_dir}/libbitcoind/src/.libs/libbitcoind.${ext}"; then
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.0.dylib" "${os_dir}/"
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.dylib" "${os_dir}/"
else
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.so*" "${os_dir}/"
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.so" "${os_dir}/"
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.so.0" "${os_dir}/"
cp "${root_dir}/libbitcoind/src/.libs/libbitcoind.so.0.0.0" "${os_dir}/"
fi
fi

35
example/getblock.js Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env node
/**
* bitcoind.js example
*/
process.title = 'bitcoind.js';
/**
* bitcoind
*/
var bitcoind = require('../index.js')({
directory: '~/.bitcoin'
});
bitcoind.on('error', function(err) {
bitcoind.log('error="%s"', err.message);
});
bitcoind.on('ready', function(err, result) {
console.log('Ready!');
bitcoind.getBlock('000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254', function(err, block) {
if (err) {
console.log(err);
}
console.log('block', block);
});
});
bitcoind.on('open', function(status) {
bitcoind.log('status="%s"', status);
});

View File

@ -304,6 +304,10 @@ Bitcoin.prototype.start = function(options, callback) {
self.stop();
});
bitcoindjs.onBlocksReady(function(err, result) {
self.emit('ready', result);
});
setTimeout(function callee() {
// Wait until wallet is loaded:
if (callback) {

View File

@ -43,6 +43,12 @@ async_start_node(uv_work_t *req);
static void
async_start_node_after(uv_work_t *req);
static void
async_blocks_ready(uv_work_t *req);
static void
async_blocks_ready_after(uv_work_t *req);
static void
async_stop_node(uv_work_t *req);
@ -149,6 +155,17 @@ static bool g_txindex = false;
* Used for async functions and necessary linked lists at points.
*/
/**
* 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;
};
/**
* async_node_data
* Where the uv async request data resides.
@ -292,6 +309,82 @@ set_cooked(void);
* Functions
*/
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()) {
usleep(1E4);
}
}
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;
}
/**
* StartBitcoind()
* bitcoind.start(callback)
@ -746,15 +839,20 @@ async_get_block(uv_work_t *req) {
std::string strHash = data->hash;
uint256 hash(strHash);
CBlock cblock;
CBlockIndex* pblockindex = mapBlockIndex[hash];
if (ReadBlockFromDisk(cblock, pblockindex)) {
data->cblock = cblock;
data->cblock_index = pblockindex;
if (mapBlockIndex.count(hash) == 0) {
data->err_msg = std::string("Block not found.");
} else {
data->err_msg = std::string("Block not found.");
CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];
if(!ReadBlockFromDisk(block, pblockindex)) {
data->err_msg = std::string("Can't read block from disk");
} else {
data->cblock = block;
data->cblock_index = pblockindex;
}
}
}
@ -3494,6 +3592,7 @@ init(Handle<Object> target) {
NanScope();
NODE_SET_METHOD(target, "start", StartBitcoind);
NODE_SET_METHOD(target, "onBlocksReady", OnBlocksReady);
NODE_SET_METHOD(target, "stop", StopBitcoind);
NODE_SET_METHOD(target, "stopping", IsStopping);
NODE_SET_METHOD(target, "stopped", IsStopped);

View File

@ -18,6 +18,7 @@
#include <boost/filesystem.hpp>
NAN_METHOD(StartBitcoind);
NAN_METHOD(OnBlocksReady);
NAN_METHOD(IsStopping);
NAN_METHOD(IsStopped);
NAN_METHOD(StopBitcoind);