Skip to content

Commit

Permalink
Merge 5013fff into a94ce87
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Feb 24, 2021
2 parents a94ce87 + 5013fff commit dcbe44f
Show file tree
Hide file tree
Showing 4 changed files with 573 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## unreleased

### Wallet changes

- Fixes a bug that caused rescans to fail if a name being "watched" was ever
`TRANSFER`ed. A `deepclean` plus `rescan` may be required to fix affected wallets.

### Wallet API changes

- Adds new wallet HTTP endpoint `/wallet/:id/auction` based on `POST /wallet/:id/bid`.
Expand Down
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
57 changes: 52 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,9 @@ class TXDB {
* @param {Number} i
* @param {Path} path
* @param {Number} height
* @returns {Object} out
* @returns {Boolean} out.updated
* @returns {Boolean} out.index
*/

async connectNames(b, tx, view, height) {
Expand All @@ -1856,8 +1876,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 +2107,7 @@ class TXDB {
ns.setTransfer(height);

updated = true;
index = true;

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

updated = true;
index = true;

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

updated = true;
index = true;

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

return updated;
return {updated, index};
}

/**
Expand Down Expand Up @@ -2187,6 +2221,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 dcbe44f

Please sign in to comment.