Skip to content

Commit

Permalink
txdb: update block map for all transactions which update namestates. …
Browse files Browse the repository at this point in the history
…see #564.
  • Loading branch information
chjj authored and pinheadmz committed Mar 1, 2021
1 parent 8ce8ef2 commit fcf716d
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions lib/wallet/txdb.js
Expand Up @@ -1029,19 +1029,14 @@ class TXDB {
}

// Handle names.
if (block) {
const {updated, index} = await this.connectNames(b, tx, view, height);
if (block && !await this.bucket.has(layout.U.encode(hash))) {
const updated = await this.connectNames(b, tx, view, height);

if (updated && !state.updated()) {
if (index) {
// TRANSFER, FINALIZE, and REVOKE transactions must be indexed
// so we can undo them in event of reorg or rescan.
await this.addBlockMap(b, height);
await this.addBlock(b, tx.hash(), block);
}

// Always save namestate transitions,
// even if they don't affect wallet balance
await this.addBlockMap(b, height);
await this.addBlock(b, tx.hash(), block);
await b.write();
}
}
Expand Down Expand Up @@ -1490,11 +1485,17 @@ class TXDB {
'Reverting namestate without transaction: %x',
hash
);

const b = this.bucket.batch();
await this.applyNameUndo(b, hash);
await this.removeBlockMap(b, height);
await this.removeBlock(b, hash, height);
return b.write();

if (await this.applyNameUndo(b, hash)) {
await this.removeBlockMap(b, height);
await this.removeBlock(b, hash, height);

return b.write();
}

return null;
}

if (wtx.height === -1)
Expand Down Expand Up @@ -1882,16 +1883,6 @@ class TXDB {
// If namestate has been updated we need to write to DB
let updated = false;

// If namestate has been updated with a critical property
// we need to not only save the new namestate but also
// index the transaction (whether or not it affects our balance)
// so that we can rollback the change during reorg or rescan.
// Critical properties are anything that are asserted by this
// function the _next_ time we see this transaction:
// TRANSFER / FINALIZE: ns.transfer
// REVOKE: ns.revoked
let index = false;

for (let i = 0; i < tx.outputs.length; i++) {
const output = tx.outputs[i];
const {covenant} = output;
Expand Down Expand Up @@ -2110,7 +2101,6 @@ class TXDB {
ns.setTransfer(height);

updated = true;
index = true;

break;
}
Expand Down Expand Up @@ -2148,7 +2138,6 @@ class TXDB {
ns.setRenewals(ns.renewals + 1);

updated = true;
index = true;

break;
}
Expand All @@ -2163,7 +2152,6 @@ class TXDB {
ns.setData(null);

updated = true;
index = true;

break;
}
Expand All @@ -2184,11 +2172,17 @@ class TXDB {
if (updated) {
const undo = view.toNameUndo();

if (undo.names.length > 0)
b.put(layout.U.encode(hash), undo.encode());
// Always write the undo record as it
// serves as a marker to determine whether
// we have a processed a transaction's
// covenants before. This function is
// probably better served by its own
// special record, but that would require
// a data migration.
b.put(layout.U.encode(hash), undo.encode());
}

return {updated, index};
return updated;
}

/**
Expand Down Expand Up @@ -2240,7 +2234,7 @@ class TXDB {
const raw = await this.bucket.get(layout.U.encode(hash));

if (!raw)
return;
return false;

const undo = NameUndo.decode(raw);
const view = new CoinView();
Expand All @@ -2259,6 +2253,8 @@ class TXDB {
}

b.del(layout.U.encode(hash));

return true;
}

/**
Expand Down

0 comments on commit fcf716d

Please sign in to comment.