binary encode key and value

This commit is contained in:
Patrick Nagurny 2015-09-15 16:40:31 -04:00
parent eaee098cf0
commit 00d3a0ba67
4 changed files with 41 additions and 24 deletions

View File

@ -35,8 +35,8 @@ AddressService.dependencies = [
];
AddressService.PREFIXES = {
OUTPUTS: new Buffer('32', 'hex'),
SPENTS: new Buffer('33', 'hex')
OUTPUTS: new Buffer('02', 'hex'),
SPENTS: new Buffer('03', 'hex')
};
AddressService.SPACER_MIN = new Buffer('00', 'hex');

View File

@ -61,7 +61,7 @@ util.inherits(DB, Service);
DB.dependencies = ['bitcoind'];
DB.PREFIXES = {
BLOCKS: 'blk'
BLOCKS: new Buffer('01', 'hex')
};
DB.prototype._setDataPath = function() {
@ -200,15 +200,18 @@ DB.prototype.getBlock = function(hash, callback) {
};
DB.prototype.getBlockHashesByTimestamp = function(start, end, callback) {
var self = this;
var hashes = [];
var stream = this.store.createReadStream({
start: [DB.PREFIXES.BLOCKS, start].join('-'),
end: [DB.PREFIXES.BLOCKS, end].join('-')
start: this._encodeBlockIndexKey(start),
end: this._encodeBlockIndexKey(end),
valueEncoding: 'binary',
keyEncoding: 'binary'
});
stream.on('data', function(data) {
hashes.push(data.value);
hashes.push(self._decodeBlockIndexValue(data.value));
});
var error;
@ -410,8 +413,8 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
// Update block index
operations.push({
type: add ? 'put' : 'del',
key: [DB.PREFIXES.BLOCKS, block.header.timestamp].join('-'),
value: block.hash
key: this._encodeBlockIndexKey(block.header.timestamp),
value: this._encodeBlockIndexValue(block.hash)
});
async.eachSeries(
@ -445,6 +448,20 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
);
};
DB.prototype._encodeBlockIndexKey = function(timestamp) {
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp);
return Buffer.concat([DB.PREFIXES.BLOCKS, timestampBuffer]);
};
DB.prototype._encodeBlockIndexValue = function(hash) {
return new Buffer(hash, 'hex');
};
DB.prototype._decodeBlockIndexValue = function(value) {
return value.toString('hex');
};
/**
* This function will find the common ancestor between the current chain and a forked block,
* by moving backwards from the forked block until it meets the current chain.

View File

@ -126,13 +126,13 @@ describe('Address Service', function() {
should.not.exist(err);
operations.length.should.equal(81);
operations[0].type.should.equal('put');
operations[0].key.toString('hex').should.equal('3202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
operations[0].key.toString('hex').should.equal('0202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
operations[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
operations[3].type.should.equal('put');
operations[3].key.toString('hex').should.equal('33fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
operations[3].key.toString('hex').should.equal('03fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
operations[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
operations[64].type.should.equal('put');
operations[64].key.toString('hex').should.equal('329780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
operations[64].key.toString('hex').should.equal('029780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
done();
});
@ -149,13 +149,13 @@ describe('Address Service', function() {
should.not.exist(err);
operations.length.should.equal(81);
operations[0].type.should.equal('del');
operations[0].key.toString('hex').should.equal('3202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
operations[0].key.toString('hex').should.equal('0202a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b00000543abfdbefe0d064729d85556bd3ab13c3a889b685d042499c02b4aa2064fb1e1692300000000');
operations[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
operations[3].type.should.equal('del');
operations[3].key.toString('hex').should.equal('33fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
operations[3].key.toString('hex').should.equal('03fdbd324b28ea69e49c998816407dc055fb81d06e00000543ab3d7d5d98df753ef2a4f82438513c509e3b11f3e738e94a7234967b03a03123a900000020');
operations[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
operations[64].type.should.equal('del');
operations[64].key.toString('hex').should.equal('329780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
operations[64].key.toString('hex').should.equal('029780ccd5356e2acc0ee439ee04e0fe69426c752800000543abe66f3b989c790178de2fc1a5329f94c0d8905d0d3df4e7ecf0115e7f90a6283d00000001');
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
done();
});
@ -563,7 +563,7 @@ describe('Address Service', function() {
});
createReadStreamCallCount.should.equal(1);
var data = {
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
};
testStream.emit('data', data);
@ -611,12 +611,12 @@ describe('Address Service', function() {
});
var data1 = {
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
};
var data2 = {
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
value: new Buffer('40c388000000000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
};

View File

@ -394,13 +394,13 @@ describe('DB Service', function() {
});
readStream.emit('data', {
key: 'blk-' + block1.timestamp,
value: block1.hash
key: db._encodeBlockIndexKey(block1.timestamp),
value: db._encodeBlockIndexValue(block1.hash)
});
readStream.emit('data', {
key: 'blk-' + block2.timestamp,
value: block2.hash
key: db._encodeBlockIndexKey(block2.timestamp),
value: db._encodeBlockIndexValue(block2.hash)
});
readStream.emit('close');
@ -718,9 +718,9 @@ describe('DB Service', function() {
should.not.exist(err);
var blockOp = {
type: 'put',
key: 'blk-1441906365',
value: '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863'
}
key: db._encodeBlockIndexKey(1441906365),
value: db._encodeBlockIndexValue('00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863')
};
db.store.batch.args[0][0].should.deep.equal([blockOp, 'op1', 'op2', 'op3', 'op4', 'op5']);
done();
});