diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 5f4b600b..93fde781 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -791,28 +791,24 @@ ChainDB.prototype.getTips = function getTips() { ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { var state = this.state; - var raw, coins; + var raw; if (this.options.spv) return; - if (this.coinCache.capacity !== 0) { - coins = yield this.getCoins(hash); + raw = this.coinCache.get(hash); - if (!coins) - return; - - if (state === this.state) - this.coinCache.set(hash, coins); - - return coins.getCoin(index); - } + if (raw) + return Coins.parseCoin(raw, hash, index); raw = yield this.db.get(layout.c(hash)); if (!raw) return; + if (state === this.state) + this.coinCache.set(hash, raw); + return Coins.parseCoin(raw, hash, index); }); @@ -823,24 +819,22 @@ ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { */ ChainDB.prototype.getCoins = co(function* getCoins(hash) { - var raw, coins; + var raw; if (this.options.spv) return; - coins = this.coinCache.get(hash); + raw = this.coinCache.get(hash); - if (coins) - return coins; + if (raw) + return Coins.fromRaw(raw, hash); raw = yield this.db.get(layout.c(hash)); if (!raw) return; - coins = Coins.fromRaw(raw, hash); - - return coins; + return Coins.fromRaw(raw, hash); }); /** @@ -1643,7 +1637,7 @@ ChainDB.prototype.removeBlock = co(function* removeBlock(hash) { */ ChainDB.prototype.saveView = function saveView(view) { - var i, coins; + var i, coins, raw; view = view.toArray(); @@ -1653,8 +1647,9 @@ ChainDB.prototype.saveView = function saveView(view) { this.del(layout.c(coins.hash)); this.coinCache.unpush(coins.hash); } else { - this.put(layout.c(coins.hash), coins.toRaw()); - this.coinCache.push(coins.hash, coins); + raw = coins.toRaw(); + this.put(layout.c(coins.hash), raw); + this.coinCache.push(coins.hash, raw); } } }; @@ -2226,23 +2221,7 @@ CacheUpdate.prototype.toRaw = function toRaw() { */ function getSize(value) { - var half = 4; // Average half of coins - var total = 0; - - total += 128; // coins - total += 88; // coins hash - total += 32; // coins outputs - - // Average compressed coin size - // compressed coin = 112 - // raw = 80 - // pubkeyhash buffer = 26 - total += half * (112 + 80 + 26); - - // Average coin size - total += half * 509; - - return total; + return value.length + 80; } function BlockPair(hash, height) {