diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 007c75c8..03721d2e 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -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); diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 34e5d2b6..3ea0b292 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -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 }; };