From fe8bb95e28e1542e5bfdb35adaa45f49aa17090d Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 4 Feb 2014 17:22:51 -0300 Subject: [PATCH] block test: list of hashes by date --- README.md | 11 ++++++----- lib/BlockDb.js | 22 ++++++++++++++++++++++ test/integration/blocklist.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 test/integration/blocklist.js diff --git a/README.md b/README.md index 4ee92d8f..9b8d6916 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ $ npm install -g bower ## Additional Packages * Express - Defined as npm module in the [package.json](package.json) file. -* Mongoose - Defined as npm module in the [package.json](package.json) file. * AngularJS - Defined as bower module in the [bower.json](bower.json) file. * Twitter Bootstrap - Defined as bower module in the [bower.json](bower.json) file. * UI Bootstrap - Defined as bower module in the [bower.json](bower.json) file. @@ -55,13 +54,15 @@ $ npm install -g bower http://localhost:3000 - If you get an error, please check the next section "Post-install" - ## Syncing old blockchain data - Run sync from insight repository (to save old blocks and transactions in MongoDB): + Run sync from insight repository (to save old blocks and transactions in + LevelDB): + + Create folders: - $ utils/sync.js + $ mkdir -p db/blocks + $ utils/sync.js -S Check utils/sync.js --help for options. diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 13dd2e8e..35f3adb8 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -129,6 +129,28 @@ function spec() { }); }; + BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) { + var self = this; + var list = []; + this.db.createReadStream({ + start: TIMESTAMP_ROOT + start_ts, + end: TIMESTAMP_ROOT + end_ts + }) + .on('data', function (data) { + list.push({ + ts: data.key.replace(TIMESTAMP_ROOT, ''), + hash: data.value, + info: {} + }); + }) + .on('error', function (err) { + return cb(err); + }) + .on('end', function () { + return cb(null, list); + }); + }; + BlockDb.blockIndex = function(height, cb) { var rpc = new RpcClient(config.bitcoind); rpc.getBlockHash(height, function(err, bh){ diff --git a/test/integration/blocklist.js b/test/integration/blocklist.js new file mode 100644 index 00000000..6b50952c --- /dev/null +++ b/test/integration/blocklist.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node +'use strict'; + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + +var TESTING_BLOCK = '00000000b7cc12abe8a9a604813aab1f2c4f3a242a021065be52393a147a1a86'; +var START_TS = '1391538611'; +var END_TS = '1391538638'; + +var + assert = require('assert'), + config = require('../../config/config'), + BlockDb = require('../../lib/BlockDb').class(); + +describe('BlockDb getHashes', function(){ + + var bdb = new BlockDb(); + it('Get Hash by Date', function(done) { + + bdb.getBlocksByDate(START_TS, END_TS, function(err, list) { + if (err) done(err); + assert.equal(list[0].ts, START_TS); + assert.equal(list[0].hash, TESTING_BLOCK); + done(); + }); + }); +}); +