Skip to content

Commit 00d3a0b

Browse files
author
Patrick Nagurny
committed
binary encode key and value
1 parent eaee098 commit 00d3a0b

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

lib/services/address/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ AddressService.dependencies = [
3535
];
3636

3737
AddressService.PREFIXES = {
38-
OUTPUTS: new Buffer('32', 'hex'),
39-
SPENTS: new Buffer('33', 'hex')
38+
OUTPUTS: new Buffer('02', 'hex'),
39+
SPENTS: new Buffer('03', 'hex')
4040
};
4141

4242
AddressService.SPACER_MIN = new Buffer('00', 'hex');

lib/services/db.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ util.inherits(DB, Service);
6161
DB.dependencies = ['bitcoind'];
6262

6363
DB.PREFIXES = {
64-
BLOCKS: 'blk'
64+
BLOCKS: new Buffer('01', 'hex')
6565
};
6666

6767
DB.prototype._setDataPath = function() {
@@ -200,15 +200,18 @@ DB.prototype.getBlock = function(hash, callback) {
200200
};
201201

202202
DB.prototype.getBlockHashesByTimestamp = function(start, end, callback) {
203+
var self = this;
203204
var hashes = [];
204205

205206
var stream = this.store.createReadStream({
206-
start: [DB.PREFIXES.BLOCKS, start].join('-'),
207-
end: [DB.PREFIXES.BLOCKS, end].join('-')
207+
start: this._encodeBlockIndexKey(start),
208+
end: this._encodeBlockIndexKey(end),
209+
valueEncoding: 'binary',
210+
keyEncoding: 'binary'
208211
});
209212

210213
stream.on('data', function(data) {
211-
hashes.push(data.value);
214+
hashes.push(self._decodeBlockIndexValue(data.value));
212215
});
213216

214217
var error;
@@ -410,8 +413,8 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
410413
// Update block index
411414
operations.push({
412415
type: add ? 'put' : 'del',
413-
key: [DB.PREFIXES.BLOCKS, block.header.timestamp].join('-'),
414-
value: block.hash
416+
key: this._encodeBlockIndexKey(block.header.timestamp),
417+
value: this._encodeBlockIndexValue(block.hash)
415418
});
416419

417420
async.eachSeries(
@@ -445,6 +448,20 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
445448
);
446449
};
447450

451+
DB.prototype._encodeBlockIndexKey = function(timestamp) {
452+
var timestampBuffer = new Buffer(4);
453+
timestampBuffer.writeUInt32BE(timestamp);
454+
return Buffer.concat([DB.PREFIXES.BLOCKS, timestampBuffer]);
455+
};
456+
457+
DB.prototype._encodeBlockIndexValue = function(hash) {
458+
return new Buffer(hash, 'hex');
459+
};
460+
461+
DB.prototype._decodeBlockIndexValue = function(value) {
462+
return value.toString('hex');
463+
};
464+
448465
/**
449466
* This function will find the common ancestor between the current chain and a forked block,
450467
* by moving backwards from the forked block until it meets the current chain.

test/services/address/index.unit.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ describe('Address Service', function() {
126126
should.not.exist(err);
127127
operations.length.should.equal(81);
128128
operations[0].type.should.equal('put');
129-
operations[0].key.toString('hex').should.equal('3202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
129+
operations[0].key.toString('hex').should.equal('0202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
130130
operations[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
131131
operations[3].type.should.equal('put');
132-
operations[3].key.toString('hex').should.equal('33fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
132+
operations[3].key.toString('hex').should.equal('03fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
133133
operations[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
134134
operations[64].type.should.equal('put');
135-
operations[64].key.toString('hex').should.equal('329780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
135+
operations[64].key.toString('hex').should.equal('029780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
136136
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
137137
done();
138138
});
@@ -149,13 +149,13 @@ describe('Address Service', function() {
149149
should.not.exist(err);
150150
operations.length.should.equal(81);
151151
operations[0].type.should.equal('del');
152-
operations[0].key.toString('hex').should.equal('3202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
152+
operations[0].key.toString('hex').should.equal('0202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
153153
operations[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
154154
operations[3].type.should.equal('del');
155-
operations[3].key.toString('hex').should.equal('33fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
155+
operations[3].key.toString('hex').should.equal('03fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
156156
operations[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
157157
operations[64].type.should.equal('del');
158-
operations[64].key.toString('hex').should.equal('329780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
158+
operations[64].key.toString('hex').should.equal('029780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
159159
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
160160
done();
161161
});
@@ -563,7 +563,7 @@ describe('Address Service', function() {
563563
});
564564
createReadStreamCallCount.should.equal(1);
565565
var data = {
566-
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
566+
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
567567
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
568568
};
569569
testStream.emit('data', data);
@@ -611,12 +611,12 @@ describe('Address Service', function() {
611611
});
612612

613613
var data1 = {
614-
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
614+
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
615615
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
616616
};
617617

618618
var data2 = {
619-
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
619+
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
620620
value: new Buffer('40c388000000000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
621621
};
622622

test/services/db.unit.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,13 @@ describe('DB Service', function() {
394394
});
395395

396396
readStream.emit('data', {
397-
key: 'blk-' + block1.timestamp,
398-
value: block1.hash
397+
key: db._encodeBlockIndexKey(block1.timestamp),
398+
value: db._encodeBlockIndexValue(block1.hash)
399399
});
400400

401401
readStream.emit('data', {
402-
key: 'blk-' + block2.timestamp,
403-
value: block2.hash
402+
key: db._encodeBlockIndexKey(block2.timestamp),
403+
value: db._encodeBlockIndexValue(block2.hash)
404404
});
405405

406406
readStream.emit('close');
@@ -718,9 +718,9 @@ describe('DB Service', function() {
718718
should.not.exist(err);
719719
var blockOp = {
720720
type: 'put',
721-
key: 'blk-1441906365',
722-
value: '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863'
723-
}
721+
key: db._encodeBlockIndexKey(1441906365),
722+
value: db._encodeBlockIndexValue('00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863')
723+
};
724724
db.store.batch.args[0][0].should.deep.equal([blockOp, 'op1', 'op2', 'op3', 'op4', 'op5']);
725725
done();
726726
});

0 commit comments

Comments
 (0)