get coin parsing working.

This commit is contained in:
Christopher Jeffrey 2016-02-15 01:27:00 -08:00
parent 7175f81d59
commit 2a20e56f7b
2 changed files with 28 additions and 11 deletions

View File

@ -35,6 +35,7 @@ function BlockDB(options) {
options.file = process.env.HOME + '/bcoin-server-' + network.type + '.ldb';
this.options = options;
this.parser = new bcoin.protocol.parser();
this.data = new BlockData();
this.index = levelup(options.file, {
keyEncoding: 'ascii',
@ -92,7 +93,10 @@ BlockDB.prototype.saveBlock = function saveBlock(block, callback) {
batch.put('b/h/' + block.height, blockOffset);
block.txs.forEach(function(tx, i) {
var txOffset = self.createOffset(tx._size, data.offset + tx._offset, block.height);
var txOffset = self.createOffset(
tx._size, data.offset + tx._offset,
block.height);
var hash = tx.hash('hex');
var uniq = {};
@ -145,7 +149,6 @@ BlockDB.prototype.saveBlock = function saveBlock(block, callback) {
output._size,
data.offset + tx._offset + output._offset,
block.height);
// console.log(self.parseOffset(coinOffset));
if (uaddr)
batch.put('t/a/' + uaddr + '/' + hash, txOffset);
@ -357,14 +360,22 @@ BlockDB.prototype._getCoinsByAddress = function _getCoinsByAddress(address, call
var record = self.parseOffset(data.value);
pending++;
self.data.getAsync(record.size, record.offset, function(err, data) {
var coin;
if (err)
return callback(err);
if (data) {
var coin = bcoin.coin.fromRaw(data, true);
coin.hash = hash;
coin.index = index;
coin.height = record.height;
data = self.parser.parseTXOut(data);
coin = bcoin.coin({
version: 1,
hash: hash,
index: index,
height: record.height,
script: data.script,
value: data.value,
spent: false
});
coins.push(coin);
}
@ -408,10 +419,16 @@ BlockDB.prototype.getCoin = function getCoin(hash, index, callback) {
return callback(err);
if (data) {
coin = bcoin.coin.fromRaw(data, true);
coin.hash = hash;
coin.index = index;
coin.height = record.height;
data = self.parser.parseTXOut(data);
coin = bcoin.coin({
version: 1,
hash: hash,
index: index,
height: record.height,
script: data.script,
value: data.value,
spent: false
});
}
return callback(null, coin);

View File

@ -460,11 +460,11 @@ Parser.prototype.parseTX = function parseTX(p) {
}
return {
_raw: p.slice(0, off + 4),
version: utils.read32(p, 0),
inputs: txIn,
outputs: txOut,
locktime: utils.readU32(p, off),
_raw: p.slice(0, off + 4),
_size: off + 4
};
};