Skip to content

Commit

Permalink
Merge e841702 into a94ce87
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Feb 24, 2021
2 parents a94ce87 + e841702 commit 9aea544
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 7 deletions.
2 changes: 0 additions & 2 deletions lib/primitives/address.js
Expand Up @@ -542,8 +542,6 @@ class Address extends bio.Struct {
throw new Error('Object is not an address.');

if (Buffer.isBuffer(data)) {
if (data.length !== 20 && data.length !== 32)
throw new Error('Object is not an address.');
return data;
}

Expand Down
55 changes: 50 additions & 5 deletions lib/wallet/txdb.js
Expand Up @@ -1030,10 +1030,20 @@ class TXDB {

// Handle names.
if (block) {
const updated = await this.connectNames(b, tx, view, height);
const {updated, index} = 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);
}

if (updated && !state.updated())
// Always save namestate transitions,
// even if they don't affect wallet balance
await b.write();
}
}

// If this didn't update any coins,
Expand Down Expand Up @@ -1474,8 +1484,15 @@ class TXDB {
async unconfirm(hash) {
const wtx = await this.getTX(hash);

if (!wtx)
return null;
if (!wtx) {
this.logger.warning(
'Reverting namestate without transaction: %x',
hash
);
const b = this.bucket.batch();
await this.applyNameUndo(b, hash);
return b.write();
}

if (wtx.height === -1)
return null;
Expand Down Expand Up @@ -1848,6 +1865,7 @@ class TXDB {
* @param {Number} i
* @param {Path} path
* @param {Number} height
* @returns {Object}
*/

async connectNames(b, tx, view, height) {
Expand All @@ -1856,8 +1874,19 @@ class TXDB {

assert(height !== -1);

// 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 @@ -2076,6 +2105,7 @@ class TXDB {
ns.setTransfer(height);

updated = true;
index = true;

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

updated = true;
index = true;

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

updated = true;
index = true;

break;
}
Expand All @@ -2151,7 +2183,7 @@ class TXDB {
b.put(layout.U.encode(hash), undo.encode());
}

return updated;
return {updated, index};
}

/**
Expand Down Expand Up @@ -2187,6 +2219,19 @@ class TXDB {
}
}

return this.applyNameUndo(b, hash);
}

/**
* Apply namestate undo data by hash without transaction.
* Should only be called directly to undo namestate transitions
* that do not affect wallet balance like a TRANSFER for a name
* that is in the nameMap but does not involve wallet addresses.
* @param {Object} b
* @param {Hash} hash
*/

async applyNameUndo(b, hash) {
const raw = await this.bucket.get(layout.U.encode(hash));

if (!raw)
Expand Down

0 comments on commit 9aea544

Please sign in to comment.