binary encode key and value
This commit is contained in:
parent
eaee098cf0
commit
00d3a0ba67
@ -35,8 +35,8 @@ AddressService.dependencies = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
AddressService.PREFIXES = {
|
AddressService.PREFIXES = {
|
||||||
OUTPUTS: new Buffer('32', 'hex'),
|
OUTPUTS: new Buffer('02', 'hex'),
|
||||||
SPENTS: new Buffer('33', 'hex')
|
SPENTS: new Buffer('03', 'hex')
|
||||||
};
|
};
|
||||||
|
|
||||||
AddressService.SPACER_MIN = new Buffer('00', 'hex');
|
AddressService.SPACER_MIN = new Buffer('00', 'hex');
|
||||||
|
|||||||
@ -61,7 +61,7 @@ util.inherits(DB, Service);
|
|||||||
DB.dependencies = ['bitcoind'];
|
DB.dependencies = ['bitcoind'];
|
||||||
|
|
||||||
DB.PREFIXES = {
|
DB.PREFIXES = {
|
||||||
BLOCKS: 'blk'
|
BLOCKS: new Buffer('01', 'hex')
|
||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype._setDataPath = function() {
|
DB.prototype._setDataPath = function() {
|
||||||
@ -200,15 +200,18 @@ DB.prototype.getBlock = function(hash, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
DB.prototype.getBlockHashesByTimestamp = function(start, end, callback) {
|
DB.prototype.getBlockHashesByTimestamp = function(start, end, callback) {
|
||||||
|
var self = this;
|
||||||
var hashes = [];
|
var hashes = [];
|
||||||
|
|
||||||
var stream = this.store.createReadStream({
|
var stream = this.store.createReadStream({
|
||||||
start: [DB.PREFIXES.BLOCKS, start].join('-'),
|
start: this._encodeBlockIndexKey(start),
|
||||||
end: [DB.PREFIXES.BLOCKS, end].join('-')
|
end: this._encodeBlockIndexKey(end),
|
||||||
|
valueEncoding: 'binary',
|
||||||
|
keyEncoding: 'binary'
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('data', function(data) {
|
stream.on('data', function(data) {
|
||||||
hashes.push(data.value);
|
hashes.push(self._decodeBlockIndexValue(data.value));
|
||||||
});
|
});
|
||||||
|
|
||||||
var error;
|
var error;
|
||||||
@ -410,8 +413,8 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
|
|||||||
// Update block index
|
// Update block index
|
||||||
operations.push({
|
operations.push({
|
||||||
type: add ? 'put' : 'del',
|
type: add ? 'put' : 'del',
|
||||||
key: [DB.PREFIXES.BLOCKS, block.header.timestamp].join('-'),
|
key: this._encodeBlockIndexKey(block.header.timestamp),
|
||||||
value: block.hash
|
value: this._encodeBlockIndexValue(block.hash)
|
||||||
});
|
});
|
||||||
|
|
||||||
async.eachSeries(
|
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,
|
* 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.
|
* by moving backwards from the forked block until it meets the current chain.
|
||||||
|
|||||||
@ -126,13 +126,13 @@ describe('Address Service', function() {
|
|||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
operations.length.should.equal(81);
|
operations.length.should.equal(81);
|
||||||
operations[0].type.should.equal('put');
|
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[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
|
||||||
operations[3].type.should.equal('put');
|
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[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
|
||||||
operations[64].type.should.equal('put');
|
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');
|
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -149,13 +149,13 @@ describe('Address Service', function() {
|
|||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
operations.length.should.equal(81);
|
operations.length.should.equal(81);
|
||||||
operations[0].type.should.equal('del');
|
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[0].value.toString('hex').should.equal('41e2a49ec1c0000076a91402a61d2066d19e9e2fd348a8320b7ebd4dd3ca2b88ac');
|
||||||
operations[3].type.should.equal('del');
|
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[3].value.toString('hex').should.equal('5780f3ee54889a0717152a01abee9a32cec1b0cdf8d5537a08c7bd9eeb6bfbca00000000');
|
||||||
operations[64].type.should.equal('del');
|
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');
|
operations[64].value.toString('hex').should.equal('4147a6b00000000076a9149780ccd5356e2acc0ee439ee04e0fe69426c752888ac');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -563,7 +563,7 @@ describe('Address Service', function() {
|
|||||||
});
|
});
|
||||||
createReadStreamCallCount.should.equal(1);
|
createReadStreamCallCount.should.equal(1);
|
||||||
var data = {
|
var data = {
|
||||||
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
|
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b687000000000f125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
|
||||||
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
||||||
};
|
};
|
||||||
testStream.emit('data', data);
|
testStream.emit('data', data);
|
||||||
@ -611,12 +611,12 @@ describe('Address Service', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var data1 = {
|
var data1 = {
|
||||||
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
|
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543a8125dd0e50fc732d67c37b6c56be7f9dc00b6859cebf982ee2cc83ed2d604bf8700000001', 'hex'),
|
||||||
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
value: new Buffer('41f0de058a80000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
||||||
};
|
};
|
||||||
|
|
||||||
var data2 = {
|
var data2 = {
|
||||||
key: new Buffer('32038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
|
key: new Buffer('02038a213afdfc551fc658e9a2a58a86e98d69b68700000543ac3b6bc2939d1a70ce04bc4f619ee32608fbff5e565c1f9b02e4eaa97959c59ae700000002', 'hex'),
|
||||||
value: new Buffer('40c388000000000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
value: new Buffer('40c388000000000076a914038a213afdfc551fc658e9a2a58a86e98d69b68788ac', 'hex')
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -394,13 +394,13 @@ describe('DB Service', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
readStream.emit('data', {
|
readStream.emit('data', {
|
||||||
key: 'blk-' + block1.timestamp,
|
key: db._encodeBlockIndexKey(block1.timestamp),
|
||||||
value: block1.hash
|
value: db._encodeBlockIndexValue(block1.hash)
|
||||||
});
|
});
|
||||||
|
|
||||||
readStream.emit('data', {
|
readStream.emit('data', {
|
||||||
key: 'blk-' + block2.timestamp,
|
key: db._encodeBlockIndexKey(block2.timestamp),
|
||||||
value: block2.hash
|
value: db._encodeBlockIndexValue(block2.hash)
|
||||||
});
|
});
|
||||||
|
|
||||||
readStream.emit('close');
|
readStream.emit('close');
|
||||||
@ -718,9 +718,9 @@ describe('DB Service', function() {
|
|||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
var blockOp = {
|
var blockOp = {
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: 'blk-1441906365',
|
key: db._encodeBlockIndexKey(1441906365),
|
||||||
value: '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863'
|
value: db._encodeBlockIndexValue('00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863')
|
||||||
}
|
};
|
||||||
db.store.batch.args[0][0].should.deep.equal([blockOp, 'op1', 'op2', 'op3', 'op4', 'op5']);
|
db.store.batch.args[0][0].should.deep.equal([blockOp, 'op1', 'op2', 'op3', 'op4', 'op5']);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user