Skip to content

Commit

Permalink
check for timestamp out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Nagurny committed Sep 16, 2015
1 parent 7e1d433 commit a0be38f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,16 @@ DB.prototype.getBlockHashesByTimestamp = function(high, low, callback) {
var self = this;
var hashes = [];

try {
var lowKey = this._encodeBlockIndexKey(low);
var highKey = this._encodeBlockIndexKey(high);
} catch(e) {
return callback(e);
}

var stream = this.store.createReadStream({
gte: this._encodeBlockIndexKey(low),
lte: this._encodeBlockIndexKey(high),
gte: lowKey,
lte: highKey,
reverse: true,
valueEncoding: 'binary',
keyEncoding: 'binary'
Expand Down Expand Up @@ -456,6 +463,7 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
};

DB.prototype._encodeBlockIndexKey = function(timestamp) {
$.checkArgument(timestamp >= 0 && timestamp <= 4294967295, 'timestamp out of bounds');
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp);
return Buffer.concat([DB.PREFIXES.BLOCKS, timestampBuffer]);
Expand Down
14 changes: 14 additions & 0 deletions test/services/db.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ describe('DB Service', function() {

readStream.emit('close');
});

it('should give an error if the timestamp is out of range', function(done) {
var db = new DB(baseConfig);
var readStream = new EventEmitter();
db.store = {
createReadStream: sinon.stub().returns(readStream)
};

db.getBlockHashesByTimestamp(-1, -5, function(err, hashes) {
should.exist(err);
err.message.should.equal('Invalid Argument: timestamp out of bounds');
done();
});
});
});

describe('#getPrevHash', function() {
Expand Down

0 comments on commit a0be38f

Please sign in to comment.