binary keys work.
This commit is contained in:
parent
fd7a42a240
commit
68d8a6f2d3
@ -36,74 +36,131 @@ var Parser = bcoin.protocol.parser;
|
|||||||
var layout = {
|
var layout = {
|
||||||
R: function R() {
|
R: function R() {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('R', 'ascii');
|
p.writeU8(0x52); // R
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
e: function e(hash) {
|
e: function e(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('e', 'ascii');
|
p.writeU8(0x65); // e
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
h: function h(hash) {
|
h: function h(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('h', 'ascii');
|
p.writeU8(0x68); // h
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
H: function H(height) {
|
H: function H(height) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('H', 'ascii');
|
p.writeU8(0x48); // H
|
||||||
p.writeU32(height);
|
p.writeU32(height);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
n: function n(hash) {
|
n: function n(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('n', 'ascii');
|
p.writeU8(0x6e); // n
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
b: function b(hash) {
|
b: function b(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('b', 'ascii');
|
p.writeU8(0x62); // b
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
t: function t(hash) {
|
t: function t(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('t', 'ascii');
|
p.writeU8(0x74); // t
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
c: function c(hash) {
|
c: function c(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('c', 'ascii');
|
p.writeU8(0x63); // c
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
u: function u(hash) {
|
u: function u(hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('u', 'ascii');
|
p.writeU8(0x75); // u
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
|
return p.render();
|
||||||
|
},
|
||||||
|
q: function q(height) {
|
||||||
|
var p = new BufferWriter();
|
||||||
|
p.writeU8(0x71); // q
|
||||||
|
p.writeU32(height);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
T: function T(address, hash) {
|
T: function T(address, hash) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('T', 'ascii');
|
if (address.length === 64)
|
||||||
p.writeString(address, 'hex');
|
p.writeU8(0x41); // A
|
||||||
p.writeString('\0', 'binary');
|
else
|
||||||
|
p.writeU8(0x54); // T
|
||||||
|
p.writeHash(address);
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
},
|
},
|
||||||
C: function C(address, hash, index) {
|
C: function C(address, hash, index) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
p.writeString('C', 'ascii');
|
if (address.length === 64)
|
||||||
p.writeString(address, 'hex');
|
p.writeU8(0x42); // B
|
||||||
p.writeString('\0', 'binary');
|
else
|
||||||
|
p.writeU8(0x43); // C
|
||||||
|
p.writeHash(address);
|
||||||
p.writeHash(hash);
|
p.writeHash(hash);
|
||||||
p.writeString('\0', 'binary');
|
|
||||||
p.writeU32(index);
|
p.writeU32(index);
|
||||||
|
if (utils.isBrowser)
|
||||||
|
return p.render().toString('hex');
|
||||||
return p.render();
|
return p.render();
|
||||||
|
},
|
||||||
|
Cc: function Cc(key) {
|
||||||
|
var hash, index;
|
||||||
|
|
||||||
|
if (utils.isBrowser)
|
||||||
|
key = new Buffer(key, 'hex');
|
||||||
|
|
||||||
|
if (key.length === 69) {
|
||||||
|
hash = key.slice(33, 65).toString('hex');
|
||||||
|
index = key.readUInt32LE(65, 0);
|
||||||
|
} else {
|
||||||
|
hash = key.slice(21, 53).toString('hex');
|
||||||
|
index = key.readUInt32LE(53, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [hash, index];
|
||||||
|
},
|
||||||
|
Tt: function Tt(key) {
|
||||||
|
if (utils.isBrowser)
|
||||||
|
key = new Buffer(key, 'hex');
|
||||||
|
|
||||||
|
return key.length === 65
|
||||||
|
? key.slice(33).toString('hex')
|
||||||
|
: key.slice(21).toString('hex');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1082,7 +1139,7 @@ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
|||||||
return callback(null, coins);
|
return callback(null, coins);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.db.fetch('c/' + hash, function(data) {
|
this.db.fetch(layout.c(hash), function(data) {
|
||||||
self.coinCache.set(hash, data);
|
self.coinCache.set(hash, data);
|
||||||
return bcoin.coins.parseCoin(data, hash, index);
|
return bcoin.coins.parseCoin(data, hash, index);
|
||||||
}, callback);
|
}, callback);
|
||||||
@ -1175,12 +1232,10 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call
|
|||||||
return next();
|
return next();
|
||||||
|
|
||||||
self.db.iterate({
|
self.db.iterate({
|
||||||
gte: 'C/' + address,
|
gte: layout.C(address, constants.NULL_HASH, 0),
|
||||||
lte: 'C/' + address + '~',
|
lte: layout.C(address, constants.MAX_HASH, 0xffffffff),
|
||||||
transform: function(key) {
|
keyAsBuffer: !utils.isBrowser,
|
||||||
key = key.split('/');
|
transform: layout.Cc
|
||||||
return [key[2], +key[3]];
|
|
||||||
}
|
|
||||||
}, function(err, keys) {
|
}, function(err, keys) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -1222,29 +1277,28 @@ ChainDB.prototype.getTXByAddress = function getTXByAddress(addresses, callback)
|
|||||||
|
|
||||||
utils.forEachSerial(addresses, function(address, next) {
|
utils.forEachSerial(addresses, function(address, next) {
|
||||||
address = bcoin.address.getHash(address);
|
address = bcoin.address.getHash(address);
|
||||||
|
|
||||||
if (!address)
|
if (!address)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
self.db.lookup({
|
self.db.lookup({
|
||||||
gte: 'T/' + address,
|
gte: layout.T(address, constants.NULL_HASH),
|
||||||
lte: 'T/' + address + '~',
|
lte: layout.T(address, constants.MAX_HASH),
|
||||||
|
keyAsBuffer: !utils.isBrowser,
|
||||||
transform: function(key) {
|
transform: function(key) {
|
||||||
var hash = key.split('/')[2];
|
var hash = layout.Tt(key);
|
||||||
if (addresses.length > 1) {
|
|
||||||
if (have[hash])
|
if (have[hash])
|
||||||
return false;
|
return;
|
||||||
have[hash] = true;
|
|
||||||
}
|
have[hash] = true;
|
||||||
return 't/' + hash;
|
|
||||||
|
return layout.t(hash);
|
||||||
},
|
},
|
||||||
parse: function(data, key) {
|
parse: function(data, key) {
|
||||||
return bcoin.tx.fromRaw(data);
|
txs.push(bcoin.tx.fromRaw(data));
|
||||||
}
|
}
|
||||||
}, function(err, tx) {
|
}, next);
|
||||||
if (err)
|
|
||||||
return next(err);
|
|
||||||
txs = txs.concat(tx);
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -1468,11 +1522,11 @@ ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) {
|
|||||||
if (block.height <= this.network.block.pruneAfterHeight)
|
if (block.height <= this.network.block.pruneAfterHeight)
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
futureHeight = pad32(block.height + this.keepBlocks);
|
futureHeight = block.height + this.keepBlocks;
|
||||||
|
|
||||||
batch.put('q/' + futureHeight, block.hash());
|
batch.put(layout.q(futureHeight), block.hash());
|
||||||
|
|
||||||
key = 'q/' + pad32(block.height);
|
key = layout.q(block.height);
|
||||||
|
|
||||||
this.db.fetch(key, function(data) {
|
this.db.fetch(key, function(data) {
|
||||||
assert(data.length === 32, 'Database corruption.');
|
assert(data.length === 32, 'Database corruption.');
|
||||||
|
|||||||
@ -245,7 +245,7 @@ LowlevelUp.prototype.iterate = function iterate(options, callback) {
|
|||||||
keys: true,
|
keys: true,
|
||||||
values: options.values,
|
values: options.values,
|
||||||
fillCache: false,
|
fillCache: false,
|
||||||
keyAsBuffer: false,
|
keyAsBuffer: options.keyAsBuffer || false,
|
||||||
limit: options.limit,
|
limit: options.limit,
|
||||||
reverse: options.reverse
|
reverse: options.reverse
|
||||||
};
|
};
|
||||||
|
|||||||
@ -597,6 +597,15 @@ exports.ZERO_HASH = new Buffer(
|
|||||||
exports.NULL_HASH =
|
exports.NULL_HASH =
|
||||||
'0000000000000000000000000000000000000000000000000000000000000000';
|
'0000000000000000000000000000000000000000000000000000000000000000';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hash of all 0xff.
|
||||||
|
* @const {String}
|
||||||
|
* @default
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.MAX_HASH =
|
||||||
|
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BCoin version.
|
* BCoin version.
|
||||||
* @const {String}
|
* @const {String}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user