From bd4367a2d1ab0ce86a8f9588abf4442e98b95c7e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 19 Oct 2017 12:29:36 -0700 Subject: [PATCH] txdb: fixes for input writing. --- lib/http/server.js | 2 +- lib/wallet/txdb.js | 52 ++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/http/server.js b/lib/http/server.js index 5eb7b550..d259aed7 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -452,7 +452,7 @@ HTTPServer.prototype.handleAuth = function handleAuth(socket) { return entry.toRaw(); }); - socket.hook('get hashes', (args) => { + socket.hook('get hashes', async (args) => { const valid = new Validator([args]); const start = valid.i32(0, -1); const end = valid.i32(1, -1); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index d408ac19..92ec8777 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -195,10 +195,9 @@ TXDB.prototype.hasPath = function hasPath(output) { */ TXDB.prototype.saveCredit = async function saveCredit(b, credit, path) { - const coin = credit.coin; - const raw = credit.toRaw(); + const {coin} = credit; - b.put(layout.c(coin.hash, coin.index), raw); + b.put(layout.c(coin.hash, coin.index), credit.toRaw()); b.put(layout.C(path.account, coin.hash, coin.index), null); return this.addOutpointMap(b, coin.hash, coin.index); @@ -211,7 +210,7 @@ TXDB.prototype.saveCredit = async function saveCredit(b, credit, path) { */ TXDB.prototype.removeCredit = async function removeCredit(b, credit, path) { - const coin = credit.coin; + const {coin} = credit; b.del(layout.c(coin.hash, coin.index)); b.del(layout.C(path.account, coin.hash, coin.index)); @@ -227,9 +226,9 @@ TXDB.prototype.removeCredit = async function removeCredit(b, credit, path) { */ TXDB.prototype.spendCredit = function spendCredit(b, credit, tx, index) { - const {hash, index} = tx.inputs[index].prevout; + const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - b.put(layout.s(hash, index), spender.toRaw()); + b.put(layout.s(prevout.hash, prevout.index), spender.toRaw()); b.put(layout.d(spender.hash, spender.index), credit.coin.toRaw()); }; @@ -240,9 +239,9 @@ TXDB.prototype.spendCredit = function spendCredit(b, credit, tx, index) { */ TXDB.prototype.unspendCredit = function unspendCredit(b, tx, index) { - const {hash, index} = tx.inputs[index].prevout; + const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - b.del(layout.s(hash, index)); + b.del(layout.s(prevout.hash, prevout.index)); b.del(layout.d(spender.hash, spender.index)); }; @@ -252,11 +251,11 @@ TXDB.prototype.unspendCredit = function unspendCredit(b, tx, index) { * @param {Number} index */ -TXDB.prototype.writeInput = async function writeInput(tx, index) { - const {hash, index} = tx.inputs[index].prevout; +TXDB.prototype.writeInput = async function writeInput(b, tx, index) { + const prevout = tx.inputs[index].prevout; const spender = Outpoint.fromTX(tx, index); - this.put(layout.s(hash, index), spender.toRaw()); - return this.addOutpointMap(b, hash, index); + b.put(layout.s(prevout.hash, prevout.index), spender.toRaw()); + return this.addOutpointMap(b, prevout.hash, prevout.index); }; /** @@ -265,10 +264,10 @@ TXDB.prototype.writeInput = async function writeInput(tx, index) { * @param {Number} index */ -TXDB.prototype.removeInput = async function removeInput(tx, index) { - const {hash, index} = tx.inputs[index].prevout; - this.del(layout.s(hash, index)); - return this.removeOutpointMap(b, hash, index); +TXDB.prototype.removeInput = async function removeInput(b, tx, index) { + const prevout = tx.inputs[index].prevout; + b.del(layout.s(prevout.hash, prevout.index)); + return this.removeOutpointMap(b, prevout.hash, prevout.index); }; /** @@ -543,7 +542,7 @@ TXDB.prototype.insert = async function insert(wtx, block) { // Watch all inputs for incoming txs. // This allows us to check for double spends. if (!block) - await this.writeInput(tx, i); + await this.writeInput(b, tx, i); continue; } @@ -713,7 +712,7 @@ TXDB.prototype.confirm = async function confirm(wtx, block) { // There may be new credits available // that we haven't seen yet. if (!credit) { - await this.removeInput(tx, i); + await this.removeInput(b, tx, i); credit = await this.getCredit(hash, index); @@ -853,7 +852,7 @@ TXDB.prototype.erase = async function erase(wtx, block) { if (!credit) { if (!block) - await this.removeInput(tx, i); + await this.removeInput(b, tx, i); continue; } @@ -1048,7 +1047,7 @@ TXDB.prototype.disconnect = async function disconnect(wtx, block) { const credit = credits[i]; if (!credit) { - await this.writeInput(tx, i); + await this.writeInput(b, tx, i); continue; } @@ -1170,16 +1169,15 @@ TXDB.prototype.removeConflict = async function removeConflict(wtx) { */ TXDB.prototype.removeConflicts = async function removeConflicts(tx, conf) { - const hash = tx.hash('hex'); - const spends = []; - if (tx.isCoinbase()) return true; + const txid = tx.hash('hex'); + const spends = []; + // Gather all spent records first. - for (let i = 0; i < tx.inputs.length; i++) { - const input = tx.inputs[i]; - const {hash, index} = input.prevout; + for (const {prevout} of tx.inputs) { + const {hash, index} = prevout; // Is it already spent? const spent = await this.getSpent(hash, index); @@ -1188,7 +1186,7 @@ TXDB.prototype.removeConflicts = async function removeConflicts(tx, conf) { continue; // Did _we_ spend it? - if (spent.hash === hash) + if (spent.hash === txid) continue; const spender = await this.getTX(spent.hash);