refactor example.

This commit is contained in:
Christopher Jeffrey 2014-09-25 15:22:48 -07:00
parent 6eb95cf293
commit ebb2341323
2 changed files with 37 additions and 25 deletions

View File

@ -7,6 +7,7 @@
process.title = 'bitcoind.js'; process.title = 'bitcoind.js';
var util = require('util'); var util = require('util');
var argv = require('optimist').argv;
/** /**
* bitcoind * bitcoind
@ -24,31 +25,38 @@ bitcoind.on('error', function(err) {
bitcoind.on('open', function(status) { bitcoind.on('open', function(status) {
print('status="%s"', status); print('status="%s"', status);
// getBlocks(bitcoind); if (argv.blocks) {
getBlocks(bitcoind);
}
// bitcoind.on('block', function(block) { if (argv['on-block']) {
// print('Found block:'); bitcoind.on('block', function(block) {
// print(block); print('Found Block:');
// }); print(block);
});
}
// bitcoind.on('tx', function(tx) { if (argv['on-tx']) {
// print('Found tx:'); bitcoind.on('tx', function(tx) {
// print(tx); print('Found TX:');
// });
bitcoind.once('tx', function(tx) {
print('Broadcasting tx...');
return tx.broadcast(function(err, hash, tx) {
if (err) throw err;
print('tx hash: %s', hash);
print(tx); print(tx);
}); });
}); bitcoind.on('mptx', function(mptx) {
print('Found mempool TX:');
print(mptx);
});
}
bitcoind.on('mptx', function(mptx) { if (argv.broadcast) {
print('Found mempool tx:'); bitcoind.once('tx', function(tx) {
print(mptx); print('Broadcasting TX...');
}); return tx.broadcast(function(err, hash, tx) {
if (err) throw err;
print('TX Hash: %s', hash);
print(tx);
});
});
}
}); });
/** /**
@ -60,10 +68,12 @@ function getBlocks(bitcoind) {
return (function next(hash) { return (function next(hash) {
return bitcoind.getBlock(hash, function(err, block) { return bitcoind.getBlock(hash, function(err, block) {
if (err) return print(err.message); if (err) return print(err.message);
print(block); print(block);
if (block.tx.length && block.tx[0].txid) {
if (argv['get-tx'] && block.tx.length && block.tx[0].txid) {
var txid = block.tx[0].txid; var txid = block.tx[0].txid;
// XXX Dies with a segfault! // XXX Dies with a segfault
// bitcoind.getTx(txid, hash, function(err, tx) { // bitcoind.getTx(txid, hash, function(err, tx) {
bitcoind.getTx(txid, function(err, tx) { bitcoind.getTx(txid, function(err, tx) {
if (err) return print(err.message); if (err) return print(err.message);
@ -72,8 +82,9 @@ function getBlocks(bitcoind) {
print('/TX ----------------------------------------------------'); print('/TX ----------------------------------------------------');
}); });
} }
if (process.argv[2] === '-r' && block.nextblockhash) {
return setTimeout(next.bind(null, block.nextblockhash), 500); if (argv.all && block.nextblockhash) {
setTimeout(next.bind(null, block.nextblockhash), 500);
} }
}); });
})(genesisBlock); })(genesisBlock);

View File

@ -21,6 +21,7 @@
"bn.js": "^0.10.0" "bn.js": "^0.10.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "~1.16.2" "mocha": "~1.16.2",
"optimist": "0.6.0"
} }
} }