txdb: fixes for input writing.
This commit is contained in:
parent
abcf48cbe9
commit
bd4367a2d1
@ -452,7 +452,7 @@ HTTPServer.prototype.handleAuth = function handleAuth(socket) {
|
|||||||
return entry.toRaw();
|
return entry.toRaw();
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.hook('get hashes', (args) => {
|
socket.hook('get hashes', async (args) => {
|
||||||
const valid = new Validator([args]);
|
const valid = new Validator([args]);
|
||||||
const start = valid.i32(0, -1);
|
const start = valid.i32(0, -1);
|
||||||
const end = valid.i32(1, -1);
|
const end = valid.i32(1, -1);
|
||||||
|
|||||||
@ -195,10 +195,9 @@ TXDB.prototype.hasPath = function hasPath(output) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.saveCredit = async function saveCredit(b, credit, path) {
|
TXDB.prototype.saveCredit = async function saveCredit(b, credit, path) {
|
||||||
const coin = credit.coin;
|
const {coin} = credit;
|
||||||
const raw = credit.toRaw();
|
|
||||||
|
|
||||||
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);
|
b.put(layout.C(path.account, coin.hash, coin.index), null);
|
||||||
|
|
||||||
return this.addOutpointMap(b, coin.hash, coin.index);
|
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) {
|
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(coin.hash, coin.index));
|
||||||
b.del(layout.C(path.account, 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) {
|
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);
|
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());
|
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) {
|
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);
|
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));
|
b.del(layout.d(spender.hash, spender.index));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -252,11 +251,11 @@ TXDB.prototype.unspendCredit = function unspendCredit(b, tx, index) {
|
|||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.writeInput = async function writeInput(tx, index) {
|
TXDB.prototype.writeInput = async function writeInput(b, tx, index) {
|
||||||
const {hash, index} = tx.inputs[index].prevout;
|
const prevout = tx.inputs[index].prevout;
|
||||||
const spender = Outpoint.fromTX(tx, index);
|
const spender = Outpoint.fromTX(tx, index);
|
||||||
this.put(layout.s(hash, index), spender.toRaw());
|
b.put(layout.s(prevout.hash, prevout.index), spender.toRaw());
|
||||||
return this.addOutpointMap(b, hash, index);
|
return this.addOutpointMap(b, prevout.hash, prevout.index);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -265,10 +264,10 @@ TXDB.prototype.writeInput = async function writeInput(tx, index) {
|
|||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.removeInput = async function removeInput(tx, index) {
|
TXDB.prototype.removeInput = async function removeInput(b, tx, index) {
|
||||||
const {hash, index} = tx.inputs[index].prevout;
|
const prevout = tx.inputs[index].prevout;
|
||||||
this.del(layout.s(hash, index));
|
b.del(layout.s(prevout.hash, prevout.index));
|
||||||
return this.removeOutpointMap(b, hash, 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.
|
// Watch all inputs for incoming txs.
|
||||||
// This allows us to check for double spends.
|
// This allows us to check for double spends.
|
||||||
if (!block)
|
if (!block)
|
||||||
await this.writeInput(tx, i);
|
await this.writeInput(b, tx, i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -713,7 +712,7 @@ TXDB.prototype.confirm = async function confirm(wtx, block) {
|
|||||||
// There may be new credits available
|
// There may be new credits available
|
||||||
// that we haven't seen yet.
|
// that we haven't seen yet.
|
||||||
if (!credit) {
|
if (!credit) {
|
||||||
await this.removeInput(tx, i);
|
await this.removeInput(b, tx, i);
|
||||||
|
|
||||||
credit = await this.getCredit(hash, index);
|
credit = await this.getCredit(hash, index);
|
||||||
|
|
||||||
@ -853,7 +852,7 @@ TXDB.prototype.erase = async function erase(wtx, block) {
|
|||||||
|
|
||||||
if (!credit) {
|
if (!credit) {
|
||||||
if (!block)
|
if (!block)
|
||||||
await this.removeInput(tx, i);
|
await this.removeInput(b, tx, i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1048,7 +1047,7 @@ TXDB.prototype.disconnect = async function disconnect(wtx, block) {
|
|||||||
const credit = credits[i];
|
const credit = credits[i];
|
||||||
|
|
||||||
if (!credit) {
|
if (!credit) {
|
||||||
await this.writeInput(tx, i);
|
await this.writeInput(b, tx, i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1170,16 +1169,15 @@ TXDB.prototype.removeConflict = async function removeConflict(wtx) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.removeConflicts = async function removeConflicts(tx, conf) {
|
TXDB.prototype.removeConflicts = async function removeConflicts(tx, conf) {
|
||||||
const hash = tx.hash('hex');
|
|
||||||
const spends = [];
|
|
||||||
|
|
||||||
if (tx.isCoinbase())
|
if (tx.isCoinbase())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
const txid = tx.hash('hex');
|
||||||
|
const spends = [];
|
||||||
|
|
||||||
// Gather all spent records first.
|
// Gather all spent records first.
|
||||||
for (let i = 0; i < tx.inputs.length; i++) {
|
for (const {prevout} of tx.inputs) {
|
||||||
const input = tx.inputs[i];
|
const {hash, index} = prevout;
|
||||||
const {hash, index} = input.prevout;
|
|
||||||
|
|
||||||
// Is it already spent?
|
// Is it already spent?
|
||||||
const spent = await this.getSpent(hash, index);
|
const spent = await this.getSpent(hash, index);
|
||||||
@ -1188,7 +1186,7 @@ TXDB.prototype.removeConflicts = async function removeConflicts(tx, conf) {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Did _we_ spend it?
|
// Did _we_ spend it?
|
||||||
if (spent.hash === hash)
|
if (spent.hash === txid)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const spender = await this.getTX(spent.hash);
|
const spender = await this.getTX(spent.hash);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user